From 2a5eaad1bcac0f0ff61bd82b6427932cb4d03872 Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Mon, 7 Apr 2025 12:50:46 +0800 Subject: Copy working NRFM code. --- lock/Back.Makefile | 44 +++++++ lock/Frnt.Makefile | 44 +++++++ lock/back.c | 64 ++++++++++ lock/frnt.c | 28 +++++ lock/nrfm.c | 333 +++++++++++++++++++++++++++++++++++++++++++++++++ lock/nrfm.h | 19 +++ lock/uart.c | 37 ++++++ lock/uart.h | 8 ++ nrf24l01/Recv.Makefile | 43 ------- nrf24l01/Send.Makefile | 44 ------- nrf24l01/nrf24l01.c | 134 -------------------- nrf24l01/radio.h | 19 --- nrf24l01/recv.c | 64 ---------- nrf24l01/send.c | 34 ----- nrf24l01/uart.c | 33 ----- nrf24l01/uart.h | 8 -- rf69/Recv.Makefile | 43 ------- rf69/Send.Makefile | 44 ------- rf69/radio.c | 212 ------------------------------- rf69/radio.h | 22 ---- rf69/recv.c | 64 ---------- rf69/send.c | 34 ----- rf69/serial.c | 33 ----- rf69/serial.h | 8 -- 24 files changed, 577 insertions(+), 839 deletions(-) create mode 100644 lock/Back.Makefile create mode 100644 lock/Frnt.Makefile create mode 100644 lock/back.c create mode 100644 lock/frnt.c create mode 100644 lock/nrfm.c create mode 100644 lock/nrfm.h create mode 100644 lock/uart.c create mode 100644 lock/uart.h delete mode 100644 nrf24l01/Recv.Makefile delete mode 100644 nrf24l01/Send.Makefile delete mode 100644 nrf24l01/nrf24l01.c delete mode 100644 nrf24l01/radio.h delete mode 100644 nrf24l01/recv.c delete mode 100644 nrf24l01/send.c delete mode 100644 nrf24l01/uart.c delete mode 100644 nrf24l01/uart.h delete mode 100644 rf69/Recv.Makefile delete mode 100644 rf69/Send.Makefile delete mode 100644 rf69/radio.c delete mode 100644 rf69/radio.h delete mode 100644 rf69/recv.c delete mode 100644 rf69/send.c delete mode 100644 rf69/serial.c delete mode 100644 rf69/serial.h diff --git a/lock/Back.Makefile b/lock/Back.Makefile new file mode 100644 index 0000000..00e72fd --- /dev/null +++ b/lock/Back.Makefile @@ -0,0 +1,44 @@ +CC = avr-gcc +MCU = atmega328p +TARGET = sender + +SRC = back.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/lock/Frnt.Makefile b/lock/Frnt.Makefile new file mode 100644 index 0000000..908f4d8 --- /dev/null +++ b/lock/Frnt.Makefile @@ -0,0 +1,44 @@ +CC = avr-gcc +MCU = atmega328p +TARGET = app + +SRC = frnt.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/lock/back.c b/lock/back.c new file mode 100644 index 0000000..c5dc35e --- /dev/null +++ b/lock/back.c @@ -0,0 +1,64 @@ +#include +#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 s[2]; + 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) { + n = radio_recv(buf, MAXPDLEN); + buf[n] = '\0'; + uart_write("Received data: "); + uart_write(itoa(n, s, 10)); + uart_write_line(" bytes"); + 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/lock/frnt.c b/lock/frnt.c new file mode 100644 index 0000000..d0cfe74 --- /dev/null +++ b/lock/frnt.c @@ -0,0 +1,28 @@ +#include +#include +#include + +#include "nrfm.h" +#include "uart.h" + +int main(void) +{ + const char *s = "hello world!"; + //const char *s = "hello how are you doing? This is supposed to be a very long message!"; + 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/lock/nrfm.c b/lock/nrfm.c new file mode 100644 index 0000000..00127ee --- /dev/null +++ b/lock/nrfm.c @@ -0,0 +1,333 @@ +#include +#include + +#include +#include +#include + +#include "nrfm.h" +#include "uart.h" + +#define SPI_SS PB2 +#define SPI_SCK PB5 +#define SPI_MISO PB4 +#define SPI_MOSI PB3 +#define SPI_DDR DDRB +#define SPI_PORT PORTB + +#define NRF_CE PB1 +#define NRF_CE_DDR DDRB +#define NRF_CE_PORT PORTB + +#define NOP 0xFF +#define R_REGISTER 0x1F +#define W_REGISTER 0x20 + +#define PWR_UP 1 +#define PRIM_RX 0 + +#define MODCHG_DELAY_MS 5 + +#define LEN(a) (sizeof(a) / sizeof(a[0])) + +const char *bittab[16] = { + [ 0] = "0000", [ 1] = "0001", [ 2] = "0010", [ 3] = "0011", + [ 4] = "0100", [ 5] = "0101", [ 6] = "0110", [ 7] = "0111", + [ 8] = "1000", [ 9] = "1001", [10] = "1010", [11] = "1011", + [12] = "1100", [13] = "1101", [14] = "1110", [15] = "1111", +}; + +static inline uint8_t read_reg(uint8_t reg) +{ + SPI_PORT &= ~(1 << SPI_SS); + SPDR = reg & R_REGISTER; + while (!(SPSR & (1 << SPIF))) + ; + SPDR = NOP; + while (!(SPSR & (1 << SPIF))) + ; + SPI_PORT |= (1 << SPI_SS); + return SPDR; +} + +static inline void write_reg(uint8_t reg, uint8_t val) +{ + SPI_PORT &= ~(1 << SPI_SS); + SPDR = (reg & 0x1F) | W_REGISTER; + while (!(SPSR & (1 << SPIF))) + ; + SPDR = val; + while (!(SPSR & (1 << SPIF))) + ; + SPI_PORT |= (1 << SPI_SS); +} + +static inline void read_reg_bulk(uint8_t reg, uint8_t *data, uint8_t n) +{ + uint8_t i; + + SPI_PORT &= ~(1 << SPI_SS); + SPDR = reg & R_REGISTER; + while (!(SPSR & (1 << SPIF))) + ; + for (i = 0; i < n; i++) { + SPDR = NOP; + while (!(SPSR & (1 << SPIF))) + ; + data[i] = SPDR; + } + SPI_PORT |= (1 << SPI_SS); +} + +static inline void setaddr(uint8_t reg, const uint8_t addr[ADDRLEN]) +{ + int i; + + SPI_PORT &= ~(1 << SPI_SS); + SPDR = (reg & 0x1F) | W_REGISTER; + while (!(SPSR & (1 << SPIF))) + ; + for (i = ADDRLEN - 1; i >= 0; i--) { + SPDR = addr[i]; + while (!(SPSR & (1 << SPIF))) + ; + } + SPI_PORT |= (1 << SPI_SS); +} + +static inline void reset_irqs(void) +{ + uint8_t rv; + + rv = read_reg(0x07); + if (rv != 0b00001110) + write_reg(0x07, 0b01111110); +} + +static inline void tx_mode(void) +{ + uint8_t rv; + + rv = read_reg(0x00); + if ((rv & 0x03) != 0x02) { + rv |= (1 << PWR_UP); + rv &= ~(1 << PRIM_RX); + write_reg(0x00, rv); + _delay_ms(MODCHG_DELAY_MS); + } +} + +static inline void rx_mode(void) +{ + uint8_t rv; + + rv = read_reg(0x00); + if ((rv & 0x03) != 0x02) { + rv |= (1 << PWR_UP); + rv |= (1 << PRIM_RX); + write_reg(0x00, rv); + _delay_ms(MODCHG_DELAY_MS); + } +} + +static inline void enable_chip(void) +{ + NRF_CE_PORT |= (1 << NRF_CE); + _delay_us(130); +} + +static inline void disable_chip(void) +{ + NRF_CE_PORT &= ~(1 << NRF_CE); +} + +static inline void flush_tx(void) +{ + SPI_PORT &= ~(1 << SPI_SS); + SPDR = 0b11100001; + while (!(SPSR & (1 << SPIF))) + ; + SPI_PORT |= (1 << SPI_SS); +} + +static inline void flush_rx(void) +{ + SPI_PORT &= ~(1 << SPI_SS); + SPDR = 0b11100010; + while (!(SPSR & (1 << SPIF))) + ; + SPI_PORT |= (1 << SPI_SS); +} + +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); + return SPDR; +} + +void radio_print_config(void) +{ + char s[22]; + uint8_t i, rv, addr[ADDRLEN]; + + uint8_t regs[] = { + 0x00, 0x01, 0x02, 0x03, 0x04, + 0x05, 0x06, 0x07, 0x11, 0x1C, 0x1D + }; + + uart_write_line("NRF24L01 config:"); + + for (i = 0; i < LEN(regs); i++) { + rv = read_reg(regs[i]); + snprintf(s, LEN(s), "\t0x%02X: 0x%02X %s%s", + regs[i], rv, bittab[rv >> 4], bittab[rv & 0x0F]); + uart_write_line(s); + } + + read_reg_bulk(0x0A, addr, ADDRLEN); + snprintf(s, LEN(s), "\r\n\t0x0A: %d.%d.%d", addr[2], addr[1], addr[0]); + uart_write_line(s); +} + +void radio_init(const uint8_t rxaddr[ADDRLEN]) +{ + SPI_DDR |= (1 << SPI_SS) | (1 << SPI_SCK) | (1 << SPI_MOSI); + SPI_PORT |= (1 << SPI_SS); + SPCR |= (1 << SPE) | (1 << MSTR); + + NRF_CE_DDR |= (1 << NRF_CE); + NRF_CE_PORT &= ~(1 << NRF_CE); + + _delay_ms(110); /* power on reset delay */ + + write_reg(0x00, 0b00111100); /* use 2-byte CRC, enable only the rx interrupt */ + write_reg(0x01, 0b00111111); /* enable auto ack on all pipes */ + write_reg(0x02, 0b00000001); /* enable rx address on pipe 0 */ + write_reg(0x03, 0b00000001); /* set address width to 3 bytes */ + write_reg(0x04, 0b00101111); /* 750uS retransmission delay, 15 tries */ + write_reg(0x05, 0b01110011); /* use 2.515GHz channel */ + write_reg(0x06, 0b00001110); /* set data rate to 1Mbps */ + write_reg(0x1D, 0b00000100); /* enable dynamic payload length */ + write_reg(0x1C, 0b00111111); /* enable dynamic payload length for all pipes */ + + reset_irqs(); + setaddr(0x0A, rxaddr); +} + +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; + + disable_chip(); + + cfg = read_reg(0x00); + + tx_mode(); + reset_irqs(); + flush_tx(); + + setaddr(0x10, addr); + setaddr(0x0A, addr); + + uart_write("DEBUG: sending to "); + uart_write(itoa(addr[0], s, 10)); + uart_write("."); + uart_write(itoa(addr[1], s, 10)); + uart_write("."); + uart_write_line(itoa(addr[2], s, 10)); + + for (i = 0; i < n; i += MAXPDLEN) { + SPI_PORT &= ~(1 << SPI_SS); + SPDR = 0b10100000; + 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; + } + } + + write_reg(0x00, cfg); + _delay_ms(MODCHG_DELAY_MS); +} + +void radio_listen(void) +{ + rx_mode(); + enable_chip(); +} + +uint8_t radio_recv(char *buf, uint8_t n) +{ + char s[5]; + uint8_t rxdr, readlen, pdlen, maxlen; + + pdlen = 0; + disable_chip(); + + pdlen = rx_pdlen(); + if (pdlen == 0) { + flush_rx(); + reset_irqs(); + uart_write_line("ERROR: PDLEN = 0, abort read"); + return 0; + } + + if (pdlen > MAXPDLEN) { + flush_rx(); + reset_irqs(); + uart_write_line("ERROR: PDLEN > MAXPDLEN, abort read"); + return 0; + } + + maxlen = pdlen < n ? pdlen : n; + + SPI_PORT &= ~(1 << SPI_SS); + SPDR = 0b01100001; + while (!(SPSR & (1 << SPIF))) + ; + for (readlen = 0; readlen < maxlen && SPDR; readlen++) { + SPDR = NOP; + while (!(SPSR & (1 << SPIF))) + ; + buf[readlen] = SPDR; + } + SPI_PORT |= (1 << SPI_SS); + + flush_rx(); + reset_irqs(); + enable_chip(); + + return readlen; +} diff --git a/lock/nrfm.h b/lock/nrfm.h new file mode 100644 index 0000000..22a9b89 --- /dev/null +++ b/lock/nrfm.h @@ -0,0 +1,19 @@ +#ifndef NRFM_H +#define NRFM_H + +#include + +#define ADDRLEN 3 +#define MAXPDLEN 32 + +void radio_init(const uint8_t rxaddr[ADDRLEN]); + +void radio_print_config(void); + +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); + +#endif /* NRFM_H */ diff --git a/lock/uart.c b/lock/uart.c new file mode 100644 index 0000000..a6d6674 --- /dev/null +++ b/lock/uart.c @@ -0,0 +1,37 @@ +#include +#include + +#include "uart.h" + +void uart_init(void) +{ + UBRR0H = UBRRH_VALUE; + UBRR0L = UBRRL_VALUE; +#if USE_2X + UCSR0A |= (1 << U2X0); +#else + UCSR0A &= ~(1 << U2X0); +#endif + UCSR0B = (1 << TXEN0) | (1 << RXEN0); + UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); +} + +static inline void uart_write_char(char c) +{ + while (!(UCSR0A & (1 << UDRE0))) + ; + UDR0 = c; +} + +void uart_write(const char *s) +{ + for (; *s; s++) + uart_write_char(*s); +} + +void uart_write_line(const char *s) +{ + uart_write(s); + uart_write_char('\r'); + uart_write_char('\n'); +} diff --git a/lock/uart.h b/lock/uart.h new file mode 100644 index 0000000..a88a3c6 --- /dev/null +++ b/lock/uart.h @@ -0,0 +1,8 @@ +#ifndef UART_H +#define UART_H + +void uart_init(void); +void uart_write(const char *s); +void uart_write_line(const char *s); + +#endif /* UART_H */ diff --git a/nrf24l01/Recv.Makefile b/nrf24l01/Recv.Makefile deleted file mode 100644 index 67ac41f..0000000 --- a/nrf24l01/Recv.Makefile +++ /dev/null @@ -1,43 +0,0 @@ -CC = avr-gcc -MCU = atmega328p -TARGET = recv - -SRC = radio.c recv.c serial.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/nrf24l01/Send.Makefile b/nrf24l01/Send.Makefile deleted file mode 100644 index 06e083b..0000000 --- a/nrf24l01/Send.Makefile +++ /dev/null @@ -1,44 +0,0 @@ -CC = avr-gcc -MCU = atmega328p -TARGET = send - -SRC = radio.c send.c serial.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/nrf24l01/nrf24l01.c b/nrf24l01/nrf24l01.c deleted file mode 100644 index 0e1cbe6..0000000 --- a/nrf24l01/nrf24l01.c +++ /dev/null @@ -1,134 +0,0 @@ -#include - -#include -#include - -#include "radio.h" -#include "uart.h" - -#define SPI_SS PB2 -#define SPI_SCK PB5 -#define SPI_MISO PB4 -#define SPI_MOSI PB3 -#define SPI_DDR DDRB -#define SPI_PORT PORTB - -#define NRF24_CE PB0 -#define NRF24_CE_DDR DDRB -#define NRF24_CE_PORT DDRB - -#define NRF24_POWER_ON_RST_DELAY 100 - -#define NRF24_REG_CONFIG 0x00 -#define NRF24_REG_EN_AA 0x01 -#define NRF24_REG_EN_RXADDR 0x02 -#define NRF24_REG_SETUP_AW 0x03 -#define NRF24_REG_SETUP_RETR 0x04 -#define NRF24_REG_RF_CH 0x05 -#define NRF24_REG_RF_SETUP 0x06 -#define NRF24_REG_STATUS 0x07 -#define NRF24_REG_OBSERVE_TX 0x08 -#define NRF24_REG_RPD 0x09 -#define NRF24_REG_RX_ADDR_P0 0x0A -#define NRF24_REG_RX_ADDR_P1 0x0B -#define NRF24_REG_RX_ADDR_P2 0x0C -#define NRF24_REG_RX_ADDR_P3 0x0D -#define NRF24_REG_RX_ADDR_P4 0x0E -#define NRF24_REG_RX_ADDR_P5 0x0F -#define NRF24_REG_TX_ADDR 0x10 -#define NRF24_REG_RX_PW_P0 0x11 -#define NRF24_REG_RX_PW_P1 0x12 -#define NRF24_REG_RX_PW_P2 0x13 -#define NRF24_REG_RX_PW_P3 0x14 -#define NRF24_REG_RX_PW_P4 0x15 -#define NRF24_REG_RX_PW_P5 0x16 -#define NRF24_REG_FIFO_STATUS 0x17 -#define NRF24_REG_DYNPD 0x1C -#define NRF24_REG_FEATURE 0x1D - -#define NRF24_NOP 0xFF -#define NRF24_R_REGISTER 0x00 -#define NRF24_W_REGISTER 0x20 -#define NRF24_R_RX_PAYLOAD 0x60 -#define NRF24_W_TX_PAYLOAD 0xA0 -#define NRF24_FLUSH_TX 0xE1 -#define NRF24_FLUSH_RX 0xE2 - -#define NRF24_MASK_RX_DR 6 -#define NRF24_MASK_TX_DS 5 -#define NRF24_MASK_MAX_RT 4 -#define NRF24_PWR_UP 1 -#define NRF24_PRIM_RX 0 -#define NRF24_ERX_P1 1 -#define NRF24_ERX_P0 0 - -static inline uint8_t read_reg(uint8_t reg) -{ - SPI_PORT &= ~(1 << SPI_SS); - SPDR = NRF24_R_REGISTER | reg; - while (!(SPSR & (1 << SPIF))) - ; - SPDR = 0; - while (!(SPSR & (1 << SPIF))) - ; - SPI_PORT |= (1 << SPI_SS); - return SPDR; -} - -static inline void write_reg(uint8_t reg, uint8_t val) -{ - SPI_PORT &= ~(1 << SPI_SS); - SPDR = NRF24_W_REGISTER | reg; - while (!(SPSR & (1 << SPIF))) - ; - SPDR = val; - while (!(SPSR & (1 << SPIF))) - ; - SPI_PORT |= (1 << SPI_SS); -} - -void radio_send(const char *data, uint8_t n) -{ -} - -uint8_t radio_recv(char *buf, uint8_t n) -{ -} - -void radio_listen(void) -{ -} - -void radio_init(const struct radio_cfg *cfg) -{ - uint8_t conf; - - SPI_DDR |= (1 << SPI_SS) | (1 << S2PI_SCK) | (1 << SPI_MOSI); - SPI_PORT |= (1 << SPI_SS); - SPCR |= (1 << SPE) | (1 << MSTR); - - NRF24_CE_DDR |= (1 << NRF24_CE); - NRF24_CE_PORT &= ~(1 << NRF24_CE); - - _delay_ms(NRF24_POWER_ON_RST_DELAY); - - // disable IRQs, set CRC encoding scheme to 2 bytes - conf = 0; - conf |= (1 << NRF24_MASK_RX_DR); - conf |= (1 << NRF24_MASK_TX_DS); - conf |= (1 << NRF24_MASK_MAX_RT); - conf |= (1 << NRF24_CRCO); - write_reg(NRF24_REG_CONFIG, conf); - - // only use data pipe 0 - conf = 0; - conf &= ~(1 << NRF24_ERX_P1); - conf |= (1 << NRF24_ERX_P0); - write_reg(NRF24_REG_EN_RXADDR, conf); - - // set address width to 3 bytes - write_reg(NRF24_REG_SETUP_AW, 0x01); - - conf = 0; - conf = -} diff --git a/nrf24l01/radio.h b/nrf24l01/radio.h deleted file mode 100644 index a44d67e..0000000 --- a/nrf24l01/radio.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef RADIO_H -#define RADIO_H - -#include - -struct radio_cfg { - uint8_t netid; - uint8_t nodeid; -}; - -void radio_init(const struct radio_cfg *cfg); - -void radio_listen(void); - -void radio_send(const char *data, uint8_t n); - -uint8_t radio_recv(char *buf, uint8_t n); - -#endif diff --git a/nrf24l01/recv.c b/nrf24l01/recv.c deleted file mode 100644 index 1c6ac0f..0000000 --- a/nrf24l01/recv.c +++ /dev/null @@ -1,64 +0,0 @@ -#include -#include - -#include "radio.h" -#include "serial.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 - -#define MAX_PAYLOAD_LEN 60 - -static char *s = "hello, world!"; -static uint8_t slen = 13; - -int main(void) -{ - struct radio_cfg cfg; - - cfg.netid = 0x01; - cfg.nodeid = 0x01; - cfg.payload_len = slen; - - RX_DDR &= ~(1 << RX_PIN); - RX_PORT &= ~(1 << RX_PIN); - PCICR |= (1 << RX_PCIE); - RX_PCMSK |= (1 << RX_PCINT); - - serial_init(); - - radio_init(&cfg); - radio_listen(); - - sei(); - - for (;;) - ; - - return 0; -} - -ISR(RX_PCINTVEC) -{ - uint8_t i, n; - char buf[MAX_PAYLOAD_LEN]; - - cli(); - - serial_write_line("Detected pin change IRQ"); - - n = radio_recv(buf, MAX_PAYLOAD_LEN - 1); - buf[n] = '\0'; - - for (i = 0; i < n; i++) - serial_write(buf[i]); - serial_write('\r'); - serial_write('\n'); - - sei(); -} diff --git a/nrf24l01/send.c b/nrf24l01/send.c deleted file mode 100644 index b390cf3..0000000 --- a/nrf24l01/send.c +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include - -#include -#include -#include - -#include "radio.h" -#include "serial.h" - -int main(void) -{ - uint8_t n; - struct radio_cfg cfg; - const char *s = "hello, world!"; - - n = strlen(s); - - cfg.netid = 0x01; - cfg.nodeid = 0x02; - cfg.payload_len = n; - - serial_init(); - radio_init(&cfg); - radio_set_tx_power(18); - - for (;;) { - radio_send(s, n); - serial_write_line("sent"); - _delay_ms(1500); - } - - return 0; -} diff --git a/nrf24l01/uart.c b/nrf24l01/uart.c deleted file mode 100644 index e5b6e05..0000000 --- a/nrf24l01/uart.c +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include - -#include "uart.h" - -void uart_init(void) -{ - UBRR0H = UBRRH_VALUE; - UBRR0L = UBRRL_VALUE; -#if USE_2X - UCSR0A |= (1 << U2X0); -#else - UCSR0A &= ~(1 << U2X0); -#endif - UCSR0B = (1 << TXEN0) | (1 << RXEN0); - UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); -} - -void uart_write(char c) -{ - while (!(UCSR0A & (1 << UDRE0))) - ; - UDR0 = data; -} - -void uart_write_line(const char *s) -{ - for (; *s; s++) - serial_write(*s); - - serial_write('\r'); - serial_write('\n'); -} diff --git a/nrf24l01/uart.h b/nrf24l01/uart.h deleted file mode 100644 index 81db74b..0000000 --- a/nrf24l01/uart.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef UART_H -#define UART_H - -void uart_init(void); -void uart_write(char c); -void uart_write_line(const char *s); - -#endif /* UART_H */ diff --git a/rf69/Recv.Makefile b/rf69/Recv.Makefile deleted file mode 100644 index 67ac41f..0000000 --- a/rf69/Recv.Makefile +++ /dev/null @@ -1,43 +0,0 @@ -CC = avr-gcc -MCU = atmega328p -TARGET = recv - -SRC = radio.c recv.c serial.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/rf69/Send.Makefile b/rf69/Send.Makefile deleted file mode 100644 index 06e083b..0000000 --- a/rf69/Send.Makefile +++ /dev/null @@ -1,44 +0,0 @@ -CC = avr-gcc -MCU = atmega328p -TARGET = send - -SRC = radio.c send.c serial.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/rf69/radio.c b/rf69/radio.c deleted file mode 100644 index f5d98c8..0000000 --- a/rf69/radio.c +++ /dev/null @@ -1,212 +0,0 @@ -#include - -#include -#include - -#include "radio.h" -#include "serial.h" - -#define SPI_SS PB2 -#define SPI_SCK PB5 -#define SPI_MISO PB4 -#define SPI_MOSI PB3 -#define SPI_DDR DDRB -#define SPI_PORT PORTB - -#define RF69_OPMODE_RX 0x10 -#define RF69_OPMODE_TX 0x0C -#define RF69_OPMODE_STDBY 0x04 - -#define RF69_REG_FIFO 0x01 -#define RF69_REG_OPMODE 0x01 -#define RF69_REG_PALEVEL 0x11 -#define RF69_REG_TESTPA1 0x5A -#define RF69_REG_TESTPA2 0x5C -#define RF69_REG_IRQFLAGS1 0x27 -#define RF69_REG_IRQFLAGS2 0x28 -#define RF69_REG_DIOMAPPING1 0x25 - -#define RF69_PALEVEL_PA1 0x40 -#define RF69_PALEVEL_PA2 0x20 -#define RF69_TESTPA1_BOOST 0x5D -#define RF69_TESTPA2_BOOST 0x7C -#define RF69_TESTPA1_NORMAL 0x55 -#define RF69_TESTPA2_NORMAL 0x70 -#define RF69_DIOMAPPING1_PACKET_SENT 0x00 -#define RF69_DIOMAPPING1_PAYLOAD_READY 0x40 - -static int8_t power = 0; -static uint8_t opmode = 0; - -static inline uint8_t read_reg(uint8_t reg) -{ - SPI_PORT &= ~(1 << SPI_SS); - SPDR = reg & 0x7F; - while (!(SPSR & (1 << SPIF))) - ; - SPDR = 0; - while (!(SPSR & (1 << SPIF))) - ; - SPI_PORT |= (1 << SPI_SS); - return SPDR; -} - -static inline void write_reg(uint8_t reg, uint8_t val) -{ - SPI_PORT &= ~(1 << SPI_SS); - SPDR = reg | 0x80; - while (!(SPSR & (1 << SPIF))) - ; - SPDR = val; - while (!(SPSR & (1 << SPIF))) - ; - SPI_PORT |= (1 << SPI_SS); -} - -static inline void set_mode(uint8_t mode) -{ - uint8_t val; - - if (opmode != mode) { - if (mode == RF69_OPMODE_TX) { - if (power >= 18) { - write_reg(RF69_REG_TESTPA1, RF69_TESTPA1_BOOST); - write_reg(RF69_REG_TESTPA2, RF69_TESTPA2_BOOST); - } - write_reg(RF69_REG_DIOMAPPING1, RF69_DIOMAPPING1_PACKET_SENT); - } else { - if (power >= 18) { - write_reg(RF69_REG_TESTPA1, RF69_TESTPA1_NORMAL); - write_reg(RF69_REG_TESTPA2, RF69_TESTPA2_NORMAL); - } - if (mode == RF69_OPMODE_RX) - write_reg(RF69_REG_DIOMAPPING1, RF69_DIOMAPPING1_PAYLOAD_READY); - } - - val = read_reg(RF69_REG_OPMODE); - val &= ~0x1C; - val |= (mode & 0x1C); - - write_reg(RF69_REG_OPMODE, val); - while (!(read_reg(RF69_REG_IRQFLAGS1) & 0x80)) - ; - - opmode = mode; - } -} - -void radio_set_tx_power(int8_t val) -{ - uint8_t pa; - - power = val; - - if (power < -2) - power = -2; - - if (power <= 13) - pa = (RF69_PALEVEL_PA1 | ((power + 18) & 0x1F)); - else if (power >= 18) - pa = (RF69_PALEVEL_PA1 | RF69_PALEVEL_PA2 | ((power + 11) & 0x1F)); - else - pa = (RF69_PALEVEL_PA1 | RF69_PALEVEL_PA2 | ((power + 14) & 0x1F)); - - write_reg(RF69_REG_PALEVEL, pa); -} - -void radio_send(const char *data, uint8_t n) -{ - uint8_t i, mode; - - mode = opmode; - set_mode(RF69_OPMODE_STDBY); - - SPI_PORT &= ~(1 << SPI_SS); - - SPDR = RF69_REG_FIFO | 0x80; - while (!(SPSR & (1 << SPIF))) - ; - - for (i = 0; i < n; i++) { - SPDR = data[i]; - while (!(SPSR & (1 << SPIF))) - ; - } - - SPI_PORT |= (1 << SPI_SS); - - set_mode(RF69_OPMODE_TX); - while (!(read_reg(RF69_REG_IRQFLAGS2) & 0x08)) - ; - - set_mode(mode); -} - -uint8_t radio_recv(char *buf, uint8_t n) -{ - uint8_t read_len, mode; - - read_len = 0; - mode = opmode; - - set_mode(RF69_OPMODE_STDBY); - - SPI_PORT &= ~(1 << SPI_SS); - - SPDR = RF69_REG_FIFO | 0x7F; - while (!(SPSR & (1 << SPIF))) - ; - - while (read_len < n) { - SPDR = 0; - while (!(SPSR & (1 << SPIF))) - ; - buf[read_len++] = SPDR; - } - - SPI_PORT |= (1 << SPI_SS); - - set_mode(mode); - return read_len; -} - -void radio_listen(void) -{ - set_mode(RF69_OPMODE_RX); -} - -void radio_init(const struct radio_cfg *cfg) -{ - SPI_DDR |= (1 << SPI_SS) | (1 << SPI_SCK) | (1 << SPI_MOSI); - SPI_PORT |= (1 << SPI_SS); - SPCR |= (1 << SPE) | (1 << MSTR); - - set_mode(RF69_OPMODE_STDBY); - - // LNA, AFC and RXBW settings - write_reg(0x18, 0x88); - write_reg(0x19, 0x55); - write_reg(0x1A, 0x8B); - - // DIO mappings: IRQ on DIO0 - write_reg(0x25, 0x40); - write_reg(0x26, 0x07); - - // RSSI threshold - write_reg(0x29, 0xE4); - - // sync config - write_reg(0x2E, 0x80); - write_reg(0x2F, cfg->netid); - - // packet config - write_reg(0x37, 0x10); - write_reg(0x38, cfg->payload_len); - write_reg(0x39, cfg->nodeid); - - // fifo config - write_reg(0x3C, 0x8F); - - // DAGC config - write_reg(0x6F, 0x30); -} diff --git a/rf69/radio.h b/rf69/radio.h deleted file mode 100644 index 924da2d..0000000 --- a/rf69/radio.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef RADIO_H -#define RADIO_H - -#include - -struct radio_cfg { - uint8_t netid; - uint8_t nodeid; - uint8_t payload_len; -}; - -void radio_init(const struct radio_cfg *cfg); - -void radio_set_tx_power(int8_t val); - -void radio_listen(void); - -void radio_send(const char *data, uint8_t n); - -uint8_t radio_recv(char *buf, uint8_t n); - -#endif diff --git a/rf69/recv.c b/rf69/recv.c deleted file mode 100644 index 1c6ac0f..0000000 --- a/rf69/recv.c +++ /dev/null @@ -1,64 +0,0 @@ -#include -#include - -#include "radio.h" -#include "serial.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 - -#define MAX_PAYLOAD_LEN 60 - -static char *s = "hello, world!"; -static uint8_t slen = 13; - -int main(void) -{ - struct radio_cfg cfg; - - cfg.netid = 0x01; - cfg.nodeid = 0x01; - cfg.payload_len = slen; - - RX_DDR &= ~(1 << RX_PIN); - RX_PORT &= ~(1 << RX_PIN); - PCICR |= (1 << RX_PCIE); - RX_PCMSK |= (1 << RX_PCINT); - - serial_init(); - - radio_init(&cfg); - radio_listen(); - - sei(); - - for (;;) - ; - - return 0; -} - -ISR(RX_PCINTVEC) -{ - uint8_t i, n; - char buf[MAX_PAYLOAD_LEN]; - - cli(); - - serial_write_line("Detected pin change IRQ"); - - n = radio_recv(buf, MAX_PAYLOAD_LEN - 1); - buf[n] = '\0'; - - for (i = 0; i < n; i++) - serial_write(buf[i]); - serial_write('\r'); - serial_write('\n'); - - sei(); -} diff --git a/rf69/send.c b/rf69/send.c deleted file mode 100644 index b390cf3..0000000 --- a/rf69/send.c +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include - -#include -#include -#include - -#include "radio.h" -#include "serial.h" - -int main(void) -{ - uint8_t n; - struct radio_cfg cfg; - const char *s = "hello, world!"; - - n = strlen(s); - - cfg.netid = 0x01; - cfg.nodeid = 0x02; - cfg.payload_len = n; - - serial_init(); - radio_init(&cfg); - radio_set_tx_power(18); - - for (;;) { - radio_send(s, n); - serial_write_line("sent"); - _delay_ms(1500); - } - - return 0; -} diff --git a/rf69/serial.c b/rf69/serial.c deleted file mode 100644 index 782e848..0000000 --- a/rf69/serial.c +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include - -#include "serial.h" - -void serial_init(void) -{ - UBRR0H = UBRRH_VALUE; - UBRR0L = UBRRL_VALUE; -#if USE_2X - UCSR0A |= (1 << U2X0); -#else - UCSR0A &= ~(1 << U2X0); -#endif - UCSR0B = (1 << TXEN0) | (1 << RXEN0); - UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); -} - -void serial_write(char data) -{ - while (!(UCSR0A & (1 << UDRE0))) - ; - UDR0 = data; -} - -void serial_write_line(const char *s) -{ - for (; *s; s++) - serial_write(*s); - - serial_write('\r'); - serial_write('\n'); -} diff --git a/rf69/serial.h b/rf69/serial.h deleted file mode 100644 index 0f9415b..0000000 --- a/rf69/serial.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef SA_SERIAL_H -#define SA_SERIAL_H - -void serial_init(void); -void serial_write(char data); -void serial_write_line(const char *s); - -#endif /* SA_SERIAL_H */ -- cgit v1.2.3