-
Notifications
You must be signed in to change notification settings - Fork 20
/
radio_demo.c
952 lines (735 loc) · 28.4 KB
/
radio_demo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
#include <memory.h>
#include "support.h"
#include "nrf24.h"
//
// Created by ilia.motornyi on 13-Dec-18.
//
// Buffer to store a payload of maximum width
#define HEX_CHARS "0123456789ABCDEF"
#ifdef USE_HAL_DRIVER
extern UART_HandleTypeDef huart2;
void UART_SendChar(char b) {
HAL_UART_Transmit(&huart2, (uint8_t *) &b, 1, 200);
}
void UART_SendStr(char *string) {
HAL_UART_Transmit(&huart2, (uint8_t *) string, (uint16_t) strlen(string), 200);
}
void Toggle_LED() {
HAL_GPIO_TogglePin(LD2_GPIO_Port,LD2_Pin);
}
#else //USE_HAL_DRIVER
void UART_SendChar(char b) {
while (!LL_USART_IsActiveFlag_TXE(USART2)) {}
LL_USART_TransmitData8(USART2, (uint8_t) b);
}
void UART_SendStr(char *string) {
for (; (*string) != 0; string++) {
UART_SendChar(*string);
}
}
void Toggle_LED() {
LL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
}
#endif
void UART_SendBufHex(char *buf, uint16_t bufsize) {
uint16_t i;
char ch;
for (i = 0; i < bufsize; i++) {
ch = *buf++;
UART_SendChar(HEX_CHARS[(ch >> 4) % 0x10]);
UART_SendChar(HEX_CHARS[(ch & 0x0f) % 0x10]);
}
}
void UART_SendHex8(uint16_t num) {
UART_SendChar(HEX_CHARS[(num >> 4) % 0x10]);
UART_SendChar(HEX_CHARS[(num & 0x0f) % 0x10]);
}
void UART_SendInt(int32_t num) {
char str[10]; // 10 chars max for INT32_MAX
int i = 0;
if (num < 0) {
UART_SendChar('-');
num *= -1;
}
do str[i++] = (char) (num % 10 + '0'); while ((num /= 10) > 0);
for (i--; i >= 0; i--) UART_SendChar(str[i]);
}
uint8_t nRF24_payload[32];
// Pipe number
nRF24_RXResult pipe;
uint32_t i, j, k;
// Length of received payload
uint8_t payload_length;
#define DEMO_RX_SINGLE 0 // Single address receiver (1 pipe)
#define DEMO_RX_MULTI 0 // Multiple address receiver (3 pipes)
#define DEMO_RX_SOLAR 0 // Solar temperature sensor receiver
#define DEMO_TX_SINGLE 0 // Single address transmitter (1 pipe)
#define DEMO_TX_MULTI 0 // Multiple address transmitter (3 pipes)
#define DEMO_RX_SINGLE_ESB 0 // Single address receiver with Enhanced ShockBurst (1 pipe)
#define DEMO_TX_SINGLE_ESB 0 // Single address transmitter with Enhanced ShockBurst (1 pipe)
#define DEMO_RX_ESB_ACK_PL 1 // Single address receiver with Enhanced ShockBurst (1 pipe) + payload sent back
#define DEMO_TX_ESB_ACK_PL 0 // Single address transmitter with Enhanced ShockBurst (1 pipe) + payload received in ACK
// Kinda foolproof :)
#if ((DEMO_RX_SINGLE + DEMO_RX_MULTI + DEMO_RX_SOLAR + DEMO_TX_SINGLE + DEMO_TX_MULTI + DEMO_RX_SINGLE_ESB + DEMO_TX_SINGLE_ESB + DEMO_RX_ESB_ACK_PL + DEMO_TX_ESB_ACK_PL) != 1)
#error "Define only one DEMO_xx, use the '1' value"
#endif
#if ((DEMO_TX_SINGLE) || (DEMO_TX_MULTI) || (DEMO_TX_SINGLE_ESB))
// Helpers for transmit mode demo
// Timeout counter (depends on the CPU speed)
// Used for not stuck waiting for IRQ
#define nRF24_WAIT_TIMEOUT (uint32_t)0x000FFFFF
// Result of packet transmission
typedef enum {
nRF24_TX_ERROR = (uint8_t)0x00, // Unknown error
nRF24_TX_SUCCESS, // Packet has been transmitted successfully
nRF24_TX_TIMEOUT, // It was timeout during packet transmit
nRF24_TX_MAXRT // Transmit failed with maximum auto retransmit count
} nRF24_TXResult;
nRF24_TXResult tx_res;
// Function to transmit data packet
// input:
// pBuf - pointer to the buffer with data to transmit
// length - length of the data buffer in bytes
// return: one of nRF24_TX_xx values
nRF24_TXResult nRF24_TransmitPacket(uint8_t *pBuf, uint8_t length) {
volatile uint32_t wait = nRF24_WAIT_TIMEOUT;
uint8_t status;
// Deassert the CE pin (in case if it still high)
nRF24_CE_L();
// Transfer a data from the specified buffer to the TX FIFO
nRF24_WritePayload(pBuf, length);
// Start a transmission by asserting CE pin (must be held at least 10us)
nRF24_CE_H();
// Poll the transceiver status register until one of the following flags will be set:
// TX_DS - means the packet has been transmitted
// MAX_RT - means the maximum number of TX retransmits happened
// note: this solution is far from perfect, better to use IRQ instead of polling the status
do {
status = nRF24_GetStatus();
if (status & (nRF24_FLAG_TX_DS | nRF24_FLAG_MAX_RT)) {
break;
}
} while (wait--);
// Deassert the CE pin (Standby-II --> Standby-I)
nRF24_CE_L();
if (!wait) {
// Timeout
return nRF24_TX_TIMEOUT;
}
// Check the flags in STATUS register
UART_SendStr("[");
UART_SendHex8(status);
UART_SendStr("] ");
// Clear pending IRQ flags
nRF24_ClearIRQFlags();
if (status & nRF24_FLAG_MAX_RT) {
// Auto retransmit counter exceeds the programmed maximum limit (FIFO is not removed)
return nRF24_TX_MAXRT;
}
if (status & nRF24_FLAG_TX_DS) {
// Successful transmission
return nRF24_TX_SUCCESS;
}
// Some banana happens, a payload remains in the TX FIFO, flush it
nRF24_FlushTX();
return nRF24_TX_ERROR;
}
#endif // DEMO_TX_
int runRadio(void) {
UART_SendStr("\r\nSTM32F303RE is online.\r\n");
// RX/TX disabled
nRF24_CE_L();
// Configure the nRF24L01+
UART_SendStr("nRF24L01+ check: ");
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
if (!nRF24_Check()) {
UART_SendStr("FAIL\r\n");
while (1) {
Toggle_LED();
Delay_ms(50);
}
}
#pragma clang diagnostic pop
UART_SendStr("OK\r\n");
// Initialize the nRF24L01 to its default state
nRF24_Init();
/***************************************************************************/
#if (DEMO_RX_SINGLE)
// This is simple receiver with one RX pipe:
// - pipe#1 address: '0xE7 0x1C 0xE3'
// - payload: 5 bytes
// - RF channel: 115 (2515MHz)
// - data rate: 250kbps (minimum possible, to increase reception reliability)
// - CRC scheme: 2 byte
// The transmitter sends a 5-byte packets to the address '0xE7 0x1C 0xE3' without Auto-ACK (ShockBurst disabled)
// Disable ShockBurst for all RX pipes
nRF24_DisableAA(0xFF);
// Set RF channel
nRF24_SetRFChannel(115);
// Set data rate
nRF24_SetDataRate(nRF24_DR_250kbps);
// Set CRC scheme
nRF24_SetCRCScheme(nRF24_CRC_2byte);
// Set address width, its common for all pipes (RX and TX)
nRF24_SetAddrWidth(3);
// Configure RX PIPE#1
static const uint8_t nRF24_ADDR[] = { 0xE7, 0x1C, 0xE3 };
nRF24_SetAddr(nRF24_PIPE1, nRF24_ADDR); // program address for RX pipe #1
nRF24_SetRXPipe(nRF24_PIPE1, nRF24_AA_OFF, 5); // Auto-ACK: disabled, payload length: 5 bytes
// Set operational mode (PRX == receiver)
nRF24_SetOperationalMode(nRF24_MODE_RX);
// Wake the transceiver
nRF24_SetPowerMode(nRF24_PWR_UP);
// Put the transceiver to the RX mode
nRF24_CE_H();
// The main loop
while (1) {
//
// Constantly poll the status of the RX FIFO and get a payload if FIFO is not empty
//
// This is far from best solution, but it's ok for testing purposes
// More smart way is to use the IRQ pin :)
//
if (nRF24_GetStatus_RXFIFO() != nRF24_STATUS_RXFIFO_EMPTY) {
// Get a payload from the transceiver
pipe = nRF24_ReadPayload(nRF24_payload, &payload_length);
// Clear all pending IRQ flags
nRF24_ClearIRQFlags();
// Print a payload contents to UART
UART_SendStr("RCV PIPE#");
UART_SendInt(pipe);
UART_SendStr(" PAYLOAD:>");
UART_SendBufHex((char *)nRF24_payload, payload_length);
UART_SendStr("<\r\n");
}
}
#endif // DEMO_RX_SINGLE
/***************************************************************************/
#if (DEMO_RX_MULTI)
// This is simple receiver with multiple RX pipes:
// - pipe#0 address: "WBC"
// - pipe#0 payload: 11 bytes
// - pipe#1 address: '0xE7 0x1C 0xE3'
// - pipe#1 payload: 5 bytes
// - pipe#4 address: '0xE7 0x1C 0xE6' (this is pipe#1 address with different last byte)
// - pipe#4 payload: 32 bytes (the maximum payload length)
// - RF channel: 115 (2515MHz)
// - data rate: 250kbps (minimum possible, to increase reception reliability)
// - CRC scheme: 2 byte
// The transmitter sends packets of different length to the three different logical addresses,
// cycling them one after another, that packets comes to different pipes (0, 1 and 4)
// Disable ShockBurst for all RX pipes
nRF24_DisableAA(0xFF);
// Set RF channel
nRF24_SetRFChannel(115);
// Set data rate
nRF24_SetDataRate(nRF24_DR_250kbps);
// Set CRC scheme
nRF24_SetCRCScheme(nRF24_CRC_2byte);
// Set address width, its common for all pipes (RX and TX)
nRF24_SetAddrWidth(3);
// Configure RX PIPE#0
static const uint8_t nRF24_ADDR0[] = { 'W', 'B', 'C' };
nRF24_SetAddr(nRF24_PIPE0, nRF24_ADDR0); // program address for RX pipe #0
nRF24_SetRXPipe(nRF24_PIPE0, nRF24_AA_OFF, 11); // Auto-ACK: disabled, payload length: 11 bytes
// Configure RX PIPE#1
static const uint8_t nRF24_ADDR1[] = { 0xE7, 0x1C, 0xE3 };
nRF24_SetAddr(nRF24_PIPE1, nRF24_ADDR1); // program address for RX pipe #1
nRF24_SetRXPipe(nRF24_PIPE1, nRF24_AA_OFF, 5); // Auto-ACK: disabled, payload length: 5 bytes
// Configure RX PIPE#4
static const uint8_t nRF24_ADDR4[] = { 0xE6 };
nRF24_SetAddr(nRF24_PIPE4, nRF24_ADDR4); // program address for RX pipe #4
nRF24_SetRXPipe(nRF24_PIPE4, nRF24_AA_OFF, 32); // Auto-ACK: disabled, payload length: 32 bytes
// Set operational mode (PRX == receiver)
nRF24_SetOperationalMode(nRF24_MODE_RX);
// Wake the transceiver
nRF24_SetPowerMode(nRF24_PWR_UP);
// Put the transceiver to the RX mode
nRF24_CE_H();
// The main loop
while (1) {
//
// Constantly poll the status of the RX FIFO and get a payload if FIFO is not empty
//
// This is far from best solution, but it's ok for testing purposes
// More smart way is to use the IRQ pin :)
//
if (nRF24_GetStatus_RXFIFO() != nRF24_STATUS_RXFIFO_EMPTY) {
// Get a payload from the transceiver
pipe = nRF24_ReadPayload(nRF24_payload, &payload_length);
// Clear all pending IRQ flags
nRF24_ClearIRQFlags();
// Print a payload contents to UART
UART_SendStr("RCV PIPE#");
UART_SendInt(pipe);
UART_SendStr(" PAYLOAD:>");
UART_SendBufHex((char *)nRF24_payload, payload_length);
UART_SendStr("<\r\n");
}
}
#endif // DEMO_RX_MULTI
/***************************************************************************/
#if (DEMO_RX_SOLAR)
// This part is to receive data packets from the old solar-powered temperature sensor
// Set RF channel
nRF24_SetRFChannel(110);
// Set data rate
nRF24_SetDataRate(nRF24_DR_1Mbps);
// Set CRC scheme
nRF24_SetCRCScheme(nRF24_CRC_2byte);
// Set address width, its common for all pipes (RX and TX)
nRF24_SetAddrWidth(5);
// Configure RX PIPE#0
#define RX_PAYLOAD 18
// That address written here in reverse order because the old device that
// transmits data uses old nRF24 "library" with errors :)
static const uint8_t nRF24_ADDR[] = { 'T', 'k', 'l', 'o', 'W' };
nRF24_SetAddr(nRF24_PIPE0, nRF24_ADDR); // program pipe address
nRF24_SetRXPipe(nRF24_PIPE0, nRF24_AA_ON, RX_PAYLOAD); // Auto-ACK: enabled, payload length: 18 bytes
// Configure TX PIPE address, this must be done for Auto-ACK (a.k.a. ShockBurst)
// The address of TX PIPE must be same as it configured on the transmitter side
nRF24_SetAddr(nRF24_PIPETX, nRF24_ADDR);
// Set TX power to maximum (for more reliable Auto-ACK)
nRF24_SetTXPower(nRF24_TXPWR_0dBm);
// Set operational mode (PRX == receiver)
nRF24_SetOperationalMode(nRF24_MODE_RX);
// Wake the transceiver
nRF24_SetPowerMode(nRF24_PWR_UP);
// Put the transceiver to the RX mode
nRF24_CE_H();
int16_t temp;
uint16_t vrefint;
uint16_t LSI_freq;
uint8_t TR1, TR2, TR3;
uint8_t DR1, DR2, DR3;
uint8_t hours, minutes, seconds;
uint8_t day, month, year;
// The main loop
while (1) {
if (nRF24_GetStatus_RXFIFO() != nRF24_STATUS_RXFIFO_EMPTY) {
// Get a payload from the transceiver
pipe = nRF24_ReadPayload(nRF24_payload, &payload_length);
// Clear all pending IRQ flags
nRF24_ClearIRQFlags();
// Print a payload contents to UART
UART_SendStr("RX=["); UART_SendBufHex((char *)nRF24_payload, payload_length);
UART_SendStr("] CRC=(");
UART_SendHex8(nRF24_payload[payload_length - 1]);
UART_SendStr(")\r\nTemperature: ");
temp = (nRF24_payload[0] << 8) | nRF24_payload[1];
if (temp < 0) {
temp *= -1;
UART_SendChar('-');
} else {
UART_SendChar('+');
}
UART_SendInt(temp / 10); UART_SendChar('.');
temp %= 10;
UART_SendInt(temp % 10); UART_SendStr("C\r\n");
UART_SendStr("Packet: #");
UART_SendInt((uint32_t)((nRF24_payload[2] << 24) | (nRF24_payload[3] << 16) | (nRF24_payload[4] << 8) | (nRF24_payload[5])));
UART_SendStr("\r\n");
vrefint = (nRF24_payload[6] << 8) + nRF24_payload[7];
UART_SendStr("Vcc: ");
UART_SendInt(vrefint / 100);
UART_SendChar('.');
UART_SendInt0(vrefint % 100);
UART_SendStr("V\r\n");
LSI_freq = (nRF24_payload[14] << 8) + nRF24_payload[15];
UART_SendStr("LSI: ");
UART_SendInt(LSI_freq);
UART_SendStr("Hz\r\nOBSERVE_TX:\r\n\t");
UART_SendHex8(nRF24_payload[16] >> 4);
UART_SendStr(" pckts lost\r\n\t");
UART_SendHex8(nRF24_payload[16] & 0x0F);
UART_SendStr(" retries\r\n");
TR1 = nRF24_payload[8];
TR2 = nRF24_payload[9];
TR3 = nRF24_payload[10];
DR1 = nRF24_payload[11];
DR2 = nRF24_payload[12];
DR3 = nRF24_payload[13];
seconds = ((TR1 >> 4) * 10) + (TR1 & 0x0F);
minutes = ((TR2 >> 4) * 10) + (TR2 & 0x0F);
hours = (((TR3 & 0x30) >> 4) * 10) + (TR3 & 0x0F);
day = ((DR1 >> 4) * 10) + (DR1 & 0x0F);
//dow = DR2 >> 5;
month = (((DR2 & 0x1F) >> 4) * 10) + (DR2 & 0x0F);
year = ((DR3 >> 4) * 10) + (DR3 & 0x0F);
UART_SendStr("Uptime: ");
UART_SendInt0(hours);
UART_SendChar(':');
UART_SendInt0(minutes);
UART_SendChar(':');
UART_SendInt0(seconds);
UART_SendChar(' ');
UART_SendInt0(day);
UART_SendChar('.');
UART_SendInt0(month);
UART_SendStr(".20");
UART_SendInt0(year);
UART_SendStr("\r\n");
}
}
#endif // DEMO_RX_SOLAR
/***************************************************************************/
#if (DEMO_TX_SINGLE)
// This is simple transmitter (to one logic address):
// - TX address: '0xE7 0x1C 0xE3'
// - payload: 5 bytes
// - RF channel: 115 (2515MHz)
// - data rate: 250kbps (minimum possible, to increase reception reliability)
// - CRC scheme: 2 byte
// The transmitter sends a 5-byte packets to the address '0xE7 0x1C 0xE3' without Auto-ACK (ShockBurst disabled)
// Disable ShockBurst for all RX pipes
nRF24_DisableAA(0xFF);
// Set RF channel
nRF24_SetRFChannel(115);
// Set data rate
nRF24_SetDataRate(nRF24_DR_250kbps);
// Set CRC scheme
nRF24_SetCRCScheme(nRF24_CRC_2byte);
// Set address width, its common for all pipes (RX and TX)
nRF24_SetAddrWidth(3);
// Configure TX PIPE
static const uint8_t nRF24_ADDR[] = { 0xE7, 0x1C, 0xE3 };
nRF24_SetAddr(nRF24_PIPETX, nRF24_ADDR); // program TX address
// Set TX power (maximum)
nRF24_SetTXPower(nRF24_TXPWR_0dBm);
// Set operational mode (PTX == transmitter)
nRF24_SetOperationalMode(nRF24_MODE_TX);
// Clear any pending IRQ flags
nRF24_ClearIRQFlags();
// Wake the transceiver
nRF24_SetPowerMode(nRF24_PWR_UP);
// The main loop
j = 0;
payload_length = 5;
while (1) {
// Prepare data packet
for (i = 0; i < payload_length; i++) {
nRF24_payload[i] = j++;
if (j > 0x000000FF) j = 0;
}
// Print a payload
UART_SendStr("PAYLOAD:>");
UART_SendBufHex((char *)nRF24_payload, payload_length);
UART_SendStr("< ... TX: ");
// Transmit a packet
tx_res = nRF24_TransmitPacket(nRF24_payload, payload_length);
switch (tx_res) {
case nRF24_TX_SUCCESS:
UART_SendStr("OK");
break;
case nRF24_TX_TIMEOUT:
UART_SendStr("TIMEOUT");
break;
case nRF24_TX_MAXRT:
UART_SendStr("MAX RETRANSMIT");
break;
default:
UART_SendStr("ERROR");
break;
}
UART_SendStr("\r\n");
// Wait ~0.5s
Delay_ms(500);
}
#endif // DEMO_TX_SINGLE
/***************************************************************************/
#if (DEMO_TX_MULTI)
// This is simple transmitter (to multiple logic addresses):
// - TX addresses and payload lengths:
// 'WBC', 11 bytes
// '0xE7 0x1C 0xE3', 5 bytes
// '0xE7 0x1C 0xE6', 32 bytes
// - RF channel: 115 (2515MHz)
// - data rate: 250kbps (minimum possible, to increase reception reliability)
// - CRC scheme: 2 byte
// The transmitter sends a data packets to the three logic addresses without Auto-ACK (ShockBurst disabled)
// The payload length depends on the logic address
// Disable ShockBurst for all RX pipes
nRF24_DisableAA(0xFF);
// Set RF channel
nRF24_SetRFChannel(115);
// Set data rate
nRF24_SetDataRate(nRF24_DR_250kbps);
// Set CRC scheme
nRF24_SetCRCScheme(nRF24_CRC_2byte);
// Set address width, its common for all pipes (RX and TX)
nRF24_SetAddrWidth(3);
// Set TX power (maximum)
nRF24_SetTXPower(nRF24_TXPWR_0dBm);
// Set operational mode (PTX == transmitter)
nRF24_SetOperationalMode(nRF24_MODE_TX);
// Clear any pending IRQ flags
nRF24_ClearIRQFlags();
// Wake the transceiver
nRF24_SetPowerMode(nRF24_PWR_UP);
static const uint8_t nRF24_ADDR0[] = { 'W', 'B', 'C' };
static const uint8_t nRF24_ADDR1[] = { 0xE7, 0x1C, 0xE3 };
static const uint8_t nRF24_ADDR2[] = { 0xE7, 0x1C, 0xE6 };
// The main loop
j = 0; pipe = 0;
while (1) {
// Logic address
UART_SendStr("ADDR#");
UART_SendInt(pipe);
// Configure the TX address and payload length
switch (pipe) {
case 0:
// addr #1
nRF24_SetAddr(nRF24_PIPETX, nRF24_ADDR0);
payload_length = 11;
break;
case 1:
// addr #2
nRF24_SetAddr(nRF24_PIPETX, nRF24_ADDR1);
payload_length = 5;
break;
case 2:
// addr #3
nRF24_SetAddr(nRF24_PIPETX, nRF24_ADDR2);
payload_length = 32;
break;
default:
break;
}
// Prepare data packet
for (i = 0; i < payload_length; i++) {
nRF24_payload[i] = j++;
if (j > 0x000000FF) j = 0;
}
// Print a payload
UART_SendStr(" PAYLOAD:>");
UART_SendBufHex((char *)nRF24_payload, payload_length);
UART_SendStr("< ... TX: ");
// Transmit a packet
tx_res = nRF24_TransmitPacket(nRF24_payload, payload_length);
switch (tx_res) {
case nRF24_TX_SUCCESS:
UART_SendStr("OK");
break;
case nRF24_TX_TIMEOUT:
UART_SendStr("TIMEOUT");
break;
case nRF24_TX_MAXRT:
UART_SendStr("MAX RETRANSMIT");
break;
default:
UART_SendStr("ERROR");
break;
}
UART_SendStr("\r\n");
// Proceed to next address
pipe++;
if (pipe > 2) {
pipe = 0;
}
// Wait ~0.5s
Delay_ms(500);
}
#endif // DEMO_TX_MULTI
/***************************************************************************/
#if (DEMO_RX_SINGLE_ESB)
// This is simple receiver with Enhanced ShockBurst:
// - RX address: 'ESB'
// - payload: 10 bytes
// - RF channel: 40 (2440MHz)
// - data rate: 2Mbps
// - CRC scheme: 2 byte
// The transmitter sends a 10-byte packets to the address 'ESB' with Auto-ACK (ShockBurst enabled)
// Set RF channel
nRF24_SetRFChannel(40);
// Set data rate
nRF24_SetDataRate(nRF24_DR_2Mbps);
// Set CRC scheme
nRF24_SetCRCScheme(nRF24_CRC_2byte);
// Set address width, its common for all pipes (RX and TX)
nRF24_SetAddrWidth(3);
// Configure RX PIPE
static const uint8_t nRF24_ADDR[] = {'E', 'S', 'B'};
nRF24_SetAddr(nRF24_PIPE1, nRF24_ADDR); // program address for pipe
nRF24_SetRXPipe(nRF24_PIPE1, nRF24_AA_ON, 10); // Auto-ACK: enabled, payload length: 10 bytes
// Set TX power for Auto-ACK (maximum, to ensure that transmitter will hear ACK reply)
nRF24_SetTXPower(nRF24_TXPWR_0dBm);
// Set operational mode (PRX == receiver)
nRF24_SetOperationalMode(nRF24_MODE_RX);
// Clear any pending IRQ flags
nRF24_ClearIRQFlags();
// Wake the transceiver
nRF24_SetPowerMode(nRF24_PWR_UP);
// Put the transceiver to the RX mode
nRF24_CE_H();
// The main loop
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
while (1) {
//
// Constantly poll the status of the RX FIFO and get a payload if FIFO is not empty
//
// This is far from best solution, but it's ok for testing purposes
// More smart way is to use the IRQ pin :)
//
if (nRF24_GetStatus_RXFIFO() != nRF24_STATUS_RXFIFO_EMPTY) {
// Get a payload from the transceiver
pipe = nRF24_ReadPayload(nRF24_payload, &payload_length);
// Clear all pending IRQ flags
nRF24_ClearIRQFlags();
// Print a payload contents to UART
UART_SendStr("RCV PIPE#");
UART_SendInt(pipe);
UART_SendStr(" PAYLOAD:>");
Toggle_LED();
UART_SendBufHex((char *) nRF24_payload, payload_length);
UART_SendStr("<\r\n");
}
}
#pragma clang diagnostic pop
#endif // DEMO_RX_SINGLE_ESB
/***************************************************************************/
#if (DEMO_RX_ESB_ACK_PL)
// This is simple receiver with Enhanced ShockBurst:
// - RX address: 'ESB'
// - payload: 10 bytes
// - RF channel: 40 (2440MHz)
// - data rate: 2Mbps
// - CRC scheme: 2 byte
// The transmitter sends a 10-byte packets to the address 'ESB' with Auto-ACK (ShockBurst enabled)
// Set RF channel
nRF24_SetRFChannel(40);
// Set data rate
nRF24_SetDataRate(nRF24_DR_2Mbps);
// Set CRC scheme
nRF24_SetCRCScheme(nRF24_CRC_2byte);
// Set address width, its common for all pipes (RX and TX)
nRF24_SetAddrWidth(3);
// Configure RX PIPE
static const uint8_t nRF24_ADDR[] = {'E', 'S', 'B'};
nRF24_SetAddr(nRF24_PIPE1, nRF24_ADDR); // program address for pipe
nRF24_SetRXPipe(nRF24_PIPE1, nRF24_AA_ON, 10); // Auto-ACK: enabled, payload length: 10 bytes
// Set TX power for Auto-ACK (maximum, to ensure that transmitter will hear ACK reply)
nRF24_SetTXPower(nRF24_TXPWR_0dBm);
// Set operational mode (PRX == receiver)
nRF24_SetOperationalMode(nRF24_MODE_RX);
// Clear any pending IRQ flags
nRF24_ClearIRQFlags();
// Wake the transceiver
nRF24_SetPowerMode(nRF24_PWR_UP);
// Enable DPL
nRF24_SetDynamicPayloadLength(nRF24_DPL_ON);
nRF24_SetPayloadWithAck(1);
// Put the transceiver to the RX mode
nRF24_CE_H();
// The main loop
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
while (1) {
//
// Constantly poll the status of the RX FIFO and get a payload if FIFO is not empty
//
// This is far from best solution, but it's ok for testing purposes
// More smart way is to use the IRQ pin :)
//
if (nRF24_GetStatus_RXFIFO() != nRF24_STATUS_RXFIFO_EMPTY) {
// Get a payload from the transceiver
pipe = nRF24_ReadPayloadDpl(nRF24_payload, &payload_length);
if(payload_length > 0) {
nRF24_WriteAckPayload(pipe, "aCk PaYlOaD",11);
}
// Clear all pending IRQ flags
nRF24_ClearIRQFlags();
// Print a payload contents to UART
UART_SendStr("RCV PIPE#");
UART_SendInt(pipe);
UART_SendStr(" PAYLOAD:>");
Toggle_LED();
UART_SendBufHex((char *) nRF24_payload, payload_length);
UART_SendStr("<\r\n");
}
}
#pragma clang diagnostic pop
#endif // DEMO_RX_SINGLE_ESB
/***************************************************************************/
#if (DEMO_TX_SINGLE_ESB)
// This is simple transmitter with Enhanced ShockBurst (to one logic address):
// - TX address: 'ESB'
// - payload: 10 bytes
// - RF channel: 40 (2440MHz)
// - data rate: 2Mbps
// - CRC scheme: 2 byte
// The transmitter sends a 10-byte packets to the address 'ESB' with Auto-ACK (ShockBurst enabled)
// Set RF channel
nRF24_SetRFChannel(40);
// Set data rate
nRF24_SetDataRate(nRF24_DR_2Mbps);
// Set CRC scheme
nRF24_SetCRCScheme(nRF24_CRC_2byte);
// Set address width, its common for all pipes (RX and TX)
nRF24_SetAddrWidth(3);
// Configure TX PIPE
static const uint8_t nRF24_ADDR[] = { 'E', 'S', 'B' };
nRF24_SetAddr(nRF24_PIPETX, nRF24_ADDR); // program TX address
nRF24_SetAddr(nRF24_PIPE0, nRF24_ADDR); // program address for pipe#0, must be same as TX (for Auto-ACK)
// Set TX power (maximum)
nRF24_SetTXPower(nRF24_TXPWR_0dBm);
// Configure auto retransmit: 10 retransmissions with pause of 2500s in between
nRF24_SetAutoRetr(nRF24_ARD_2500us, 10);
// Enable Auto-ACK for pipe#0 (for ACK packets)
nRF24_EnableAA(nRF24_PIPE0);
// Set operational mode (PTX == transmitter)
nRF24_SetOperationalMode(nRF24_MODE_TX);
// Clear any pending IRQ flags
nRF24_ClearIRQFlags();
// Wake the transceiver
nRF24_SetPowerMode(nRF24_PWR_UP);
// Some variables
uint32_t packets_lost = 0; // global counter of lost packets
uint8_t otx;
uint8_t otx_plos_cnt; // lost packet count
uint8_t otx_arc_cnt; // retransmit count
// The main loop
payload_length = 10;
j = 0;
while (1) {
// Prepare data packet
for (i = 0; i < payload_length; i++) {
nRF24_payload[i] = j++;
if (j > 0x000000FF) j = 0;
}
// Print a payload
UART_SendStr("PAYLOAD:>");
UART_SendBufHex((char *)nRF24_payload, payload_length);
UART_SendStr("< ... TX: ");
// Transmit a packet
tx_res = nRF24_TransmitPacket(nRF24_payload, payload_length);
otx = nRF24_GetRetransmitCounters();
otx_plos_cnt = (otx & nRF24_MASK_PLOS_CNT) >> 4; // packets lost counter
otx_arc_cnt = (otx & nRF24_MASK_ARC_CNT); // auto retransmissions counter
switch (tx_res) {
case nRF24_TX_SUCCESS:
UART_SendStr("OK");
break;
case nRF24_TX_TIMEOUT:
UART_SendStr("TIMEOUT");
break;
case nRF24_TX_MAXRT:
UART_SendStr("MAX RETRANSMIT");
packets_lost += otx_plos_cnt;
nRF24_ResetPLOS();
break;
default:
UART_SendStr("ERROR");
break;
}
UART_SendStr(" ARC=");
UART_SendInt(otx_arc_cnt);
UART_SendStr(" LOST=");
UART_SendInt(packets_lost);
UART_SendStr("\r\n");
// Wait ~0.5s
Delay_ms(500);
}
#endif // DEMO_TX_SINGLE_ESB
}