nrfx_uart.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /**
  2. * Copyright (c) 2015 - 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 NRFX_UART_H__
  41. #define NRFX_UART_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_uart.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @defgroup nrfx_uart UART driver
  49. * @{
  50. * @ingroup nrf_uart
  51. * @brief UART peripheral driver.
  52. */
  53. /**
  54. * @brief UART driver instance data structure.
  55. */
  56. typedef struct
  57. {
  58. NRF_UART_Type * p_reg; ///< Pointer to a structure with UART registers.
  59. uint8_t drv_inst_idx; ///< Driver instance index.
  60. } nrfx_uart_t;
  61. enum {
  62. #if NRFX_CHECK(NRFX_UART0_ENABLED)
  63. NRFX_UART0_INST_IDX,
  64. #endif
  65. NRFX_UART_ENABLED_COUNT
  66. };
  67. /**
  68. * @brief Macro for creating a UART driver instance.
  69. */
  70. #define NRFX_UART_INSTANCE(id) \
  71. { \
  72. .p_reg = NRFX_CONCAT_2(NRF_UART, id), \
  73. .drv_inst_idx = NRFX_CONCAT_3(NRFX_UART, id, _INST_IDX), \
  74. }
  75. /**
  76. * @brief Types of UART driver events.
  77. */
  78. typedef enum
  79. {
  80. NRFX_UART_EVT_TX_DONE, ///< Requested TX transfer completed.
  81. NRFX_UART_EVT_RX_DONE, ///< Requested RX transfer completed.
  82. NRFX_UART_EVT_ERROR, ///< Error reported by UART peripheral.
  83. } nrfx_uart_evt_type_t;
  84. /**
  85. * @brief Structure for UART configuration.
  86. */
  87. typedef struct
  88. {
  89. uint32_t pseltxd; ///< TXD pin number.
  90. uint32_t pselrxd; ///< RXD pin number.
  91. uint32_t pselcts; ///< CTS pin number.
  92. uint32_t pselrts; ///< RTS pin number.
  93. void * p_context; ///< Context passed to interrupt handler.
  94. nrf_uart_hwfc_t hwfc; ///< Flow control configuration.
  95. nrf_uart_parity_t parity; ///< Parity configuration.
  96. nrf_uart_baudrate_t baudrate; ///< Baudrate.
  97. uint8_t interrupt_priority; ///< Interrupt priority.
  98. } nrfx_uart_config_t;
  99. /**
  100. * @brief UART default configuration.
  101. */
  102. #define NRFX_UART_DEFAULT_CONFIG \
  103. { \
  104. .pseltxd = NRF_UART_PSEL_DISCONNECTED, \
  105. .pselrxd = NRF_UART_PSEL_DISCONNECTED, \
  106. .pselcts = NRF_UART_PSEL_DISCONNECTED, \
  107. .pselrts = NRF_UART_PSEL_DISCONNECTED, \
  108. .p_context = NULL, \
  109. .hwfc = (nrf_uart_hwfc_t)NRFX_UART_DEFAULT_CONFIG_HWFC, \
  110. .parity = (nrf_uart_parity_t)NRFX_UART_DEFAULT_CONFIG_PARITY, \
  111. .baudrate = (nrf_uart_baudrate_t)NRFX_UART_DEFAULT_CONFIG_BAUDRATE, \
  112. .interrupt_priority = NRFX_UART_DEFAULT_CONFIG_IRQ_PRIORITY, \
  113. }
  114. /**
  115. * @brief Structure for UART transfer completion event.
  116. */
  117. typedef struct
  118. {
  119. uint8_t * p_data; ///< Pointer to memory used for transfer.
  120. uint32_t bytes; ///< Number of bytes transfered.
  121. } nrfx_uart_xfer_evt_t;
  122. /**
  123. * @brief Structure for UART error event.
  124. */
  125. typedef struct
  126. {
  127. nrfx_uart_xfer_evt_t rxtx; ///< Transfer details includes number of bytes transferred.
  128. uint32_t error_mask; ///< Mask of error flags that generated the event.
  129. } nrfx_uart_error_evt_t;
  130. /**
  131. * @brief Structure for UART event.
  132. */
  133. typedef struct
  134. {
  135. nrfx_uart_evt_type_t type; ///< Event type.
  136. union
  137. {
  138. nrfx_uart_xfer_evt_t rxtx; ///< Data provided for transfer completion events.
  139. nrfx_uart_error_evt_t error; ///< Data provided for error event.
  140. } data;
  141. } nrfx_uart_event_t;
  142. /**
  143. * @brief UART interrupt event handler.
  144. *
  145. * @param[in] p_event Pointer to event structure. Event is allocated on the stack so it is available
  146. * only within the context of the event handler.
  147. * @param[in] p_context Context passed to interrupt handler, set on initialization.
  148. */
  149. typedef void (*nrfx_uart_event_handler_t)(nrfx_uart_event_t const * p_event,
  150. void * p_context);
  151. /**
  152. * @brief Function for initializing the UART driver.
  153. *
  154. * This function configures and enables UART. After this function GPIO pins are controlled by UART.
  155. *
  156. * @param[in] p_instance Pointer to the driver instance structure.
  157. * @param[in] p_config Pointer to the structure with initial configuration.
  158. * @param[in] event_handler Event handler provided by the user. If not provided driver works in
  159. * blocking mode.
  160. *
  161. * @retval NRFX_SUCCESS If initialization was successful.
  162. * @retval NRFX_ERROR_INVALID_STATE If driver is already initialized.
  163. * @retval NRFX_ERROR_BUSY If some other peripheral with the same
  164. * instance ID is already in use. This is
  165. * possible only if @ref nrfx_prs module
  166. * is enabled.
  167. */
  168. nrfx_err_t nrfx_uart_init(nrfx_uart_t const * p_instance,
  169. nrfx_uart_config_t const * p_config,
  170. nrfx_uart_event_handler_t event_handler);
  171. /**
  172. * @brief Function for uninitializing the UART driver.
  173. * @param[in] p_instance Pointer to the driver instance structure.
  174. */
  175. void nrfx_uart_uninit(nrfx_uart_t const * p_instance);
  176. /**
  177. * @brief Function for getting the address of a specific UART task.
  178. *
  179. * @param[in] p_instance Pointer to the driver instance structure.
  180. * @param[in] task Task.
  181. *
  182. * @return Task address.
  183. */
  184. __STATIC_INLINE uint32_t nrfx_uart_task_address_get(nrfx_uart_t const * p_instance,
  185. nrf_uart_task_t task);
  186. /**
  187. * @brief Function for getting the address of a specific UART event.
  188. *
  189. * @param[in] p_instance Pointer to the driver instance structure.
  190. * @param[in] event Event.
  191. *
  192. * @return Event address.
  193. */
  194. __STATIC_INLINE uint32_t nrfx_uart_event_address_get(nrfx_uart_t const * p_instance,
  195. nrf_uart_event_t event);
  196. /**
  197. * @brief Function for sending data over UART.
  198. *
  199. * If an event handler was provided in nrfx_uart_init() call, this function
  200. * returns immediately and the handler is called when the transfer is done.
  201. * Otherwise, the transfer is performed in blocking mode, i.e. this function
  202. * returns when the transfer is finished. Blocking mode is not using interrupt
  203. * so there is no context switching inside the function.
  204. *
  205. * @param[in] p_instance Pointer to the driver instance structure.
  206. * @param[in] p_data Pointer to data.
  207. * @param[in] length Number of bytes to send.
  208. *
  209. * @retval NRFX_SUCCESS If initialization was successful.
  210. * @retval NRFX_ERROR_BUSY If driver is already transferring.
  211. * @retval NRFX_ERROR_FORBIDDEN If the transfer was aborted from a different context
  212. * (blocking mode only).
  213. */
  214. nrfx_err_t nrfx_uart_tx(nrfx_uart_t const * p_instance,
  215. uint8_t const * p_data,
  216. size_t length);
  217. /**
  218. * @brief Function for checking if UART is currently transmitting.
  219. *
  220. * @param[in] p_instance Pointer to the driver instance structure.
  221. *
  222. * @retval true If UART is transmitting.
  223. * @retval false If UART is not transmitting.
  224. */
  225. bool nrfx_uart_tx_in_progress(nrfx_uart_t const * p_instance);
  226. /**
  227. * @brief Function for aborting any ongoing transmission.
  228. * @note @ref NRFX_UART_EVT_TX_DONE event will be generated in non-blocking mode.
  229. * It will contain number of bytes sent until abort was called. The event
  230. * handler will be called from the function context.
  231. *
  232. * @param[in] p_instance Pointer to the driver instance structure.
  233. */
  234. void nrfx_uart_tx_abort(nrfx_uart_t const * p_instance);
  235. /**
  236. * @brief Function for receiving data over UART.
  237. *
  238. * If an event handler was provided in the nrfx_uart_init() call, this function
  239. * returns immediately and the handler is called when the transfer is done.
  240. * Otherwise, the transfer is performed in blocking mode, meaning that this function
  241. * returns when the transfer is finished. Blocking mode is not using interrupt so
  242. * there is no context switching inside the function.
  243. *
  244. * The receive buffer pointer is double buffered in non-blocking mode. The secondary
  245. * buffer can be set immediately after starting the transfer and will be filled
  246. * when the primary buffer is full. The double buffering feature allows
  247. * receiving data continuously.
  248. *
  249. * If this function is used without a previous call to @ref nrfx_uart_rx_enable, the reception
  250. * will be stopped on error or when the supplied buffer fills up. In both cases,
  251. * RX FIFO gets disabled. This means that, in case of error, the bytes that follow are lost.
  252. * If this nrfx_uart_rx() function is used with the previous call to @ref nrfx_uart_rx_enable,
  253. * the reception is stopped in case of error, but FIFO is still ongoing. The receiver is still
  254. * working, so after handling the error, an immediate repeated call to this nrfx_uart_rx()
  255. * function with fresh data buffer will re-establish reception. To disable the receiver,
  256. * you must call @ref nrfx_uart_rx_disable explicitly.
  257. *
  258. * @param[in] p_instance Pointer to the driver instance structure.
  259. * @param[in] p_data Pointer to data.
  260. * @param[in] length Number of bytes to receive.
  261. *
  262. * @retval NRFX_SUCCESS If reception is complete (in case of blocking mode) or it is
  263. * successfully started (in case of non-blocking mode).
  264. * @retval NRFX_ERROR_BUSY If the driver is already receiving
  265. * (and the secondary buffer has already been set
  266. * in non-blocking mode).
  267. * @retval NRFX_ERROR_FORBIDDEN If the transfer was aborted from a different context
  268. * (blocking mode only, also see @ref nrfx_uart_rx_disable).
  269. * @retval NRFX_ERROR_INTERNAL If UART peripheral reported an error.
  270. */
  271. nrfx_err_t nrfx_uart_rx(nrfx_uart_t const * p_instance,
  272. uint8_t * p_data,
  273. size_t length);
  274. /**
  275. * @brief Function for testing the receiver state in blocking mode.
  276. *
  277. * @param[in] p_instance Pointer to the driver instance structure.
  278. *
  279. * @retval true If the receiver has at least one byte of data to get.
  280. * @retval false If the receiver is empty.
  281. */
  282. bool nrfx_uart_rx_ready(nrfx_uart_t const * p_instance);
  283. /**
  284. * @brief Function for enabling the receiver.
  285. *
  286. * UART has a 6-byte-long RX FIFO and it is used to store incoming data. If a user does not call the
  287. * UART receive function before the FIFO is filled, an overrun error will appear. The receiver must be
  288. * explicitly closed by the user @sa nrfx_uart_rx_disable.
  289. *
  290. * @param[in] p_instance Pointer to the driver instance structure.
  291. */
  292. void nrfx_uart_rx_enable(nrfx_uart_t const * p_instance);
  293. /**
  294. * @brief Function for disabling the receiver.
  295. *
  296. * This function must be called to close the receiver after it has been explicitly enabled by
  297. * @sa nrfx_uart_rx_enable.
  298. *
  299. * @param[in] p_instance Pointer to the driver instance structure.
  300. */
  301. void nrfx_uart_rx_disable(nrfx_uart_t const * p_instance);
  302. /**
  303. * @brief Function for aborting any ongoing reception.
  304. * @note @ref NRFX_UART_EVT_TX_DONE event will be generated in non-blocking mode.
  305. * It will contain number of bytes received until abort was called. The event
  306. * handler will be called from the UART interrupt context.
  307. *
  308. * @param[in] p_instance Pointer to the driver instance structure.
  309. */
  310. void nrfx_uart_rx_abort(nrfx_uart_t const * p_instance);
  311. /**
  312. * @brief Function for reading error source mask. Mask contains values from @ref nrf_uart_error_mask_t.
  313. * @note Function should be used in blocking mode only. In case of non-blocking mode, an error event is
  314. * generated. Function clears error sources after reading.
  315. *
  316. * @param[in] p_instance Pointer to the driver instance structure.
  317. *
  318. * @retval Mask of reported errors.
  319. */
  320. uint32_t nrfx_uart_errorsrc_get(nrfx_uart_t const * p_instance);
  321. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  322. __STATIC_INLINE uint32_t nrfx_uart_task_address_get(nrfx_uart_t const * p_instance,
  323. nrf_uart_task_t task)
  324. {
  325. return nrf_uart_task_address_get(p_instance->p_reg, task);
  326. }
  327. __STATIC_INLINE uint32_t nrfx_uart_event_address_get(nrfx_uart_t const * p_instance,
  328. nrf_uart_event_t event)
  329. {
  330. return nrf_uart_event_address_get(p_instance->p_reg, event);
  331. }
  332. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  333. void nrfx_uart_0_irq_handler(void);
  334. /** @} */
  335. #ifdef __cplusplus
  336. }
  337. #endif
  338. #endif // NRFX_UART_H__