nrf_libuarte.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /**
  2. * Copyright (c) 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "sdk_config.h"
  41. #include "nrf_libuarte.h"
  42. #include "nrfx_ppi.h"
  43. #include "nrf_uarte.h"
  44. #include "nrf_gpio.h"
  45. #include "nrfx_timer.h"
  46. #define NRF_LOG_MODULE_NAME libUARTE
  47. #if NRF_LIBUARTE_CONFIG_LOG_ENABLED
  48. #define NRF_LOG_LEVEL NRF_LIBUARTE_CONFIG_LOG_LEVEL
  49. #define NRF_LOG_INFO_COLOR NRF_LIBUARTE_CONFIG_INFO_COLOR
  50. #define NRF_LOG_DEBUG_COLOR NRF_LIBUARTE_CONFIG_DEBUG_COLOR
  51. #else // NRF_LIBUARTE_CONFIG_LOG_ENABLED
  52. #define NRF_LOG_LEVEL 0
  53. #endif // NRF_LIBUARTE_CONFIG_LOG_ENABLED
  54. #include "nrf_log.h"
  55. NRF_LOG_MODULE_REGISTER();
  56. #define INTERRUPTS_MASK \
  57. (NRF_UARTE_INT_ENDRX_MASK | NRF_UARTE_INT_RXSTARTED_MASK | NRF_UARTE_INT_ERROR_MASK | \
  58. NRF_UARTE_INT_ENDTX_MASK | NRF_UARTE_INT_TXSTOPPED_MASK)
  59. #define UART_DRV_UARTE CONCAT_2(NRF_UARTE, NRF_LIBUARTE_CONFIG_UARTE_USED)
  60. #if NRF_LIBUARTE_CONFIG_UARTE_USED == 0
  61. #define UART_DRV_IRQn UARTE0_UART0_IRQn
  62. #define UART_DRV_IRQHandler UARTE0_UART0_IRQHandler
  63. #define MAX_DMA_XFER_LEN (1<<UARTE0_EASYDMA_MAXCNT_SIZE)
  64. #elif NRF_LIBUARTE_CONFIG_UARTE_USED == 1
  65. #define UART_DRV_IRQn UARTE1_UART1_IRQn
  66. #define UART_DRV_IRQHandler UARTE1_IRQHandler
  67. #define MAX_DMA_XFER_LEN (1<<UARTE1_EASYDMA_MAXCNT_SIZE)
  68. #endif
  69. typedef enum
  70. {
  71. PPI_CH_EXT_TRIGGER_STARTRX_EN_ENDRX_STARTX,
  72. PPI_CH_RXSTARTED_EXT_TSK,
  73. PPI_CH_EXT_STOP_STOPRX,
  74. PPI_CH_EXT_STOP_GROUPS_EN,
  75. PPI_CH_RXRDY_TIMER_COUNT,
  76. PPI_CH_RX_MAX,
  77. PPI_CH_ENDRX_STARTRX = PPI_CH_RX_MAX,
  78. PPI_CH_ENDRX_EXT_TSK,
  79. PPI_CH_RX_GROUP_MAX,
  80. PPI_CH_ENDTX_STARTTX = PPI_CH_RX_GROUP_MAX,
  81. PPI_CH_MAX
  82. } nrf_libuarte_ppi_channel_t;
  83. typedef enum
  84. {
  85. PPI_GROUP_ENDRX_STARTRX,
  86. PPI_GROUP_ENDRX_EXT_RXDONE_TSK,
  87. PPI_GROUP_MAX
  88. } nrf_libuarte_ppi_group_t;
  89. static nrfx_timer_t m_timer = NRFX_TIMER_INSTANCE(NRF_LIBUARTE_CONFIG_TIMER_USED);
  90. #if CONCAT_3(NRFX_TIMER, NRF_LIBUARTE_CONFIG_TIMER_USED,_ENABLED) == 0
  91. #error "Timer instance not enabled"
  92. #endif
  93. static nrf_ppi_channel_t m_ppi_channels[PPI_CH_MAX];
  94. static nrf_ppi_channel_group_t m_ppi_groups[PPI_GROUP_MAX];
  95. static uint8_t * mp_tx;
  96. static size_t m_tx_len;
  97. static size_t m_tx_cur_idx;
  98. static uint8_t * mp_cur_rx;
  99. static uint8_t * mp_next_rx;
  100. static uint8_t * mp_next_next_rx;
  101. static nrf_libuarte_evt_handler_t m_evt_handler;
  102. static uint32_t m_last_rx_byte_cnt;
  103. static uint32_t m_last_pin_rx_byte_cnt;
  104. static uint8_t m_tx_chunk8;
  105. static uint32_t m_chunk_size;
  106. #define PPI_CH_SETUP(_ch, _evt, _tsk, _fork) \
  107. ret = nrfx_ppi_channel_assign(m_ppi_channels[_ch], _evt, _tsk); \
  108. if (ret != NRF_SUCCESS) \
  109. { \
  110. return NRF_ERROR_INTERNAL; \
  111. } \
  112. if (_fork) \
  113. { \
  114. ret = nrfx_ppi_channel_fork_assign(m_ppi_channels[_ch], _fork); \
  115. if (ret != NRF_SUCCESS) \
  116. { \
  117. return NRF_ERROR_INTERNAL; \
  118. } \
  119. }
  120. #define PPI_GROUP_SETUP(_ch, _group, _en_tsk, _dis_tsk) \
  121. ret = nrfx_ppi_channel_include_in_group(m_ppi_channels[_ch], \
  122. m_ppi_groups[_group]); \
  123. if (ret != NRF_SUCCESS) \
  124. { \
  125. return NRF_ERROR_INTERNAL; \
  126. } \
  127. _en_tsk = nrfx_ppi_task_addr_group_enable_get(m_ppi_groups[_group]); \
  128. _dis_tsk = nrfx_ppi_task_addr_group_disable_get(m_ppi_groups[_group]);
  129. static ret_code_t ppi_configure(nrf_libuarte_config_t * p_config)
  130. {
  131. ret_code_t ret;
  132. /*lint -save -e666 */
  133. ///////////////////////////////////////////////////////////////////////////////
  134. uint32_t group1_en_tsk;
  135. uint32_t group1_dis_tsk;
  136. PPI_GROUP_SETUP(PPI_CH_ENDRX_STARTRX, PPI_GROUP_ENDRX_STARTRX, group1_en_tsk, group1_dis_tsk);
  137. PPI_CH_SETUP(PPI_CH_ENDRX_STARTRX,
  138. nrf_uarte_event_address_get(UART_DRV_UARTE, NRF_UARTE_EVENT_ENDRX),
  139. nrf_uarte_task_address_get(UART_DRV_UARTE, NRF_UARTE_TASK_STARTRX),
  140. nrfx_timer_capture_task_address_get(&m_timer, 0));
  141. ///////////////////////////////////////////////////////////////////////////////
  142. uint32_t group2_en_tsk;
  143. uint32_t group2_dis_tsk;
  144. PPI_GROUP_SETUP(PPI_CH_ENDRX_EXT_TSK, PPI_GROUP_ENDRX_EXT_RXDONE_TSK, group2_en_tsk, group2_dis_tsk);
  145. PPI_CH_SETUP(PPI_CH_ENDRX_EXT_TSK,
  146. nrf_uarte_event_address_get(UART_DRV_UARTE, NRF_UARTE_EVENT_ENDRX),
  147. nrfx_timer_capture_task_address_get(&m_timer, 0),
  148. p_config->rxdone_tsk);
  149. ///////////////////////////////////////////////////////////////////////////////
  150. PPI_CH_SETUP(PPI_CH_EXT_TRIGGER_STARTRX_EN_ENDRX_STARTX,
  151. p_config->startrx_evt,
  152. group1_en_tsk,
  153. nrf_uarte_task_address_get(UART_DRV_UARTE, NRF_UARTE_TASK_STARTRX));
  154. ///////////////////////////////////////////////////////////////////////////////
  155. PPI_CH_SETUP(PPI_CH_RXSTARTED_EXT_TSK,
  156. nrf_uarte_event_address_get(UART_DRV_UARTE, NRF_UARTE_EVENT_RXSTARTED),
  157. group2_dis_tsk,
  158. p_config->rxstarted_tsk);
  159. if (p_config->endrx_evt)
  160. {
  161. ///////////////////////////////////////////////////////////////////////////////
  162. PPI_CH_SETUP(PPI_CH_EXT_STOP_STOPRX,
  163. p_config->endrx_evt,
  164. nrf_uarte_task_address_get(UART_DRV_UARTE, NRF_UARTE_TASK_STOPRX),
  165. group2_en_tsk);
  166. ///////////////////////////////////////////////////////////////////////////////
  167. PPI_CH_SETUP(PPI_CH_EXT_STOP_GROUPS_EN,
  168. p_config->endrx_evt,
  169. group1_dis_tsk,
  170. nrfx_timer_capture_task_address_get(&m_timer, 1));
  171. }
  172. ////////////////////////////////TX///////////////////////////////////////////////
  173. PPI_CH_SETUP(PPI_CH_ENDTX_STARTTX,
  174. nrf_uarte_event_address_get(UART_DRV_UARTE, NRF_UARTE_EVENT_ENDTX),
  175. nrf_uarte_task_address_get(UART_DRV_UARTE, NRF_UARTE_TASK_STARTTX),
  176. 0);
  177. ////////////////////////////////TX///////////////////////////////////////////////
  178. PPI_CH_SETUP(PPI_CH_RXRDY_TIMER_COUNT,
  179. nrf_uarte_event_address_get(UART_DRV_UARTE, NRF_UARTE_EVENT_RXDRDY),
  180. nrfx_timer_task_address_get(&m_timer, NRF_TIMER_TASK_COUNT),
  181. 0);
  182. if (ret != NRFX_SUCCESS)
  183. {
  184. return NRF_ERROR_INTERNAL;
  185. }
  186. return NRF_SUCCESS;
  187. /*lint -restore */
  188. }
  189. void tmr_evt_handler(nrf_timer_event_t event_type, void * p_context)
  190. {
  191. UNUSED_PARAMETER(event_type);
  192. UNUSED_PARAMETER(p_context);
  193. }
  194. ret_code_t nrf_libuarte_init(nrf_libuarte_config_t * p_config, nrf_libuarte_evt_handler_t evt_handler)
  195. {
  196. ret_code_t ret;
  197. m_evt_handler = evt_handler;
  198. mp_cur_rx = NULL;
  199. mp_next_rx = NULL;
  200. mp_next_next_rx = NULL;
  201. mp_tx = NULL;
  202. //UART init
  203. nrf_gpio_pin_set(p_config->tx_pin);
  204. nrf_gpio_cfg_output(p_config->tx_pin);
  205. nrf_gpio_cfg_input(p_config->rx_pin, NRF_GPIO_PIN_NOPULL);
  206. nrf_uarte_baudrate_set(UART_DRV_UARTE, p_config->baudrate);
  207. nrf_uarte_configure(UART_DRV_UARTE, p_config->parity, p_config->hwfc);
  208. nrf_uarte_txrx_pins_set(UART_DRV_UARTE, p_config->tx_pin, p_config->rx_pin);
  209. if (p_config->hwfc == NRF_UARTE_HWFC_ENABLED)
  210. {
  211. if (p_config->cts_pin != NRF_UARTE_PSEL_DISCONNECTED)
  212. {
  213. nrf_gpio_cfg_input(p_config->cts_pin, NRF_GPIO_PIN_NOPULL);
  214. }
  215. if (p_config->rts_pin != NRF_UARTE_PSEL_DISCONNECTED)
  216. {
  217. nrf_gpio_pin_set(p_config->rts_pin);
  218. nrf_gpio_cfg_output(p_config->rts_pin);
  219. }
  220. nrf_uarte_hwfc_pins_set(UART_DRV_UARTE, p_config->rts_pin, p_config->cts_pin);
  221. }
  222. nrf_uarte_int_enable(UART_DRV_UARTE, INTERRUPTS_MASK);
  223. NVIC_SetPriority(UART_DRV_IRQn, p_config->irq_priority);
  224. NVIC_ClearPendingIRQ(UART_DRV_IRQn);
  225. NVIC_EnableIRQ(UART_DRV_IRQn);
  226. nrf_uarte_enable(UART_DRV_UARTE);
  227. nrfx_timer_config_t tmr_config = NRFX_TIMER_DEFAULT_CONFIG;
  228. tmr_config.mode = NRF_TIMER_MODE_COUNTER;
  229. tmr_config.bit_width = NRF_TIMER_BIT_WIDTH_32;
  230. ret = nrfx_timer_init(&m_timer, &tmr_config, tmr_evt_handler);
  231. if (ret != NRFX_SUCCESS)
  232. {
  233. return NRF_ERROR_INTERNAL;
  234. }
  235. nrfx_timer_enable(&m_timer);
  236. nrfx_timer_clear(&m_timer);
  237. m_last_rx_byte_cnt = 0;
  238. m_last_pin_rx_byte_cnt = 0;
  239. uint32_t i;
  240. for (i = 0; i < PPI_CH_MAX; i++)
  241. {
  242. ret = nrfx_ppi_channel_alloc(&m_ppi_channels[i]);
  243. if (ret != NRFX_SUCCESS)
  244. {
  245. //we don't free already allocated channels, system is wrongly configured.
  246. return NRF_ERROR_INTERNAL;
  247. }
  248. }
  249. for (i = 0; i < PPI_GROUP_MAX; i++)
  250. {
  251. ret = nrfx_ppi_group_alloc(&m_ppi_groups[i]);
  252. if (ret != NRFX_SUCCESS)
  253. {
  254. //we don't free already allocated channels, system is wrongly configured.
  255. return NRF_ERROR_INTERNAL;
  256. }
  257. }
  258. return ppi_configure(p_config);
  259. }
  260. void nrf_libuarte_uninit(void)
  261. {
  262. NVIC_DisableIRQ(UART_DRV_IRQn);
  263. nrf_uarte_int_disable(UART_DRV_UARTE, 0xFFFFFFFF);
  264. nrf_uarte_disable(UART_DRV_UARTE);
  265. nrfx_timer_disable(&m_timer);
  266. nrfx_timer_uninit(&m_timer);
  267. uint32_t i;
  268. ret_code_t ret;
  269. for (i = 0; i < PPI_CH_MAX; i++)
  270. {
  271. ret = nrfx_ppi_channel_disable(m_ppi_channels[i]);
  272. ASSERT(ret == NRFX_SUCCESS)
  273. ret = nrfx_ppi_channel_free(m_ppi_channels[i]);
  274. ASSERT(ret == NRFX_SUCCESS)
  275. }
  276. for (i = 0; i < PPI_GROUP_MAX; i++)
  277. {
  278. ret = nrfx_ppi_group_free(m_ppi_groups[i]);
  279. ASSERT(ret == NRFX_SUCCESS)
  280. }
  281. }
  282. ret_code_t nrf_libuarte_tx(uint8_t * p_data, size_t len)
  283. {
  284. if (mp_tx)
  285. {
  286. return NRF_ERROR_BUSY;
  287. }
  288. mp_tx = p_data;
  289. m_tx_len = len;
  290. m_tx_cur_idx = 0;
  291. uint8_t first_chunk;
  292. if (len <= MAX_DMA_XFER_LEN)
  293. {
  294. first_chunk = len;
  295. m_tx_chunk8 = 0;
  296. }
  297. else
  298. {
  299. uint32_t num_of_chunks = CEIL_DIV(len, MAX_DMA_XFER_LEN);
  300. m_tx_chunk8 = len/num_of_chunks;
  301. first_chunk = m_tx_chunk8 + len%m_tx_chunk8;
  302. }
  303. NRF_LOG_DEBUG("Started TX total length:%d, first chunk:%d", len, first_chunk);
  304. nrf_uarte_tx_buffer_set(UART_DRV_UARTE, p_data, first_chunk);
  305. nrf_uarte_event_clear(UART_DRV_UARTE, NRF_UARTE_EVENT_TXSTARTED);
  306. nrf_uarte_task_trigger(UART_DRV_UARTE, NRF_UARTE_TASK_STARTTX);
  307. if (len > MAX_DMA_XFER_LEN)
  308. {
  309. while(nrf_uarte_event_check(UART_DRV_UARTE, NRF_UARTE_EVENT_TXSTARTED) == 0)
  310. {
  311. }
  312. nrf_uarte_event_clear(UART_DRV_UARTE, NRF_UARTE_EVENT_TXSTARTED);
  313. nrfx_err_t err = nrfx_ppi_channel_enable(m_ppi_channels[PPI_CH_ENDTX_STARTTX]);
  314. if (err != NRFX_SUCCESS)
  315. {
  316. return NRF_ERROR_INTERNAL;
  317. }
  318. nrf_uarte_tx_buffer_set(UART_DRV_UARTE, &p_data[first_chunk], m_tx_chunk8);
  319. }
  320. return NRF_SUCCESS;
  321. }
  322. ret_code_t nrf_libuarte_rx_start(uint8_t * p_data, size_t len, bool ext_trigger_en)
  323. {
  324. m_chunk_size = len;
  325. uint32_t i;
  326. for (i = 0; i < PPI_CH_RX_MAX; i++)
  327. {
  328. nrfx_err_t err = nrfx_ppi_channel_enable(m_ppi_channels[i]);
  329. if (err != NRFX_SUCCESS)
  330. {
  331. return NRF_ERROR_INTERNAL;
  332. }
  333. }
  334. ASSERT(len <= MAX_DMA_XFER_LEN);
  335. if (p_data)
  336. {
  337. mp_cur_rx = p_data;
  338. nrf_uarte_rx_buffer_set(UART_DRV_UARTE, p_data, len);
  339. }
  340. nrf_uarte_event_clear(UART_DRV_UARTE, NRF_UARTE_EVENT_ENDRX);
  341. nrf_uarte_event_clear(UART_DRV_UARTE, NRF_UARTE_EVENT_RXSTARTED);
  342. if (ext_trigger_en)
  343. {
  344. }
  345. else
  346. {
  347. *(uint32_t *)nrfx_ppi_task_addr_group_enable_get(m_ppi_groups[PPI_GROUP_ENDRX_STARTRX]) = 1;
  348. nrf_uarte_task_trigger(UART_DRV_UARTE, NRF_UARTE_TASK_STARTRX);
  349. }
  350. NRF_LOG_DEBUG("Start continues RX. Provided buffer:0x%08X", p_data);
  351. return NRF_SUCCESS;
  352. }
  353. void nrf_libuarte_rx_buf_rsp(uint8_t * p_data, size_t len)
  354. {
  355. if (mp_next_rx == NULL)
  356. {
  357. mp_next_rx = p_data;
  358. NRF_LOG_DEBUG("RX buf response (next). Provided buffer:0x%08X", p_data);
  359. nrf_uarte_rx_buffer_set(UART_DRV_UARTE, p_data, len);
  360. }
  361. else
  362. {
  363. NRF_LOG_DEBUG("RX buf response (mp_next_rx not NULL:0x%08X), Provided buffer:0x%08X",
  364. mp_next_rx,
  365. p_data);
  366. mp_next_next_rx = p_data;
  367. }
  368. }
  369. void nrf_libuarte_rx_stop(void)
  370. {
  371. uint32_t i;
  372. for (i = 0; i < PPI_CH_RX_MAX; i++)
  373. {
  374. nrfx_err_t err = nrfx_ppi_channel_disable(m_ppi_channels[i]);
  375. ASSERT(err == NRFX_SUCCESS);
  376. }
  377. NRF_LOG_DEBUG("RX stopped.");
  378. nrf_uarte_task_trigger(UART_DRV_UARTE, NRF_UARTE_TASK_STOPRX);
  379. }
  380. void UART_DRV_IRQHandler(void)
  381. {
  382. if (nrf_uarte_event_check(UART_DRV_UARTE, NRF_UARTE_EVENT_ERROR))
  383. {
  384. nrf_uarte_event_clear(UART_DRV_UARTE, NRF_UARTE_EVENT_ERROR);
  385. nrf_libuarte_evt_t evt = {
  386. .type = NRF_LIBUARTE_EVT_ERROR
  387. };
  388. m_evt_handler(&evt);
  389. }
  390. if (nrf_uarte_event_check(UART_DRV_UARTE, NRF_UARTE_EVENT_RXSTARTED))
  391. {
  392. nrf_uarte_event_clear(UART_DRV_UARTE, NRF_UARTE_EVENT_RXSTARTED);
  393. nrf_libuarte_evt_t evt = {
  394. .type = NRF_LIBUARTE_EVT_RX_BUF_REQ,
  395. };
  396. m_evt_handler(&evt);
  397. }
  398. if (nrf_uarte_event_check(UART_DRV_UARTE, NRF_UARTE_EVENT_ENDRX))
  399. {
  400. nrf_uarte_event_clear(UART_DRV_UARTE, NRF_UARTE_EVENT_ENDRX);
  401. uint32_t endrx_byte_cnt = nrfx_timer_capture_get(&m_timer, NRF_TIMER_CC_CHANNEL0);
  402. uint32_t stop_byte_cnt = nrfx_timer_capture_get(&m_timer, NRF_TIMER_CC_CHANNEL1);
  403. uint32_t dma_amount = endrx_byte_cnt - m_last_rx_byte_cnt;
  404. uint32_t pin_amount = stop_byte_cnt - m_last_pin_rx_byte_cnt;
  405. NRF_LOG_DEBUG("(evt) RX dma_cnt:%d, endrx_cnt:%d, stop_cnt:%d",
  406. dma_amount,
  407. endrx_byte_cnt,
  408. stop_byte_cnt);
  409. m_last_rx_byte_cnt = endrx_byte_cnt;
  410. m_last_pin_rx_byte_cnt = stop_byte_cnt;
  411. if (dma_amount || pin_amount)
  412. {
  413. uint32_t chunk0 = (dma_amount > m_chunk_size) ? m_chunk_size : dma_amount;
  414. uint32_t chunk1 = dma_amount - chunk0;
  415. NRF_LOG_DEBUG("RX END chunk0:%d, chunk1:%d, data[0]=%d %d",
  416. chunk0,
  417. chunk1,
  418. mp_cur_rx[0],
  419. mp_cur_rx[1]);
  420. nrf_libuarte_evt_t evt = {
  421. .type = NRF_LIBUARTE_EVT_RX_DATA,
  422. .data = {
  423. .rxtx = {
  424. .p_data = mp_cur_rx,
  425. .length = chunk0
  426. }
  427. }
  428. };
  429. mp_cur_rx = mp_next_rx;
  430. mp_next_rx = NULL;
  431. if (mp_next_next_rx)
  432. {
  433. mp_next_rx = mp_next_next_rx;
  434. mp_next_next_rx = NULL;
  435. nrf_uarte_rx_buffer_set(UART_DRV_UARTE, mp_next_rx, m_chunk_size);
  436. }
  437. m_evt_handler(&evt);
  438. if ( chunk1 ||
  439. ((dma_amount == m_chunk_size) && (endrx_byte_cnt == stop_byte_cnt)))
  440. {
  441. NRF_LOG_WARNING("RX END Chunk1:%d", chunk1);
  442. evt.data.rxtx.length = chunk1;
  443. evt.data.rxtx.p_data = mp_cur_rx;
  444. mp_cur_rx = mp_next_rx;
  445. mp_next_rx = NULL;
  446. m_evt_handler(&evt);
  447. }
  448. }
  449. }
  450. if (nrf_uarte_event_check(UART_DRV_UARTE, NRF_UARTE_EVENT_TXSTOPPED))
  451. {
  452. nrf_uarte_event_clear(UART_DRV_UARTE, NRF_UARTE_EVENT_TXSTOPPED);
  453. nrf_libuarte_evt_t evt = {
  454. .type = NRF_LIBUARTE_EVT_TX_DONE,
  455. .data = {
  456. .rxtx = {
  457. .p_data = mp_tx,
  458. .length = m_tx_len
  459. }
  460. }
  461. };
  462. mp_tx = NULL;
  463. m_evt_handler(&evt);
  464. }
  465. if (nrf_uarte_event_check(UART_DRV_UARTE, NRF_UARTE_EVENT_ENDTX))
  466. {
  467. nrf_uarte_event_clear(UART_DRV_UARTE, NRF_UARTE_EVENT_ENDTX);
  468. uint8_t amount = nrf_uarte_tx_amount_get(UART_DRV_UARTE);
  469. NRF_LOG_DEBUG("(evt) TX completed (%d)", amount);
  470. m_tx_cur_idx += amount;
  471. if (m_tx_cur_idx == m_tx_len)
  472. {
  473. nrf_uarte_event_clear(UART_DRV_UARTE, NRF_UARTE_EVENT_TXSTOPPED);
  474. nrf_uarte_task_trigger(UART_DRV_UARTE, NRF_UARTE_TASK_STOPTX);
  475. }
  476. else
  477. {
  478. size_t rem_len = (m_tx_len - m_tx_cur_idx);
  479. if ( rem_len <= MAX_DMA_XFER_LEN)
  480. {
  481. nrfx_err_t err = nrfx_ppi_channel_disable(m_ppi_channels[PPI_CH_ENDTX_STARTTX]);
  482. ASSERT(err == NRFX_SUCCESS);
  483. }
  484. else
  485. {
  486. if (nrf_uarte_event_check(UART_DRV_UARTE, NRF_UARTE_EVENT_TXSTARTED) == 0)
  487. {
  488. NRF_LOG_ERROR("Tx not started yet!");
  489. ASSERT(false);
  490. }
  491. nrf_uarte_event_clear(UART_DRV_UARTE, NRF_UARTE_EVENT_TXSTARTED);
  492. nrf_uarte_tx_buffer_set(UART_DRV_UARTE, &mp_tx[m_tx_cur_idx + m_tx_chunk8], m_tx_chunk8);
  493. }
  494. }
  495. }
  496. }