nrf_libuarte_async.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /**
  2. * Copyright (c) 2019 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef UART_ASYNC_H
  41. #define UART_ASYNC_H
  42. #include <stdint.h>
  43. #include "sdk_errors.h"
  44. #include "nrf_balloc.h"
  45. #include "nrf_queue.h"
  46. #include "nrfx_ppi.h"
  47. #include "nrfx_timer.h"
  48. #include "nrfx_rtc.h"
  49. #include "nrf_libuarte_drv.h"
  50. #include <hal/nrf_uarte.h>
  51. /**
  52. * @defgroup nrf_libuarte_async libUARTE asynchronous library
  53. * @ingroup app_common
  54. *
  55. * @brief Module for reliable communication over UARTE.
  56. *
  57. * @{
  58. */
  59. /* Safe guard for sdk_config.h now up to date. */
  60. #ifndef NRF_LIBUARTE_ASYNC_WITH_APP_TIMER
  61. #warning "sdk_config.h is missing NRF_LIBUARTE_ASYNC_WITH_APP_TIMER option"
  62. #define NRF_LIBUARTE_ASYNC_WITH_APP_TIMER 0
  63. #endif
  64. #if NRF_LIBUARTE_ASYNC_WITH_APP_TIMER
  65. #include "app_timer.h"
  66. #endif
  67. /** @brief Types of libuarte driver events. */
  68. typedef enum
  69. {
  70. NRF_LIBUARTE_ASYNC_EVT_RX_DATA, ///< Requested TX transfer completed.
  71. NRF_LIBUARTE_ASYNC_EVT_TX_DONE, ///< Requested RX transfer completed.
  72. NRF_LIBUARTE_ASYNC_EVT_ERROR, ///< Error reported by UARTE peripheral.
  73. NRF_LIBUARTE_ASYNC_EVT_OVERRUN_ERROR ///< Error reported by the driver.
  74. } nrf_libuarte_async_evt_type_t;
  75. typedef enum
  76. {
  77. NRF_LIBUARTE_ASYNC_PPI_CH_RXRDY_CLEAR,
  78. NRF_LIBUARTE_ASYNC_PPI_CH_COMPARE_SHUTDOWN,
  79. NRF_LIBUARTE_ASYNC_PPI_CH_MAX
  80. } nrf_libuarte_async_ppi_channel_t;
  81. /** @brief Structure for libuarte async transfer completion event. */
  82. typedef struct
  83. {
  84. uint8_t * p_data; ///< Pointer to memory used for transfer.
  85. size_t length; ///< Number of bytes transfered.
  86. } nrf_libuarte_async_data_t;
  87. /** @brief Structu for software error event. */
  88. typedef struct
  89. {
  90. uint32_t overrun_length; ///< Number of bytes lost due to overrun.
  91. } nrf_libuarte_async_overrun_err_evt_t;
  92. /** @brief Structure for libuarte error event. */
  93. typedef struct
  94. {
  95. nrf_libuarte_async_evt_type_t type; ///< Event type.
  96. union {
  97. nrf_libuarte_async_data_t rxtx; ///< RXD/TXD data.
  98. uint8_t errorsrc; ///< Error source.
  99. nrf_libuarte_async_overrun_err_evt_t overrun_err; ///< Overrun error data.
  100. } data; ///< Union with data.
  101. } nrf_libuarte_async_evt_t;
  102. /**
  103. * @brief Interrupt event handler.
  104. *
  105. * @param[in] p_evt Pointer to event structure. Event is allocated on the stack so it is available
  106. * only within the context of the event handler.
  107. */
  108. typedef void (*nrf_libuarte_async_evt_handler_t)(void * context, nrf_libuarte_async_evt_t * p_evt);
  109. /** @brief Structure for libuarte async configuration. */
  110. typedef struct
  111. {
  112. uint32_t rx_pin; ///< RXD pin number.
  113. uint32_t tx_pin; ///< TXD pin number.
  114. uint32_t cts_pin; ///< CTS pin number.
  115. uint32_t rts_pin; ///< RTS pin number.
  116. uint32_t timeout_us; ///< Receiver timeout in us unit.
  117. nrf_uarte_hwfc_t hwfc; ///< Flow control configuration.
  118. nrf_uarte_parity_t parity; ///< Parity configuration.
  119. nrf_uarte_baudrate_t baudrate; ///< Baudrate.
  120. bool pullup_rx; ///< Pull up on RX pin.
  121. uint8_t int_prio; ///< Interrupt priority of UARTE (RTC, TIMER have int_prio - 1)
  122. } nrf_libuarte_async_config_t;
  123. /**
  124. * @brief nrf_libuarte_async control block (placed in RAM).
  125. */
  126. typedef struct {
  127. nrf_libuarte_async_evt_handler_t evt_handler;
  128. void * context;
  129. nrf_ppi_channel_t ppi_channels[NRF_LIBUARTE_ASYNC_PPI_CH_MAX];
  130. int32_t alloc_cnt;
  131. uint32_t rx_count;
  132. uint32_t sub_rx_count;
  133. uint8_t * p_curr_rx_buf;
  134. uint32_t rx_free_cnt;
  135. uint32_t timeout_us;
  136. bool app_timer_created;
  137. bool hwfc;
  138. bool rx_halted;
  139. bool enabled;
  140. } nrf_libuarte_async_ctrl_blk_t;
  141. typedef struct {
  142. uint32_t rx_count;
  143. uint32_t timestamp;
  144. bool activate;
  145. } nrf_libuarte_app_timer_ctrl_blk_t;
  146. /**
  147. * @brief nrf_libuarte_async instance structure (placed in ROM).
  148. */
  149. typedef struct {
  150. const nrf_balloc_t * p_rx_pool;
  151. const nrf_queue_t * p_rx_queue;
  152. const nrfx_rtc_t * p_rtc;
  153. const nrfx_timer_t * p_timer;
  154. #if NRF_LIBUARTE_ASYNC_WITH_APP_TIMER
  155. const app_timer_id_t * p_app_timer;
  156. #else
  157. void ** p_app_timer;
  158. #endif
  159. nrf_libuarte_app_timer_ctrl_blk_t * p_app_timer_ctrl_blk;
  160. const nrf_libuarte_drv_t * p_libuarte;
  161. nrf_libuarte_async_ctrl_blk_t * p_ctrl_blk;
  162. nrfx_rtc_handler_t rtc_handler;
  163. uint32_t rx_buf_size;
  164. } nrf_libuarte_async_t;
  165. void nrf_libuarte_async_timeout_handler(const nrf_libuarte_async_t * p_libuarte);
  166. #define NRF_LIBUARTE_PERIPHERAL_NOT_USED 255
  167. #define LIBUARTE_ASYNC_DEBRACKET(...) __VA_ARGS__
  168. #define __LIBUARTE_ASYNC_ARG_2(ignore_this, val, ...) val
  169. #define __LIBUARTE_ASYNC_ARG_2_DEBRACKET(ignore_this, val, ...) LIBUARTE_ASYNC_DEBRACKET val
  170. /* Macro for injecting code based on flag evaluation. If flag exists and equals 1
  171. * then first code is compiled in, else second option. Note that code must be
  172. * in the brackets. Example usage:
  173. * _LIBUARTE_ASYNC_EVAL(MY_FLAG, (foo();), () )
  174. * If MY_FLAG exists and equals 1 then macros resolves to foo(); call, else it resolves to
  175. * empty line.
  176. *
  177. * @param _eval_level Flag to be evaluated. It's positively evaluated if exists and equals 1.
  178. * @param _iftrue Macro is resolved to that code on positive flag evaluation. Code must be
  179. * in the brackets.
  180. * @param _iffalse Macro is resolved to that code on negative flag evaluation. Code must be
  181. * in the brackets.
  182. */
  183. #define _LIBUARTE_ASYNC_EVAL(_eval_flag, _iftrue, _iffalse) \
  184. _LIBUARTE_ASYNC_EVAL1(_eval_flag, _iftrue, _iffalse)
  185. #define _LIBUARTE_ASYNC_EVAL1(_eval_flag, _iftrue, _iffalse) \
  186. _LIBUARTE_ASYNC_EVAL2(_LIBUARTE_ASYNC_ZZZZ##_eval_flag, _iftrue, _iffalse)
  187. #define _LIBUARTE_ASYNC_ZZZZ1 _LIBUARTE_ASYNC_YYYY,
  188. #define _LIBUARTE_ASYNC_EVAL2(one_or_two_args, _iftrue, _iffalse) \
  189. __LIBUARTE_ASYNC_ARG_2_DEBRACKET(one_or_two_args _iftrue, _iffalse)
  190. /**
  191. * @brief Macro for creating instance of libuarte_async.
  192. *
  193. * Libuarte_async requires one timer-like peripheral (RTC or TIMER) for triggering RX timeout.
  194. * Macro will create instance only for peripheral which is used.
  195. *
  196. * @param _name Instance name.
  197. * @param _uarte_idx UARTE instance used.
  198. * @param _timer0_idx TIMER instance used by libuarte for bytes counting.
  199. * @param _rtc1_idx RTC instance used for timeout. If set to NRF_LIBUARTE_PERIPHERAL_NOT_USED
  200. * then TIMER instance is used or app_timer instance if _timer1_idx is also set
  201. * to NRF_LIBUARTE_PERIPHERAL_NOT_USED.
  202. * @param _timer1_idx TIMER instance used for timeout. If set to NRF_LIBUARTE_PERIPHERAL_NOT_USED
  203. * then RTC instance is used or app_timer instance if _rtc1_idx is also set
  204. * to NRF_LIBUARTE_PERIPHERAL_NOT_USED.
  205. * @param _rx_buf_size Size of single RX buffer. Size impacts accepted latency between buffer
  206. * request and providing next buffer. Next must be provided within before
  207. * _rx_buf_size bytes is received.
  208. * @param _rx_buf_cnt Number of buffers in the RX buffer pool. Size impacts accepted latency
  209. * between NRF_LIBUARTE_ASYNC_EVT_RX_DATA event and
  210. * @ref nrf_libuarte_async_rx_free.
  211. */
  212. #define NRF_LIBUARTE_ASYNC_DEFINE(_name, _uarte_idx, _timer0_idx,\
  213. _rtc1_idx, _timer1_idx,\
  214. _rx_buf_size, _rx_buf_cnt) \
  215. STATIC_ASSERT(_rx_buf_cnt >= 3, "Wrong number of RX buffers");\
  216. STATIC_ASSERT(!((NRF_LIBUARTE_ASYNC_WITH_APP_TIMER == 0) && \
  217. (_rtc1_idx == NRF_LIBUARTE_PERIPHERAL_NOT_USED) && \
  218. (_timer1_idx == NRF_LIBUARTE_PERIPHERAL_NOT_USED)), \
  219. "App timer support disabled");\
  220. NRF_LIBUARTE_DRV_DEFINE(CONCAT_2(_name, _libuarte), _uarte_idx, _timer0_idx);\
  221. NRF_QUEUE_DEF(uint8_t *, CONCAT_2(_name,_rxdata_queue), _rx_buf_cnt, NRF_QUEUE_MODE_NO_OVERFLOW);\
  222. NRF_BALLOC_DEF(CONCAT_2(_name,_rx_pool), _rx_buf_size, _rx_buf_cnt);\
  223. /* Create TIMER instance only if _timer1_idx != NRF_LIBUARTE_PERIPHERAL_NOT_USED */ \
  224. _LIBUARTE_ASYNC_EVAL(\
  225. NRFX_CONCAT_3(NRFX_TIMER, _timer1_idx, _ENABLED),\
  226. (STATIC_ASSERT((_timer1_idx == NRF_LIBUARTE_PERIPHERAL_NOT_USED) || (CONCAT_3(NRFX_TIMER,_timer1_idx, _ENABLED) == 1), "TIMER instance not enabled");\
  227. static const nrfx_timer_t CONCAT_2(_name, _timer) = NRFX_TIMER_INSTANCE(_timer1_idx);),\
  228. (/* empty */))\
  229. /* Create RTC instance only if _timer1_idx != NRF_LIBUARTE_PERIPHERAL_NOT_USED */ \
  230. _LIBUARTE_ASYNC_EVAL(\
  231. NRFX_CONCAT_3(NRFX_RTC, _rtc1_idx, _ENABLED),\
  232. (STATIC_ASSERT((_rtc1_idx == NRF_LIBUARTE_PERIPHERAL_NOT_USED) || (CONCAT_3(NRFX_RTC,_rtc1_idx, _ENABLED) == 1), "RTC instance not enabled");\
  233. static const nrfx_rtc_t CONCAT_2(_name, _rtc) = NRFX_RTC_INSTANCE(_rtc1_idx);),\
  234. (/* empty */))\
  235. _LIBUARTE_ASYNC_EVAL(NRFX_CONCAT_3(NRFX_TIMER, _timer1_idx, _ENABLED),\
  236. (/* empty */),\
  237. (_LIBUARTE_ASYNC_EVAL(NRFX_CONCAT_3(NRFX_RTC, _rtc1_idx, _ENABLED),(/* empty */), \
  238. (APP_TIMER_DEF(CONCAT_2(_name,_app_timer)); \
  239. nrf_libuarte_app_timer_ctrl_blk_t CONCAT_2(_name,_app_timer_ctrl_blk);))) \
  240. )\
  241. static nrf_libuarte_async_ctrl_blk_t CONCAT_2(_name, ctrl_blk);\
  242. _LIBUARTE_ASYNC_EVAL(\
  243. NRFX_CONCAT_3(NRFX_RTC, _rtc1_idx, _ENABLED), \
  244. (static void CONCAT_2(_name, _rtc_handler)(nrfx_rtc_int_type_t int_type);),\
  245. (/* empty */)) \
  246. \
  247. static const nrf_libuarte_async_t _name = {\
  248. .p_rx_pool = &CONCAT_2(_name,_rx_pool),\
  249. .p_rx_queue = &CONCAT_2(_name,_rxdata_queue),\
  250. /* If p_rtc is not NULL it means that RTC is used for RX timeout */ \
  251. .p_rtc = _LIBUARTE_ASYNC_EVAL(NRFX_CONCAT_3(NRFX_RTC, _rtc1_idx, _ENABLED), (&CONCAT_2(_name, _rtc)), (NULL)),\
  252. /* If p_timer is not NULL it means that RTC is used for RX timeout */ \
  253. .p_timer = _LIBUARTE_ASYNC_EVAL(NRFX_CONCAT_3(NRFX_TIMER, _timer1_idx, _ENABLED), (&CONCAT_2(_name, _timer)), (NULL)),\
  254. /* If p_time and p_rtc is NULL it means that app_timer is used for RX timeout */ \
  255. .p_app_timer = _LIBUARTE_ASYNC_EVAL(NRFX_CONCAT_3(NRFX_TIMER, _timer1_idx, _ENABLED),\
  256. (NULL),\
  257. (_LIBUARTE_ASYNC_EVAL(NRFX_CONCAT_3(NRFX_RTC, _rtc1_idx, _ENABLED),(NULL), \
  258. (&CONCAT_2(_name,_app_timer)))) \
  259. ),\
  260. .p_app_timer_ctrl_blk = _LIBUARTE_ASYNC_EVAL(NRFX_CONCAT_3(NRFX_TIMER, _timer1_idx, _ENABLED),\
  261. (NULL),\
  262. (_LIBUARTE_ASYNC_EVAL(NRFX_CONCAT_3(NRFX_RTC, _rtc1_idx, _ENABLED),(NULL), \
  263. (&CONCAT_2(_name,_app_timer_ctrl_blk)))) \
  264. ),\
  265. .p_libuarte = &CONCAT_2(_name, _libuarte),\
  266. .p_ctrl_blk = &CONCAT_2(_name, ctrl_blk),\
  267. .rx_buf_size = _rx_buf_size,\
  268. _LIBUARTE_ASYNC_EVAL(\
  269. NRFX_CONCAT_3(NRFX_RTC, _rtc1_idx, _ENABLED),\
  270. (.rtc_handler =CONCAT_2(_name, _rtc_handler)),\
  271. ()\
  272. )\
  273. };\
  274. /* RTC compare event is not periodic but need to be enabled again in the callback. */ \
  275. _LIBUARTE_ASYNC_EVAL(\
  276. NRFX_CONCAT_3(NRFX_RTC, _rtc1_idx, _ENABLED),\
  277. (\
  278. static void CONCAT_2(_name, _rtc_handler)(nrfx_rtc_int_type_t int_type)\
  279. { \
  280. (void)nrfx_rtc_cc_set(_name.p_rtc, 0, _name.p_ctrl_blk->timeout_us/32, true);\
  281. nrf_libuarte_async_timeout_handler(&_name);\
  282. }\
  283. ),\
  284. ()\
  285. )
  286. /**
  287. * @brief Function for initializing the libuarte async library.
  288. *
  289. * @param[in] p_libuarte Libuarte_async instance.
  290. * @param[in] p_config Pointer to the structure with initial configuration.
  291. * @param[in] evt_handler Event handler provided by the user. Must not be NULL.
  292. * @param[in] context User context passed to the event handler.
  293. *
  294. * @return NRF_SUCCESS when properly initialized. NRF_ERROR_INTERNAL otherwise.
  295. */
  296. ret_code_t nrf_libuarte_async_init(const nrf_libuarte_async_t * const p_libuarte,
  297. nrf_libuarte_async_config_t const * p_config,
  298. nrf_libuarte_async_evt_handler_t evt_handler,
  299. void * context);
  300. /** @brief Function for uninitializing the libuarte async library.
  301. *
  302. * @param[in] p_libuarte Libuarte_async instance.
  303. */
  304. void nrf_libuarte_async_uninit(const nrf_libuarte_async_t * const p_libuarte);
  305. /**
  306. * @brief Function for enabling receiver.
  307. *
  308. * @param p_libuarte Libuarte_async instance.
  309. */
  310. void nrf_libuarte_async_enable(const nrf_libuarte_async_t * const p_libuarte);
  311. /**
  312. * @brief Function for deasserting RTS to pause the transmission.
  313. *
  314. * Flow control must be enabled.
  315. *
  316. * @param p_libuarte Libuarte_async instance.
  317. */
  318. void nrf_libuarte_async_rts_clear(const nrf_libuarte_async_t * const p_libuarte);
  319. /**
  320. * @brief Function for asserting RTS to restart the transmission.
  321. *
  322. * Flow control must be enabled.
  323. *
  324. * @param p_libuarte Libuarte_async instance.
  325. */
  326. void nrf_libuarte_async_rts_set(const nrf_libuarte_async_t * const p_libuarte);
  327. /**
  328. * @brief Function for sending data asynchronously over UARTE.
  329. *
  330. * @param[in] p_libuarte Libuarte_async instance.
  331. * @param[in] p_data Pointer to data.
  332. * @param[in] length Number of bytes to send. Maximum possible length is
  333. * dependent on the used SoC (see the MAXCNT register
  334. * description in the Product Specification). The library
  335. * checks it with assertion.
  336. *
  337. * @retval NRF_ERROR_BUSY Data is transferring.
  338. * @retval NRF_ERROR_INTERNAL Error during configuration.
  339. * @retval NRF_SUCCESS Buffer set for sending.
  340. */
  341. ret_code_t nrf_libuarte_async_tx(const nrf_libuarte_async_t * const p_libuarte,
  342. uint8_t * p_data, size_t length);
  343. /**
  344. * @brief Function for deallocating received buffer data.
  345. *
  346. * @param[in] p_libuarte Libuarte_async instance.
  347. * @param[in] p_data Pointer to data.
  348. * @param[in] length Number of bytes to free.
  349. */
  350. void nrf_libuarte_async_rx_free(const nrf_libuarte_async_t * const p_libuarte,
  351. uint8_t * p_data, size_t length);
  352. /** @} */
  353. #endif //UART_ASYNC_H