nrf_drv_uart.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. /**@file
  13. * @addtogroup nrf_uart UART driver and HAL
  14. * @ingroup nrf_drivers
  15. * @brief UART API.
  16. * @details The UART driver provides APIs for utilizing the UART peripheral.
  17. *
  18. * @defgroup nrf_drv_uart UART driver
  19. * @{
  20. * @ingroup nrf_uart
  21. *
  22. * @brief UART driver.
  23. */
  24. #ifndef NRF_DRV_UART_H
  25. #define NRF_DRV_UART_H
  26. #include "nrf_uart.h"
  27. #ifdef NRF52
  28. #include "nrf_uarte.h"
  29. #endif
  30. #include "sdk_errors.h"
  31. #include "nrf_drv_config.h"
  32. /**
  33. * @brief Types of UART driver events.
  34. */
  35. typedef enum
  36. {
  37. NRF_DRV_UART_EVT_TX_DONE, ///< Requested TX transfer completed.
  38. NRF_DRV_UART_EVT_RX_DONE, ///< Requested RX transfer completed.
  39. NRF_DRV_UART_EVT_ERROR, ///< Error reported by UART peripheral.
  40. } nrf_drv_uart_evt_type_t;
  41. /**@brief Structure for UART configuration. */
  42. typedef struct
  43. {
  44. uint32_t pseltxd; ///< TXD pin number.
  45. uint32_t pselrxd; ///< RXD pin number.
  46. uint32_t pselcts; ///< CTS pin number.
  47. uint32_t pselrts; ///< RTS pin number.
  48. void * p_context; ///< Context passed to interrupt handler.
  49. nrf_uart_hwfc_t hwfc; ///< Flow control configuration.
  50. nrf_uart_parity_t parity; ///< Parity configuration.
  51. nrf_uart_baudrate_t baudrate; ///< Baudrate.
  52. uint8_t interrupt_priority; ///< Interrupt priority.
  53. #ifdef NRF52
  54. bool use_easy_dma;
  55. #endif
  56. } nrf_drv_uart_config_t;
  57. /**@brief UART default configuration. */
  58. #ifdef NRF52
  59. #if !UART_LEGACY_SUPPORT
  60. #define DEFAULT_CONFIG_USE_EASY_DMA true
  61. #elif !UART_EASY_DMA_SUPPORT
  62. #define DEFAULT_CONFIG_USE_EASY_DMA false
  63. #else
  64. #define DEFAULT_CONFIG_USE_EASY_DMA UART0_CONFIG_USE_EASY_DMA
  65. #endif
  66. #define NRF_DRV_UART_DEFAULT_CONFIG \
  67. { \
  68. .pseltxd = UART0_CONFIG_PSEL_TXD, \
  69. .pselrxd = UART0_CONFIG_PSEL_RXD, \
  70. .pselcts = UART0_CONFIG_PSEL_CTS, \
  71. .pselrts = UART0_CONFIG_PSEL_RTS, \
  72. .p_context = NULL, \
  73. .hwfc = UART0_CONFIG_HWFC, \
  74. .parity = UART0_CONFIG_PARITY, \
  75. .baudrate = UART0_CONFIG_BAUDRATE, \
  76. .interrupt_priority = UART0_CONFIG_IRQ_PRIORITY, \
  77. .use_easy_dma = DEFAULT_CONFIG_USE_EASY_DMA \
  78. }
  79. #else
  80. #define NRF_DRV_UART_DEFAULT_CONFIG \
  81. { \
  82. .pseltxd = UART0_CONFIG_PSEL_TXD, \
  83. .pselrxd = UART0_CONFIG_PSEL_RXD, \
  84. .pselcts = UART0_CONFIG_PSEL_CTS, \
  85. .pselrts = UART0_CONFIG_PSEL_RTS, \
  86. .p_context = NULL, \
  87. .hwfc = UART0_CONFIG_HWFC, \
  88. .parity = UART0_CONFIG_PARITY, \
  89. .baudrate = UART0_CONFIG_BAUDRATE, \
  90. .interrupt_priority = UART0_CONFIG_IRQ_PRIORITY \
  91. }
  92. #endif
  93. /**@brief Structure for UART transfer completion event. */
  94. typedef struct
  95. {
  96. uint8_t * p_data; ///< Pointer to memory used for transfer.
  97. uint8_t bytes; ///< Number of bytes transfered.
  98. } nrf_drv_uart_xfer_evt_t;
  99. /**@brief Structure for UART error event. */
  100. typedef struct
  101. {
  102. nrf_drv_uart_xfer_evt_t rxtx; ///< Transfer details includes number of bytes transfered.
  103. uint32_t error_mask;///< Mask of error flags that generated the event.
  104. } nrf_drv_uart_error_evt_t;
  105. /**@brief Structure for UART event. */
  106. typedef struct
  107. {
  108. nrf_drv_uart_evt_type_t type; ///< Event type.
  109. union
  110. {
  111. nrf_drv_uart_xfer_evt_t rxtx; ///< Data provided for transfer completion events.
  112. nrf_drv_uart_error_evt_t error;///< Data provided for error event.
  113. } data;
  114. } nrf_drv_uart_event_t;
  115. /**
  116. * @brief UART interrupt event handler.
  117. *
  118. * @param[in] p_event Pointer to event structure. Event is allocated on the stack so it is available
  119. * only within the context of the event handler.
  120. * @param[in] p_context Context passed to interrupt handler, set on initialization.
  121. */
  122. typedef void (*nrf_uart_event_handler_t)(nrf_drv_uart_event_t * p_event, void * p_context);
  123. /**
  124. * @brief Function for initializing the UART driver.
  125. *
  126. * This function configures and enables UART. After this function GPIO pins are controlled by UART.
  127. *
  128. * @param[in] p_config Initial configuration. Default configuration used if NULL.
  129. * @param[in] event_handler Event handler provided by the user. If not provided driver works in
  130. * blocking mode.
  131. *
  132. * @retval NRF_SUCCESS If initialization was successful.
  133. * @retval NRF_ERROR_INVALID_STATE If driver is already initialized.
  134. */
  135. ret_code_t nrf_drv_uart_init(nrf_drv_uart_config_t const * p_config,
  136. nrf_uart_event_handler_t event_handler);
  137. /**
  138. * @brief Function for uninitializing the UART driver.
  139. */
  140. void nrf_drv_uart_uninit(void);
  141. /**
  142. * @brief Function for getting the address of a specific UART task.
  143. *
  144. * @param[in] task Task.
  145. *
  146. * @return Task address.
  147. */
  148. __STATIC_INLINE uint32_t nrf_drv_uart_task_address_get(nrf_uart_task_t task);
  149. /**
  150. * @brief Function for getting the address of a specific UART event.
  151. *
  152. * @param[in] event Event.
  153. *
  154. * @return Event address.
  155. */
  156. __STATIC_INLINE uint32_t nrf_drv_uart_event_address_get(nrf_uart_event_t event);
  157. /**
  158. * @brief Function for sending data over UART.
  159. *
  160. * If an event handler was provided in nrf_drv_uart_init() call, this function
  161. * returns immediately and the handler is called when the transfer is done.
  162. * Otherwise, the transfer is performed in blocking mode, i.e. this function
  163. * returns when the transfer is finished. Blocking mode is not using interrupt so
  164. * there is no context switching inside the function.
  165. *
  166. * @note Peripherals using EasyDMA (i.e. UARTE) require that the transfer buffers
  167. * are placed in the Data RAM region. If they are not and UARTE instance is
  168. * used, this function will fail with error code NRF_ERROR_INVALID_ADDR.
  169. *
  170. * @param[in] p_data Pointer to data.
  171. * @param[in] length Number of bytes to send.
  172. *
  173. * @retval NRF_SUCCESS If initialization was successful.
  174. * @retval NRF_ERROR_BUSY If driver is already transferring.
  175. * @retval NRF_ERROR_FORBIDDEN If the transfer was aborted from a different context
  176. * (blocking mode only, also see @ref nrf_drv_uart_rx_disable).
  177. * @retval NRF_ERROR_INVALID_ADDR If p_data does not point to RAM buffer (UARTE only).
  178. */
  179. ret_code_t nrf_drv_uart_tx(uint8_t const * const p_data, uint8_t length);
  180. /**
  181. * @brief Function for checking if UART is currently transmitting.
  182. *
  183. * @retval true If UART is transmitting.
  184. * @retval false If UART is not transmitting.
  185. */
  186. bool nrf_drv_uart_tx_in_progress(void);
  187. /**
  188. * @brief Function for aborting any ongoing transmission.
  189. * @note @ref NRF_DRV_UART_EVT_TX_DONE event will be generated in non-blocking mode. Event will
  190. * contain number of bytes sent until abort was called. If Easy DMA is not used event will be
  191. * called from the function context. If Easy DMA is used it will be called from UART interrupt
  192. * context.
  193. */
  194. void nrf_drv_uart_tx_abort(void);
  195. /**
  196. * @brief Function for receiving data over UART.
  197. *
  198. * If an event handler was provided in the nrf_drv_uart_init() call, this function
  199. * returns immediately and the handler is called when the transfer is done.
  200. * Otherwise, the transfer is performed in blocking mode, i.e. this function
  201. * returns when the transfer is finished. Blocking mode is not using interrupt so
  202. * there is no context switching inside the function.
  203. * The receive buffer pointer is double buffered in non-blocking mode. The secondary
  204. * buffer can be set immediately after starting the transfer and will be filled
  205. * when the primary buffer is full. The double buffering feature allows
  206. * receiving data continuously.
  207. *
  208. * @note Peripherals using EasyDMA (i.e. UARTE) require that the transfer buffers
  209. * are placed in the Data RAM region. If they are not and UARTE instance is
  210. * used, this function will fail with error code NRF_ERROR_INVALID_ADDR.
  211. * @param[in] p_data Pointer to data.
  212. * @param[in] length Number of bytes to receive.
  213. *
  214. * @retval NRF_SUCCESS If initialization was successful.
  215. * @retval NRF_ERROR_BUSY If the driver is already receiving
  216. * (and the secondary buffer has already been set
  217. * in non-blocking mode).
  218. * @retval NRF_ERROR_FORBIDDEN If the transfer was aborted from a different context
  219. * (blocking mode only, also see @ref nrf_drv_uart_rx_disable).
  220. * @retval NRF_ERROR_INTERNAL If UART peripheral reported an error.
  221. * @retval NRF_ERROR_INVALID_ADDR If p_data does not point to RAM buffer (UARTE only).
  222. */
  223. ret_code_t nrf_drv_uart_rx(uint8_t * p_data, uint8_t length);
  224. /**
  225. * @brief Function for enabling receiver.
  226. *
  227. * UART has 6 byte long RX FIFO and it will be used to store incoming data. If user will not call
  228. * UART receive function before FIFO is filled, overrun error will encounter. Enabling receiver
  229. * without specifying RX buffer is supported only in UART mode (without Easy DMA). Receiver must be
  230. * explicitly closed by the user @sa nrf_drv_uart_rx_disable. Function asserts if mode is wrong.
  231. */
  232. void nrf_drv_uart_rx_enable(void);
  233. /**
  234. * @brief Function for disabling receiver.
  235. *
  236. * Function must be called to close the receiver after it has been explicitly enabled by
  237. * @sa nrf_drv_uart_rx_enable. Feature is supported only in UART mode (without Easy DMA). Function
  238. * asserts if mode is wrong.
  239. */
  240. void nrf_drv_uart_rx_disable(void);
  241. /**
  242. * @brief Function for aborting any ongoing reception.
  243. * @note @ref NRF_DRV_UART_EVT_RX_DONE event will be generated in non-blocking mode. Event will
  244. * contain number of bytes received until abort was called. If Easy DMA is not used event will be
  245. * called from the function context. If Easy DMA is used it will be called from UART interrupt
  246. * context.
  247. */
  248. void nrf_drv_uart_rx_abort(void);
  249. /**
  250. * @brief Function for reading error source mask. Mask contains values from @ref nrf_uart_error_mask_t.
  251. * @note Function should be used in blocking mode only. In case of non-blocking mode error event is
  252. * generated. Function clears error sources after reading.
  253. *
  254. * @retval Mask of reported errors.
  255. */
  256. uint32_t nrf_drv_uart_errorsrc_get(void);
  257. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  258. __STATIC_INLINE uint32_t nrf_drv_uart_task_address_get(nrf_uart_task_t task)
  259. {
  260. return nrf_uart_task_address_get(NRF_UART0, task);
  261. }
  262. __STATIC_INLINE uint32_t nrf_drv_uart_event_address_get(nrf_uart_event_t event)
  263. {
  264. return nrf_uart_event_address_get(NRF_UART0, event);
  265. }
  266. #endif //SUPPRESS_INLINE_IMPLEMENTATION
  267. #endif //NRF_DRV_UART_H
  268. /** @} */