fsl_uart.h 32 KB

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