nrf_to_nrf - NRF52 Radio Driver v1.2.2
TMRh20 2023 - OSI Layer 2 radio driver using RF24 API
Loading...
Searching...
No Matches
nrf_to_nrf.cpp
Go to the documentation of this file.
1
2
3#include "nrf_to_nrf.h"
4
5#if defined NRF54_CCM_ENCRYPTION
6 #include <nrf54l15_hal.h>
7static xiao_nrf54l15::Ccm nrf54Ccm;
8static xiao_nrf54l15::CracenRng nrf54Rng;
9#endif
10
11#ifndef __REV
12 #define __REV(x) __builtin_bswap32(x);
13#endif
14
15#if defined(NRF52832_XXAA) || defined(NRF52832_XXAB) || defined(NRF52811_XXAA) || defined(NRF52810_XXAA) || defined(NRF52805_XXAA)
16 // TX power range (Product Specification): -20 .. +4dbm, configurable in 4 dB steps
17 #define TXPOWER_PA_MIN 0xF4 // -12dBm
18 #define TXPOWER_PA_LOW 0xFC // -4dBm
19 #define TXPOWER_PA_HIGH 0x00 // 0dBm
20 #define TXPOWER_PA_MAX 0x04 // 4dBm
21#elif !defined(ARDUINO_NRF54L15) // nRF52840, nRF52833, nRF52820
22 // TX power range (Product Specification): -20 .. +8dbm, configurable in 4 dB steps
23 #define TXPOWER_PA_MIN 0xF4 // -12dBm
24 #define TXPOWER_PA_LOW 0x02 // 2dBm
25 #define TXPOWER_PA_HIGH 0x06 // 6dBm
26 #define TXPOWER_PA_MAX 0x08 // 8dBm
27#else
28 #define TXPOWER_PA_MIN 0x6 // -12dBm
29 #define TXPOWER_PA_LOW 0x1F // 2dBm
30 #define TXPOWER_PA_HIGH 0x33 // 6dBm
31 #define TXPOWER_PA_MAX 0x3F // 8dBm
32#endif
33// Note that 250Kbit mode is deprecated and might not work reliably on all devices.
34// See: https://devzone.nordicsemi.com/f/nordic-q-a/78469/250-kbit-s-nordic-proprietary-radio-mode-on-nrf52840
35#ifndef RADIO_MODE_MODE_Nrf_250Kbit
36 #define RADIO_MODE_MODE_Nrf_250Kbit (2UL)
37#endif
38#ifdef ARDUINO_NRF54L15
39 #define RADIO_MODE_MODE_Nrf_4Mbit_OBT4 (10UL)
40 #define RADIO_MODE_MODE_Nrf_4Mbit_OBT6 (9UL)
41#endif
42
43#define DEFAULT_TIMEOUT 250
44
45/**********************************************************************************************************/
46
47// The count ends the wait if millis() doesn't advance inside the radio interrupt
48static bool waitForEvent(volatile uint32_t* event, uint32_t timeout = DEFAULT_TIMEOUT)
49{
50 uint32_t start = millis();
51 for (uint32_t count = 0; *event == 0; count++) {
52 if (millis() - start > timeout || count > 4000000) {
53 return false;
54 }
55 }
56 return true;
57}
58
59/**********************************************************************************************************/
60
61#if defined(ARDUINO_NRF54L15)
62 #define RX_IRQ_NUMBER RADIO_1_IRQn
63 #define RX_IRQ_MASK (RADIO_INTENSET10_CRCOK_Msk | RADIO_INTENSET10_CRCERROR_Msk)
64 #define RX_IRQ_SET INTENSET10
65 #define RX_IRQ_CLEAR INTENCLR10
66 #define RX_IRQ_HANDLER RADIO_1_IRQHandler // The nRF54 HAL defines RADIO_0_IRQHandler
67#else
68 #define RX_IRQ_NUMBER RADIO_IRQn
69 #define RX_IRQ_MASK (RADIO_INTENSET_CRCOK_Msk | RADIO_INTENSET_CRCERROR_Msk)
70 #define RX_IRQ_SET INTENSET
71 #define RX_IRQ_CLEAR INTENCLR
72 #if defined(__MBED__)
73 #define RX_IRQ_HANDLER RADIO_IRQHandler_v
74 #else
75 #define RX_IRQ_HANDLER RADIO_IRQHandler
76 #endif
77#endif
78
79// Only enabled by enableRxInterrupt(), which also sets the radio to use
80static nrf_to_nrf* rxIrqRadio = nullptr;
81
82extern "C" __attribute__((__used__)) void RX_IRQ_HANDLER(void)
83{
84 if (rxIrqRadio == nullptr) {
85 NRF_RADIO->RX_IRQ_CLEAR = RX_IRQ_MASK;
86 (void)NRF_RADIO->RX_IRQ_CLEAR;
87 return;
88 }
90}
91
92static void rxIrqDisable()
93{
94 NRF_RADIO->RX_IRQ_CLEAR = RX_IRQ_MASK;
95 (void)NRF_RADIO->RX_IRQ_CLEAR; // Writes are buffered, so make sure this has taken effect
96}
97
98// Stops the interrupt handler from using buffers and the CCM peripheral while this is in scope
100{
101 bool locked;
102 RadioIrqLock(bool lock) : locked(lock)
103 {
104 if (locked) {
105 NVIC_DisableIRQ(RX_IRQ_NUMBER);
106 }
107 }
109 {
110 if (locked) {
111 NVIC_EnableIRQ(RX_IRQ_NUMBER);
112 }
113 }
114};
115
116/**********************************************************************************************************/
117
118// Function to do bytewise bit-swap on an unsigned 32-bit value
119static uint32_t bytewise_bit_swap(uint8_t const* p_inp)
120{
121 uint32_t inp = (p_inp[3] << 24) | (p_inp[2] << 16) | (p_inp[1] << 8) | (p_inp[0]);
122 inp = (inp & 0xF0F0F0F0) >> 4 | (inp & 0x0F0F0F0F) << 4;
123 inp = (inp & 0xCCCCCCCC) >> 2 | (inp & 0x33333333) << 2;
124 inp = (inp & 0xAAAAAAAA) >> 1 | (inp & 0x55555555) << 1;
125 return inp;
126}
127
128/**********************************************************************************************************/
129
130// Convert a base address from nRF24L format to nRF5 format
131static uint32_t addr_conv(uint8_t const* p_addr)
132{
133 return __REV(
134 bytewise_bit_swap(p_addr)); // lint -esym(628, __rev) -esym(526, __rev) */
135}
136
137/**********************************************************************************************************/
138
139uint32_t nrf_to_nrf::addrConv32(uint32_t addr)
140{
141
142 uint8_t buffer[4];
143 buffer[0] = addr & 0xFF;
144 buffer[1] = (addr >> 8) & 0xFF;
145 buffer[2] = (addr >> 16) & 0xFF;
146 buffer[3] = (addr >> 24) & 0xFF;
147
148 return addr_conv(buffer);
149}
150
151/**********************************************************************************************************/
152
154{
155 DPL = false;
156 staticPayloadSize = 32;
157 retries = 5;
158 retryDuration = 5;
159 ackPayloadsEnabled = false;
160 ackPipe = 0;
161 inRxMode = false;
162 arcCounter = 0;
163 ackTimeout = ACK_TIMEOUT_1MBPS;
164 payloadAvailable = false;
165 rxQueue = nullptr;
166 rxQueueHead = 0;
167 rxQueueTail = 0;
168 ackQueue = nullptr;
169 ackQueueHead = 0;
170 ackQueueCount = 0;
171 ackBuffer[0] = 0;
172 ackPayloadSent = false;
173 inInterrupt = false;
174#ifndef ARDUINO_NRF54L15
175 interframeSpacing = 115;
176#else
177 interframeSpacing = 130;
178#endif
179
180#if defined CCM_ENCRYPTION_ENABLED
181 #if !defined NRF54_CCM_ENCRYPTION
182 NRF_CCM->INPTR = (uint32_t)inBuffer;
183 NRF_CCM->OUTPTR = (uint32_t)outBuffer;
184 NRF_CCM->CNFPTR = (uint32_t)&ccmData;
185 NRF_CCM->SCRATCHPTR = (uint32_t)scratchPTR;
186 #endif
187 ccmData.counter = 12345;
188 enableEncryption = false;
189#endif
190};
191
192/**********************************************************************************************************/
193
195{
196
197#ifndef ARDUINO_NRF54L15
198 NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
199 NRF_CLOCK->TASKS_HFCLKSTART = 1;
200
201 /* Wait for the external oscillator to start up */
202 if (!waitForEvent(&NRF_CLOCK->EVENTS_HFCLKSTARTED))
203 return false;
204#else
205 NRF_POWER->TASKS_CONSTLAT = 1;
206 NRF_CLOCK->EVENTS_XOSTARTED = 0;
207 NRF_CLOCK->TASKS_XOSTART = 1;
208
209 if (!waitForEvent(&NRF_CLOCK->EVENTS_XOSTARTED))
210 return false;
211#endif
212
213 NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
214 NRF_CLOCK->TASKS_LFCLKSTART = 1;
215
216 /* Wait for the low frequency clock to start up */
217 if (!waitForEvent(&NRF_CLOCK->EVENTS_LFCLKSTARTED))
218 return false;
219
220#ifndef ARDUINO_NRF54L15
221 NRF_RADIO->POWER = 1;
222#endif
223
224 NRF_RADIO->PCNF0 = (1 << RADIO_PCNF0_S0LEN_Pos) | (0 << RADIO_PCNF0_LFLEN_Pos) | (1 << RADIO_PCNF0_S1LEN_Pos);
225
226 NRF_RADIO->PCNF1 = (RADIO_PCNF1_WHITEEN_Disabled << RADIO_PCNF1_WHITEEN_Pos) | (RADIO_PCNF1_ENDIAN_Big << RADIO_PCNF1_ENDIAN_Pos) | (4 << RADIO_PCNF1_BALEN_Pos) | (staticPayloadSize << RADIO_PCNF1_STATLEN_Pos) | (staticPayloadSize << RADIO_PCNF1_MAXLEN_Pos);
227
228 NRF_RADIO->BASE0 = 0xE7E7E7E7; /* Base address 0 */
229 NRF_RADIO->BASE1 = 0x43434343;
230 NRF_RADIO->PREFIX0 = 0x23C343E7; /* Prefixes bytes for logical addresses 0 */
231 NRF_RADIO->PREFIX1 = 0x13E363A3;
232 NRF_RADIO->RXADDRESSES = 0x01;
233 NRF_RADIO->TXADDRESS = 0x00;
234 /* Receive address select */
235 txBase = NRF_RADIO->BASE0;
236 txPrefix = NRF_RADIO->PREFIX0;
237 rxBase = NRF_RADIO->BASE0;
238 rxPrefix = NRF_RADIO->PREFIX0;
239 // Configure CRC for 16-bit
240 NRF_RADIO->CRCCNF = RADIO_CRCCNF_LEN_Two; /* CRC configuration: 16bit */
241 NRF_RADIO->CRCINIT = 0xFFFFUL; // Initial value
242 NRF_RADIO->CRCPOLY = 0x11021UL; // CRC poly: x^16+x^12^x^5+1
243
244 NRF_RADIO->PACKETPTR = (uint32_t)radioData;
245 NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_1Mbit << RADIO_MODE_MODE_Pos);
246
247#ifndef ARDUINO_NRF54L15
248 NRF_RADIO->MODECNF0 = 0x201;
249#else
250 NRF_RADIO->TIMING = 0x1;
251#endif
252 NRF_RADIO->TXPOWER = (TXPOWER_PA_MAX << RADIO_TXPOWER_TXPOWER_Pos);
253 NRF_RADIO->FREQUENCY = 0x4C;
254
255 DPL = false;
256 // Enable auto ack on all pipes by default
257 setAutoAck(1);
258
259#ifdef ARDUINO_NRF54L15
260 NRF_RADIO->TASKS_START = 1;
261#endif
263
264 return 1;
265}
266
267/**********************************************************************************************************/
268
269#ifdef NRF_HAS_ENERGY_DETECT
270 #define ED_RSSISCALE 4 // From electrical specifications
271uint8_t nrf_to_nrf::sample_ed(void)
272{
273 int val;
274 RadioIrqLock lock(rxQueue != nullptr);
275 NRF_RADIO->EVENTS_EDEND = 0;
276 NRF_RADIO->TASKS_EDSTART = 1; // Start
277 if (!waitForEvent(&NRF_RADIO->EVENTS_EDEND))
278 return 0;
279
280 val = NRF_RADIO->EDSAMPLE; // Read level
281 return (uint8_t)(val > 63
282 ? 255
283 : val * ED_RSSISCALE); // Convert to IEEE 802.15.4 scale
284}
285#endif
286
287/**********************************************************************************************************/
288
290{
291 uint8_t pipe_num = 0;
292 return available(&pipe_num);
293}
294
295/**********************************************************************************************************/
296
298{
299 if (rxQueue != nullptr || depth == 0 || depth == 255) {
300 return false;
301 }
302 // One slot stays empty to tell a full queue from an empty one
303 // Each slot holds the pipe number, payload length and payload
304 rxQueueSlots = depth + 1;
305 rxQueueSlotSize = staticPayloadSize + 2;
306 rxQueue = (uint8_t*)malloc(rxQueueSlots * rxQueueSlotSize);
307 if (rxQueue == nullptr) {
308 return false;
309 }
310 if (ackPayloadsEnabled) {
311 allocateAckQueue();
312 }
313
314 rxIrqRadio = this;
315 NVIC_SetPriority(RX_IRQ_NUMBER, 7);
316 NVIC_ClearPendingIRQ(RX_IRQ_NUMBER);
317 NVIC_EnableIRQ(RX_IRQ_NUMBER);
318 if (inRxMode) {
319 NRF_RADIO->RX_IRQ_SET = RX_IRQ_MASK;
320 }
321 return true;
322}
323
324/**********************************************************************************************************/
325
327{
328 if (rxQueue == nullptr) {
329 return;
330 }
331 rxIrqDisable();
332 NVIC_DisableIRQ(RX_IRQ_NUMBER);
333
334 free(rxQueue);
335 rxQueue = nullptr;
336 rxQueueHead = 0;
337 rxQueueTail = 0;
338
339 if (ackQueue != nullptr) {
340 // Polling mode re-sends one ACK payload, so keep the newest one
341 if (ackQueueCount) {
342 const uint8_t* slot = &ackQueue[((ackQueueHead + ackQueueCount - 1) % ackQueueDepth) * ackQueueSlotSize];
343 ackPipe = slot[0];
344 memcpy(ackBuffer, &slot[1], slot[1] + 1);
345 ackPayloadSent = false;
346 }
347 free(ackQueue);
348 ackQueue = nullptr;
349 ackQueueHead = 0;
350 ackQueueCount = 0;
351 }
352}
353
354/**********************************************************************************************************/
355
356void nrf_to_nrf::allocateAckQueue()
357{
358 // Call with the RX interrupt disabled or locked
359 if (rxQueue == nullptr || ackQueue != nullptr) {
360 return;
361 }
362 ackQueueDepth = rxQueueSlots - 1;
363 ackQueueSlotSize = rxQueueSlotSize;
364 ackQueueHead = 0;
365 ackQueueCount = 0;
366 ackQueue = (uint8_t*)malloc(ackQueueDepth * ackQueueSlotSize);
367}
368
369/**********************************************************************************************************/
370
371void nrf_to_nrf::loadAckPayload(uint8_t pipe)
372{
373 // Called for each new packet, not retransmissions: the payload sent with the last ACK is done
374 if (ackPayloadSent) {
375 ackBuffer[0] = 0;
376 ackPayloadSent = false;
377 }
378 if (ackQueue == nullptr) {
379 return; // Polling mode keeps an unsent payload for its pipe
380 }
381 // Move the oldest queued payload for this pipe into ackBuffer, or send a plain ACK
382 ackPipe = pipe;
383 ackBuffer[0] = 0;
384 for (uint8_t i = 0; i < ackQueueCount; i++) {
385 uint8_t* slot = &ackQueue[((ackQueueHead + i) % ackQueueDepth) * ackQueueSlotSize];
386 if (slot[0] != pipe) {
387 continue;
388 }
389 memcpy(ackBuffer, &slot[1], slot[1] + 1);
390 // Close the gap by moving older payloads for other pipes up one slot
391 for (uint8_t j = i; j > 0; j--) {
392 memcpy(&ackQueue[((ackQueueHead + j) % ackQueueDepth) * ackQueueSlotSize],
393 &ackQueue[((ackQueueHead + j - 1) % ackQueueDepth) * ackQueueSlotSize], ackQueueSlotSize);
394 }
395 ackQueueHead = (ackQueueHead + 1) % ackQueueDepth;
396 ackQueueCount--;
397 return;
398 }
399}
400
401/**********************************************************************************************************/
402
404{
405 const uint8_t next = (rxQueueHead + 1) % rxQueueSlots;
406 if (next == rxQueueTail) {
407 NRF_RADIO->EVENTS_CRCOK = 0;
408 NRF_RADIO->EVENTS_CRCERROR = 0;
409 NRF_RADIO->TASKS_START = 1;
410 (void)NRF_RADIO->EVENTS_CRCERROR;
411 return;
412 }
413
414 uint8_t pipe = 0;
415 inInterrupt = true;
416 if (pollRadio(&pipe)) {
417 uint8_t* slot = &rxQueue[rxQueueHead * rxQueueSlotSize];
418 const uint8_t length = min((uint16_t)rxBuffer[0], (uint16_t)(rxQueueSlotSize - 2));
419 slot[0] = pipe;
420 slot[1] = length;
421 memcpy(&slot[2], &rxBuffer[1], length);
422 payloadAvailable = false;
423 __DMB();
424 rxQueueHead = next;
425 }
426 inInterrupt = false;
427 (void)NRF_RADIO->EVENTS_CRCOK; // Writes are buffered, so make sure the event is clear before returning
428}
429
430/**********************************************************************************************************/
431
432uint8_t* nrf_to_nrf::rxQueueFront()
433{
434 // An ACK payload from write() is read through rxBuffer, as usual
435 if (rxQueue == nullptr || (!inRxMode && ackPayloadAvailable) || rxQueueTail == rxQueueHead) {
436 return nullptr;
437 }
438 return &rxQueue[rxQueueTail * rxQueueSlotSize];
439}
440
441/**********************************************************************************************************/
442
443bool nrf_to_nrf::available(uint8_t* pipe_num)
444{
445 if (rxQueue == nullptr) {
446 return pollRadio(pipe_num);
447 }
448 if (!inRxMode && ackPayloadAvailable) {
449 *pipe_num = ackAvailablePipeNo;
450 return true;
451 }
452 const uint8_t* slot = rxQueueFront();
453 if (slot == nullptr) {
454 return false;
455 }
456 *pipe_num = slot[0];
457 return true;
458}
459
460/**********************************************************************************************************/
461
462bool nrf_to_nrf::pollRadio(uint8_t* pipe_num)
463{
464
465 if (payloadAvailable) {
466 *pipe_num = (uint8_t)NRF_RADIO->RXMATCH;
467 return true;
468 }
469
470 if (!inRxMode) {
471 if (ackPayloadAvailable) {
472 *pipe_num = ackAvailablePipeNo;
473 return true;
474 }
475 }
476 if (NRF_RADIO->EVENTS_CRCOK) {
477 NRF_RADIO->EVENTS_CRCOK = 0;
478 if (DPL) {
479 if (radioData[0] > ACTUAL_MAX_PAYLOAD_SIZE - (2 + NRF_RADIO->CRCCNF) || radioData[0] == 0) {
480 return restartReturnRx();
481 }
482 }
483
484 *pipe_num = (uint8_t)NRF_RADIO->RXMATCH;
485 if (!DPL && acksEnabled(0) == false) {
486#if defined CCM_ENCRYPTION_ENABLED
487 if (enableEncryption) {
488 memcpy(&rxBuffer[1], &radioData[CCM_IV_SIZE + CCM_COUNTER_SIZE], staticPayloadSize - CCM_IV_SIZE - CCM_COUNTER_SIZE);
489 memcpy(ccmData.iv, &radioData[0], CCM_IV_SIZE);
490 memcpy(&ccmData.counter, &radioData[CCM_IV_SIZE], CCM_COUNTER_SIZE);
491 }
492 else {
493#endif
494 memcpy(&rxBuffer[1], &radioData[0], staticPayloadSize);
495#if defined CCM_ENCRYPTION_ENABLED
496 }
497#endif
498 }
499 else {
500#if defined CCM_ENCRYPTION_ENABLED
501 if (enableEncryption) {
502 if (DPL) {
503 memcpy(&rxBuffer[1], &radioData[2 + CCM_IV_SIZE + CCM_COUNTER_SIZE], max(0, (int16_t)radioData[0] - CCM_IV_SIZE - CCM_COUNTER_SIZE));
504 }
505 else {
506 memcpy(&rxBuffer[1], &radioData[CCM_IV_SIZE + CCM_COUNTER_SIZE], max(0, (int16_t)staticPayloadSize - CCM_IV_SIZE - CCM_COUNTER_SIZE));
507 }
508 memcpy(ccmData.iv, &radioData[2], CCM_IV_SIZE);
509 memcpy(&ccmData.counter, &radioData[2 + CCM_IV_SIZE], CCM_COUNTER_SIZE);
510 }
511 else {
512#endif
513 if (DPL) {
514 memcpy(&rxBuffer[1], &radioData[2], radioData[0]);
515 }
516 else {
517 memcpy(&rxBuffer[1], &radioData[2], staticPayloadSize);
518 }
519#if defined CCM_ENCRYPTION_ENABLED
520 }
521#endif
522 }
523
524 rxFifoAvailable = true;
525 uint8_t packetCtr = 0;
526 if (DPL) {
527 packetCtr = radioData[1];
528 rxBuffer[0] = radioData[0];
529 }
530 else {
531 packetCtr = radioData[0];
532 rxBuffer[0] = staticPayloadSize;
533 }
534
535 ackPID = packetCtr;
536 uint16_t packetData = NRF_RADIO->RXCRC;
537 // If ack is enabled on this receiving pipe
538 if (acksEnabled(NRF_RADIO->RXMATCH)) {
539 // If the packet has the same ID number and data, it is most likely a duplicate
540 const bool duplicate = NRF_RADIO->CRCCNF != 0 && packetCtr == lastPacketCounter && packetData == lastData;
541 // A retransmission means the last ACK was lost, so it gets the same ACK payload again
542 if (!duplicate) {
543 loadAckPayload(*pipe_num);
544 }
545 stopListening(false, false);
546 uint32_t txAddress = NRF_RADIO->TXADDRESS;
547 NRF_RADIO->TXADDRESS = NRF_RADIO->RXMATCH;
548 delayMicroseconds(75);
549 if (ackPayloadsEnabled) {
550 if (*pipe_num == ackPipe) {
551 write(&ackBuffer[1], ackBuffer[0], 1, 0);
552 ackPayloadSent = ackBuffer[0] > 0;
553 }
554 else {
555 write(0, 0, 1, 0);
556 }
557 }
558 else {
559 uint8_t payloadSize = 0;
560 if (!DPL) {
561 payloadSize = getPayloadSize();
562 setStaticLength(0);
563 }
564 write(0, 0, 1, 0); // Send an ACK
565 if (!DPL) {
566 setStaticLength(payloadSize);
567 }
568 }
569 NRF_RADIO->TXADDRESS = txAddress;
570 startListening(false);
571
572 if (duplicate) {
573 return restartReturnRx();
574 }
575 }
576
577#if defined CCM_ENCRYPTION_ENABLED
578 if (enableEncryption) {
579 uint8_t bufferLength = 0;
580 if (DPL) {
581 bufferLength = rxBuffer[0] - CCM_IV_SIZE - CCM_COUNTER_SIZE;
582 }
583 else {
584 bufferLength = staticPayloadSize - CCM_IV_SIZE - CCM_COUNTER_SIZE;
585 }
586 if (!decrypt(&rxBuffer[1], bufferLength)) {
587 if (!inInterrupt) {
588 Serial.println("DECRYPT FAIL");
589 }
590 return restartReturnRx();
591 }
592
593 memset(&rxBuffer[1], 0, sizeof(rxBuffer) - 1);
594
595 if (DPL) {
596 rxBuffer[0] -= (CCM_MIC_SIZE + CCM_IV_SIZE + CCM_COUNTER_SIZE);
597 memcpy(&rxBuffer[1], &outBuffer[CCM_START_SIZE], rxBuffer[0]);
598 }
599 else {
600 memcpy(&rxBuffer[1], &outBuffer[CCM_START_SIZE], staticPayloadSize - (CCM_MIC_SIZE + CCM_IV_SIZE + CCM_COUNTER_SIZE));
601 }
602 }
603#endif
604 lastPacketCounter = packetCtr;
605 lastData = packetData;
606
607 if (inRxMode && !acksEnabled(NRF_RADIO->RXMATCH)) {
608 NRF_RADIO->TASKS_START = 1;
609 }
610 if ((DPL && rxBuffer[0]) || !DPL) {
611 payloadAvailable = true;
612 return 1;
613 }
614 }
615 if (NRF_RADIO->EVENTS_CRCERROR) {
616 NRF_RADIO->EVENTS_CRCERROR = 0;
617 NRF_RADIO->TASKS_START = 1;
618 }
619 return 0;
620}
621
622/**********************************************************************************************************/
623
624bool nrf_to_nrf::restartReturnRx()
625{
626 if (inRxMode) {
627 NRF_RADIO->TASKS_START = 1;
628 }
629 return 0;
630}
631
632/**********************************************************************************************************/
633
634void nrf_to_nrf::read(void* buf, uint8_t len)
635{
636 const uint8_t* slot = rxQueueFront();
637 if (slot != nullptr) {
638 memcpy(buf, &slot[2], min((uint16_t)len, (uint16_t)(rxQueueSlotSize - 2)));
639 rxQueueTail = (rxQueueTail + 1) % rxQueueSlots;
640 return;
641 }
642 memcpy(buf, &rxBuffer[1], len);
643 ackPayloadAvailable = false;
644 payloadAvailable = false;
645}
646
647/**********************************************************************************************************/
648
649bool nrf_to_nrf::write(void* buf, uint8_t len, bool multicast, bool doEncryption)
650{
651
652 uint8_t PID = ackPID;
653 if (DPL) {
654 PID = ((ackPID += 1) % 7) << 1;
655 }
656 else {
657 PID = ackPID++;
658 }
659 uint8_t payloadSize = 0;
660
661#if defined CCM_ENCRYPTION_ENABLED
662
663 if (enableEncryption && doEncryption) {
664 if (len) {
665
666 if (!generateIV(ccmData.iv)) {
667 return 0;
668 }
669 ccmData.counter = packetCounter;
670
671 if (!encrypt(buf, len)) {
672 return 0;
673 }
674
676 packetCounter++;
677 if (packetCounter > 200000) {
678 packetCounter = 0;
679 }
680 }
681 }
682#endif
683
684 for (int i = 0; i < (retries + 1); i++) {
685 arcCounter = i;
686 if (DPL) {
687 radioData[0] = len;
688 radioData[1] = PID;
689 }
690 else {
691 radioData[1] = 0;
692 radioData[0] = PID;
693 }
694
695 uint8_t dataStart = 0;
696
697#if defined CCM_ENCRYPTION_ENABLED
698
699 if (enableEncryption && doEncryption) {
700 dataStart = (!DPL && acksEnabled(0) == false) ? CCM_IV_SIZE + CCM_COUNTER_SIZE : CCM_IV_SIZE + CCM_COUNTER_SIZE + 2;
701 }
702 else {
703#endif
704 dataStart = (!DPL && acksEnabled(0) == false) ? 0 : 2;
705#if defined CCM_ENCRYPTION_ENABLED
706 }
707#endif
708
709#if defined CCM_ENCRYPTION_ENABLED
710 if (enableEncryption && doEncryption) {
711 memcpy(&radioData[dataStart - CCM_COUNTER_SIZE], &ccmData.counter, CCM_COUNTER_SIZE);
712 memcpy(&radioData[dataStart - CCM_IV_SIZE - CCM_COUNTER_SIZE], ccmData.iv, CCM_IV_SIZE);
713 memcpy(&radioData[dataStart], &outBuffer[CCM_START_SIZE], len - (CCM_IV_SIZE + CCM_COUNTER_SIZE));
714 }
715 else {
716#endif
717 memcpy(&radioData[dataStart], buf, len);
718#if defined CCM_ENCRYPTION_ENABLED
719 }
720#endif
721#ifdef ARDUINO_NRF54L15
722 uint32_t timeout = millis();
723 while (NRF_RADIO->STATE != 10) {
724 if (!inInterrupt) {
725 yield();
726 }
727 if (millis() - timeout > 250) {
728 return 0;
729 }
730 }
731#endif
732 NRF_RADIO->EVENTS_END = 0;
733 NRF_RADIO->TASKS_START = 1;
734 if (!waitForEvent(&NRF_RADIO->EVENTS_END))
735 return false;
736
737 NRF_RADIO->EVENTS_END = 0;
738 if (!multicast && acksPerPipe[NRF_RADIO->TXADDRESS] == true) {
739 uint32_t rxAddress = NRF_RADIO->RXADDRESSES;
740 NRF_RADIO->RXADDRESSES = 1 << NRF_RADIO->TXADDRESS;
741 if (!DPL) {
742 payloadSize = getPayloadSize();
743 setStaticLength(0);
744 }
745 startListening(false);
746
747 int32_t realAckTimeout = (int32_t)ackTimeout;
748 if (!DPL) {
749 if (NRF_RADIO->MODE == (RADIO_MODE_MODE_Nrf_1Mbit << RADIO_MODE_MODE_Pos)) {
750 realAckTimeout -= ACK_TIMEOUT_1MBPS_OFFSET;
751 }
752 else if (NRF_RADIO->MODE == (RADIO_MODE_MODE_Nrf_250Kbit << RADIO_MODE_MODE_Pos)) {
753 realAckTimeout -= ACK_TIMEOUT_250KBPS_OFFSET;
754 }
755 else {
756 realAckTimeout -= ACK_TIMEOUT_2MBPS_OFFSET;
757 }
758 }
759 else {
760 if (ackPayloadsEnabled && staticPayloadSize <= DEFAULT_MAX_PAYLOAD_SIZE) {
761 realAckTimeout += 200;
762 }
763 else {
764 realAckTimeout += ACK_PAYLOAD_TIMEOUT_OFFSET;
765 }
766 }
767 if (realAckTimeout < 0) {
768 realAckTimeout = 0;
769 }
770
771 uint32_t ack_timeout = micros();
772 while (!NRF_RADIO->EVENTS_CRCOK && !NRF_RADIO->EVENTS_CRCERROR) {
773 if (micros() - ack_timeout > realAckTimeout) {
774 break;
775 }
776 }
777 if (NRF_RADIO->EVENTS_CRCOK) {
778 if (ackPayloadsEnabled && radioData[0] > 0) {
779#if defined CCM_ENCRYPTION_ENABLED
780 if (enableEncryption && doEncryption) {
781 memcpy(&rxBuffer[1], &radioData[2 + CCM_COUNTER_SIZE + CCM_IV_SIZE], max(0, radioData[0] - CCM_COUNTER_SIZE - CCM_IV_SIZE));
782 }
783 else {
784 memcpy(&rxBuffer[1], &radioData[2], radioData[0]);
785 }
786#else
787 memcpy(&rxBuffer[1], &radioData[2], radioData[0]);
788#endif
789
790#if defined CCM_ENCRYPTION_ENABLED
791 if (enableEncryption && radioData[0] > 0) {
792 memcpy(ccmData.iv, &radioData[2], CCM_IV_SIZE);
793 memcpy(&ccmData.counter, &radioData[2 + CCM_IV_SIZE], CCM_COUNTER_SIZE);
794
795 if (!decrypt(&rxBuffer[1], radioData[0])) {
796 Serial.println("DECRYPT FAIL");
797 return 0;
798 }
801 }
802 memcpy(&rxBuffer[1], &outBuffer[CCM_START_SIZE], radioData[0]);
803 }
804#endif
805 rxBuffer[0] = radioData[0];
806 ackPayloadAvailable = true;
807 ackAvailablePipeNo = NRF_RADIO->RXMATCH;
808 }
809 NRF_RADIO->EVENTS_CRCOK = 0;
810 stopListening(false, false);
811 if (!DPL) {
812 setStaticLength(payloadSize);
813 }
814 NRF_RADIO->RXADDRESSES = rxAddress;
815 lastTxResult = true;
816 return 1;
817 }
818 else if (NRF_RADIO->EVENTS_CRCERROR) {
819 NRF_RADIO->EVENTS_CRCERROR = 0;
820 }
821 uint32_t duration = 258 * retryDuration;
822 delayMicroseconds(duration);
823 stopListening(false, false);
824 if (!DPL) {
825 setStaticLength(payloadSize);
826 }
827 NRF_RADIO->RXADDRESSES = rxAddress;
828 }
829 else {
830 lastTxResult = true;
831 return 1;
832 }
833 }
834 lastTxResult = false;
835 return 0;
836}
837
838/**********************************************************************************************************/
839
840bool nrf_to_nrf::startWrite(void* buf, uint8_t len, bool multicast, bool doEncryption)
841{
842
843 uint8_t PID = ackPID;
844 if (DPL) {
845 PID = ((ackPID += 1) % 7) << 1;
846 }
847 else {
848 PID = ackPID++;
849 }
850
851#if defined CCM_ENCRYPTION_ENABLED
852 uint8_t tmpIV[CCM_IV_SIZE];
853 uint32_t tmpCounter = 0;
854
855 if (enableEncryption && doEncryption) {
856 if (len) {
857
858 if (!generateIV(tmpIV)) {
859 return 0;
860 }
861 memcpy(ccmData.iv, tmpIV, CCM_IV_SIZE);
862 tmpCounter = packetCounter;
863 ccmData.counter = tmpCounter;
864
865 if (!encrypt(buf, len)) {
866 return 0;
867 }
868
870 packetCounter++;
871 if (packetCounter > 200000) {
872 packetCounter = 0;
873 }
874 }
875 }
876#endif
877
878 // for (int i = 0; i < retries; i++) {
879 arcCounter = 0;
880 if (DPL) {
881 radioData[0] = len;
882 radioData[1] = PID;
883 }
884 else {
885 radioData[1] = 0;
886 radioData[0] = PID;
887 }
888
889 uint8_t dataStart = 0;
890
891#if defined CCM_ENCRYPTION_ENABLED
892
893 if (enableEncryption && doEncryption) {
894 dataStart = (!DPL && acksEnabled(0) == false) ? CCM_IV_SIZE + CCM_COUNTER_SIZE : CCM_IV_SIZE + CCM_COUNTER_SIZE + 2;
895 }
896 else {
897#endif
898 dataStart = (!DPL && acksEnabled(0) == false) ? 0 : 2;
899#if defined CCM_ENCRYPTION_ENABLED
900 }
901#endif
902
903#if defined CCM_ENCRYPTION_ENABLED
904 if (enableEncryption && doEncryption) {
905 memcpy(&radioData[dataStart - CCM_COUNTER_SIZE], &tmpCounter, CCM_COUNTER_SIZE);
906 memcpy(&radioData[dataStart - CCM_IV_SIZE - CCM_COUNTER_SIZE], &tmpIV[0], CCM_IV_SIZE);
907 memcpy(&radioData[dataStart], &outBuffer[CCM_START_SIZE], len - (CCM_IV_SIZE + CCM_COUNTER_SIZE));
908 }
909 else {
910#endif
911 memcpy(&radioData[dataStart], buf, len);
912#if defined CCM_ENCRYPTION_ENABLED
913 }
914#endif
915
916 NRF_RADIO->EVENTS_END = 0;
917 NRF_RADIO->TASKS_START = 1;
918 lastTxResult = true;
919
920 return true;
921}
922
923/**********************************************************************************************************/
924
925bool nrf_to_nrf::writeAckPayload(uint8_t pipe, void* buf, uint8_t len)
926{
927 RadioIrqLock lock(rxQueue != nullptr);
928
929 uint16_t sendLength = len;
930#if defined CCM_ENCRYPTION_ENABLED
931 if (enableEncryption && len) {
932 sendLength += CCM_IV_SIZE + CCM_COUNTER_SIZE + CCM_MIC_SIZE;
933 }
934#endif
935
936 // With the RX interrupt, ACK payloads are queued and each one is sent once
937 uint8_t* dest = ackBuffer; // Length byte followed by the payload
938 if (rxQueue != nullptr) {
939 if (ackQueue == nullptr) {
940 return false; // ACK payloads aren't enabled, or there wasn't enough memory
941 }
942 if (ackQueueCount >= ackQueueDepth || sendLength > ackQueueSlotSize - 2) {
943 return false;
944 }
945 uint8_t* slot = &ackQueue[((ackQueueHead + ackQueueCount) % ackQueueDepth) * ackQueueSlotSize];
946 slot[0] = pipe;
947 dest = &slot[1];
948 }
949 else if (sendLength > ACTUAL_MAX_PAYLOAD_SIZE) {
950 return false;
951 }
952
953#if defined CCM_ENCRYPTION_ENABLED
954 if (enableEncryption) {
955 if (len) {
956
957 if (!generateIV(ccmData.iv)) {
958 return 0;
959 }
960 memcpy(&dest[1], ccmData.iv, CCM_IV_SIZE);
961
962 ccmData.counter = packetCounter;
963 memcpy(&dest[1 + CCM_IV_SIZE], &ccmData.counter, CCM_COUNTER_SIZE);
964
965 if (!encrypt(buf, len)) {
966 return 0;
967 }
968
971 packetCounter++;
972 if (packetCounter > 200000) {
973 packetCounter = 0;
974 }
975 }
976 }
977 else {
978#endif
979 memcpy(&dest[1], buf, len);
980#if defined CCM_ENCRYPTION_ENABLED
981 }
982#endif
983 dest[0] = len;
984 if (dest == ackBuffer) {
985 ackPipe = pipe;
986 ackPayloadSent = false;
987 }
988 else {
989 ackQueueCount++;
990 }
991 return true;
992}
993
994/**********************************************************************************************************/
995
997{
998 RadioIrqLock lock(rxQueue != nullptr);
999 ackPayloadsEnabled = true;
1000 allocateAckQueue();
1001}
1002
1003/**********************************************************************************************************/
1004
1005void nrf_to_nrf::enableDynamicAck() { dynamicAckEnabled = true; }
1006
1007/**********************************************************************************************************/
1008
1010{
1011 RadioIrqLock lock(rxQueue != nullptr);
1012 ackPayloadsEnabled = false;
1013 free(ackQueue);
1014 ackQueue = nullptr;
1015 ackQueueHead = 0;
1016 ackQueueCount = 0;
1017}
1018
1019/**********************************************************************************************************/
1020
1021void nrf_to_nrf::startListening(bool resetAddresses)
1022{
1023 // Only the library's own calls, which manage the interrupt themselves, pass false
1024 if (resetAddresses && rxQueue != nullptr) {
1025 rxIrqDisable();
1026 }
1027
1028 // The shortcuts from stopListening() would start the transmitter as soon as the radio is disabled
1029 NRF_RADIO->SHORTS = 0x0;
1030 NRF_RADIO->EVENTS_DISABLED = 0;
1031 NRF_RADIO->TASKS_DISABLE = 1;
1032 if (!waitForEvent(&NRF_RADIO->EVENTS_DISABLED))
1033 return;
1034 NRF_RADIO->EVENTS_DISABLED = 0;
1035
1036 if (resetAddresses == true) {
1037 NRF_RADIO->BASE0 = rxBase;
1038 NRF_RADIO->PREFIX0 = rxPrefix;
1039#ifndef ARDUINO_NRF54L15
1040 NRF_RADIO->MODECNF0 = 0x201;
1041#else
1042 NRF_RADIO->TIMING = 0x1;
1043#endif
1044 // NRF_RADIO->MODECNF0 = 0x201;
1045 NRF_RADIO->TIFS = 0;
1046 }
1047#if defined(NRF52832_XXAA)
1048 NRF_RADIO->EVENTS_READY = 0;
1049#else
1050 NRF_RADIO->EVENTS_RXREADY = 0;
1051#endif
1052 NRF_RADIO->EVENTS_CRCOK = 0;
1053 NRF_RADIO->TASKS_RXEN = 1;
1054#if defined(NRF52832_XXAA)
1055 if (!waitForEvent(&NRF_RADIO->EVENTS_READY))
1056 return;
1057#else
1058 if (!waitForEvent(&NRF_RADIO->EVENTS_RXREADY))
1059 return;
1060#endif
1061 NRF_RADIO->TASKS_START = 1;
1062 inRxMode = true;
1063 if (resetAddresses && rxQueue != nullptr) {
1064 NRF_RADIO->RX_IRQ_SET = RX_IRQ_MASK;
1065 }
1066}
1067
1068/**********************************************************************************************************/
1069
1070void nrf_to_nrf::stopListening(bool setWritingPipe, bool resetAddresses)
1071{
1072 if (resetAddresses && rxQueue != nullptr) {
1073 rxIrqDisable();
1074 }
1075
1076 NRF_RADIO->EVENTS_DISABLED = 0;
1077 NRF_RADIO->TASKS_DISABLE = 1;
1078 if (!waitForEvent(&NRF_RADIO->EVENTS_DISABLED))
1079 return;
1080 NRF_RADIO->EVENTS_DISABLED = 0;
1081
1082 if (resetAddresses) {
1083 NRF_RADIO->BASE0 = txBase;
1084 NRF_RADIO->PREFIX0 = txPrefix;
1085 }
1086 if (setWritingPipe) {
1087 NRF_RADIO->TXADDRESS = 0x00;
1088 NRF_RADIO->TIFS = interframeSpacing;
1089#ifndef ARDUINO_NRF54L15
1090 NRF_RADIO->MODECNF0 = 0x200;
1091#else
1092 NRF_RADIO->TIMING = 0x0;
1093#endif
1094 }
1095#ifndef ARDUINO_NRF54L15
1096 NRF_RADIO->SHORTS = 0x6;
1097#else
1098 NRF_RADIO->SHORTS = 0x80004;
1099#endif
1100 if (NRF_RADIO->STATE < 9) {
1101#if defined(NRF52832_XXAA)
1102 NRF_RADIO->EVENTS_READY = 0;
1103#else
1104 NRF_RADIO->EVENTS_TXREADY = 0;
1105#endif
1106 NRF_RADIO->TASKS_TXEN = 1;
1107#if defined(NRF52832_XXAA)
1108 if (!waitForEvent(&NRF_RADIO->EVENTS_READY))
1109 return;
1110 NRF_RADIO->EVENTS_READY = 0;
1111#else
1112 if (!waitForEvent(&NRF_RADIO->EVENTS_TXREADY))
1113 return;
1114 NRF_RADIO->EVENTS_TXREADY = 0;
1115#endif
1116 }
1117
1118 inRxMode = false;
1119}
1120
1121/**********************************************************************************************************/
1122
1123void nrf_to_nrf::stopListening(const uint8_t* txAddress, bool setWritingPipe, bool resetAddresses)
1124{
1125 stopListening(setWritingPipe, resetAddresses);
1126 openWritingPipe(txAddress);
1127}
1128
1129/**********************************************************************************************************/
1130
1132{
1133 const uint8_t* slot = rxQueueFront();
1134 if (slot != nullptr) {
1135 return slot[1];
1136 }
1137 uint8_t size = min(staticPayloadSize, rxBuffer[0]);
1138 return size;
1139}
1140
1141/**********************************************************************************************************/
1142
1144{
1145
1146 uint32_t freq = NRF_RADIO->FREQUENCY;
1147 NRF_RADIO->FREQUENCY = 0x4C;
1148 if (NRF_RADIO->FREQUENCY == 0x4C) {
1149 NRF_RADIO->FREQUENCY = freq;
1150 return 1;
1151 }
1152 return 0;
1153}
1154
1155/**********************************************************************************************************/
1156
1157void nrf_to_nrf::setChannel(uint8_t channel, bool map) { NRF_RADIO->FREQUENCY = channel | map << RADIO_FREQUENCY_MAP_Pos; }
1158
1159/**********************************************************************************************************/
1160
1161uint8_t nrf_to_nrf::getChannel() { return (uint8_t)NRF_RADIO->FREQUENCY; }
1162
1163/**********************************************************************************************************/
1164
1165void nrf_to_nrf::setAutoAck(bool enable)
1166{
1167
1168 for (int i = 0; i < 8; i++) {
1169 acksPerPipe[i] = enable;
1170 }
1171 if (!DPL) {
1172 disableDynamicPayloads(); // Called to re-configure the PCNF0 register
1173 }
1174}
1175
1176/**********************************************************************************************************/
1177
1178void nrf_to_nrf::setAutoAck(uint8_t pipe, bool enable)
1179{
1180
1181 acksPerPipe[pipe] = enable;
1182 if (!DPL) {
1183 disableDynamicPayloads(); // Called to re-configure the PCNF0 register
1184 }
1185}
1186
1187/**********************************************************************************************************/
1188
1189void nrf_to_nrf::enableDynamicPayloads(uint8_t payloadSize)
1190{
1191
1192 if (!DPL) {
1193 DPL = true;
1194 staticPayloadSize = payloadSize;
1195
1196 if (payloadSize <= 63) {
1197 NRF_RADIO->PCNF0 = (0 << RADIO_PCNF0_S0LEN_Pos) | (6 << RADIO_PCNF0_LFLEN_Pos) | (3 << RADIO_PCNF0_S1LEN_Pos);
1198 }
1199 else {
1200 // Using 8 bits for length
1201 NRF_RADIO->PCNF0 = (0 << RADIO_PCNF0_S0LEN_Pos) | (8 << RADIO_PCNF0_LFLEN_Pos) | (3 << RADIO_PCNF0_S1LEN_Pos);
1202 }
1203
1204 NRF_RADIO->PCNF1 &= ~(0xFF << RADIO_PCNF1_MAXLEN_Pos | 0xFF << RADIO_PCNF1_STATLEN_Pos);
1205 NRF_RADIO->PCNF1 |= payloadSize << RADIO_PCNF1_MAXLEN_Pos;
1206 resizeRxQueue();
1207 }
1208}
1209
1210/**********************************************************************************************************/
1211
1213{
1214 DPL = false;
1215
1216 uint8_t lenConfig = 0;
1217 if (acksEnabled(0)) {
1218 lenConfig = 1;
1219 }
1220 NRF_RADIO->PCNF0 = (lenConfig << RADIO_PCNF0_S0LEN_Pos) | (0 << RADIO_PCNF0_LFLEN_Pos) | (lenConfig << RADIO_PCNF0_S1LEN_Pos);
1221
1222 NRF_RADIO->PCNF1 &= ~(0xFF << RADIO_PCNF1_MAXLEN_Pos | 0xFF << RADIO_PCNF1_STATLEN_Pos);
1223 NRF_RADIO->PCNF1 |= staticPayloadSize << RADIO_PCNF1_STATLEN_Pos | staticPayloadSize << RADIO_PCNF1_MAXLEN_Pos;
1224}
1225
1226/**********************************************************************************************************/
1227
1229{
1230 setStaticLength(size);
1231 resizeRxQueue();
1232}
1233
1234/**********************************************************************************************************/
1235
1236void nrf_to_nrf::setStaticLength(uint8_t size)
1237{
1238 staticPayloadSize = size;
1239 DPL = false;
1240
1241 uint8_t lenConfig = 0;
1242 if (acksEnabled(0)) {
1243 lenConfig = 1;
1244 }
1245 NRF_RADIO->PCNF0 = (lenConfig << RADIO_PCNF0_S0LEN_Pos) | (0 << RADIO_PCNF0_LFLEN_Pos) | (lenConfig << RADIO_PCNF0_S1LEN_Pos);
1246
1247 NRF_RADIO->PCNF1 &= ~(0xFF << RADIO_PCNF1_MAXLEN_Pos | 0xFF << RADIO_PCNF1_STATLEN_Pos);
1248 NRF_RADIO->PCNF1 |= staticPayloadSize << RADIO_PCNF1_STATLEN_Pos | staticPayloadSize << RADIO_PCNF1_MAXLEN_Pos;
1249}
1250
1251/**********************************************************************************************************/
1252
1253void nrf_to_nrf::resizeRxQueue()
1254{
1255 if (rxQueue != nullptr && rxQueueSlotSize != staticPayloadSize + 2) {
1256 uint8_t depth = rxQueueSlots - 1;
1258 enableRxInterrupt(depth);
1259 }
1260}
1261
1262/**********************************************************************************************************/
1263
1265{
1266 return staticPayloadSize;
1267}
1268
1269/**********************************************************************************************************/
1270
1271void nrf_to_nrf::setRetries(uint8_t retryVar, uint8_t attempts)
1272{
1273
1274 retries = attempts;
1275 retryDuration = retryVar;
1276}
1277
1278/**********************************************************************************************************/
1279
1280void nrf_to_nrf::openReadingPipe(uint8_t child, uint64_t address)
1281{
1282 uint32_t base = addrConv32(address >> 8);
1283 uint32_t prefix = addrConv32(address & 0xFF) >> 24;
1284
1285 openReadingPipe(child, base, prefix);
1286}
1287
1288/**********************************************************************************************************/
1289
1290void nrf_to_nrf::openWritingPipe(uint64_t address)
1291{
1292 uint32_t base = addrConv32(address >> 8);
1293 uint32_t prefix = addrConv32(address & 0xFF) >> 24;
1294
1295 openWritingPipe(base, prefix);
1296}
1297
1298/**********************************************************************************************************/
1299
1300void nrf_to_nrf::openReadingPipe(uint8_t child, const uint8_t* address)
1301{
1302 uint32_t base = addr_conv(&address[1]);
1303 uint32_t prefix = addr_conv(&address[0]) >> 24;
1304
1305 openReadingPipe(child, base, prefix);
1306}
1307
1308/**********************************************************************************************************/
1309
1310void nrf_to_nrf::openReadingPipe(uint8_t child, uint32_t base, uint32_t prefix)
1311{
1312
1313 // Using pipes 1-7 for reading pipes, leaving pipe0 for a tx pipe
1314 if (!child) {
1315 NRF_RADIO->PREFIX0 = rxPrefix;
1316 NRF_RADIO->BASE0 = base;
1317 NRF_RADIO->PREFIX0 &= ~(0xFF);
1318 NRF_RADIO->PREFIX0 |= prefix;
1319 rxBase = NRF_RADIO->BASE0;
1320 rxPrefix = NRF_RADIO->PREFIX0;
1321 }
1322 else if (child < 4) { // prefixes AP1-3 are in prefix0
1323 NRF_RADIO->PREFIX0 = rxPrefix;
1324 NRF_RADIO->BASE1 = base;
1325 NRF_RADIO->PREFIX0 &= ~(0xFF << (8 * child));
1326 NRF_RADIO->PREFIX0 |= prefix << (8 * child);
1327 rxPrefix = NRF_RADIO->PREFIX0;
1328 }
1329 else {
1330 NRF_RADIO->BASE1 = base;
1331 NRF_RADIO->PREFIX1 &= ~(0xFF << (8 * (child - 4)));
1332 NRF_RADIO->PREFIX1 |= prefix << (8 * (child - 4));
1333 }
1334 NRF_RADIO->RXADDRESSES |= 1 << child;
1335}
1336
1337/**********************************************************************************************************/
1338
1339void nrf_to_nrf::openWritingPipe(const uint8_t* address)
1340{
1341
1342 uint32_t base = addr_conv(&address[1]);
1343 uint32_t prefix = addr_conv(&address[0]) >> 24;
1344
1345 openWritingPipe(base, prefix);
1346}
1347
1348/**********************************************************************************************************/
1349
1350void nrf_to_nrf::openWritingPipe(uint32_t base, uint32_t prefix)
1351{
1352
1353 NRF_RADIO->BASE0 = base;
1354 NRF_RADIO->PREFIX0 &= ~(0xFF);
1355 NRF_RADIO->PREFIX0 |= prefix;
1356 NRF_RADIO->TXADDRESS = 0x00;
1357 txBase = NRF_RADIO->BASE0;
1358 txPrefix = NRF_RADIO->PREFIX0;
1359}
1360/**********************************************************************************************************/
1361
1363{
1364 return lastTxResult;
1365}
1366
1367/**********************************************************************************************************/
1368
1369bool nrf_to_nrf::txStandBy(uint32_t timeout, bool startTx)
1370{
1371 return lastTxResult;
1372}
1373
1374/**********************************************************************************************************/
1375
1376bool nrf_to_nrf::writeFast(void* buf, uint8_t len, bool multicast)
1377{
1378 lastTxResult = write(buf, len, multicast);
1379 return lastTxResult;
1380}
1381
1382/**********************************************************************************************************/
1383
1384bool nrf_to_nrf::acksEnabled(uint8_t pipe)
1385{
1386
1387 if (acksPerPipe[pipe]) {
1388 return 1;
1389 }
1390 return 0;
1391}
1392
1393/**********************************************************************************************************/
1394
1396
1397/**********************************************************************************************************/
1398
1399bool nrf_to_nrf::setDataRate(uint8_t speed)
1400{
1401
1402 if (speed == NRF_1MBPS) {
1403 NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_1Mbit << RADIO_MODE_MODE_Pos);
1404 ackTimeout = ACK_TIMEOUT_1MBPS;
1405 }
1406 else if (speed == NRF_250KBPS) {
1407 NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_250Kbit << RADIO_MODE_MODE_Pos);
1408 ackTimeout = ACK_TIMEOUT_250KBPS;
1409 }
1410 else if (speed == NRF_2MBPS) { // NRF_2MBPS
1411 NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_2Mbit << RADIO_MODE_MODE_Pos);
1412 ackTimeout = ACK_TIMEOUT_2MBPS;
1413 }
1414#ifdef ARDUINO_NRF54L15
1415 else if (speed == NRF_4MBPS_OBT4) {
1416 NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_4Mbit_OBT4 << RADIO_MODE_MODE_Pos);
1417 ackTimeout = ACK_TIMEOUT_2MBPS;
1418 }
1419 else {
1420 NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_4Mbit_OBT6 << RADIO_MODE_MODE_Pos);
1421 ackTimeout = ACK_TIMEOUT_2MBPS;
1422 }
1423#endif
1424
1425 return 1;
1426}
1427
1428/**********************************************************************************************************/
1429
1430void nrf_to_nrf::setPALevel(uint8_t level, bool lnaEnable)
1431{
1432
1433 uint8_t paLevel = 0x00;
1434
1435 if (level == NRF_PA_MIN) {
1436 paLevel = TXPOWER_PA_MIN;
1437 }
1438 else if (level == NRF_PA_LOW) {
1439 paLevel = TXPOWER_PA_LOW;
1440 }
1441 else if (level == NRF_PA_HIGH) {
1442 paLevel = TXPOWER_PA_HIGH;
1443 }
1444 else if (level == NRF_PA_MAX) {
1445 paLevel = TXPOWER_PA_MAX;
1446 }
1447 NRF_RADIO->TXPOWER = paLevel;
1448}
1449
1450/**********************************************************************************************************/
1451
1453{
1454
1455 uint8_t paLevel = NRF_RADIO->TXPOWER;
1456
1457 if (paLevel == TXPOWER_PA_MIN) {
1458 return NRF_PA_MIN;
1459 }
1460 else if (paLevel == TXPOWER_PA_LOW) {
1461 return NRF_PA_LOW;
1462 }
1463 else if (paLevel == TXPOWER_PA_HIGH) {
1464 return NRF_PA_HIGH;
1465 }
1466 else if (paLevel == TXPOWER_PA_MAX) {
1467 return NRF_PA_MAX;
1468 }
1469 else {
1470 return NRF_PA_ERROR;
1471 }
1472}
1473
1474/**********************************************************************************************************/
1475
1477{
1478 return arcCounter;
1479}
1480
1481/**********************************************************************************************************/
1482
1484{
1485 if (length == NRF_CRC_24) {
1486 NRF_RADIO->CRCCNF = RADIO_CRCCNF_LEN_Three; /* CRC configuration: 24bit */
1487 NRF_RADIO->CRCINIT = 0x555555UL; // Initial value
1488 NRF_RADIO->CRCPOLY = 0x65BUL;
1489 }
1490 else if (length == NRF_CRC_16) {
1491 NRF_RADIO->CRCCNF = RADIO_CRCCNF_LEN_Two; /* CRC configuration: 16bit */
1492 NRF_RADIO->CRCINIT = 0xFFFFUL; // Initial value
1493 NRF_RADIO->CRCPOLY = 0x11021UL; // CRC poly: x^16+x^12^x^5+1
1494 }
1495 else if (length == NRF_CRC_8) {
1496 NRF_RADIO->CRCCNF = RADIO_CRCCNF_LEN_One; /* CRC configuration: 8bit */
1497 NRF_RADIO->CRCINIT = 0xFFUL;
1498 NRF_RADIO->CRCPOLY = 0x107UL;
1499 }
1500 else {
1501 NRF_RADIO->CRCCNF = 0; /* CRC configuration: Disabled */
1502 NRF_RADIO->CRCINIT = 0x00L;
1503 NRF_RADIO->CRCPOLY = 0x00UL;
1504 }
1505}
1506
1507/**********************************************************************************************************/
1508
1510
1511/**********************************************************************************************************/
1512
1514{
1515 if (NRF_RADIO->CRCCNF == 0) {
1516 return NRF_CRC_DISABLED;
1517 }
1518 else if (NRF_RADIO->CRCCNF == RADIO_CRCCNF_LEN_One) {
1519 return NRF_CRC_8;
1520 }
1521 if (NRF_RADIO->CRCCNF == RADIO_CRCCNF_LEN_Two) {
1522 return NRF_CRC_16;
1523 }
1524 else {
1525 return NRF_CRC_24;
1526 }
1527}
1528
1529/**********************************************************************************************************/
1530
1531bool nrf_to_nrf::testCarrier(uint8_t RSSI)
1532{
1533#ifndef ARDUINO_NRF54L15
1534 RadioIrqLock lock(rxQueue != nullptr);
1535 NRF_RADIO->EVENTS_RSSIEND = 0;
1536 NRF_RADIO->TASKS_RSSISTART = 1;
1537 if (!waitForEvent(&NRF_RADIO->EVENTS_RSSIEND))
1538 return false;
1539
1540 if (NRF_RADIO->RSSISAMPLE < RSSI) {
1541 return 1;
1542 }
1543#endif
1544 return 0;
1545}
1546
1547/**********************************************************************************************************/
1548
1549bool nrf_to_nrf::testRPD(uint8_t RSSI)
1550{
1551 return testCarrier(RSSI);
1552}
1553
1554/**********************************************************************************************************/
1555
1557{
1558#ifndef ARDUINO_NRF54L15
1559 RadioIrqLock lock(rxQueue != nullptr);
1560 NRF_RADIO->EVENTS_RSSIEND = 0;
1561 NRF_RADIO->TASKS_RSSISTART = 1;
1562 if (!waitForEvent(&NRF_RADIO->EVENTS_RSSIEND))
1563 return 0;
1564
1565 return (uint8_t)NRF_RADIO->RSSISAMPLE;
1566#endif
1567 return 0;
1568}
1569
1570/**********************************************************************************************************/
1571
1573{
1574 rxQueueTail = rxQueueHead;
1575 payloadAvailable = false;
1576 ackPayloadAvailable = false;
1577 return 0;
1578}
1579
1580/**********************************************************************************************************/
1581
1583{
1584 RadioIrqLock lock(rxQueue != nullptr);
1585 ackQueueHead = 0;
1586 ackQueueCount = 0;
1587 ackBuffer[0] = 0;
1588 ackPayloadSent = false;
1589 return 0;
1590}
1591
1592/**********************************************************************************************************/
1593
1595{
1596#ifndef ARDUINO_NRF54L15
1597 NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
1598 NRF_CLOCK->TASKS_HFCLKSTART = 1;
1599
1600 /* Wait for the external oscillator to start up */
1601 waitForEvent(&NRF_CLOCK->EVENTS_HFCLKSTARTED);
1602
1603 NRF_RADIO->POWER = 1;
1604#else
1605 NRF_POWER->TASKS_CONSTLAT = 1;
1606 NRF_CLOCK->EVENTS_XOSTARTED = 0;
1607 NRF_CLOCK->TASKS_XOSTART = 1;
1608
1609 waitForEvent(&NRF_CLOCK->EVENTS_XOSTARTED);
1610#endif
1611
1612#ifdef CCM_ENCRYPTION_ENABLED
1613 if (enableEncryption) {
1614 #if defined NRF54_CCM_ENCRYPTION
1615 nrf54Rng.begin(); // The Ccm class enables CCM00 for each operation
1616 #else
1617 NRF_RNG->CONFIG = 1;
1618 NRF_RNG->TASKS_START = 1;
1619 NRF_CCM->ENABLE = 2;
1620 #endif
1621 }
1622#endif
1623}
1624
1625/**********************************************************************************************************/
1626
1628{
1629#ifndef ARDUINO_NRF54L15
1630 NRF_RADIO->POWER = 0;
1631 NRF_CLOCK->TASKS_HFCLKSTOP = 1;
1632#else
1633 NRF_CLOCK->TASKS_XOSTOP = 1;
1634#endif
1635
1636#ifdef CCM_ENCRYPTION_ENABLED
1637 if (enableEncryption) {
1638 #if defined NRF54_CCM_ENCRYPTION
1639 nrf54Rng.end();
1640 #else
1641 NRF_RNG->TASKS_STOP = 1;
1642 NRF_RNG->CONFIG = 0;
1643 NRF_CCM->ENABLE = 0;
1644 #endif
1645 }
1646#endif
1647}
1648
1649/**********************************************************************************************************/
1650void nrf_to_nrf::setAddressWidth(uint8_t a_width)
1651{
1652 NRF_RADIO->PCNF1 &= ~(0xFF << RADIO_PCNF1_BALEN_Pos);
1653 NRF_RADIO->PCNF1 |= (a_width - 1) << RADIO_PCNF1_BALEN_Pos;
1654}
1655
1656/**********************************************************************************************************/
1657
1659{
1660 uint8_t addressWidth = ((NRF_RADIO->PCNF1 >> 16) & 0xFF) + 1;
1661
1662 Serial.println("================ Radio Configuration ================");
1663 Serial.print("STATUS\t\t= ");
1664 Serial.println(NRF_RADIO->STATE);
1665
1666 // Serial.println(addrConv32(NRF_RADIO->PREFIX0);
1667 Serial.print("RX_ADDR_P0-1\t= 0x");
1668 uint32_t base = addrConv32(NRF_RADIO->BASE0);
1669 for (int i = addressWidth - 2; i > -1; i--) {
1670 Serial.print((base >> (i * 8)) & 0xFF, HEX);
1671 }
1672 uint32_t prefixes = addrConv32(NRF_RADIO->PREFIX0);
1673 uint8_t prefix = (prefixes >> 24) & 0xFF;
1674 Serial.print(prefix, HEX);
1675 Serial.print(" 0x");
1676 base = addrConv32(NRF_RADIO->BASE1);
1677 for (int i = addressWidth - 2; i > -1; i--) {
1678 Serial.print((base >> (i * 8)) & 0xFF, HEX);
1679 }
1680 prefix = (prefixes >> 16) & 0xFF;
1681 Serial.println(prefix, HEX);
1682
1683 Serial.print("RX_ADDR_P2-7\t= 0x");
1684 prefix = (prefixes >> 8) & 0xFF;
1685 Serial.print(prefix, HEX);
1686 Serial.print(" 0x");
1687 prefix = (prefixes) & 0xFF;
1688 Serial.print(prefix, HEX);
1689 Serial.print(" 0x");
1690 prefixes = addrConv32(NRF_RADIO->PREFIX1);
1691 prefix = (prefixes >> 24) & 0xFF;
1692 Serial.print(prefix, HEX);
1693 Serial.print(" 0x");
1694 prefix = (prefixes >> 16) & 0xFF;
1695 Serial.print(prefix, HEX);
1696 Serial.print(" 0x");
1697 prefix = (prefixes >> 8) & 0xFF;
1698 Serial.print(prefix, HEX);
1699 Serial.print(" 0x");
1700 prefix = (prefixes) & 0xFF;
1701 Serial.println(prefix, HEX);
1702
1703 uint8_t enAA = 0;
1704 for (int i = 0; i < 6; i++) {
1705 enAA |= acksPerPipe[i] << i;
1706 }
1707 Serial.print("EN_AA\t\t= 0x");
1708 Serial.println(enAA, HEX);
1709 Serial.print("EN_RXADDR\t= 0x");
1710 Serial.println(NRF_RADIO->RXADDRESSES, HEX);
1711 Serial.print("RF_CH\t\t= 0x");
1712 Serial.println(NRF_RADIO->FREQUENCY, HEX);
1713 Serial.println("DYNPD/FEATURE\t= 0x");
1714 Serial.print("Data Rate\t= ");
1715 Serial.println(NRF_RADIO->MODE ? "2 MBPS" : "1MBPS");
1716 Serial.println("Model\t\t= NRF52");
1717 Serial.print("CRC Length\t= ");
1718 uint8_t crcLen = getCRCLength();
1719 if (crcLen == NRF_CRC_16) {
1720 Serial.println("16 bits");
1721 }
1722 else if (crcLen == NRF_CRC_8) {
1723 Serial.println("8 bits");
1724 }
1725 else {
1726 Serial.println("Disabled");
1727 }
1728 Serial.print("PA Power\t= ");
1729 uint8_t paLevel = getPALevel();
1730 if (paLevel == NRF_PA_MAX) {
1731 Serial.println("PA_MAX");
1732 }
1733 else if (paLevel == NRF_PA_HIGH) {
1734 Serial.println("PA_HIGH");
1735 }
1736 else if (paLevel == NRF_PA_LOW) {
1737 Serial.println("PA_LOW");
1738 }
1739 else if (paLevel == NRF_PA_MIN) {
1740 Serial.println("PA_MIN");
1741 }
1742 else {
1743 Serial.println("?");
1744 }
1745 Serial.print("ARC\t\t= ");
1746 Serial.println(arcCounter);
1747}
1748
1749/**********************************************************************************************************/
1750
1751#if defined CCM_ENCRYPTION_ENABLED
1752
1753 #if defined NRF54_CCM_ENCRYPTION
1754
1755// The nRF54L15 CCM takes the key and nonce in registers and the data through EasyDMA job lists.
1756// The core's Ccm class handles that and builds the same Bluetooth LE nonce (packet counter,
1757// direction bit, IV) as the nRF52 CCM, so frames are compatible with nRF52 nodes and RF24CCM.
1758// The result goes to outBuffer[CCM_START_SIZE] and its length to outBuffer[1], as on the nRF52.
1759
1760uint8_t nrf_to_nrf::encrypt(void* bufferIn, uint8_t size)
1761{
1762 if (!size || size > MAX_PACKET_SIZE) {
1763 return 0;
1764 }
1765 memset(outBuffer, 0, sizeof(outBuffer));
1766
1767 uint8_t length = 0;
1768 if (!nrf54Ccm.encryptBlePacket(ccmData.key, ccmData.iv, ccmData.counter, ccmData.direction, 0, static_cast<const uint8_t*>(bufferIn), size,
1769 &outBuffer[CCM_START_SIZE], &length, xiao_nrf54l15::CcmBleDataRate::k2Mbit)) {
1770 return 0;
1771 }
1772 outBuffer[1] = length;
1773 return length;
1774}
1775
1776/**********************************************************************************************************/
1777
1778uint8_t nrf_to_nrf::decrypt(void* bufferIn, uint8_t size)
1779{
1780 if (!size || size > MAX_PACKET_SIZE) {
1781 return 0;
1782 }
1783 memset(outBuffer, 0, sizeof(outBuffer));
1784
1785 // Decryption runs at the pace of the data rate, so use the fastest BLE rate
1786 uint8_t length = 0;
1787 bool macValid = false;
1788 if (!nrf54Ccm.decryptBlePacket(ccmData.key, ccmData.iv, ccmData.counter, ccmData.direction, 0, static_cast<const uint8_t*>(bufferIn), size,
1789 &outBuffer[CCM_START_SIZE], &length, &macValid, xiao_nrf54l15::CcmBleDataRate::k2Mbit)
1790 || !macValid) {
1791 return 0;
1792 }
1793 outBuffer[1] = length;
1794 return length;
1795}
1796
1797/**********************************************************************************************************/
1798
1799void nrf_to_nrf::setKey(uint8_t key[CCM_KEY_SIZE])
1800{
1801 nrf54Rng.begin();
1802 memcpy(ccmData.key, key, CCM_KEY_SIZE);
1803}
1804
1805/**********************************************************************************************************/
1806
1807 #else
1808
1809uint8_t nrf_to_nrf::encrypt(void* bufferIn, uint8_t size)
1810{
1811 NRF_CCM->MODE = 0 | 1 << 24 | 1 << 16;
1812
1813 if (!size) {
1814 return 0;
1815 }
1816 if (size > MAX_PACKET_SIZE) {
1817 return 0;
1818 }
1819
1820 inBuffer[0] = 0;
1821 inBuffer[1] = size;
1822 inBuffer[2] = 0;
1823
1824 memcpy(&inBuffer[CCM_START_SIZE], bufferIn, size);
1825 memset(outBuffer, 0, sizeof(outBuffer));
1826
1827 NRF_CCM->EVENTS_ENDKSGEN = 0;
1828 NRF_CCM->EVENTS_ENDCRYPT = 0;
1829 NRF_CCM->TASKS_KSGEN = 1;
1830 if (!waitForEvent(&NRF_CCM->EVENTS_ENDCRYPT))
1831 return 0;
1832
1833 if (NRF_CCM->EVENTS_ERROR) {
1834 return 0;
1835 }
1836 return outBuffer[1];
1837}
1838
1839/**********************************************************************************************************/
1840
1841uint8_t nrf_to_nrf::decrypt(void* bufferIn, uint8_t size)
1842{
1843 NRF_CCM->MODE = 1 | 1 << 24 | 1 << 16;
1844
1845 if (!size) {
1846 return 0;
1847 }
1848 if (size > MAX_PACKET_SIZE) {
1849 return 0;
1850 }
1851
1852 memcpy(&inBuffer[CCM_START_SIZE], bufferIn, size);
1853
1854 inBuffer[0] = 0;
1855 inBuffer[1] = size;
1856 inBuffer[2] = 0;
1857
1858 memset(outBuffer, 0, sizeof(outBuffer));
1859
1860 NRF_CCM->EVENTS_ENDKSGEN = 0;
1861 NRF_CCM->EVENTS_ENDCRYPT = 0;
1862 NRF_CCM->TASKS_KSGEN = 1;
1863
1864 if (!waitForEvent(&NRF_CCM->EVENTS_ENDCRYPT))
1865 return 0;
1866
1867 if (NRF_CCM->EVENTS_ERROR) {
1868 return 0;
1869 }
1870
1871 if (NRF_CCM->MICSTATUS == (CCM_MICSTATUS_MICSTATUS_CheckFailed << CCM_MICSTATUS_MICSTATUS_Pos)) {
1872 return 0;
1873 }
1874
1875 return outBuffer[1];
1876}
1877
1878/**********************************************************************************************************/
1879
1881{
1882
1883 NRF_CCM->MODE = 1 << 24 | 1 << 16;
1884#ifndef NRF52832_XXAA
1885 NRF_CCM->MAXPACKETSIZE = MAX_PACKET_SIZE > 251 ? 251 : MAX_PACKET_SIZE;
1886#endif
1887 NRF_CCM->SHORTS = 1;
1888 NRF_CCM->ENABLE = 2;
1889
1890 NRF_RNG->CONFIG = 1;
1891 NRF_RNG->TASKS_START = 1;
1892
1893 memcpy(ccmData.key, key, CCM_KEY_SIZE);
1894}
1895
1896 #endif // defined NRF54_CCM_ENCRYPTION
1897
1898/**********************************************************************************************************/
1899
1900void nrf_to_nrf::setCounter(uint64_t counter)
1901{
1902
1903 ccmData.counter = counter;
1904 packetCounter = counter;
1905}
1906/**********************************************************************************************************/
1907
1909{
1910
1911 for (int i = 0; i < CCM_IV_SIZE; i++) {
1912 ccmData.iv[i] = IV[i];
1913 }
1914}
1915
1916/**********************************************************************************************************/
1917
1918bool nrf_to_nrf::generateIV(uint8_t iv[CCM_IV_SIZE])
1919{
1920 #if defined NRF54_CCM_ENCRYPTION
1921 return nrf54Rng.fill(iv, CCM_IV_SIZE);
1922 #else
1923 for (int i = 0; i < CCM_IV_SIZE; i++) {
1924 if (!waitForEvent(&NRF_RNG->EVENTS_VALRDY, 100)) {
1925 return false;
1926 }
1927 NRF_RNG->EVENTS_VALRDY = 0;
1928 iv[i] = NRF_RNG->VALUE;
1929 }
1930 return true;
1931 #endif
1932}
1933
1934#endif // defined CCM_ENCRYPTION_ENABLED
Driver for the 2.4GHz radio in nRF52x and nRF54x chips, using the RF24 API.
Definition nrf_to_nrf.h:142
void startListening(bool resetAddresses=true)
void openReadingPipe(uint8_t child, const uint8_t *address)
bool available()
void setPALevel(uint8_t level, bool lnaEnable=true)
void disableRxInterrupt()
bool testCarrier(uint8_t RSSI=65)
bool txStandBy()
uint8_t flush_tx()
uint8_t decrypt(void *bufferIn, uint8_t size)
uint8_t radioData[ACTUAL_MAX_PAYLOAD_SIZE+2]
Definition nrf_to_nrf.h:408
uint32_t addrConv32(uint32_t addr)
void disableDynamicPayloads()
bool write(void *buf, uint8_t len, bool multicast=false, bool doEncryption=true)
bool testRPD(uint8_t RSSI=65)
void setCounter(uint64_t counter)
bool isChipConnected()
void setAddressWidth(uint8_t a_width)
void setChannel(uint8_t channel, bool map=0)
uint8_t getDynamicPayloadSize()
bool setDataRate(uint8_t speed)
void enableDynamicAck()
void setKey(uint8_t key[CCM_KEY_SIZE])
bool writeAckPayload(uint8_t pipe, void *buf, uint8_t len)
bool enableEncryption
Definition nrf_to_nrf.h:890
void enableDynamicPayloads(uint8_t payloadSize=DEFAULT_MAX_PAYLOAD_SIZE)
void handleInterrupt()
void disableAckPayload()
void disableCRC()
void powerDown()
uint8_t getARC()
uint8_t flush_rx()
uint8_t encrypt(void *bufferIn, uint8_t size)
void printDetails()
uint8_t getChannel()
bool startWrite(void *buf, uint8_t len, bool multicast, bool doEncryption=true)
uint8_t getRSSI()
uint16_t interframeSpacing
Definition nrf_to_nrf.h:816
uint8_t outBuffer[MAX_PACKET_SIZE+CCM_MIC_SIZE+CCM_START_SIZE]
Definition nrf_to_nrf.h:857
void setRetries(uint8_t retryVar, uint8_t attempts)
void enableAckPayload()
void openWritingPipe(const uint8_t *address)
void setCRCLength(nrf_crclength_e length)
bool writeFast(void *buf, uint8_t len, bool multicast=0)
uint8_t getPALevel()
void setAutoAck(bool enable)
void read(void *buf, uint8_t len)
void stopListening(bool setWritingPipe=true, bool resetAddresses=true)
bool enableRxInterrupt(uint8_t depth=3)
void setIV(uint8_t IV[CCM_IV_SIZE])
void setPayloadSize(uint8_t size)
nrf_crclength_e getCRCLength()
uint8_t getPayloadSize()
#define RADIO_MODE_MODE_Nrf_250Kbit
#define TXPOWER_PA_MIN
static nrf_to_nrf * rxIrqRadio
#define __REV(x)
#define TXPOWER_PA_MAX
#define TXPOWER_PA_HIGH
static bool waitForEvent(volatile uint32_t *event, uint32_t timeout=DEFAULT_TIMEOUT)
#define TXPOWER_PA_LOW
__attribute__((__used__)) void RX_IRQ_HANDLER(void)
static void rxIrqDisable()
static uint32_t bytewise_bit_swap(uint8_t const *p_inp)
#define RX_IRQ_NUMBER
static uint32_t addr_conv(uint8_t const *p_addr)
#define DEFAULT_TIMEOUT
#define RX_IRQ_MASK
#define RX_IRQ_HANDLER
#define ACK_TIMEOUT_1MBPS_OFFSET
Definition nrf_to_nrf.h:25
#define DEFAULT_MAX_PAYLOAD_SIZE
Definition nrf_to_nrf.h:20
#define ACK_PAYLOAD_TIMEOUT_OFFSET
Definition nrf_to_nrf.h:28
@ NRF_PA_MAX
Definition nrf_to_nrf.h:81
@ NRF_PA_HIGH
Definition nrf_to_nrf.h:77
@ NRF_PA_LOW
Definition nrf_to_nrf.h:73
@ NRF_PA_MIN
Definition nrf_to_nrf.h:69
@ NRF_PA_ERROR
Definition nrf_to_nrf.h:85
#define CCM_COUNTER_SIZE
Definition nrf_to_nrf.h:44
#define ACK_TIMEOUT_250KBPS_OFFSET
Definition nrf_to_nrf.h:27
nrf_crclength_e
Definition nrf_to_nrf.h:125
@ NRF_CRC_8
Definition nrf_to_nrf.h:129
@ NRF_CRC_24
Definition nrf_to_nrf.h:133
@ NRF_CRC_DISABLED
Definition nrf_to_nrf.h:127
@ NRF_CRC_16
Definition nrf_to_nrf.h:131
#define ACK_TIMEOUT_250KBPS
Definition nrf_to_nrf.h:24
@ NRF_1MBPS
Definition nrf_to_nrf.h:99
@ NRF_2MBPS
Definition nrf_to_nrf.h:101
@ NRF_250KBPS
Definition nrf_to_nrf.h:103
#define ACK_TIMEOUT_2MBPS
Definition nrf_to_nrf.h:23
#define CCM_START_SIZE
Definition nrf_to_nrf.h:46
#define ACTUAL_MAX_PAYLOAD_SIZE
Definition nrf_to_nrf.h:21
#define MAX_PACKET_SIZE
Definition nrf_to_nrf.h:40
#define CCM_MIC_SIZE
Definition nrf_to_nrf.h:45
#define CCM_KEY_SIZE
Definition nrf_to_nrf.h:41
#define ACK_TIMEOUT_2MBPS_OFFSET
Definition nrf_to_nrf.h:26
#define ACK_TIMEOUT_1MBPS
Definition nrf_to_nrf.h:22
#define CCM_IV_SIZE
Definition nrf_to_nrf.h:42
RadioIrqLock(bool lock)