summaryrefslogtreecommitdiffstats
path: root/rf_test/radio.c
diff options
context:
space:
mode:
authorSadeep Madurange <sadeep@asciimx.com>2024-12-08 11:23:01 +0800
committerSadeep Madurange <sadeep@asciimx.com>2024-12-08 11:23:01 +0800
commit45191846fd728d9b1abee5e4d358b6d2ea0bfce1 (patch)
tree4fb58cd68571f98f6171b1720035a3a984d4e044 /rf_test/radio.c
parent831d421e5bc9c6949cb97395acf909f1983c270c (diff)
downloadsmart-home-45191846fd728d9b1abee5e4d358b6d2ea0bfce1.tar.gz
wip: tx not working.
Diffstat (limited to 'rf_test/radio.c')
-rw-r--r--rf_test/radio.c48
1 files changed, 33 insertions, 15 deletions
diff --git a/rf_test/radio.c b/rf_test/radio.c
index a2dea45..c967fe0 100644
--- a/rf_test/radio.c
+++ b/rf_test/radio.c
@@ -39,8 +39,10 @@
#define SPI_DDR DDRB
#define SPI_PORT PORTB
+#define MAX_PAYLOAD_LEN 61
#define MAX_POWER_LEVEL 23
+static uint8_t node_id = 0;
static uint8_t payload_len = 0;
static inline uint8_t read_reg(uint8_t reg)
@@ -95,21 +97,36 @@ void radio_sendto(uint8_t addr, const char *data, uint8_t n)
{
uint8_t i;
+ if (n > MAX_PAYLOAD_LEN)
+ n = MAX_PAYLOAD_LEN;
+
// force-stop rx
write_reg(0x3D, ((read_reg(0x3D) & 0xFB) | 0x04));
- //while (((read_reg(0x01) & 0x1C) == 0x10) && payload_len == 0 && rssi() < -90)
- // write_reg(0x01, 0x04);
-
- write_reg(0x01, 0x04);
- while (!(read_reg(0x27) & 0x80))
+ while (((read_reg(0x01) & 0x1C) == 0x10) && payload_len == 0 && rssi() < -90)
+ // todo: read data in fifo
;
- // todo: implement the rest...
+ write_reg(0x01, 0x04);
+ while (!(read_reg(0x27) & 80))
+ ;
SPI_PORT &= ~(1 << SPI_SS);
SPDR = 0x00 | 0x80;
while (!(SPSR & (1 << SPIF)))
;
+ SPDR = n + 3;
+ while (!(SPSR & (1 << SPIF)))
+ ;
+ SPDR = addr;
+ while (!(SPSR & (1 << SPIF)))
+ ;
+ SPDR = node_id;
+ while (!(SPSR & (1 << SPIF)))
+ ;
+ // ctl byte
+ SPDR = 0;
+ while (!(SPSR & (1 << SPIF)))
+ ;
for (i = 0; i < n; i++) {
SPDR = data[i];
while (!(SPSR & (1 << SPIF)))
@@ -117,19 +134,18 @@ void radio_sendto(uint8_t addr, const char *data, uint8_t n)
}
SPI_PORT |= (1 << SPI_SS);
- write_reg(0x01, 0x0C);
- while (!read_reg(0x28))
- ;
+ write_reg(0x01, ((read_reg(0x01) & 0xE3) | 0x0C));
+ write_reg(0x5A, 0x5D);
+ write_reg(0x5C, 0x7C);
- write_reg(0x01, 0x04);
- while ((read_reg(0x27) >> 7) != 1)
+ while (!(read_reg(0x28) & 0x08))
;
- // todo: do a more reliable check
- _delay_ms(10);
+ serial_write_line("146");
- // ListenOn
- write_reg(0x01, (read_reg(0x01) | 0x40));
+ write_reg(0x01, ((read_reg(0x01) & 0xE3) | 0x04));
+ while (!(read_reg(0x27) & 0x80))
+ ;
}
uint8_t radio_recv(char *buf, uint8_t n)
@@ -161,6 +177,8 @@ uint8_t radio_recv(char *buf, uint8_t n)
void radio_init(struct radio_cfg *cfg)
{
+ node_id = cfg->node_id;
+
SPI_DDR |= (1 << SPI_SS) | (1 << SPI_SCK) | (1 << SPI_MOSI);
SPI_PORT |= (1 << SPI_SS);
SPCR |= (1 << SPE) | (1 << MSTR);