summaryrefslogtreecommitdiffstats
path: root/rf_test
diff options
context:
space:
mode:
authorSadeep Madurange <sadeep@asciimx.com>2024-12-03 18:00:14 +0800
committerSadeep Madurange <sadeep@asciimx.com>2024-12-03 18:00:14 +0800
commitd162d1cfd2421509a8d6933cb93dd80ffeda9cf8 (patch)
tree6236628fd6868f100b039036f020dc485b563874 /rf_test
parentab97e3dfbd93a876bf16b74345af1d6b8b3843f1 (diff)
downloadsmart-home-d162d1cfd2421509a8d6933cb93dd80ffeda9cf8.tar.gz
Move the rfm code to separate file.
Diffstat (limited to 'rf_test')
-rw-r--r--rf_test/Recv.Makefile2
-rw-r--r--rf_test/Send.Makefile2
-rw-r--r--rf_test/radio.c108
-rw-r--r--rf_test/radio.h16
-rw-r--r--rf_test/recv.c64
-rw-r--r--rf_test/rfm.c122
-rw-r--r--rf_test/rfm.h12
-rw-r--r--rf_test/send.c137
8 files changed, 157 insertions, 306 deletions
diff --git a/rf_test/Recv.Makefile b/rf_test/Recv.Makefile
index 1c6733b..67ac41f 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 serial.c
+SRC = radio.c recv.c serial.c
OBJ = $(SRC:.c=.o)
CFLAGS = -std=gnu99
diff --git a/rf_test/Send.Makefile b/rf_test/Send.Makefile
index 6e171b1..06e083b 100644
--- a/rf_test/Send.Makefile
+++ b/rf_test/Send.Makefile
@@ -2,7 +2,7 @@ CC = avr-gcc
MCU = atmega328p
TARGET = send
-SRC = rfm.c send.c serial.c
+SRC = radio.c send.c serial.c
OBJ = $(SRC:.c=.o)
CFLAGS = -std=gnu99
diff --git a/rf_test/radio.c b/rf_test/radio.c
new file mode 100644
index 0000000..73c7099
--- /dev/null
+++ b/rf_test/radio.c
@@ -0,0 +1,108 @@
+#include <avr/io.h>
+
+#include "radio.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
+
+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);
+}
+
+void radio_send(const char *data, uint8_t n)
+{
+ uint8_t i;
+
+ // STDBY + ListenAbort mode
+ write_reg(0x01, 0x04);
+ while ((read_reg(0x27) >> 7) != 1)
+ ;
+
+ SPI_PORT &= ~(1 << SPI_SS);
+ SPDR = 0x00 | 0x80;
+ while (!(SPSR & (1 << SPIF)))
+ ;
+ for (i = 0; i < n; i++) {
+ SPDR = data[i];
+ while (!(SPSR & (1 << SPIF)))
+ ;
+ }
+ SPI_PORT |= (1 << SPI_SS);
+
+ write_reg(0x01, 0x0C);
+ while (!read_reg(0x28))
+ ;
+
+ write_reg(0x01, 0x04);
+ while ((read_reg(0x27) >> 7) != 1)
+ ;
+
+ // enable ListenOn in STDBY mode
+ write_reg(0x01, (read_reg(0x01) | 0x40));
+}
+
+uint8_t radio_recv(char *buf, uint8_t n)
+{
+ uint8_t i;
+
+ i = 0;
+
+ if ((read_reg(0x28) & 0x04))
+ {
+ write_reg(0x01, 0x04);
+ while ((read_reg(0x27) >> 7) != 1)
+ ;
+
+ SPI_PORT &= ~(1 << SPI_SS);
+ SPDR = 0x00 | 0x7F;
+ while (!(SPSR & (1 << SPIF)))
+ ;
+ while (i < n) {
+ SPDR = 0;
+ while (!(SPSR & (1 << SPIF)))
+ ;
+ buf[i++] = SPDR;
+ }
+
+ SPI_PORT |= (1 << SPI_SS);
+ }
+ return i;
+}
+
+void radio_init(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);
+
+ if (cfg->payload_len > 0)
+ write_reg(0x38, cfg->payload_len);
+}
+
+
diff --git a/rf_test/radio.h b/rf_test/radio.h
new file mode 100644
index 0000000..fe715b2
--- /dev/null
+++ b/rf_test/radio.h
@@ -0,0 +1,16 @@
+#ifndef RADIO_H
+#define RADIO_H
+
+#include <stdint.h>
+
+struct radio_cfg {
+ uint8_t payload_len;
+};
+
+void radio_init(struct radio_cfg *cfg);
+
+void radio_send(const char *data, uint8_t n);
+
+uint8_t radio_recv(char *buf, uint8_t n);
+
+#endif
diff --git a/rf_test/recv.c b/rf_test/recv.c
index 2e5c1c9..cfbe2d6 100644
--- a/rf_test/recv.c
+++ b/rf_test/recv.c
@@ -1,40 +1,31 @@
#include <avr/io.h>
#include <avr/interrupt.h>
-#include <util/delay.h>
-#include "rfm.h"
+#include "radio.h"
#include "serial.h"
-#define TEST_LED PB1
-#define LOCK_LED PD6
-#define UNLOCK_LED PD7
+#define PAYLOAD_LEN 13
-#define SYN 0xAA
-#define LOCK 0xB5
-#define UNLOCK 0xAE
+#define RX_PIN PB0
+#define RX_DDR DDRB
+#define RX_PORT PORTB
+#define RX_PCIE PCIE0
+#define RX_PCINT PCINT0
+#define RX_PCMSK PCMSK0
+#define RX_PCINTVEC PCINT0_vect
-#define SIGPIN PB0
-
-static inline void led_init(void)
+int main(void)
{
- DDRB |= (1 << TEST_LED);
- DDRD |= (1 << LOCK_LED) | (1 << UNLOCK_LED);
-}
+ struct radio_cfg cfg;
-static inline void pcint2_init(void)
-{
- PCICR |= (1 << PCIE2);
- PCMSK2 |= (1 << PCINT0);
-}
+ cfg.payload_len = PAYLOAD_LEN;
-int main(void)
-{
- DDRB &= ~(1 << SIGPIN);
- PORTB &= ~(1 << SIGPIN);
+ RX_DDR &= ~(1 << RX_PIN);
+ PCICR |= (1 << RX_PCIE);
+ RX_PCMSK |= (1 << RX_PCINT);
- led_init();
serial_init();
- pcint2_init();
+ radio_init(&cfg);
sei();
@@ -44,19 +35,20 @@ int main(void)
return 0;
}
-ISR(PCINT2_vect)
+ISR(RX_PCINTVEC)
{
- char *s;
- uint8_t buf[2], n;
+ uint8_t i, n;
+ char buf[PAYLOAD_LEN + 1];
- n = rfm_recvfrom(0x00, buf, 2);
+ cli();
- if (buf[1] == LOCK)
- s = "LOCK";
- else if (buf[1] == UNLOCK)
- s = "UNLOCK";
- else
- s = "Garbage";
+ n = radio_recv(buf, PAYLOAD_LEN);
+ buf[n] = '\0';
- serial_write_line(s);
+ for (i = 0; i < n; i++)
+ serial_write(buf[i]);
+ serial_write('\r');
+ serial_write('\n');
+
+ sei();
}
diff --git a/rf_test/rfm.c b/rf_test/rfm.c
deleted file mode 100644
index 0273312..0000000
--- a/rf_test/rfm.c
+++ /dev/null
@@ -1,122 +0,0 @@
-#include <avr/io.h>
-
-#include "rfm.h"
-
-#define SS_PIN PB2
-#define SS_DDR DDRB
-#define SS_PORT PORTB
-
-#define SCK_PIN PB5
-#define MOSI_PIN PB3
-#define MISO_PIN PB4
-#define SPI_DDR DDRB
-
-// RFM69 op modes
-#define RX 0x10
-#define TX 0x0C
-#define SLEEP 0x00
-#define STDBY 0x04
-#define LISTEN_ON 0x40
-
-static inline void spi_init(void)
-{
- SS_DDR |= (1 << SS_PIN);
- SS_PORT |= (1 << SS_PIN);
- SPI_DDR = (1 << MOSI_PIN) | (1 << SCK_PIN);
-
- SPCR = (1 << SPE) | (1 << MSTR);
-}
-
-static inline uint8_t read_reg(uint8_t reg)
-{
- uint8_t data;
-
- SS_PORT |= (1 << SS_PIN);
- SPDR = reg | 0x7F;
- while (!(SPSR & (1 << SPIF)))
- ;
- data = SPDR;
- SS_PORT &= ~(1 << SS_PIN);
-
- return data;
-}
-
-static inline void write_reg(uint8_t reg, uint8_t val)
-{
- while (read_reg(reg) != val) {
- SS_PORT |= (1 << SS_PIN);
- SPDR = reg | 0x80;
- while (!(SPSR & (1 << SPIF)))
- ;
- SS_PORT &= ~(1 << SS_PIN);
- }
-}
-
-static inline void set_mode(uint8_t mode)
-{
- write_reg(0x01, mode);
- while (!read_reg(0x27))
- ;
-}
-
-void rfm_init(void)
-{
- spi_init();
-
- set_mode(STDBY | LISTEN_ON);
-
- // rx interrupt on DPIO0
- write_reg(0x25, 0x40);
- write_reg(0x26, 0x07);
-
- // packet format: 8 bits + whitening + crc
- write_reg(0x37, 0x52);
- write_reg(0x38, 0x08);
-
- // disable encryption
- write_reg(0x3D, 0x02);
-}
-
-void rfm_sendto(uint8_t addr, uint8_t *data, uint8_t n)
-{
- uint8_t i;
-
- set_mode(STDBY);
-
- SS_PORT |= (1 << SS_PIN);
-
- SPDR = 0x7F;
- while (!(SPSR & (1 << SPIF)))
- ;
-
- SPDR = addr;
- while (!(SPSR & (1 << SPIF)))
- ;
-
- for (i = 0; i < n; i++) {
- SPDR = data[i];
- while (!(SPSR & (1 << SPIF)))
- ;
- }
-
- SS_PORT &= ~(1 << SS_PIN);
-
- set_mode(TX);
-
- while (!((read_reg(0x28) >> 3) & 1))
- ;
-}
-
-uint8_t rfm_recvfrom(uint8_t addr, uint8_t *buf, uint8_t n)
-{
- uint8_t i;
-
- SS_PORT |= (1 << SS_PIN);
-
- for (i = 0; i < n && ((read_reg(0x28) >> 6) & 1); i++)
- buf[i] = read_reg(0x00);
-
- SS_PORT &= ~(1 << SS_PIN);
-
- return i;
-}
diff --git a/rf_test/rfm.h b/rf_test/rfm.h
deleted file mode 100644
index 6bd3013..0000000
--- a/rf_test/rfm.h
+++ /dev/null
@@ -1,12 +0,0 @@
-#ifndef RFM69_H
-#define RFM69_H
-
-#include <stdint.h>
-
-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 71e764b..5dea8f3 100644
--- a/rf_test/send.c
+++ b/rf_test/send.c
@@ -5,135 +5,16 @@
#include <avr/interrupt.h>
#include <util/delay.h>
+#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 RX_PIN PB0
-#define RX_DDR DDRB
-#define RX_PORT PORTB
-#define RX_PCIE PCIE0
-#define RX_PCINT PCINT0
-#define RX_PCMSK PCMSK0
-#define RX_PCINTVEC PCINT0_vect
-
-#define PAYLOAD_LEN 32
-
-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 radio_send(const char *data, uint8_t n)
-{
- uint8_t i;
-
- // STDBY + ListenAbort mode
- write_reg(0x01, 0x04);
- while ((read_reg(0x27) >> 7) != 1)
- ;
-
- SPI_PORT &= ~(1 << SPI_SS);
- SPDR = 0x00 | 0x80;
- while (!(SPSR & (1 << SPIF)))
- ;
- for (i = 0; i < n; i++) {
- SPDR = data[i];
- while (!(SPSR & (1 << SPIF)))
- ;
- }
- SPI_PORT |= (1 << SPI_SS);
-
- write_reg(0x01, 0x0C);
- while (!read_reg(0x28))
- ;
-
- write_reg(0x01, 0x04);
- while ((read_reg(0x27) >> 7) != 1)
- ;
-
- // enable ListenOn in STDBY mode
- write_reg(0x01, (read_reg(0x01) | 0x40));
-}
-
-static inline uint8_t radio_recv(char *buf, uint8_t n)
-{
- uint8_t i;
-
- i = 0;
-
- if ((read_reg(0x28) & 0x04))
- {
- write_reg(0x01, 0x04);
- while ((read_reg(0x27) >> 7) != 1)
- ;
-
- SPI_PORT &= ~(1 << SPI_SS);
- SPDR = 0x00 | 0x7F;
- while (!(SPSR & (1 << SPIF)))
- ;
- while (i < n) {
- SPDR = 0;
- while (!(SPSR & (1 << SPIF)))
- ;
- buf[i++] = SPDR;
- }
-
- SPI_PORT |= (1 << SPI_SS);
- }
- return i;
-}
-
-struct radio_cfg {
- uint8_t payload_len;
-};
-
-static inline void radio_init(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);
-
- if (cfg->payload_len > 0)
- write_reg(0x38, cfg->payload_len);
-
- RX_DDR &= ~(1 << RX_PIN);
- PCICR |= (1 << RX_PCIE);
- RX_PCMSK |= (1 << RX_PCINT);
-}
+#define PAYLOAD_LEN 13
int main(void)
{
struct radio_cfg cfg;
- const char *s = "hello";
-
+ const char *s = "hello, world!";
cfg.payload_len = PAYLOAD_LEN;
serial_init();
@@ -150,15 +31,3 @@ int main(void)
return 0;
}
-ISR(RX_PCINTVEC)
-{
- uint8_t i, n;
- char buf[PAYLOAD_LEN];
-
- n = radio_recv(buf, PAYLOAD_LEN);
-
- for (i = 0; i < n; i++)
- serial_write(buf[i]);
- serial_write('\r');
- serial_write('\n');
-}