nrfx_uarte.h 14 KB

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