From d0f895105f41c40f9a6a000b3437ad55b1ef9bc6 Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Wed, 16 Apr 2025 17:21:15 +0800 Subject: Function to flush RX buffers. --- lock/bend.c | 11 ++++++----- lock/fend.c | 16 +++++++++------- lock/nrfm.c | 8 ++++---- lock/nrfm.h | 2 ++ 4 files changed, 21 insertions(+), 16 deletions(-) (limited to 'lock') diff --git a/lock/bend.c b/lock/bend.c index f70b6be..a9796d8 100644 --- a/lock/bend.c +++ b/lock/bend.c @@ -37,11 +37,12 @@ static char tab[] = { 'P', 'f', ':', 'B', ']', 'Y', '^', 'F', '%', 'C', 'x' }; -static uint8_t synced = 0; +static uint8_t sync = 0; +static uint16_t tablen = sizeof(tab) / sizeof(tab[0]); + static volatile uint8_t rxd = 0; static volatile uint8_t islock = 0; static volatile uint8_t isunlock = 0; -static uint16_t tablen = sizeof(tab) / sizeof(tab[0]); static inline void keygen(char *buf, uint8_t n) { @@ -133,15 +134,15 @@ int main(void) if (rxd) { n = radio_recv(buf, WDLEN); buf[n] = '\0'; - if (!synced) { + if (!sync) { xor(KEY, buf, msg, WDLEN); if (strncmp(msg, SYN, WDLEN) == 0) { keygen(key, WDLEN + 1); xor(KEY, key, buf, WDLEN); - synced = radio_sendto(txaddr, buf, WDLEN); + sync = radio_sendto(txaddr, buf, WDLEN); } } else { - synced = 0; + sync = 0; xor(key, buf, msg, WDLEN); if (strncmp(msg, LOCK, WDLEN) == 0) lock(); diff --git a/lock/fend.c b/lock/fend.c index 853fa24..340daeb 100644 --- a/lock/fend.c +++ b/lock/fend.c @@ -21,7 +21,7 @@ #define RX_PCMSK PCMSK2 #define RX_PCINTVEC PCINT2_vect -uint8_t synced = 0; +uint8_t sync = 0; static volatile uint8_t rxd = 0; static volatile uint8_t islock = 0; static volatile uint8_t isunlock = 0; @@ -61,17 +61,17 @@ int main(void) sei(); for (;;) { - if ((islock || isunlock) && !synced) { + if ((islock || isunlock) && !sync) { xor(KEY, SYN, buf, WDLEN); do { - synced = radio_sendto(txaddr, buf, WDLEN); + sync = radio_sendto(txaddr, buf, WDLEN); _delay_ms(10); - } while (!synced); - synced = 1; + } while (!sync); + sync = 1; } if (rxd) { - if (synced) { + if (sync) { n = radio_recv(buf, WDLEN); buf[n] = '\0'; xor(KEY, buf, key, WDLEN); @@ -83,7 +83,9 @@ int main(void) xor(key, UNLOCK, buf, WDLEN); } radio_sendto(txaddr, buf, WDLEN); - synced = 0; + sync = 0; + } else { + radio_flush_rx(); } rxd = 0; } diff --git a/lock/nrfm.c b/lock/nrfm.c index 560609c..7630453 100644 --- a/lock/nrfm.c +++ b/lock/nrfm.c @@ -152,7 +152,7 @@ static inline void flush_tx(void) reset_irqs(); } -static inline void flush_rx(void) +void radio_flush_rx(void) { SPI_PORT &= ~(1 << SPI_SS); SPDR = 0b11100010; @@ -304,7 +304,7 @@ uint8_t radio_recv(char *buf, uint8_t n) pdlen = rx_pdlen(); if (pdlen == 0) { - flush_rx(); + radio_flush_rx(); uart_write_line("ERROR: PDLEN = 0, abort read"); return 0; } @@ -314,7 +314,7 @@ uint8_t radio_recv(char *buf, uint8_t n) uart_write_line(s); if (pdlen > MAXPDLEN) { - flush_rx(); + radio_flush_rx(); uart_write_line("ERROR: PDLEN > MAXPDLEN, abort read"); return 0; } @@ -333,7 +333,7 @@ uint8_t radio_recv(char *buf, uint8_t n) } SPI_PORT |= (1 << SPI_SS); - flush_rx(); + radio_flush_rx(); enable_chip(); return readlen - 1; } diff --git a/lock/nrfm.h b/lock/nrfm.h index 5f33e86..7c38031 100644 --- a/lock/nrfm.h +++ b/lock/nrfm.h @@ -14,6 +14,8 @@ void radio_listen(void); uint8_t radio_recv(char *buf, uint8_t n); +void radio_flush_rx(void); + uint8_t radio_sendto(const uint8_t addr[ADDRLEN], const char *msg, uint8_t n); #endif /* NRFM_H */ -- cgit v1.2.3