diff options
| author | Sadeep Madurange <sadeep@asciimx.com> | 2025-04-24 09:39:08 +0800 |
|---|---|---|
| committer | Sadeep Madurange <sadeep@asciimx.com> | 2025-04-24 09:39:08 +0800 |
| commit | 9bce9c092a47a3a3a9028be858053f17146dad20 (patch) | |
| tree | 1fb8518021f1d8775990013cd44394b348e0e3fb | |
| parent | 917a2a4020061bb24492d7eb3f53a074263237e0 (diff) | |
| download | avr-nrf24l01-driver-9bce9c092a47a3a3a9028be858053f17146dad20.tar.gz | |
Copy the nrfm driver from the smart home project.
| -rw-r--r-- | nrfm.c | 33 | ||||
| -rw-r--r-- | nrfm.h | 4 |
2 files changed, 21 insertions, 16 deletions
@@ -15,9 +15,9 @@ #define SPI_DDR DDRB #define SPI_PORT PORTB -#define NRF_CE PC0 +#define NRF_CE PC0 #define NRF_CE_DDR DDRC -#define NRF_CE_PORT PORTC +#define NRF_CE_PORT PORTC #define NOP 0xFF #define R_REGISTER 0x1F @@ -148,15 +148,19 @@ static inline void flush_tx(void) while (!(SPSR & (1 << SPIF))) ; SPI_PORT |= (1 << SPI_SS); + + reset_irqs(); } -static inline void flush_rx(void) +void radio_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) @@ -228,7 +232,10 @@ void radio_listen(void) enable_chip(); } -void 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; @@ -240,7 +247,6 @@ void radio_sendto(const uint8_t addr[ADDRLEN], const char *msg, uint8_t n) tx_mode(); flush_tx(); - reset_irqs(); setaddr(0x10, addr); setaddr(0x0A, addr); @@ -283,9 +289,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); enable_chip(); + return txds; } uint8_t radio_recv(char *buf, uint8_t n) @@ -298,8 +304,7 @@ uint8_t radio_recv(char *buf, uint8_t n) pdlen = rx_pdlen(); if (pdlen == 0) { - flush_rx(); - reset_irqs(); + radio_flush_rx(); uart_write_line("ERROR: PDLEN = 0, abort read"); return 0; } @@ -309,19 +314,18 @@ uint8_t radio_recv(char *buf, uint8_t n) uart_write_line(s); if (pdlen > MAXPDLEN) { - flush_rx(); - reset_irqs(); + radio_flush_rx(); uart_write_line("ERROR: PDLEN > MAXPDLEN, abort read"); return 0; } - readmax = (n - 1) < pdlen ? (n - 1) : pdlen; + readmax = n < pdlen ? n : 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))) ; @@ -329,9 +333,8 @@ uint8_t radio_recv(char *buf, uint8_t n) } SPI_PORT |= (1 << SPI_SS); - flush_rx(); - reset_irqs(); + radio_flush_rx(); enable_chip(); - return readlen - 1; } + @@ -14,6 +14,8 @@ void radio_listen(void); uint8_t radio_recv(char *buf, uint8_t n); -void radio_sendto(const uint8_t addr[ADDRLEN], const char *msg, 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 */ |
