From de096ea780245c8beb748839120e5669fe37d457 Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Wed, 9 Apr 2025 19:31:30 +0800 Subject: Rename and fix pdlen read. --- Ding.Makefile | 44 +++++++++++++++++++++++++++++++++++++++++++ Dong.Makefile | 44 +++++++++++++++++++++++++++++++++++++++++++ Recv.Makefile | 44 ------------------------------------------- Send.Makefile | 44 ------------------------------------------- ding.c | 27 +++++++++++++++++++++++++++ dong.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ nrfm.c | 15 ++++++++++++--- recv.c | 60 ----------------------------------------------------------- send.c | 27 --------------------------- 9 files changed, 187 insertions(+), 178 deletions(-) create mode 100644 Ding.Makefile create mode 100644 Dong.Makefile delete mode 100644 Recv.Makefile delete mode 100644 Send.Makefile create mode 100644 ding.c create mode 100644 dong.c delete mode 100644 recv.c delete mode 100644 send.c diff --git a/Ding.Makefile b/Ding.Makefile new file mode 100644 index 0000000..2f31bc2 --- /dev/null +++ b/Ding.Makefile @@ -0,0 +1,44 @@ +CC = avr-gcc +MCU = atmega328p +TARGET = ding + +SRC = ding.c uart.c nrfm.c +OBJ = $(SRC:.c=.o) + +CFLAGS = -std=gnu99 +CFLAGS += -Os +CFLAGS += -Wall +CFLAGS += -mmcu=$(MCU) +CFLAGS += -DBAUD=115200 +CFLAGS += -DF_CPU=16000000UL +CFLAGS += -ffunction-sections -fdata-sections + +LDFLAGS = -mmcu=$(MCU) +LDFLAGS += -Wl,--gc-sections + +HEX_FLAGS = -O ihex +HEX_FLAGS += -j .text -j .data + +AVRDUDE_FLAGS = -p $(MCU) +AVRDUDE_FLAGS += -c arduino +AVRDUDE_FLAGS += -P /dev/cuaU0 +AVRDUDE_FLAGS += -D -U + +%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< + +elf: $(OBJ) + $(CC) $(LDFLAGS) $(OBJ) -o $(TARGET).elf + +hex: elf + avr-objcopy $(HEX_FLAGS) $(TARGET).elf $(TARGET).hex + +upload: hex + avrdude $(AVRDUDE_FLAGS) flash:w:$(TARGET).hex:i + +.PHONY: clean + +clean: + rm *.o *.elf *.hex + + diff --git a/Dong.Makefile b/Dong.Makefile new file mode 100644 index 0000000..4dcb942 --- /dev/null +++ b/Dong.Makefile @@ -0,0 +1,44 @@ +CC = avr-gcc +MCU = atmega328p +TARGET = dong + +SRC = dong.c uart.c nrfm.c +OBJ = $(SRC:.c=.o) + +CFLAGS = -std=gnu99 +CFLAGS += -Os +CFLAGS += -Wall +CFLAGS += -mmcu=$(MCU) +CFLAGS += -DBAUD=115200 +CFLAGS += -DF_CPU=16000000UL +CFLAGS += -ffunction-sections -fdata-sections + +LDFLAGS = -mmcu=$(MCU) +LDFLAGS += -Wl,--gc-sections + +HEX_FLAGS = -O ihex +HEX_FLAGS += -j .text -j .data + +AVRDUDE_FLAGS = -p $(MCU) +AVRDUDE_FLAGS += -c arduino +AVRDUDE_FLAGS += -P /dev/cuaU0 +AVRDUDE_FLAGS += -D -U + +%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< + +elf: $(OBJ) + $(CC) $(LDFLAGS) $(OBJ) -o $(TARGET).elf + +hex: elf + avr-objcopy $(HEX_FLAGS) $(TARGET).elf $(TARGET).hex + +upload: hex + avrdude $(AVRDUDE_FLAGS) flash:w:$(TARGET).hex:i + +.PHONY: clean + +clean: + rm *.o *.elf *.hex + + diff --git a/Recv.Makefile b/Recv.Makefile deleted file mode 100644 index f77be2f..0000000 --- a/Recv.Makefile +++ /dev/null @@ -1,44 +0,0 @@ -CC = avr-gcc -MCU = atmega328p -TARGET = app - -SRC = recv.c uart.c nrfm.c -OBJ = $(SRC:.c=.o) - -CFLAGS = -std=gnu99 -CFLAGS += -Os -CFLAGS += -Wall -CFLAGS += -mmcu=$(MCU) -CFLAGS += -DBAUD=115200 -CFLAGS += -DF_CPU=16000000UL -CFLAGS += -ffunction-sections -fdata-sections - -LDFLAGS = -mmcu=$(MCU) -LDFLAGS += -Wl,--gc-sections - -HEX_FLAGS = -O ihex -HEX_FLAGS += -j .text -j .data - -AVRDUDE_FLAGS = -p $(MCU) -AVRDUDE_FLAGS += -c arduino -AVRDUDE_FLAGS += -P /dev/cuaU0 -AVRDUDE_FLAGS += -D -U - -%.o: %.c - $(CC) $(CFLAGS) -c -o $@ $< - -elf: $(OBJ) - $(CC) $(LDFLAGS) $(OBJ) -o $(TARGET).elf - -hex: elf - avr-objcopy $(HEX_FLAGS) $(TARGET).elf $(TARGET).hex - -upload: hex - avrdude $(AVRDUDE_FLAGS) flash:w:$(TARGET).hex:i - -.PHONY: clean - -clean: - rm *.o *.elf *.hex - - diff --git a/Send.Makefile b/Send.Makefile deleted file mode 100644 index 941fa0f..0000000 --- a/Send.Makefile +++ /dev/null @@ -1,44 +0,0 @@ -CC = avr-gcc -MCU = atmega328p -TARGET = sender - -SRC = send.c uart.c nrfm.c -OBJ = $(SRC:.c=.o) - -CFLAGS = -std=gnu99 -CFLAGS += -Os -CFLAGS += -Wall -CFLAGS += -mmcu=$(MCU) -CFLAGS += -DBAUD=115200 -CFLAGS += -DF_CPU=16000000UL -CFLAGS += -ffunction-sections -fdata-sections - -LDFLAGS = -mmcu=$(MCU) -LDFLAGS += -Wl,--gc-sections - -HEX_FLAGS = -O ihex -HEX_FLAGS += -j .text -j .data - -AVRDUDE_FLAGS = -p $(MCU) -AVRDUDE_FLAGS += -c arduino -AVRDUDE_FLAGS += -P /dev/cuaU0 -AVRDUDE_FLAGS += -D -U - -%.o: %.c - $(CC) $(CFLAGS) -c -o $@ $< - -elf: $(OBJ) - $(CC) $(LDFLAGS) $(OBJ) -o $(TARGET).elf - -hex: elf - avr-objcopy $(HEX_FLAGS) $(TARGET).elf $(TARGET).hex - -upload: hex - avrdude $(AVRDUDE_FLAGS) flash:w:$(TARGET).hex:i - -.PHONY: clean - -clean: - rm *.o *.elf *.hex - - diff --git a/ding.c b/ding.c new file mode 100644 index 0000000..5950ddc --- /dev/null +++ b/ding.c @@ -0,0 +1,27 @@ +#include +#include +#include + +#include "nrfm.h" +#include "uart.h" + +int main(void) +{ + const char *s = "hello world!"; + uint8_t slen = strlen(s); + + uint8_t rxaddr[ADDRLEN] = { 194, 178, 82 }; + uint8_t txaddr[ADDRLEN] = { 194, 178, 83 }; + + uart_init(); + radio_init(rxaddr); + radio_print_config(); + + for (;;) { + radio_sendto(txaddr, s, slen); + _delay_ms(2000); + } + + + return 0; +} diff --git a/dong.c b/dong.c new file mode 100644 index 0000000..70006dc --- /dev/null +++ b/dong.c @@ -0,0 +1,60 @@ +#include +#include +#include +#include + +#include "nrfm.h" +#include "uart.h" + +#define RX_PIN PD7 +#define RX_DDR DDRD +#define RX_PORT PORTD +#define RX_PCIE PCIE2 +#define RX_PCINT PCINT23 +#define RX_PCMSK PCMSK2 +#define RX_PCINTVEC PCINT2_vect + +static int rxdr = 0; + +int main(void) +{ + uint8_t n; + char buf[MAXPDLEN + 1]; + + uint8_t rxaddr[] = { 194, 178, 83 }; + + RX_DDR &= ~(1 << RX_PIN); + RX_PORT |= (1 << RX_PIN); + PCICR |= (1 << RX_PCIE); + RX_PCMSK |= (1 << RX_PCINT); + + uart_init(); + radio_init(rxaddr); + radio_print_config(); + + sei(); + radio_listen(); + + for (;;) { + if (rxdr) { + uart_write_line("IRQ recv, reading data"); + n = radio_recv(buf, MAXPDLEN); + buf[n] = '\0'; + rxdr = 0; + if (n > 0) { + uart_write("INFO: "); + uart_write_line(buf); + } + } else { + uart_write_line("No IRQ"); + _delay_ms(2000); + } + } + + return 0; +} + +ISR(RX_PCINTVEC) +{ + rxdr = 1; +} diff --git a/nrfm.c b/nrfm.c index afbb1d9..1c3444a 100644 --- a/nrfm.c +++ b/nrfm.c @@ -163,6 +163,9 @@ static inline uint8_t rx_pdlen(void) { SPI_PORT &= ~(1 << SPI_SS); SPDR = 0b01100000; + while (!(SPSR & (1 << SPIF))) + ; + SPDR = NOP; while (!(SPSR & (1 << SPIF))) ; SPI_PORT |= (1 << SPI_SS); @@ -287,8 +290,8 @@ void radio_listen(void) uint8_t radio_recv(char *buf, uint8_t n) { - char s[5]; - uint8_t rxdr, readlen, pdlen, maxlen; + char s[3]; + uint8_t readlen, pdlen, maxlen; pdlen = 0; disable_chip(); @@ -300,8 +303,14 @@ uint8_t radio_recv(char *buf, uint8_t n) uart_write_line("ERROR: PDLEN = 0, abort read"); return 0; } + + s[2] = '\0'; + pdlen = 0; + pdlen = rx_pdlen(); + itoa(pdlen, s, 10); + uart_write("DEBUG: PDLEN="); + uart_write_line(s); - // for some reason, PDLEN reads as 64 causing the below check to fail //if (pdlen > MAXPDLEN) { // flush_rx(); // reset_irqs(); diff --git a/recv.c b/recv.c deleted file mode 100644 index 70006dc..0000000 --- a/recv.c +++ /dev/null @@ -1,60 +0,0 @@ -#include -#include -#include -#include - -#include "nrfm.h" -#include "uart.h" - -#define RX_PIN PD7 -#define RX_DDR DDRD -#define RX_PORT PORTD -#define RX_PCIE PCIE2 -#define RX_PCINT PCINT23 -#define RX_PCMSK PCMSK2 -#define RX_PCINTVEC PCINT2_vect - -static int rxdr = 0; - -int main(void) -{ - uint8_t n; - char buf[MAXPDLEN + 1]; - - uint8_t rxaddr[] = { 194, 178, 83 }; - - RX_DDR &= ~(1 << RX_PIN); - RX_PORT |= (1 << RX_PIN); - PCICR |= (1 << RX_PCIE); - RX_PCMSK |= (1 << RX_PCINT); - - uart_init(); - radio_init(rxaddr); - radio_print_config(); - - sei(); - radio_listen(); - - for (;;) { - if (rxdr) { - uart_write_line("IRQ recv, reading data"); - n = radio_recv(buf, MAXPDLEN); - buf[n] = '\0'; - rxdr = 0; - if (n > 0) { - uart_write("INFO: "); - uart_write_line(buf); - } - } else { - uart_write_line("No IRQ"); - _delay_ms(2000); - } - } - - return 0; -} - -ISR(RX_PCINTVEC) -{ - rxdr = 1; -} diff --git a/send.c b/send.c deleted file mode 100644 index 5950ddc..0000000 --- a/send.c +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include -#include - -#include "nrfm.h" -#include "uart.h" - -int main(void) -{ - const char *s = "hello world!"; - uint8_t slen = strlen(s); - - uint8_t rxaddr[ADDRLEN] = { 194, 178, 82 }; - uint8_t txaddr[ADDRLEN] = { 194, 178, 83 }; - - uart_init(); - radio_init(rxaddr); - radio_print_config(); - - for (;;) { - radio_sendto(txaddr, s, slen); - _delay_ms(2000); - } - - - return 0; -} -- cgit v1.2.3