fsl_usart.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2018 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef _FSL_USART_H_
  9. #define _FSL_USART_H_
  10. #include "fsl_common.h"
  11. /*!
  12. * @addtogroup usart_driver
  13. * @{
  14. */
  15. /*******************************************************************************
  16. * Definitions
  17. ******************************************************************************/
  18. /*! @name Driver version */
  19. /*@{*/
  20. /*! @brief USART driver version 2.0.3. */
  21. #define FSL_USART_DRIVER_VERSION (MAKE_VERSION(2, 0, 3))
  22. /*@}*/
  23. #define USART_FIFOTRIG_TXLVL_GET(base) (((base)->FIFOTRIG & USART_FIFOTRIG_TXLVL_MASK) >> USART_FIFOTRIG_TXLVL_SHIFT)
  24. #define USART_FIFOTRIG_RXLVL_GET(base) (((base)->FIFOTRIG & USART_FIFOTRIG_RXLVL_MASK) >> USART_FIFOTRIG_RXLVL_SHIFT)
  25. /*! @brief Error codes for the USART driver. */
  26. enum _usart_status
  27. {
  28. kStatus_USART_TxBusy = MAKE_STATUS(kStatusGroup_LPC_USART, 0), /*!< Transmitter is busy. */
  29. kStatus_USART_RxBusy = MAKE_STATUS(kStatusGroup_LPC_USART, 1), /*!< Receiver is busy. */
  30. kStatus_USART_TxIdle = MAKE_STATUS(kStatusGroup_LPC_USART, 2), /*!< USART transmitter is idle. */
  31. kStatus_USART_RxIdle = MAKE_STATUS(kStatusGroup_LPC_USART, 3), /*!< USART receiver is idle. */
  32. kStatus_USART_TxError = MAKE_STATUS(kStatusGroup_LPC_USART, 7), /*!< Error happens on txFIFO. */
  33. kStatus_USART_RxError = MAKE_STATUS(kStatusGroup_LPC_USART, 9), /*!< Error happens on rxFIFO. */
  34. kStatus_USART_RxRingBufferOverrun = MAKE_STATUS(kStatusGroup_LPC_USART, 8), /*!< Error happens on rx ring buffer */
  35. kStatus_USART_NoiseError = MAKE_STATUS(kStatusGroup_LPC_USART, 10), /*!< USART noise error. */
  36. kStatus_USART_FramingError = MAKE_STATUS(kStatusGroup_LPC_USART, 11), /*!< USART framing error. */
  37. kStatus_USART_ParityError = MAKE_STATUS(kStatusGroup_LPC_USART, 12), /*!< USART parity error. */
  38. kStatus_USART_BaudrateNotSupport =
  39. MAKE_STATUS(kStatusGroup_LPC_USART, 13), /*!< Baudrate is not support in current clock source */
  40. };
  41. /*! @brief USART parity mode. */
  42. typedef enum _usart_parity_mode
  43. {
  44. kUSART_ParityDisabled = 0x0U, /*!< Parity disabled */
  45. kUSART_ParityEven = 0x2U, /*!< Parity enabled, type even, bit setting: PE|PT = 10 */
  46. kUSART_ParityOdd = 0x3U, /*!< Parity enabled, type odd, bit setting: PE|PT = 11 */
  47. } usart_parity_mode_t;
  48. /*! @brief USART stop bit count. */
  49. typedef enum _usart_stop_bit_count
  50. {
  51. kUSART_OneStopBit = 0U, /*!< One stop bit */
  52. kUSART_TwoStopBit = 1U, /*!< Two stop bits */
  53. } usart_stop_bit_count_t;
  54. /*! @brief USART data size. */
  55. typedef enum _usart_data_len
  56. {
  57. kUSART_7BitsPerChar = 0U, /*!< Seven bit mode */
  58. kUSART_8BitsPerChar = 1U, /*!< Eight bit mode */
  59. } usart_data_len_t;
  60. /*! @brief txFIFO watermark values */
  61. typedef enum _usart_txfifo_watermark
  62. {
  63. kUSART_TxFifo0 = 0, /*!< USART tx watermark is empty */
  64. kUSART_TxFifo1 = 1, /*!< USART tx watermark at 1 item */
  65. kUSART_TxFifo2 = 2, /*!< USART tx watermark at 2 items */
  66. kUSART_TxFifo3 = 3, /*!< USART tx watermark at 3 items */
  67. kUSART_TxFifo4 = 4, /*!< USART tx watermark at 4 items */
  68. kUSART_TxFifo5 = 5, /*!< USART tx watermark at 5 items */
  69. kUSART_TxFifo6 = 6, /*!< USART tx watermark at 6 items */
  70. kUSART_TxFifo7 = 7, /*!< USART tx watermark at 7 items */
  71. } usart_txfifo_watermark_t;
  72. /*! @brief rxFIFO watermark values */
  73. typedef enum _usart_rxfifo_watermark
  74. {
  75. kUSART_RxFifo1 = 0, /*!< USART rx watermark at 1 item */
  76. kUSART_RxFifo2 = 1, /*!< USART rx watermark at 2 items */
  77. kUSART_RxFifo3 = 2, /*!< USART rx watermark at 3 items */
  78. kUSART_RxFifo4 = 3, /*!< USART rx watermark at 4 items */
  79. kUSART_RxFifo5 = 4, /*!< USART rx watermark at 5 items */
  80. kUSART_RxFifo6 = 5, /*!< USART rx watermark at 6 items */
  81. kUSART_RxFifo7 = 6, /*!< USART rx watermark at 7 items */
  82. kUSART_RxFifo8 = 7, /*!< USART rx watermark at 8 items */
  83. } usart_rxfifo_watermark_t;
  84. /*!
  85. * @brief USART interrupt configuration structure, default settings all disabled.
  86. */
  87. enum _usart_interrupt_enable
  88. {
  89. kUSART_TxErrorInterruptEnable = (USART_FIFOINTENSET_TXERR_MASK),
  90. kUSART_RxErrorInterruptEnable = (USART_FIFOINTENSET_RXERR_MASK),
  91. kUSART_TxLevelInterruptEnable = (USART_FIFOINTENSET_TXLVL_MASK),
  92. kUSART_RxLevelInterruptEnable = (USART_FIFOINTENSET_RXLVL_MASK),
  93. };
  94. /*!
  95. * @brief USART status flags.
  96. *
  97. * This provides constants for the USART status flags for use in the USART functions.
  98. */
  99. enum _usart_flags
  100. {
  101. kUSART_TxError = (USART_FIFOSTAT_TXERR_MASK), /*!< TEERR bit, sets if TX buffer is error */
  102. kUSART_RxError = (USART_FIFOSTAT_RXERR_MASK), /*!< RXERR bit, sets if RX buffer is error */
  103. kUSART_TxFifoEmptyFlag = (USART_FIFOSTAT_TXEMPTY_MASK), /*!< TXEMPTY bit, sets if TX buffer is empty */
  104. kUSART_TxFifoNotFullFlag = (USART_FIFOSTAT_TXNOTFULL_MASK), /*!< TXNOTFULL bit, sets if TX buffer is not full */
  105. kUSART_RxFifoNotEmptyFlag = (USART_FIFOSTAT_RXNOTEMPTY_MASK), /*!< RXNOEMPTY bit, sets if RX buffer is not empty */
  106. kUSART_RxFifoFullFlag = (USART_FIFOSTAT_RXFULL_MASK), /*!< RXFULL bit, sets if RX buffer is full */
  107. };
  108. /*! @brief USART configuration structure. */
  109. typedef struct _usart_config
  110. {
  111. uint32_t baudRate_Bps; /*!< USART baud rate */
  112. usart_parity_mode_t parityMode; /*!< Parity mode, disabled (default), even, odd */
  113. usart_stop_bit_count_t stopBitCount; /*!< Number of stop bits, 1 stop bit (default) or 2 stop bits */
  114. usart_data_len_t bitCountPerChar; /*!< Data length - 7 bit, 8 bit */
  115. bool loopback; /*!< Enable peripheral loopback */
  116. bool enableRx; /*!< Enable RX */
  117. bool enableTx; /*!< Enable TX */
  118. usart_txfifo_watermark_t txWatermark; /*!< txFIFO watermark */
  119. usart_rxfifo_watermark_t rxWatermark; /*!< rxFIFO watermark */
  120. } usart_config_t;
  121. /*! @brief USART transfer structure. */
  122. typedef struct _usart_transfer
  123. {
  124. uint8_t *data; /*!< The buffer of data to be transfer.*/
  125. size_t dataSize; /*!< The byte count to be transfer. */
  126. } usart_transfer_t;
  127. /* Forward declaration of the handle typedef. */
  128. typedef struct _usart_handle usart_handle_t;
  129. /*! @brief USART transfer callback function. */
  130. typedef void (*usart_transfer_callback_t)(USART_Type *base, usart_handle_t *handle, status_t status, void *userData);
  131. /*! @brief USART handle structure. */
  132. struct _usart_handle
  133. {
  134. uint8_t *volatile txData; /*!< Address of remaining data to send. */
  135. volatile size_t txDataSize; /*!< Size of the remaining data to send. */
  136. size_t txDataSizeAll; /*!< Size of the data to send out. */
  137. uint8_t *volatile rxData; /*!< Address of remaining data to receive. */
  138. volatile size_t rxDataSize; /*!< Size of the remaining data to receive. */
  139. size_t rxDataSizeAll; /*!< Size of the data to receive. */
  140. uint8_t *rxRingBuffer; /*!< Start address of the receiver ring buffer. */
  141. size_t rxRingBufferSize; /*!< Size of the ring buffer. */
  142. volatile uint16_t rxRingBufferHead; /*!< Index for the driver to store received data into ring buffer. */
  143. volatile uint16_t rxRingBufferTail; /*!< Index for the user to get data from the ring buffer. */
  144. usart_transfer_callback_t callback; /*!< Callback function. */
  145. void *userData; /*!< USART callback function parameter.*/
  146. volatile uint8_t txState; /*!< TX transfer state. */
  147. volatile uint8_t rxState; /*!< RX transfer state */
  148. usart_txfifo_watermark_t txWatermark; /*!< txFIFO watermark */
  149. usart_rxfifo_watermark_t rxWatermark; /*!< rxFIFO watermark */
  150. };
  151. /*******************************************************************************
  152. * API
  153. ******************************************************************************/
  154. #if defined(__cplusplus)
  155. extern "C" {
  156. #endif /* _cplusplus */
  157. /*! @brief Returns instance number for USART peripheral base address. */
  158. uint32_t USART_GetInstance(USART_Type *base);
  159. /*!
  160. * @name Initialization and deinitialization
  161. * @{
  162. */
  163. /*!
  164. * @brief Initializes a USART instance with user configuration structure and peripheral clock.
  165. *
  166. * This function configures the USART module with the user-defined settings. The user can configure the configuration
  167. * structure and also get the default configuration by using the USART_GetDefaultConfig() function.
  168. * Example below shows how to use this API to configure USART.
  169. * @code
  170. * usart_config_t usartConfig;
  171. * usartConfig.baudRate_Bps = 115200U;
  172. * usartConfig.parityMode = kUSART_ParityDisabled;
  173. * usartConfig.stopBitCount = kUSART_OneStopBit;
  174. * USART_Init(USART1, &usartConfig, 20000000U);
  175. * @endcode
  176. *
  177. * @param base USART peripheral base address.
  178. * @param config Pointer to user-defined configuration structure.
  179. * @param srcClock_Hz USART clock source frequency in HZ.
  180. * @retval kStatus_USART_BaudrateNotSupport Baudrate is not support in current clock source.
  181. * @retval kStatus_InvalidArgument USART base address is not valid
  182. * @retval kStatus_Success Status USART initialize succeed
  183. */
  184. status_t USART_Init(USART_Type *base, const usart_config_t *config, uint32_t srcClock_Hz);
  185. /*!
  186. * @brief Deinitializes a USART instance.
  187. *
  188. * This function waits for TX complete, disables TX and RX, and disables the USART clock.
  189. *
  190. * @param base USART peripheral base address.
  191. */
  192. void USART_Deinit(USART_Type *base);
  193. /*!
  194. * @brief Gets the default configuration structure.
  195. *
  196. * This function initializes the USART configuration structure to a default value. The default
  197. * values are:
  198. * usartConfig->baudRate_Bps = 115200U;
  199. * usartConfig->parityMode = kUSART_ParityDisabled;
  200. * usartConfig->stopBitCount = kUSART_OneStopBit;
  201. * usartConfig->bitCountPerChar = kUSART_8BitsPerChar;
  202. * usartConfig->loopback = false;
  203. * usartConfig->enableTx = false;
  204. * usartConfig->enableRx = false;
  205. *
  206. * @param config Pointer to configuration structure.
  207. */
  208. void USART_GetDefaultConfig(usart_config_t *config);
  209. /*!
  210. * @brief Sets the USART instance baud rate.
  211. *
  212. * This function configures the USART module baud rate. This function is used to update
  213. * the USART module baud rate after the USART module is initialized by the USART_Init.
  214. * @code
  215. * USART_SetBaudRate(USART1, 115200U, 20000000U);
  216. * @endcode
  217. *
  218. * @param base USART peripheral base address.
  219. * @param baudrate_Bps USART baudrate to be set.
  220. * @param srcClock_Hz USART clock source freqency in HZ.
  221. * @retval kStatus_USART_BaudrateNotSupport Baudrate is not support in current clock source.
  222. * @retval kStatus_Success Set baudrate succeed.
  223. * @retval kStatus_InvalidArgument One or more arguments are invalid.
  224. */
  225. status_t USART_SetBaudRate(USART_Type *base, uint32_t baudrate_Bps, uint32_t srcClock_Hz);
  226. /* @} */
  227. /*!
  228. * @name Status
  229. * @{
  230. */
  231. /*!
  232. * @brief Get USART status flags.
  233. *
  234. * This function get all USART status flags, the flags are returned as the logical
  235. * OR value of the enumerators @ref _usart_flags. To check a specific status,
  236. * compare the return value with enumerators in @ref _usart_flags.
  237. * For example, to check whether the TX is empty:
  238. * @code
  239. * if (kUSART_TxFifoNotFullFlag & USART_GetStatusFlags(USART1))
  240. * {
  241. * ...
  242. * }
  243. * @endcode
  244. *
  245. * @param base USART peripheral base address.
  246. * @return USART status flags which are ORed by the enumerators in the _usart_flags.
  247. */
  248. static inline uint32_t USART_GetStatusFlags(USART_Type *base)
  249. {
  250. return base->FIFOSTAT;
  251. }
  252. /*!
  253. * @brief Clear USART status flags.
  254. *
  255. * This function clear supported USART status flags
  256. * Flags that can be cleared or set are:
  257. * kUSART_TxError
  258. * kUSART_RxError
  259. * For example:
  260. * @code
  261. * USART_ClearStatusFlags(USART1, kUSART_TxError | kUSART_RxError)
  262. * @endcode
  263. *
  264. * @param base USART peripheral base address.
  265. * @param mask status flags to be cleared.
  266. */
  267. static inline void USART_ClearStatusFlags(USART_Type *base, uint32_t mask)
  268. {
  269. /* Only TXERR, RXERR fields support write. Remaining fields should be set to zero */
  270. base->FIFOSTAT = mask & (USART_FIFOSTAT_TXERR_MASK | USART_FIFOSTAT_RXERR_MASK);
  271. }
  272. /* @} */
  273. /*!
  274. * @name Interrupts
  275. * @{
  276. */
  277. /*!
  278. * @brief Enables USART interrupts according to the provided mask.
  279. *
  280. * This function enables the USART interrupts according to the provided mask. The mask
  281. * is a logical OR of enumeration members. See @ref _usart_interrupt_enable.
  282. * For example, to enable TX empty interrupt and RX full interrupt:
  283. * @code
  284. * USART_EnableInterrupts(USART1, kUSART_TxLevelInterruptEnable | kUSART_RxLevelInterruptEnable);
  285. * @endcode
  286. *
  287. * @param base USART peripheral base address.
  288. * @param mask The interrupts to enable. Logical OR of @ref _usart_interrupt_enable.
  289. */
  290. static inline void USART_EnableInterrupts(USART_Type *base, uint32_t mask)
  291. {
  292. base->FIFOINTENSET = mask & 0xF;
  293. }
  294. /*!
  295. * @brief Disables USART interrupts according to a provided mask.
  296. *
  297. * This function disables the USART interrupts according to a provided mask. The mask
  298. * is a logical OR of enumeration members. See @ref _usart_interrupt_enable.
  299. * This example shows how to disable the TX empty interrupt and RX full interrupt:
  300. * @code
  301. * USART_DisableInterrupts(USART1, kUSART_TxLevelInterruptEnable | kUSART_RxLevelInterruptEnable);
  302. * @endcode
  303. *
  304. * @param base USART peripheral base address.
  305. * @param mask The interrupts to disable. Logical OR of @ref _usart_interrupt_enable.
  306. */
  307. static inline void USART_DisableInterrupts(USART_Type *base, uint32_t mask)
  308. {
  309. base->FIFOINTENCLR = mask & 0xF;
  310. }
  311. /*!
  312. * @brief Returns enabled USART interrupts.
  313. *
  314. * This function returns the enabled USART interrupts.
  315. *
  316. * @param base USART peripheral base address.
  317. */
  318. static inline uint32_t USART_GetEnabledInterrupts(USART_Type *base)
  319. {
  320. return base->FIFOINTENSET;
  321. }
  322. /*!
  323. * @brief Enable DMA for Tx
  324. */
  325. static inline void USART_EnableTxDMA(USART_Type *base, bool enable)
  326. {
  327. if (enable)
  328. {
  329. base->FIFOCFG |= USART_FIFOCFG_DMATX_MASK;
  330. }
  331. else
  332. {
  333. base->FIFOCFG &= ~(USART_FIFOCFG_DMATX_MASK);
  334. }
  335. }
  336. /*!
  337. * @brief Enable DMA for Rx
  338. */
  339. static inline void USART_EnableRxDMA(USART_Type *base, bool enable)
  340. {
  341. if (enable)
  342. {
  343. base->FIFOCFG |= USART_FIFOCFG_DMARX_MASK;
  344. }
  345. else
  346. {
  347. base->FIFOCFG &= ~(USART_FIFOCFG_DMARX_MASK);
  348. }
  349. }
  350. /*!
  351. * @brief Enable CTS.
  352. * This function will determine whether CTS is used for flow control.
  353. *
  354. * @param base USART peripheral base address.
  355. * @param enable Enable CTS or not, true for enable and false for disable.
  356. */
  357. static inline void USART_EnableCTS(USART_Type *base, bool enable)
  358. {
  359. if (enable)
  360. {
  361. base->CFG |= USART_CFG_CTSEN_MASK;
  362. }
  363. else
  364. {
  365. base->CFG &= ~USART_CFG_CTSEN_MASK;
  366. }
  367. }
  368. /* @} */
  369. /*!
  370. * @name Bus Operations
  371. * @{
  372. */
  373. /*!
  374. * @brief Writes to the FIFOWR register.
  375. *
  376. * This function writes data to the txFIFO directly. The upper layer must ensure
  377. * that txFIFO has space for data to write before calling this function.
  378. *
  379. * @param base USART peripheral base address.
  380. * @param data The byte to write.
  381. */
  382. static inline void USART_WriteByte(USART_Type *base, uint8_t data)
  383. {
  384. base->FIFOWR = data;
  385. }
  386. /*!
  387. * @brief Reads the FIFORD register directly.
  388. *
  389. * This function reads data from the rxFIFO directly. The upper layer must
  390. * ensure that the rxFIFO is not empty before calling this function.
  391. *
  392. * @param base USART peripheral base address.
  393. * @return The byte read from USART data register.
  394. */
  395. static inline uint8_t USART_ReadByte(USART_Type *base)
  396. {
  397. return base->FIFORD;
  398. }
  399. /*!
  400. * @brief Writes to the TX register using a blocking method.
  401. *
  402. * This function polls the TX register, waits for the TX register to be empty or for the TX FIFO
  403. * to have room and writes data to the TX buffer.
  404. *
  405. * @param base USART peripheral base address.
  406. * @param data Start address of the data to write.
  407. * @param length Size of the data to write.
  408. */
  409. void USART_WriteBlocking(USART_Type *base, const uint8_t *data, size_t length);
  410. /*!
  411. * @brief Read RX data register using a blocking method.
  412. *
  413. * This function polls the RX register, waits for the RX register to be full or for RX FIFO to
  414. * have data and read data from the TX register.
  415. *
  416. * @param base USART peripheral base address.
  417. * @param data Start address of the buffer to store the received data.
  418. * @param length Size of the buffer.
  419. * @retval kStatus_USART_FramingError Receiver overrun happened while receiving data.
  420. * @retval kStatus_USART_ParityError Noise error happened while receiving data.
  421. * @retval kStatus_USART_NoiseError Framing error happened while receiving data.
  422. * @retval kStatus_USART_RxError Overflow or underflow rxFIFO happened.
  423. * @retval kStatus_Success Successfully received all data.
  424. */
  425. status_t USART_ReadBlocking(USART_Type *base, uint8_t *data, size_t length);
  426. /* @} */
  427. /*!
  428. * @name Transactional
  429. * @{
  430. */
  431. /*!
  432. * @brief Initializes the USART handle.
  433. *
  434. * This function initializes the USART handle which can be used for other USART
  435. * transactional APIs. Usually, for a specified USART instance,
  436. * call this API once to get the initialized handle.
  437. *
  438. * @param base USART peripheral base address.
  439. * @param handle USART handle pointer.
  440. * @param callback The callback function.
  441. * @param userData The parameter of the callback function.
  442. */
  443. status_t USART_TransferCreateHandle(USART_Type *base,
  444. usart_handle_t *handle,
  445. usart_transfer_callback_t callback,
  446. void *userData);
  447. /*!
  448. * @brief Transmits a buffer of data using the interrupt method.
  449. *
  450. * This function sends data using an interrupt method. This is a non-blocking function, which
  451. * returns directly without waiting for all data to be written to the TX register. When
  452. * all data is written to the TX register in the IRQ handler, the USART driver calls the callback
  453. * function and passes the @ref kStatus_USART_TxIdle as status parameter.
  454. *
  455. * @note The kStatus_USART_TxIdle is passed to the upper layer when all data is written
  456. * to the TX register. However it does not ensure that all data are sent out. Before disabling the TX,
  457. * check the kUSART_TransmissionCompleteFlag to ensure that the TX is finished.
  458. *
  459. * @param base USART peripheral base address.
  460. * @param handle USART handle pointer.
  461. * @param xfer USART transfer structure. See #usart_transfer_t.
  462. * @retval kStatus_Success Successfully start the data transmission.
  463. * @retval kStatus_USART_TxBusy Previous transmission still not finished, data not all written to TX register yet.
  464. * @retval kStatus_InvalidArgument Invalid argument.
  465. */
  466. status_t USART_TransferSendNonBlocking(USART_Type *base, usart_handle_t *handle, usart_transfer_t *xfer);
  467. /*!
  468. * @brief Sets up the RX ring buffer.
  469. *
  470. * This function sets up the RX ring buffer to a specific USART handle.
  471. *
  472. * When the RX ring buffer is used, data received are stored into the ring buffer even when the
  473. * user doesn't call the USART_TransferReceiveNonBlocking() API. If there is already data received
  474. * in the ring buffer, the user can get the received data from the ring buffer directly.
  475. *
  476. * @note When using the RX ring buffer, one byte is reserved for internal use. In other
  477. * words, if @p ringBufferSize is 32, then only 31 bytes are used for saving data.
  478. *
  479. * @param base USART peripheral base address.
  480. * @param handle USART handle pointer.
  481. * @param ringBuffer Start address of the ring buffer for background receiving. Pass NULL to disable the ring buffer.
  482. * @param ringBufferSize size of the ring buffer.
  483. */
  484. void USART_TransferStartRingBuffer(USART_Type *base,
  485. usart_handle_t *handle,
  486. uint8_t *ringBuffer,
  487. size_t ringBufferSize);
  488. /*!
  489. * @brief Aborts the background transfer and uninstalls the ring buffer.
  490. *
  491. * This function aborts the background transfer and uninstalls the ring buffer.
  492. *
  493. * @param base USART peripheral base address.
  494. * @param handle USART handle pointer.
  495. */
  496. void USART_TransferStopRingBuffer(USART_Type *base, usart_handle_t *handle);
  497. /*!
  498. * @brief Get the length of received data in RX ring buffer.
  499. *
  500. * @param handle USART handle pointer.
  501. * @return Length of received data in RX ring buffer.
  502. */
  503. size_t USART_TransferGetRxRingBufferLength(usart_handle_t *handle);
  504. /*!
  505. * @brief Aborts the interrupt-driven data transmit.
  506. *
  507. * This function aborts the interrupt driven data sending. The user can get the remainBtyes to find out
  508. * how many bytes are still not sent out.
  509. *
  510. * @param base USART peripheral base address.
  511. * @param handle USART handle pointer.
  512. */
  513. void USART_TransferAbortSend(USART_Type *base, usart_handle_t *handle);
  514. /*!
  515. * @brief Get the number of bytes that have been written to USART TX register.
  516. *
  517. * This function gets the number of bytes that have been written to USART TX
  518. * register by interrupt method.
  519. *
  520. * @param base USART peripheral base address.
  521. * @param handle USART handle pointer.
  522. * @param count Send bytes count.
  523. * @retval kStatus_NoTransferInProgress No send in progress.
  524. * @retval kStatus_InvalidArgument Parameter is invalid.
  525. * @retval kStatus_Success Get successfully through the parameter \p count;
  526. */
  527. status_t USART_TransferGetSendCount(USART_Type *base, usart_handle_t *handle, uint32_t *count);
  528. /*!
  529. * @brief Receives a buffer of data using an interrupt method.
  530. *
  531. * This function receives data using an interrupt method. This is a non-blocking function, which
  532. * returns without waiting for all data to be received.
  533. * If the RX ring buffer is used and not empty, the data in the ring buffer is copied and
  534. * the parameter @p receivedBytes shows how many bytes are copied from the ring buffer.
  535. * After copying, if the data in the ring buffer is not enough to read, the receive
  536. * request is saved by the USART driver. When the new data arrives, the receive request
  537. * is serviced first. When all data is received, the USART driver notifies the upper layer
  538. * through a callback function and passes the status parameter @ref kStatus_USART_RxIdle.
  539. * For example, the upper layer needs 10 bytes but there are only 5 bytes in the ring buffer.
  540. * The 5 bytes are copied to the xfer->data and this function returns with the
  541. * parameter @p receivedBytes set to 5. For the left 5 bytes, newly arrived data is
  542. * saved from the xfer->data[5]. When 5 bytes are received, the USART driver notifies the upper layer.
  543. * If the RX ring buffer is not enabled, this function enables the RX and RX interrupt
  544. * to receive data to the xfer->data. When all data is received, the upper layer is notified.
  545. *
  546. * @param base USART peripheral base address.
  547. * @param handle USART handle pointer.
  548. * @param xfer USART transfer structure, see #usart_transfer_t.
  549. * @param receivedBytes Bytes received from the ring buffer directly.
  550. * @retval kStatus_Success Successfully queue the transfer into transmit queue.
  551. * @retval kStatus_USART_RxBusy Previous receive request is not finished.
  552. * @retval kStatus_InvalidArgument Invalid argument.
  553. */
  554. status_t USART_TransferReceiveNonBlocking(USART_Type *base,
  555. usart_handle_t *handle,
  556. usart_transfer_t *xfer,
  557. size_t *receivedBytes);
  558. /*!
  559. * @brief Aborts the interrupt-driven data receiving.
  560. *
  561. * This function aborts the interrupt-driven data receiving. The user can get the remainBytes to find out
  562. * how many bytes not received yet.
  563. *
  564. * @param base USART peripheral base address.
  565. * @param handle USART handle pointer.
  566. */
  567. void USART_TransferAbortReceive(USART_Type *base, usart_handle_t *handle);
  568. /*!
  569. * @brief Get the number of bytes that have been received.
  570. *
  571. * This function gets the number of bytes that have been received.
  572. *
  573. * @param base USART peripheral base address.
  574. * @param handle USART handle pointer.
  575. * @param count Receive bytes count.
  576. * @retval kStatus_NoTransferInProgress No receive in progress.
  577. * @retval kStatus_InvalidArgument Parameter is invalid.
  578. * @retval kStatus_Success Get successfully through the parameter \p count;
  579. */
  580. status_t USART_TransferGetReceiveCount(USART_Type *base, usart_handle_t *handle, uint32_t *count);
  581. /*!
  582. * @brief USART IRQ handle function.
  583. *
  584. * This function handles the USART transmit and receive IRQ request.
  585. *
  586. * @param base USART peripheral base address.
  587. * @param handle USART handle pointer.
  588. */
  589. void USART_TransferHandleIRQ(USART_Type *base, usart_handle_t *handle);
  590. /* @} */
  591. #if defined(__cplusplus)
  592. }
  593. #endif
  594. /*! @}*/
  595. #endif /* _FSL_USART_H_ */