diff options
Diffstat (limited to 'lock')
| -rw-r--r-- | lock/Back.Makefile | 44 | ||||
| -rw-r--r-- | lock/Frnt.Makefile | 44 | ||||
| -rw-r--r-- | lock/back.c | 64 | ||||
| -rw-r--r-- | lock/frnt.c | 28 | ||||
| -rw-r--r-- | lock/nrfm.c | 333 | ||||
| -rw-r--r-- | lock/nrfm.h | 19 | ||||
| -rw-r--r-- | lock/uart.c | 37 | ||||
| -rw-r--r-- | lock/uart.h | 8 |
8 files changed, 577 insertions, 0 deletions
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 <stdint.h> +#include <stdlib.h> +#include <string.h> +#include <avr/interrupt.h> +#include <util/delay.h> + +#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 <stdint.h> +#include <string.h> +#include <util/delay.h> + +#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 <stdio.h> +#include <stdlib.h> + +#include <avr/io.h> +#include <avr/interrupt.h> +#include <util/delay.h> + +#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 <stdint.h> + +#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 <avr/io.h> +#include <util/setbaud.h> + +#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 */ |
