nrfx_uarte.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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_UARTE_H__
  41. #define NRFX_UARTE_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_uarte.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @defgroup nrfx_uarte UARTE driver
  49. * @{
  50. * @ingroup nrf_uarte
  51. * @brief UARTE peripheral driver.
  52. */
  53. /** @brief Structure for the UARTE driver instance. */
  54. typedef struct
  55. {
  56. NRF_UARTE_Type * p_reg; ///< Pointer to a structure with UARTE registers.
  57. uint8_t drv_inst_idx; ///< Index of the driver instance. For internal use only.
  58. } nrfx_uarte_t;
  59. #ifndef __NRFX_DOXYGEN__
  60. enum {
  61. #if NRFX_CHECK(NRFX_UARTE0_ENABLED)
  62. NRFX_UARTE0_INST_IDX,
  63. #endif
  64. #if NRFX_CHECK(NRFX_UARTE1_ENABLED)
  65. NRFX_UARTE1_INST_IDX,
  66. #endif
  67. #if NRFX_CHECK(NRFX_UARTE2_ENABLED)
  68. NRFX_UARTE2_INST_IDX,
  69. #endif
  70. #if NRFX_CHECK(NRFX_UARTE3_ENABLED)
  71. NRFX_UARTE3_INST_IDX,
  72. #endif
  73. NRFX_UARTE_ENABLED_COUNT
  74. };
  75. #endif
  76. /** @brief Macro for creating a UARTE driver instance. */
  77. #define NRFX_UARTE_INSTANCE(id) \
  78. { \
  79. .p_reg = NRFX_CONCAT_2(NRF_UARTE, id), \
  80. .drv_inst_idx = NRFX_CONCAT_3(NRFX_UARTE, id, _INST_IDX), \
  81. }
  82. /** @brief Types of UARTE driver events. */
  83. typedef enum
  84. {
  85. NRFX_UARTE_EVT_TX_DONE, ///< Requested TX transfer completed.
  86. NRFX_UARTE_EVT_RX_DONE, ///< Requested RX transfer completed.
  87. NRFX_UARTE_EVT_ERROR, ///< Error reported by UART peripheral.
  88. } nrfx_uarte_evt_type_t;
  89. /** @brief Structure for the UARTE configuration. */
  90. typedef struct
  91. {
  92. uint32_t pseltxd; ///< TXD pin number.
  93. uint32_t pselrxd; ///< RXD pin number.
  94. uint32_t pselcts; ///< CTS pin number.
  95. uint32_t pselrts; ///< RTS pin number.
  96. void * p_context; ///< Context passed to interrupt handler.
  97. nrf_uarte_hwfc_t hwfc; ///< Flow control configuration.
  98. nrf_uarte_parity_t parity; ///< Parity configuration.
  99. nrf_uarte_baudrate_t baudrate; ///< Baud rate.
  100. uint8_t interrupt_priority; ///< Interrupt priority.
  101. } nrfx_uarte_config_t;
  102. /** @brief UARTE default configuration. */
  103. #define NRFX_UARTE_DEFAULT_CONFIG \
  104. { \
  105. .pseltxd = NRF_UARTE_PSEL_DISCONNECTED, \
  106. .pselrxd = NRF_UARTE_PSEL_DISCONNECTED, \
  107. .pselcts = NRF_UARTE_PSEL_DISCONNECTED, \
  108. .pselrts = NRF_UARTE_PSEL_DISCONNECTED, \
  109. .p_context = NULL, \
  110. .hwfc = (nrf_uarte_hwfc_t)NRFX_UARTE_DEFAULT_CONFIG_HWFC, \
  111. .parity = (nrf_uarte_parity_t)NRFX_UARTE_DEFAULT_CONFIG_PARITY, \
  112. .baudrate = (nrf_uarte_baudrate_t)NRFX_UARTE_DEFAULT_CONFIG_BAUDRATE, \
  113. .interrupt_priority = NRFX_UARTE_DEFAULT_CONFIG_IRQ_PRIORITY, \
  114. }
  115. /** @brief Structure for the UARTE transfer completion event. */
  116. typedef struct
  117. {
  118. uint8_t * p_data; ///< Pointer to memory used for transfer.
  119. size_t bytes; ///< Number of bytes transfered.
  120. } nrfx_uarte_xfer_evt_t;
  121. /** @brief Structure for UARTE error event. */
  122. typedef struct
  123. {
  124. nrfx_uarte_xfer_evt_t rxtx; ///< Transfer details, including number of bytes transferred.
  125. uint32_t error_mask; ///< Mask of error flags that generated the event.
  126. } nrfx_uarte_error_evt_t;
  127. /** @brief Structure for UARTE event. */
  128. typedef struct
  129. {
  130. nrfx_uarte_evt_type_t type; ///< Event type.
  131. union
  132. {
  133. nrfx_uarte_xfer_evt_t rxtx; ///< Data provided for transfer completion events.
  134. nrfx_uarte_error_evt_t error; ///< Data provided for error event.
  135. } data; ///< Union to store event data.
  136. } nrfx_uarte_event_t;
  137. /**
  138. * @brief UARTE interrupt event handler.
  139. *
  140. * @param[in] p_event Pointer to event structure. Event is allocated on the stack so it is available
  141. * only within the context of the event handler.
  142. * @param[in] p_context Context passed to the interrupt handler, set on initialization.
  143. */
  144. typedef void (*nrfx_uarte_event_handler_t)(nrfx_uarte_event_t const * p_event,
  145. void * p_context);
  146. /**
  147. * @brief Function for initializing the UARTE driver.
  148. *
  149. * This function configures and enables UARTE. After this function GPIO pins are controlled by UARTE.
  150. *
  151. * @param[in] p_instance Pointer to the driver instance structure.
  152. * @param[in] p_config Pointer to the structure with the initial configuration.
  153. * @param[in] event_handler Event handler provided by the user. If not provided driver works in
  154. * blocking mode.
  155. *
  156. * @retval NRFX_SUCCESS Initialization was successful.
  157. * @retval NRFX_ERROR_INVALID_STATE Driver is already initialized.
  158. * @retval NRFX_ERROR_BUSY Some other peripheral with the same
  159. * instance ID is already in use. This is
  160. * possible only if @ref nrfx_prs module
  161. * is enabled.
  162. */
  163. nrfx_err_t nrfx_uarte_init(nrfx_uarte_t const * p_instance,
  164. nrfx_uarte_config_t const * p_config,
  165. nrfx_uarte_event_handler_t event_handler);
  166. /**
  167. * @brief Function for uninitializing the UARTE driver.
  168. *
  169. * @param[in] p_instance Pointer to the driver instance structure.
  170. */
  171. void nrfx_uarte_uninit(nrfx_uarte_t const * p_instance);
  172. /**
  173. * @brief Function for getting the address of the specified UARTE task.
  174. *
  175. * @param[in] p_instance Pointer to the driver instance structure.
  176. * @param[in] task Task.
  177. *
  178. * @return Task address.
  179. */
  180. __STATIC_INLINE uint32_t nrfx_uarte_task_address_get(nrfx_uarte_t const * p_instance,
  181. nrf_uarte_task_t task);
  182. /**
  183. * @brief Function for getting the address of the specified UARTE event.
  184. *
  185. * @param[in] p_instance Pointer to the driver instance structure.
  186. * @param[in] event Event.
  187. *
  188. * @return Event address.
  189. */
  190. __STATIC_INLINE uint32_t nrfx_uarte_event_address_get(nrfx_uarte_t const * p_instance,
  191. nrf_uarte_event_t event);
  192. /**
  193. * @brief Function for sending data over UARTE.
  194. *
  195. * If an event handler is provided in nrfx_uarte_init() call, this function
  196. * returns immediately and the handler is called when the transfer is done.
  197. * Otherwise, the transfer is performed in blocking mode, that is this function
  198. * returns when the transfer is finished. Blocking mode is not using interrupt
  199. * so there is no context switching inside the function.
  200. *
  201. * @note Peripherals using EasyDMA (including UARTE) require the transfer buffers
  202. * to be placed in the Data RAM region. If this condition is not met,
  203. * this function will fail with the error code NRFX_ERROR_INVALID_ADDR.
  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. Maximum possible length is
  208. * dependent on the used SoC (see the MAXCNT register
  209. * description in the Product Specification). The driver
  210. * checks it with assertion.
  211. *
  212. * @retval NRFX_SUCCESS Initialization was successful.
  213. * @retval NRFX_ERROR_BUSY Driver is already transferring.
  214. * @retval NRFX_ERROR_FORBIDDEN The transfer was aborted from a different context
  215. * (blocking mode only).
  216. * @retval NRFX_ERROR_INVALID_ADDR p_data does not point to RAM buffer.
  217. */
  218. nrfx_err_t nrfx_uarte_tx(nrfx_uarte_t const * p_instance,
  219. uint8_t const * p_data,
  220. size_t length);
  221. /**
  222. * @brief Function for checking if UARTE is currently transmitting.
  223. *
  224. * @param[in] p_instance Pointer to the driver instance structure.
  225. *
  226. * @retval true The UARTE is transmitting.
  227. * @retval false The UARTE is not transmitting.
  228. */
  229. bool nrfx_uarte_tx_in_progress(nrfx_uarte_t const * p_instance);
  230. /**
  231. * @brief Function for aborting any ongoing transmission.
  232. * @note @ref NRFX_UARTE_EVT_TX_DONE event will be generated in non-blocking mode.
  233. * It will contain number of bytes sent until the abort was called. The event
  234. * handler will be called from the UARTE interrupt context.
  235. *
  236. * @param[in] p_instance Pointer to the driver instance structure.
  237. */
  238. void nrfx_uarte_tx_abort(nrfx_uarte_t const * p_instance);
  239. /**
  240. * @brief Function for receiving data over UARTE.
  241. *
  242. * If an event handler is provided in the nrfx_uarte_init() call, this function
  243. * returns immediately and the handler is called when the transfer is done.
  244. * Otherwise, the transfer is performed in blocking mode, that is this function
  245. * returns when the transfer is finished. Blocking mode is not using interrupt so
  246. * there is no context switching inside the function.
  247. * The receive buffer pointer is double-buffered in non-blocking mode. The secondary
  248. * buffer can be set immediately after starting the transfer and will be filled
  249. * when the primary buffer is full. The double-buffering feature allows
  250. * receiving data continuously.
  251. *
  252. * @note Peripherals using EasyDMA (including UARTE) require the transfer buffers
  253. * to be placed in the Data RAM region. If this condition is not met,
  254. * this function fails with the error code NRFX_ERROR_INVALID_ADDR.
  255. *
  256. * @param[in] p_instance Pointer to the driver instance structure.
  257. * @param[in] p_data Pointer to data.
  258. * @param[in] length Number of bytes to receive. Maximum possible length is
  259. * dependent on the used SoC (see the MAXCNT register
  260. * description in the Product Specification). The driver
  261. * checks it with assertion.
  262. *
  263. * @retval NRFX_SUCCESS Initialization is successful.
  264. * @retval NRFX_ERROR_BUSY The driver is already receiving
  265. * (and the secondary buffer has already been set
  266. * in non-blocking mode).
  267. * @retval NRFX_ERROR_FORBIDDEN The transfer is aborted from a different context
  268. * (blocking mode only).
  269. * @retval NRFX_ERROR_INTERNAL The UARTE peripheral reports an error.
  270. * @retval NRFX_ERROR_INVALID_ADDR p_data does not point to RAM buffer.
  271. */
  272. nrfx_err_t nrfx_uarte_rx(nrfx_uarte_t const * p_instance,
  273. uint8_t * p_data,
  274. size_t length);
  275. /**
  276. * @brief Function for testing the receiver state in blocking mode.
  277. *
  278. * @param[in] p_instance Pointer to the driver instance structure.
  279. *
  280. * @retval true The receiver has at least one byte of data to get.
  281. * @retval false The receiver is empty.
  282. */
  283. bool nrfx_uarte_rx_ready(nrfx_uarte_t const * p_instance);
  284. /**
  285. * @brief Function for aborting any ongoing reception.
  286. * @note @ref NRFX_UARTE_EVT_RX_DONE event will be generated in non-blocking mode.
  287. * It will contain number of bytes received until the abort was called. The event
  288. * handler will be called from the UARTE interrupt context.
  289. *
  290. * @param[in] p_instance Pointer to the driver instance structure.
  291. */
  292. void nrfx_uarte_rx_abort(nrfx_uarte_t const * p_instance);
  293. /**
  294. * @brief Function for reading error source mask. Mask contains values from @ref nrf_uarte_error_mask_t.
  295. * @note Function must be used in the blocking mode only. In case of non-blocking mode, an error event is
  296. * generated. Function clears error sources after reading.
  297. *
  298. * @param[in] p_instance Pointer to the driver instance structure.
  299. *
  300. * @return Mask of reported errors.
  301. */
  302. uint32_t nrfx_uarte_errorsrc_get(nrfx_uarte_t const * p_instance);
  303. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  304. __STATIC_INLINE uint32_t nrfx_uarte_task_address_get(nrfx_uarte_t const * p_instance,
  305. nrf_uarte_task_t task)
  306. {
  307. return nrf_uarte_task_address_get(p_instance->p_reg, task);
  308. }
  309. __STATIC_INLINE uint32_t nrfx_uarte_event_address_get(nrfx_uarte_t const * p_instance,
  310. nrf_uarte_event_t event)
  311. {
  312. return nrf_uarte_event_address_get(p_instance->p_reg, event);
  313. }
  314. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  315. /** @} */
  316. void nrfx_uarte_0_irq_handler(void);
  317. void nrfx_uarte_1_irq_handler(void);
  318. void nrfx_uarte_2_irq_handler(void);
  319. void nrfx_uarte_3_irq_handler(void);
  320. #ifdef __cplusplus
  321. }
  322. #endif
  323. #endif // NRFX_UARTE_H__