summaryrefslogtreecommitdiffstats
path: root/nrfm.c
diff options
context:
space:
mode:
Diffstat (limited to 'nrfm.c')
-rw-r--r--nrfm.c66
1 files changed, 32 insertions, 34 deletions
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)))
;