nrf_esb.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. /* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "nrf_error.h"
  13. #include "nrf_esb.h"
  14. #include "nrf_esb_error_codes.h"
  15. #include "nrf_gpio.h"
  16. #include <string.h>
  17. #include <stddef.h>
  18. #include "sdk_common.h"
  19. #include "sdk_macros.h"
  20. #include "app_util.h"
  21. #include "nrf_log.h"
  22. #define BIT_MASK_UINT_8(x) (0xFF >> (8 - (x)))
  23. #define NRF_ESB_PIPE_COUNT 9
  24. // Constant parameters
  25. #define RX_WAIT_FOR_ACK_TIMEOUT_US_2MBPS (48) /**< 2MBit RX wait for ack timout value. Smallest reliable value - 43 */
  26. #define RX_WAIT_FOR_ACK_TIMEOUT_US_1MBPS (64) /**< 1MBit RX wait for ack timout value. Smallest reliable value - 59 */
  27. #define RX_WAIT_FOR_ACK_TIMEOUT_US_250KBPS (250) /**< 250KBit RX RX wait for ack timout value. */
  28. #define RX_WAIT_FOR_ACK_TIMEOUT_US_1MBPS_BLE (64) /**< 1MBit RX wait for ack timeout (combined with BLE). */
  29. // Interrupt flags
  30. #define NRF_ESB_INT_TX_SUCCESS_MSK 0x01 /**< Interrupt mask value for TX success. */
  31. #define NRF_ESB_INT_TX_FAILED_MSK 0x02 /**< Interrupt mask value for TX failed*/
  32. #define NRF_ESB_INT_RX_DATA_RECEIVED_MSK 0x04 /**< Interrupt mask value for RX_DR*/
  33. #define NRF_ESB_PID_RESET_VALUE 0xFF /**< Invalid PID value which is guaranteed to not colide with any valid PID value. */
  34. #define NRF_ESB_PID_MAX 3 /**< Maximum value for PID. */
  35. #define NRF_ESB_CRC_RESET_VALUE 0xFFFF /**< CRC reset value*/
  36. // Internal Enhanced ShockBurst module state.
  37. typedef enum {
  38. NRF_ESB_STATE_IDLE, /**< Module idle. */
  39. NRF_ESB_STATE_PTX_TX, /**< Module transmitting without ack. */
  40. NRF_ESB_STATE_PTX_TX_ACK, /**< Module transmitting with ack. */
  41. NRF_ESB_STATE_PTX_RX_ACK, /**< Module transmitting with ack and reception of payload with the ack response. */
  42. NRF_ESB_STATE_PRX, /**< Module receiving packets without ack. */
  43. NRF_ESB_STATE_PRX_SEND_ACK, /**< Module transmitting ack in RX mode. */
  44. } nrf_esb_mainstate_t;
  45. #define DISABLE_RF_IRQ() NVIC_DisableIRQ(RADIO_IRQn)
  46. #define ENABLE_RF_IRQ() NVIC_EnableIRQ(RADIO_IRQn)
  47. #define RADIO_SHORTS_COMMON ( RADIO_SHORTS_READY_START_Msk | RADIO_SHORTS_END_DISABLE_Msk | \
  48. RADIO_SHORTS_ADDRESS_RSSISTART_Msk | RADIO_SHORTS_DISABLED_RSSISTOP_Msk )
  49. #define VERIFY_PAYLOAD_LENGTH(p) \
  50. do \
  51. { \
  52. if(p->length == 0 || \
  53. p->length > NRF_ESB_MAX_PAYLOAD_LENGTH || \
  54. (m_config_local.protocol == NRF_ESB_PROTOCOL_ESB && \
  55. p->length > m_config_local.payload_length)) \
  56. { \
  57. return NRF_ERROR_INVALID_LENGTH; \
  58. } \
  59. }while(0)
  60. //Structure holding pipe info PID and CRC and ack payload.
  61. typedef struct
  62. {
  63. uint16_t m_crc;
  64. uint8_t m_pid;
  65. uint8_t m_ack_payload;
  66. } pipe_info_t;
  67. // First in first out queue of payloads to be transmitted.
  68. typedef struct
  69. {
  70. nrf_esb_payload_t * p_payload[NRF_ESB_TX_FIFO_SIZE]; /**< Pointer to the actual queue. */
  71. uint32_t entry_point; /**< Current start of queue. */
  72. uint32_t exit_point; /**< Current end of queue. */
  73. uint32_t count; /**< Current number of elements in the queue. */
  74. } nrf_esb_payload_tx_fifo_t;
  75. // First in first out queue of received payloads.
  76. typedef struct
  77. {
  78. nrf_esb_payload_t * p_payload[NRF_ESB_RX_FIFO_SIZE]; /**< Pointer to the actual queue. */
  79. uint32_t entry_point; /**< Current start of queue. */
  80. uint32_t exit_point; /**< Current end of queue. */
  81. uint32_t count; /**< Current number of elements in the queue. */
  82. } nrf_esb_payload_rx_fifo_t;
  83. // Module state
  84. static bool m_esb_initialized = false;
  85. static nrf_esb_mainstate_t m_nrf_esb_mainstate = NRF_ESB_STATE_IDLE;
  86. static nrf_esb_payload_t * mp_current_payload;
  87. static nrf_esb_event_handler_t m_event_handler;
  88. // address parameters
  89. static nrf_esb_address_t m_esb_addr = NRF_ESB_ADDR_DEFAULT;
  90. // RF parameters
  91. static nrf_esb_config_t m_config_local;
  92. // TX FIFO
  93. static nrf_esb_payload_t m_tx_fifo_payload[NRF_ESB_TX_FIFO_SIZE];
  94. static nrf_esb_payload_tx_fifo_t m_tx_fifo;
  95. // RX FIFO
  96. static nrf_esb_payload_t m_rx_fifo_payload[NRF_ESB_RX_FIFO_SIZE];
  97. static nrf_esb_payload_rx_fifo_t m_rx_fifo;
  98. // Payload buffers
  99. static uint8_t m_tx_payload_buffer[NRF_ESB_MAX_PAYLOAD_LENGTH + 2];
  100. static uint8_t m_rx_payload_buffer[NRF_ESB_MAX_PAYLOAD_LENGTH + 2];
  101. // Run time variables
  102. static volatile uint32_t m_interrupt_flags = 0;
  103. static uint8_t m_pids[NRF_ESB_PIPE_COUNT];
  104. static pipe_info_t m_rx_pipe_info[NRF_ESB_PIPE_COUNT];
  105. static volatile uint32_t m_retransmits_remaining;
  106. static volatile uint32_t m_last_tx_attempts;
  107. static volatile uint32_t m_wait_for_ack_timeout_us;
  108. // These function pointers are changed dynamically, depending on protocol configuration and state.
  109. static void (*on_radio_disabled)(void) = 0;
  110. static void (*on_radio_end)(void) = 0;
  111. static void (*update_rf_payload_format)(uint32_t payload_length) = 0;
  112. // The following functions are assigned to the function pointers above.
  113. static void on_radio_disabled_tx_noack(void);
  114. static void on_radio_disabled_tx(void);
  115. static void on_radio_disabled_tx_wait_for_ack(void);
  116. static void on_radio_disabled_rx(void);
  117. static void on_radio_disabled_rx_ack(void);
  118. #define NRF_ESB_ADDR_UPDATE_MASK_BASE0 (1 << 0) /*< Mask value to signal updating BASE0 radio address. */
  119. #define NRF_ESB_ADDR_UPDATE_MASK_BASE1 (1 << 1) /*< Mask value to signal updating BASE1 radio address. */
  120. #define NRF_ESB_ADDR_UPDATE_MASK_PREFIX (1 << 2) /*< Mask value to signal updating radio prefixes */
  121. // Function to do bytewise bit-swap on a unsigned 32 bit value
  122. static uint32_t bytewise_bit_swap(uint8_t const * p_inp)
  123. {
  124. uint32_t inp = (*(uint32_t*)p_inp);
  125. #if defined(NRF52)
  126. return __REV((uint32_t)__RBIT(inp)); //lint -esym(628, __rev) -esym(526, __rev) -esym(628, __rbit) -esym(526, __rbit) */
  127. #else
  128. inp = (inp & 0xF0F0F0F0) >> 4 | (inp & 0x0F0F0F0F) << 4;
  129. inp = (inp & 0xCCCCCCCC) >> 2 | (inp & 0x33333333) << 2;
  130. inp = (inp & 0xAAAAAAAA) >> 1 | (inp & 0x55555555) << 1;
  131. return inp;
  132. #endif
  133. }
  134. // Internal function to convert base addresses from nRF24L type addressing to nRF51 type addressing
  135. static uint32_t addr_conv(uint8_t const* p_addr)
  136. {
  137. return __REV(bytewise_bit_swap(p_addr)); //lint -esym(628, __rev) -esym(526, __rev) */
  138. }
  139. static void update_rf_payload_format_esb_dpl(uint32_t payload_length)
  140. {
  141. #if (NRF_ESB_MAX_PAYLOAD_LENGTH <= 32)
  142. // Using 6 bits for length
  143. NRF_RADIO->PCNF0 = (0 << RADIO_PCNF0_S0LEN_Pos) |
  144. (6 << RADIO_PCNF0_LFLEN_Pos) |
  145. (3 << RADIO_PCNF0_S1LEN_Pos) ;
  146. #else
  147. // Using 8 bits for length
  148. NRF_RADIO->PCNF0 = (0 << RADIO_PCNF0_S0LEN_Pos) |
  149. (8 << RADIO_PCNF0_LFLEN_Pos) |
  150. (3 << RADIO_PCNF0_S1LEN_Pos) ;
  151. #endif
  152. NRF_RADIO->PCNF1 = (RADIO_PCNF1_WHITEEN_Disabled << RADIO_PCNF1_WHITEEN_Pos) |
  153. (RADIO_PCNF1_ENDIAN_Big << RADIO_PCNF1_ENDIAN_Pos) |
  154. ((m_esb_addr.addr_length - 1) << RADIO_PCNF1_BALEN_Pos) |
  155. (0 << RADIO_PCNF1_STATLEN_Pos) |
  156. (NRF_ESB_MAX_PAYLOAD_LENGTH << RADIO_PCNF1_MAXLEN_Pos);
  157. }
  158. static void update_rf_payload_format_esb(uint32_t payload_length)
  159. {
  160. NRF_RADIO->PCNF0 = (1 << RADIO_PCNF0_S0LEN_Pos) |
  161. (0 << RADIO_PCNF0_LFLEN_Pos) |
  162. (1 << RADIO_PCNF0_S1LEN_Pos);
  163. NRF_RADIO->PCNF1 = (RADIO_PCNF1_WHITEEN_Disabled << RADIO_PCNF1_WHITEEN_Pos) |
  164. (RADIO_PCNF1_ENDIAN_Big << RADIO_PCNF1_ENDIAN_Pos) |
  165. ((m_esb_addr.addr_length - 1) << RADIO_PCNF1_BALEN_Pos) |
  166. (payload_length << RADIO_PCNF1_STATLEN_Pos) |
  167. (payload_length << RADIO_PCNF1_MAXLEN_Pos);
  168. }
  169. static void update_radio_addresses(uint8_t update_mask)
  170. {
  171. if ((update_mask & NRF_ESB_ADDR_UPDATE_MASK_BASE0) != 0)
  172. {
  173. NRF_RADIO->BASE0 = addr_conv(m_esb_addr.base_addr_p0);
  174. }
  175. if ((update_mask & NRF_ESB_ADDR_UPDATE_MASK_BASE1) != 0)
  176. {
  177. NRF_RADIO->BASE1 = addr_conv(m_esb_addr.base_addr_p1);
  178. }
  179. if ((update_mask & NRF_ESB_ADDR_UPDATE_MASK_PREFIX) != 0)
  180. {
  181. NRF_RADIO->PREFIX0 = bytewise_bit_swap(&m_esb_addr.pipe_prefixes[0]);
  182. NRF_RADIO->PREFIX1 = bytewise_bit_swap(&m_esb_addr.pipe_prefixes[4]);
  183. }
  184. }
  185. static void update_radio_tx_power()
  186. {
  187. NRF_RADIO->TXPOWER = m_config_local.tx_output_power << RADIO_TXPOWER_TXPOWER_Pos;
  188. }
  189. static void update_radio_bitrate()
  190. {
  191. NRF_RADIO->MODE = m_config_local.bitrate << RADIO_MODE_MODE_Pos;
  192. switch (m_config_local.bitrate)
  193. {
  194. case NRF_ESB_BITRATE_2MBPS:
  195. m_wait_for_ack_timeout_us = RX_WAIT_FOR_ACK_TIMEOUT_US_2MBPS;
  196. break;
  197. case NRF_ESB_BITRATE_1MBPS:
  198. m_wait_for_ack_timeout_us = RX_WAIT_FOR_ACK_TIMEOUT_US_1MBPS;
  199. break;
  200. case NRF_ESB_BITRATE_250KBPS:
  201. m_wait_for_ack_timeout_us = RX_WAIT_FOR_ACK_TIMEOUT_US_250KBPS;
  202. break;
  203. case NRF_ESB_BITRATE_1MBPS_BLE:
  204. m_wait_for_ack_timeout_us = RX_WAIT_FOR_ACK_TIMEOUT_US_1MBPS_BLE;
  205. break;
  206. default:
  207. // Should not be reached
  208. break;
  209. }
  210. }
  211. static void update_radio_protocol()
  212. {
  213. switch (m_config_local.protocol)
  214. {
  215. case NRF_ESB_PROTOCOL_ESB_DPL:
  216. update_rf_payload_format = update_rf_payload_format_esb_dpl;
  217. break;
  218. case NRF_ESB_PROTOCOL_ESB:
  219. update_rf_payload_format = update_rf_payload_format_esb;
  220. break;
  221. default:
  222. // Should not be reached
  223. break;
  224. }
  225. }
  226. static void update_radio_crc()
  227. {
  228. NRF_RADIO->CRCCNF = m_config_local.crc << RADIO_CRCCNF_LEN_Pos;
  229. if (m_config_local.crc == RADIO_CRCCNF_LEN_Two)
  230. {
  231. NRF_RADIO->CRCINIT = 0xFFFFUL; // Initial value
  232. NRF_RADIO->CRCPOLY = 0x11021UL; // CRC poly: x^16+x^12^x^5+1
  233. }
  234. else if (m_config_local.crc == RADIO_CRCCNF_LEN_One)
  235. {
  236. NRF_RADIO->CRCINIT = 0xFFUL; // Initial value
  237. NRF_RADIO->CRCPOLY = 0x107UL; // CRC poly: x^8+x^2^x^1+1
  238. }
  239. }
  240. static void update_radio_parameters()
  241. {
  242. update_radio_tx_power();
  243. update_radio_bitrate();
  244. update_radio_protocol();
  245. update_radio_crc();
  246. update_rf_payload_format(m_config_local.payload_length);
  247. }
  248. static void reset_fifos()
  249. {
  250. m_tx_fifo.entry_point = 0;
  251. m_tx_fifo.exit_point = 0;
  252. m_tx_fifo.count = 0;
  253. m_rx_fifo.entry_point = 0;
  254. m_rx_fifo.exit_point = 0;
  255. m_rx_fifo.count = 0;
  256. }
  257. static void initialize_fifos()
  258. {
  259. reset_fifos();
  260. for (int i = 0; i < NRF_ESB_TX_FIFO_SIZE; i++)
  261. {
  262. m_tx_fifo.p_payload[i] = &m_tx_fifo_payload[i];
  263. }
  264. for (int i = 0; i < NRF_ESB_RX_FIFO_SIZE; i++)
  265. {
  266. m_rx_fifo.p_payload[i] = &m_rx_fifo_payload[i];
  267. }
  268. }
  269. static void tx_fifo_remove_last()
  270. {
  271. if (m_tx_fifo.count > 0)
  272. {
  273. DISABLE_RF_IRQ();
  274. m_tx_fifo.count--;
  275. if (++m_tx_fifo.exit_point >= NRF_ESB_TX_FIFO_SIZE)
  276. {
  277. m_tx_fifo.exit_point = 0;
  278. }
  279. ENABLE_RF_IRQ();
  280. }
  281. }
  282. /** @brief Function to push the content of the rx_buffer to the RX FIFO.
  283. *
  284. * The module will point the register NRF_RADIO->PACKETPTR to a buffer for receiving packets.
  285. * After receiving a packet the module will call this function to copy the received data to
  286. * the RX FIFO.
  287. *
  288. * @param pipe Pipe number to set for the packet.
  289. * @param pid Packet ID.
  290. *
  291. * @retval true Operation successful.
  292. * @retval false Operation failed.
  293. */
  294. static bool rx_fifo_push_rfbuf(uint8_t pipe, uint8_t pid)
  295. {
  296. if (m_rx_fifo.count < NRF_ESB_RX_FIFO_SIZE)
  297. {
  298. if (m_config_local.protocol == NRF_ESB_PROTOCOL_ESB_DPL)
  299. {
  300. if (m_rx_payload_buffer[0] > NRF_ESB_MAX_PAYLOAD_LENGTH)
  301. {
  302. return false;
  303. }
  304. m_rx_fifo.p_payload[m_rx_fifo.entry_point]->length = m_rx_payload_buffer[0];
  305. }
  306. else if (m_config_local.mode == NRF_ESB_MODE_PTX)
  307. {
  308. // Received packet is an acknowledgement
  309. m_rx_fifo.p_payload[m_rx_fifo.entry_point]->length = 0;
  310. }
  311. else
  312. {
  313. m_rx_fifo.p_payload[m_rx_fifo.entry_point]->length = m_config_local.payload_length;
  314. }
  315. memcpy(m_rx_fifo.p_payload[m_rx_fifo.entry_point]->data, &m_rx_payload_buffer[2],
  316. m_rx_fifo.p_payload[m_rx_fifo.entry_point]->length);
  317. m_rx_fifo.p_payload[m_rx_fifo.entry_point]->pipe = pipe;
  318. m_rx_fifo.p_payload[m_rx_fifo.entry_point]->rssi = NRF_RADIO->RSSISAMPLE;
  319. m_rx_fifo.p_payload[m_rx_fifo.entry_point]->pid = pid;
  320. if (++m_rx_fifo.entry_point >= NRF_ESB_RX_FIFO_SIZE)
  321. {
  322. m_rx_fifo.entry_point = 0;
  323. }
  324. m_rx_fifo.count++;
  325. return true;
  326. }
  327. return false;
  328. }
  329. static void sys_timer_init()
  330. {
  331. // Configure the system timer with a 1 MHz base frequency
  332. NRF_ESB_SYS_TIMER->PRESCALER = 4;
  333. NRF_ESB_SYS_TIMER->BITMODE = TIMER_BITMODE_BITMODE_16Bit;
  334. NRF_ESB_SYS_TIMER->SHORTS = TIMER_SHORTS_COMPARE1_CLEAR_Msk | TIMER_SHORTS_COMPARE1_STOP_Msk;
  335. }
  336. static void ppi_init()
  337. {
  338. NRF_PPI->CH[NRF_ESB_PPI_TIMER_START].EEP = (uint32_t)&NRF_RADIO->EVENTS_READY;
  339. NRF_PPI->CH[NRF_ESB_PPI_TIMER_START].TEP = (uint32_t)&NRF_ESB_SYS_TIMER->TASKS_START;
  340. NRF_PPI->CH[NRF_ESB_PPI_TIMER_STOP].EEP = (uint32_t)&NRF_RADIO->EVENTS_ADDRESS;
  341. NRF_PPI->CH[NRF_ESB_PPI_TIMER_STOP].TEP = (uint32_t)&NRF_ESB_SYS_TIMER->TASKS_STOP;
  342. NRF_PPI->CH[NRF_ESB_PPI_RX_TIMEOUT].EEP = (uint32_t)&NRF_ESB_SYS_TIMER->EVENTS_COMPARE[0];
  343. NRF_PPI->CH[NRF_ESB_PPI_RX_TIMEOUT].TEP = (uint32_t)&NRF_RADIO->TASKS_DISABLE;
  344. NRF_PPI->CH[NRF_ESB_PPI_TX_START].EEP = (uint32_t)&NRF_ESB_SYS_TIMER->EVENTS_COMPARE[1];
  345. NRF_PPI->CH[NRF_ESB_PPI_TX_START].TEP = (uint32_t)&NRF_RADIO->TASKS_TXEN;
  346. }
  347. static void start_tx_transaction()
  348. {
  349. bool ack;
  350. m_last_tx_attempts = 1;
  351. // Prepare the payload
  352. mp_current_payload = m_tx_fifo.p_payload[m_tx_fifo.exit_point];
  353. // Handling ack if noack is set to false or if selctive auto ack is turned turned off
  354. ack = !mp_current_payload->noack || !m_config_local.selective_auto_ack;
  355. switch (m_config_local.protocol)
  356. {
  357. case NRF_ESB_PROTOCOL_ESB:
  358. update_rf_payload_format(mp_current_payload->length);
  359. m_tx_payload_buffer[0] = mp_current_payload->pid;
  360. m_tx_payload_buffer[1] = 0;
  361. memcpy(&m_tx_payload_buffer[2], mp_current_payload->data, mp_current_payload->length);
  362. NRF_RADIO->SHORTS = RADIO_SHORTS_COMMON | RADIO_SHORTS_DISABLED_RXEN_Msk;
  363. NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk | RADIO_INTENSET_READY_Msk;
  364. // Configure the retransmit counter
  365. m_retransmits_remaining = m_config_local.retransmit_count;
  366. on_radio_disabled = on_radio_disabled_tx;
  367. m_nrf_esb_mainstate = NRF_ESB_STATE_PTX_TX_ACK;
  368. break;
  369. case NRF_ESB_PROTOCOL_ESB_DPL:
  370. m_tx_payload_buffer[0] = mp_current_payload->length;
  371. m_tx_payload_buffer[1] = mp_current_payload->pid << 1;
  372. m_tx_payload_buffer[1] |= ack ? 0x00 : 0x01;
  373. memcpy(&m_tx_payload_buffer[2], mp_current_payload->data, mp_current_payload->length);
  374. if (ack)
  375. {
  376. NRF_RADIO->SHORTS = RADIO_SHORTS_COMMON | RADIO_SHORTS_DISABLED_RXEN_Msk;
  377. NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk | RADIO_INTENSET_READY_Msk;
  378. // Configure the retransmit counter
  379. m_retransmits_remaining = m_config_local.retransmit_count;
  380. on_radio_disabled = on_radio_disabled_tx;
  381. m_nrf_esb_mainstate = NRF_ESB_STATE_PTX_TX_ACK;
  382. }
  383. else
  384. {
  385. NRF_RADIO->SHORTS = RADIO_SHORTS_COMMON;
  386. NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk;
  387. on_radio_disabled = on_radio_disabled_tx_noack;
  388. m_nrf_esb_mainstate = NRF_ESB_STATE_PTX_TX;
  389. }
  390. break;
  391. default:
  392. // Should not be reached
  393. break;
  394. }
  395. NRF_RADIO->TXADDRESS = mp_current_payload->pipe;
  396. NRF_RADIO->RXADDRESSES = 1 << mp_current_payload->pipe;
  397. NRF_RADIO->FREQUENCY = m_esb_addr.rf_channel;
  398. NRF_RADIO->PACKETPTR = (uint32_t)m_tx_payload_buffer;
  399. NVIC_ClearPendingIRQ(RADIO_IRQn);
  400. NVIC_EnableIRQ(RADIO_IRQn);
  401. NRF_RADIO->EVENTS_ADDRESS = 0;
  402. NRF_RADIO->EVENTS_PAYLOAD = 0;
  403. NRF_RADIO->EVENTS_DISABLED = 0;
  404. DEBUG_PIN_SET(DEBUGPIN4);
  405. NRF_RADIO->TASKS_TXEN = 1;
  406. }
  407. void RADIO_IRQHandler()
  408. {
  409. if (NRF_RADIO->EVENTS_READY && (NRF_RADIO->INTENSET & RADIO_INTENSET_READY_Msk))
  410. {
  411. NRF_RADIO->EVENTS_READY = 0;
  412. DEBUG_PIN_SET(DEBUGPIN1);
  413. }
  414. if (NRF_RADIO->EVENTS_END && (NRF_RADIO->INTENSET & RADIO_INTENSET_END_Msk))
  415. {
  416. NRF_RADIO->EVENTS_END = 0;
  417. DEBUG_PIN_SET(DEBUGPIN2);
  418. // Call the correct on_radio_end function, depending on the current protocol state
  419. if (on_radio_end)
  420. {
  421. on_radio_end();
  422. }
  423. }
  424. if (NRF_RADIO->EVENTS_DISABLED && (NRF_RADIO->INTENSET & RADIO_INTENSET_DISABLED_Msk))
  425. {
  426. NRF_RADIO->EVENTS_DISABLED = 0;
  427. DEBUG_PIN_SET(DEBUGPIN3);
  428. // Call the correct on_radio_disable function, depending on the current protocol state
  429. if (on_radio_disabled)
  430. {
  431. on_radio_disabled();
  432. }
  433. }
  434. DEBUG_PIN_CLR(DEBUGPIN1);
  435. DEBUG_PIN_CLR(DEBUGPIN2);
  436. DEBUG_PIN_CLR(DEBUGPIN3);
  437. DEBUG_PIN_CLR(DEBUGPIN4);
  438. }
  439. static void on_radio_disabled_tx_noack()
  440. {
  441. m_interrupt_flags |= NRF_ESB_INT_TX_SUCCESS_MSK;
  442. tx_fifo_remove_last();
  443. if (m_tx_fifo.count == 0)
  444. {
  445. m_nrf_esb_mainstate = NRF_ESB_STATE_IDLE;
  446. NVIC_SetPendingIRQ(ESB_EVT_IRQ);
  447. }
  448. else
  449. {
  450. NVIC_SetPendingIRQ(ESB_EVT_IRQ);
  451. start_tx_transaction();
  452. }
  453. }
  454. static void on_radio_disabled_tx()
  455. {
  456. // Remove the DISABLED -> RXEN shortcut, to make sure the radio stays
  457. // disabled after the RX window
  458. NRF_RADIO->SHORTS = RADIO_SHORTS_COMMON;
  459. // Make sure the timer is started the next time the radio is ready,
  460. // and that it will disable the radio automatically if no packet is
  461. // received by the time defined in m_wait_for_ack_timeout_us
  462. NRF_ESB_SYS_TIMER->CC[0] = m_wait_for_ack_timeout_us;
  463. NRF_ESB_SYS_TIMER->CC[1] = m_config_local.retransmit_delay - 130;
  464. NRF_ESB_SYS_TIMER->TASKS_CLEAR = 1;
  465. NRF_ESB_SYS_TIMER->EVENTS_COMPARE[0] = 0;
  466. NRF_ESB_SYS_TIMER->EVENTS_COMPARE[1] = 0;
  467. NRF_PPI->CHENSET = (1 << NRF_ESB_PPI_TIMER_START) |
  468. (1 << NRF_ESB_PPI_RX_TIMEOUT) |
  469. (1 << NRF_ESB_PPI_TIMER_STOP);
  470. NRF_PPI->CHENCLR = (1 << NRF_ESB_PPI_TX_START);
  471. NRF_RADIO->EVENTS_END = 0;
  472. if (m_config_local.protocol == NRF_ESB_PROTOCOL_ESB)
  473. {
  474. update_rf_payload_format(0);
  475. }
  476. NRF_RADIO->PACKETPTR = (uint32_t)m_rx_payload_buffer;
  477. on_radio_disabled = on_radio_disabled_tx_wait_for_ack;
  478. m_nrf_esb_mainstate = NRF_ESB_STATE_PTX_RX_ACK;
  479. }
  480. static void on_radio_disabled_tx_wait_for_ack()
  481. {
  482. // This marks the completion of a TX_RX sequence (TX with ACK)
  483. // Make sure the timer will not deactivate the radio if a packet is received
  484. NRF_PPI->CHENCLR = (1 << NRF_ESB_PPI_TIMER_START) |
  485. (1 << NRF_ESB_PPI_RX_TIMEOUT) |
  486. (1 << NRF_ESB_PPI_TIMER_STOP);
  487. // If the radio has received a packet and the CRC status is OK
  488. if (NRF_RADIO->EVENTS_END && NRF_RADIO->CRCSTATUS != 0)
  489. {
  490. NRF_ESB_SYS_TIMER->TASKS_STOP = 1;
  491. NRF_PPI->CHENCLR = (1 << NRF_ESB_PPI_TX_START);
  492. m_interrupt_flags |= NRF_ESB_INT_TX_SUCCESS_MSK;
  493. m_last_tx_attempts = m_config_local.retransmit_count - m_retransmits_remaining + 1;
  494. tx_fifo_remove_last();
  495. if (m_config_local.protocol != NRF_ESB_PROTOCOL_ESB && m_rx_payload_buffer[0] > 0)
  496. {
  497. if (rx_fifo_push_rfbuf((uint8_t)NRF_RADIO->TXADDRESS, 0))
  498. {
  499. m_interrupt_flags |= NRF_ESB_INT_RX_DATA_RECEIVED_MSK;
  500. }
  501. }
  502. if ((m_tx_fifo.count == 0) || (m_config_local.tx_mode == NRF_ESB_TXMODE_MANUAL))
  503. {
  504. m_nrf_esb_mainstate = NRF_ESB_STATE_IDLE;
  505. NVIC_SetPendingIRQ(ESB_EVT_IRQ);
  506. }
  507. else
  508. {
  509. NVIC_SetPendingIRQ(ESB_EVT_IRQ);
  510. start_tx_transaction();
  511. }
  512. }
  513. else
  514. {
  515. if (m_retransmits_remaining-- == 0)
  516. {
  517. NRF_ESB_SYS_TIMER->TASKS_STOP = 1;
  518. NRF_PPI->CHENCLR = (1 << NRF_ESB_PPI_TX_START);
  519. // All retransmits are expended, and the TX operation is suspended
  520. m_last_tx_attempts = m_config_local.retransmit_count + 1;
  521. m_interrupt_flags |= NRF_ESB_INT_TX_FAILED_MSK;
  522. m_nrf_esb_mainstate = NRF_ESB_STATE_IDLE;
  523. NVIC_SetPendingIRQ(ESB_EVT_IRQ);
  524. }
  525. else
  526. {
  527. // There are still have more retransmits left, TX mode should be
  528. // entered again as soon as the system timer reaches CC[1].
  529. NRF_RADIO->SHORTS = RADIO_SHORTS_COMMON | RADIO_SHORTS_DISABLED_RXEN_Msk;
  530. update_rf_payload_format(mp_current_payload->length);
  531. NRF_RADIO->PACKETPTR = (uint32_t)m_tx_payload_buffer;
  532. on_radio_disabled = on_radio_disabled_tx;
  533. m_nrf_esb_mainstate = NRF_ESB_STATE_PTX_TX_ACK;
  534. NRF_ESB_SYS_TIMER->TASKS_START = 1;
  535. NRF_PPI->CHENSET = (1 << NRF_ESB_PPI_TX_START);
  536. if (NRF_ESB_SYS_TIMER->EVENTS_COMPARE[1])
  537. {
  538. NRF_RADIO->TASKS_TXEN = 1;
  539. }
  540. }
  541. }
  542. }
  543. static void clear_events_restart_rx(void)
  544. {
  545. NRF_RADIO->SHORTS = RADIO_SHORTS_COMMON;
  546. update_rf_payload_format(m_config_local.payload_length);
  547. NRF_RADIO->PACKETPTR = (uint32_t)m_rx_payload_buffer;
  548. NRF_RADIO->EVENTS_DISABLED = 0;
  549. NRF_RADIO->TASKS_DISABLE = 1;
  550. while (NRF_RADIO->EVENTS_DISABLED == 0);
  551. NRF_RADIO->EVENTS_DISABLED = 0;
  552. NRF_RADIO->SHORTS = RADIO_SHORTS_COMMON | RADIO_SHORTS_DISABLED_TXEN_Msk;
  553. NRF_RADIO->TASKS_RXEN = 1;
  554. }
  555. static void on_radio_disabled_rx(void)
  556. {
  557. bool ack = false;
  558. bool retransmit_payload = false;
  559. bool send_rx_event = true;
  560. pipe_info_t * p_pipe_info;
  561. if (NRF_RADIO->CRCSTATUS == 0)
  562. {
  563. clear_events_restart_rx();
  564. return;
  565. }
  566. if(m_rx_fifo.count >= NRF_ESB_RX_FIFO_SIZE)
  567. {
  568. clear_events_restart_rx();
  569. return;
  570. }
  571. p_pipe_info = &m_rx_pipe_info[NRF_RADIO->RXMATCH];
  572. if (NRF_RADIO->RXCRC == p_pipe_info->m_crc &&
  573. (m_rx_payload_buffer[1] >> 1) == p_pipe_info->m_pid )
  574. {
  575. retransmit_payload = true;
  576. send_rx_event = false;
  577. }
  578. p_pipe_info->m_pid = m_rx_payload_buffer[1] >> 1;
  579. p_pipe_info->m_crc = NRF_RADIO->RXCRC;
  580. if(m_config_local.selective_auto_ack == false || ((m_rx_payload_buffer[1] & 0x01) == 0))
  581. ack = true;
  582. if(ack)
  583. {
  584. NRF_RADIO->SHORTS = RADIO_SHORTS_COMMON | RADIO_SHORTS_DISABLED_RXEN_Msk;
  585. switch(m_config_local.protocol)
  586. {
  587. case NRF_ESB_PROTOCOL_ESB_DPL:
  588. {
  589. if (m_tx_fifo.count > 0 &&
  590. (m_tx_fifo.p_payload[m_tx_fifo.exit_point]->pipe == NRF_RADIO->RXMATCH))
  591. {
  592. // Pipe stays in ACK with payload until TX fifo is empty
  593. // Do not report TX success on first ack payload or retransmit
  594. if (p_pipe_info->m_ack_payload != 0 && !retransmit_payload)
  595. {
  596. if(++m_tx_fifo.exit_point >= NRF_ESB_TX_FIFO_SIZE)
  597. {
  598. m_tx_fifo.exit_point = 0;
  599. }
  600. m_tx_fifo.count--;
  601. // ACK payloads also require TX_DS
  602. // (page 40 of the 'nRF24LE1_Product_Specification_rev1_6.pdf').
  603. m_interrupt_flags |= NRF_ESB_INT_TX_SUCCESS_MSK;
  604. }
  605. p_pipe_info->m_ack_payload = 1;
  606. mp_current_payload = m_tx_fifo.p_payload[m_tx_fifo.exit_point];
  607. update_rf_payload_format(mp_current_payload->length);
  608. m_tx_payload_buffer[0] = mp_current_payload->length;
  609. memcpy(&m_tx_payload_buffer[2],
  610. mp_current_payload->data,
  611. mp_current_payload->length);
  612. }
  613. else
  614. {
  615. p_pipe_info->m_ack_payload = 0;
  616. update_rf_payload_format(0);
  617. m_tx_payload_buffer[0] = 0;
  618. }
  619. m_tx_payload_buffer[1] = m_rx_payload_buffer[1];
  620. }
  621. break;
  622. case NRF_ESB_PROTOCOL_ESB:
  623. {
  624. update_rf_payload_format(0);
  625. m_tx_payload_buffer[0] = m_rx_payload_buffer[0];
  626. m_tx_payload_buffer[1] = 0;
  627. }
  628. break;
  629. }
  630. m_nrf_esb_mainstate = NRF_ESB_STATE_PRX_SEND_ACK;
  631. NRF_RADIO->TXADDRESS = NRF_RADIO->RXMATCH;
  632. NRF_RADIO->PACKETPTR = (uint32_t)m_tx_payload_buffer;
  633. on_radio_disabled = on_radio_disabled_rx_ack;
  634. }
  635. else
  636. {
  637. clear_events_restart_rx();
  638. }
  639. if (send_rx_event)
  640. {
  641. // Push the new packet to the RX buffer and trigger a received event if the operation was
  642. // successful.
  643. if (rx_fifo_push_rfbuf(NRF_RADIO->RXMATCH, p_pipe_info->m_pid))
  644. {
  645. m_interrupt_flags |= NRF_ESB_INT_RX_DATA_RECEIVED_MSK;
  646. NVIC_SetPendingIRQ(ESB_EVT_IRQ);
  647. }
  648. }
  649. }
  650. static void on_radio_disabled_rx_ack(void)
  651. {
  652. NRF_RADIO->SHORTS = RADIO_SHORTS_COMMON | RADIO_SHORTS_DISABLED_TXEN_Msk;
  653. update_rf_payload_format(m_config_local.payload_length);
  654. NRF_RADIO->PACKETPTR = (uint32_t)m_rx_payload_buffer;
  655. on_radio_disabled = on_radio_disabled_rx;
  656. m_nrf_esb_mainstate = NRF_ESB_STATE_PRX;
  657. }
  658. uint32_t nrf_esb_init(nrf_esb_config_t const * p_config)
  659. {
  660. uint32_t err_code;
  661. VERIFY_PARAM_NOT_NULL(p_config);
  662. if(m_esb_initialized)
  663. {
  664. err_code = nrf_esb_disable();
  665. if (err_code != NRF_SUCCESS)
  666. {
  667. return err_code;
  668. }
  669. }
  670. m_event_handler = p_config->event_handler;
  671. memcpy(&m_config_local, p_config, sizeof(nrf_esb_config_t));
  672. m_interrupt_flags = 0;
  673. memset(m_rx_pipe_info, 0, sizeof(m_rx_pipe_info));
  674. memset(m_pids, 0, sizeof(m_pids));
  675. update_radio_parameters();
  676. initialize_fifos();
  677. sys_timer_init();
  678. ppi_init();
  679. NVIC_SetPriority(RADIO_IRQn, m_config_local.radio_irq_priority & 0x03);
  680. NVIC_SetPriority(ESB_EVT_IRQ, m_config_local.event_irq_priority & 0x03);
  681. NVIC_EnableIRQ(ESB_EVT_IRQ);
  682. m_nrf_esb_mainstate = NRF_ESB_STATE_IDLE;
  683. m_esb_initialized = true;
  684. return NRF_SUCCESS;
  685. }
  686. uint32_t nrf_esb_suspend(void)
  687. {
  688. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  689. // Clear PPI
  690. NRF_PPI->CHENCLR = (1 << NRF_ESB_PPI_TIMER_START) |
  691. (1 << NRF_ESB_PPI_TIMER_STOP) |
  692. (1 << NRF_ESB_PPI_RX_TIMEOUT) |
  693. (1 << NRF_ESB_PPI_TX_START);
  694. m_nrf_esb_mainstate = NRF_ESB_STATE_IDLE;
  695. return NRF_SUCCESS;
  696. }
  697. uint32_t nrf_esb_disable(void)
  698. {
  699. // Clear PPI
  700. NRF_PPI->CHENCLR = (1 << NRF_ESB_PPI_TIMER_START) |
  701. (1 << NRF_ESB_PPI_TIMER_STOP) |
  702. (1 << NRF_ESB_PPI_RX_TIMEOUT) |
  703. (1 << NRF_ESB_PPI_TX_START);
  704. m_nrf_esb_mainstate = NRF_ESB_STATE_IDLE;
  705. reset_fifos();
  706. memset(m_rx_pipe_info, 0, sizeof(m_rx_pipe_info));
  707. memset(m_pids, 0, sizeof(m_pids));
  708. // Disable the radio
  709. NVIC_DisableIRQ(ESB_EVT_IRQ);
  710. NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Enabled << RADIO_SHORTS_READY_START_Pos |
  711. RADIO_SHORTS_END_DISABLE_Enabled << RADIO_SHORTS_END_DISABLE_Pos;
  712. return NRF_SUCCESS;
  713. }
  714. bool nrf_esb_is_idle(void)
  715. {
  716. return m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE;
  717. }
  718. void ESB_EVT_IRQHandler(void)
  719. {
  720. ret_code_t err_code;
  721. uint32_t interrupts;
  722. nrf_esb_evt_t event;
  723. event.tx_attempts = m_last_tx_attempts;
  724. err_code = nrf_esb_get_clear_interrupts(&interrupts);
  725. if (err_code == NRF_SUCCESS && m_event_handler != 0)
  726. {
  727. if(interrupts & NRF_ESB_INT_TX_SUCCESS_MSK)
  728. {
  729. event.evt_id = NRF_ESB_EVENT_TX_SUCCESS;
  730. m_event_handler(&event);
  731. }
  732. if(interrupts & NRF_ESB_INT_TX_FAILED_MSK)
  733. {
  734. event.evt_id = NRF_ESB_EVENT_TX_FAILED;
  735. m_event_handler(&event);
  736. }
  737. if(interrupts & NRF_ESB_INT_RX_DATA_RECEIVED_MSK)
  738. {
  739. event.evt_id = NRF_ESB_EVENT_RX_RECEIVED;
  740. m_event_handler(&event);
  741. }
  742. }
  743. }
  744. uint32_t nrf_esb_write_payload(nrf_esb_payload_t const * p_payload)
  745. {
  746. VERIFY_TRUE(m_esb_initialized, NRF_ERROR_INVALID_STATE);
  747. VERIFY_PARAM_NOT_NULL(p_payload);
  748. VERIFY_PAYLOAD_LENGTH(p_payload);
  749. VERIFY_FALSE(m_tx_fifo.count >= NRF_ESB_TX_FIFO_SIZE, NRF_ERROR_NO_MEM);
  750. if (m_config_local.mode == NRF_ESB_MODE_PTX &&
  751. p_payload->noack && !m_config_local.selective_auto_ack )
  752. {
  753. return NRF_ERROR_NOT_SUPPORTED;
  754. }
  755. DISABLE_RF_IRQ();
  756. memcpy(m_tx_fifo.p_payload[m_tx_fifo.entry_point], p_payload, sizeof(nrf_esb_payload_t));
  757. m_pids[p_payload->pipe] = (m_pids[p_payload->pipe] + 1) % (NRF_ESB_PID_MAX + 1);
  758. m_tx_fifo.p_payload[m_tx_fifo.entry_point]->pid = m_pids[p_payload->pipe];
  759. if (++m_tx_fifo.entry_point >= NRF_ESB_TX_FIFO_SIZE)
  760. {
  761. m_tx_fifo.entry_point = 0;
  762. }
  763. m_tx_fifo.count++;
  764. ENABLE_RF_IRQ();
  765. if (m_config_local.mode == NRF_ESB_MODE_PTX &&
  766. m_config_local.tx_mode == NRF_ESB_TXMODE_AUTO &&
  767. m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE)
  768. {
  769. start_tx_transaction();
  770. }
  771. return NRF_SUCCESS;
  772. }
  773. uint32_t nrf_esb_read_rx_payload(nrf_esb_payload_t * p_payload)
  774. {
  775. VERIFY_TRUE(m_esb_initialized, NRF_ERROR_INVALID_STATE);
  776. VERIFY_PARAM_NOT_NULL(p_payload);
  777. if (m_rx_fifo.count == 0)
  778. {
  779. return NRF_ERROR_NOT_FOUND;
  780. }
  781. DISABLE_RF_IRQ();
  782. p_payload->length = m_rx_fifo.p_payload[m_rx_fifo.exit_point]->length;
  783. p_payload->pipe = m_rx_fifo.p_payload[m_rx_fifo.exit_point]->pipe;
  784. p_payload->rssi = m_rx_fifo.p_payload[m_rx_fifo.exit_point]->rssi;
  785. p_payload->pid = m_rx_fifo.p_payload[m_rx_fifo.exit_point]->pid;
  786. memcpy(p_payload->data, m_rx_fifo.p_payload[m_rx_fifo.exit_point]->data, p_payload->length);
  787. if (++m_rx_fifo.exit_point >= NRF_ESB_RX_FIFO_SIZE)
  788. {
  789. m_rx_fifo.exit_point = 0;
  790. }
  791. m_rx_fifo.count--;
  792. ENABLE_RF_IRQ();
  793. return NRF_SUCCESS;
  794. }
  795. uint32_t nrf_esb_start_tx(void)
  796. {
  797. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  798. if (m_tx_fifo.count == 0)
  799. {
  800. return NRF_ERROR_BUFFER_EMPTY;
  801. }
  802. start_tx_transaction();
  803. return NRF_SUCCESS;
  804. }
  805. uint32_t nrf_esb_start_rx(void)
  806. {
  807. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  808. NRF_RADIO->INTENCLR = 0xFFFFFFFF;
  809. NRF_RADIO->EVENTS_DISABLED = 0;
  810. on_radio_disabled = on_radio_disabled_rx;
  811. NRF_RADIO->SHORTS = RADIO_SHORTS_COMMON | RADIO_SHORTS_DISABLED_TXEN_Msk;
  812. NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk;
  813. m_nrf_esb_mainstate = NRF_ESB_STATE_PRX;
  814. NRF_RADIO->RXADDRESSES = m_esb_addr.rx_pipes_enabled;
  815. NRF_RADIO->FREQUENCY = m_esb_addr.rf_channel;
  816. NRF_RADIO->PACKETPTR = (uint32_t)m_rx_payload_buffer;
  817. NVIC_ClearPendingIRQ(RADIO_IRQn);
  818. NVIC_EnableIRQ(RADIO_IRQn);
  819. NRF_RADIO->EVENTS_ADDRESS = 0;
  820. NRF_RADIO->EVENTS_PAYLOAD = 0;
  821. NRF_RADIO->EVENTS_DISABLED = 0;
  822. NRF_RADIO->TASKS_RXEN = 1;
  823. return NRF_SUCCESS;
  824. }
  825. uint32_t nrf_esb_stop_rx(void)
  826. {
  827. if (m_nrf_esb_mainstate == NRF_ESB_STATE_PRX)
  828. {
  829. NRF_RADIO->SHORTS = 0;
  830. NRF_RADIO->INTENCLR = 0xFFFFFFFF;
  831. on_radio_disabled = NULL;
  832. NRF_RADIO->EVENTS_DISABLED = 0;
  833. NRF_RADIO->TASKS_DISABLE = 1;
  834. while (NRF_RADIO->EVENTS_DISABLED == 0);
  835. m_nrf_esb_mainstate = NRF_ESB_STATE_IDLE;
  836. return NRF_SUCCESS;
  837. }
  838. return NRF_ESB_ERROR_NOT_IN_RX_MODE;
  839. }
  840. uint32_t nrf_esb_flush_tx(void)
  841. {
  842. VERIFY_TRUE(m_esb_initialized, NRF_ERROR_INVALID_STATE);
  843. DISABLE_RF_IRQ();
  844. m_tx_fifo.count = 0;
  845. m_tx_fifo.entry_point = 0;
  846. m_tx_fifo.exit_point = 0;
  847. ENABLE_RF_IRQ();
  848. return NRF_SUCCESS;
  849. }
  850. uint32_t nrf_esb_pop_tx(void)
  851. {
  852. VERIFY_TRUE(m_esb_initialized, NRF_ERROR_INVALID_STATE);
  853. VERIFY_TRUE(m_tx_fifo.count > 0, NRF_ERROR_BUFFER_EMPTY);
  854. DISABLE_RF_IRQ();
  855. if (++m_tx_fifo.entry_point >= NRF_ESB_TX_FIFO_SIZE)
  856. {
  857. m_tx_fifo.entry_point = 0;
  858. }
  859. m_tx_fifo.count--;
  860. ENABLE_RF_IRQ();
  861. return NRF_SUCCESS;
  862. }
  863. uint32_t nrf_esb_flush_rx(void)
  864. {
  865. VERIFY_TRUE(m_esb_initialized, NRF_ERROR_INVALID_STATE);
  866. DISABLE_RF_IRQ();
  867. m_rx_fifo.count = 0;
  868. m_rx_fifo.entry_point = 0;
  869. m_rx_fifo.exit_point = 0;
  870. memset(m_rx_pipe_info, 0, sizeof(m_rx_pipe_info));
  871. ENABLE_RF_IRQ();
  872. return NRF_SUCCESS;
  873. }
  874. uint32_t nrf_esb_get_clear_interrupts(uint32_t * p_interrupts)
  875. {
  876. VERIFY_TRUE(m_esb_initialized, NRF_ERROR_INVALID_STATE);
  877. VERIFY_PARAM_NOT_NULL(p_interrupts);
  878. DISABLE_RF_IRQ();
  879. *p_interrupts = m_interrupt_flags;
  880. m_interrupt_flags = 0;
  881. ENABLE_RF_IRQ();
  882. return NRF_SUCCESS;
  883. }
  884. uint32_t nrf_esb_set_address_length(uint8_t length)
  885. {
  886. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  887. VERIFY_TRUE(length > 2 && length < 6, NRF_ERROR_INVALID_PARAM);
  888. m_esb_addr.addr_length = length;
  889. update_rf_payload_format(m_config_local.payload_length);
  890. return NRF_SUCCESS;
  891. }
  892. uint32_t nrf_esb_set_base_address_0(uint8_t const * p_addr)
  893. {
  894. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  895. VERIFY_PARAM_NOT_NULL(p_addr);
  896. memcpy(m_esb_addr.base_addr_p0, p_addr, 4);
  897. update_radio_addresses(NRF_ESB_ADDR_UPDATE_MASK_BASE0);
  898. return NRF_SUCCESS;
  899. }
  900. uint32_t nrf_esb_set_base_address_1(uint8_t const * p_addr)
  901. {
  902. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  903. VERIFY_PARAM_NOT_NULL(p_addr);
  904. memcpy(m_esb_addr.base_addr_p1, p_addr, 4);
  905. update_radio_addresses(NRF_ESB_ADDR_UPDATE_MASK_BASE1);
  906. return NRF_SUCCESS;
  907. }
  908. uint32_t nrf_esb_set_prefixes(uint8_t const * p_prefixes, uint8_t num_pipes)
  909. {
  910. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  911. VERIFY_PARAM_NOT_NULL(p_prefixes);
  912. VERIFY_TRUE(num_pipes < 9, NRF_ERROR_INVALID_PARAM);
  913. memcpy(m_esb_addr.pipe_prefixes, p_prefixes, num_pipes);
  914. m_esb_addr.num_pipes = num_pipes;
  915. m_esb_addr.rx_pipes_enabled = BIT_MASK_UINT_8(num_pipes);
  916. update_radio_addresses(NRF_ESB_ADDR_UPDATE_MASK_PREFIX);
  917. return NRF_SUCCESS;
  918. }
  919. uint32_t nrf_esb_update_prefix(uint8_t pipe, uint8_t prefix)
  920. {
  921. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  922. VERIFY_TRUE(pipe < 8, NRF_ERROR_INVALID_PARAM);
  923. m_esb_addr.pipe_prefixes[pipe] = prefix;
  924. update_radio_addresses(NRF_ESB_ADDR_UPDATE_MASK_PREFIX);
  925. return NRF_SUCCESS;
  926. }
  927. uint32_t nrf_esb_enable_pipes(uint8_t enable_mask)
  928. {
  929. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  930. m_esb_addr.rx_pipes_enabled = enable_mask;
  931. return NRF_SUCCESS;
  932. }
  933. uint32_t nrf_esb_set_rf_channel(uint32_t channel)
  934. {
  935. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  936. VERIFY_TRUE(channel <= 125, NRF_ERROR_INVALID_PARAM);
  937. m_esb_addr.rf_channel = channel;
  938. return NRF_SUCCESS;
  939. }
  940. uint32_t nrf_esb_get_rf_channel(uint32_t * p_channel)
  941. {
  942. VERIFY_PARAM_NOT_NULL(p_channel);
  943. *p_channel = m_esb_addr.rf_channel;
  944. return NRF_SUCCESS;
  945. }
  946. uint32_t nrf_esb_set_tx_power(nrf_esb_tx_power_t tx_output_power)
  947. {
  948. VERIFY_TRUE(m_nrf_esb_mainstate == NRF_ESB_STATE_IDLE, NRF_ERROR_BUSY);
  949. if ( m_config_local.tx_output_power != tx_output_power )
  950. {
  951. m_config_local.tx_output_power = tx_output_power;
  952. update_radio_tx_power();
  953. }
  954. return NRF_SUCCESS;
  955. }