fsl_uart.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * Copyright 2018 NXP
  3. * All rights reserved.
  4. *
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef __HAL_UART_ADAPTER_H__
  9. #define __HAL_UART_ADAPTER_H__
  10. /*******************************************************************************
  11. * Definitions
  12. ******************************************************************************/
  13. #ifdef DEBUG_CONSOLE_TRANSFER_NON_BLOCKING
  14. #define UART_ADAPTER_NON_BLOCKING_MODE \
  15. (1U) /* Enable or disable Uart adapter non-blocking mode (1 - enable, 0 - disable) */
  16. #else
  17. #ifndef SERIAL_MANAGER_NON_BLOCKING_MODE
  18. #define UART_ADAPTER_NON_BLOCKING_MODE \
  19. (0U) /* Enable or disable Uart adapter non-blocking mode (1 - enable, 0 - disable) */
  20. #else
  21. #define UART_ADAPTER_NON_BLOCKING_MODE SERIAL_MANAGER_NON_BLOCKING_MODE
  22. #endif
  23. #endif
  24. #if (defined(UART_ADAPTER_NON_BLOCKING_MODE) && (UART_ADAPTER_NON_BLOCKING_MODE > 0U))
  25. #define HAL_UART_HANDLE_SIZE (90U)
  26. #else
  27. #define HAL_UART_HANDLE_SIZE (4U)
  28. #endif
  29. #define HAL_UART_TRANSFER_MODE \
  30. (0U) /*!< Whether enable transactional function of the uart. (0 - disable, 1 - enable) \ \
  31. */
  32. typedef void *hal_uart_handle_t;
  33. /*! @brief uart status */
  34. typedef enum _hal_uart_status
  35. {
  36. kStatus_HAL_UartSuccess = kStatus_Success, /*!< Successfully */
  37. kStatus_HAL_UartTxBusy = MAKE_STATUS(kStatusGroup_HAL_UART, 1), /*!< TX busy */
  38. kStatus_HAL_UartRxBusy = MAKE_STATUS(kStatusGroup_HAL_UART, 2), /*!< RX busy */
  39. kStatus_HAL_UartTxIdle = MAKE_STATUS(kStatusGroup_HAL_UART, 3), /*!< HAL uart transmitter is idle. */
  40. kStatus_HAL_UartRxIdle = MAKE_STATUS(kStatusGroup_HAL_UART, 4), /*!< HAL uart receiver is idle */
  41. kStatus_HAL_UartBaudrateNotSupport =
  42. MAKE_STATUS(kStatusGroup_HAL_UART, 5), /*!< Baudrate is not support in current clock source */
  43. kStatus_HAL_UartProtocolError = MAKE_STATUS(
  44. kStatusGroup_HAL_UART,
  45. 6), /*!< Error occurs for Noise, Framing, Parity, etc.
  46. For transcational transfer, The up layer needs to abort the transfer and then starts again */
  47. kStatus_HAL_UartError = MAKE_STATUS(kStatusGroup_HAL_UART, 7), /*!< Error occurs on HAL uart */
  48. } hal_uart_status_t;
  49. /*! @brief uart parity mode. */
  50. typedef enum _hal_uart_parity_mode
  51. {
  52. kHAL_UartParityDisabled = 0x0U, /*!< Parity disabled */
  53. kHAL_UartParityEven = 0x1U, /*!< Parity even enabled */
  54. kHAL_UartParityOdd = 0x2U, /*!< Parity odd enabled */
  55. } hal_uart_parity_mode_t;
  56. /*! @brief uart stop bit count. */
  57. typedef enum _hal_uart_stop_bit_count
  58. {
  59. kHAL_UartOneStopBit = 0U, /*!< One stop bit */
  60. kHAL_UartTwoStopBit = 1U, /*!< Two stop bits */
  61. } hal_uart_stop_bit_count_t;
  62. /*! @brief uart configuration structure. */
  63. typedef struct _hal_uart_config
  64. {
  65. uint32_t srcClock_Hz; /*!< Source clock */
  66. uint32_t baudRate_Bps; /*!< Baud rate */
  67. hal_uart_parity_mode_t parityMode; /*!< Parity mode, disabled (default), even, odd */
  68. hal_uart_stop_bit_count_t stopBitCount; /*!< Number of stop bits, 1 stop bit (default) or 2 stop bits */
  69. uint8_t enableRx; /*!< Enable RX */
  70. uint8_t enableTx; /*!< Enable TX */
  71. uint8_t instance; /*!< Instance (0 - UART0, 1 - UART1, ...), detail information please refer to the
  72. SOC corresponding RM.
  73. Invalid instance value will cause initialization failure. */
  74. } hal_uart_config_t;
  75. /*! @brief uart transfer callback function. */
  76. typedef void (*hal_uart_transfer_callback_t)(hal_uart_handle_t handle, hal_uart_status_t status, void *callbackParam);
  77. /*! @brief uart transfer structure. */
  78. typedef struct _hal_uart_transfer
  79. {
  80. uint8_t *data; /*!< The buffer of data to be transfer.*/
  81. size_t dataSize; /*!< The byte count to be transfer. */
  82. } hal_uart_transfer_t;
  83. /*******************************************************************************
  84. * API
  85. ******************************************************************************/
  86. #if defined(__cplusplus)
  87. extern "C" {
  88. #endif /* _cplusplus */
  89. /*!
  90. * @name Initialization and deinitialization
  91. * @{
  92. */
  93. /*!
  94. * @brief Initializes a uart instance with the uart handle and the user configuration structure.
  95. *
  96. * This function configures the uart module with user-defined settings. The user can configure the configuration
  97. * structure. The parameter handle is a pointer to point to a memory space of size #HAL_UART_HANDLE_SIZE allocated by the
  98. * caller.
  99. * Example below shows how to use this API to configure the uart.
  100. * @code
  101. * uint8_t g_UartHandleBuffer[HAL_UART_HANDLE_SIZE];
  102. * hal_uart_handle_t g_UartHandle = &g_UartHandleBuffer[0];
  103. * hal_uart_config_t config;
  104. * config.srcClock_Hz = 48000000;
  105. * config.baudRate_Bps = 115200U;
  106. * config.parityMode = kHAL_UartParityDisabled;
  107. * config.stopBitCount = kHAL_UartOneStopBit;
  108. * config.enableRx = 1;
  109. * config.enableTx = 1;
  110. * config.instance = 0;
  111. * HAL_UartInit(g_UartHandle, &config);
  112. * @endcode
  113. *
  114. * @param handle Pointer to point to a memory space of size #HAL_UART_HANDLE_SIZE allocated by the caller.
  115. * @param config Pointer to user-defined configuration structure.
  116. * @retval kStatus_HAL_UartBaudrateNotSupport Baudrate is not support in current clock source.
  117. * @retval kStatus_HAL_UartSuccess uart initialization succeed
  118. */
  119. hal_uart_status_t HAL_UartInit(hal_uart_handle_t handle, hal_uart_config_t *config);
  120. /*!
  121. * @brief Deinitializes a uart instance.
  122. *
  123. * This function waits for TX complete, disables TX and RX, and disables the uart clock.
  124. *
  125. * @param handle uart handle pointer.
  126. * @retval kStatus_HAL_UartSuccess uart de-initialization succeed
  127. */
  128. hal_uart_status_t HAL_UartDeinit(hal_uart_handle_t handle);
  129. /* @} */
  130. /*!
  131. * @name Blocking bus Operations
  132. * @{
  133. */
  134. /*!
  135. * @brief Reads RX data register using a blocking method.
  136. *
  137. * This function polls the RX register, waits for the RX register to be full or for RX FIFO to
  138. * have data, and reads data from the RX register.
  139. *
  140. * @note The function #HAL_UartReceiveBlocking and the function #HAL_UartTransferReceiveNonBlocking
  141. * cannot be used at the same time.
  142. * And, the function #HAL_UartTransferAbortReceive cannot be used to abort the transmission of this function.
  143. *
  144. * @param handle uart handle pointer.
  145. * @param data Start address of the buffer to store the received data.
  146. * @param length Size of the buffer.
  147. * @retval kStatus_HAL_UartError An error occurred while receiving data.
  148. * @retval kStatus_HAL_UartParityError A parity error occurred while receiving data.
  149. * @retval kStatus_HAL_UartSuccess Successfully received all data.
  150. */
  151. hal_uart_status_t HAL_UartReceiveBlocking(hal_uart_handle_t handle, uint8_t *data, size_t length);
  152. /*!
  153. * @brief Writes to the TX register using a blocking method.
  154. *
  155. * This function polls the TX register, waits for the TX register to be empty or for the TX FIFO
  156. * to have room and writes data to the TX buffer.
  157. *
  158. * @note The function #HAL_UartSendBlocking and the function #HAL_UartTransferSendNonBlocking
  159. * cannot be used at the same time.
  160. * And, the function #HAL_UartTransferAbortSend cannot be used to abort the transmission of this function.
  161. *
  162. * @param handle uart handle pointer.
  163. * @param data Start address of the data to write.
  164. * @param length Size of the data to write.
  165. * @retval kStatus_HAL_UartSuccess Successfully sent all data.
  166. */
  167. hal_uart_status_t HAL_UartSendBlocking(hal_uart_handle_t handle, const uint8_t *data, size_t length);
  168. /* @} */
  169. #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
  170. /*!
  171. * @name Transactional
  172. * @note The transactional API and the functional API cannot be used at the same time. The macro
  173. * #HAL_UART_TRANSFER_MODE is used to set which one will be used. If #HAL_UART_TRANSFER_MODE is zero, the
  174. * functional API with non-blocking mode will be used. Otherwise, transactional API will be used.
  175. * @{
  176. */
  177. /*!
  178. * @brief Installs a callback and callback parameter.
  179. *
  180. * This function is used to install the callback and callback parameter for uart module.
  181. * When any status of the uart changed, the driver will notify the upper layer by the installed callback
  182. * function. And the status is also passed as status parameter when the callback is called.
  183. *
  184. * @param handle uart handle pointer.
  185. * @param callback The callback function.
  186. * @param callbackParam The parameter of the callback function.
  187. * @retval kStatus_HAL_UartSuccess Successfully install the callback.
  188. */
  189. hal_uart_status_t HAL_UartTransferInstallCallback(hal_uart_handle_t handle,
  190. hal_uart_transfer_callback_t callback,
  191. void *callbackParam);
  192. /*!
  193. * @brief Receives a buffer of data using an interrupt method.
  194. *
  195. * This function receives data using an interrupt method. This is a non-blocking function, which
  196. * returns directly without waiting for all data to be received.
  197. * The receive request is saved by the uart driver.
  198. * When the new data arrives, the receive request is serviced first.
  199. * When all data is received, the uart driver notifies the upper layer
  200. * through a callback function and passes the status parameter @ref kStatus_UART_RxIdle.
  201. *
  202. * @note The function #HAL_UartReceiveBlocking and the function #HAL_UartTransferReceiveNonBlocking
  203. * cannot be used at the same time.
  204. *
  205. * @param handle uart handle pointer.
  206. * @param transfer uart transfer structure, see #hal_uart_transfer_t.
  207. * @retval kStatus_HAL_UartSuccess Successfully queue the transfer into transmit queue.
  208. * @retval kStatus_HAL_UartRxBusy Previous receive request is not finished.
  209. * @retval kStatus_HAL_UartError An error occurred.
  210. */
  211. hal_uart_status_t HAL_UartTransferReceiveNonBlocking(hal_uart_handle_t handle, hal_uart_transfer_t *transfer);
  212. /*!
  213. * @brief Transmits a buffer of data using the interrupt method.
  214. *
  215. * This function sends data using an interrupt method. This is a non-blocking function, which
  216. * returns directly without waiting for all data to be written to the TX register. When
  217. * all data is written to the TX register in the ISR, the uart driver calls the callback
  218. * function and passes the @ref kStatus_UART_TxIdle as status parameter.
  219. *
  220. * @note The function #HAL_UartSendBlocking and the function #HAL_UartTransferSendNonBlocking
  221. * cannot be used at the same time.
  222. *
  223. * @param handle uart handle pointer.
  224. * @param transfer uart transfer structure. See #hal_uart_transfer_t.
  225. * @retval kStatus_HAL_UartSuccess Successfully start the data transmission.
  226. * @retval kStatus_HAL_UartTxBusy Previous transmission still not finished; data not all written to TX register yet.
  227. * @retval kStatus_HAL_UartError An error occurred.
  228. */
  229. hal_uart_status_t HAL_UartTransferSendNonBlocking(hal_uart_handle_t handle, hal_uart_transfer_t *transfer);
  230. /*!
  231. * @brief Gets the number of bytes that have been received.
  232. *
  233. * This function gets the number of bytes that have been received.
  234. *
  235. * @param handle uart handle pointer.
  236. * @param count Receive bytes count.
  237. * @retval kStatus_HAL_UartError An error occurred.
  238. * @retval kStatus_Success Get successfully through the parameter \p count.
  239. */
  240. hal_uart_status_t HAL_UartTransferGetReceiveCount(hal_uart_handle_t handle, uint32_t *count);
  241. /*!
  242. * @brief Gets the number of bytes written to the uart TX register.
  243. *
  244. * This function gets the number of bytes written to the uart TX
  245. * register by using the interrupt method.
  246. *
  247. * @param handle uart handle pointer.
  248. * @param count Send bytes count.
  249. * @retval kStatus_HAL_UartError An error occurred.
  250. * @retval kStatus_Success Get successfully through the parameter \p count.
  251. */
  252. hal_uart_status_t HAL_UartTransferGetSendCount(hal_uart_handle_t handle, uint32_t *count);
  253. /*!
  254. * @brief Aborts the interrupt-driven data receiving.
  255. *
  256. * This function aborts the interrupt-driven data receiving. The user can get the remainBytes to know
  257. * how many bytes are not received yet.
  258. *
  259. * @note The function #HAL_UartTransferAbortReceive cannot be used to abort the transmission of
  260. * the function #HAL_UartReceiveBlocking.
  261. *
  262. * @param handle uart handle pointer.
  263. * @retval kStatus_Success Get successfully abort the receiving.
  264. */
  265. hal_uart_status_t HAL_UartTransferAbortReceive(hal_uart_handle_t handle);
  266. /*!
  267. * @brief Aborts the interrupt-driven data sending.
  268. *
  269. * This function aborts the interrupt-driven data sending. The user can get the remainBytes to find out
  270. * how many bytes are not sent out.
  271. *
  272. * @note The function #HAL_UartTransferAbortSend cannot be used to abort the transmission of
  273. * the function #HAL_UartSendBlocking.
  274. *
  275. * @param handle uart handle pointer.
  276. * @retval kStatus_Success Get successfully abort the sending.
  277. */
  278. hal_uart_status_t HAL_UartTransferAbortSend(hal_uart_handle_t handle);
  279. /* @} */
  280. #else
  281. /*!
  282. * @name Functional API with non-blocking mode.
  283. * @note The functional API and the transactional API cannot be used at the same time. The macro
  284. * #HAL_UART_TRANSFER_MODE is used to set which one will be used. If #HAL_UART_TRANSFER_MODE is zero, the
  285. * functional API with non-blocking mode will be used. Otherwise, transactional API will be used.
  286. * @{
  287. */
  288. /*!
  289. * @brief Installs a callback and callback parameter.
  290. *
  291. * This function is used to install the callback and callback parameter for uart module.
  292. * When non-blocking sending or receiving finished, the adapter will notify the upper layer by the installed callback
  293. * function. And the status is also passed as status parameter when the callback is called.
  294. *
  295. * @param handle uart handle pointer.
  296. * @param callback The callback function.
  297. * @param callbackParam The parameter of the callback function.
  298. * @retval kStatus_HAL_UartSuccess Successfully install the callback.
  299. */
  300. hal_uart_status_t HAL_UartInstallCallback(hal_uart_handle_t handle,
  301. hal_uart_transfer_callback_t callback,
  302. void *callbackParam);
  303. /*!
  304. * @brief Receives a buffer of data using an interrupt method.
  305. *
  306. * This function receives data using an interrupt method. This is a non-blocking function, which
  307. * returns directly without waiting for all data to be received.
  308. * The receive request is saved by the uart adapter.
  309. * When the new data arrives, the receive request is serviced first.
  310. * When all data is received, the uart adapter notifies the upper layer
  311. * through a callback function and passes the status parameter @ref kStatus_UART_RxIdle.
  312. *
  313. * @note The function #HAL_UartReceiveBlocking and the function #HAL_UartReceiveNonBlocking
  314. * cannot be used at the same time.
  315. *
  316. * @param handle uart handle pointer.
  317. * @param data Start address of the data to write.
  318. * @param length Size of the data to write.
  319. * @retval kStatus_HAL_UartSuccess Successfully queue the transfer into transmit queue.
  320. * @retval kStatus_HAL_UartRxBusy Previous receive request is not finished.
  321. * @retval kStatus_HAL_UartError An error occurred.
  322. */
  323. hal_uart_status_t HAL_UartReceiveNonBlocking(hal_uart_handle_t handle, uint8_t *data, size_t length);
  324. /*!
  325. * @brief Transmits a buffer of data using the interrupt method.
  326. *
  327. * This function sends data using an interrupt method. This is a non-blocking function, which
  328. * returns directly without waiting for all data to be written to the TX register. When
  329. * all data is written to the TX register in the ISR, the uart driver calls the callback
  330. * function and passes the @ref kStatus_UART_TxIdle as status parameter.
  331. *
  332. * @note The function #HAL_UartSendBlocking and the function #HAL_UartSendNonBlocking
  333. * cannot be used at the same time.
  334. *
  335. * @param handle uart handle pointer.
  336. * @param data Start address of the data to write.
  337. * @param length Size of the data to write.
  338. * @retval kStatus_HAL_UartSuccess Successfully start the data transmission.
  339. * @retval kStatus_HAL_UartTxBusy Previous transmission still not finished; data not all written to TX register yet.
  340. * @retval kStatus_HAL_UartError An error occurred.
  341. */
  342. hal_uart_status_t HAL_UartSendNonBlocking(hal_uart_handle_t handle, const uint8_t *data, size_t length);
  343. /*!
  344. * @brief Gets the number of bytes that have been received.
  345. *
  346. * This function gets the number of bytes that have been received.
  347. *
  348. * @param handle uart handle pointer.
  349. * @param count Receive bytes count.
  350. * @retval kStatus_HAL_UartError An error occurred.
  351. * @retval kStatus_Success Get successfully through the parameter \p count.
  352. */
  353. hal_uart_status_t HAL_UartGetReceiveCount(hal_uart_handle_t handle, uint32_t *count);
  354. /*!
  355. * @brief Gets the number of bytes written to the uart TX register.
  356. *
  357. * This function gets the number of bytes written to the uart TX
  358. * register by using the interrupt method.
  359. *
  360. * @param handle uart handle pointer.
  361. * @param count Send bytes count.
  362. * @retval kStatus_HAL_UartError An error occurred.
  363. * @retval kStatus_Success Get successfully through the parameter \p count.
  364. */
  365. hal_uart_status_t HAL_UartGetSendCount(hal_uart_handle_t handle, uint32_t *count);
  366. /*!
  367. * @brief Aborts the interrupt-driven data receiving.
  368. *
  369. * This function aborts the interrupt-driven data receiving. The user can get the remainBytes to know
  370. * how many bytes are not received yet.
  371. *
  372. * @note The function #HAL_UartAbortReceive cannot be used to abort the transmission of
  373. * the function #HAL_UartReceiveBlocking.
  374. *
  375. * @param handle uart handle pointer.
  376. * @retval kStatus_Success Get successfully abort the receiving.
  377. */
  378. hal_uart_status_t HAL_UartAbortReceive(hal_uart_handle_t handle);
  379. /*!
  380. * @brief Aborts the interrupt-driven data sending.
  381. *
  382. * This function aborts the interrupt-driven data sending. The user can get the remainBytes to find out
  383. * how many bytes are not sent out.
  384. *
  385. * @note The function #HAL_UartAbortSend cannot be used to abort the transmission of
  386. * the function #HAL_UartSendBlocking.
  387. *
  388. * @param handle uart handle pointer.
  389. * @retval kStatus_Success Get successfully abort the sending.
  390. */
  391. hal_uart_status_t HAL_UartAbortSend(hal_uart_handle_t handle);
  392. /* @} */
  393. #endif
  394. /*!
  395. * @brief uart IRQ handle function.
  396. *
  397. * This function handles the uart transmit and receive IRQ request.
  398. *
  399. * @param handle uart handle pointer.
  400. */
  401. void HAL_UartIsrFunction(hal_uart_handle_t handle);
  402. #if defined(__cplusplus)
  403. }
  404. #endif
  405. #endif /* __HAL_UART_ADAPTER_H__ */