nrfx_uart.h 15 KB

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