From 343a139e10436d074a8d94a63b26ee0fe74280be Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Sun, 24 Nov 2024 15:16:10 +0800 Subject: Updated send and recv files to use rfm. --- rf_test/Recv.Makefile | 2 +- rf_test/recv.c | 43 +++++++++++++++++++------------------------ rf_test/rfm.c | 4 ++-- rf_test/rfm.h | 2 ++ rf_test/send.c | 18 ++++++++++++------ rf_test/serial.c | 33 +++++++++++++++++++++++++++++++++ rf_test/serial.h | 7 +++++++ 7 files changed, 76 insertions(+), 33 deletions(-) create mode 100644 rf_test/serial.c create mode 100644 rf_test/serial.h (limited to 'rf_test') diff --git a/rf_test/Recv.Makefile b/rf_test/Recv.Makefile index bbf7a26..b1f7a8b 100644 --- a/rf_test/Recv.Makefile +++ b/rf_test/Recv.Makefile @@ -2,7 +2,7 @@ CC = avr-gcc MCU = atmega328p TARGET = recv -SRC = rfm.c recv.c +SRC = rfm.c recv.c serial.c OBJ = $(SRC:.c=.o) CFLAGS = -std=gnu99 diff --git a/rf_test/recv.c b/rf_test/recv.c index 9cb3daa..010a432 100644 --- a/rf_test/recv.c +++ b/rf_test/recv.c @@ -2,6 +2,9 @@ #include #include +#include "rfm.h" +#include "serial.h" + #define TEST_LED PB1 #define LOCK_LED PD6 #define UNLOCK_LED PD7 @@ -11,9 +14,6 @@ #define UNLOCK 0xAE #define SIGPIN PB3 -#define SIGLEN 200 - -static volatile unsigned char data = 0; static inline void led_init(void) { @@ -33,35 +33,30 @@ int main(void) PORTB &= ~(1 << SIGPIN); led_init(); + serial_init(); pcint2_init(); sei(); - for (;;) { - if (data == LOCK) { - PORTD |= (1 << LOCK_LED); - PORTD &= ~(1 << UNLOCK_LED); - } - - if (data == UNLOCK) { - PORTD &= ~(1 << LOCK_LED); - PORTD |= (1 << UNLOCK_LED); - } - - data = 0; - _delay_ms(100); - } + for (;;) + ; return 0; } ISR(PCINT2_vect) { - int n, bit; - - for (n = 7; n >= 0; n--) { - _delay_ms(SIGLEN); - bit = ((PINB >> SIGPIN) & 1); - data = bit == 1 ? (data | (1 << n)) : (data & ~(1 << n)); - } + char *s; + uint8_t buf[2], n; + + n = rfm_recvfrom(0x00, buf, 2); + + if (buf[1] == LOCK) + s = "LOCK"; + else if (buf[1] == UNLOCK) + s = "UNLOCK"; + else + s = "Garbage"; + + serial_write_line(s); } diff --git a/rf_test/rfm.c b/rf_test/rfm.c index 7684578..5b64e4e 100644 --- a/rf_test/rfm.c +++ b/rf_test/rfm.c @@ -32,7 +32,7 @@ static inline uint8_t read_reg(uint8_t reg) uint8_t data; SS_PORT |= (1 << SS_PIN); - SPDR = addr | 0x7F; + SPDR = reg | 0x7F; while (!(SPSR & (1 << SPIF))) ; data = SPDR; @@ -45,7 +45,7 @@ static inline void write_reg(uint8_t reg, uint8_t val) { while (read_reg(reg) != val) { SS_PORT |= (1 << SS_PIN); - SPDR = addr | 0x80; + SPDR = reg | 0x80; while (!(SPSR & (1 << SPIF))) ; SS_PORT &= ~(1 << SS_PIN); diff --git a/rf_test/rfm.h b/rf_test/rfm.h index 8adaf3b..6bd3013 100644 --- a/rf_test/rfm.h +++ b/rf_test/rfm.h @@ -7,4 +7,6 @@ void rfm_init(void); void rfm_sendto(uint8_t addr, uint8_t *data, uint8_t n); +uint8_t rfm_recvfrom(uint8_t addr, uint8_t *buf, uint8_t n); + #endif /* RFM69_H */ diff --git a/rf_test/send.c b/rf_test/send.c index 97ebbcf..539cf79 100644 --- a/rf_test/send.c +++ b/rf_test/send.c @@ -2,23 +2,29 @@ #include #include +#include "rfm.h" + #define LOCK_BTN PD6 #define UNLOCK_BTN PD7 -#define SYN 0xAA +#define ADDR 0xAA #define LOCK 0xB5 #define UNLOCK 0xAE static inline void lock(void) { - spi_send(SYN); - spi_send(LOCK); + uint8_t data[1]; + + data[0] = LOCK; + rfm_sendto(ADDR, data, 1); } static inline void unlock(void) { - spi_send(SYN); - spi_send(UNLOCK); + uint8_t data[1]; + + data[0] = UNLOCK; + rfm_sendto(ADDR, data, 1); } static inline int is_btn_pressed(unsigned char btn) @@ -42,7 +48,7 @@ int main(void) DDRD &= ~((1 << LOCK_BTN) | (1 << UNLOCK_BTN)); PORTD |= (1 << LOCK_BTN) | (1 << UNLOCK_BTN); - spi_init(); + rfm_init(); pcint2_init(); sei(); diff --git a/rf_test/serial.c b/rf_test/serial.c new file mode 100644 index 0000000..0b9aecf --- /dev/null +++ b/rf_test/serial.c @@ -0,0 +1,33 @@ +#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); +} + +static void serial_write(unsigned 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/rf_test/serial.h b/rf_test/serial.h new file mode 100644 index 0000000..6128623 --- /dev/null +++ b/rf_test/serial.h @@ -0,0 +1,7 @@ +#ifndef SA_SERIAL_H +#define SA_SERIAL_H + +void serial_init(void); +void serial_write_line(const char *s); + +#endif /* SA_SERIAL_H */ -- cgit v1.2.3