nrfx_uarte.h 15 KB

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