nrf_libuarte_async.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /**
  2. * Copyright (c) 2018 - 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. #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.h"
  50. #include <hal/nrf_uarte.h>
  51. /** @brief Types of libuarte driver events. */
  52. typedef enum
  53. {
  54. NRF_LIBUARTE_ASYNC_EVT_RX_DATA, ///< Requested TX transfer completed.
  55. NRF_LIBUARTE_ASYNC_EVT_TX_DONE, ///< Requested RX transfer completed.
  56. NRF_LIBUARTE_ASYNC_EVT_ERROR ///< Error reported by UARTE peripheral.
  57. } nrf_libuarte_async_evt_type_t;
  58. typedef enum
  59. {
  60. NRF_LIBUARTE_ASYNC_PPI_CH_RXRDY_CLEAR,
  61. NRF_LIBUARTE_ASYNC_PPI_CH_COMPARE_SHUTDOWN,
  62. NRF_LIBUARTE_ASYNC_PPI_CH_MAX
  63. } nrf_libuarte_async_ppi_channel_t;
  64. /** @brief Structure for libuarte async transfer completion event. */
  65. typedef struct
  66. {
  67. uint8_t * p_data; ///< Pointer to memory used for transfer.
  68. size_t length; ///< Number of bytes transfered.
  69. } nrf_libuarte_async_data_t;
  70. /** @brief Structure for libuarte error event. */
  71. typedef struct
  72. {
  73. nrf_libuarte_async_evt_type_t type; ///< Event type.
  74. union {
  75. nrf_libuarte_async_data_t rxtx; ///< RXD/TXD data.
  76. } data; ///< Union with data.
  77. } nrf_libuarte_async_evt_t;
  78. /**
  79. * @brief Interrupt event handler.
  80. *
  81. * @param[in] p_evt Pointer to event structure. Event is allocated on the stack so it is available
  82. * only within the context of the event handler.
  83. */
  84. typedef void (*nrf_libuarte_async_evt_handler_t)(void * context, nrf_libuarte_async_evt_t * p_evt);
  85. /** @brief Structure for libuarte async configuration. */
  86. typedef struct
  87. {
  88. uint32_t rx_pin; ///< RXD pin number.
  89. uint32_t tx_pin; ///< TXD pin number.
  90. uint32_t cts_pin; ///< CTS pin number.
  91. uint32_t rts_pin; ///< RTS pin number.
  92. uint32_t timeout_us; ///< Receiver timeout in us unit.
  93. nrf_uarte_hwfc_t hwfc; ///< Flow control configuration.
  94. nrf_uarte_parity_t parity; ///< Parity configuration.
  95. nrf_uarte_baudrate_t baudrate; ///< Baudrate.
  96. } nrf_libuarte_async_config_t;
  97. /**
  98. * @brief nrf_libuarte_async control block (placed in RAM).
  99. */
  100. typedef struct {
  101. nrf_libuarte_async_evt_handler_t evt_handler;
  102. void * context;
  103. nrf_ppi_channel_t ppi_channels[NRF_LIBUARTE_ASYNC_PPI_CH_MAX];
  104. int32_t alloc_cnt;
  105. uint32_t rx_count;
  106. uint32_t sub_rx_count;
  107. uint8_t * p_curr_rx_buf;
  108. uint32_t rx_free_cnt;
  109. size_t rx_chunk_size;
  110. uint32_t timeout_us;
  111. } nrf_libuarte_async_ctrl_blk_t;
  112. /**
  113. * @brief nrf_libuarte_async instance structure (placed in ROM).
  114. */
  115. typedef struct {
  116. const nrf_balloc_t * p_rx_pool;
  117. const nrf_queue_t * p_rx_queue;
  118. const nrfx_rtc_t * p_rtc;
  119. const nrfx_timer_t * p_timer;
  120. const nrf_libuarte_t * p_libuarte;
  121. nrf_libuarte_async_ctrl_blk_t * p_ctrl_blk;
  122. nrfx_rtc_handler_t rtc_handler;
  123. } nrf_libuarte_async_t;
  124. void nrf_libuarte_async_timeout_handler(const nrf_libuarte_async_t * p_libuarte);
  125. #define NRF_LIBUARTE_PERIPHERAL_NOT_USED 255
  126. #define LIBUARTE_ASYNC_DEBRACKET(...) __VA_ARGS__
  127. #define __LIBUARTE_ASYNC_ARG_2(ignore_this, val, ...) val
  128. #define __LIBUARTE_ASYNC_ARG_2_DEBRACKET(ignore_this, val, ...) LIBUARTE_ASYNC_DEBRACKET val
  129. /* Macro for injecting code based on flag evaluation. If flag exists and equals 1
  130. * then first code is compiled in, else second option. Note that code must be
  131. * in the brackets. Example usage:
  132. * _LIBUARTE_ASYNC_EVAL(MY_FLAG, (foo();), () )
  133. * If MY_FLAG exists and equals 1 then macros resolves to foo(); call, else it resolves to
  134. * empty line.
  135. *
  136. * @param _eval_level Flag to be evaluated. It's positively evaluated if exists and equals 1.
  137. * @param _iftrue Macro is resolved to that code on positive flag evaluation. Code must be
  138. * in the brackets.
  139. * @param _iffalse Macro is resolved to that code on negative flag evaluation. Code must be
  140. * in the brackets.
  141. */
  142. #define _LIBUARTE_ASYNC_EVAL(_eval_flag, _iftrue, _iffalse) \
  143. _LIBUARTE_ASYNC_EVAL1(_eval_flag, _iftrue, _iffalse)
  144. #define _LIBUARTE_ASYNC_EVAL1(_eval_flag, _iftrue, _iffalse) \
  145. _LIBUARTE_ASYNC_EVAL2(_LIBUARTE_ASYNC_ZZZZ##_eval_flag, _iftrue, _iffalse)
  146. #define _LIBUARTE_ASYNC_ZZZZ1 _LIBUARTE_ASYNC_YYYY,
  147. #define _LIBUARTE_ASYNC_EVAL2(one_or_two_args, _iftrue, _iffalse) \
  148. __LIBUARTE_ASYNC_ARG_2_DEBRACKET(one_or_two_args _iftrue, _iffalse)
  149. /**
  150. * @brief Macro for creating instance of libuarte_async.
  151. *
  152. * Libuarte_async requires one timer-like peripheral (RTC or TIMER) for triggering RX timeout.
  153. * Macro will create instance only for peripheral which is used.
  154. *
  155. * @param _name Instance name.
  156. * @param _uarte_idx UARTE instance used.
  157. * @param _timer0_idx TIMER instance used by libuarte for bytes counting.
  158. * @param _rtc1_idx RTC instance used for timeout, set to NRF_LIBUARTE_PERIPHERAL_NOT_USED
  159. * if TIMER instance is used.
  160. * @param _timer1_idx TIMER instance used for timeout, set to NRF_LIBUARTE_PERIPHERAL_NOT_USED
  161. * if RTC instance is used.
  162. * @param _rx_buf_size Size of single RX buffer. Size impacts accepted latency between buffer
  163. * request and providing next buffer. Next must be provided within before
  164. * _rx_buf_size bytes is received.
  165. * @param _rx_buf_cnt Number of buffers in the RX buffer pool. Size impacts accepted latency
  166. * between NRF_LIBUARTE_ASYNC_EVT_RX_DATA event and
  167. * @ref nrf_libuarte_async_rx_free.
  168. */
  169. #define NRF_LIBUARTE_ASYNC_DEFINE(_name, _uarte_idx, _timer0_idx,\
  170. _rtc1_idx, _timer1_idx,\
  171. _rx_buf_size, _rx_buf_cnt) \
  172. STATIC_ASSERT(_rx_buf_cnt >= 3, "Wrong number of RX buffers");\
  173. NRF_LIBUARTE_DEFINE(CONCAT_2(_name, _libuarte), _uarte_idx, _timer0_idx);\
  174. NRF_QUEUE_DEF(uint8_t *, CONCAT_2(_name,_rxdata_queue), _rx_buf_cnt, NRF_QUEUE_MODE_NO_OVERFLOW);\
  175. NRF_BALLOC_DEF(CONCAT_2(_name,_rx_pool), _rx_buf_size, _rx_buf_cnt);\
  176. /* Create TIMER instance only if _timer1_idx != NRF_LIBUARTE_PERIPHERAL_NOT_USED */ \
  177. _LIBUARTE_ASYNC_EVAL(\
  178. NRFX_CONCAT_3(NRFX_TIMER, _timer1_idx, _ENABLED),\
  179. (STATIC_ASSERT((_timer1_idx == NRF_LIBUARTE_PERIPHERAL_NOT_USED) || (CONCAT_3(NRFX_TIMER,_timer1_idx, _ENABLED) == 1), "TIMER instance not enabled");\
  180. static const nrfx_timer_t CONCAT_2(_name, _timer) = NRFX_TIMER_INSTANCE(_timer1_idx);),\
  181. (/* empty */))\
  182. /* Create RTC instance only if _timer1_idx != NRF_LIBUARTE_PERIPHERAL_NOT_USED */ \
  183. _LIBUARTE_ASYNC_EVAL(\
  184. NRFX_CONCAT_3(NRFX_RTC, _rtc1_idx, _ENABLED),\
  185. (STATIC_ASSERT((_rtc1_idx == NRF_LIBUARTE_PERIPHERAL_NOT_USED) || (CONCAT_3(NRFX_RTC,_rtc1_idx, _ENABLED) == 1), "RTC instance not enabled");\
  186. static const nrfx_rtc_t CONCAT_2(_name, _rtc) = NRFX_RTC_INSTANCE(_rtc1_idx);),\
  187. (/* empty */))\
  188. static nrf_libuarte_async_ctrl_blk_t CONCAT_2(_name, ctrl_blk);\
  189. static void CONCAT_2(_name, _rtc_handler)(nrfx_rtc_int_type_t int_type);\
  190. static nrf_libuarte_async_t _name = {\
  191. .p_rx_pool = &CONCAT_2(_name,_rx_pool),\
  192. .p_rx_queue = &CONCAT_2(_name,_rxdata_queue),\
  193. /* If p_rtc is not NULL it means that RTC is used for RX timeout */ \
  194. .p_rtc = _LIBUARTE_ASYNC_EVAL(NRFX_CONCAT_3(NRFX_RTC, _rtc1_idx, _ENABLED), (&CONCAT_2(_name, _rtc)), (NULL)),\
  195. /* If p_timer is not NULL it means that RTC is used for RX timeout */ \
  196. .p_timer = _LIBUARTE_ASYNC_EVAL(NRFX_CONCAT_3(NRFX_TIMER, _timer1_idx, _ENABLED), (&CONCAT_2(_name, _timer)), (NULL)),\
  197. .p_libuarte = &CONCAT_2(_name, _libuarte),\
  198. .p_ctrl_blk = &CONCAT_2(_name, ctrl_blk),\
  199. _LIBUARTE_ASYNC_EVAL(\
  200. NRFX_CONCAT_3(NRFX_RTC, _rtc1_idx, _ENABLED),\
  201. (.rtc_handler =CONCAT_2(_name, _rtc_handler)),\
  202. ()\
  203. )\
  204. };\
  205. /* RTC compare event is not periodic but need to be enabled again in the callback. */ \
  206. _LIBUARTE_ASYNC_EVAL(\
  207. NRFX_CONCAT_3(NRFX_RTC, _rtc1_idx, _ENABLED),\
  208. (\
  209. static void CONCAT_2(_name, _rtc_handler)(nrfx_rtc_int_type_t int_type)\
  210. { \
  211. (void)nrfx_rtc_cc_set(_name.p_rtc, 0, 32 * _name.p_ctrl_blk->timeout_us, true);\
  212. nrf_libuarte_async_timeout_handler(&_name);\
  213. }\
  214. ),\
  215. ()\
  216. )
  217. /**
  218. * @brief Function for initializing the libuarte async library.
  219. *
  220. * @param[in] p_libuarte Libuarte_async instance.
  221. * @param[in] p_config Pointer to the structure with initial configuration.
  222. * @param[in] evt_handler Event handler provided by the user. Must not be NULL.
  223. *
  224. * @return NRF_SUCCESS when properly initialized. NRF_ERROR_INTERNAL otherwise.
  225. */
  226. ret_code_t nrf_libuarte_async_init(const nrf_libuarte_async_t * const p_libuarte,
  227. nrf_libuarte_async_config_t const * p_config,
  228. nrf_libuarte_async_evt_handler_t evt_handler,
  229. void * context);
  230. /** @brief Function for uninitializing the libuarte async library.
  231. *
  232. * @param[in] p_libuarte Libuarte_async instance.
  233. */
  234. void nrf_libuarte_async_uninit(const nrf_libuarte_async_t * const p_libuarte);
  235. /**
  236. * @brief Function for enabling receiver.
  237. *
  238. * @param p_libuarte Libuarte_async instance.
  239. */
  240. void nrf_libuarte_async_enable(const nrf_libuarte_async_t * const p_libuarte);
  241. /**
  242. * @brief Function for sending data asynchronously over UARTE.
  243. *
  244. * @param[in] p_libuarte Libuarte_async instance.
  245. * @param[in] p_data Pointer to data.
  246. * @param[in] length Number of bytes to send. Maximum possible length is
  247. * dependent on the used SoC (see the MAXCNT register
  248. * description in the Product Specification). The library
  249. * checks it with assertion.
  250. *
  251. * @retval NRF_ERROR_BUSY Data is transferring.
  252. * @retval NRF_ERROR_INTERNAL Error during configuration.
  253. * @retval NRF_SUCCESS Buffer set for sending.
  254. */
  255. ret_code_t nrf_libuarte_async_tx(const nrf_libuarte_async_t * const p_libuarte,
  256. uint8_t * p_data, size_t length);
  257. /**
  258. * @brief Function for deallocating received buffer data.
  259. *
  260. * @param[in] p_libuarte Libuarte_async instance.
  261. * @param[in] p_data Pointer to data.
  262. * @param[in] length Number of bytes to free.
  263. */
  264. void nrf_libuarte_async_rx_free(const nrf_libuarte_async_t * const p_libuarte,
  265. uint8_t * p_data, size_t length);
  266. #endif //UART_ASYNC_H