summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Borges <paulo.borges@openbossa.org>2013-12-25 00:44:18 -0300
committerPaulo Borges <paulo.borges@openbossa.org>2013-12-25 23:54:11 -0300
commit0626b56e3dc8d8bb65d3a0190e0effd8327fd8d7 (patch)
treee4e7409a58b1eb4eb71ff23f0344998179421ac2
parentf989f3f5ea59f6057389b723aad5ece2fd874b4d (diff)
nrf51822: Implement radio_recv()
-rw-r--r--platform/nrf51822/radio.c69
-rw-r--r--stack/radio.h8
2 files changed, 77 insertions, 0 deletions
diff --git a/platform/nrf51822/radio.c b/platform/nrf51822/radio.c
index 62e0789..5656ec3 100644
--- a/platform/nrf51822/radio.c
+++ b/platform/nrf51822/radio.c
@@ -35,12 +35,80 @@
#define MAX_BUF_LEN (RADIO_MAX_PDU_LEN + 1) /* S0, LEN
and S1 occupies 3 bytes, not 2 */
+#define STATUS_INITIALIZED 1
+#define STATUS_RX 2
+#define STATUS_BUSY STATUS_RX
+
static radio_handler handler;
static uint8_t buf[MAX_BUF_LEN];
+static uint8_t status;
+
+#define COMMON_INITIALIZATION(ch, aa, crc) \
+ do { \
+ int8_t freq; \
+ if (!(status & STATUS_INITIALIZED)) \
+ return -1; \
+ if (status & STATUS_BUSY) \
+ return -1; \
+ freq = ch2freq(ch); \
+ if (freq < 0) \
+ return -1; \
+ NRF_RADIO->DATAWHITEIV = ch & 0x3F; \
+ NRF_RADIO->FREQUENCY = freq; \
+ NRF_RADIO->BASE0 = (aa << 8) & 0xFFFFFF00; \
+ NRF_RADIO->PREFIX0 = (aa >> 24) & 0x000000FF; \
+ NRF_RADIO->CRCINIT = crc; \
+ } while (0)
+
+static __inline int8_t ch2freq(uint8_t ch)
+{
+ switch (ch) {
+ case 37:
+ return 2;
+ case 38:
+ return 26;
+ case 39:
+ return 80;
+ default:
+ if (ch > 39)
+ return -1;
+ else if (ch < 11)
+ return 4 + (2 * ch);
+ else
+ return 6 + (2 * ch);
+ }
+}
void RADIO_IRQHandler(void)
{
+ struct radio_packet packet;
+ uint8_t old_status;
+
NRF_RADIO->EVENTS_END = 0UL;
+
+ old_status = status;
+ status = STATUS_INITIALIZED;
+
+ if ((old_status & STATUS_RX) && handler) {
+ packet.len = buf[1] + 2;
+ packet.crc = NRF_RADIO->CRCSTATUS;
+
+ packet.pdu[0] = buf[0];
+ packet.pdu[1] = ((buf[2] & 0x3) << 6) | (buf[1] & 0x3F);
+ memcpy(packet.pdu + 2, buf + 3, buf[1]);
+
+ handler(RADIO_EVT_RX_COMPLETED, &packet);
+ }
+}
+
+int16_t radio_recv(uint8_t ch, uint32_t aa, uint32_t crc)
+{
+ COMMON_INITIALIZATION(ch, aa, crc);
+
+ NRF_RADIO->TASKS_RXEN = 1UL;
+ status |= STATUS_RX;
+
+ return 0;
}
void radio_register_handler(radio_handler hdlr)
@@ -91,6 +159,7 @@ int16_t radio_init(void)
NRF_RADIO->PACKETPTR = (uint32_t) buf;
memset(buf, 0, sizeof(buf));
+ status = STATUS_INITIALIZED;
handler = NULL;
return 0;
diff --git a/stack/radio.h b/stack/radio.h
index a1fce9a..52f6be7 100644
--- a/stack/radio.h
+++ b/stack/radio.h
@@ -26,6 +26,14 @@
#define RADIO_MAX_PDU_LEN 39
+#define RADIO_EVT_RX_COMPLETED 1
+
+struct radio_packet {
+ uint8_t pdu[RADIO_MAX_PDU_LEN];
+ uint8_t len;
+ uint8_t crc;
+};
+
typedef void (*radio_handler) (uint8_t evt, void *data);
int16_t radio_init(void);