From 52112f19821f6f667c51f3ac2c68da41365e0b43 Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Fri, 11 Apr 2025 13:25:11 +0800 Subject: wip: some packets are being exchanged, after about 3 packets they fail (WDT) --- ding.c | 43 ++++++++++++++++++++++++++++++++++++++----- dong.c | 12 +++++++++--- nrfm.c | 35 +++++++++++++++++++++++++++-------- util.c | 8 ++++++++ util.h | 2 ++ 5 files changed, 84 insertions(+), 16 deletions(-) diff --git a/ding.c b/ding.c index 34ff38c..2bf9fe6 100644 --- a/ding.c +++ b/ding.c @@ -1,6 +1,7 @@ #include #include +#include #include #include @@ -8,24 +9,56 @@ #include "uart.h" #include "util.h" +#define RX_PIN PD7 +#define RX_DDR DDRD +#define RX_PORT PORTD +#define RX_PCIE PCIE2 +#define RX_PCINT PCINT23 +#define RX_PCMSK PCMSK2 +#define RX_PCINTVEC PCINT2_vect + +static volatile int rxdr = 0; + int main(void) { - const char *s = "hello world!"; - uint8_t slen = strlen(s); + uint8_t n; + char buf[MAXPDLEN + 1]; uint8_t rxaddr[ADDRLEN] = { 194, 178, 82 }; uint8_t txaddr[ADDRLEN] = { 194, 178, 83 }; - wdt_init(); + //wdt_stop(); uart_init(); radio_init(rxaddr); radio_print_config(); + sei(); + radio_listen(); + + _delay_ms(2000); + radio_sendto(txaddr, "SYN", 3); + for (;;) { - radio_sendto(txaddr, s, slen); - wdt_reset(); + if (rxdr) { + n = radio_recv(buf, MAXPDLEN); + buf[n] = '\0'; + rxdr = 0; + if (strncmp(buf, "ACK", 3) == 0) { + uart_write("INFO: "); + uart_write_line(buf); + } + } else { + uart_write_line("No IRQ"); + } + _delay_ms(2000); + radio_sendto(txaddr, "SYN", 3); } return 0; } + +ISR(RX_PCINTVEC) +{ + rxdr = 1; +} diff --git a/dong.c b/dong.c index 1ef872f..fbe4b19 100644 --- a/dong.c +++ b/dong.c @@ -1,10 +1,13 @@ #include #include + #include +#include #include #include "nrfm.h" #include "uart.h" +#include "util.h" #define RX_PIN PD7 #define RX_DDR DDRD @@ -22,12 +25,14 @@ int main(void) char buf[MAXPDLEN + 1]; uint8_t rxaddr[] = { 194, 178, 83 }; + uint8_t txaddr[] = { 194, 178, 82 }; RX_DDR &= ~(1 << RX_PIN); RX_PORT |= (1 << RX_PIN); PCICR |= (1 << RX_PCIE); RX_PCMSK |= (1 << RX_PCINT); + wdt_stop(); uart_init(); radio_init(rxaddr); radio_print_config(); @@ -37,18 +42,19 @@ int main(void) for (;;) { if (rxdr) { - uart_write_line("IRQ recv, reading data"); n = radio_recv(buf, MAXPDLEN); buf[n] = '\0'; rxdr = 0; - if (n > 0) { + if (strncmp(buf, "SYN", 3) == 0) { uart_write("INFO: "); uart_write_line(buf); + radio_sendto(txaddr, "ACK", 3); } } else { uart_write_line("No IRQ"); - _delay_ms(2000); } + + _delay_ms(1000); } return 0; diff --git a/nrfm.c b/nrfm.c index 84f790a..de9d132 100644 --- a/nrfm.c +++ b/nrfm.c @@ -79,6 +79,22 @@ static inline void read_reg_bulk(uint8_t reg, uint8_t *data, uint8_t n) SPI_PORT |= (1 << SPI_SS); } +static inline void write_reg_bulk(uint8_t reg, const uint8_t *data, uint8_t n) +{ + int i; + + SPI_PORT &= ~(1 << SPI_SS); + SPDR = (reg & 0x1F) | W_REGISTER; + while (!(SPSR & (1 << SPIF))) + ; + for (i = 0; i < n; i++) { + SPDR = data[i]; + while (!(SPSR & (1 << SPIF))) + ; + } + SPI_PORT |= (1 << SPI_SS); +} + static inline void setaddr(uint8_t reg, const uint8_t addr[ADDRLEN]) { int i; @@ -221,11 +237,18 @@ void radio_init(const uint8_t rxaddr[ADDRLEN]) setaddr(0x0A, rxaddr); } +void radio_listen(void) +{ + disable_chip(); + rx_mode(); + enable_chip(); +} + void radio_sendto(const uint8_t addr[ADDRLEN], const char *msg, uint8_t n) { char s[4]; int i, imax; - uint8_t cfg, rv, maxrt, txds; + uint8_t cfg, rv, maxrt, txds, rxaddr[ADDRLEN]; disable_chip(); @@ -235,6 +258,7 @@ void radio_sendto(const uint8_t addr[ADDRLEN], const char *msg, uint8_t n) flush_tx(); reset_irqs(); + read_reg_bulk(0x0A, rxaddr, ADDRLEN); setaddr(0x10, addr); setaddr(0x0A, addr); @@ -276,14 +300,9 @@ void radio_sendto(const uint8_t addr[ADDRLEN], const char *msg, uint8_t n) uart_write_line("ERROR: sendto() failed: MAX_RT"); } + // restore config, typically rx mode write_reg(0x00, cfg); - _delay_ms(MODCHG_DELAY_MS); -} - -void radio_listen(void) -{ - disable_chip(); - rx_mode(); + write_reg_bulk(0x0A, rxaddr, ADDRLEN); enable_chip(); } diff --git a/util.c b/util.c index ddfdf04..fa208a5 100644 --- a/util.c +++ b/util.c @@ -8,3 +8,11 @@ void wdt_init(void) WDTCSR |= (1 << WDCE) | (1 << WDE); WDTCSR = (1 << WDE) | (1 << WDP3) | (1 << WDP0); } + +void wdt_stop(void) +{ + wdt_reset(); + MCUSR &= ~(1<