summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSadeep Madurange <sadeep@asciimx.com>2025-04-10 11:08:04 +0800
committerSadeep Madurange <sadeep@asciimx.com>2025-04-10 11:39:27 +0800
commit081e11b460f71ce99e69604e9ffe16f5d667f84b (patch)
tree28d0dc97985939c80f934bb517032b9567306b7e
parent058aac02f4428edd90bb288c2188748357550ced (diff)
downloadavr-nrf24l01-driver-081e11b460f71ce99e69604e9ffe16f5d667f84b.tar.gz
Get ding working with breadboard and limit sendto() to 32 bytes.
-rw-r--r--Ding.Makefile2
-rw-r--r--Dong.Makefile2
-rw-r--r--ding.c6
-rw-r--r--nrfm.c66
-rw-r--r--util.c10
-rw-r--r--util.h6
6 files changed, 55 insertions, 37 deletions
diff --git a/Ding.Makefile b/Ding.Makefile
index 2f31bc2..6d6801a 100644
--- a/Ding.Makefile
+++ b/Ding.Makefile
@@ -2,7 +2,7 @@ CC = avr-gcc
MCU = atmega328p
TARGET = ding
-SRC = ding.c uart.c nrfm.c
+SRC = ding.c uart.c nrfm.c util.c
OBJ = $(SRC:.c=.o)
CFLAGS = -std=gnu99
diff --git a/Dong.Makefile b/Dong.Makefile
index 4dcb942..bf03e11 100644
--- a/Dong.Makefile
+++ b/Dong.Makefile
@@ -2,7 +2,7 @@ CC = avr-gcc
MCU = atmega328p
TARGET = dong
-SRC = dong.c uart.c nrfm.c
+SRC = dong.c uart.c nrfm.c util.c
OBJ = $(SRC:.c=.o)
CFLAGS = -std=gnu99
diff --git a/ding.c b/ding.c
index 5950ddc..34ff38c 100644
--- a/ding.c
+++ b/ding.c
@@ -1,9 +1,12 @@
#include <stdint.h>
#include <string.h>
+
+#include <avr/wdt.h>
#include <util/delay.h>
#include "nrfm.h"
#include "uart.h"
+#include "util.h"
int main(void)
{
@@ -13,15 +16,16 @@ int main(void)
uint8_t rxaddr[ADDRLEN] = { 194, 178, 82 };
uint8_t txaddr[ADDRLEN] = { 194, 178, 83 };
+ wdt_init();
uart_init();
radio_init(rxaddr);
radio_print_config();
for (;;) {
radio_sendto(txaddr, s, slen);
+ wdt_reset();
_delay_ms(2000);
}
-
return 0;
}
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)))
;
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..ddfdf04
--- /dev/null
+++ b/util.c
@@ -0,0 +1,10 @@
+#include <avr/wdt.h>
+
+#include "util.h"
+
+void wdt_init(void)
+{
+ wdt_reset();
+ WDTCSR |= (1 << WDCE) | (1 << WDE);
+ WDTCSR = (1 << WDE) | (1 << WDP3) | (1 << WDP0);
+}
diff --git a/util.h b/util.h
new file mode 100644
index 0000000..75cd9ad
--- /dev/null
+++ b/util.h
@@ -0,0 +1,6 @@
+#ifndef MY_UTIL_H
+#define MY_UTIL_H
+
+void wdt_init(void);
+
+#endif