nrf_esb.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  1. /**
  2. * Copyright (c) 2016 - 2019, 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. m_nrf_esb_mainstate == NRF_ESB_STATE_PRX_SEND_ACK)
  964. {
  965. NRF_RADIO->SHORTS = 0;
  966. NRF_RADIO->INTENCLR = 0xFFFFFFFF;
  967. on_radio_disabled = NULL;
  968. NRF_RADIO->EVENTS_DISABLED = 0;
  969. NRF_RADIO->TASKS_DISABLE = 1;
  970. while (NRF_RADIO->EVENTS_DISABLED == 0);
  971. m_nrf_esb_mainstate = NRF_ESB_STATE_IDLE;
  972. return NRF_SUCCESS;
  973. }
  974. return NRF_ESB_ERROR_NOT_IN_RX_MODE;
  975. }
  976. uint32_t nrf_esb_flush_tx(void)
  977. {
  978. VERIFY_TRUE(m_esb_initialized, NRF_ERROR_INVALID_STATE);
  979. DISABLE_RF_IRQ();
  980. m_tx_fifo.count = 0;
  981. m_tx_fifo.entry_point = 0;
  982. m_tx_fifo.exit_point = 0;
  983. ENABLE_RF_IRQ();
  984. return NRF_SUCCESS;
  985. }
  986. uint32_t nrf_esb_pop_tx(void)
  987. {
  988. VERIFY_TRUE(m_esb_initialized, NRF_ERROR_INVALID_STATE);
  989. VERIFY_TRUE(m_tx_fifo.count > 0, NRF_ERROR_BUFFER_EMPTY);
  990. DISABLE_RF_IRQ();
  991. if (--m_tx_fifo.entry_point >= NRF_ESB_TX_FIFO_SIZE)
  992. {
  993. m_tx_fifo.entry_point = 0;
  994. }
  995. m_tx_fifo.count--;
  996. ENABLE_RF_IRQ();
  997. return NRF_SUCCESS;
  998. }
  999. uint32_t nrf_esb_flush_rx(void)
  1000. {
  1001. VERIFY_TRUE(m_esb_initialized, NRF_ERROR_INVALID_STATE);
  1002. DISABLE_RF_IRQ();
  1003. m_rx_fifo.count = 0;
  1004. m_rx_fifo.entry_point = 0;
  1005. m_rx_fifo.exit_point = 0;
  1006. memset(m_rx_pipe_info, 0, sizeof(m_rx_pipe_info));
  1007. ENABLE_RF_IRQ();
  1008. return NRF_SUCCESS;
  1009. }
  1010. uint32_t nrf_esb_set_address_length(uint8_t length)
  1011. {
  1012. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1013. VERIFY_TRUE(length > 2 && length < 6, NRF_ERROR_INVALID_PARAM);
  1014. #ifdef NRF52832_XXAA
  1015. uint32_t base_address_mask = length == 5 ? 0xFFFF0000 : 0xFF000000;
  1016. if ((NRF_FICR->INFO.VARIANT & 0x0000FF00) == 0x00004200) //Check if the device is an nRF52832 Rev. 1.
  1017. {
  1018. /*
  1019. Workaround for nRF52832 Rev 1 Errata 107
  1020. Check if pipe 0 or pipe 1-7 has a 'zero address'.
  1021. Avoid using access addresses in the following pattern (where X is don't care):
  1022. ADDRLEN=5
  1023. BASE0 = 0x0000XXXX, PREFIX0 = 0xXXXXXX00
  1024. BASE1 = 0x0000XXXX, PREFIX0 = 0xXXXX00XX
  1025. BASE1 = 0x0000XXXX, PREFIX0 = 0xXX00XXXX
  1026. BASE1 = 0x0000XXXX, PREFIX0 = 0x00XXXXXX
  1027. BASE1 = 0x0000XXXX, PREFIX1 = 0xXXXXXX00
  1028. BASE1 = 0x0000XXXX, PREFIX1 = 0xXXXX00XX
  1029. BASE1 = 0x0000XXXX, PREFIX1 = 0xXX00XXXX
  1030. BASE1 = 0x0000XXXX, PREFIX1 = 0x00XXXXXX
  1031. ADDRLEN=4
  1032. BASE0 = 0x00XXXXXX, PREFIX0 = 0xXXXXXX00
  1033. BASE1 = 0x00XXXXXX, PREFIX0 = 0xXXXX00XX
  1034. BASE1 = 0x00XXXXXX, PREFIX0 = 0xXX00XXXX
  1035. BASE1 = 0x00XXXXXX, PREFIX0 = 0x00XXXXXX
  1036. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXXXXXX00
  1037. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXXXX00XX
  1038. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXX00XXXX
  1039. BASE1 = 0x00XXXXXX, PREFIX1 = 0x00XXXXXX
  1040. */
  1041. if ((NRF_RADIO->BASE0 & base_address_mask) == 0 && (NRF_RADIO->PREFIX0 & 0x000000FF) == 0)
  1042. {
  1043. return NRF_ERROR_INVALID_PARAM;
  1044. }
  1045. if ((NRF_RADIO->BASE1 & base_address_mask) == 0 && ((NRF_RADIO->PREFIX0 & 0x0000FF00) == 0 ||(NRF_RADIO->PREFIX0 & 0x00FF0000) == 0 || (NRF_RADIO->PREFIX0 & 0xFF000000) == 0 ||
  1046. (NRF_RADIO->PREFIX1 & 0xFF000000) == 0 || (NRF_RADIO->PREFIX1 & 0x00FF0000) == 0 ||(NRF_RADIO->PREFIX1 & 0x0000FF00) == 0 || (NRF_RADIO->PREFIX1 & 0x000000FF) == 0))
  1047. {
  1048. return NRF_ERROR_INVALID_PARAM;
  1049. }
  1050. }
  1051. #endif
  1052. m_esb_addr.addr_length = length;
  1053. update_rf_payload_format(m_config_local.payload_length);
  1054. #ifdef NRF52832_XXAA
  1055. if ((NRF_FICR->INFO.VARIANT & 0x0000FF00) == 0x00004500) //Check if the device is an nRF52832 Rev. 2.
  1056. {
  1057. return apply_address_workarounds();
  1058. }
  1059. else
  1060. {
  1061. return NRF_SUCCESS;
  1062. }
  1063. #else
  1064. return NRF_SUCCESS;
  1065. #endif
  1066. }
  1067. uint32_t nrf_esb_set_base_address_0(uint8_t const * p_addr)
  1068. {
  1069. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1070. VERIFY_PARAM_NOT_NULL(p_addr);
  1071. #ifdef NRF52832_XXAA
  1072. if ((NRF_FICR->INFO.VARIANT & 0x0000FF00) == 0x00004200) //Check if the device is an nRF52832 Rev. 1.
  1073. {
  1074. /*
  1075. Workaround for nRF52832 Rev 1 Errata 107
  1076. Check if pipe 0 or pipe 1-7 has a 'zero address'.
  1077. Avoid using access addresses in the following pattern (where X is don't care):
  1078. ADDRLEN=5
  1079. BASE0 = 0x0000XXXX, PREFIX0 = 0xXXXXXX00
  1080. BASE1 = 0x0000XXXX, PREFIX0 = 0xXXXX00XX
  1081. BASE1 = 0x0000XXXX, PREFIX0 = 0xXX00XXXX
  1082. BASE1 = 0x0000XXXX, PREFIX0 = 0x00XXXXXX
  1083. BASE1 = 0x0000XXXX, PREFIX1 = 0xXXXXXX00
  1084. BASE1 = 0x0000XXXX, PREFIX1 = 0xXXXX00XX
  1085. BASE1 = 0x0000XXXX, PREFIX1 = 0xXX00XXXX
  1086. BASE1 = 0x0000XXXX, PREFIX1 = 0x00XXXXXX
  1087. ADDRLEN=4
  1088. BASE0 = 0x00XXXXXX, PREFIX0 = 0xXXXXXX00
  1089. BASE1 = 0x00XXXXXX, PREFIX0 = 0xXXXX00XX
  1090. BASE1 = 0x00XXXXXX, PREFIX0 = 0xXX00XXXX
  1091. BASE1 = 0x00XXXXXX, PREFIX0 = 0x00XXXXXX
  1092. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXXXXXX00
  1093. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXXXX00XX
  1094. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXX00XXXX
  1095. BASE1 = 0x00XXXXXX, PREFIX1 = 0x00XXXXXX
  1096. */
  1097. uint32_t base_address_mask = m_esb_addr.addr_length == 5 ? 0xFFFF0000 : 0xFF000000;
  1098. if ((addr_conv(p_addr) & base_address_mask) == 0 && (NRF_RADIO->PREFIX0 & 0x000000FF) == 0)
  1099. {
  1100. return NRF_ERROR_INVALID_PARAM;
  1101. }
  1102. }
  1103. #endif
  1104. memcpy(m_esb_addr.base_addr_p0, p_addr, 4);
  1105. update_radio_addresses(NRF_ESB_ADDR_UPDATE_MASK_BASE0);
  1106. #ifdef NRF52832_XXAA
  1107. return apply_address_workarounds();
  1108. #else
  1109. return NRF_SUCCESS;
  1110. #endif
  1111. }
  1112. uint32_t nrf_esb_set_base_address_1(uint8_t const * p_addr)
  1113. {
  1114. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1115. VERIFY_PARAM_NOT_NULL(p_addr);
  1116. #ifdef NRF52832_XXAA
  1117. if ((NRF_FICR->INFO.VARIANT & 0x0000FF00) == 0x00004200) //Check if the device is an nRF52832 Rev. 1.
  1118. {
  1119. /*
  1120. Workaround for nRF52832 Rev 1 Errata 107
  1121. Check if pipe 0 or pipe 1-7 has a 'zero address'.
  1122. Avoid using access addresses in the following pattern (where X is don't care):
  1123. ADDRLEN=5
  1124. BASE0 = 0x0000XXXX, PREFIX0 = 0xXXXXXX00
  1125. BASE1 = 0x0000XXXX, PREFIX0 = 0xXXXX00XX
  1126. BASE1 = 0x0000XXXX, PREFIX0 = 0xXX00XXXX
  1127. BASE1 = 0x0000XXXX, PREFIX0 = 0x00XXXXXX
  1128. BASE1 = 0x0000XXXX, PREFIX1 = 0xXXXXXX00
  1129. BASE1 = 0x0000XXXX, PREFIX1 = 0xXXXX00XX
  1130. BASE1 = 0x0000XXXX, PREFIX1 = 0xXX00XXXX
  1131. BASE1 = 0x0000XXXX, PREFIX1 = 0x00XXXXXX
  1132. ADDRLEN=4
  1133. BASE0 = 0x00XXXXXX, PREFIX0 = 0xXXXXXX00
  1134. BASE1 = 0x00XXXXXX, PREFIX0 = 0xXXXX00XX
  1135. BASE1 = 0x00XXXXXX, PREFIX0 = 0xXX00XXXX
  1136. BASE1 = 0x00XXXXXX, PREFIX0 = 0x00XXXXXX
  1137. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXXXXXX00
  1138. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXXXX00XX
  1139. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXX00XXXX
  1140. BASE1 = 0x00XXXXXX, PREFIX1 = 0x00XXXXXX
  1141. */
  1142. uint32_t base_address_mask = m_esb_addr.addr_length == 5 ? 0xFFFF0000 : 0xFF000000;
  1143. if ((addr_conv(p_addr) & base_address_mask) == 0 &&
  1144. ((NRF_RADIO->PREFIX0 & 0x0000FF00) == 0 ||(NRF_RADIO->PREFIX0 & 0x00FF0000) == 0 ||
  1145. (NRF_RADIO->PREFIX0 & 0xFF000000) == 0 || (NRF_RADIO->PREFIX1 & 0xFF000000) == 0 ||
  1146. (NRF_RADIO->PREFIX1 & 0x00FF0000) == 0 ||(NRF_RADIO->PREFIX1 & 0x0000FF00) == 0 ||
  1147. (NRF_RADIO->PREFIX1 & 0x000000FF) == 0))
  1148. {
  1149. return NRF_ERROR_INVALID_PARAM;
  1150. }
  1151. }
  1152. #endif
  1153. memcpy(m_esb_addr.base_addr_p1, p_addr, 4);
  1154. update_radio_addresses(NRF_ESB_ADDR_UPDATE_MASK_BASE1);
  1155. #ifdef NRF52832_XXAA
  1156. return apply_address_workarounds();
  1157. #else
  1158. return NRF_SUCCESS;
  1159. #endif
  1160. }
  1161. uint32_t nrf_esb_set_prefixes(uint8_t const * p_prefixes, uint8_t num_pipes)
  1162. {
  1163. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1164. VERIFY_PARAM_NOT_NULL(p_prefixes);
  1165. VERIFY_TRUE(num_pipes <= NRF_ESB_PIPE_COUNT, NRF_ERROR_INVALID_PARAM);
  1166. #ifdef NRF52832_XXAA
  1167. if ((NRF_FICR->INFO.VARIANT & 0x0000FF00) == 0x00004200) //Check if the device is an nRF52832 Rev. 1.
  1168. {
  1169. /*
  1170. Workaround for nRF52832 Rev 1 Errata 107
  1171. Check if pipe 0 or pipe 1-7 has a 'zero address'.
  1172. Avoid using access addresses in the following pattern (where X is don't care):
  1173. ADDRLEN=5
  1174. BASE0 = 0x0000XXXX, PREFIX0 = 0xXXXXXX00
  1175. BASE1 = 0x0000XXXX, PREFIX0 = 0xXXXX00XX
  1176. BASE1 = 0x0000XXXX, PREFIX0 = 0xXX00XXXX
  1177. BASE1 = 0x0000XXXX, PREFIX0 = 0x00XXXXXX
  1178. BASE1 = 0x0000XXXX, PREFIX1 = 0xXXXXXX00
  1179. BASE1 = 0x0000XXXX, PREFIX1 = 0xXXXX00XX
  1180. BASE1 = 0x0000XXXX, PREFIX1 = 0xXX00XXXX
  1181. BASE1 = 0x0000XXXX, PREFIX1 = 0x00XXXXXX
  1182. ADDRLEN=4
  1183. BASE0 = 0x00XXXXXX, PREFIX0 = 0xXXXXXX00
  1184. BASE1 = 0x00XXXXXX, PREFIX0 = 0xXXXX00XX
  1185. BASE1 = 0x00XXXXXX, PREFIX0 = 0xXX00XXXX
  1186. BASE1 = 0x00XXXXXX, PREFIX0 = 0x00XXXXXX
  1187. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXXXXXX00
  1188. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXXXX00XX
  1189. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXX00XXXX
  1190. BASE1 = 0x00XXXXXX, PREFIX1 = 0x00XXXXXX
  1191. */
  1192. uint32_t base_address_mask = m_esb_addr.addr_length == 5 ? 0xFFFF0000 : 0xFF000000;
  1193. if (num_pipes >= 1 && (NRF_RADIO->BASE0 & base_address_mask) == 0 && p_prefixes[0] == 0)
  1194. {
  1195. return NRF_ERROR_INVALID_PARAM;
  1196. }
  1197. if ((NRF_RADIO->BASE1 & base_address_mask) == 0)
  1198. {
  1199. for (uint8_t i = 1; i < num_pipes; i++)
  1200. {
  1201. if (p_prefixes[i] == 0)
  1202. {
  1203. return NRF_ERROR_INVALID_PARAM;
  1204. }
  1205. }
  1206. }
  1207. }
  1208. #endif
  1209. memcpy(m_esb_addr.pipe_prefixes, p_prefixes, num_pipes);
  1210. m_esb_addr.num_pipes = num_pipes;
  1211. m_esb_addr.rx_pipes_enabled = BIT_MASK_UINT_8(num_pipes);
  1212. update_radio_addresses(NRF_ESB_ADDR_UPDATE_MASK_PREFIX);
  1213. #ifdef NRF52832_XXAA
  1214. return apply_address_workarounds();
  1215. #else
  1216. return NRF_SUCCESS;
  1217. #endif
  1218. }
  1219. uint32_t nrf_esb_update_prefix(uint8_t pipe, uint8_t prefix)
  1220. {
  1221. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1222. VERIFY_TRUE(pipe < NRF_ESB_PIPE_COUNT, NRF_ERROR_INVALID_PARAM);
  1223. #ifdef NRF52832_XXAA
  1224. if ((NRF_FICR->INFO.VARIANT & 0x0000FF00) == 0x00004200) //Check if the device is an nRF52832 Rev. 1.
  1225. {
  1226. /*
  1227. Workaround for nRF52832 Rev 1 Errata 107
  1228. Check if pipe 0 or pipe 1-7 has a 'zero address'.
  1229. Avoid using access addresses in the following pattern (where X is don't care):
  1230. ADDRLEN=5
  1231. BASE0 = 0x0000XXXX, PREFIX0 = 0xXXXXXX00
  1232. BASE1 = 0x0000XXXX, PREFIX0 = 0xXXXX00XX
  1233. BASE1 = 0x0000XXXX, PREFIX0 = 0xXX00XXXX
  1234. BASE1 = 0x0000XXXX, PREFIX0 = 0x00XXXXXX
  1235. BASE1 = 0x0000XXXX, PREFIX1 = 0xXXXXXX00
  1236. BASE1 = 0x0000XXXX, PREFIX1 = 0xXXXX00XX
  1237. BASE1 = 0x0000XXXX, PREFIX1 = 0xXX00XXXX
  1238. BASE1 = 0x0000XXXX, PREFIX1 = 0x00XXXXXX
  1239. ADDRLEN=4
  1240. BASE0 = 0x00XXXXXX, PREFIX0 = 0xXXXXXX00
  1241. BASE1 = 0x00XXXXXX, PREFIX0 = 0xXXXX00XX
  1242. BASE1 = 0x00XXXXXX, PREFIX0 = 0xXX00XXXX
  1243. BASE1 = 0x00XXXXXX, PREFIX0 = 0x00XXXXXX
  1244. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXXXXXX00
  1245. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXXXX00XX
  1246. BASE1 = 0x00XXXXXX, PREFIX1 = 0xXX00XXXX
  1247. BASE1 = 0x00XXXXXX, PREFIX1 = 0x00XXXXXX
  1248. */
  1249. uint32_t base_address_mask = m_esb_addr.addr_length == 5 ? 0xFFFF0000 : 0xFF000000;
  1250. if (pipe == 0)
  1251. {
  1252. if ((NRF_RADIO->BASE0 & base_address_mask) == 0 && prefix == 0)
  1253. {
  1254. return NRF_ERROR_INVALID_PARAM;
  1255. }
  1256. }
  1257. else
  1258. {
  1259. if ((NRF_RADIO->BASE1 & base_address_mask) == 0 && prefix == 0)
  1260. {
  1261. return NRF_ERROR_INVALID_PARAM;
  1262. }
  1263. }
  1264. }
  1265. #endif
  1266. m_esb_addr.pipe_prefixes[pipe] = prefix;
  1267. update_radio_addresses(NRF_ESB_ADDR_UPDATE_MASK_PREFIX);
  1268. #ifdef NRF52832_XXAA
  1269. return apply_address_workarounds();
  1270. #else
  1271. return NRF_SUCCESS;
  1272. #endif
  1273. }
  1274. uint32_t nrf_esb_enable_pipes(uint8_t enable_mask)
  1275. {
  1276. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1277. VERIFY_TRUE((enable_mask | BIT_MASK_UINT_8(NRF_ESB_PIPE_COUNT)) == BIT_MASK_UINT_8(NRF_ESB_PIPE_COUNT), NRF_ERROR_INVALID_PARAM);
  1278. m_esb_addr.rx_pipes_enabled = enable_mask;
  1279. #ifdef NRF52832_XXAA
  1280. return apply_address_workarounds();
  1281. #else
  1282. return NRF_SUCCESS;
  1283. #endif
  1284. }
  1285. uint32_t nrf_esb_set_rf_channel(uint32_t channel)
  1286. {
  1287. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1288. VERIFY_TRUE(channel <= 100, NRF_ERROR_INVALID_PARAM);
  1289. m_esb_addr.rf_channel = channel;
  1290. return NRF_SUCCESS;
  1291. }
  1292. uint32_t nrf_esb_get_rf_channel(uint32_t * p_channel)
  1293. {
  1294. VERIFY_PARAM_NOT_NULL(p_channel);
  1295. *p_channel = m_esb_addr.rf_channel;
  1296. return NRF_SUCCESS;
  1297. }
  1298. uint32_t nrf_esb_set_tx_power(nrf_esb_tx_power_t tx_output_power)
  1299. {
  1300. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1301. if ( m_config_local.tx_output_power != tx_output_power )
  1302. {
  1303. m_config_local.tx_output_power = tx_output_power;
  1304. update_radio_tx_power();
  1305. }
  1306. return NRF_SUCCESS;
  1307. }
  1308. uint32_t nrf_esb_set_retransmit_delay(uint16_t delay)
  1309. {
  1310. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1311. VERIFY_TRUE(delay >= NRF_ESB_RETRANSMIT_DELAY_MIN, NRF_ERROR_INVALID_PARAM);
  1312. m_config_local.retransmit_delay = delay;
  1313. return NRF_SUCCESS;
  1314. }
  1315. uint32_t nrf_esb_set_retransmit_count(uint16_t count)
  1316. {
  1317. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1318. m_config_local.retransmit_count = count;
  1319. return NRF_SUCCESS;
  1320. }
  1321. uint32_t nrf_esb_set_bitrate(nrf_esb_bitrate_t bitrate)
  1322. {
  1323. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1324. m_config_local.bitrate = bitrate;
  1325. return update_radio_bitrate() ? NRF_SUCCESS : NRF_ERROR_INVALID_PARAM;
  1326. }
  1327. uint32_t nrf_esb_reuse_pid(uint8_t pipe)
  1328. {
  1329. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  1330. VERIFY_TRUE(pipe < NRF_ESB_PIPE_COUNT, NRF_ERROR_INVALID_PARAM);
  1331. m_pids[pipe] = (m_pids[pipe] + NRF_ESB_PID_MAX) % (NRF_ESB_PID_MAX + 1);
  1332. return NRF_SUCCESS;
  1333. }
  1334. #ifdef NRF52832_XXAA
  1335. // Workaround neccessary on nRF52832 Rev. 1.
  1336. void NRF_ESB_BUGFIX_TIMER_IRQHandler(void)
  1337. {
  1338. if (NRF_ESB_BUGFIX_TIMER->EVENTS_COMPARE[0])
  1339. {
  1340. NRF_ESB_BUGFIX_TIMER->EVENTS_COMPARE[0] = 0;
  1341. // If the timeout timer fires and we are in the PTX receive ACK state, disable the radio
  1342. if (m_nrf_esb_mainstate == NRF_ESB_STATE_PTX_RX_ACK)
  1343. {
  1344. NRF_RADIO->TASKS_DISABLE = 1;
  1345. }
  1346. }
  1347. }
  1348. #endif