nrf_drv_uart.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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 NRF_DRV_UART_H__
  41. #define NRF_DRV_UART_H__
  42. #include <nrfx.h>
  43. #if defined(UARTE_PRESENT) && NRFX_CHECK(NRFX_UARTE_ENABLED)
  44. #define NRF_DRV_UART_WITH_UARTE
  45. #endif
  46. #if defined(UART_PRESENT) && NRFX_CHECK(NRFX_UART_ENABLED)
  47. #define NRF_DRV_UART_WITH_UART
  48. #endif
  49. #if defined(NRF_DRV_UART_WITH_UARTE)
  50. #include <nrfx_uarte.h>
  51. #define NRF_DRV_UART_CREATE_UARTE(id) \
  52. .uarte = NRFX_UARTE_INSTANCE(id),
  53. #else
  54. // Compilers (at least the smart ones) will remove the UARTE related code
  55. // (blocks starting with "if (NRF_DRV_UART_USE_UARTE)") when it is not used,
  56. // but to perform the compilation they need the following definitions.
  57. #define nrfx_uarte_init(...) 0
  58. #define nrfx_uarte_uninit(...)
  59. #define nrfx_uarte_task_address_get(...) 0
  60. #define nrfx_uarte_event_address_get(...) 0
  61. #define nrfx_uarte_tx(...) 0
  62. #define nrfx_uarte_tx_in_progress(...) 0
  63. #define nrfx_uarte_tx_abort(...)
  64. #define nrfx_uarte_rx(...) 0
  65. #define nrfx_uarte_rx_ready(...) 0
  66. #define nrfx_uarte_rx_abort(...)
  67. #define nrfx_uarte_errorsrc_get(...) 0
  68. #define NRF_DRV_UART_CREATE_UARTE(id)
  69. #endif
  70. #if defined(NRF_DRV_UART_WITH_UART)
  71. #include <nrfx_uart.h>
  72. #define NRF_DRV_UART_CREATE_UART(id) _NRF_DRV_UART_CREATE_UART(id)
  73. #define _NRF_DRV_UART_CREATE_UART(id) NRF_DRV_UART_CREATE_UART_##id
  74. #define NRF_DRV_UART_CREATE_UART_0 \
  75. .uart = NRFX_UART_INSTANCE(0),
  76. #define NRF_DRV_UART_CREATE_UART_1 \
  77. .uart = { .p_reg = NULL },
  78. #else
  79. // Compilers (at least the smart ones) will remove the UART related code
  80. // (blocks starting with "if (NRF_DRV_UART_USE_UART)") when it is not used,
  81. // but to perform the compilation they need the following definitions.
  82. #define nrfx_uart_init(...) 0
  83. #define nrfx_uart_uninit(...)
  84. #define nrfx_uart_task_address_get(...) 0
  85. #define nrfx_uart_event_address_get(...) 0
  86. #define nrfx_uart_tx(...) 0
  87. #define nrfx_uart_tx_in_progress(...) 0
  88. #define nrfx_uart_tx_abort(...)
  89. #define nrfx_uart_rx(...) 0
  90. #define nrfx_uart_rx_enable(...)
  91. #define nrfx_uart_rx_disable(...)
  92. #define nrfx_uart_rx_ready(...) 0
  93. #define nrfx_uart_rx_abort(...)
  94. #define nrfx_uart_errorsrc_get(...) 0
  95. #define NRF_DRV_UART_CREATE_UART(id)
  96. // This part is for old modules that use directly UART HAL definitions
  97. // (to make them compilable for chips that have only UARTE).
  98. #define NRF_UART_BAUDRATE_1200 NRF_UARTE_BAUDRATE_1200
  99. #define NRF_UART_BAUDRATE_2400 NRF_UARTE_BAUDRATE_2400
  100. #define NRF_UART_BAUDRATE_4800 NRF_UARTE_BAUDRATE_4800
  101. #define NRF_UART_BAUDRATE_9600 NRF_UARTE_BAUDRATE_9600
  102. #define NRF_UART_BAUDRATE_14400 NRF_UARTE_BAUDRATE_14400
  103. #define NRF_UART_BAUDRATE_19200 NRF_UARTE_BAUDRATE_19200
  104. #define NRF_UART_BAUDRATE_28800 NRF_UARTE_BAUDRATE_28800
  105. #define NRF_UART_BAUDRATE_38400 NRF_UARTE_BAUDRATE_38400
  106. #define NRF_UART_BAUDRATE_57600 NRF_UARTE_BAUDRATE_57600
  107. #define NRF_UART_BAUDRATE_76800 NRF_UARTE_BAUDRATE_76800
  108. #define NRF_UART_BAUDRATE_115200 NRF_UARTE_BAUDRATE_115200
  109. #define NRF_UART_BAUDRATE_230400 NRF_UARTE_BAUDRATE_230400
  110. #define NRF_UART_BAUDRATE_250000 NRF_UARTE_BAUDRATE_250000
  111. #define NRF_UART_BAUDRATE_460800 NRF_UARTE_BAUDRATE_460800
  112. #define NRF_UART_BAUDRATE_921600 NRF_UARTE_BAUDRATE_921600
  113. #define NRF_UART_BAUDRATE_1000000 NRF_UARTE_BAUDRATE_1000000
  114. typedef nrf_uarte_baudrate_t nrf_uart_baudrate_t;
  115. #define NRF_UART_ERROR_OVERRUN_MASK NRF_UARTE_ERROR_OVERRUN_MASK
  116. #define NRF_UART_ERROR_PARITY_MASK NRF_UARTE_ERROR_PARITY_MASK
  117. #define NRF_UART_ERROR_FRAMING_MASK NRF_UARTE_ERROR_PARITY_MASK
  118. #define NRF_UART_ERROR_BREAK_MASK NRF_UARTE_ERROR_BREAK_MASK
  119. typedef nrf_uarte_error_mask_t nrf_uart_error_mask_t;
  120. #define NRF_UART_HWFC_DISABLED NRF_UARTE_HWFC_DISABLED
  121. #define NRF_UART_HWFC_ENABLED NRF_UARTE_HWFC_ENABLED
  122. typedef nrf_uarte_hwfc_t nrf_uart_hwfc_t;
  123. #define NRF_UART_PARITY_EXCLUDED NRF_UARTE_PARITY_EXCLUDED
  124. #define NRF_UART_PARITY_INCLUDED NRF_UARTE_PARITY_INCLUDED
  125. typedef nrf_uarte_parity_t nrf_uart_parity_t;
  126. typedef nrf_uarte_task_t nrf_uart_task_t;
  127. typedef nrf_uarte_event_t nrf_uart_event_t;
  128. #define NRF_UART_PSEL_DISCONNECTED NRF_UARTE_PSEL_DISCONNECTED
  129. #define nrf_uart_event_clear(...)
  130. #endif
  131. #ifdef __cplusplus
  132. extern "C" {
  133. #endif
  134. /**
  135. * @defgroup nrf_drv_uart UART driver - legacy layer
  136. * @{
  137. * @ingroup nrf_uart
  138. * @brief Layer providing compatibility with the former API.
  139. */
  140. /**
  141. * @brief Structure for the UART driver instance.
  142. */
  143. typedef struct
  144. {
  145. uint8_t inst_idx;
  146. #if defined(NRF_DRV_UART_WITH_UARTE)
  147. nrfx_uarte_t uarte;
  148. #endif
  149. #if defined(NRF_DRV_UART_WITH_UART)
  150. nrfx_uart_t uart;
  151. #endif
  152. } nrf_drv_uart_t;
  153. /**
  154. * @brief Macro for creating an UART driver instance.
  155. */
  156. #define NRF_DRV_UART_INSTANCE(id) \
  157. { \
  158. .inst_idx = id, \
  159. NRF_DRV_UART_CREATE_UARTE(id) \
  160. NRF_DRV_UART_CREATE_UART(id) \
  161. }
  162. /**
  163. * @brief Types of UART driver events.
  164. */
  165. typedef enum
  166. {
  167. NRF_DRV_UART_EVT_TX_DONE, ///< Requested TX transfer completed.
  168. NRF_DRV_UART_EVT_RX_DONE, ///< Requested RX transfer completed.
  169. NRF_DRV_UART_EVT_ERROR, ///< Error reported by UART peripheral.
  170. } nrf_drv_uart_evt_type_t;
  171. /**@brief Structure for UART configuration. */
  172. typedef struct
  173. {
  174. uint32_t pseltxd; ///< TXD pin number.
  175. uint32_t pselrxd; ///< RXD pin number.
  176. uint32_t pselcts; ///< CTS pin number.
  177. uint32_t pselrts; ///< RTS pin number.
  178. void * p_context; ///< Context passed to interrupt handler.
  179. nrf_uart_hwfc_t hwfc; ///< Flow control configuration.
  180. nrf_uart_parity_t parity; ///< Parity configuration.
  181. nrf_uart_baudrate_t baudrate; ///< Baudrate.
  182. uint8_t interrupt_priority; ///< Interrupt priority.
  183. #if defined(NRF_DRV_UART_WITH_UARTE) && defined(NRF_DRV_UART_WITH_UART)
  184. bool use_easy_dma;
  185. #endif
  186. } nrf_drv_uart_config_t;
  187. #if defined(NRF_DRV_UART_WITH_UARTE) && defined(NRF_DRV_UART_WITH_UART)
  188. extern uint8_t nrf_drv_uart_use_easy_dma[];
  189. #define NRF_DRV_UART_DEFAULT_CONFIG_USE_EASY_DMA .use_easy_dma = true,
  190. #else
  191. #define NRF_DRV_UART_DEFAULT_CONFIG_USE_EASY_DMA
  192. #endif
  193. /**@brief UART default configuration. */
  194. #define NRF_DRV_UART_DEFAULT_CONFIG \
  195. { \
  196. .pseltxd = NRF_UART_PSEL_DISCONNECTED, \
  197. .pselrxd = NRF_UART_PSEL_DISCONNECTED, \
  198. .pselcts = NRF_UART_PSEL_DISCONNECTED, \
  199. .pselrts = NRF_UART_PSEL_DISCONNECTED, \
  200. .p_context = NULL, \
  201. .hwfc = (nrf_uart_hwfc_t)UART_DEFAULT_CONFIG_HWFC, \
  202. .parity = (nrf_uart_parity_t)UART_DEFAULT_CONFIG_PARITY, \
  203. .baudrate = (nrf_uart_baudrate_t)UART_DEFAULT_CONFIG_BAUDRATE, \
  204. .interrupt_priority = UART_DEFAULT_CONFIG_IRQ_PRIORITY, \
  205. NRF_DRV_UART_DEFAULT_CONFIG_USE_EASY_DMA \
  206. }
  207. /**@brief Structure for UART transfer completion event. */
  208. typedef struct
  209. {
  210. uint8_t * p_data; ///< Pointer to memory used for transfer.
  211. uint8_t bytes; ///< Number of bytes transfered.
  212. } nrf_drv_uart_xfer_evt_t;
  213. /**@brief Structure for UART error event. */
  214. typedef struct
  215. {
  216. nrf_drv_uart_xfer_evt_t rxtx; ///< Transfer details includes number of bytes transfered.
  217. uint32_t error_mask;///< Mask of error flags that generated the event.
  218. } nrf_drv_uart_error_evt_t;
  219. /**@brief Structure for UART event. */
  220. typedef struct
  221. {
  222. nrf_drv_uart_evt_type_t type; ///< Event type.
  223. union
  224. {
  225. nrf_drv_uart_xfer_evt_t rxtx; ///< Data provided for transfer completion events.
  226. nrf_drv_uart_error_evt_t error;///< Data provided for error event.
  227. } data;
  228. } nrf_drv_uart_event_t;
  229. /**
  230. * @brief UART interrupt event handler.
  231. *
  232. * @param[in] p_event Pointer to event structure. Event is allocated on the stack so it is available
  233. * only within the context of the event handler.
  234. * @param[in] p_context Context passed to interrupt handler, set on initialization.
  235. */
  236. typedef void (*nrf_uart_event_handler_t)(nrf_drv_uart_event_t * p_event, void * p_context);
  237. /**
  238. * @brief Function for initializing the UART driver.
  239. *
  240. * This function configures and enables UART. After this function GPIO pins are controlled by UART.
  241. *
  242. * @param[in] p_instance Pointer to the driver instance structure.
  243. * @param[in] p_config Initial configuration.
  244. * @param[in] event_handler Event handler provided by the user. If not provided driver works in
  245. * blocking mode.
  246. *
  247. * @retval NRFX_SUCCESS If initialization was successful.
  248. * @retval NRFX_ERROR_INVALID_STATE If driver is already initialized.
  249. */
  250. ret_code_t nrf_drv_uart_init(nrf_drv_uart_t const * p_instance,
  251. nrf_drv_uart_config_t const * p_config,
  252. nrf_uart_event_handler_t event_handler);
  253. /**
  254. * @brief Function for uninitializing the UART driver.
  255. * @param[in] p_instance Pointer to the driver instance structure.
  256. */
  257. __STATIC_INLINE
  258. void nrf_drv_uart_uninit(nrf_drv_uart_t const * p_instance);
  259. /**
  260. * @brief Function for getting the address of a specific UART task.
  261. *
  262. * @param[in] p_instance Pointer to the driver instance structure.
  263. * @param[in] task Task.
  264. *
  265. * @return Task address.
  266. */
  267. __STATIC_INLINE
  268. uint32_t nrf_drv_uart_task_address_get(nrf_drv_uart_t const * p_instance,
  269. nrf_uart_task_t task);
  270. /**
  271. * @brief Function for getting the address of a specific UART event.
  272. *
  273. * @param[in] p_instance Pointer to the driver instance structure.
  274. * @param[in] event Event.
  275. *
  276. * @return Event address.
  277. */
  278. __STATIC_INLINE
  279. uint32_t nrf_drv_uart_event_address_get(nrf_drv_uart_t const * p_instance,
  280. nrf_uart_event_t event);
  281. /**
  282. * @brief Function for sending data over UART.
  283. *
  284. * If an event handler was provided in nrf_drv_uart_init() call, this function
  285. * returns immediately and the handler is called when the transfer is done.
  286. * Otherwise, the transfer is performed in blocking mode, i.e. this function
  287. * returns when the transfer is finished. Blocking mode is not using interrupt so
  288. * there is no context switching inside the function.
  289. *
  290. * @note Peripherals using EasyDMA (i.e. UARTE) require that the transfer buffers
  291. * are placed in the Data RAM region. If they are not and UARTE instance is
  292. * used, this function will fail with error code NRFX_ERROR_INVALID_ADDR.
  293. *
  294. * @param[in] p_instance Pointer to the driver instance structure.
  295. * @param[in] p_data Pointer to data.
  296. * @param[in] length Number of bytes to send.
  297. *
  298. * @retval NRFX_SUCCESS If initialization was successful.
  299. * @retval NRFX_ERROR_BUSY If driver is already transferring.
  300. * @retval NRFX_ERROR_FORBIDDEN If the transfer was aborted from a different context
  301. * (blocking mode only, also see @ref nrf_drv_uart_rx_disable).
  302. * @retval NRFX_ERROR_INVALID_ADDR If p_data does not point to RAM buffer (UARTE only).
  303. */
  304. __STATIC_INLINE
  305. ret_code_t nrf_drv_uart_tx(nrf_drv_uart_t const * p_instance,
  306. uint8_t const * const p_data,
  307. uint8_t length);
  308. /**
  309. * @brief Function for checking if UART is currently transmitting.
  310. *
  311. * @param[in] p_instance Pointer to the driver instance structure.
  312. *
  313. * @retval true If UART is transmitting.
  314. * @retval false If UART is not transmitting.
  315. */
  316. __STATIC_INLINE
  317. bool nrf_drv_uart_tx_in_progress(nrf_drv_uart_t const * p_instance);
  318. /**
  319. * @brief Function for aborting any ongoing transmission.
  320. * @note @ref NRF_DRV_UART_EVT_TX_DONE event will be generated in non-blocking mode. Event will
  321. * contain number of bytes sent until abort was called. If Easy DMA is not used event will be
  322. * called from the function context. If Easy DMA is used it will be called from UART interrupt
  323. * context.
  324. *
  325. * @param[in] p_instance Pointer to the driver instance structure.
  326. */
  327. __STATIC_INLINE
  328. void nrf_drv_uart_tx_abort(nrf_drv_uart_t const * p_instance);
  329. /**
  330. * @brief Function for receiving data over UART.
  331. *
  332. * If an event handler was provided in the nrf_drv_uart_init() call, this function
  333. * returns immediately and the handler is called when the transfer is done.
  334. * Otherwise, the transfer is performed in blocking mode, i.e. this function
  335. * returns when the transfer is finished. Blocking mode is not using interrupt so
  336. * there is no context switching inside the function.
  337. * The receive buffer pointer is double buffered in non-blocking mode. The secondary
  338. * buffer can be set immediately after starting the transfer and will be filled
  339. * when the primary buffer is full. The double buffering feature allows
  340. * receiving data continuously.
  341. *
  342. * @note Peripherals using EasyDMA (i.e. UARTE) require that the transfer buffers
  343. * are placed in the Data RAM region. If they are not and UARTE driver instance
  344. * is used, this function will fail with error code NRFX_ERROR_INVALID_ADDR.
  345. *
  346. * @param[in] p_instance Pointer to the driver instance structure.
  347. * @param[in] p_data Pointer to data.
  348. * @param[in] length Number of bytes to receive.
  349. *
  350. * @retval NRFX_SUCCESS If initialization was successful.
  351. * @retval NRFX_ERROR_BUSY If the driver is already receiving
  352. * (and the secondary buffer has already been set
  353. * in non-blocking mode).
  354. * @retval NRFX_ERROR_FORBIDDEN If the transfer was aborted from a different context
  355. * (blocking mode only, also see @ref nrf_drv_uart_rx_disable).
  356. * @retval NRFX_ERROR_INTERNAL If UART peripheral reported an error.
  357. * @retval NRFX_ERROR_INVALID_ADDR If p_data does not point to RAM buffer (UARTE only).
  358. */
  359. __STATIC_INLINE
  360. ret_code_t nrf_drv_uart_rx(nrf_drv_uart_t const * p_instance,
  361. uint8_t * p_data,
  362. uint8_t length);
  363. /**
  364. * @brief Function for testing the receiver state in blocking mode.
  365. *
  366. * @param[in] p_instance Pointer to the driver instance structure.
  367. *
  368. * @retval true If the receiver has at least one byte of data to get.
  369. * @retval false If the receiver is empty.
  370. */
  371. __STATIC_INLINE
  372. bool nrf_drv_uart_rx_ready(nrf_drv_uart_t const * p_instance);
  373. /**
  374. * @brief Function for enabling the receiver.
  375. *
  376. * UART has a 6-byte-long RX FIFO and it is used to store incoming data. If a user does not call the
  377. * UART receive function before the FIFO is filled, an overrun error will appear. Enabling the receiver
  378. * without specifying an RX buffer is supported only in UART mode (without Easy DMA). The receiver must be
  379. * explicitly closed by the user @sa nrf_drv_uart_rx_disable. This function asserts if the mode is wrong.
  380. *
  381. * @param[in] p_instance Pointer to the driver instance structure.
  382. */
  383. __STATIC_INLINE
  384. void nrf_drv_uart_rx_enable(nrf_drv_uart_t const * p_instance);
  385. /**
  386. * @brief Function for disabling the receiver.
  387. *
  388. * This function must be called to close the receiver after it has been explicitly enabled by
  389. * @sa nrf_drv_uart_rx_enable. The feature is supported only in UART mode (without Easy DMA). The function
  390. * asserts if mode is wrong.
  391. *
  392. * @param[in] p_instance Pointer to the driver instance structure.
  393. */
  394. __STATIC_INLINE
  395. void nrf_drv_uart_rx_disable(nrf_drv_uart_t const * p_instance);
  396. /**
  397. * @brief Function for aborting any ongoing reception.
  398. * @note @ref NRF_DRV_UART_EVT_RX_DONE event will be generated in non-blocking mode. The event will
  399. * contain the number of bytes received until abort was called. The event is called from UART interrupt
  400. * context.
  401. *
  402. * @param[in] p_instance Pointer to the driver instance structure.
  403. */
  404. __STATIC_INLINE
  405. void nrf_drv_uart_rx_abort(nrf_drv_uart_t const * p_instance);
  406. /**
  407. * @brief Function for reading error source mask. Mask contains values from @ref nrf_uart_error_mask_t.
  408. * @note Function should be used in blocking mode only. In case of non-blocking mode, an error event is
  409. * generated. Function clears error sources after reading.
  410. *
  411. * @param[in] p_instance Pointer to the driver instance structure.
  412. *
  413. * @retval Mask of reported errors.
  414. */
  415. __STATIC_INLINE
  416. uint32_t nrf_drv_uart_errorsrc_get(nrf_drv_uart_t const * p_instance);
  417. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  418. #if defined(NRF_DRV_UART_WITH_UARTE) && defined(NRF_DRV_UART_WITH_UART)
  419. #define NRF_DRV_UART_USE_UARTE (nrf_drv_uart_use_easy_dma[p_instance->inst_idx])
  420. #elif defined(NRF_DRV_UART_WITH_UARTE)
  421. #define NRF_DRV_UART_USE_UARTE true
  422. #else
  423. #define NRF_DRV_UART_USE_UARTE false
  424. #endif
  425. #define NRF_DRV_UART_USE_UART (!NRF_DRV_UART_USE_UARTE)
  426. __STATIC_INLINE
  427. void nrf_drv_uart_uninit(nrf_drv_uart_t const * p_instance)
  428. {
  429. if (NRF_DRV_UART_USE_UARTE)
  430. {
  431. nrfx_uarte_uninit(&p_instance->uarte);
  432. }
  433. else if (NRF_DRV_UART_USE_UART)
  434. {
  435. nrfx_uart_uninit(&p_instance->uart);
  436. }
  437. }
  438. __STATIC_INLINE
  439. uint32_t nrf_drv_uart_task_address_get(nrf_drv_uart_t const * p_instance,
  440. nrf_uart_task_t task)
  441. {
  442. uint32_t result = 0;
  443. if (NRF_DRV_UART_USE_UARTE)
  444. {
  445. result = nrfx_uarte_task_address_get(&p_instance->uarte,
  446. (nrf_uarte_task_t)task);
  447. }
  448. else if (NRF_DRV_UART_USE_UART)
  449. {
  450. result = nrfx_uart_task_address_get(&p_instance->uart, task);
  451. }
  452. return result;
  453. }
  454. __STATIC_INLINE
  455. uint32_t nrf_drv_uart_event_address_get(nrf_drv_uart_t const * p_instance,
  456. nrf_uart_event_t event)
  457. {
  458. uint32_t result = 0;
  459. if (NRF_DRV_UART_USE_UARTE)
  460. {
  461. result = nrfx_uarte_event_address_get(&p_instance->uarte,
  462. (nrf_uarte_event_t)event);
  463. }
  464. else if (NRF_DRV_UART_USE_UART)
  465. {
  466. result = nrfx_uart_event_address_get(&p_instance->uart, event);
  467. }
  468. return result;
  469. }
  470. __STATIC_INLINE
  471. ret_code_t nrf_drv_uart_tx(nrf_drv_uart_t const * p_instance,
  472. uint8_t const * p_data,
  473. uint8_t length)
  474. {
  475. uint32_t result = 0;
  476. if (NRF_DRV_UART_USE_UARTE)
  477. {
  478. result = nrfx_uarte_tx(&p_instance->uarte,
  479. p_data,
  480. length);
  481. }
  482. else if (NRF_DRV_UART_USE_UART)
  483. {
  484. result = nrfx_uart_tx(&p_instance->uart,
  485. p_data,
  486. length);
  487. }
  488. return result;
  489. }
  490. __STATIC_INLINE
  491. bool nrf_drv_uart_tx_in_progress(nrf_drv_uart_t const * p_instance)
  492. {
  493. bool result = 0;
  494. if (NRF_DRV_UART_USE_UARTE)
  495. {
  496. result = nrfx_uarte_tx_in_progress(&p_instance->uarte);
  497. }
  498. else if (NRF_DRV_UART_USE_UART)
  499. {
  500. result = nrfx_uart_tx_in_progress(&p_instance->uart);
  501. }
  502. return result;
  503. }
  504. __STATIC_INLINE
  505. void nrf_drv_uart_tx_abort(nrf_drv_uart_t const * p_instance)
  506. {
  507. if (NRF_DRV_UART_USE_UARTE)
  508. {
  509. nrfx_uarte_tx_abort(&p_instance->uarte);
  510. }
  511. else if (NRF_DRV_UART_USE_UART)
  512. {
  513. nrfx_uart_tx_abort(&p_instance->uart);
  514. }
  515. }
  516. __STATIC_INLINE
  517. ret_code_t nrf_drv_uart_rx(nrf_drv_uart_t const * p_instance,
  518. uint8_t * p_data,
  519. uint8_t length)
  520. {
  521. uint32_t result = 0;
  522. if (NRF_DRV_UART_USE_UARTE)
  523. {
  524. result = nrfx_uarte_rx(&p_instance->uarte,
  525. p_data,
  526. length);
  527. }
  528. else if (NRF_DRV_UART_USE_UART)
  529. {
  530. result = nrfx_uart_rx(&p_instance->uart,
  531. p_data,
  532. length);
  533. }
  534. return result;
  535. }
  536. __STATIC_INLINE
  537. bool nrf_drv_uart_rx_ready(nrf_drv_uart_t const * p_instance)
  538. {
  539. bool result = 0;
  540. if (NRF_DRV_UART_USE_UARTE)
  541. {
  542. result = nrfx_uarte_rx_ready(&p_instance->uarte);
  543. }
  544. else if (NRF_DRV_UART_USE_UART)
  545. {
  546. result = nrfx_uart_rx_ready(&p_instance->uart);
  547. }
  548. return result;
  549. }
  550. __STATIC_INLINE
  551. void nrf_drv_uart_rx_enable(nrf_drv_uart_t const * p_instance)
  552. {
  553. if (NRF_DRV_UART_USE_UARTE)
  554. {
  555. NRFX_ASSERT(false); // not supported
  556. }
  557. else if (NRF_DRV_UART_USE_UART)
  558. {
  559. nrfx_uart_rx_enable(&p_instance->uart);
  560. }
  561. }
  562. __STATIC_INLINE
  563. void nrf_drv_uart_rx_disable(nrf_drv_uart_t const * p_instance)
  564. {
  565. if (NRF_DRV_UART_USE_UARTE)
  566. {
  567. NRFX_ASSERT(false); // not supported
  568. }
  569. else if (NRF_DRV_UART_USE_UART)
  570. {
  571. nrfx_uart_rx_disable(&p_instance->uart);
  572. }
  573. }
  574. __STATIC_INLINE
  575. void nrf_drv_uart_rx_abort(nrf_drv_uart_t const * p_instance)
  576. {
  577. if (NRF_DRV_UART_USE_UARTE)
  578. {
  579. nrfx_uarte_rx_abort(&p_instance->uarte);
  580. }
  581. else if (NRF_DRV_UART_USE_UART)
  582. {
  583. nrfx_uart_rx_abort(&p_instance->uart);
  584. }
  585. }
  586. __STATIC_INLINE
  587. uint32_t nrf_drv_uart_errorsrc_get(nrf_drv_uart_t const * p_instance)
  588. {
  589. uint32_t result = 0;
  590. if (NRF_DRV_UART_USE_UARTE)
  591. {
  592. result = nrfx_uarte_errorsrc_get(&p_instance->uarte);
  593. }
  594. else if (NRF_DRV_UART_USE_UART)
  595. {
  596. nrf_uart_event_clear(p_instance->uart.p_reg, NRF_UART_EVENT_ERROR);
  597. result = nrfx_uart_errorsrc_get(&p_instance->uart);
  598. }
  599. return result;
  600. }
  601. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  602. /** @} */
  603. #ifdef __cplusplus
  604. }
  605. #endif
  606. #endif // NRF_DRV_UART_H__