fsl_uart.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /*
  2. * Copyright (c) 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.0.0. */
  43. #define FSL_UART_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
  44. /*@}*/
  45. /*! @brief Error codes for the UART driver. */
  46. enum _uart_status
  47. {
  48. kStatus_UART_TxBusy = MAKE_STATUS(kStatusGroup_IUART, 0), /*!< Transmitter is busy. */
  49. kStatus_UART_RxBusy = MAKE_STATUS(kStatusGroup_IUART, 1), /*!< Receiver is busy. */
  50. kStatus_UART_TxIdle = MAKE_STATUS(kStatusGroup_IUART, 2), /*!< UART transmitter is idle. */
  51. kStatus_UART_RxIdle = MAKE_STATUS(kStatusGroup_IUART, 3), /*!< UART receiver is idle. */
  52. kStatus_UART_TxWatermarkTooLarge = MAKE_STATUS(kStatusGroup_IUART, 4), /*!< TX FIFO watermark too large */
  53. kStatus_UART_RxWatermarkTooLarge = MAKE_STATUS(kStatusGroup_IUART, 5), /*!< RX FIFO watermark too large */
  54. kStatus_UART_FlagCannotClearManually =
  55. MAKE_STATUS(kStatusGroup_IUART, 6), /*!< UART flag can't be manually cleared. */
  56. kStatus_UART_Error = MAKE_STATUS(kStatusGroup_IUART, 7), /*!< Error happens on UART. */
  57. kStatus_UART_RxRingBufferOverrun = MAKE_STATUS(kStatusGroup_IUART, 8), /*!< UART RX software ring buffer overrun. */
  58. kStatus_UART_RxHardwareOverrun = MAKE_STATUS(kStatusGroup_IUART, 9), /*!< UART RX receiver overrun. */
  59. kStatus_UART_NoiseError = MAKE_STATUS(kStatusGroup_IUART, 10), /*!< UART noise error. */
  60. kStatus_UART_FramingError = MAKE_STATUS(kStatusGroup_IUART, 11), /*!< UART framing error. */
  61. kStatus_UART_ParityError = MAKE_STATUS(kStatusGroup_IUART, 12), /*!< UART parity error. */
  62. kStatus_UART_BaudrateNotSupport =
  63. MAKE_STATUS(kStatusGroup_IUART, 13), /*!< Baudrate is not support in current clock source */
  64. kStatus_UART_BreakDetect = MAKE_STATUS(kStatusGroup_IUART, 14), /*!< Receiver detect BREAK signal */
  65. };
  66. /*! @brief UART data bits count. */
  67. typedef enum _uart_data_bits
  68. {
  69. kUART_SevenDataBits = 0x0U, /*!< Seven data bit */
  70. kUART_EightDataBits = 0x1U, /*!< Eight data bit */
  71. } uart_data_bits_t;
  72. /*! @brief UART parity mode. */
  73. typedef enum _uart_parity_mode
  74. {
  75. kUART_ParityDisabled = 0x0U, /*!< Parity disabled */
  76. kUART_ParityEven = 0x2U, /*!< Even error check is selected */
  77. kUART_ParityOdd = 0x3U, /*!< Odd error check is selected */
  78. } uart_parity_mode_t;
  79. /*! @brief UART stop bit count. */
  80. typedef enum _uart_stop_bit_count
  81. {
  82. kUART_OneStopBit = 0x0U, /*!< One stop bit */
  83. kUART_TwoStopBit = 0x1U, /*!< Two stop bits */
  84. } uart_stop_bit_count_t;
  85. /*! @brief This structure contains the settings for all of the UART interrupt configurations. */
  86. enum _uart_interrupt_enable
  87. {
  88. kUART_AutoBaudEnable = 0x1U, /* !< Automatic baud rate detection Interrupt Enable. */
  89. kUART_TxReadyEnable = (0X1U << 1), /* !< transmitter ready Interrupt Enable. */
  90. kUART_IdleEnable = (0x1U << 2), /* !< IDLE Interrupt Enable. */
  91. kUART_RxReadyEnable = (0x1U << 3), /* !< Receiver Ready Interrupt Enable. */
  92. kUART_TxEmptyEnable = (0x1U << 4), /* !< Transmitter Empty Interrupt Enable. */
  93. kUART_RtsDeltaEnable = (0x1U << 5), /* !< RTS Delta Interrupt Enable. */
  94. kUART_EscapeEnable = (0x1U << 8), /* !< Escape Sequence Interrupt Enable. */
  95. kUART_RtsEnable = (0x1U << 9), /* !< Request to Send Interrupt Enable. */
  96. kUART_AgingTimerEnable = (0x1U << 10), /* !< Aging Timer Interrupt Enable. */
  97. kUART_DtrEnable = (0x1U << 12), /* !< Data Terminal Ready Interrupt Enable. */
  98. kUART_ParityErrorEnable = (0x1U << 13), /* !< Parity Error Interrupt Enable. */
  99. kUART_FrameErrorEnable = (0x1U << 14), /* !< Frame Error Interrupt Enable. */
  100. kUART_DcdEnable = (0x1U << 15), /* !< Data Carrier Detect Interrupt Enable. */
  101. kUART_RiEnable = (0x1U << 16), /* !< Ring Indicator Interrupt Enable. */
  102. kUART_RxDsEnable = (0x1U << 17), /* !< Receive Status Interrupt Enable. */
  103. kUART_tAirWakeEnable = (0x1U << 18), /* !< Asynchronous IR WAKE Interrupt Enable. */
  104. kUART_AwakeEnable = (0x1U << 19), /* !< Asynchronous WAKE Interrupt Enable. */
  105. kUART_DtrDeltaEnable = (0x1U << 20), /* !< Data Terminal Ready Delta Interrupt Enable. */
  106. kUART_AutoBaudCntEnable = (0x1U << 21), /* !< Auto-baud Counter Interrupt Enable. */
  107. kUART_IrEnable = (0X1U << 24), /* !< Serial Infrared Interrupt Enable. */
  108. kUART_WakeEnable = (0X1U << 25), /* !< WAKE Interrupt Enable. */
  109. kUART_TxCompleteEnable = (0X1U << 26), /* !< TransmitComplete Interrupt Enable. */
  110. kUART_BreakDetectEnable = (0X1U << 27), /* !< BREAK Condition Detected Interrupt Enable. */
  111. kUART_RxOverrunEnable = (0X1U << 28), /* !< Receiver Overrun Interrupt Enable. */
  112. kUART_RxDataReadyEnable = (0X1U << 29), /* !< Receive Data Ready Interrupt Enable. */
  113. kUART_AllInterruptsEnable = kUART_AutoBaudEnable | kUART_TxReadyEnable | kUART_IdleEnable | kUART_RxReadyEnable |
  114. kUART_TxEmptyEnable | kUART_RtsDeltaEnable | kUART_EscapeEnable | kUART_RtsEnable |
  115. kUART_AgingTimerEnable | kUART_DtrEnable | kUART_ParityErrorEnable |
  116. kUART_FrameErrorEnable | kUART_DcdEnable | kUART_RiEnable | kUART_RxDsEnable |
  117. kUART_tAirWakeEnable | kUART_AwakeEnable | kUART_DtrDeltaEnable |
  118. kUART_AutoBaudCntEnable | kUART_IrEnable | kUART_WakeEnable | kUART_TxCompleteEnable |
  119. kUART_BreakDetectEnable | kUART_RxOverrunEnable | kUART_RxDataReadyEnable,
  120. };
  121. /*!
  122. * @brief UART status flags.
  123. *
  124. * This provides constants for the UART status flags for use in the UART functions.
  125. */
  126. enum _uart_flags
  127. {
  128. kUART_RxCharReadyFlag = 0x0000000FU, /*!< Rx Character Ready Flag. */
  129. kUART_RxErrorFlag = 0x0000000EU, /*!< Rx Error Detect Flag. */
  130. kUART_RxOverrunErrorFlag = 0x0000000DU, /*!< Rx Overrun Flag. */
  131. kUART_RxFrameErrorFlag = 0x0000000CU, /*!< Rx Frame Error Flag. */
  132. kUART_RxBreakDetectFlag = 0x0000000BU, /*!< Rx Break Detect Flag. */
  133. kUART_RxParityErrorFlag = 0x0000000AU, /*!< Rx Parity Error Flag. */
  134. kUART_ParityErrorFlag = 0x0094000FU, /*!< Parity Error Interrupt Flag. */
  135. kUART_RtsStatusFlag = 0x0094000EU, /*!< RTS_B Pin Status Flag. */
  136. kUART_TxReadyFlag = 0x0094000DU, /*!< Transmitter Ready Interrupt/DMA Flag. */
  137. kUART_RtsDeltaFlag = 0x0094000CU, /*!< RTS Delta Flag. */
  138. kUART_EscapeFlag = 0x0094000BU, /*!< Escape Sequence Interrupt Flag. */
  139. kUART_FrameErrorFlag = 0x0094000AU, /*!< Frame Error Interrupt Flag. */
  140. kUART_RxReadyFlag = 0x00940009U, /*!< Receiver Ready Interrupt/DMA Flag. */
  141. kUART_AgingTimerFlag = 0x00940008U, /*!< Aging Timer Interrupt Flag. */
  142. kUART_DtrDeltaFlag = 0x00940007U, /*!< DTR Delta Flag. */
  143. kUART_RxDsFlag = 0x00940006U, /*!< Receiver IDLE Interrupt Flag. */
  144. kUART_tAirWakeFlag = 0x00940005U, /*!< Asynchronous IR WAKE Interrupt Flag. */
  145. kUART_AwakeFlag = 0x00940004U, /*!< Asynchronous WAKE Interrupt Flag. */
  146. kUART_Rs485SlaveAddrMatchFlag = 0x00940003U, /*!< RS-485 Slave Address Detected Interrupt Flag. */
  147. kUART_AutoBaudFlag = 0x0098000FU, /*!< Automatic Baud Rate Detect Complete Flag. */
  148. kUART_TxEmptyFlag = 0x0098000EU, /*!< Transmit Buffer FIFO Empty. */
  149. kUART_DtrFlag = 0x0098000DU, /*!< DTR edge triggered interrupt flag. */
  150. kUART_IdleFlag = 0x0098000CU, /*!< Idle Condition Flag. */
  151. kUART_AutoBaudCntStopFlag = 0x0098000BU, /*!< Auto-baud Counter Stopped Flag. */
  152. kUART_RiDeltaFlag = 0x0098000AU, /*!< Ring Indicator Delta Flag. */
  153. kUART_RiFlag = 0x00980009U, /*!< Ring Indicator Input Flag. */
  154. kUART_IrFlag = 0x00980008U, /*!< Serial Infrared Interrupt Flag. */
  155. kUART_WakeFlag = 0x00980007U, /*!< Wake Flag. */
  156. kUART_DcdDeltaFlag = 0x00980006U, /*!< Data Carrier Detect Delta Flag. */
  157. kUART_DcdFlag = 0x00980005U, /*!< Data Carrier Detect Input Flag. */
  158. kUART_RtsFlag = 0x00980004U, /*!< RTS Edge Triggered Interrupt Flag. */
  159. kUART_TxCompleteFlag = 0x00980003U, /*!< Transmitter Complete Flag. */
  160. kUART_BreakDetectFlag = 0x00980002U, /*!< BREAK Condition Detected Flag. */
  161. kUART_RxOverrunFlag = 0x00980001U, /*!< Overrun Error Flag. */
  162. kUART_RxDataReadyFlag = 0x00980000U, /*!< Receive Data Ready Flag. */
  163. };
  164. /*! @brief UART configuration structure. */
  165. typedef struct _uart_config
  166. {
  167. uint32_t baudRate_Bps; /*!< UART baud rate. */
  168. uart_parity_mode_t parityMode; /*!< Parity error check mode of this module. */
  169. uart_data_bits_t dataBitsCount; /*!< Data bits count, eight (default), seven */
  170. uart_stop_bit_count_t stopBitCount; /*!< Number of stop bits in one frame. */
  171. uint8_t txFifoWatermark; /*!< TX FIFO watermark */
  172. uint8_t rxFifoWatermark; /*!< RX FIFO watermark */
  173. bool enableAutoBaudRate; /*!< Enable automatic baud rate detection */
  174. bool enableTx; /*!< Enable TX */
  175. bool enableRx; /*!< Enable RX */
  176. } uart_config_t;
  177. /*! @brief UART transfer structure. */
  178. typedef struct _uart_transfer
  179. {
  180. uint8_t *data; /*!< The buffer of data to be transfer.*/
  181. size_t dataSize; /*!< The byte count to be transfer. */
  182. } uart_transfer_t;
  183. /* Forward declaration of the handle typedef. */
  184. typedef struct _uart_handle uart_handle_t;
  185. /*! @brief UART transfer callback function. */
  186. typedef void (*uart_transfer_callback_t)(UART_Type *base, uart_handle_t *handle, status_t status, void *userData);
  187. /*! @brief UART handle structure. */
  188. struct _uart_handle
  189. {
  190. uint8_t *volatile txData; /*!< Address of remaining data to send. */
  191. volatile size_t txDataSize; /*!< Size of the remaining data to send. */
  192. size_t txDataSizeAll; /*!< Size of the data to send out. */
  193. uint8_t *volatile rxData; /*!< Address of remaining data to receive. */
  194. volatile size_t rxDataSize; /*!< Size of the remaining data to receive. */
  195. size_t rxDataSizeAll; /*!< Size of the data to receive. */
  196. uint8_t *rxRingBuffer; /*!< Start address of the receiver ring buffer. */
  197. size_t rxRingBufferSize; /*!< Size of the ring buffer. */
  198. volatile uint16_t rxRingBufferHead; /*!< Index for the driver to store received data into ring buffer. */
  199. volatile uint16_t rxRingBufferTail; /*!< Index for the user to get data from the ring buffer. */
  200. uart_transfer_callback_t callback; /*!< Callback function. */
  201. void *userData; /*!< UART callback function parameter.*/
  202. volatile uint8_t txState; /*!< TX transfer state. */
  203. volatile uint8_t rxState; /*!< RX transfer state */
  204. };
  205. /*******************************************************************************
  206. * API
  207. ******************************************************************************/
  208. #if defined(__cplusplus)
  209. extern "C" {
  210. #endif
  211. /*!
  212. * @name Software Reset
  213. * @{
  214. */
  215. /*!
  216. * @brief Resets the UART using software.
  217. *
  218. * This function resets the transmit and receive state machines, all FIFOs and register
  219. * USR1, USR2, UBIR, UBMR, UBRC , URXD, UTXD and UTS[6-3]
  220. *
  221. * @param base UART peripheral base address.
  222. */
  223. static inline void UART_SoftwareReset(UART_Type *base)
  224. {
  225. base->UCR2 &= ~UART_UCR2_SRST_MASK;
  226. while ((base->UCR2 & UART_UCR2_SRST_MASK) == 0)
  227. {
  228. }
  229. }
  230. /* @} */
  231. /*!
  232. * @name Initialization and deinitialization
  233. * @{
  234. */
  235. /*!
  236. * @brief Initializes an UART instance with the user configuration structure and the peripheral clock.
  237. *
  238. * This function configures the UART module with user-defined settings. Call the UART_GetDefaultConfig() function
  239. * to configure the configuration structure and get the default configuration.
  240. * The example below shows how to use this API to configure the UART.
  241. * @code
  242. * uart_config_t uartConfig;
  243. * uartConfig.baudRate_Bps = 115200U;
  244. * uartConfig.parityMode = kUART_ParityDisabled;
  245. * uartConfig.dataBitsCount = kUART_EightDataBits;
  246. * uartConfig.stopBitCount = kUART_OneStopBit;
  247. * uartConfig.txFifoWatermark = 2;
  248. * uartConfig.rxFifoWatermark = 1;
  249. * uartConfig.enableAutoBaudrate = false;
  250. * uartConfig.enableTx = true;
  251. * uartConfig.enableRx = true;
  252. * UART_Init(UART1, &uartConfig, 24000000U);
  253. * @endcode
  254. *
  255. * @param base UART peripheral base address.
  256. * @param config Pointer to a user-defined configuration structure.
  257. * @param srcClock_Hz UART clock source frequency in HZ.
  258. * @retval kStatus_Success UART initialize succeed
  259. */
  260. status_t UART_Init(UART_Type *base, const uart_config_t *config, uint32_t srcClock_Hz);
  261. /*!
  262. * @brief Deinitializes a UART instance.
  263. *
  264. * This function waits for transmit to complete, disables TX and RX, and disables the UART clock.
  265. *
  266. * @param base UART peripheral base address.
  267. */
  268. void UART_Deinit(UART_Type *base);
  269. /*!l
  270. * @brief Gets the default configuration structure.
  271. *
  272. * This function initializes the UART configuration structure to a default value. The default
  273. * values are:
  274. * uartConfig->baudRate_Bps = 115200U;
  275. * uartConfig->parityMode = kUART_ParityDisabled;
  276. * uartConfig->dataBitsCount = kUART_EightDataBits;
  277. * uartConfig->stopBitCount = kUART_OneStopBit;
  278. * uartConfig->txFifoWatermark = 2;
  279. * uartConfig->rxFifoWatermark = 1;
  280. * uartConfig->enableAutoBaudrate = flase;
  281. * uartConfig->enableTx = false;
  282. * uartConfig->enableRx = false;
  283. *
  284. * @param config Pointer to a configuration structure.
  285. */
  286. void UART_GetDefaultConfig(uart_config_t *config);
  287. /*!
  288. * @brief Sets the UART instance baud rate.
  289. *
  290. * This function configures the UART module baud rate. This function is used to update
  291. * the UART module baud rate after the UART module is initialized by the UART_Init.
  292. * @code
  293. * UART_SetBaudRate(UART1, 115200U, 20000000U);
  294. * @endcode
  295. *
  296. * @param base UART peripheral base address.
  297. * @param baudRate_Bps UART baudrate to be set.
  298. * @param srcClock_Hz UART clock source freqency in Hz.
  299. * @retval kStatus_UART_BaudrateNotSupport Baudrate is not support in the current clock source.
  300. * @retval kStatus_Success Set baudrate succeeded.
  301. */
  302. status_t UART_SetBaudRate(UART_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz);
  303. /*!
  304. * @brief This function is used to Enable the UART Module.
  305. *
  306. * @param base UART base pointer.
  307. */
  308. static inline void UART_Enable(UART_Type *base)
  309. {
  310. base->UCR1 |= UART_UCR1_UARTEN_MASK;
  311. }
  312. /*!
  313. * @brief This function is used to Disable the UART Module.
  314. *
  315. * @param base UART base pointer.
  316. */
  317. static inline void UART_Disable(UART_Type *base)
  318. {
  319. base->UCR1 &= ~UART_UCR1_UARTEN_MASK;
  320. }
  321. /* @} */
  322. /*!
  323. * @name Status
  324. * @{
  325. */
  326. /*!
  327. * @brief This function is used to get the current status of specific
  328. * UART status flag(including interrupt flag). The available
  329. * status flag can be select from @ref uart_status_flag_t enumeration.
  330. *
  331. * @param base UART base pointer.
  332. * @param flag Status flag to check.
  333. * @retval current state of corresponding status flag.
  334. */
  335. bool UART_GetStatusFlag(UART_Type *base, uint32_t flag);
  336. /*!
  337. * @brief This function is used to clear the current status
  338. * of specific UART status flag. The available status
  339. * flag can be select from @ref uart_status_flag_t enumeration.
  340. *
  341. * @param base UART base pointer.
  342. * @param flag Status flag to clear.
  343. */
  344. void UART_ClearStatusFlag(UART_Type *base, uint32_t flag);
  345. /*@}*/
  346. /*!
  347. * @name Interrupts
  348. * @{
  349. */
  350. /*!
  351. * @brief Enables UART interrupts according to the provided mask.
  352. *
  353. * This function enables the UART interrupts according to the provided mask. The mask
  354. * is a logical OR of enumeration members. See @ref _uart_interrupt_enable.
  355. * For example, to enable TX empty interrupt and RX data ready interrupt, do the following.
  356. * @code
  357. * UART_EnableInterrupts(UART1,kUART_TxEmptyEnable | kUART_RxDataReadyEnable);
  358. * @endcode
  359. *
  360. * @param base UART peripheral base address.
  361. * @param mask The interrupts to enable. Logical OR of @ref _uart_interrupt_enable.
  362. */
  363. void UART_EnableInterrupts(UART_Type *base, uint32_t mask);
  364. /*!
  365. * @brief Disables the UART interrupts according to the provided mask.
  366. *
  367. * This function disables the UART interrupts according to the provided mask. The mask
  368. * is a logical OR of enumeration members. See @ref _uart_interrupt_enable.
  369. * For example, to disable TX empty interrupt and RX data ready interrupt do the following.
  370. * @code
  371. * UART_EnableInterrupts(UART1,kUART_TxEmptyEnable | kUART_RxDataReadyEnable);
  372. * @endcode
  373. *
  374. * @param base UART peripheral base address.
  375. * @param mask The interrupts to disable. Logical OR of @ref _uart_interrupt_enable.
  376. */
  377. void UART_DisableInterrupts(UART_Type *base, uint32_t mask);
  378. /*!
  379. * @brief Gets enabled UART interrupts.
  380. *
  381. * This function gets the enabled UART interrupts. The enabled interrupts are returned
  382. * as the logical OR value of the enumerators @ref _uart_interrupt_enable. To check
  383. * a specific interrupt enable status, compare the return value with enumerators
  384. * in @ref _uart_interrupt_enable.
  385. * For example, to check whether the TX empty interrupt is enabled:
  386. * @code
  387. * uint32_t enabledInterrupts = UART_GetEnabledInterrupts(UART1);
  388. *
  389. * if (kUART_TxEmptyEnable & enabledInterrupts)
  390. * {
  391. * ...
  392. * }
  393. * @endcode
  394. *
  395. * @param base UART peripheral base address.
  396. * @return UART interrupt flags which are logical OR of the enumerators in @ref _uart_interrupt_enable.
  397. */
  398. uint32_t UART_GetEnabledInterrupts(UART_Type *base);
  399. /* @} */
  400. /*!
  401. * @name Bus Operations
  402. * @{
  403. */
  404. /*!
  405. * @brief Enables or disables the UART transmitter.
  406. *
  407. * This function enables or disables the UART transmitter.
  408. *
  409. * @param base UART peripheral base address.
  410. * @param enable True to enable, false to disable.
  411. */
  412. static inline void UART_EnableTx(UART_Type *base, bool enable)
  413. {
  414. if (enable)
  415. {
  416. base->UCR2 |= UART_UCR2_TXEN_MASK;
  417. }
  418. else
  419. {
  420. base->UCR2 &= ~UART_UCR2_TXEN_MASK;
  421. }
  422. }
  423. /*!
  424. * @brief Enables or disables the UART receiver.
  425. *
  426. * This function enables or disables the UART receiver.
  427. *
  428. * @param base UART peripheral base address.
  429. * @param enable True to enable, false to disable.
  430. */
  431. static inline void UART_EnableRx(UART_Type *base, bool enable)
  432. {
  433. if (enable)
  434. {
  435. base->UCR2 |= UART_UCR2_RXEN_MASK;
  436. }
  437. else
  438. {
  439. base->UCR2 &= ~UART_UCR2_RXEN_MASK;
  440. }
  441. }
  442. /*!
  443. * @brief Writes to the transmitter register.
  444. *
  445. * This function is used to write data to transmitter register.
  446. * The upper layer must ensure that the TX register is empty or that
  447. * the TX FIFO has room before calling this function.
  448. *
  449. * @param base UART peripheral base address.
  450. * @param data Data write to the TX register.
  451. */
  452. static inline void UART_WriteByte(UART_Type *base, uint8_t data)
  453. {
  454. base->UTXD = data & UART_UTXD_TX_DATA_MASK;
  455. }
  456. /*!
  457. * @brief Reads the receiver register.
  458. *
  459. * This function is used to read data from receiver register.
  460. * The upper layer must ensure that the receiver register is full or that
  461. * the RX FIFO has data before calling this function.
  462. *
  463. * @param base UART peripheral base address.
  464. * @return Data read from data register.
  465. */
  466. static inline uint8_t UART_ReadByte(UART_Type *base)
  467. {
  468. return (uint8_t)((base->URXD & UART_URXD_RX_DATA_MASK) >> UART_URXD_RX_DATA_SHIFT);
  469. }
  470. /*!
  471. * @brief Writes to the TX register using a blocking method.
  472. *
  473. * This function polls the TX register, waits for the TX register to be empty or for the TX FIFO
  474. * to have room and writes data to the TX buffer.
  475. *
  476. * @note This function does not check whether all data is sent out to the bus.
  477. * Before disabling the TX, check kUART_TransmissionCompleteFlag to ensure that the TX is
  478. * finished.
  479. *
  480. * @param base UART peripheral base address.
  481. * @param data Start address of the data to write.
  482. * @param length Size of the data to write.
  483. */
  484. void UART_WriteBlocking(UART_Type *base, const uint8_t *data, size_t length);
  485. /*!
  486. * @brief Read RX data register using a blocking method.
  487. *
  488. * This function polls the RX register, waits for the RX register to be full or for RX FIFO to
  489. * have data, and reads data from the TX register.
  490. *
  491. * @param base UART peripheral base address.
  492. * @param data Start address of the buffer to store the received data.
  493. * @param length Size of the buffer.
  494. * @retval kStatus_UART_RxHardwareOverrun Receiver overrun occurred while receiving data.
  495. * @retval kStatus_UART_NoiseError A noise error occurred while receiving data.
  496. * @retval kStatus_UART_FramingError A framing error occurred while receiving data.
  497. * @retval kStatus_UART_ParityError A parity error occurred while receiving data.
  498. * @retval kStatus_Success Successfully received all data.
  499. */
  500. status_t UART_ReadBlocking(UART_Type *base, uint8_t *data, size_t length);
  501. /* @} */
  502. /*!
  503. * @name Transactional
  504. * @{
  505. */
  506. /*!
  507. * @brief Initializes the UART handle.
  508. *
  509. * This function initializes the UART handle which can be used for other UART
  510. * transactional APIs. Usually, for a specified UART instance,
  511. * call this API once to get the initialized handle.
  512. *
  513. * @param base UART peripheral base address.
  514. * @param handle UART handle pointer.
  515. * @param callback The callback function.
  516. * @param userData The parameter of the callback function.
  517. */
  518. void UART_TransferCreateHandle(UART_Type *base,
  519. uart_handle_t *handle,
  520. uart_transfer_callback_t callback,
  521. void *userData);
  522. /*!
  523. * @brief Sets up the RX ring buffer.
  524. *
  525. * This function sets up the RX ring buffer to a specific UART handle.
  526. *
  527. * When the RX ring buffer is used, data received are stored into the ring buffer even when the
  528. * user doesn't call the UART_TransferReceiveNonBlocking() API. If data is already received
  529. * in the ring buffer, the user can get the received data from the ring buffer directly.
  530. *
  531. * @note When using the RX ring buffer, one byte is reserved for internal use. In other
  532. * words, if @p ringBufferSize is 32, only 31 bytes are used for saving data.
  533. *
  534. * @param base UART peripheral base address.
  535. * @param handle UART handle pointer.
  536. * @param ringBuffer Start address of the ring buffer for background receiving. Pass NULL to disable the ring buffer.
  537. * @param ringBufferSize Size of the ring buffer.
  538. */
  539. void UART_TransferStartRingBuffer(UART_Type *base, uart_handle_t *handle, uint8_t *ringBuffer, size_t ringBufferSize);
  540. /*!
  541. * @brief Aborts the background transfer and uninstalls the ring buffer.
  542. *
  543. * This function aborts the background transfer and uninstalls the ring buffer.
  544. *
  545. * @param base UART peripheral base address.
  546. * @param handle UART handle pointer.
  547. */
  548. void UART_TransferStopRingBuffer(UART_Type *base, uart_handle_t *handle);
  549. /*!
  550. * @brief Get the length of received data in RX ring buffer.
  551. *
  552. * @param handle UART handle pointer.
  553. * @return Length of received data in RX ring buffer.
  554. */
  555. size_t UART_TransferGetRxRingBufferLength(uart_handle_t *handle);
  556. /*!
  557. * @brief Transmits a buffer of data using the interrupt method.
  558. *
  559. * This function sends data using an interrupt method. This is a non-blocking function, which
  560. * returns directly without waiting for all data to be written to the TX register. When
  561. * all data is written to the TX register in the ISR, the UART driver calls the callback
  562. * function and passes the @ref kStatus_UART_TxIdle as status parameter.
  563. *
  564. * @note The kStatus_UART_TxIdle is passed to the upper layer when all data is written
  565. * to the TX register. However, it does not ensure that all data is sent out. Before disabling the TX,
  566. * check the kUART_TransmissionCompleteFlag to ensure that the TX is finished.
  567. *
  568. * @param base UART peripheral base address.
  569. * @param handle UART handle pointer.
  570. * @param xfer UART transfer structure. See #uart_transfer_t.
  571. * @retval kStatus_Success Successfully start the data transmission.
  572. * @retval kStatus_UART_TxBusy Previous transmission still not finished; data not all written to TX register yet.
  573. * @retval kStatus_InvalidArgument Invalid argument.
  574. */
  575. status_t UART_TransferSendNonBlocking(UART_Type *base, uart_handle_t *handle, uart_transfer_t *xfer);
  576. /*!
  577. * @brief Aborts the interrupt-driven data transmit.
  578. *
  579. * This function aborts the interrupt-driven data sending. The user can get the remainBytes to find out
  580. * how many bytes are not sent out.
  581. *
  582. * @param base UART peripheral base address.
  583. * @param handle UART handle pointer.
  584. */
  585. void UART_TransferAbortSend(UART_Type *base, uart_handle_t *handle);
  586. /*!
  587. * @brief Gets the number of bytes written to the UART TX register.
  588. *
  589. * This function gets the number of bytes written to the UART TX
  590. * register by using the interrupt method.
  591. *
  592. * @param base UART peripheral base address.
  593. * @param handle UART handle pointer.
  594. * @param count Send bytes count.
  595. * @retval kStatus_NoTransferInProgress No send in progress.
  596. * @retval kStatus_InvalidArgument The parameter is invalid.
  597. * @retval kStatus_Success Get successfully through the parameter \p count;
  598. */
  599. status_t UART_TransferGetSendCount(UART_Type *base, uart_handle_t *handle, uint32_t *count);
  600. /*!
  601. * @brief Receives a buffer of data using an interrupt method.
  602. *
  603. * This function receives data using an interrupt method. This is a non-blocking function, which
  604. * returns without waiting for all data to be received.
  605. * If the RX ring buffer is used and not empty, the data in the ring buffer is copied and
  606. * the parameter @p receivedBytes shows how many bytes are copied from the ring buffer.
  607. * After copying, if the data in the ring buffer is not enough to read, the receive
  608. * request is saved by the UART driver. When the new data arrives, the receive request
  609. * is serviced first. When all data is received, the UART driver notifies the upper layer
  610. * through a callback function and passes the status parameter @ref kStatus_UART_RxIdle.
  611. * For example, the upper layer needs 10 bytes but there are only 5 bytes in the ring buffer.
  612. * The 5 bytes are copied to the xfer->data and this function returns with the
  613. * parameter @p receivedBytes set to 5. For the left 5 bytes, newly arrived data is
  614. * saved from the xfer->data[5]. When 5 bytes are received, the UART driver notifies the upper layer.
  615. * If the RX ring buffer is not enabled, this function enables the RX and RX interrupt
  616. * to receive data to the xfer->data. When all data is received, the upper layer is notified.
  617. *
  618. * @param base UART peripheral base address.
  619. * @param handle UART handle pointer.
  620. * @param xfer UART transfer structure, see #uart_transfer_t.
  621. * @param receivedBytes Bytes received from the ring buffer directly.
  622. * @retval kStatus_Success Successfully queue the transfer into transmit queue.
  623. * @retval kStatus_UART_RxBusy Previous receive request is not finished.
  624. * @retval kStatus_InvalidArgument Invalid argument.
  625. */
  626. status_t UART_TransferReceiveNonBlocking(UART_Type *base,
  627. uart_handle_t *handle,
  628. uart_transfer_t *xfer,
  629. size_t *receivedBytes);
  630. /*!
  631. * @brief Aborts the interrupt-driven data receiving.
  632. *
  633. * This function aborts the interrupt-driven data receiving. The user can get the remainBytes to know
  634. * how many bytes are not received yet.
  635. *
  636. * @param base UART peripheral base address.
  637. * @param handle UART handle pointer.
  638. */
  639. void UART_TransferAbortReceive(UART_Type *base, uart_handle_t *handle);
  640. /*!
  641. * @brief Gets the number of bytes that have been received.
  642. *
  643. * This function gets the number of bytes that have been received.
  644. *
  645. * @param base UART peripheral base address.
  646. * @param handle UART handle pointer.
  647. * @param count Receive bytes count.
  648. * @retval kStatus_NoTransferInProgress No receive in progress.
  649. * @retval kStatus_InvalidArgument Parameter is invalid.
  650. * @retval kStatus_Success Get successfully through the parameter \p count;
  651. */
  652. status_t UART_TransferGetReceiveCount(UART_Type *base, uart_handle_t *handle, uint32_t *count);
  653. /*!
  654. * @brief UART IRQ handle function.
  655. *
  656. * This function handles the UART transmit and receive IRQ request.
  657. *
  658. * @param base UART peripheral base address.
  659. * @param handle UART handle pointer.
  660. */
  661. void UART_TransferHandleIRQ(UART_Type *base, uart_handle_t *handle);
  662. /*@}*/
  663. /*!
  664. * @name DMA control functions.
  665. * @{
  666. */
  667. /*!
  668. * @brief Enables or disables the UART transmitter DMA request.
  669. *
  670. * This function enables or disables the transmit request when the transmitter
  671. * has one or more slots available in the TxFIFO. The fill level in the TxFIFO
  672. * that generates the DMA request is controlled by the TXTL bits.
  673. *
  674. * @param base UART peripheral base address.
  675. * @param enable True to enable, false to disable.
  676. */
  677. static inline void UART_EnableTxDMA(UART_Type *base, bool enable)
  678. {
  679. assert(base);
  680. if (enable)
  681. {
  682. base->UCR1 |= UART_UCR1_TXDMAEN_MASK;
  683. }
  684. else
  685. {
  686. base->UCR1 &= ~UART_UCR1_TXDMAEN_MASK;
  687. }
  688. }
  689. /*!
  690. * @brief Enables or disables the UART receiver DMA request.
  691. *
  692. * This function enables or disables the receive request when the receiver
  693. * has data in the RxFIFO. The fill level in the RxFIFO at which a DMA request
  694. * is generated is controlled by the RXTL bits .
  695. *
  696. * @param base UART peripheral base address.
  697. * @param enable True to enable, false to disable.
  698. */
  699. static inline void UART_EnableRxDMA(UART_Type *base, bool enable)
  700. {
  701. assert(base);
  702. if (enable)
  703. {
  704. base->UCR1 |= UART_UCR1_RXDMAEN_MASK;
  705. }
  706. else
  707. {
  708. base->UCR1 &= ~UART_UCR1_RXDMAEN_MASK;
  709. }
  710. }
  711. /*@}*/
  712. /*!
  713. * @name FIFO control functions.
  714. * @{
  715. */
  716. /*!
  717. * @brief This function is used to set the watermark of UART Tx FIFO.
  718. * A maskable interrupt is generated whenever the data level in
  719. * the TxFIFO falls below the Tx FIFO watermark.
  720. *
  721. * @param base UART base pointer.
  722. * @param watermark The Tx FIFO watermark.
  723. */
  724. static inline void UART_SetTxFifoWatermark(UART_Type *base, uint8_t watermark)
  725. {
  726. assert((watermark >= 2) && (watermark <= FSL_FEATURE_IUART_FIFO_SIZEn(base)));
  727. base->UFCR = (base->UFCR & ~UART_UFCR_TXTL_MASK) | UART_UFCR_TXTL(watermark);
  728. }
  729. /*!
  730. * @brief This function is used to set the watermark of UART Rx FIFO.
  731. * A maskable interrupt is generated whenever the data level in
  732. * the RxFIFO reaches the Rx FIFO watermark.
  733. *
  734. * @param base UART base pointer.
  735. * @param watermark The Rx FIFO watermark.
  736. */
  737. static inline void UART_SetRxFifoWatermark(UART_Type *base, uint8_t watermark)
  738. {
  739. assert(watermark <= FSL_FEATURE_IUART_FIFO_SIZEn(base));
  740. base->UFCR = (base->UFCR & ~UART_UFCR_RXTL_MASK) | UART_UFCR_RXTL(watermark);
  741. }
  742. /*@}*/
  743. /*!
  744. * @name Auto baud rate detection.
  745. * @{
  746. */
  747. /*!
  748. * @brief This function is used to set the enable condition of
  749. * Automatic Baud Rate Detection feature.
  750. *
  751. * @param base UART base pointer.
  752. * @param enable Enable/Disable Automatic Baud Rate Detection feature.
  753. * - true: Enable Automatic Baud Rate Detection feature.
  754. * - false: Disable Automatic Baud Rate Detection feature.
  755. */
  756. static inline void UART_EnableAutoBaudRate(UART_Type *base, bool enable)
  757. {
  758. if (enable)
  759. {
  760. /* When ADET=0 and ADBR=1, automatic baud rate detection starts */
  761. /* Enable automatic baud rate detection */
  762. base->UCR1 |= UART_UCR1_ADBR_MASK;
  763. /* Clear ADET brfore start automatic baud rate detection*/
  764. base->USR2 |= UART_USR2_ADET_MASK;
  765. }
  766. else
  767. {
  768. /* Disable automatic baud rate detection */
  769. base->UCR1 &= ~UART_UCR1_ADBR_MASK;
  770. }
  771. }
  772. /*!
  773. * @brief This function is used to read if the automatic baud rate detection
  774. * has finished.
  775. *
  776. * @param base UART base pointer.
  777. * @return - true: Automatic baud rate detection has finished.
  778. * - false: Automatic baud rate detection has not finished.
  779. */
  780. static inline bool UART_IsAutoBaudRateComplete(UART_Type *base)
  781. {
  782. if (UART_USR2_ACST_MASK & base->USR2)
  783. {
  784. base->USR2 |= UART_USR2_ACST_MASK;
  785. return true;
  786. }
  787. else
  788. {
  789. return false;
  790. }
  791. }
  792. #ifdef __cplusplus
  793. }
  794. #endif
  795. /*! @}*/
  796. #endif /* _FSL_UART_H_ */