5#if defined NRF54_CCM_ENCRYPTION
6 #include <nrf54l15_hal.h>
7static xiao_nrf54l15::Ccm nrf54Ccm;
8static xiao_nrf54l15::CracenRng nrf54Rng;
12 #define __REV(x) __builtin_bswap32(x);
15#if defined(NRF52832_XXAA) || defined(NRF52832_XXAB) || defined(NRF52811_XXAA) || defined(NRF52810_XXAA) || defined(NRF52805_XXAA)
17 #define TXPOWER_PA_MIN 0xF4
18 #define TXPOWER_PA_LOW 0xFC
19 #define TXPOWER_PA_HIGH 0x00
20 #define TXPOWER_PA_MAX 0x04
21#elif !defined(ARDUINO_NRF54L15)
23 #define TXPOWER_PA_MIN 0xF4
24 #define TXPOWER_PA_LOW 0x02
25 #define TXPOWER_PA_HIGH 0x06
26 #define TXPOWER_PA_MAX 0x08
28 #define TXPOWER_PA_MIN 0x6
29 #define TXPOWER_PA_LOW 0x1F
30 #define TXPOWER_PA_HIGH 0x33
31 #define TXPOWER_PA_MAX 0x3F
35#ifndef RADIO_MODE_MODE_Nrf_250Kbit
36 #define RADIO_MODE_MODE_Nrf_250Kbit (2UL)
38#ifdef ARDUINO_NRF54L15
39 #define RADIO_MODE_MODE_Nrf_4Mbit_OBT4 (10UL)
40 #define RADIO_MODE_MODE_Nrf_4Mbit_OBT6 (9UL)
43#define DEFAULT_TIMEOUT 250
50 uint32_t start = millis();
51 for (uint32_t count = 0; *
event == 0; count++) {
52 if (millis() - start > timeout || count > 4000000) {
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
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
73 #define RX_IRQ_HANDLER RADIO_IRQHandler_v
75 #define RX_IRQ_HANDLER RADIO_IRQHandler
86 (void)NRF_RADIO->RX_IRQ_CLEAR;
95 (void)NRF_RADIO->RX_IRQ_CLEAR;
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;
143 buffer[0] = addr & 0xFF;
144 buffer[1] = (addr >> 8) & 0xFF;
145 buffer[2] = (addr >> 16) & 0xFF;
146 buffer[3] = (addr >> 24) & 0xFF;
156 staticPayloadSize = 32;
159 ackPayloadsEnabled =
false;
164 payloadAvailable =
false;
172 ackPayloadSent =
false;
174#ifndef ARDUINO_NRF54L15
180#if defined CCM_ENCRYPTION_ENABLED
181 #if !defined NRF54_CCM_ENCRYPTION
182 NRF_CCM->INPTR = (uint32_t)inBuffer;
184 NRF_CCM->CNFPTR = (uint32_t)&ccmData;
185 NRF_CCM->SCRATCHPTR = (uint32_t)scratchPTR;
187 ccmData.counter = 12345;
197#ifndef ARDUINO_NRF54L15
198 NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
199 NRF_CLOCK->TASKS_HFCLKSTART = 1;
205 NRF_POWER->TASKS_CONSTLAT = 1;
206 NRF_CLOCK->EVENTS_XOSTARTED = 0;
207 NRF_CLOCK->TASKS_XOSTART = 1;
213 NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
214 NRF_CLOCK->TASKS_LFCLKSTART = 1;
220#ifndef ARDUINO_NRF54L15
221 NRF_RADIO->POWER = 1;
224 NRF_RADIO->PCNF0 = (1 << RADIO_PCNF0_S0LEN_Pos) | (0 << RADIO_PCNF0_LFLEN_Pos) | (1 << RADIO_PCNF0_S1LEN_Pos);
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);
228 NRF_RADIO->BASE0 = 0xE7E7E7E7;
229 NRF_RADIO->BASE1 = 0x43434343;
230 NRF_RADIO->PREFIX0 = 0x23C343E7;
231 NRF_RADIO->PREFIX1 = 0x13E363A3;
232 NRF_RADIO->RXADDRESSES = 0x01;
233 NRF_RADIO->TXADDRESS = 0x00;
235 txBase = NRF_RADIO->BASE0;
236 txPrefix = NRF_RADIO->PREFIX0;
237 rxBase = NRF_RADIO->BASE0;
238 rxPrefix = NRF_RADIO->PREFIX0;
240 NRF_RADIO->CRCCNF = RADIO_CRCCNF_LEN_Two;
241 NRF_RADIO->CRCINIT = 0xFFFFUL;
242 NRF_RADIO->CRCPOLY = 0x11021UL;
244 NRF_RADIO->PACKETPTR = (uint32_t)
radioData;
245 NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_1Mbit << RADIO_MODE_MODE_Pos);
247#ifndef ARDUINO_NRF54L15
248 NRF_RADIO->MODECNF0 = 0x201;
250 NRF_RADIO->TIMING = 0x1;
252 NRF_RADIO->TXPOWER = (
TXPOWER_PA_MAX << RADIO_TXPOWER_TXPOWER_Pos);
253 NRF_RADIO->FREQUENCY = 0x4C;
259#ifdef ARDUINO_NRF54L15
260 NRF_RADIO->TASKS_START = 1;
269#ifdef NRF_HAS_ENERGY_DETECT
270 #define ED_RSSISCALE 4
271uint8_t nrf_to_nrf::sample_ed(
void)
275 NRF_RADIO->EVENTS_EDEND = 0;
276 NRF_RADIO->TASKS_EDSTART = 1;
280 val = NRF_RADIO->EDSAMPLE;
281 return (uint8_t)(val > 63
283 : val * ED_RSSISCALE);
291 uint8_t pipe_num = 0;
299 if (rxQueue !=
nullptr || depth == 0 || depth == 255) {
304 rxQueueSlots = depth + 1;
305 rxQueueSlotSize = staticPayloadSize + 2;
306 rxQueue = (uint8_t*)malloc(rxQueueSlots * rxQueueSlotSize);
307 if (rxQueue ==
nullptr) {
310 if (ackPayloadsEnabled) {
328 if (rxQueue ==
nullptr) {
339 if (ackQueue !=
nullptr) {
342 const uint8_t* slot = &ackQueue[((ackQueueHead + ackQueueCount - 1) % ackQueueDepth) * ackQueueSlotSize];
344 memcpy(ackBuffer, &slot[1], slot[1] + 1);
345 ackPayloadSent =
false;
356void nrf_to_nrf::allocateAckQueue()
359 if (rxQueue ==
nullptr || ackQueue !=
nullptr) {
362 ackQueueDepth = rxQueueSlots - 1;
363 ackQueueSlotSize = rxQueueSlotSize;
366 ackQueue = (uint8_t*)malloc(ackQueueDepth * ackQueueSlotSize);
371void nrf_to_nrf::loadAckPayload(uint8_t pipe)
374 if (ackPayloadSent) {
376 ackPayloadSent =
false;
378 if (ackQueue ==
nullptr) {
384 for (uint8_t i = 0; i < ackQueueCount; i++) {
385 uint8_t* slot = &ackQueue[((ackQueueHead + i) % ackQueueDepth) * ackQueueSlotSize];
386 if (slot[0] != pipe) {
389 memcpy(ackBuffer, &slot[1], slot[1] + 1);
391 for (uint8_t j = i; j > 0; j--) {
392 memcpy(&ackQueue[((ackQueueHead + j) % ackQueueDepth) * ackQueueSlotSize],
393 &ackQueue[((ackQueueHead + j - 1) % ackQueueDepth) * ackQueueSlotSize], ackQueueSlotSize);
395 ackQueueHead = (ackQueueHead + 1) % ackQueueDepth;
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;
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));
421 memcpy(&slot[2], &rxBuffer[1], length);
422 payloadAvailable =
false;
427 (void)NRF_RADIO->EVENTS_CRCOK;
432uint8_t* nrf_to_nrf::rxQueueFront()
435 if (rxQueue ==
nullptr || (!inRxMode && ackPayloadAvailable) || rxQueueTail == rxQueueHead) {
438 return &rxQueue[rxQueueTail * rxQueueSlotSize];
445 if (rxQueue ==
nullptr) {
446 return pollRadio(pipe_num);
448 if (!inRxMode && ackPayloadAvailable) {
449 *pipe_num = ackAvailablePipeNo;
452 const uint8_t* slot = rxQueueFront();
453 if (slot ==
nullptr) {
462bool nrf_to_nrf::pollRadio(uint8_t* pipe_num)
465 if (payloadAvailable) {
466 *pipe_num = (uint8_t)NRF_RADIO->RXMATCH;
471 if (ackPayloadAvailable) {
472 *pipe_num = ackAvailablePipeNo;
476 if (NRF_RADIO->EVENTS_CRCOK) {
477 NRF_RADIO->EVENTS_CRCOK = 0;
480 return restartReturnRx();
484 *pipe_num = (uint8_t)NRF_RADIO->RXMATCH;
485 if (!DPL && acksEnabled(0) ==
false) {
486#if defined CCM_ENCRYPTION_ENABLED
494 memcpy(&rxBuffer[1], &
radioData[0], staticPayloadSize);
495#if defined CCM_ENCRYPTION_ENABLED
500#if defined CCM_ENCRYPTION_ENABLED
517 memcpy(&rxBuffer[1], &
radioData[2], staticPayloadSize);
519#if defined CCM_ENCRYPTION_ENABLED
524 rxFifoAvailable =
true;
525 uint8_t packetCtr = 0;
532 rxBuffer[0] = staticPayloadSize;
536 uint16_t packetData = NRF_RADIO->RXCRC;
538 if (acksEnabled(NRF_RADIO->RXMATCH)) {
540 const bool duplicate = NRF_RADIO->CRCCNF != 0 && packetCtr == lastPacketCounter && packetData == lastData;
543 loadAckPayload(*pipe_num);
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;
559 uint8_t payloadSize = 0;
566 setStaticLength(payloadSize);
569 NRF_RADIO->TXADDRESS = txAddress;
573 return restartReturnRx();
577#if defined CCM_ENCRYPTION_ENABLED
579 uint8_t bufferLength = 0;
586 if (!
decrypt(&rxBuffer[1], bufferLength)) {
588 Serial.println(
"DECRYPT FAIL");
590 return restartReturnRx();
593 memset(&rxBuffer[1], 0,
sizeof(rxBuffer) - 1);
604 lastPacketCounter = packetCtr;
605 lastData = packetData;
607 if (inRxMode && !acksEnabled(NRF_RADIO->RXMATCH)) {
608 NRF_RADIO->TASKS_START = 1;
610 if ((DPL && rxBuffer[0]) || !DPL) {
611 payloadAvailable =
true;
615 if (NRF_RADIO->EVENTS_CRCERROR) {
616 NRF_RADIO->EVENTS_CRCERROR = 0;
617 NRF_RADIO->TASKS_START = 1;
624bool nrf_to_nrf::restartReturnRx()
627 NRF_RADIO->TASKS_START = 1;
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;
642 memcpy(buf, &rxBuffer[1], len);
643 ackPayloadAvailable =
false;
644 payloadAvailable =
false;
652 uint8_t PID = ackPID;
654 PID = ((ackPID += 1) % 7) << 1;
659 uint8_t payloadSize = 0;
661#if defined CCM_ENCRYPTION_ENABLED
666 if (!generateIV(ccmData.iv)) {
669 ccmData.counter = packetCounter;
677 if (packetCounter > 200000) {
684 for (
int i = 0; i < (retries + 1); i++) {
695 uint8_t dataStart = 0;
697#if defined CCM_ENCRYPTION_ENABLED
704 dataStart = (!DPL && acksEnabled(0) ==
false) ? 0 : 2;
705#if defined CCM_ENCRYPTION_ENABLED
709#if defined CCM_ENCRYPTION_ENABLED
718#if defined CCM_ENCRYPTION_ENABLED
721#ifdef ARDUINO_NRF54L15
722 uint32_t timeout = millis();
723 while (NRF_RADIO->STATE != 10) {
727 if (millis() - timeout > 250) {
732 NRF_RADIO->EVENTS_END = 0;
733 NRF_RADIO->TASKS_START = 1;
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;
747 int32_t realAckTimeout = (int32_t)ackTimeout;
749 if (NRF_RADIO->MODE == (RADIO_MODE_MODE_Nrf_1Mbit << RADIO_MODE_MODE_Pos)) {
761 realAckTimeout += 200;
767 if (realAckTimeout < 0) {
771 uint32_t ack_timeout = micros();
772 while (!NRF_RADIO->EVENTS_CRCOK && !NRF_RADIO->EVENTS_CRCERROR) {
773 if (micros() - ack_timeout > realAckTimeout) {
777 if (NRF_RADIO->EVENTS_CRCOK) {
778 if (ackPayloadsEnabled &&
radioData[0] > 0) {
779#if defined CCM_ENCRYPTION_ENABLED
790#if defined CCM_ENCRYPTION_ENABLED
796 Serial.println(
"DECRYPT FAIL");
806 ackPayloadAvailable =
true;
807 ackAvailablePipeNo = NRF_RADIO->RXMATCH;
809 NRF_RADIO->EVENTS_CRCOK = 0;
812 setStaticLength(payloadSize);
814 NRF_RADIO->RXADDRESSES = rxAddress;
818 else if (NRF_RADIO->EVENTS_CRCERROR) {
819 NRF_RADIO->EVENTS_CRCERROR = 0;
821 uint32_t duration = 258 * retryDuration;
822 delayMicroseconds(duration);
825 setStaticLength(payloadSize);
827 NRF_RADIO->RXADDRESSES = rxAddress;
834 lastTxResult =
false;
843 uint8_t PID = ackPID;
845 PID = ((ackPID += 1) % 7) << 1;
851#if defined CCM_ENCRYPTION_ENABLED
853 uint32_t tmpCounter = 0;
858 if (!generateIV(tmpIV)) {
862 tmpCounter = packetCounter;
863 ccmData.counter = tmpCounter;
871 if (packetCounter > 200000) {
889 uint8_t dataStart = 0;
891#if defined CCM_ENCRYPTION_ENABLED
898 dataStart = (!DPL && acksEnabled(0) ==
false) ? 0 : 2;
899#if defined CCM_ENCRYPTION_ENABLED
903#if defined CCM_ENCRYPTION_ENABLED
912#if defined CCM_ENCRYPTION_ENABLED
916 NRF_RADIO->EVENTS_END = 0;
917 NRF_RADIO->TASKS_START = 1;
929 uint16_t sendLength = len;
930#if defined CCM_ENCRYPTION_ENABLED
937 uint8_t* dest = ackBuffer;
938 if (rxQueue !=
nullptr) {
939 if (ackQueue ==
nullptr) {
942 if (ackQueueCount >= ackQueueDepth || sendLength > ackQueueSlotSize - 2) {
945 uint8_t* slot = &ackQueue[((ackQueueHead + ackQueueCount) % ackQueueDepth) * ackQueueSlotSize];
953#if defined CCM_ENCRYPTION_ENABLED
957 if (!generateIV(ccmData.iv)) {
962 ccmData.counter = packetCounter;
972 if (packetCounter > 200000) {
979 memcpy(&dest[1], buf, len);
980#if defined CCM_ENCRYPTION_ENABLED
984 if (dest == ackBuffer) {
986 ackPayloadSent =
false;
999 ackPayloadsEnabled =
true;
1012 ackPayloadsEnabled =
false;
1024 if (resetAddresses && rxQueue !=
nullptr) {
1029 NRF_RADIO->SHORTS = 0x0;
1030 NRF_RADIO->EVENTS_DISABLED = 0;
1031 NRF_RADIO->TASKS_DISABLE = 1;
1034 NRF_RADIO->EVENTS_DISABLED = 0;
1036 if (resetAddresses ==
true) {
1037 NRF_RADIO->BASE0 = rxBase;
1038 NRF_RADIO->PREFIX0 = rxPrefix;
1039#ifndef ARDUINO_NRF54L15
1040 NRF_RADIO->MODECNF0 = 0x201;
1042 NRF_RADIO->TIMING = 0x1;
1045 NRF_RADIO->TIFS = 0;
1047#if defined(NRF52832_XXAA)
1048 NRF_RADIO->EVENTS_READY = 0;
1050 NRF_RADIO->EVENTS_RXREADY = 0;
1052 NRF_RADIO->EVENTS_CRCOK = 0;
1053 NRF_RADIO->TASKS_RXEN = 1;
1054#if defined(NRF52832_XXAA)
1061 NRF_RADIO->TASKS_START = 1;
1063 if (resetAddresses && rxQueue !=
nullptr) {
1072 if (resetAddresses && rxQueue !=
nullptr) {
1076 NRF_RADIO->EVENTS_DISABLED = 0;
1077 NRF_RADIO->TASKS_DISABLE = 1;
1080 NRF_RADIO->EVENTS_DISABLED = 0;
1082 if (resetAddresses) {
1083 NRF_RADIO->BASE0 = txBase;
1084 NRF_RADIO->PREFIX0 = txPrefix;
1086 if (setWritingPipe) {
1087 NRF_RADIO->TXADDRESS = 0x00;
1089#ifndef ARDUINO_NRF54L15
1090 NRF_RADIO->MODECNF0 = 0x200;
1092 NRF_RADIO->TIMING = 0x0;
1095#ifndef ARDUINO_NRF54L15
1096 NRF_RADIO->SHORTS = 0x6;
1098 NRF_RADIO->SHORTS = 0x80004;
1100 if (NRF_RADIO->STATE < 9) {
1101#if defined(NRF52832_XXAA)
1102 NRF_RADIO->EVENTS_READY = 0;
1104 NRF_RADIO->EVENTS_TXREADY = 0;
1106 NRF_RADIO->TASKS_TXEN = 1;
1107#if defined(NRF52832_XXAA)
1110 NRF_RADIO->EVENTS_READY = 0;
1114 NRF_RADIO->EVENTS_TXREADY = 0;
1133 const uint8_t* slot = rxQueueFront();
1134 if (slot !=
nullptr) {
1137 uint8_t size = min(staticPayloadSize, rxBuffer[0]);
1146 uint32_t freq = NRF_RADIO->FREQUENCY;
1147 NRF_RADIO->FREQUENCY = 0x4C;
1148 if (NRF_RADIO->FREQUENCY == 0x4C) {
1149 NRF_RADIO->FREQUENCY = freq;
1168 for (
int i = 0; i < 8; i++) {
1169 acksPerPipe[i] = enable;
1181 acksPerPipe[pipe] = enable;
1194 staticPayloadSize = payloadSize;
1196 if (payloadSize <= 63) {
1197 NRF_RADIO->PCNF0 = (0 << RADIO_PCNF0_S0LEN_Pos) | (6 << RADIO_PCNF0_LFLEN_Pos) | (3 << RADIO_PCNF0_S1LEN_Pos);
1201 NRF_RADIO->PCNF0 = (0 << RADIO_PCNF0_S0LEN_Pos) | (8 << RADIO_PCNF0_LFLEN_Pos) | (3 << RADIO_PCNF0_S1LEN_Pos);
1204 NRF_RADIO->PCNF1 &= ~(0xFF << RADIO_PCNF1_MAXLEN_Pos | 0xFF << RADIO_PCNF1_STATLEN_Pos);
1205 NRF_RADIO->PCNF1 |= payloadSize << RADIO_PCNF1_MAXLEN_Pos;
1216 uint8_t lenConfig = 0;
1217 if (acksEnabled(0)) {
1220 NRF_RADIO->PCNF0 = (lenConfig << RADIO_PCNF0_S0LEN_Pos) | (0 << RADIO_PCNF0_LFLEN_Pos) | (lenConfig << RADIO_PCNF0_S1LEN_Pos);
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;
1230 setStaticLength(size);
1236void nrf_to_nrf::setStaticLength(uint8_t size)
1238 staticPayloadSize = size;
1241 uint8_t lenConfig = 0;
1242 if (acksEnabled(0)) {
1245 NRF_RADIO->PCNF0 = (lenConfig << RADIO_PCNF0_S0LEN_Pos) | (0 << RADIO_PCNF0_LFLEN_Pos) | (lenConfig << RADIO_PCNF0_S1LEN_Pos);
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;
1253void nrf_to_nrf::resizeRxQueue()
1255 if (rxQueue !=
nullptr && rxQueueSlotSize != staticPayloadSize + 2) {
1256 uint8_t depth = rxQueueSlots - 1;
1266 return staticPayloadSize;
1275 retryDuration = retryVar;
1283 uint32_t prefix =
addrConv32(address & 0xFF) >> 24;
1293 uint32_t prefix =
addrConv32(address & 0xFF) >> 24;
1303 uint32_t prefix =
addr_conv(&address[0]) >> 24;
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;
1322 else if (child < 4) {
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;
1330 NRF_RADIO->BASE1 = base;
1331 NRF_RADIO->PREFIX1 &= ~(0xFF << (8 * (child - 4)));
1332 NRF_RADIO->PREFIX1 |= prefix << (8 * (child - 4));
1334 NRF_RADIO->RXADDRESSES |= 1 << child;
1343 uint32_t prefix =
addr_conv(&address[0]) >> 24;
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;
1364 return lastTxResult;
1371 return lastTxResult;
1378 lastTxResult =
write(buf, len, multicast);
1379 return lastTxResult;
1384bool nrf_to_nrf::acksEnabled(uint8_t pipe)
1387 if (acksPerPipe[pipe]) {
1403 NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_1Mbit << RADIO_MODE_MODE_Pos);
1411 NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_2Mbit << RADIO_MODE_MODE_Pos);
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);
1420 NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_4Mbit_OBT6 << RADIO_MODE_MODE_Pos);
1433 uint8_t paLevel = 0x00;
1447 NRF_RADIO->TXPOWER = paLevel;
1455 uint8_t paLevel = NRF_RADIO->TXPOWER;
1486 NRF_RADIO->CRCCNF = RADIO_CRCCNF_LEN_Three;
1487 NRF_RADIO->CRCINIT = 0x555555UL;
1488 NRF_RADIO->CRCPOLY = 0x65BUL;
1491 NRF_RADIO->CRCCNF = RADIO_CRCCNF_LEN_Two;
1492 NRF_RADIO->CRCINIT = 0xFFFFUL;
1493 NRF_RADIO->CRCPOLY = 0x11021UL;
1496 NRF_RADIO->CRCCNF = RADIO_CRCCNF_LEN_One;
1497 NRF_RADIO->CRCINIT = 0xFFUL;
1498 NRF_RADIO->CRCPOLY = 0x107UL;
1501 NRF_RADIO->CRCCNF = 0;
1502 NRF_RADIO->CRCINIT = 0x00L;
1503 NRF_RADIO->CRCPOLY = 0x00UL;
1515 if (NRF_RADIO->CRCCNF == 0) {
1518 else if (NRF_RADIO->CRCCNF == RADIO_CRCCNF_LEN_One) {
1521 if (NRF_RADIO->CRCCNF == RADIO_CRCCNF_LEN_Two) {
1533#ifndef ARDUINO_NRF54L15
1535 NRF_RADIO->EVENTS_RSSIEND = 0;
1536 NRF_RADIO->TASKS_RSSISTART = 1;
1540 if (NRF_RADIO->RSSISAMPLE < RSSI) {
1558#ifndef ARDUINO_NRF54L15
1560 NRF_RADIO->EVENTS_RSSIEND = 0;
1561 NRF_RADIO->TASKS_RSSISTART = 1;
1565 return (uint8_t)NRF_RADIO->RSSISAMPLE;
1574 rxQueueTail = rxQueueHead;
1575 payloadAvailable =
false;
1576 ackPayloadAvailable =
false;
1588 ackPayloadSent =
false;
1596#ifndef ARDUINO_NRF54L15
1597 NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
1598 NRF_CLOCK->TASKS_HFCLKSTART = 1;
1603 NRF_RADIO->POWER = 1;
1605 NRF_POWER->TASKS_CONSTLAT = 1;
1606 NRF_CLOCK->EVENTS_XOSTARTED = 0;
1607 NRF_CLOCK->TASKS_XOSTART = 1;
1612#ifdef CCM_ENCRYPTION_ENABLED
1614 #if defined NRF54_CCM_ENCRYPTION
1617 NRF_RNG->CONFIG = 1;
1618 NRF_RNG->TASKS_START = 1;
1619 NRF_CCM->ENABLE = 2;
1629#ifndef ARDUINO_NRF54L15
1630 NRF_RADIO->POWER = 0;
1631 NRF_CLOCK->TASKS_HFCLKSTOP = 1;
1633 NRF_CLOCK->TASKS_XOSTOP = 1;
1636#ifdef CCM_ENCRYPTION_ENABLED
1638 #if defined NRF54_CCM_ENCRYPTION
1641 NRF_RNG->TASKS_STOP = 1;
1642 NRF_RNG->CONFIG = 0;
1643 NRF_CCM->ENABLE = 0;
1652 NRF_RADIO->PCNF1 &= ~(0xFF << RADIO_PCNF1_BALEN_Pos);
1653 NRF_RADIO->PCNF1 |= (a_width - 1) << RADIO_PCNF1_BALEN_Pos;
1660 uint8_t addressWidth = ((NRF_RADIO->PCNF1 >> 16) & 0xFF) + 1;
1662 Serial.println(
"================ Radio Configuration ================");
1663 Serial.print(
"STATUS\t\t= ");
1664 Serial.println(NRF_RADIO->STATE);
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);
1672 uint32_t prefixes =
addrConv32(NRF_RADIO->PREFIX0);
1673 uint8_t prefix = (prefixes >> 24) & 0xFF;
1674 Serial.print(prefix, HEX);
1675 Serial.print(
" 0x");
1677 for (
int i = addressWidth - 2; i > -1; i--) {
1678 Serial.print((base >> (i * 8)) & 0xFF, HEX);
1680 prefix = (prefixes >> 16) & 0xFF;
1681 Serial.println(prefix, HEX);
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");
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);
1704 for (
int i = 0; i < 6; i++) {
1705 enAA |= acksPerPipe[i] << i;
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= ");
1720 Serial.println(
"16 bits");
1723 Serial.println(
"8 bits");
1726 Serial.println(
"Disabled");
1728 Serial.print(
"PA Power\t= ");
1731 Serial.println(
"PA_MAX");
1734 Serial.println(
"PA_HIGH");
1737 Serial.println(
"PA_LOW");
1740 Serial.println(
"PA_MIN");
1743 Serial.println(
"?");
1745 Serial.print(
"ARC\t\t= ");
1746 Serial.println(arcCounter);
1751#if defined CCM_ENCRYPTION_ENABLED
1753 #if defined NRF54_CCM_ENCRYPTION
1768 if (!nrf54Ccm.encryptBlePacket(ccmData.key, ccmData.iv, ccmData.counter, ccmData.direction, 0,
static_cast<const uint8_t*
>(bufferIn), size,
1787 bool macValid =
false;
1788 if (!nrf54Ccm.decryptBlePacket(ccmData.key, ccmData.iv, ccmData.counter, ccmData.direction, 0,
static_cast<const uint8_t*
>(bufferIn), size,
1811 NRF_CCM->MODE = 0 | 1 << 24 | 1 << 16;
1827 NRF_CCM->EVENTS_ENDKSGEN = 0;
1828 NRF_CCM->EVENTS_ENDCRYPT = 0;
1829 NRF_CCM->TASKS_KSGEN = 1;
1833 if (NRF_CCM->EVENTS_ERROR) {
1843 NRF_CCM->MODE = 1 | 1 << 24 | 1 << 16;
1860 NRF_CCM->EVENTS_ENDKSGEN = 0;
1861 NRF_CCM->EVENTS_ENDCRYPT = 0;
1862 NRF_CCM->TASKS_KSGEN = 1;
1867 if (NRF_CCM->EVENTS_ERROR) {
1871 if (NRF_CCM->MICSTATUS == (CCM_MICSTATUS_MICSTATUS_CheckFailed << CCM_MICSTATUS_MICSTATUS_Pos)) {
1883 NRF_CCM->MODE = 1 << 24 | 1 << 16;
1884#ifndef NRF52832_XXAA
1887 NRF_CCM->SHORTS = 1;
1888 NRF_CCM->ENABLE = 2;
1890 NRF_RNG->CONFIG = 1;
1891 NRF_RNG->TASKS_START = 1;
1903 ccmData.counter = counter;
1904 packetCounter = counter;
1912 ccmData.iv[i] = IV[i];
1918bool nrf_to_nrf::generateIV(uint8_t iv[
CCM_IV_SIZE])
1920 #if defined NRF54_CCM_ENCRYPTION
1927 NRF_RNG->EVENTS_VALRDY = 0;
1928 iv[i] = NRF_RNG->VALUE;
Driver for the 2.4GHz radio in nRF52x and nRF54x chips, using the RF24 API.
void startListening(bool resetAddresses=true)
void openReadingPipe(uint8_t child, const uint8_t *address)
void setPALevel(uint8_t level, bool lnaEnable=true)
void disableRxInterrupt()
bool testCarrier(uint8_t RSSI=65)
uint8_t decrypt(void *bufferIn, uint8_t size)
uint8_t radioData[ACTUAL_MAX_PAYLOAD_SIZE+2]
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)
void setAddressWidth(uint8_t a_width)
void setChannel(uint8_t channel, bool map=0)
uint8_t getDynamicPayloadSize()
bool setDataRate(uint8_t speed)
void setKey(uint8_t key[CCM_KEY_SIZE])
bool writeAckPayload(uint8_t pipe, void *buf, uint8_t len)
void enableDynamicPayloads(uint8_t payloadSize=DEFAULT_MAX_PAYLOAD_SIZE)
uint8_t encrypt(void *bufferIn, uint8_t size)
bool startWrite(void *buf, uint8_t len, bool multicast, bool doEncryption=true)
uint16_t interframeSpacing
uint8_t outBuffer[MAX_PACKET_SIZE+CCM_MIC_SIZE+CCM_START_SIZE]
void setRetries(uint8_t retryVar, uint8_t attempts)
void openWritingPipe(const uint8_t *address)
void setCRCLength(nrf_crclength_e length)
bool writeFast(void *buf, uint8_t len, bool multicast=0)
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()
#define RADIO_MODE_MODE_Nrf_250Kbit
static nrf_to_nrf * rxIrqRadio
static bool waitForEvent(volatile uint32_t *event, uint32_t timeout=DEFAULT_TIMEOUT)
__attribute__((__used__)) void RX_IRQ_HANDLER(void)
static void rxIrqDisable()
static uint32_t bytewise_bit_swap(uint8_t const *p_inp)
static uint32_t addr_conv(uint8_t const *p_addr)
#define ACK_TIMEOUT_1MBPS_OFFSET
#define DEFAULT_MAX_PAYLOAD_SIZE
#define ACK_PAYLOAD_TIMEOUT_OFFSET
#define ACK_TIMEOUT_250KBPS_OFFSET
#define ACK_TIMEOUT_250KBPS
#define ACK_TIMEOUT_2MBPS
#define ACTUAL_MAX_PAYLOAD_SIZE
#define ACK_TIMEOUT_2MBPS_OFFSET
#define ACK_TIMEOUT_1MBPS