summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSadeep Madurange <sadeep@asciimx.com>2025-04-24 10:10:10 +0800
committerSadeep Madurange <sadeep@asciimx.com>2025-04-24 10:10:10 +0800
commit5dc7c2d195f7c59a7f25464506a7fd9d01190cc1 (patch)
tree6e83a0eeb131a03cb7bec0284030d7a212e925b3
parent9bce9c092a47a3a3a9028be858053f17146dad20 (diff)
downloadavr-nrf24l01-driver-5dc7c2d195f7c59a7f25464506a7fd9d01190cc1.tar.gz
wip: tx-rx not working.
-rw-r--r--Ding.Makefile3
-rw-r--r--Dong.Makefile3
-rw-r--r--ding.c35
-rw-r--r--dong.c3
-rw-r--r--nrfm.c36
5 files changed, 42 insertions, 38 deletions
diff --git a/Ding.Makefile b/Ding.Makefile
index 6d6801a..d4b7e33 100644
--- a/Ding.Makefile
+++ b/Ding.Makefile
@@ -1,5 +1,6 @@
CC = avr-gcc
MCU = atmega328p
+PORT = /dev/cuaU0
TARGET = ding
SRC = ding.c uart.c nrfm.c util.c
@@ -21,7 +22,7 @@ HEX_FLAGS += -j .text -j .data
AVRDUDE_FLAGS = -p $(MCU)
AVRDUDE_FLAGS += -c arduino
-AVRDUDE_FLAGS += -P /dev/cuaU0
+AVRDUDE_FLAGS += -P $(PORT)
AVRDUDE_FLAGS += -D -U
%.o: %.c
diff --git a/Dong.Makefile b/Dong.Makefile
index bf03e11..9226c42 100644
--- a/Dong.Makefile
+++ b/Dong.Makefile
@@ -1,5 +1,6 @@
CC = avr-gcc
MCU = atmega328p
+PORT = /dev/cuaU0
TARGET = dong
SRC = dong.c uart.c nrfm.c util.c
@@ -21,7 +22,7 @@ HEX_FLAGS += -j .text -j .data
AVRDUDE_FLAGS = -p $(MCU)
AVRDUDE_FLAGS += -c arduino
-AVRDUDE_FLAGS += -P /dev/cuaU0
+AVRDUDE_FLAGS += -P $(PORT)
AVRDUDE_FLAGS += -D -U
%.o: %.c
diff --git a/ding.c b/ding.c
index 548c618..32b088a 100644
--- a/ding.c
+++ b/ding.c
@@ -17,13 +17,8 @@
#define RX_PCMSK PCMSK2
#define RX_PCINTVEC PCINT2_vect
-#define LOCK_PIN PC5
-#define LOCK_DDR DDRC
-#define LOCK_PORT PORTC
-#define LOCK_PCIE PCIE1
-#define LOCK_PCINT PCINT13
-#define LOCK_PCMSK PCMSK1
-#define LOCK_PCINTVEC PCINT1_vect
+#define LOCK_PIN PD2
+#define UNLOCK_PIN PD3
static volatile int rxdr = 0;
static volatile int lock = 0;
@@ -38,17 +33,18 @@ static inline void init_rx(void)
static inline void init_btns(void)
{
- LOCK_DDR &= ~(1 << LOCK_PIN);
- LOCK_PORT |= (1 << LOCK_PIN);
- PCICR |= (1 << LOCK_PCIE);
- LOCK_PCMSK |= (1 << LOCK_PCINT);
+ DDRD &= ~((1 << LOCK_PIN) | (1 << UNLOCK_PIN));
+ PORTD |= ((1 << LOCK_PIN) | (1 << UNLOCK_PIN));
+
+ EICRA = 0b00000000;
+ EIMSK = (1 << INT0) | (1 << INT1);
}
static inline int is_btn_pressed(unsigned char btn)
{
- if (!((PINC >> btn) & 0x01)) {
+ if (!((PIND >> btn) & 0x01)) {
_delay_us(2000);
- return !((PINC >> btn) & 0x01);
+ return !((PIND >> btn) & 0x01);
}
return 0;
@@ -65,7 +61,6 @@ int main(void)
init_rx();
init_btns();
- //wdt_stop();
uart_init();
radio_init(rxaddr);
radio_print_config();
@@ -85,8 +80,8 @@ int main(void)
}
if (lock) {
- radio_sendto(txaddr, "SYN", 3);
lock = 0;
+ radio_sendto(txaddr, "SYN", 3);
}
}
@@ -98,8 +93,14 @@ ISR(RX_PCINTVEC)
rxdr = 1;
}
-ISR(LOCK_PCINTVEC)
+ISR(INT0_vect)
+{
+ if (is_btn_pressed(LOCK_PIN))
+ lock = 1;
+}
+
+ISR(INT1_vect)
{
- if (is_btn_pressed(LOCK_PIN))
+ if (is_btn_pressed(UNLOCK_PIN))
lock = 1;
}
diff --git a/dong.c b/dong.c
index cdf1f08..308d3ab 100644
--- a/dong.c
+++ b/dong.c
@@ -48,6 +48,9 @@ int main(void)
uart_write("INFO: ");
uart_write_line(buf);
}
+ if (strncmp(buf, "SYN", 3) == 0) {
+ radio_sendto(txaddr, "ACK", 3);
+ }
}
//_delay_ms(1000);
diff --git a/nrfm.c b/nrfm.c
index 5da4937..b57cfd6 100644
--- a/nrfm.c
+++ b/nrfm.c
@@ -15,9 +15,9 @@
#define SPI_DDR DDRB
#define SPI_PORT PORTB
-#define NRF_CE PC0
-#define NRF_CE_DDR DDRC
-#define NRF_CE_PORT PORTC
+#define NRF_CE PB1
+#define NRF_CE_DDR DDRB
+#define NRF_CE_PORT PORTB
#define NOP 0xFF
#define R_REGISTER 0x1F
@@ -148,19 +148,15 @@ static inline void flush_tx(void)
while (!(SPSR & (1 << SPIF)))
;
SPI_PORT |= (1 << SPI_SS);
-
- reset_irqs();
}
-void radio_flush_rx(void)
+static inline void flush_rx(void)
{
SPI_PORT &= ~(1 << SPI_SS);
SPDR = 0b11100010;
while (!(SPSR & (1 << SPIF)))
;
SPI_PORT |= (1 << SPI_SS);
-
- reset_irqs();
}
static inline uint8_t rx_pdlen(void)
@@ -196,7 +192,7 @@ void radio_print_config(void)
}
read_reg_bulk(0x0B, addr, ADDRLEN);
- snprintf(s, LEN(s), "\r\n\t0x0A: %d.%d.%d", addr[2], addr[1], addr[0]);
+ snprintf(s, LEN(s), "\r\n\t0x0B: %d.%d.%d", addr[2], addr[1], addr[0]);
uart_write_line(s);
}
@@ -232,10 +228,7 @@ void radio_listen(void)
enable_chip();
}
-uint8_t radio_sendto(
- const uint8_t addr[ADDRLEN],
- const char *msg,
- uint8_t n)
+uint8_t radio_sendto(const uint8_t addr[ADDRLEN], const char *msg, uint8_t n)
{
char s[4];
int i, imax;
@@ -247,6 +240,7 @@ uint8_t radio_sendto(
tx_mode();
flush_tx();
+ reset_irqs();
setaddr(0x10, addr);
setaddr(0x0A, addr);
@@ -289,6 +283,7 @@ uint8_t radio_sendto(
uart_write_line("ERROR: sendto() failed: MAX_RT");
}
+ // restore config, typically rx mode
write_reg(0x00, cfg);
enable_chip();
return txds;
@@ -304,7 +299,8 @@ uint8_t radio_recv(char *buf, uint8_t n)
pdlen = rx_pdlen();
if (pdlen == 0) {
- radio_flush_rx();
+ flush_rx();
+ reset_irqs();
uart_write_line("ERROR: PDLEN = 0, abort read");
return 0;
}
@@ -314,18 +310,19 @@ uint8_t radio_recv(char *buf, uint8_t n)
uart_write_line(s);
if (pdlen > MAXPDLEN) {
- radio_flush_rx();
+ flush_rx();
+ reset_irqs();
uart_write_line("ERROR: PDLEN > MAXPDLEN, abort read");
return 0;
}
- readmax = n < pdlen ? n : pdlen;
+ readmax = (n - 1) < pdlen ? (n - 1) : pdlen;
SPI_PORT &= ~(1 << SPI_SS);
SPDR = 0b01100001;
while (!(SPSR & (1 << SPIF)))
;
- for (readlen = 0; readlen < readmax; readlen++) {
+ for (readlen = 0; readlen <= readmax; readlen++) {
SPDR = NOP;
while (!(SPSR & (1 << SPIF)))
;
@@ -333,8 +330,9 @@ uint8_t radio_recv(char *buf, uint8_t n)
}
SPI_PORT |= (1 << SPI_SS);
- radio_flush_rx();
+ flush_rx();
+ reset_irqs();
enable_chip();
+
return readlen - 1;
}
-