From 081e11b460f71ce99e69604e9ffe16f5d667f84b Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Thu, 10 Apr 2025 11:08:04 +0800 Subject: Get ding working with breadboard and limit sendto() to 32 bytes. --- nrfm.c | 66 ++++++++++++++++++++++++++++++++---------------------------------- 1 file changed, 32 insertions(+), 34 deletions(-) (limited to 'nrfm.c') diff --git a/nrfm.c b/nrfm.c index 885155a..7ec45df 100644 --- a/nrfm.c +++ b/nrfm.c @@ -224,12 +224,12 @@ void radio_init(const uint8_t rxaddr[ADDRLEN]) void radio_sendto(const uint8_t addr[ADDRLEN], const char *msg, uint8_t n) { char s[4]; - uint8_t cfg; - int i, j, jmax; - uint8_t rv, maxrt, txds; + int i, imax; + uint8_t cfg, rv, maxrt, txds; disable_chip(); + imax = n < MAXPDLEN ? n : MAXPDLEN; cfg = read_reg(0x00); tx_mode(); @@ -246,36 +246,32 @@ void radio_sendto(const uint8_t addr[ADDRLEN], const char *msg, uint8_t n) uart_write("."); uart_write_line(itoa(addr[2], s, 10)); - for (i = 0; i < n; i += MAXPDLEN) { - SPI_PORT &= ~(1 << SPI_SS); - SPDR = 0b10100000; + SPI_PORT &= ~(1 << SPI_SS); + SPDR = 0b10100000; + while (!(SPSR & (1 << SPIF))) + ; + for (i = 0; i < imax; i++) { + SPDR = msg[i]; while (!(SPSR & (1 << SPIF))) - ; - jmax = i + MAXPDLEN; - for (j = i; j < jmax; j++) { - SPDR = msg[j]; - while (!(SPSR & (1 << SPIF))) - ; - } - SPI_PORT |= (1 << SPI_SS); - - enable_chip(); - _delay_us(12); - disable_chip(); - - txds = 0, maxrt = 0; - do { - rv = read_reg(0x07); - txds = rv & (1 << 5); - maxrt = rv & (1 << 4); - } while (txds == 0 && maxrt == 0); - - if (txds) - uart_write_line("DEBUG: packet sent"); - else if (maxrt) { - uart_write_line("ERROR: sendto() failed: MAX_RT"); - break; - } + ; + } + SPI_PORT |= (1 << SPI_SS); + + enable_chip(); + _delay_us(12); + disable_chip(); + + txds = 0, maxrt = 0; + do { + rv = read_reg(0x07); + txds = rv & (1 << 5); + maxrt = rv & (1 << 4); + } while (txds == 0 && maxrt == 0); + + if (txds) + uart_write_line("DEBUG: packet sent"); + else if (maxrt) { + uart_write_line("ERROR: sendto() failed: MAX_RT"); } write_reg(0x00, cfg); @@ -291,7 +287,7 @@ void radio_listen(void) uint8_t radio_recv(char *buf, uint8_t n) { char s[3]; - int readlen, pdlen; + int readlen, pdlen, readmax; pdlen = 0; disable_chip(); @@ -315,11 +311,13 @@ uint8_t radio_recv(char *buf, uint8_t n) return 0; } + readmax = (n - 1) < pdlen ? (n - 1) : pdlen; + SPI_PORT &= ~(1 << SPI_SS); SPDR = 0b01100001; while (!(SPSR & (1 << SPIF))) ; - for (readlen = 0; readlen < pdlen; readlen++) { + for (readlen = 0; readlen <= readmax; readlen++) { SPDR = NOP; while (!(SPSR & (1 << SPIF))) ; -- cgit v1.2.3