nrf_esb.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  1. /**
  2. * Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "nrf_error.h"
  41. #include "nrf_esb.h"
  42. #include "nrf_esb_error_codes.h"
  43. #include "nrf_gpio.h"
  44. #include <string.h>
  45. #include <stddef.h>
  46. #include "sdk_common.h"
  47. #include "sdk_macros.h"
  48. #include "app_util.h"
  49. #include "nrf_delay.h"
  50. #define BIT_MASK_UINT_8(x) (0xFF >> (8 - (x)))
  51. // Constant parameters
  52. #define RX_WAIT_FOR_ACK_TIMEOUT_US_2MBPS (48) /**< 2 Mb RX wait for acknowledgment time-out value. Smallest reliable value - 43. */
  53. #define RX_WAIT_FOR_ACK_TIMEOUT_US_1MBPS (73) /**< 1 Mb RX wait for acknowledgment time-out value. Smallest reliable value - 68. */
  54. #define RX_WAIT_FOR_ACK_TIMEOUT_US_250KBPS (250) /**< 250 Kb RX wait for acknowledgment time-out value. */
  55. #define RX_WAIT_FOR_ACK_TIMEOUT_US_1MBPS_BLE (73) /**< 1 Mb RX wait for acknowledgment time-out (combined with BLE). Smallest reliable value - 68.*/
  56. #define RETRANSMIT_DELAY_US_OFFSET (62) /**< Never retransmit before the wait for ack time plus this offset. */
  57. // Interrupt flags
  58. #define NRF_ESB_INT_TX_SUCCESS_MSK 0x01 /**< Interrupt mask value for TX success. */
  59. #define NRF_ESB_INT_TX_FAILED_MSK 0x02 /**< Interrupt mask value for TX failure. */
  60. #define NRF_ESB_INT_RX_DATA_RECEIVED_MSK 0x04 /**< Interrupt mask value for RX_DR. */
  61. #define NRF_ESB_PID_RESET_VALUE 0xFF /**< Invalid PID value which is guaranteed to not collide with any valid PID value. */
  62. #define NRF_ESB_PID_MAX 3 /**< Maximum value for PID. */
  63. #define NRF_ESB_CRC_RESET_VALUE 0xFFFF /**< CRC reset value. */
  64. // Internal Enhanced ShockBurst module state.
  65. typedef enum {
  66. NRF_ESB_STATE_IDLE, /**< Module idle. */
  67. NRF_ESB_STATE_PTX_TX, /**< Module transmitting without acknowledgment. */
  68. NRF_ESB_STATE_PTX_TX_ACK, /**< Module transmitting with acknowledgment. */
  69. NRF_ESB_STATE_PTX_RX_ACK, /**< Module transmitting with acknowledgment and reception of payload with the acknowledgment response. */
  70. NRF_ESB_STATE_PRX, /**< Module receiving packets without acknowledgment. */
  71. NRF_ESB_STATE_PRX_SEND_ACK, /**< Module transmitting acknowledgment in RX mode. */
  72. } nrf_esb_mainstate_t;
  73. #define DISABLE_RF_IRQ() NVIC_DisableIRQ(RADIO_IRQn)
  74. #define ENABLE_RF_IRQ() NVIC_EnableIRQ(RADIO_IRQn)
  75. #define _RADIO_SHORTS_COMMON ( RADIO_SHORTS_READY_START_Msk | RADIO_SHORTS_END_DISABLE_Msk | \
  76. RADIO_SHORTS_ADDRESS_RSSISTART_Msk | RADIO_SHORTS_DISABLED_RSSISTOP_Msk )
  77. #define VERIFY_PAYLOAD_LENGTH(p) \
  78. do \
  79. { \
  80. if (p->length == 0 || \
  81. p->length > NRF_ESB_MAX_PAYLOAD_LENGTH || \
  82. (m_config_local.protocol == NRF_ESB_PROTOCOL_ESB && \
  83. p->length > m_config_local.payload_length)) \
  84. { \
  85. return NRF_ERROR_INVALID_LENGTH; \
  86. } \
  87. }while (0)
  88. /* @brief Structure holding pipe info PID and CRC and acknowledgment payload. */
  89. typedef struct
  90. {
  91. uint16_t crc; /**< CRC value of the last received packet (Used to detect retransmits). */
  92. uint8_t pid; /**< Packet ID of the last received packet (Used to detect retransmits). */
  93. bool ack_payload; /**< Flag indicating the state of the transmission of acknowledgment payloads. */
  94. } pipe_info_t;
  95. /* @brief Structure used by the PRX to organize ACK payloads for multiple pipes. */
  96. typedef struct
  97. {
  98. nrf_esb_payload_t * p_payload; /**< Pointer to the ACK payload. */
  99. bool in_use; /**< Value used to determine if the current payload pointer is used. */
  100. struct nrf_esb_payload_random_access_buf_wrapper_t * p_next; /**< Pointer to the next ACK payload queued on the same pipe. */
  101. } nrf_esb_payload_random_access_buf_wrapper_t;
  102. /* @brief First-in, first-out queue of payloads to be transmitted. */
  103. typedef struct
  104. {
  105. nrf_esb_payload_t * p_payload[NRF_ESB_TX_FIFO_SIZE]; /**< Pointer to the actual queue. */
  106. uint32_t entry_point; /**< Current start of queue. */
  107. uint32_t exit_point; /**< Current end of queue. */
  108. uint32_t count; /**< Current number of elements in the queue. */
  109. } nrf_esb_payload_tx_fifo_t;
  110. /* @brief First-in, first-out queue of received payloads. */
  111. typedef struct
  112. {
  113. nrf_esb_payload_t * p_payload[NRF_ESB_RX_FIFO_SIZE]; /**< Pointer to the actual queue. */
  114. uint32_t entry_point; /**< Current start of queue. */
  115. uint32_t exit_point; /**< Current end of queue. */
  116. uint32_t count; /**< Current number of elements in the queue. */
  117. } nrf_esb_payload_rx_fifo_t;
  118. /**@brief Enhanced ShockBurst address.
  119. *
  120. * Enhanced ShockBurst addresses consist of a base address and a prefix
  121. * that is unique for each pipe. See @ref esb_addressing in the ESB user
  122. * guide for more information.
  123. */
  124. typedef struct
  125. {
  126. uint8_t base_addr_p0[4]; /**< Base address for pipe 0 encoded in big endian. */
  127. uint8_t base_addr_p1[4]; /**< Base address for pipe 1-7 encoded in big endian. */
  128. uint8_t pipe_prefixes[8]; /**< Address prefix for pipe 0 to 7. */
  129. uint8_t num_pipes; /**< Number of pipes available. */
  130. uint8_t addr_length; /**< Length of the address including the prefix. */
  131. uint8_t rx_pipes_enabled; /**< Bitfield for enabled pipes. */
  132. uint8_t rf_channel; /**< Channel to use (must be between 0 and 100). */
  133. } nrf_esb_address_t;
  134. // Module state
  135. static bool m_esb_initialized = false;
  136. static volatile nrf_esb_mainstate_t m_nrf_esb_mainstate = NRF_ESB_STATE_IDLE;
  137. static nrf_esb_payload_t * mp_current_payload;
  138. static nrf_esb_event_handler_t m_event_handler;
  139. // Address parameters
  140. __ALIGN(4) static nrf_esb_address_t m_esb_addr = NRF_ESB_ADDR_DEFAULT;
  141. // RF parameters
  142. static nrf_esb_config_t m_config_local;
  143. // TX FIFO
  144. static nrf_esb_payload_t m_tx_fifo_payload[NRF_ESB_TX_FIFO_SIZE];
  145. static nrf_esb_payload_tx_fifo_t m_tx_fifo;
  146. // RX FIFO
  147. static nrf_esb_payload_t m_rx_fifo_payload[NRF_ESB_RX_FIFO_SIZE];
  148. static nrf_esb_payload_rx_fifo_t m_rx_fifo;
  149. // Payload buffers
  150. static uint8_t m_tx_payload_buffer[NRF_ESB_MAX_PAYLOAD_LENGTH + 2];
  151. static uint8_t m_rx_payload_buffer[NRF_ESB_MAX_PAYLOAD_LENGTH + 2];
  152. // Random access buffer variables for better ACK payload handling
  153. nrf_esb_payload_random_access_buf_wrapper_t m_ack_pl_container[NRF_ESB_TX_FIFO_SIZE];
  154. nrf_esb_payload_random_access_buf_wrapper_t * m_ack_pl_container_entry_point_pr_pipe[NRF_ESB_PIPE_COUNT];
  155. // Run time variables
  156. static volatile uint32_t m_interrupt_flags = 0;
  157. static uint8_t m_pids[NRF_ESB_PIPE_COUNT];
  158. static pipe_info_t m_rx_pipe_info[NRF_ESB_PIPE_COUNT];
  159. static volatile uint32_t m_retransmits_remaining;
  160. static volatile uint32_t m_last_tx_attempts;
  161. static volatile uint32_t m_wait_for_ack_timeout_us;
  162. static volatile uint32_t m_radio_shorts_common = _RADIO_SHORTS_COMMON;
  163. // These function pointers are changed dynamically, depending on protocol configuration and state.
  164. static void (*on_radio_disabled)(void) = 0;
  165. static void (*on_radio_end)(void) = 0;
  166. static void (*update_rf_payload_format)(uint32_t payload_length) = 0;
  167. // The following functions are assigned to the function pointers above.
  168. static void on_radio_disabled_tx_noack(void);
  169. static void on_radio_disabled_tx(void);
  170. static void on_radio_disabled_tx_wait_for_ack(void);
  171. static void on_radio_disabled_rx(void);
  172. static void on_radio_disabled_rx_ack(void);
  173. #define NRF_ESB_ADDR_UPDATE_MASK_BASE0 (1 << 0) /*< Mask value to signal updating BASE0 radio address. */
  174. #define NRF_ESB_ADDR_UPDATE_MASK_BASE1 (1 << 1) /*< Mask value to signal updating BASE1 radio address. */
  175. #define NRF_ESB_ADDR_UPDATE_MASK_PREFIX (1 << 2) /*< Mask value to signal updating radio prefixes. */
  176. // Function to do bytewise bit-swap on an unsigned 32-bit value
  177. static uint32_t bytewise_bit_swap(uint8_t const * p_inp)
  178. {
  179. #if __CORTEX_M == (0x04U)
  180. uint32_t inp = (*(uint32_t*)p_inp);
  181. return __REV((uint32_t)__RBIT(inp)); //lint -esym(628, __rev) -esym(526, __rev) -esym(628, __rbit) -esym(526, __rbit) */
  182. #else
  183. uint32_t inp = (p_inp[3] << 24) | (p_inp[2] << 16) | (p_inp[1] << 8) | (p_inp[0]);
  184. inp = (inp & 0xF0F0F0F0) >> 4 | (inp & 0x0F0F0F0F) << 4;
  185. inp = (inp & 0xCCCCCCCC) >> 2 | (inp & 0x33333333) << 2;
  186. inp = (inp & 0xAAAAAAAA) >> 1 | (inp & 0x55555555) << 1;
  187. return inp;
  188. #endif
  189. }
  190. // Convert a base address from nRF24L format to nRF5 format
  191. static uint32_t addr_conv(uint8_t const* p_addr)
  192. {
  193. return __REV(bytewise_bit_swap(p_addr)); //lint -esym(628, __rev) -esym(526, __rev) */
  194. }
  195. #ifdef NRF52832_XXAA
  196. static ret_code_t apply_address_workarounds()
  197. {
  198. if ((NRF_FICR->INFO.VARIANT & 0x0000FF00) == 0x00004200) //Check if the device is an nRF52832 Rev. 1.
  199. {
  200. // Workaround for nRF52832 Rev 1 erratas
  201. // Set up radio parameters.
  202. NRF_RADIO->MODECNF0 = (NRF_RADIO->MODECNF0 & ~RADIO_MODECNF0_RU_Msk) | RADIO_MODECNF0_RU_Default << RADIO_MODECNF0_RU_Pos;
  203. // Workaround for nRF52832 Rev 1 Errata 102 and nRF52832 Rev 1 Errata 106. This will reduce sensitivity by 3dB.
  204. *((volatile uint32_t *)0x40001774) = (*((volatile uint32_t *)0x40001774) & 0xFFFFFFFE) | 0x01000000;
  205. }
  206. if ((NRF_FICR->INFO.VARIANT & 0x0000FF00) == 0x00004500)//Check if the device is an nRF52832 Rev. 2.
  207. {
  208. /*
  209. Workaround for nRF52832 Rev 2 Errata 143
  210. Check if the most significant bytes of address 0 (including prefix) match those of another address.
  211. It's recommended to use a unique address 0 since this will avoid the 3dBm penalty incurred from the workaround.
  212. */
  213. uint32_t base_address_mask = m_esb_addr.addr_length == 5 ? 0xFFFF0000 : 0xFF000000;
  214. // Load the two addresses before comparing them to ensure defined ordering of volatile accesses.
  215. uint32_t addr0 = NRF_RADIO->BASE0 & base_address_mask;
  216. uint32_t addr1 = NRF_RADIO->BASE1 & base_address_mask;
  217. if (addr0 == addr1)
  218. {
  219. uint32_t prefix0 = NRF_RADIO->PREFIX0 & 0x000000FF;
  220. uint32_t prefix1 = (NRF_RADIO->PREFIX0 & 0x0000FF00) >> 8;
  221. uint32_t prefix2 = (NRF_RADIO->PREFIX0 & 0x00FF0000) >> 16;
  222. uint32_t prefix3 = (NRF_RADIO->PREFIX0 & 0xFF000000) >> 24;
  223. uint32_t prefix4 = NRF_RADIO->PREFIX1 & 0x000000FF;
  224. uint32_t prefix5 = (NRF_RADIO->PREFIX1 & 0x0000FF00) >> 8;
  225. uint32_t prefix6 = (NRF_RADIO->PREFIX1 & 0x00FF0000) >> 16;
  226. uint32_t prefix7 = (NRF_RADIO->PREFIX1 & 0xFF000000) >> 24;
  227. if (prefix0 == prefix1 || prefix0 == prefix2 || prefix0 == prefix3 || prefix0 == prefix4 ||
  228. prefix0 == prefix5 || prefix0 == prefix6 || prefix0 == prefix7)
  229. {
  230. // This will cause a 3dBm sensitivity loss, avoid using such address combinations if possible.
  231. *(volatile uint32_t *) 0x40001774 = ((*(volatile uint32_t *) 0x40001774) & 0xfffffffe) | 0x01000000;
  232. }
  233. }
  234. }
  235. return NRF_SUCCESS;
  236. }
  237. #endif
  238. static void update_rf_payload_format_esb_dpl(uint32_t payload_length)
  239. {
  240. #if (NRF_ESB_MAX_PAYLOAD_LENGTH <= 32)
  241. // Using 6 bits for length
  242. NRF_RADIO->PCNF0 = (0 << RADIO_PCNF0_S0LEN_Pos) |
  243. (6 << RADIO_PCNF0_LFLEN_Pos) |
  244. (3 << RADIO_PCNF0_S1LEN_Pos) ;
  245. #else
  246. // Using 8 bits for length
  247. NRF_RADIO->PCNF0 = (0 << RADIO_PCNF0_S0LEN_Pos) |
  248. (8 << RADIO_PCNF0_LFLEN_Pos) |
  249. (3 << RADIO_PCNF0_S1LEN_Pos) ;
  250. #endif
  251. NRF_RADIO->PCNF1 = (RADIO_PCNF1_WHITEEN_Disabled << RADIO_PCNF1_WHITEEN_Pos) |
  252. (RADIO_PCNF1_ENDIAN_Big << RADIO_PCNF1_ENDIAN_Pos) |
  253. ((m_esb_addr.addr_length - 1) << RADIO_PCNF1_BALEN_Pos) |
  254. (0 << RADIO_PCNF1_STATLEN_Pos) |
  255. (NRF_ESB_MAX_PAYLOAD_LENGTH << RADIO_PCNF1_MAXLEN_Pos);
  256. }
  257. static void update_rf_payload_format_esb(uint32_t payload_length)
  258. {
  259. NRF_RADIO->PCNF0 = (1 << RADIO_PCNF0_S0LEN_Pos) |
  260. (0 << RADIO_PCNF0_LFLEN_Pos) |
  261. (1 << RADIO_PCNF0_S1LEN_Pos);
  262. NRF_RADIO->PCNF1 = (RADIO_PCNF1_WHITEEN_Disabled << RADIO_PCNF1_WHITEEN_Pos) |
  263. (RADIO_PCNF1_ENDIAN_Big << RADIO_PCNF1_ENDIAN_Pos) |
  264. ((m_esb_addr.addr_length - 1) << RADIO_PCNF1_BALEN_Pos) |
  265. (payload_length << RADIO_PCNF1_STATLEN_Pos) |
  266. (payload_length << RADIO_PCNF1_MAXLEN_Pos);
  267. }
  268. static void update_radio_addresses(uint8_t update_mask)
  269. {
  270. if ((update_mask & NRF_ESB_ADDR_UPDATE_MASK_BASE0) != 0)
  271. {
  272. NRF_RADIO->BASE0 = addr_conv(m_esb_addr.base_addr_p0);
  273. }
  274. if ((update_mask & NRF_ESB_ADDR_UPDATE_MASK_BASE1) != 0)
  275. {
  276. NRF_RADIO->BASE1 = addr_conv(m_esb_addr.base_addr_p1);
  277. }
  278. if ((update_mask & NRF_ESB_ADDR_UPDATE_MASK_PREFIX) != 0)
  279. {
  280. NRF_RADIO->PREFIX0 = bytewise_bit_swap(&m_esb_addr.pipe_prefixes[0]);
  281. NRF_RADIO->PREFIX1 = bytewise_bit_swap(&m_esb_addr.pipe_prefixes[4]);
  282. }
  283. }
  284. static void update_radio_tx_power()
  285. {
  286. NRF_RADIO->TXPOWER = m_config_local.tx_output_power << RADIO_TXPOWER_TXPOWER_Pos;
  287. }
  288. static bool update_radio_bitrate()
  289. {
  290. NRF_RADIO->MODE = m_config_local.bitrate << RADIO_MODE_MODE_Pos;
  291. switch (m_config_local.bitrate)
  292. {
  293. case NRF_ESB_BITRATE_2MBPS:
  294. #ifdef RADIO_MODE_MODE_Ble_2Mbit
  295. case NRF_ESB_BITRATE_2MBPS_BLE:
  296. #endif
  297. m_wait_for_ack_timeout_us = RX_WAIT_FOR_ACK_TIMEOUT_US_2MBPS;
  298. break;
  299. case NRF_ESB_BITRATE_1MBPS:
  300. m_wait_for_ack_timeout_us = RX_WAIT_FOR_ACK_TIMEOUT_US_1MBPS;
  301. break;
  302. #ifdef RADIO_MODE_MODE_Nrf_250Kbit
  303. case NRF_ESB_BITRATE_250KBPS:
  304. m_wait_for_ack_timeout_us = RX_WAIT_FOR_ACK_TIMEOUT_US_250KBPS;
  305. break;
  306. #endif
  307. case NRF_ESB_BITRATE_1MBPS_BLE:
  308. m_wait_for_ack_timeout_us = RX_WAIT_FOR_ACK_TIMEOUT_US_1MBPS_BLE;
  309. break;
  310. default:
  311. // Should not be reached
  312. return false;
  313. }
  314. // Ensure that we do not attempt retransmitting before ack timeout.
  315. if (m_config_local.retransmit_delay < m_wait_for_ack_timeout_us + RETRANSMIT_DELAY_US_OFFSET)
  316. {
  317. m_config_local.retransmit_delay = m_wait_for_ack_timeout_us + RETRANSMIT_DELAY_US_OFFSET;
  318. }
  319. return true;
  320. }
  321. static bool update_radio_protocol()
  322. {
  323. switch (m_config_local.protocol)
  324. {
  325. case NRF_ESB_PROTOCOL_ESB_DPL:
  326. update_rf_payload_format = update_rf_payload_format_esb_dpl;
  327. break;
  328. case NRF_ESB_PROTOCOL_ESB:
  329. update_rf_payload_format = update_rf_payload_format_esb;
  330. break;
  331. default:
  332. // Should not be reached
  333. return false;
  334. }
  335. return true;
  336. }
  337. static bool update_radio_crc()
  338. {
  339. switch(m_config_local.crc)
  340. {
  341. case NRF_ESB_CRC_16BIT:
  342. NRF_RADIO->CRCINIT = 0xFFFFUL; // Initial value
  343. NRF_RADIO->CRCPOLY = 0x11021UL; // CRC poly: x^16+x^12^x^5+1
  344. break;
  345. case NRF_ESB_CRC_8BIT:
  346. NRF_RADIO->CRCINIT = 0xFFUL; // Initial value
  347. NRF_RADIO->CRCPOLY = 0x107UL; // CRC poly: x^8+x^2^x^1+1
  348. break;
  349. case NRF_ESB_CRC_OFF:
  350. NRF_RADIO->CRCINIT = 0x00UL;
  351. NRF_RADIO->CRCPOLY = 0x00UL;
  352. break;
  353. default:
  354. return false;
  355. }
  356. NRF_RADIO->CRCCNF = m_config_local.crc << RADIO_CRCCNF_LEN_Pos;
  357. return true;
  358. }
  359. static bool update_radio_parameters()
  360. {
  361. bool params_valid = true;
  362. update_radio_tx_power();
  363. params_valid &= update_radio_bitrate();
  364. params_valid &= update_radio_protocol();
  365. params_valid &= update_radio_crc();
  366. update_rf_payload_format(m_config_local.payload_length);
  367. return params_valid;
  368. }
  369. static void reset_fifos()
  370. {
  371. m_tx_fifo.entry_point = 0;
  372. m_tx_fifo.exit_point = 0;
  373. m_tx_fifo.count = 0;
  374. m_rx_fifo.entry_point = 0;
  375. m_rx_fifo.exit_point = 0;
  376. m_rx_fifo.count = 0;
  377. }
  378. static void initialize_fifos()
  379. {
  380. reset_fifos();
  381. for (int i = 0; i < NRF_ESB_TX_FIFO_SIZE; i++)
  382. {
  383. m_tx_fifo.p_payload[i] = &m_tx_fifo_payload[i];
  384. }
  385. for (int i = 0; i < NRF_ESB_RX_FIFO_SIZE; i++)
  386. {
  387. m_rx_fifo.p_payload[i] = &m_rx_fifo_payload[i];
  388. }
  389. for (int i = 0; i < NRF_ESB_TX_FIFO_SIZE; i++)
  390. {
  391. m_ack_pl_container[i].p_payload = &m_tx_fifo_payload[i];
  392. m_ack_pl_container[i].in_use = false;
  393. m_ack_pl_container[i].p_next = 0;
  394. }
  395. for (int i = 0; i < NRF_ESB_PIPE_COUNT; i++)
  396. {
  397. m_ack_pl_container_entry_point_pr_pipe[i] = 0;
  398. }
  399. }
  400. uint32_t nrf_esb_skip_tx()
  401. {
  402. VERIFY_TRUE(m_esb_initialized, NRF_ERROR_INVALID_STATE);
  403. VERIFY_TRUE(m_tx_fifo.count > 0, NRF_ERROR_BUFFER_EMPTY);
  404. DISABLE_RF_IRQ();
  405. m_tx_fifo.count--;
  406. if (++m_tx_fifo.exit_point >= NRF_ESB_TX_FIFO_SIZE)
  407. {
  408. m_tx_fifo.exit_point = 0;
  409. }
  410. ENABLE_RF_IRQ();
  411. return NRF_SUCCESS;
  412. }
  413. /** @brief Function to push the content of the rx_buffer to the RX FIFO.
  414. *
  415. * The module will point the register NRF_RADIO->PACKETPTR to a buffer for receiving packets.
  416. * After receiving a packet the module will call this function to copy the received data to
  417. * the RX FIFO.
  418. *
  419. * @param pipe Pipe number to set for the packet.
  420. * @param pid Packet ID.
  421. *
  422. * @retval true Operation successful.
  423. * @retval false Operation failed.
  424. */
  425. static bool rx_fifo_push_rfbuf(uint8_t pipe, uint8_t pid)
  426. {
  427. if (m_rx_fifo.count < NRF_ESB_RX_FIFO_SIZE)
  428. {
  429. if (m_config_local.protocol == NRF_ESB_PROTOCOL_ESB_DPL)
  430. {
  431. if (m_rx_payload_buffer[0] > NRF_ESB_MAX_PAYLOAD_LENGTH)
  432. {
  433. return false;
  434. }
  435. m_rx_fifo.p_payload[m_rx_fifo.entry_point]->length = m_rx_payload_buffer[0];
  436. }
  437. else if (m_config_local.mode == NRF_ESB_MODE_PTX)
  438. {
  439. // Received packet is an acknowledgment
  440. m_rx_fifo.p_payload[m_rx_fifo.entry_point]->length = 0;
  441. }
  442. else
  443. {
  444. m_rx_fifo.p_payload[m_rx_fifo.entry_point]->length = m_config_local.payload_length;
  445. }
  446. memcpy(m_rx_fifo.p_payload[m_rx_fifo.entry_point]->data, &m_rx_payload_buffer[2],
  447. m_rx_fifo.p_payload[m_rx_fifo.entry_point]->length);
  448. m_rx_fifo.p_payload[m_rx_fifo.entry_point]->pipe = pipe;
  449. m_rx_fifo.p_payload[m_rx_fifo.entry_point]->rssi = NRF_RADIO->RSSISAMPLE;
  450. m_rx_fifo.p_payload[m_rx_fifo.entry_point]->pid = pid;
  451. m_rx_fifo.p_payload[m_rx_fifo.entry_point]->noack = !(m_rx_payload_buffer[1] & 0x01);
  452. if (++m_rx_fifo.entry_point >= NRF_ESB_RX_FIFO_SIZE)
  453. {
  454. m_rx_fifo.entry_point = 0;
  455. }
  456. m_rx_fifo.count++;
  457. return true;
  458. }
  459. return false;
  460. }
  461. static void sys_timer_init()
  462. {
  463. // Configure the system timer with a 1 MHz base frequency
  464. NRF_ESB_SYS_TIMER->PRESCALER = 4;
  465. NRF_ESB_SYS_TIMER->BITMODE = TIMER_BITMODE_BITMODE_16Bit;
  466. NRF_ESB_SYS_TIMER->SHORTS = TIMER_SHORTS_COMPARE1_CLEAR_Msk | TIMER_SHORTS_COMPARE1_STOP_Msk;
  467. }
  468. static void ppi_init()
  469. {
  470. NRF_PPI->CH[NRF_ESB_PPI_TIMER_START].EEP = (uint32_t)&NRF_RADIO->EVENTS_READY;
  471. NRF_PPI->CH[NRF_ESB_PPI_TIMER_START].TEP = (uint32_t)&NRF_ESB_SYS_TIMER->TASKS_START;
  472. NRF_PPI->CH[NRF_ESB_PPI_TIMER_STOP].EEP = (uint32_t)&NRF_RADIO->EVENTS_ADDRESS;
  473. NRF_PPI->CH[NRF_ESB_PPI_TIMER_STOP].TEP = (uint32_t)&NRF_ESB_SYS_TIMER->TASKS_SHUTDOWN;
  474. NRF_PPI->CH[NRF_ESB_PPI_RX_TIMEOUT].EEP = (uint32_t)&NRF_ESB_SYS_TIMER->EVENTS_COMPARE[0];
  475. NRF_PPI->CH[NRF_ESB_PPI_RX_TIMEOUT].TEP = (uint32_t)&NRF_RADIO->TASKS_DISABLE;
  476. NRF_PPI->CH[NRF_ESB_PPI_TX_START].EEP = (uint32_t)&NRF_ESB_SYS_TIMER->EVENTS_COMPARE[1];
  477. NRF_PPI->CH[NRF_ESB_PPI_TX_START].TEP = (uint32_t)&NRF_RADIO->TASKS_TXEN;
  478. }
  479. static void start_tx_transaction()
  480. {
  481. bool ack;
  482. m_last_tx_attempts = 1;
  483. // Prepare the payload
  484. mp_current_payload = m_tx_fifo.p_payload[m_tx_fifo.exit_point];
  485. switch (m_config_local.protocol)
  486. {
  487. case NRF_ESB_PROTOCOL_ESB:
  488. update_rf_payload_format(mp_current_payload->length);
  489. m_tx_payload_buffer[0] = mp_current_payload->pid;
  490. m_tx_payload_buffer[1] = 0;
  491. memcpy(&m_tx_payload_buffer[2], mp_current_payload->data, mp_current_payload->length);
  492. NRF_RADIO->SHORTS = m_radio_shorts_common | RADIO_SHORTS_DISABLED_RXEN_Msk;
  493. NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk | RADIO_INTENSET_READY_Msk;
  494. // Configure the retransmit counter
  495. m_retransmits_remaining = m_config_local.retransmit_count;
  496. on_radio_disabled = on_radio_disabled_tx;
  497. m_nrf_esb_mainstate = NRF_ESB_STATE_PTX_TX_ACK;
  498. break;
  499. case NRF_ESB_PROTOCOL_ESB_DPL:
  500. ack = !mp_current_payload->noack || !m_config_local.selective_auto_ack;
  501. m_tx_payload_buffer[0] = mp_current_payload->length;
  502. m_tx_payload_buffer[1] = mp_current_payload->pid << 1;
  503. m_tx_payload_buffer[1] |= mp_current_payload->noack ? 0x00 : 0x01;
  504. memcpy(&m_tx_payload_buffer[2], mp_current_payload->data, mp_current_payload->length);
  505. // Handling ack if noack is set to false or if selective auto ack is turned off
  506. if (ack)
  507. {
  508. NRF_RADIO->SHORTS = m_radio_shorts_common | RADIO_SHORTS_DISABLED_RXEN_Msk;
  509. NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk | RADIO_INTENSET_READY_Msk;
  510. // Configure the retransmit counter
  511. m_retransmits_remaining = m_config_local.retransmit_count;
  512. on_radio_disabled = on_radio_disabled_tx;
  513. m_nrf_esb_mainstate = NRF_ESB_STATE_PTX_TX_ACK;
  514. }
  515. else
  516. {
  517. NRF_RADIO->SHORTS = m_radio_shorts_common;
  518. NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk;
  519. on_radio_disabled = on_radio_disabled_tx_noack;
  520. m_nrf_esb_mainstate = NRF_ESB_STATE_PTX_TX;
  521. }
  522. break;
  523. default:
  524. // Should not be reached
  525. break;
  526. }
  527. NRF_RADIO->TXADDRESS = mp_current_payload->pipe;
  528. NRF_RADIO->RXADDRESSES = 1 << mp_current_payload->pipe;
  529. NRF_RADIO->FREQUENCY = m_esb_addr.rf_channel;
  530. NRF_RADIO->PACKETPTR = (uint32_t)m_tx_payload_buffer;
  531. NVIC_ClearPendingIRQ(RADIO_IRQn);
  532. NVIC_EnableIRQ(RADIO_IRQn);
  533. NRF_RADIO->EVENTS_ADDRESS = 0;
  534. NRF_RADIO->EVENTS_PAYLOAD = 0;
  535. NRF_RADIO->EVENTS_DISABLED = 0;
  536. DEBUG_PIN_SET(DEBUGPIN4);
  537. NRF_RADIO->TASKS_TXEN = 1;
  538. }
  539. static void on_radio_disabled_tx_noack()
  540. {
  541. m_interrupt_flags |= NRF_ESB_INT_TX_SUCCESS_MSK;
  542. (void) nrf_esb_skip_tx();
  543. if (m_tx_fifo.count == 0)
  544. {
  545. m_nrf_esb_mainstate = NRF_ESB_STATE_IDLE;
  546. NVIC_SetPendingIRQ(ESB_EVT_IRQ);
  547. }
  548. else
  549. {
  550. NVIC_SetPendingIRQ(ESB_EVT_IRQ);
  551. start_tx_transaction();
  552. }
  553. }
  554. static void on_radio_disabled_tx()
  555. {
  556. // Remove the DISABLED -> RXEN shortcut, to make sure the radio stays
  557. // disabled after the RX window
  558. NRF_RADIO->SHORTS = m_radio_shorts_common;
  559. // Make sure the timer is started the next time the radio is ready,
  560. // and that it will disable the radio automatically if no packet is
  561. // received by the time defined in m_wait_for_ack_timeout_us
  562. NRF_ESB_SYS_TIMER->CC[0] = m_wait_for_ack_timeout_us;
  563. NRF_ESB_SYS_TIMER->CC[1] = m_config_local.retransmit_delay - 130;
  564. NRF_ESB_SYS_TIMER->TASKS_CLEAR = 1;
  565. NRF_ESB_SYS_TIMER->EVENTS_COMPARE[0] = 0;
  566. NRF_ESB_SYS_TIMER->EVENTS_COMPARE[1] = 0;
  567. NRF_PPI->CHENSET = (1 << NRF_ESB_PPI_TIMER_START) |
  568. (1 << NRF_ESB_PPI_RX_TIMEOUT) |
  569. (1 << NRF_ESB_PPI_TIMER_STOP);
  570. NRF_PPI->CHENCLR = (1 << NRF_ESB_PPI_TX_START);
  571. NRF_RADIO->EVENTS_END = 0;
  572. if (m_config_local.protocol == NRF_ESB_PROTOCOL_ESB)
  573. {
  574. update_rf_payload_format(0);
  575. }
  576. NRF_RADIO->PACKETPTR = (uint32_t)m_rx_payload_buffer;
  577. on_radio_disabled = on_radio_disabled_tx_wait_for_ack;
  578. m_nrf_esb_mainstate = NRF_ESB_STATE_PTX_RX_ACK;
  579. }
  580. static void on_radio_disabled_tx_wait_for_ack()
  581. {
  582. // This marks the completion of a TX_RX sequence (TX with ACK)
  583. // Make sure the timer will not deactivate the radio if a packet is received
  584. NRF_PPI->CHENCLR = (1 << NRF_ESB_PPI_TIMER_START) |
  585. (1 << NRF_ESB_PPI_RX_TIMEOUT) |
  586. (1 << NRF_ESB_PPI_TIMER_STOP);
  587. // If the radio has received a packet and the CRC status is OK
  588. if (NRF_RADIO->EVENTS_END && NRF_RADIO->CRCSTATUS != 0)
  589. {
  590. NRF_ESB_SYS_TIMER->TASKS_SHUTDOWN = 1;
  591. NRF_PPI->CHENCLR = (1 << NRF_ESB_PPI_TX_START);
  592. m_interrupt_flags |= NRF_ESB_INT_TX_SUCCESS_MSK;
  593. m_last_tx_attempts = m_config_local.retransmit_count - m_retransmits_remaining + 1;
  594. (void) nrf_esb_skip_tx();
  595. if (m_config_local.protocol != NRF_ESB_PROTOCOL_ESB && m_rx_payload_buffer[0] > 0)
  596. {
  597. if (rx_fifo_push_rfbuf((uint8_t)NRF_RADIO->TXADDRESS, m_rx_payload_buffer[1] >> 1))
  598. {
  599. m_interrupt_flags |= NRF_ESB_INT_RX_DATA_RECEIVED_MSK;
  600. }
  601. }
  602. if ((m_tx_fifo.count == 0) || (m_config_local.tx_mode == NRF_ESB_TXMODE_MANUAL))
  603. {
  604. m_nrf_esb_mainstate = NRF_ESB_STATE_IDLE;
  605. NVIC_SetPendingIRQ(ESB_EVT_IRQ);
  606. }
  607. else
  608. {
  609. NVIC_SetPendingIRQ(ESB_EVT_IRQ);
  610. start_tx_transaction();
  611. }
  612. }
  613. else
  614. {
  615. if (m_retransmits_remaining-- == 0)
  616. {
  617. NRF_ESB_SYS_TIMER->TASKS_SHUTDOWN = 1;
  618. NRF_PPI->CHENCLR = (1 << NRF_ESB_PPI_TX_START);
  619. // All retransmits are expended, and the TX operation is suspended
  620. m_last_tx_attempts = m_config_local.retransmit_count + 1;
  621. m_interrupt_flags |= NRF_ESB_INT_TX_FAILED_MSK;
  622. m_nrf_esb_mainstate = NRF_ESB_STATE_IDLE;
  623. NVIC_SetPendingIRQ(ESB_EVT_IRQ);
  624. }
  625. else
  626. {
  627. // There are still more retransmits left, TX mode should be
  628. // entered again as soon as the system timer reaches CC[1].
  629. NRF_RADIO->SHORTS = m_radio_shorts_common | RADIO_SHORTS_DISABLED_RXEN_Msk;
  630. update_rf_payload_format(mp_current_payload->length);
  631. NRF_RADIO->PACKETPTR = (uint32_t)m_tx_payload_buffer;
  632. on_radio_disabled = on_radio_disabled_tx;
  633. m_nrf_esb_mainstate = NRF_ESB_STATE_PTX_TX_ACK;
  634. NRF_ESB_SYS_TIMER->TASKS_START = 1;
  635. NRF_PPI->CHENSET = (1 << NRF_ESB_PPI_TX_START);
  636. if (NRF_ESB_SYS_TIMER->EVENTS_COMPARE[1])
  637. {
  638. NRF_RADIO->TASKS_TXEN = 1;
  639. }
  640. }
  641. }
  642. }
  643. static void clear_events_restart_rx(void)
  644. {
  645. NRF_RADIO->SHORTS = m_radio_shorts_common;
  646. update_rf_payload_format(m_config_local.payload_length);
  647. NRF_RADIO->PACKETPTR = (uint32_t)m_rx_payload_buffer;
  648. NRF_RADIO->EVENTS_DISABLED = 0;
  649. NRF_RADIO->TASKS_DISABLE = 1;
  650. while (NRF_RADIO->EVENTS_DISABLED == 0);
  651. NRF_RADIO->EVENTS_DISABLED = 0;
  652. NRF_RADIO->SHORTS = m_radio_shorts_common | RADIO_SHORTS_DISABLED_TXEN_Msk;
  653. NRF_RADIO->TASKS_RXEN = 1;
  654. }
  655. static void on_radio_disabled_rx(void)
  656. {
  657. bool ack = false;
  658. bool retransmit_payload = false;
  659. bool send_rx_event = true;
  660. pipe_info_t * p_pipe_info;
  661. if (NRF_RADIO->CRCSTATUS == 0)
  662. {
  663. clear_events_restart_rx();
  664. return;
  665. }
  666. if (m_rx_fifo.count >= NRF_ESB_RX_FIFO_SIZE)
  667. {
  668. clear_events_restart_rx();
  669. return;
  670. }
  671. p_pipe_info = &m_rx_pipe_info[NRF_RADIO->RXMATCH];
  672. if (NRF_RADIO->RXCRC == p_pipe_info->crc &&
  673. (m_rx_payload_buffer[1] >> 1) == p_pipe_info->pid
  674. )
  675. {
  676. retransmit_payload = true;
  677. send_rx_event = false;
  678. }
  679. p_pipe_info->pid = m_rx_payload_buffer[1] >> 1;
  680. p_pipe_info->crc = NRF_RADIO->RXCRC;
  681. if ((m_config_local.selective_auto_ack == false) || ((m_rx_payload_buffer[1] & 0x01) == 1))
  682. {
  683. ack = true;
  684. }
  685. if (ack)
  686. {
  687. NRF_RADIO->SHORTS = m_radio_shorts_common | RADIO_SHORTS_DISABLED_RXEN_Msk;
  688. switch (m_config_local.protocol)
  689. {
  690. case NRF_ESB_PROTOCOL_ESB_DPL:
  691. {
  692. if (m_tx_fifo.count > 0 && m_ack_pl_container_entry_point_pr_pipe[NRF_RADIO->RXMATCH] != 0)
  693. {
  694. mp_current_payload = m_ack_pl_container_entry_point_pr_pipe[NRF_RADIO->RXMATCH]->p_payload;
  695. // Pipe stays in ACK with payload until TX FIFO is empty
  696. // Do not report TX success on first ack payload or retransmit
  697. if (p_pipe_info->ack_payload == true && !retransmit_payload)
  698. {
  699. uint32_t pipe = NRF_RADIO->RXMATCH;
  700. m_ack_pl_container_entry_point_pr_pipe[pipe]->in_use = false;
  701. m_ack_pl_container_entry_point_pr_pipe[pipe] = (nrf_esb_payload_random_access_buf_wrapper_t *)m_ack_pl_container_entry_point_pr_pipe[pipe]->p_next;
  702. m_tx_fifo.count--;
  703. if (m_tx_fifo.count > 0 && m_ack_pl_container_entry_point_pr_pipe[pipe] != 0)
  704. {
  705. mp_current_payload = m_ack_pl_container_entry_point_pr_pipe[pipe]->p_payload;
  706. }
  707. else mp_current_payload = 0;
  708. // ACK payloads also require TX_DS
  709. // (page 40 of the 'nRF24LE1_Product_Specification_rev1_6.pdf').
  710. m_interrupt_flags |= NRF_ESB_INT_TX_SUCCESS_MSK;
  711. }
  712. if(mp_current_payload != 0)
  713. {
  714. p_pipe_info->ack_payload = true;
  715. update_rf_payload_format(mp_current_payload->length);
  716. m_tx_payload_buffer[0] = mp_current_payload->length;
  717. memcpy(&m_tx_payload_buffer[2],
  718. mp_current_payload->data,
  719. mp_current_payload->length);
  720. }
  721. else
  722. {
  723. p_pipe_info->ack_payload = false;
  724. update_rf_payload_format(0);
  725. m_tx_payload_buffer[0] = 0;
  726. }
  727. }
  728. else
  729. {
  730. p_pipe_info->ack_payload = false;
  731. update_rf_payload_format(0);
  732. m_tx_payload_buffer[0] = 0;
  733. }
  734. m_tx_payload_buffer[1] = m_rx_payload_buffer[1];
  735. }
  736. break;
  737. case NRF_ESB_PROTOCOL_ESB:
  738. {
  739. update_rf_payload_format(0);
  740. m_tx_payload_buffer[0] = m_rx_payload_buffer[0];
  741. m_tx_payload_buffer[1] = 0;
  742. }
  743. break;
  744. }
  745. m_nrf_esb_mainstate = NRF_ESB_STATE_PRX_SEND_ACK;
  746. NRF_RADIO->TXADDRESS = NRF_RADIO->RXMATCH;
  747. NRF_RADIO->PACKETPTR = (uint32_t)m_tx_payload_buffer;
  748. on_radio_disabled = on_radio_disabled_rx_ack;
  749. }
  750. else
  751. {
  752. clear_events_restart_rx();
  753. }
  754. if (send_rx_event)
  755. {
  756. // Push the new packet to the RX buffer and trigger a received event if the operation was
  757. // successful.
  758. if (rx_fifo_push_rfbuf(NRF_RADIO->RXMATCH, p_pipe_info->pid))
  759. {
  760. m_interrupt_flags |= NRF_ESB_INT_RX_DATA_RECEIVED_MSK;
  761. NVIC_SetPendingIRQ(ESB_EVT_IRQ);
  762. }
  763. }
  764. }
  765. static void on_radio_disabled_rx_ack(void)
  766. {
  767. NRF_RADIO->SHORTS = m_radio_shorts_common | RADIO_SHORTS_DISABLED_TXEN_Msk;
  768. update_rf_payload_format(m_config_local.payload_length);
  769. NRF_RADIO->PACKETPTR = (uint32_t)m_rx_payload_buffer;
  770. on_radio_disabled = on_radio_disabled_rx;
  771. m_nrf_esb_mainstate = NRF_ESB_STATE_PRX;
  772. }
  773. /**@brief Function for clearing pending interrupts.
  774. *
  775. * @param[in,out] p_interrupts Pointer to the value that holds the current interrupts.
  776. *
  777. * @retval NRF_SUCCESS If the interrupts were cleared successfully.
  778. * @retval NRF_ERROR_NULL If the required parameter was NULL.
  779. * @retval NRF_INVALID_STATE If the module is not initialized.
  780. */
  781. static uint32_t nrf_esb_get_clear_interrupts(uint32_t * p_interrupts)
  782. {
  783. VERIFY_TRUE(m_esb_initialized, NRF_ERROR_INVALID_STATE);
  784. VERIFY_PARAM_NOT_NULL(p_interrupts);
  785. DISABLE_RF_IRQ();
  786. *p_interrupts = m_interrupt_flags;
  787. m_interrupt_flags = 0;
  788. ENABLE_RF_IRQ();
  789. return NRF_SUCCESS;
  790. }
  791. void RADIO_IRQHandler()
  792. {
  793. if (NRF_RADIO->EVENTS_READY && (NRF_RADIO->INTENSET & RADIO_INTENSET_READY_Msk))
  794. {
  795. NRF_RADIO->EVENTS_READY = 0;
  796. DEBUG_PIN_SET(DEBUGPIN1);
  797. }
  798. if (NRF_RADIO->EVENTS_END && (NRF_RADIO->INTENSET & RADIO_INTENSET_END_Msk))
  799. {
  800. NRF_RADIO->EVENTS_END = 0;
  801. DEBUG_PIN_SET(DEBUGPIN2);
  802. // Call the correct on_radio_end function, depending on the current protocol state
  803. if (on_radio_end)
  804. {
  805. on_radio_end();
  806. }
  807. }
  808. if (NRF_RADIO->EVENTS_DISABLED && (NRF_RADIO->INTENSET & RADIO_INTENSET_DISABLED_Msk))
  809. {
  810. NRF_RADIO->EVENTS_DISABLED = 0;
  811. DEBUG_PIN_SET(DEBUGPIN3);
  812. // Call the correct on_radio_disable function, depending on the current protocol state
  813. if (on_radio_disabled)
  814. {
  815. on_radio_disabled();
  816. }
  817. }
  818. DEBUG_PIN_CLR(DEBUGPIN1);
  819. DEBUG_PIN_CLR(DEBUGPIN2);
  820. DEBUG_PIN_CLR(DEBUGPIN3);
  821. DEBUG_PIN_CLR(DEBUGPIN4);
  822. }
  823. uint32_t nrf_esb_init(nrf_esb_config_t const * p_config)
  824. {
  825. uint32_t err_code;
  826. VERIFY_PARAM_NOT_NULL(p_config);
  827. if (m_esb_initialized)
  828. {
  829. err_code = nrf_esb_disable();
  830. if (err_code != NRF_SUCCESS)
  831. {
  832. return err_code;
  833. }
  834. }
  835. m_event_handler = p_config->event_handler;
  836. memcpy(&m_config_local, p_config, sizeof(nrf_esb_config_t));
  837. m_interrupt_flags = 0;
  838. memset(m_rx_pipe_info, 0, sizeof(m_rx_pipe_info));
  839. memset(m_pids, 0, sizeof(m_pids));
  840. VERIFY_TRUE(update_radio_parameters(), NRF_ERROR_INVALID_PARAM);
  841. // Configure radio address registers according to ESB default values
  842. NRF_RADIO->BASE0 = 0xE7E7E7E7;
  843. NRF_RADIO->BASE1 = 0x43434343;
  844. NRF_RADIO->PREFIX0 = 0x23C343E7;
  845. NRF_RADIO->PREFIX1 = 0x13E363A3;
  846. initialize_fifos();
  847. sys_timer_init();
  848. ppi_init();
  849. NVIC_SetPriority(RADIO_IRQn, m_config_local.radio_irq_priority & ESB_IRQ_PRIORITY_MSK);
  850. NVIC_SetPriority(ESB_EVT_IRQ, m_config_local.event_irq_priority & ESB_IRQ_PRIORITY_MSK);
  851. NVIC_EnableIRQ(ESB_EVT_IRQ);
  852. m_nrf_esb_mainstate = NRF_ESB_STATE_IDLE;
  853. m_esb_initialized = true;
  854. #ifdef NRF52832_XXAA
  855. if ((NRF_FICR->INFO.VARIANT & 0x0000FF00) == 0x00004500) //Check if the device is an nRF52832 Rev. 2.
  856. //Workaround for nRF52832 rev 2 errata 182
  857. *(volatile uint32_t *) 0x4000173C |= (1 << 10);
  858. #endif
  859. return NRF_SUCCESS;
  860. }
  861. uint32_t nrf_esb_suspend(void)
  862. {
  863. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  864. // Clear PPI
  865. NRF_PPI->CHENCLR = (1 << NRF_ESB_PPI_TIMER_START) |
  866. (1 << NRF_ESB_PPI_TIMER_STOP) |
  867. (1 << NRF_ESB_PPI_RX_TIMEOUT) |
  868. (1 << NRF_ESB_PPI_TX_START);
  869. m_nrf_esb_mainstate = NRF_ESB_STATE_IDLE;
  870. return NRF_SUCCESS;
  871. }
  872. uint32_t nrf_esb_disable(void)
  873. {
  874. // Clear PPI
  875. NRF_PPI->CHENCLR = (1 << NRF_ESB_PPI_TIMER_START) |
  876. (1 << NRF_ESB_PPI_TIMER_STOP) |
  877. (1 << NRF_ESB_PPI_RX_TIMEOUT) |
  878. (1 << NRF_ESB_PPI_TX_START);
  879. m_nrf_esb_mainstate = NRF_ESB_STATE_IDLE;
  880. m_esb_initialized = false;
  881. reset_fifos();
  882. memset(m_rx_pipe_info, 0, sizeof(m_rx_pipe_info));
  883. memset(m_pids, 0, sizeof(m_pids));
  884. // Disable the radio
  885. NVIC_DisableIRQ(ESB_EVT_IRQ);
  886. NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Enabled << RADIO_SHORTS_READY_START_Pos |
  887. RADIO_SHORTS_END_DISABLE_Enabled << RADIO_SHORTS_END_DISABLE_Pos;
  888. return NRF_SUCCESS;
  889. }
  890. bool nrf_esb_is_idle(void)
  891. {
  892. return m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE;
  893. }
  894. void ESB_EVT_IRQHandler(void)
  895. {
  896. ret_code_t err_code;
  897. uint32_t interrupts;
  898. nrf_esb_evt_t event;
  899. event.tx_attempts = m_last_tx_attempts;
  900. err_code = nrf_esb_get_clear_interrupts(&interrupts);
  901. if (err_code == NRF_SUCCESS && m_event_handler != 0)
  902. {
  903. if (interrupts & NRF_ESB_INT_TX_SUCCESS_MSK)
  904. {
  905. event.evt_id = NRF_ESB_EVENT_TX_SUCCESS;
  906. m_event_handler(&event);
  907. }
  908. if (interrupts & NRF_ESB_INT_TX_FAILED_MSK)
  909. {
  910. event.evt_id = NRF_ESB_EVENT_TX_FAILED;
  911. m_event_handler(&event);
  912. }
  913. if (interrupts & NRF_ESB_INT_RX_DATA_RECEIVED_MSK)
  914. {
  915. event.evt_id = NRF_ESB_EVENT_RX_RECEIVED;
  916. m_event_handler(&event);
  917. }
  918. }
  919. }
  920. static nrf_esb_payload_random_access_buf_wrapper_t *find_free_payload_cont(void)
  921. {
  922. for (int i = 0; i < NRF_ESB_TX_FIFO_SIZE; i++)
  923. {
  924. if(!m_ack_pl_container[i].in_use) return &m_ack_pl_container[i];
  925. }
  926. return 0;
  927. }
  928. uint32_t nrf_esb_write_payload(nrf_esb_payload_t const * p_payload)
  929. {
  930. VERIFY_TRUE(m_esb_initialized, NRF_ERROR_INVALID_STATE);
  931. VERIFY_PARAM_NOT_NULL(p_payload);
  932. VERIFY_PAYLOAD_LENGTH(p_payload);
  933. VERIFY_FALSE(m_tx_fifo.count >= NRF_ESB_TX_FIFO_SIZE, NRF_ERROR_NO_MEM);
  934. VERIFY_TRUE(p_payload->pipe < NRF_ESB_PIPE_COUNT, NRF_ERROR_INVALID_PARAM);
  935. DISABLE_RF_IRQ();
  936. if (m_config_local.mode == NRF_ESB_MODE_PTX)
  937. {
  938. memcpy(m_tx_fifo.p_payload[m_tx_fifo.entry_point], p_payload, sizeof(nrf_esb_payload_t));
  939. m_pids[p_payload->pipe] = (m_pids[p_payload->pipe] + 1) % (NRF_ESB_PID_MAX + 1);
  940. m_tx_fifo.p_payload[m_tx_fifo.entry_point]->pid = m_pids[p_payload->pipe];
  941. if (++m_tx_fifo.entry_point >= NRF_ESB_TX_FIFO_SIZE)
  942. {
  943. m_tx_fifo.entry_point = 0;
  944. }
  945. m_tx_fifo.count++;
  946. }
  947. else
  948. {
  949. nrf_esb_payload_random_access_buf_wrapper_t *new_ack_payload;
  950. if((new_ack_payload = find_free_payload_cont()) != 0)
  951. {
  952. new_ack_payload->in_use = true;
  953. new_ack_payload->p_next = 0;
  954. memcpy(new_ack_payload->p_payload, p_payload, sizeof(nrf_esb_payload_t));
  955. m_pids[p_payload->pipe] = (m_pids[p_payload->pipe] + 1) % (NRF_ESB_PID_MAX + 1);
  956. new_ack_payload->p_payload->pid = m_pids[p_payload->pipe];
  957. if(m_ack_pl_container_entry_point_pr_pipe[p_payload->pipe] == 0)
  958. {
  959. m_ack_pl_container_entry_point_pr_pipe[p_payload->pipe] = new_ack_payload;
  960. }
  961. else
  962. {
  963. nrf_esb_payload_random_access_buf_wrapper_t *list_iterator = m_ack_pl_container_entry_point_pr_pipe[p_payload->pipe];
  964. while(list_iterator->p_next != 0)
  965. {
  966. list_iterator = (nrf_esb_payload_random_access_buf_wrapper_t *)list_iterator->p_next;
  967. }
  968. list_iterator->p_next = (struct nrf_esb_payload_random_access_buf_wrapper_t *)new_ack_payload;
  969. }
  970. m_tx_fifo.count++;
  971. }
  972. }
  973. ENABLE_RF_IRQ();
  974. if (m_config_local.mode == NRF_ESB_MODE_PTX &&
  975. m_config_local.tx_mode == NRF_ESB_TXMODE_AUTO &&
  976. m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE)
  977. {
  978. start_tx_transaction();
  979. }
  980. return NRF_SUCCESS;
  981. }
  982. uint32_t nrf_esb_read_rx_payload(nrf_esb_payload_t * p_payload)
  983. {
  984. VERIFY_TRUE(m_esb_initialized, NRF_ERROR_INVALID_STATE);
  985. VERIFY_PARAM_NOT_NULL(p_payload);
  986. if (m_rx_fifo.count == 0)
  987. {
  988. return NRF_ERROR_NOT_FOUND;
  989. }
  990. DISABLE_RF_IRQ();
  991. p_payload->length = m_rx_fifo.p_payload[m_rx_fifo.exit_point]->length;
  992. p_payload->pipe = m_rx_fifo.p_payload[m_rx_fifo.exit_point]->pipe;
  993. p_payload->rssi = m_rx_fifo.p_payload[m_rx_fifo.exit_point]->rssi;
  994. p_payload->pid = m_rx_fifo.p_payload[m_rx_fifo.exit_point]->pid;
  995. p_payload->noack = m_rx_fifo.p_payload[m_rx_fifo.exit_point]->noack;
  996. memcpy(p_payload->data, m_rx_fifo.p_payload[m_rx_fifo.exit_point]->data, p_payload->length);
  997. if (++m_rx_fifo.exit_point >= NRF_ESB_RX_FIFO_SIZE)
  998. {
  999. m_rx_fifo.exit_point = 0;
  1000. }
  1001. m_rx_fifo.count--;
  1002. ENABLE_RF_IRQ();
  1003. return NRF_SUCCESS;
  1004. }
  1005. uint32_t nrf_esb_start_tx(void)
  1006. {
  1007. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1008. if (m_tx_fifo.count == 0)
  1009. {
  1010. return NRF_ERROR_BUFFER_EMPTY;
  1011. }
  1012. start_tx_transaction();
  1013. return NRF_SUCCESS;
  1014. }
  1015. uint32_t nrf_esb_start_rx(void)
  1016. {
  1017. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1018. NRF_RADIO->INTENCLR = 0xFFFFFFFF;
  1019. NRF_RADIO->EVENTS_DISABLED = 0;
  1020. on_radio_disabled = on_radio_disabled_rx;
  1021. NRF_RADIO->SHORTS = m_radio_shorts_common | RADIO_SHORTS_DISABLED_TXEN_Msk;
  1022. NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk;
  1023. m_nrf_esb_mainstate = NRF_ESB_STATE_PRX;
  1024. NRF_RADIO->RXADDRESSES = m_esb_addr.rx_pipes_enabled;
  1025. NRF_RADIO->FREQUENCY = m_esb_addr.rf_channel;
  1026. NRF_RADIO->PACKETPTR = (uint32_t)m_rx_payload_buffer;
  1027. NVIC_ClearPendingIRQ(RADIO_IRQn);
  1028. NVIC_EnableIRQ(RADIO_IRQn);
  1029. NRF_RADIO->EVENTS_ADDRESS = 0;
  1030. NRF_RADIO->EVENTS_PAYLOAD = 0;
  1031. NRF_RADIO->EVENTS_DISABLED = 0;
  1032. NRF_RADIO->TASKS_RXEN = 1;
  1033. return NRF_SUCCESS;
  1034. }
  1035. uint32_t nrf_esb_stop_rx(void)
  1036. {
  1037. if (m_nrf_esb_mainstate == NRF_ESB_STATE_PRX ||
  1038. m_nrf_esb_mainstate == NRF_ESB_STATE_PRX_SEND_ACK)
  1039. {
  1040. NRF_RADIO->SHORTS = 0;
  1041. NRF_RADIO->INTENCLR = 0xFFFFFFFF;
  1042. on_radio_disabled = NULL;
  1043. NRF_RADIO->EVENTS_DISABLED = 0;
  1044. NRF_RADIO->TASKS_DISABLE = 1;
  1045. while (NRF_RADIO->EVENTS_DISABLED == 0);
  1046. m_nrf_esb_mainstate = NRF_ESB_STATE_IDLE;
  1047. return NRF_SUCCESS;
  1048. }
  1049. return NRF_ESB_ERROR_NOT_IN_RX_MODE;
  1050. }
  1051. uint32_t nrf_esb_flush_tx(void)
  1052. {
  1053. VERIFY_TRUE(m_esb_initialized, NRF_ERROR_INVALID_STATE);
  1054. DISABLE_RF_IRQ();
  1055. m_tx_fifo.count = 0;
  1056. m_tx_fifo.entry_point = 0;
  1057. m_tx_fifo.exit_point = 0;
  1058. ENABLE_RF_IRQ();
  1059. return NRF_SUCCESS;
  1060. }
  1061. uint32_t nrf_esb_pop_tx(void)
  1062. {
  1063. VERIFY_TRUE(m_esb_initialized, NRF_ERROR_INVALID_STATE);
  1064. VERIFY_TRUE(m_tx_fifo.count > 0, NRF_ERROR_BUFFER_EMPTY);
  1065. DISABLE_RF_IRQ();
  1066. if (m_tx_fifo.entry_point == 0)
  1067. {
  1068. m_tx_fifo.entry_point = (NRF_ESB_TX_FIFO_SIZE-1);
  1069. }
  1070. else
  1071. {
  1072. m_tx_fifo.entry_point--;
  1073. }
  1074. m_tx_fifo.count--;
  1075. ENABLE_RF_IRQ();
  1076. return NRF_SUCCESS;
  1077. }
  1078. uint32_t nrf_esb_flush_rx(void)
  1079. {
  1080. VERIFY_TRUE(m_esb_initialized, NRF_ERROR_INVALID_STATE);
  1081. DISABLE_RF_IRQ();
  1082. m_rx_fifo.count = 0;
  1083. m_rx_fifo.entry_point = 0;
  1084. m_rx_fifo.exit_point = 0;
  1085. memset(m_rx_pipe_info, 0, sizeof(m_rx_pipe_info));
  1086. ENABLE_RF_IRQ();
  1087. return NRF_SUCCESS;
  1088. }
  1089. uint32_t nrf_esb_set_address_length(uint8_t length)
  1090. {
  1091. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1092. VERIFY_TRUE(length > 2 && length < 6, NRF_ERROR_INVALID_PARAM);
  1093. #ifdef NRF52832_XXAA
  1094. uint32_t base_address_mask = length == 5 ? 0xFFFF0000 : 0xFF000000;
  1095. if ((NRF_FICR->INFO.VARIANT & 0x0000FF00) == 0x00004200) //Check if the device is an nRF52832 Rev. 1.
  1096. {
  1097. /*
  1098. Workaround for nRF52832 Rev 1 Errata 107
  1099. Check if pipe 0 or pipe 1-7 has a 'zero address'.
  1100. Avoid using access addresses in the following pattern (where X is don't care):
  1101. ADDRLEN=5
  1102. BASE0 = 0x0000XXXX, PREFIX0 = 0xXXXXXX00
  1103. BASE1 = 0x0000XXXX, PREFIX0 = 0xXXXX00XX
  1104. BASE1 = 0x0000XXXX, PREFIX0 = 0xXX00XXXX
  1105. BASE1 = 0x0000XXXX, PREFIX0 = 0x00XXXXXX
  1106. BASE1 = 0x0000XXXX, PREFIX1 = 0xXXXXXX00
  1107. BASE1 = 0x0000XXXX, PREFIX1 = 0xXXXX00XX
  1108. BASE1 = 0x0000XXXX, PREFIX1 = 0xXX00XXXX
  1109. BASE1 = 0x0000XXXX, PREFIX1 = 0x00XXXXXX
  1110. ADDRLEN=4
  1111. BASE0 = 0x00XXXXXX, PREFIX0 = 0xXXXXXX00
  1112. BASE1 = 0x00XXXXXX, PREFIX0 = 0xXXXX00XX
  1113. BASE1 = 0x00XXXXXX, PREFIX0 = 0xXX00XXXX
  1114. BASE1 = 0x00XXXXXX, PREFIX0 = 0x00XXXXXX
  1115. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXXXXXX00
  1116. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXXXX00XX
  1117. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXX00XXXX
  1118. BASE1 = 0x00XXXXXX, PREFIX1 = 0x00XXXXXX
  1119. */
  1120. if ((NRF_RADIO->BASE0 & base_address_mask) == 0 && (NRF_RADIO->PREFIX0 & 0x000000FF) == 0)
  1121. {
  1122. return NRF_ERROR_INVALID_PARAM;
  1123. }
  1124. if ((NRF_RADIO->BASE1 & base_address_mask) == 0 && ((NRF_RADIO->PREFIX0 & 0x0000FF00) == 0 ||(NRF_RADIO->PREFIX0 & 0x00FF0000) == 0 || (NRF_RADIO->PREFIX0 & 0xFF000000) == 0 ||
  1125. (NRF_RADIO->PREFIX1 & 0xFF000000) == 0 || (NRF_RADIO->PREFIX1 & 0x00FF0000) == 0 ||(NRF_RADIO->PREFIX1 & 0x0000FF00) == 0 || (NRF_RADIO->PREFIX1 & 0x000000FF) == 0))
  1126. {
  1127. return NRF_ERROR_INVALID_PARAM;
  1128. }
  1129. }
  1130. #endif
  1131. m_esb_addr.addr_length = length;
  1132. update_rf_payload_format(m_config_local.payload_length);
  1133. #ifdef NRF52832_XXAA
  1134. if ((NRF_FICR->INFO.VARIANT & 0x0000FF00) == 0x00004500) //Check if the device is an nRF52832 Rev. 2.
  1135. {
  1136. return apply_address_workarounds();
  1137. }
  1138. else
  1139. {
  1140. return NRF_SUCCESS;
  1141. }
  1142. #else
  1143. return NRF_SUCCESS;
  1144. #endif
  1145. }
  1146. uint32_t nrf_esb_set_base_address_0(uint8_t const * p_addr)
  1147. {
  1148. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1149. VERIFY_PARAM_NOT_NULL(p_addr);
  1150. #ifdef NRF52832_XXAA
  1151. if ((NRF_FICR->INFO.VARIANT & 0x0000FF00) == 0x00004200) //Check if the device is an nRF52832 Rev. 1.
  1152. {
  1153. /*
  1154. Workaround for nRF52832 Rev 1 Errata 107
  1155. Check if pipe 0 or pipe 1-7 has a 'zero address'.
  1156. Avoid using access addresses in the following pattern (where X is don't care):
  1157. ADDRLEN=5
  1158. BASE0 = 0x0000XXXX, PREFIX0 = 0xXXXXXX00
  1159. BASE1 = 0x0000XXXX, PREFIX0 = 0xXXXX00XX
  1160. BASE1 = 0x0000XXXX, PREFIX0 = 0xXX00XXXX
  1161. BASE1 = 0x0000XXXX, PREFIX0 = 0x00XXXXXX
  1162. BASE1 = 0x0000XXXX, PREFIX1 = 0xXXXXXX00
  1163. BASE1 = 0x0000XXXX, PREFIX1 = 0xXXXX00XX
  1164. BASE1 = 0x0000XXXX, PREFIX1 = 0xXX00XXXX
  1165. BASE1 = 0x0000XXXX, PREFIX1 = 0x00XXXXXX
  1166. ADDRLEN=4
  1167. BASE0 = 0x00XXXXXX, PREFIX0 = 0xXXXXXX00
  1168. BASE1 = 0x00XXXXXX, PREFIX0 = 0xXXXX00XX
  1169. BASE1 = 0x00XXXXXX, PREFIX0 = 0xXX00XXXX
  1170. BASE1 = 0x00XXXXXX, PREFIX0 = 0x00XXXXXX
  1171. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXXXXXX00
  1172. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXXXX00XX
  1173. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXX00XXXX
  1174. BASE1 = 0x00XXXXXX, PREFIX1 = 0x00XXXXXX
  1175. */
  1176. uint32_t base_address_mask = m_esb_addr.addr_length == 5 ? 0xFFFF0000 : 0xFF000000;
  1177. if ((addr_conv(p_addr) & base_address_mask) == 0 && (NRF_RADIO->PREFIX0 & 0x000000FF) == 0)
  1178. {
  1179. return NRF_ERROR_INVALID_PARAM;
  1180. }
  1181. }
  1182. #endif
  1183. memcpy(m_esb_addr.base_addr_p0, p_addr, 4);
  1184. update_radio_addresses(NRF_ESB_ADDR_UPDATE_MASK_BASE0);
  1185. #ifdef NRF52832_XXAA
  1186. return apply_address_workarounds();
  1187. #else
  1188. return NRF_SUCCESS;
  1189. #endif
  1190. }
  1191. uint32_t nrf_esb_set_base_address_1(uint8_t const * p_addr)
  1192. {
  1193. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1194. VERIFY_PARAM_NOT_NULL(p_addr);
  1195. #ifdef NRF52832_XXAA
  1196. if ((NRF_FICR->INFO.VARIANT & 0x0000FF00) == 0x00004200) //Check if the device is an nRF52832 Rev. 1.
  1197. {
  1198. /*
  1199. Workaround for nRF52832 Rev 1 Errata 107
  1200. Check if pipe 0 or pipe 1-7 has a 'zero address'.
  1201. Avoid using access addresses in the following pattern (where X is don't care):
  1202. ADDRLEN=5
  1203. BASE0 = 0x0000XXXX, PREFIX0 = 0xXXXXXX00
  1204. BASE1 = 0x0000XXXX, PREFIX0 = 0xXXXX00XX
  1205. BASE1 = 0x0000XXXX, PREFIX0 = 0xXX00XXXX
  1206. BASE1 = 0x0000XXXX, PREFIX0 = 0x00XXXXXX
  1207. BASE1 = 0x0000XXXX, PREFIX1 = 0xXXXXXX00
  1208. BASE1 = 0x0000XXXX, PREFIX1 = 0xXXXX00XX
  1209. BASE1 = 0x0000XXXX, PREFIX1 = 0xXX00XXXX
  1210. BASE1 = 0x0000XXXX, PREFIX1 = 0x00XXXXXX
  1211. ADDRLEN=4
  1212. BASE0 = 0x00XXXXXX, PREFIX0 = 0xXXXXXX00
  1213. BASE1 = 0x00XXXXXX, PREFIX0 = 0xXXXX00XX
  1214. BASE1 = 0x00XXXXXX, PREFIX0 = 0xXX00XXXX
  1215. BASE1 = 0x00XXXXXX, PREFIX0 = 0x00XXXXXX
  1216. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXXXXXX00
  1217. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXXXX00XX
  1218. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXX00XXXX
  1219. BASE1 = 0x00XXXXXX, PREFIX1 = 0x00XXXXXX
  1220. */
  1221. uint32_t base_address_mask = m_esb_addr.addr_length == 5 ? 0xFFFF0000 : 0xFF000000;
  1222. if ((addr_conv(p_addr) & base_address_mask) == 0 &&
  1223. ((NRF_RADIO->PREFIX0 & 0x0000FF00) == 0 ||(NRF_RADIO->PREFIX0 & 0x00FF0000) == 0 ||
  1224. (NRF_RADIO->PREFIX0 & 0xFF000000) == 0 || (NRF_RADIO->PREFIX1 & 0xFF000000) == 0 ||
  1225. (NRF_RADIO->PREFIX1 & 0x00FF0000) == 0 ||(NRF_RADIO->PREFIX1 & 0x0000FF00) == 0 ||
  1226. (NRF_RADIO->PREFIX1 & 0x000000FF) == 0))
  1227. {
  1228. return NRF_ERROR_INVALID_PARAM;
  1229. }
  1230. }
  1231. #endif
  1232. memcpy(m_esb_addr.base_addr_p1, p_addr, 4);
  1233. update_radio_addresses(NRF_ESB_ADDR_UPDATE_MASK_BASE1);
  1234. #ifdef NRF52832_XXAA
  1235. return apply_address_workarounds();
  1236. #else
  1237. return NRF_SUCCESS;
  1238. #endif
  1239. }
  1240. uint32_t nrf_esb_set_prefixes(uint8_t const * p_prefixes, uint8_t num_pipes)
  1241. {
  1242. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1243. VERIFY_PARAM_NOT_NULL(p_prefixes);
  1244. VERIFY_TRUE(num_pipes <= NRF_ESB_PIPE_COUNT, NRF_ERROR_INVALID_PARAM);
  1245. #ifdef NRF52832_XXAA
  1246. if ((NRF_FICR->INFO.VARIANT & 0x0000FF00) == 0x00004200) //Check if the device is an nRF52832 Rev. 1.
  1247. {
  1248. /*
  1249. Workaround for nRF52832 Rev 1 Errata 107
  1250. Check if pipe 0 or pipe 1-7 has a 'zero address'.
  1251. Avoid using access addresses in the following pattern (where X is don't care):
  1252. ADDRLEN=5
  1253. BASE0 = 0x0000XXXX, PREFIX0 = 0xXXXXXX00
  1254. BASE1 = 0x0000XXXX, PREFIX0 = 0xXXXX00XX
  1255. BASE1 = 0x0000XXXX, PREFIX0 = 0xXX00XXXX
  1256. BASE1 = 0x0000XXXX, PREFIX0 = 0x00XXXXXX
  1257. BASE1 = 0x0000XXXX, PREFIX1 = 0xXXXXXX00
  1258. BASE1 = 0x0000XXXX, PREFIX1 = 0xXXXX00XX
  1259. BASE1 = 0x0000XXXX, PREFIX1 = 0xXX00XXXX
  1260. BASE1 = 0x0000XXXX, PREFIX1 = 0x00XXXXXX
  1261. ADDRLEN=4
  1262. BASE0 = 0x00XXXXXX, PREFIX0 = 0xXXXXXX00
  1263. BASE1 = 0x00XXXXXX, PREFIX0 = 0xXXXX00XX
  1264. BASE1 = 0x00XXXXXX, PREFIX0 = 0xXX00XXXX
  1265. BASE1 = 0x00XXXXXX, PREFIX0 = 0x00XXXXXX
  1266. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXXXXXX00
  1267. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXXXX00XX
  1268. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXX00XXXX
  1269. BASE1 = 0x00XXXXXX, PREFIX1 = 0x00XXXXXX
  1270. */
  1271. uint32_t base_address_mask = m_esb_addr.addr_length == 5 ? 0xFFFF0000 : 0xFF000000;
  1272. if (num_pipes >= 1 && (NRF_RADIO->BASE0 & base_address_mask) == 0 && p_prefixes[0] == 0)
  1273. {
  1274. return NRF_ERROR_INVALID_PARAM;
  1275. }
  1276. if ((NRF_RADIO->BASE1 & base_address_mask) == 0)
  1277. {
  1278. for (uint8_t i = 1; i < num_pipes; i++)
  1279. {
  1280. if (p_prefixes[i] == 0)
  1281. {
  1282. return NRF_ERROR_INVALID_PARAM;
  1283. }
  1284. }
  1285. }
  1286. }
  1287. #endif
  1288. memcpy(m_esb_addr.pipe_prefixes, p_prefixes, num_pipes);
  1289. m_esb_addr.num_pipes = num_pipes;
  1290. m_esb_addr.rx_pipes_enabled = BIT_MASK_UINT_8(num_pipes);
  1291. update_radio_addresses(NRF_ESB_ADDR_UPDATE_MASK_PREFIX);
  1292. #ifdef NRF52832_XXAA
  1293. return apply_address_workarounds();
  1294. #else
  1295. return NRF_SUCCESS;
  1296. #endif
  1297. }
  1298. uint32_t nrf_esb_update_prefix(uint8_t pipe, uint8_t prefix)
  1299. {
  1300. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1301. VERIFY_TRUE(pipe < NRF_ESB_PIPE_COUNT, NRF_ERROR_INVALID_PARAM);
  1302. #ifdef NRF52832_XXAA
  1303. if ((NRF_FICR->INFO.VARIANT & 0x0000FF00) == 0x00004200) //Check if the device is an nRF52832 Rev. 1.
  1304. {
  1305. /*
  1306. Workaround for nRF52832 Rev 1 Errata 107
  1307. Check if pipe 0 or pipe 1-7 has a 'zero address'.
  1308. Avoid using access addresses in the following pattern (where X is don't care):
  1309. ADDRLEN=5
  1310. BASE0 = 0x0000XXXX, PREFIX0 = 0xXXXXXX00
  1311. BASE1 = 0x0000XXXX, PREFIX0 = 0xXXXX00XX
  1312. BASE1 = 0x0000XXXX, PREFIX0 = 0xXX00XXXX
  1313. BASE1 = 0x0000XXXX, PREFIX0 = 0x00XXXXXX
  1314. BASE1 = 0x0000XXXX, PREFIX1 = 0xXXXXXX00
  1315. BASE1 = 0x0000XXXX, PREFIX1 = 0xXXXX00XX
  1316. BASE1 = 0x0000XXXX, PREFIX1 = 0xXX00XXXX
  1317. BASE1 = 0x0000XXXX, PREFIX1 = 0x00XXXXXX
  1318. ADDRLEN=4
  1319. BASE0 = 0x00XXXXXX, PREFIX0 = 0xXXXXXX00
  1320. BASE1 = 0x00XXXXXX, PREFIX0 = 0xXXXX00XX
  1321. BASE1 = 0x00XXXXXX, PREFIX0 = 0xXX00XXXX
  1322. BASE1 = 0x00XXXXXX, PREFIX0 = 0x00XXXXXX
  1323. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXXXXXX00
  1324. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXXXX00XX
  1325. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXX00XXXX
  1326. BASE1 = 0x00XXXXXX, PREFIX1 = 0x00XXXXXX
  1327. */
  1328. uint32_t base_address_mask = m_esb_addr.addr_length == 5 ? 0xFFFF0000 : 0xFF000000;
  1329. if (pipe == 0)
  1330. {
  1331. if ((NRF_RADIO->BASE0 & base_address_mask) == 0 && prefix == 0)
  1332. {
  1333. return NRF_ERROR_INVALID_PARAM;
  1334. }
  1335. }
  1336. else
  1337. {
  1338. if ((NRF_RADIO->BASE1 & base_address_mask) == 0 && prefix == 0)
  1339. {
  1340. return NRF_ERROR_INVALID_PARAM;
  1341. }
  1342. }
  1343. }
  1344. #endif
  1345. m_esb_addr.pipe_prefixes[pipe] = prefix;
  1346. update_radio_addresses(NRF_ESB_ADDR_UPDATE_MASK_PREFIX);
  1347. #ifdef NRF52832_XXAA
  1348. return apply_address_workarounds();
  1349. #else
  1350. return NRF_SUCCESS;
  1351. #endif
  1352. }
  1353. uint32_t nrf_esb_enable_pipes(uint8_t enable_mask)
  1354. {
  1355. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1356. VERIFY_TRUE((enable_mask | BIT_MASK_UINT_8(NRF_ESB_PIPE_COUNT)) == BIT_MASK_UINT_8(NRF_ESB_PIPE_COUNT), NRF_ERROR_INVALID_PARAM);
  1357. m_esb_addr.rx_pipes_enabled = enable_mask;
  1358. #ifdef NRF52832_XXAA
  1359. return apply_address_workarounds();
  1360. #else
  1361. return NRF_SUCCESS;
  1362. #endif
  1363. }
  1364. uint32_t nrf_esb_set_rf_channel(uint32_t channel)
  1365. {
  1366. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1367. VERIFY_TRUE(channel <= 100, NRF_ERROR_INVALID_PARAM);
  1368. m_esb_addr.rf_channel = channel;
  1369. return NRF_SUCCESS;
  1370. }
  1371. uint32_t nrf_esb_get_rf_channel(uint32_t * p_channel)
  1372. {
  1373. VERIFY_PARAM_NOT_NULL(p_channel);
  1374. *p_channel = m_esb_addr.rf_channel;
  1375. return NRF_SUCCESS;
  1376. }
  1377. uint32_t nrf_esb_set_tx_power(nrf_esb_tx_power_t tx_output_power)
  1378. {
  1379. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1380. if ( m_config_local.tx_output_power != tx_output_power )
  1381. {
  1382. m_config_local.tx_output_power = tx_output_power;
  1383. update_radio_tx_power();
  1384. }
  1385. return NRF_SUCCESS;
  1386. }
  1387. uint32_t nrf_esb_set_retransmit_delay(uint16_t delay)
  1388. {
  1389. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1390. VERIFY_TRUE(delay >= m_wait_for_ack_timeout_us + RETRANSMIT_DELAY_US_OFFSET, NRF_ERROR_INVALID_PARAM);
  1391. m_config_local.retransmit_delay = delay;
  1392. return NRF_SUCCESS;
  1393. }
  1394. uint32_t nrf_esb_set_retransmit_count(uint16_t count)
  1395. {
  1396. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1397. m_config_local.retransmit_count = count;
  1398. return NRF_SUCCESS;
  1399. }
  1400. uint32_t nrf_esb_set_bitrate(nrf_esb_bitrate_t bitrate)
  1401. {
  1402. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1403. m_config_local.bitrate = bitrate;
  1404. return update_radio_bitrate() ? NRF_SUCCESS : NRF_ERROR_INVALID_PARAM;
  1405. }
  1406. uint32_t nrf_esb_reuse_pid(uint8_t pipe)
  1407. {
  1408. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1409. VERIFY_TRUE(pipe < NRF_ESB_PIPE_COUNT, NRF_ERROR_INVALID_PARAM);
  1410. m_pids[pipe] = (m_pids[pipe] + NRF_ESB_PID_MAX) % (NRF_ESB_PID_MAX + 1);
  1411. return NRF_SUCCESS;
  1412. }
  1413. #ifdef NRF52832_XXAA
  1414. // Workaround neccessary on nRF52832 Rev. 1.
  1415. void NRF_ESB_BUGFIX_TIMER_IRQHandler(void)
  1416. {
  1417. if (NRF_ESB_BUGFIX_TIMER->EVENTS_COMPARE[0])
  1418. {
  1419. NRF_ESB_BUGFIX_TIMER->EVENTS_COMPARE[0] = 0;
  1420. // If the timeout timer fires and we are in the PTX receive ACK state, disable the radio
  1421. if (m_nrf_esb_mainstate == NRF_ESB_STATE_PTX_RX_ACK)
  1422. {
  1423. NRF_RADIO->TASKS_DISABLE = 1;
  1424. }
  1425. }
  1426. }
  1427. #endif