diff options
| author | Sadeep Madurange <sadeep@asciimx.com> | 2025-05-01 16:33:28 +0800 |
|---|---|---|
| committer | Sadeep Madurange <sadeep@asciimx.com> | 2025-05-01 16:33:28 +0800 |
| commit | 9084585e5006d24b741677b5b1c9c10df69ed449 (patch) | |
| tree | 03bd2cc68da5b6ee800844273d8520011016d128 /lock | |
| parent | 134bc428438b673e62cd2385ca20e6c0a0423092 (diff) | |
| download | smart-home-9084585e5006d24b741677b5b1c9c10df69ed449.tar.gz | |
Add debug flag to nrfm.c and wip fpm driver.
Diffstat (limited to 'lock')
| -rw-r--r-- | lock/Fend.Makefile | 4 | ||||
| -rw-r--r-- | lock/fend.c | 2 | ||||
| -rw-r--r-- | lock/fpm.c | 63 | ||||
| -rw-r--r-- | lock/fpm.h | 8 | ||||
| -rw-r--r-- | lock/nrfm.c | 21 |
5 files changed, 91 insertions, 7 deletions
diff --git a/lock/Fend.Makefile b/lock/Fend.Makefile index e478e47..b6601af 100644 --- a/lock/Fend.Makefile +++ b/lock/Fend.Makefile @@ -3,14 +3,14 @@ MCU = atmega328p PORT = /dev/cuaU1 TARGET = fend -SRC = fend.c uart.c nrfm.c util.c +SRC = fend.c fpm.c uart.c nrfm.c util.c OBJ = $(SRC:.c=.o) CFLAGS = -std=gnu99 CFLAGS += -Os CFLAGS += -Wall CFLAGS += -mmcu=$(MCU) -CFLAGS += -DBAUD=115200 +CFLAGS += -DBAUD=9600 CFLAGS += -DF_CPU=16000000UL CFLAGS += -ffunction-sections -fdata-sections diff --git a/lock/fend.c b/lock/fend.c index 39854c6..500ef7f 100644 --- a/lock/fend.c +++ b/lock/fend.c @@ -9,7 +9,6 @@ #include <util/delay.h> #include "nrfm.h" -#include "uart.h" #include "util.h" #define LOCK_PIN PD2 @@ -85,7 +84,6 @@ int main(void) init_leds(); init_btns(); - uart_init(); radio_init(rxaddr); radio_print_config(); diff --git a/lock/fpm.c b/lock/fpm.c new file mode 100644 index 0000000..814119f --- /dev/null +++ b/lock/fpm.c @@ -0,0 +1,63 @@ +#include <avr/io.h> +#include <util/setbaud.h> + +#include "fpm.h" + +static uint8_t start_code[] = { 0xEF, 0x01 }; +static uint8_t addr[] = { 0xFF, 0xFF, 0xFF, 0xFF }; + +static inline void write(uint8_t c) +{ + while (!(UCSR0A & (1 << UDRE0))) + ; + UDR0 = c; +} + +static inline void write_bulk(uint8_t *data, uint16_t n) +{ + int i; + + for (i = 0; i < n; i++) + write(data[i]); +} + +static inline void send(uint8_t pktid, uint8_t *data, uint8_t n) +{ + int i; + uint16_t pktlen, sum; + + pktlen = n + 2; + + write_bulk(start_code, 2); + write_bulk(addr, 4); + write(pktid); + write((uint8_t)(pktlen >> 8)); + write((uint8_t)pktlen); + + sum = (pktlen >> 8) + (pktlen & 0xFF) + pktid; + for (i = 0; i < n; i++) { + write(data[i]); + sum += data[i]; + } + + write((uint8_t)(sum >> 8)); + write((uint8_t)sum); +} + +void fpm_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 fpm_print_config(void) +{ +} + diff --git a/lock/fpm.h b/lock/fpm.h new file mode 100644 index 0000000..eb69ae2 --- /dev/null +++ b/lock/fpm.h @@ -0,0 +1,8 @@ +#ifndef FPM_H +#define FPM_H + +void fpm_init(void); + +void fpm_print_config(void); + +#endif /* FPM_H */ diff --git a/lock/nrfm.c b/lock/nrfm.c index 253c6ce..5ae5737 100644 --- a/lock/nrfm.c +++ b/lock/nrfm.c @@ -178,6 +178,7 @@ static inline uint8_t rx_pdlen(void) void radio_print_config(void) { +#if DEBUG char s[22]; uint8_t i, rv, addr[ADDRLEN]; @@ -198,6 +199,7 @@ void radio_print_config(void) read_reg_bulk(0x0B, addr, ADDRLEN); snprintf(s, LEN(s), "\r\n\t0x0B: %d.%d.%d", addr[2], addr[1], addr[0]); uart_write_line(s); +#endif } void radio_init(const uint8_t rxaddr[ADDRLEN]) @@ -244,7 +246,6 @@ void radio_pwr_dwn(void) uint8_t radio_sendto(const uint8_t addr[ADDRLEN], const char *msg, uint8_t n) { - char s[4]; int i, imax; uint8_t cfg, rv, maxrt, txds; @@ -258,12 +259,16 @@ uint8_t radio_sendto(const uint8_t addr[ADDRLEN], const char *msg, uint8_t n) setaddr(0x10, addr); setaddr(0x0A, addr); +#if DEBUG + char s[4]; + 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)); +#endif imax = n < MAXPDLEN ? n : MAXPDLEN; @@ -289,11 +294,15 @@ uint8_t radio_sendto(const uint8_t addr[ADDRLEN], const char *msg, uint8_t n) maxrt = rv & (1 << 4); } while (txds == 0 && maxrt == 0); +#if DEBUG if (txds) uart_write_line("DEBUG: packet sent"); - else if (maxrt) { +#endif + if (maxrt) { flush_tx(); +#if DEBUG uart_write_line("ERROR: sendto() failed: MAX_RT"); +#endif } // restore config, typically rx mode @@ -304,7 +313,6 @@ uint8_t radio_sendto(const uint8_t addr[ADDRLEN], const char *msg, uint8_t n) uint8_t radio_recv(char *buf, uint8_t n) { - char s[3]; int readlen, pdlen, readmax; pdlen = 0; @@ -313,17 +321,24 @@ uint8_t radio_recv(char *buf, uint8_t n) pdlen = rx_pdlen(); if (pdlen == 0) { radio_flush_rx(); +#if DEBUG uart_write_line("ERROR: PDLEN = 0, abort read"); +#endif return 0; } +#if DEBUG + char s[3]; itoa(pdlen, s, 10); uart_write("DEBUG: PDLEN="); uart_write_line(s); +#endif if (pdlen > MAXPDLEN) { radio_flush_rx(); +#if DEBUG uart_write_line("ERROR: PDLEN > MAXPDLEN, abort read"); +#endif return 0; } |
