nrf_esb.c 55 KB

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