summaryrefslogtreecommitdiffstats
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
parent831d421e5bc9c6949cb97395acf909f1983c270c (diff)
downloadsmart-home-45191846fd728d9b1abee5e4d358b6d2ea0bfce1.tar.gz
wip: tx not working.
-rw-r--r--rf_test/radio.c48
-rw-r--r--rf_test/radio.h1
-rw-r--r--rf_test/send.c5
3 files changed, 38 insertions, 16 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);
diff --git a/rf_test/radio.h b/rf_test/radio.h
index 6a006e2..aaa099f 100644
--- a/rf_test/radio.h
+++ b/rf_test/radio.h
@@ -4,6 +4,7 @@
#include <stdint.h>
struct radio_cfg {
+ uint8_t node_id;
uint8_t network_id;
uint8_t payload_len;
};
diff --git a/rf_test/send.c b/rf_test/send.c
index 53e92fc..c3831ba 100644
--- a/rf_test/send.c
+++ b/rf_test/send.c
@@ -12,6 +12,8 @@
#define LED_DDR DDRB
#define LED_PORT PORTB
+#define NODE_ID 2
+
int main(void)
{
uint8_t n;
@@ -19,6 +21,7 @@ int main(void)
const char *s = "hello, world!";
n = strlen(s);
+ cfg.node_id = NODE_ID;
cfg.payload_len = n;
LED_DDR |= (1 << LED_PIN);
@@ -29,7 +32,7 @@ int main(void)
sei();
for (;;) {
- radio_send(s, n);
+ radio_sendto(1, s, n);
serial_write_line("sent data");
LED_PORT |= (1 << LED_PIN);