aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios-contrib/testhal/NRF52/NRF52832/RADIO-ESB/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios-contrib/testhal/NRF52/NRF52832/RADIO-ESB/main.c')
-rw-r--r--lib/chibios-contrib/testhal/NRF52/NRF52832/RADIO-ESB/main.c122
1 files changed, 122 insertions, 0 deletions
diff --git a/lib/chibios-contrib/testhal/NRF52/NRF52832/RADIO-ESB/main.c b/lib/chibios-contrib/testhal/NRF52/NRF52832/RADIO-ESB/main.c
new file mode 100644
index 000000000..8971fef82
--- /dev/null
+++ b/lib/chibios-contrib/testhal/NRF52/NRF52832/RADIO-ESB/main.c
@@ -0,0 +1,122 @@
1#include <stdint.h>
2#include <string.h>
3#include <stdio.h>
4
5#include "ch.h"
6#include "hal.h"
7#include "chprintf.h"
8
9#include "nrf52_radio.h"
10
11static SerialConfig serial_config = {
12 .speed = 38400,
13 .tx_pad = UART_TX,
14 .rx_pad = UART_RX,
15#if NRF5_SERIAL_USE_HWFLOWCTRL == TRUE
16 .rts_pad = UART_RTS,
17 .cts_pad = UART_CTS,
18#endif
19};
20
21static THD_WORKING_AREA(waLEDThread, 64);
22static THD_FUNCTION(LEDThread, arg) {
23 (void)arg;
24
25 chRegSetThreadName("blinker");
26 palSetPadMode(IOPORT1, LED1, PAL_MODE_OUTPUT_PUSHPULL);
27
28 while (1) {
29 palTogglePad(IOPORT1, LED1);
30 chThdSleepMilliseconds(500);
31 }
32}
33
34static nrf52_config_t radiocfg = {
35 .protocol = NRF52_PROTOCOL_ESB_DPL,
36 .mode = NRF52_MODE_PRX,
37 .bitrate = NRF52_BITRATE_1MBPS,
38 .crc = NRF52_CRC_8BIT,
39 .tx_power = NRF52_TX_POWER_0DBM,
40 .tx_mode = NRF52_TXMODE_MANUAL_START,
41 .selective_auto_ack = false,
42 .retransmit = { 1000, 3 },
43 .payload_length = 0,
44 .address = {
45 .base_addr_p0 = { 0xF3, 0xF3, 0xF3, 0x01 },
46 .base_addr_p1 = { 0x3F, 0x3F, 0x3F, 0x01 },
47 .pipe_prefixes = { 0xF3, 0x3F, },
48 .num_pipes = 2,
49 .addr_length = 5,
50 .rx_pipes = 1 << 0,
51 .rf_channel = 1,
52 },
53};
54
55static uint16_t cnt, fail_pkt, good_pkt;
56static nrf52_payload_t tx_payload = {
57 .pipe = 1,
58};
59static nrf52_payload_t rx_payload;
60
61static THD_WORKING_AREA(waRadioThread, 256);
62static THD_FUNCTION(RadioThread, arg) {
63 (void)arg;
64
65 event_listener_t el;
66 chEvtRegisterMask(&RFD1.eventsrc, &el, EVENT_MASK(0));
67
68 chRegSetThreadName("radio");
69
70 while (1) {
71 chEvtWaitAny(EVENT_MASK(0));
72 eventflags_t flags = chEvtGetAndClearFlags(&el);
73 if (flags & NRF52_EVENT_TX_SUCCESS) {
74 radio_start_rx();
75 good_pkt++;
76 }
77 if (flags & NRF52_EVENT_TX_FAILED) {
78 radio_start_rx();
79 fail_pkt++;
80 }
81 if (flags & NRF52_EVENT_RX_RECEIVED) {
82 memset(rx_payload.data, 0, 32);
83 radio_read_rx_payload(&rx_payload);
84 }
85 }
86}
87
88/**@brief Function for application main entry.
89 */
90int main(void) {
91
92 halInit();
93 chSysInit();
94
95 sdStart(&SD1, &serial_config);
96
97 chThdCreateStatic(waLEDThread, sizeof(waLEDThread), NORMALPRIO, LEDThread, NULL);
98 chThdCreateStatic(waRadioThread, sizeof(waRadioThread), NORMALPRIO, RadioThread, NULL);
99
100 radio_init(&radiocfg);
101 radio_flush_tx();
102 radio_flush_rx();
103 radio_start_rx();
104
105 cnt = good_pkt = fail_pkt = 0;
106
107 while (true) {
108 memset(tx_payload.data, 0, 32);
109 sprintf((char*)tx_payload.data, "counter value=%d" , cnt++);
110 tx_payload.length = strlen((char *)tx_payload.data);
111 radio_stop_rx();
112 radio_write_payload(&tx_payload);
113 radio_start_tx();
114 chprintf((BaseSequentialStream *)&SD1, "packets: good=%d, fail=%d, sent=%s\r\n", good_pkt, fail_pkt, tx_payload.data);
115 chThdSleepMilliseconds(500);
116 if (strlen((char*) rx_payload.data)) {
117 chprintf((BaseSequentialStream *)&SD1, "rssi=%d, received=%s\r\n", rx_payload.rssi, rx_payload.data);
118 rx_payload.data[0] = 0;
119 }
120 }
121}
122