fsl_uart.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /*
  2. * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef _FSL_UART_H_
  31. #define _FSL_UART_H_
  32. #include "fsl_common.h"
  33. /*!
  34. * @addtogroup uart_driver
  35. * @{
  36. */
  37. /*******************************************************************************
  38. * Definitions
  39. ******************************************************************************/
  40. /*! @name Driver version */
  41. /*@{*/
  42. /*! @brief UART driver version 2.1.4. */
  43. #define FSL_UART_DRIVER_VERSION (MAKE_VERSION(2, 1, 4))
  44. /*@}*/
  45. /*! @brief Error codes for the UART driver. */
  46. enum _uart_status
  47. {
  48. kStatus_UART_TxBusy = MAKE_STATUS(kStatusGroup_UART, 0), /*!< Transmitter is busy. */
  49. kStatus_UART_RxBusy = MAKE_STATUS(kStatusGroup_UART, 1), /*!< Receiver is busy. */
  50. kStatus_UART_TxIdle = MAKE_STATUS(kStatusGroup_UART, 2), /*!< UART transmitter is idle. */
  51. kStatus_UART_RxIdle = MAKE_STATUS(kStatusGroup_UART, 3), /*!< UART receiver is idle. */
  52. kStatus_UART_TxWatermarkTooLarge = MAKE_STATUS(kStatusGroup_UART, 4), /*!< TX FIFO watermark too large */
  53. kStatus_UART_RxWatermarkTooLarge = MAKE_STATUS(kStatusGroup_UART, 5), /*!< RX FIFO watermark too large */
  54. kStatus_UART_FlagCannotClearManually =
  55. MAKE_STATUS(kStatusGroup_UART, 6), /*!< UART flag can't be manually cleared. */
  56. kStatus_UART_Error = MAKE_STATUS(kStatusGroup_UART, 7), /*!< Error happens on UART. */
  57. kStatus_UART_RxRingBufferOverrun = MAKE_STATUS(kStatusGroup_UART, 8), /*!< UART RX software ring buffer overrun. */
  58. kStatus_UART_RxHardwareOverrun = MAKE_STATUS(kStatusGroup_UART, 9), /*!< UART RX receiver overrun. */
  59. kStatus_UART_NoiseError = MAKE_STATUS(kStatusGroup_UART, 10), /*!< UART noise error. */
  60. kStatus_UART_FramingError = MAKE_STATUS(kStatusGroup_UART, 11), /*!< UART framing error. */
  61. kStatus_UART_ParityError = MAKE_STATUS(kStatusGroup_UART, 12), /*!< UART parity error. */
  62. kStatus_UART_BaudrateNotSupport =
  63. MAKE_STATUS(kStatusGroup_UART, 13), /*!< Baudrate is not support in current clock source */
  64. };
  65. /*! @brief UART parity mode. */
  66. typedef enum _uart_parity_mode
  67. {
  68. kUART_ParityDisabled = 0x0U, /*!< Parity disabled */
  69. kUART_ParityEven = 0x2U, /*!< Parity enabled, type even, bit setting: PE|PT = 10 */
  70. kUART_ParityOdd = 0x3U, /*!< Parity enabled, type odd, bit setting: PE|PT = 11 */
  71. } uart_parity_mode_t;
  72. /*! @brief UART stop bit count. */
  73. typedef enum _uart_stop_bit_count
  74. {
  75. kUART_OneStopBit = 0U, /*!< One stop bit */
  76. kUART_TwoStopBit = 1U, /*!< Two stop bits */
  77. } uart_stop_bit_count_t;
  78. /*!
  79. * @brief UART interrupt configuration structure, default settings all disabled.
  80. *
  81. * This structure contains the settings for all of the UART interrupt configurations.
  82. */
  83. enum _uart_interrupt_enable
  84. {
  85. #if defined(FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT
  86. kUART_LinBreakInterruptEnable = (UART_BDH_LBKDIE_MASK), /*!< LIN break detect interrupt. */
  87. #endif
  88. kUART_RxActiveEdgeInterruptEnable = (UART_BDH_RXEDGIE_MASK), /*!< RX active edge interrupt. */
  89. kUART_TxDataRegEmptyInterruptEnable = (UART_C2_TIE_MASK << 8), /*!< Transmit data register empty interrupt. */
  90. kUART_TransmissionCompleteInterruptEnable = (UART_C2_TCIE_MASK << 8), /*!< Transmission complete interrupt. */
  91. kUART_RxDataRegFullInterruptEnable = (UART_C2_RIE_MASK << 8), /*!< Receiver data register full interrupt. */
  92. kUART_IdleLineInterruptEnable = (UART_C2_ILIE_MASK << 8), /*!< Idle line interrupt. */
  93. kUART_RxOverrunInterruptEnable = (UART_C3_ORIE_MASK << 16), /*!< Receiver overrun interrupt. */
  94. kUART_NoiseErrorInterruptEnable = (UART_C3_NEIE_MASK << 16), /*!< Noise error flag interrupt. */
  95. kUART_FramingErrorInterruptEnable = (UART_C3_FEIE_MASK << 16), /*!< Framing error flag interrupt. */
  96. kUART_ParityErrorInterruptEnable = (UART_C3_PEIE_MASK << 16), /*!< Parity error flag interrupt. */
  97. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  98. kUART_RxFifoOverflowInterruptEnable = (UART_CFIFO_RXOFE_MASK << 24), /*!< RX FIFO overflow interrupt. */
  99. kUART_TxFifoOverflowInterruptEnable = (UART_CFIFO_TXOFE_MASK << 24), /*!< TX FIFO overflow interrupt. */
  100. kUART_RxFifoUnderflowInterruptEnable = (UART_CFIFO_RXUFE_MASK << 24), /*!< RX FIFO underflow interrupt. */
  101. #endif
  102. kUART_AllInterruptsEnable =
  103. #if defined(FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT
  104. kUART_LinBreakInterruptEnable |
  105. #endif
  106. kUART_RxActiveEdgeInterruptEnable | kUART_TxDataRegEmptyInterruptEnable |
  107. kUART_TransmissionCompleteInterruptEnable | kUART_RxDataRegFullInterruptEnable | kUART_IdleLineInterruptEnable |
  108. kUART_RxOverrunInterruptEnable | kUART_NoiseErrorInterruptEnable | kUART_FramingErrorInterruptEnable |
  109. kUART_ParityErrorInterruptEnable
  110. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  111. |
  112. kUART_RxFifoOverflowInterruptEnable | kUART_TxFifoOverflowInterruptEnable | kUART_RxFifoUnderflowInterruptEnable
  113. #endif
  114. ,
  115. };
  116. /*!
  117. * @brief UART status flags.
  118. *
  119. * This provides constants for the UART status flags for use in the UART functions.
  120. */
  121. enum _uart_flags
  122. {
  123. kUART_TxDataRegEmptyFlag = (UART_S1_TDRE_MASK), /*!< TX data register empty flag. */
  124. kUART_TransmissionCompleteFlag = (UART_S1_TC_MASK), /*!< Transmission complete flag. */
  125. kUART_RxDataRegFullFlag = (UART_S1_RDRF_MASK), /*!< RX data register full flag. */
  126. kUART_IdleLineFlag = (UART_S1_IDLE_MASK), /*!< Idle line detect flag. */
  127. kUART_RxOverrunFlag = (UART_S1_OR_MASK), /*!< RX overrun flag. */
  128. kUART_NoiseErrorFlag = (UART_S1_NF_MASK), /*!< RX takes 3 samples of each received bit.
  129. If any of these samples differ, noise flag sets */
  130. kUART_FramingErrorFlag = (UART_S1_FE_MASK), /*!< Frame error flag, sets if logic 0 was detected
  131. where stop bit expected */
  132. kUART_ParityErrorFlag = (UART_S1_PF_MASK), /*!< If parity enabled, sets upon parity error detection */
  133. #if defined(FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT
  134. kUART_LinBreakFlag =
  135. (UART_S2_LBKDIF_MASK
  136. << 8), /*!< LIN break detect interrupt flag, sets when
  137. LIN break char detected and LIN circuit enabled */
  138. #endif
  139. kUART_RxActiveEdgeFlag =
  140. (UART_S2_RXEDGIF_MASK << 8), /*!< RX pin active edge interrupt flag,
  141. sets when active edge detected */
  142. kUART_RxActiveFlag =
  143. (UART_S2_RAF_MASK << 8), /*!< Receiver Active Flag (RAF),
  144. sets at beginning of valid start bit */
  145. #if defined(FSL_FEATURE_UART_HAS_EXTENDED_DATA_REGISTER_FLAGS) && FSL_FEATURE_UART_HAS_EXTENDED_DATA_REGISTER_FLAGS
  146. kUART_NoiseErrorInRxDataRegFlag = (UART_ED_NOISY_MASK << 16), /*!< Noisy bit, sets if noise detected. */
  147. kUART_ParityErrorInRxDataRegFlag = (UART_ED_PARITYE_MASK << 16), /*!< Paritye bit, sets if parity error detected. */
  148. #endif
  149. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  150. kUART_TxFifoEmptyFlag = (UART_SFIFO_TXEMPT_MASK << 24), /*!< TXEMPT bit, sets if TX buffer is empty */
  151. kUART_RxFifoEmptyFlag = (UART_SFIFO_RXEMPT_MASK << 24), /*!< RXEMPT bit, sets if RX buffer is empty */
  152. kUART_TxFifoOverflowFlag = (UART_SFIFO_TXOF_MASK << 24), /*!< TXOF bit, sets if TX buffer overflow occurred */
  153. kUART_RxFifoOverflowFlag = (UART_SFIFO_RXOF_MASK << 24), /*!< RXOF bit, sets if receive buffer overflow */
  154. kUART_RxFifoUnderflowFlag = (UART_SFIFO_RXUF_MASK << 24), /*!< RXUF bit, sets if receive buffer underflow */
  155. #endif
  156. };
  157. /*! @brief UART configuration structure. */
  158. typedef struct _uart_config
  159. {
  160. uint32_t baudRate_Bps; /*!< UART baud rate */
  161. uart_parity_mode_t parityMode; /*!< Parity mode, disabled (default), even, odd */
  162. #if defined(FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT) && FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT
  163. uart_stop_bit_count_t stopBitCount; /*!< Number of stop bits, 1 stop bit (default) or 2 stop bits */
  164. #endif
  165. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  166. uint8_t txFifoWatermark; /*!< TX FIFO watermark */
  167. uint8_t rxFifoWatermark; /*!< RX FIFO watermark */
  168. #endif
  169. bool enableTx; /*!< Enable TX */
  170. bool enableRx; /*!< Enable RX */
  171. } uart_config_t;
  172. /*! @brief UART transfer structure. */
  173. typedef struct _uart_transfer
  174. {
  175. uint8_t *data; /*!< The buffer of data to be transfer.*/
  176. size_t dataSize; /*!< The byte count to be transfer. */
  177. } uart_transfer_t;
  178. /* Forward declaration of the handle typedef. */
  179. typedef struct _uart_handle uart_handle_t;
  180. /*! @brief UART transfer callback function. */
  181. typedef void (*uart_transfer_callback_t)(UART_Type *base, uart_handle_t *handle, status_t status, void *userData);
  182. /*! @brief UART handle structure. */
  183. struct _uart_handle
  184. {
  185. uint8_t *volatile txData; /*!< Address of remaining data to send. */
  186. volatile size_t txDataSize; /*!< Size of the remaining data to send. */
  187. size_t txDataSizeAll; /*!< Size of the data to send out. */
  188. uint8_t *volatile rxData; /*!< Address of remaining data to receive. */
  189. volatile size_t rxDataSize; /*!< Size of the remaining data to receive. */
  190. size_t rxDataSizeAll; /*!< Size of the data to receive. */
  191. uint8_t *rxRingBuffer; /*!< Start address of the receiver ring buffer. */
  192. size_t rxRingBufferSize; /*!< Size of the ring buffer. */
  193. volatile uint16_t rxRingBufferHead; /*!< Index for the driver to store received data into ring buffer. */
  194. volatile uint16_t rxRingBufferTail; /*!< Index for the user to get data from the ring buffer. */
  195. uart_transfer_callback_t callback; /*!< Callback function. */
  196. void *userData; /*!< UART callback function parameter.*/
  197. volatile uint8_t txState; /*!< TX transfer state. */
  198. volatile uint8_t rxState; /*!< RX transfer state */
  199. };
  200. /*******************************************************************************
  201. * API
  202. ******************************************************************************/
  203. #if defined(__cplusplus)
  204. extern "C" {
  205. #endif /* _cplusplus */
  206. /*!
  207. * @name Initialization and deinitialization
  208. * @{
  209. */
  210. /*!
  211. * @brief Initializes a UART instance with a user configuration structure and peripheral clock.
  212. *
  213. * This function configures the UART module with the user-defined settings. The user can configure the configuration
  214. * structure and also get the default configuration by using the UART_GetDefaultConfig() function.
  215. * The example below shows how to use this API to configure UART.
  216. * @code
  217. * uart_config_t uartConfig;
  218. * uartConfig.baudRate_Bps = 115200U;
  219. * uartConfig.parityMode = kUART_ParityDisabled;
  220. * uartConfig.stopBitCount = kUART_OneStopBit;
  221. * uartConfig.txFifoWatermark = 0;
  222. * uartConfig.rxFifoWatermark = 1;
  223. * UART_Init(UART1, &uartConfig, 20000000U);
  224. * @endcode
  225. *
  226. * @param base UART peripheral base address.
  227. * @param config Pointer to the user-defined configuration structure.
  228. * @param srcClock_Hz UART clock source frequency in HZ.
  229. * @retval kStatus_UART_BaudrateNotSupport Baudrate is not support in current clock source.
  230. * @retval kStatus_Success Status UART initialize succeed
  231. */
  232. status_t UART_Init(UART_Type *base, const uart_config_t *config, uint32_t srcClock_Hz);
  233. /*!
  234. * @brief Deinitializes a UART instance.
  235. *
  236. * This function waits for TX complete, disables TX and RX, and disables the UART clock.
  237. *
  238. * @param base UART peripheral base address.
  239. */
  240. void UART_Deinit(UART_Type *base);
  241. /*!
  242. * @brief Gets the default configuration structure.
  243. *
  244. * This function initializes the UART configuration structure to a default value. The default
  245. * values are as follows.
  246. * uartConfig->baudRate_Bps = 115200U;
  247. * uartConfig->bitCountPerChar = kUART_8BitsPerChar;
  248. * uartConfig->parityMode = kUART_ParityDisabled;
  249. * uartConfig->stopBitCount = kUART_OneStopBit;
  250. * uartConfig->txFifoWatermark = 0;
  251. * uartConfig->rxFifoWatermark = 1;
  252. * uartConfig->enableTx = false;
  253. * uartConfig->enableRx = false;
  254. *
  255. * @param config Pointer to configuration structure.
  256. */
  257. void UART_GetDefaultConfig(uart_config_t *config);
  258. /*!
  259. * @brief Sets the UART instance baud rate.
  260. *
  261. * This function configures the UART module baud rate. This function is used to update
  262. * the UART module baud rate after the UART module is initialized by the UART_Init.
  263. * @code
  264. * UART_SetBaudRate(UART1, 115200U, 20000000U);
  265. * @endcode
  266. *
  267. * @param base UART peripheral base address.
  268. * @param baudRate_Bps UART baudrate to be set.
  269. * @param srcClock_Hz UART clock source freqency in Hz.
  270. * @retval kStatus_UART_BaudrateNotSupport Baudrate is not support in the current clock source.
  271. * @retval kStatus_Success Set baudrate succeeded.
  272. */
  273. status_t UART_SetBaudRate(UART_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz);
  274. /* @} */
  275. /*!
  276. * @name Status
  277. * @{
  278. */
  279. /*!
  280. * @brief Gets UART status flags.
  281. *
  282. * This function gets all UART status flags. The flags are returned as the logical
  283. * OR value of the enumerators @ref _uart_flags. To check a specific status,
  284. * compare the return value with enumerators in @ref _uart_flags.
  285. * For example, to check whether the TX is empty, do the following.
  286. * @code
  287. * if (kUART_TxDataRegEmptyFlag & UART_GetStatusFlags(UART1))
  288. * {
  289. * ...
  290. * }
  291. * @endcode
  292. *
  293. * @param base UART peripheral base address.
  294. * @return UART status flags which are ORed by the enumerators in the _uart_flags.
  295. */
  296. uint32_t UART_GetStatusFlags(UART_Type *base);
  297. /*!
  298. * @brief Clears status flags with the provided mask.
  299. *
  300. * This function clears UART status flags with a provided mask. An automatically cleared flag
  301. * can't be cleared by this function.
  302. * These flags can only be cleared or set by hardware.
  303. * kUART_TxDataRegEmptyFlag, kUART_TransmissionCompleteFlag, kUART_RxDataRegFullFlag,
  304. * kUART_RxActiveFlag, kUART_NoiseErrorInRxDataRegFlag, kUART_ParityErrorInRxDataRegFlag,
  305. * kUART_TxFifoEmptyFlag,kUART_RxFifoEmptyFlag
  306. * Note that this API should be called when the Tx/Rx is idle. Otherwise it has no effect.
  307. *
  308. * @param base UART peripheral base address.
  309. * @param mask The status flags to be cleared; it is logical OR value of @ref _uart_flags.
  310. * @retval kStatus_UART_FlagCannotClearManually The flag can't be cleared by this function but
  311. * it is cleared automatically by hardware.
  312. * @retval kStatus_Success Status in the mask is cleared.
  313. */
  314. status_t UART_ClearStatusFlags(UART_Type *base, uint32_t mask);
  315. /* @} */
  316. /*!
  317. * @name Interrupts
  318. * @{
  319. */
  320. /*!
  321. * @brief Enables UART interrupts according to the provided mask.
  322. *
  323. * This function enables the UART interrupts according to the provided mask. The mask
  324. * is a logical OR of enumeration members. See @ref _uart_interrupt_enable.
  325. * For example, to enable TX empty interrupt and RX full interrupt, do the following.
  326. * @code
  327. * UART_EnableInterrupts(UART1,kUART_TxDataRegEmptyInterruptEnable | kUART_RxDataRegFullInterruptEnable);
  328. * @endcode
  329. *
  330. * @param base UART peripheral base address.
  331. * @param mask The interrupts to enable. Logical OR of @ref _uart_interrupt_enable.
  332. */
  333. void UART_EnableInterrupts(UART_Type *base, uint32_t mask);
  334. /*!
  335. * @brief Disables the UART interrupts according to the provided mask.
  336. *
  337. * This function disables the UART interrupts according to the provided mask. The mask
  338. * is a logical OR of enumeration members. See @ref _uart_interrupt_enable.
  339. * For example, to disable TX empty interrupt and RX full interrupt do the following.
  340. * @code
  341. * UART_DisableInterrupts(UART1,kUART_TxDataRegEmptyInterruptEnable | kUART_RxDataRegFullInterruptEnable);
  342. * @endcode
  343. *
  344. * @param base UART peripheral base address.
  345. * @param mask The interrupts to disable. Logical OR of @ref _uart_interrupt_enable.
  346. */
  347. void UART_DisableInterrupts(UART_Type *base, uint32_t mask);
  348. /*!
  349. * @brief Gets the enabled UART interrupts.
  350. *
  351. * This function gets the enabled UART interrupts. The enabled interrupts are returned
  352. * as the logical OR value of the enumerators @ref _uart_interrupt_enable. To check
  353. * a specific interrupts enable status, compare the return value with enumerators
  354. * in @ref _uart_interrupt_enable.
  355. * For example, to check whether TX empty interrupt is enabled, do the following.
  356. * @code
  357. * uint32_t enabledInterrupts = UART_GetEnabledInterrupts(UART1);
  358. *
  359. * if (kUART_TxDataRegEmptyInterruptEnable & enabledInterrupts)
  360. * {
  361. * ...
  362. * }
  363. * @endcode
  364. *
  365. * @param base UART peripheral base address.
  366. * @return UART interrupt flags which are logical OR of the enumerators in @ref _uart_interrupt_enable.
  367. */
  368. uint32_t UART_GetEnabledInterrupts(UART_Type *base);
  369. /* @} */
  370. #if defined(FSL_FEATURE_UART_HAS_DMA_SELECT) && FSL_FEATURE_UART_HAS_DMA_SELECT
  371. /*!
  372. * @name DMA Control
  373. * @{
  374. */
  375. /*!
  376. * @brief Gets the UART data register address.
  377. *
  378. * This function returns the UART data register address, which is mainly used by DMA/eDMA.
  379. *
  380. * @param base UART peripheral base address.
  381. * @return UART data register addresses which are used both by the transmitter and the receiver.
  382. */
  383. static inline uint32_t UART_GetDataRegisterAddress(UART_Type *base)
  384. {
  385. return (uint32_t) & (base->D);
  386. }
  387. /*!
  388. * @brief Enables or disables the UART transmitter DMA request.
  389. *
  390. * This function enables or disables the transmit data register empty flag, S1[TDRE], to generate the DMA requests.
  391. *
  392. * @param base UART peripheral base address.
  393. * @param enable True to enable, false to disable.
  394. */
  395. static inline void UART_EnableTxDMA(UART_Type *base, bool enable)
  396. {
  397. if (enable)
  398. {
  399. #if (defined(FSL_FEATURE_UART_IS_SCI) && FSL_FEATURE_UART_IS_SCI)
  400. base->C4 |= UART_C4_TDMAS_MASK;
  401. #else
  402. base->C5 |= UART_C5_TDMAS_MASK;
  403. #endif
  404. base->C2 |= UART_C2_TIE_MASK;
  405. }
  406. else
  407. {
  408. #if (defined(FSL_FEATURE_UART_IS_SCI) && FSL_FEATURE_UART_IS_SCI)
  409. base->C4 &= ~UART_C4_TDMAS_MASK;
  410. #else
  411. base->C5 &= ~UART_C5_TDMAS_MASK;
  412. #endif
  413. base->C2 &= ~UART_C2_TIE_MASK;
  414. }
  415. }
  416. /*!
  417. * @brief Enables or disables the UART receiver DMA.
  418. *
  419. * This function enables or disables the receiver data register full flag, S1[RDRF], to generate DMA requests.
  420. *
  421. * @param base UART peripheral base address.
  422. * @param enable True to enable, false to disable.
  423. */
  424. static inline void UART_EnableRxDMA(UART_Type *base, bool enable)
  425. {
  426. if (enable)
  427. {
  428. #if (defined(FSL_FEATURE_UART_IS_SCI) && FSL_FEATURE_UART_IS_SCI)
  429. base->C4 |= UART_C4_RDMAS_MASK;
  430. #else
  431. base->C5 |= UART_C5_RDMAS_MASK;
  432. #endif
  433. base->C2 |= UART_C2_RIE_MASK;
  434. }
  435. else
  436. {
  437. #if (defined(FSL_FEATURE_UART_IS_SCI) && FSL_FEATURE_UART_IS_SCI)
  438. base->C4 &= ~UART_C4_RDMAS_MASK;
  439. #else
  440. base->C5 &= ~UART_C5_RDMAS_MASK;
  441. #endif
  442. base->C2 &= ~UART_C2_RIE_MASK;
  443. }
  444. }
  445. /* @} */
  446. #endif /* FSL_FEATURE_UART_HAS_DMA_SELECT */
  447. /*!
  448. * @name Bus Operations
  449. * @{
  450. */
  451. /*!
  452. * @brief Enables or disables the UART transmitter.
  453. *
  454. * This function enables or disables the UART transmitter.
  455. *
  456. * @param base UART peripheral base address.
  457. * @param enable True to enable, false to disable.
  458. */
  459. static inline void UART_EnableTx(UART_Type *base, bool enable)
  460. {
  461. if (enable)
  462. {
  463. base->C2 |= UART_C2_TE_MASK;
  464. }
  465. else
  466. {
  467. base->C2 &= ~UART_C2_TE_MASK;
  468. }
  469. }
  470. /*!
  471. * @brief Enables or disables the UART receiver.
  472. *
  473. * This function enables or disables the UART receiver.
  474. *
  475. * @param base UART peripheral base address.
  476. * @param enable True to enable, false to disable.
  477. */
  478. static inline void UART_EnableRx(UART_Type *base, bool enable)
  479. {
  480. if (enable)
  481. {
  482. base->C2 |= UART_C2_RE_MASK;
  483. }
  484. else
  485. {
  486. base->C2 &= ~UART_C2_RE_MASK;
  487. }
  488. }
  489. /*!
  490. * @brief Writes to the TX register.
  491. *
  492. * This function writes data to the TX register directly. The upper layer must ensure
  493. * that the TX register is empty or TX FIFO has empty room before calling this function.
  494. *
  495. * @param base UART peripheral base address.
  496. * @param data The byte to write.
  497. */
  498. static inline void UART_WriteByte(UART_Type *base, uint8_t data)
  499. {
  500. base->D = data;
  501. }
  502. /*!
  503. * @brief Reads the RX register directly.
  504. *
  505. * This function reads data from the RX register directly. The upper layer must
  506. * ensure that the RX register is full or that the TX FIFO has data before calling this function.
  507. *
  508. * @param base UART peripheral base address.
  509. * @return The byte read from UART data register.
  510. */
  511. static inline uint8_t UART_ReadByte(UART_Type *base)
  512. {
  513. return base->D;
  514. }
  515. /*!
  516. * @brief Writes to the TX register using a blocking method.
  517. *
  518. * This function polls the TX register, waits for the TX register to be empty or for the TX FIFO
  519. * to have room and writes data to the TX buffer.
  520. *
  521. * @note This function does not check whether all data is sent out to the bus.
  522. * Before disabling the TX, check kUART_TransmissionCompleteFlag to ensure that the TX is
  523. * finished.
  524. *
  525. * @param base UART peripheral base address.
  526. * @param data Start address of the data to write.
  527. * @param length Size of the data to write.
  528. */
  529. void UART_WriteBlocking(UART_Type *base, const uint8_t *data, size_t length);
  530. /*!
  531. * @brief Read RX data register using a blocking method.
  532. *
  533. * This function polls the RX register, waits for the RX register to be full or for RX FIFO to
  534. * have data, and reads data from the TX register.
  535. *
  536. * @param base UART peripheral base address.
  537. * @param data Start address of the buffer to store the received data.
  538. * @param length Size of the buffer.
  539. * @retval kStatus_UART_RxHardwareOverrun Receiver overrun occurred while receiving data.
  540. * @retval kStatus_UART_NoiseError A noise error occurred while receiving data.
  541. * @retval kStatus_UART_FramingError A framing error occurred while receiving data.
  542. * @retval kStatus_UART_ParityError A parity error occurred while receiving data.
  543. * @retval kStatus_Success Successfully received all data.
  544. */
  545. status_t UART_ReadBlocking(UART_Type *base, uint8_t *data, size_t length);
  546. /* @} */
  547. /*!
  548. * @name Transactional
  549. * @{
  550. */
  551. /*!
  552. * @brief Initializes the UART handle.
  553. *
  554. * This function initializes the UART handle which can be used for other UART
  555. * transactional APIs. Usually, for a specified UART instance,
  556. * call this API once to get the initialized handle.
  557. *
  558. * @param base UART peripheral base address.
  559. * @param handle UART handle pointer.
  560. * @param callback The callback function.
  561. * @param userData The parameter of the callback function.
  562. */
  563. void UART_TransferCreateHandle(UART_Type *base,
  564. uart_handle_t *handle,
  565. uart_transfer_callback_t callback,
  566. void *userData);
  567. /*!
  568. * @brief Sets up the RX ring buffer.
  569. *
  570. * This function sets up the RX ring buffer to a specific UART handle.
  571. *
  572. * When the RX ring buffer is used, data received are stored into the ring buffer even when the
  573. * user doesn't call the UART_TransferReceiveNonBlocking() API. If data is already received
  574. * in the ring buffer, the user can get the received data from the ring buffer directly.
  575. *
  576. * @note When using the RX ring buffer, one byte is reserved for internal use. In other
  577. * words, if @p ringBufferSize is 32, only 31 bytes are used for saving data.
  578. *
  579. * @param base UART peripheral base address.
  580. * @param handle UART handle pointer.
  581. * @param ringBuffer Start address of the ring buffer for background receiving. Pass NULL to disable the ring buffer.
  582. * @param ringBufferSize Size of the ring buffer.
  583. */
  584. void UART_TransferStartRingBuffer(UART_Type *base, uart_handle_t *handle, uint8_t *ringBuffer, size_t ringBufferSize);
  585. /*!
  586. * @brief Aborts the background transfer and uninstalls the ring buffer.
  587. *
  588. * This function aborts the background transfer and uninstalls the ring buffer.
  589. *
  590. * @param base UART peripheral base address.
  591. * @param handle UART handle pointer.
  592. */
  593. void UART_TransferStopRingBuffer(UART_Type *base, uart_handle_t *handle);
  594. /*!
  595. * @brief Transmits a buffer of data using the interrupt method.
  596. *
  597. * This function sends data using an interrupt method. This is a non-blocking function, which
  598. * returns directly without waiting for all data to be written to the TX register. When
  599. * all data is written to the TX register in the ISR, the UART driver calls the callback
  600. * function and passes the @ref kStatus_UART_TxIdle as status parameter.
  601. *
  602. * @note The kStatus_UART_TxIdle is passed to the upper layer when all data is written
  603. * to the TX register. However, it does not ensure that all data is sent out. Before disabling the TX,
  604. * check the kUART_TransmissionCompleteFlag to ensure that the TX is finished.
  605. *
  606. * @param base UART peripheral base address.
  607. * @param handle UART handle pointer.
  608. * @param xfer UART transfer structure. See #uart_transfer_t.
  609. * @retval kStatus_Success Successfully start the data transmission.
  610. * @retval kStatus_UART_TxBusy Previous transmission still not finished; data not all written to TX register yet.
  611. * @retval kStatus_InvalidArgument Invalid argument.
  612. */
  613. status_t UART_TransferSendNonBlocking(UART_Type *base, uart_handle_t *handle, uart_transfer_t *xfer);
  614. /*!
  615. * @brief Aborts the interrupt-driven data transmit.
  616. *
  617. * This function aborts the interrupt-driven data sending. The user can get the remainBytes to find out
  618. * how many bytes are not sent out.
  619. *
  620. * @param base UART peripheral base address.
  621. * @param handle UART handle pointer.
  622. */
  623. void UART_TransferAbortSend(UART_Type *base, uart_handle_t *handle);
  624. /*!
  625. * @brief Gets the number of bytes written to the UART TX register.
  626. *
  627. * This function gets the number of bytes written to the UART TX
  628. * register by using the interrupt method.
  629. *
  630. * @param base UART peripheral base address.
  631. * @param handle UART handle pointer.
  632. * @param count Send bytes count.
  633. * @retval kStatus_NoTransferInProgress No send in progress.
  634. * @retval kStatus_InvalidArgument The parameter is invalid.
  635. * @retval kStatus_Success Get successfully through the parameter \p count;
  636. */
  637. status_t UART_TransferGetSendCount(UART_Type *base, uart_handle_t *handle, uint32_t *count);
  638. /*!
  639. * @brief Receives a buffer of data using an interrupt method.
  640. *
  641. * This function receives data using an interrupt method. This is a non-blocking function, which
  642. * returns without waiting for all data to be received.
  643. * If the RX ring buffer is used and not empty, the data in the ring buffer is copied and
  644. * the parameter @p receivedBytes shows how many bytes are copied from the ring buffer.
  645. * After copying, if the data in the ring buffer is not enough to read, the receive
  646. * request is saved by the UART driver. When the new data arrives, the receive request
  647. * is serviced first. When all data is received, the UART driver notifies the upper layer
  648. * through a callback function and passes the status parameter @ref kStatus_UART_RxIdle.
  649. * For example, the upper layer needs 10 bytes but there are only 5 bytes in the ring buffer.
  650. * The 5 bytes are copied to the xfer->data and this function returns with the
  651. * parameter @p receivedBytes set to 5. For the left 5 bytes, newly arrived data is
  652. * saved from the xfer->data[5]. When 5 bytes are received, the UART driver notifies the upper layer.
  653. * If the RX ring buffer is not enabled, this function enables the RX and RX interrupt
  654. * to receive data to the xfer->data. When all data is received, the upper layer is notified.
  655. *
  656. * @param base UART peripheral base address.
  657. * @param handle UART handle pointer.
  658. * @param xfer UART transfer structure, see #uart_transfer_t.
  659. * @param receivedBytes Bytes received from the ring buffer directly.
  660. * @retval kStatus_Success Successfully queue the transfer into transmit queue.
  661. * @retval kStatus_UART_RxBusy Previous receive request is not finished.
  662. * @retval kStatus_InvalidArgument Invalid argument.
  663. */
  664. status_t UART_TransferReceiveNonBlocking(UART_Type *base,
  665. uart_handle_t *handle,
  666. uart_transfer_t *xfer,
  667. size_t *receivedBytes);
  668. /*!
  669. * @brief Aborts the interrupt-driven data receiving.
  670. *
  671. * This function aborts the interrupt-driven data receiving. The user can get the remainBytes to know
  672. * how many bytes are not received yet.
  673. *
  674. * @param base UART peripheral base address.
  675. * @param handle UART handle pointer.
  676. */
  677. void UART_TransferAbortReceive(UART_Type *base, uart_handle_t *handle);
  678. /*!
  679. * @brief Gets the number of bytes that have been received.
  680. *
  681. * This function gets the number of bytes that have been received.
  682. *
  683. * @param base UART peripheral base address.
  684. * @param handle UART handle pointer.
  685. * @param count Receive bytes count.
  686. * @retval kStatus_NoTransferInProgress No receive in progress.
  687. * @retval kStatus_InvalidArgument Parameter is invalid.
  688. * @retval kStatus_Success Get successfully through the parameter \p count;
  689. */
  690. status_t UART_TransferGetReceiveCount(UART_Type *base, uart_handle_t *handle, uint32_t *count);
  691. /*!
  692. * @brief UART IRQ handle function.
  693. *
  694. * This function handles the UART transmit and receive IRQ request.
  695. *
  696. * @param base UART peripheral base address.
  697. * @param handle UART handle pointer.
  698. */
  699. void UART_TransferHandleIRQ(UART_Type *base, uart_handle_t *handle);
  700. /*!
  701. * @brief UART Error IRQ handle function.
  702. *
  703. * This function handles the UART error IRQ request.
  704. *
  705. * @param base UART peripheral base address.
  706. * @param handle UART handle pointer.
  707. */
  708. void UART_TransferHandleErrorIRQ(UART_Type *base, uart_handle_t *handle);
  709. /* @} */
  710. #if defined(__cplusplus)
  711. }
  712. #endif
  713. /*! @}*/
  714. #endif /* _FSL_UART_H_ */