fsl_uart.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  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. #include "fsl_uart.h"
  31. /*******************************************************************************
  32. * Definitions
  33. ******************************************************************************/
  34. /* UART transfer state. */
  35. enum _uart_tansfer_states
  36. {
  37. kUART_TxIdle, /* TX idle. */
  38. kUART_TxBusy, /* TX busy. */
  39. kUART_RxIdle, /* RX idle. */
  40. kUART_RxBusy, /* RX busy. */
  41. kUART_RxFramingError, /* Rx framing error */
  42. kUART_RxParityError /* Rx parity error */
  43. };
  44. /* Typedef for interrupt handler. */
  45. typedef void (*uart_isr_t)(UART_Type *base, uart_handle_t *handle);
  46. /*******************************************************************************
  47. * Prototypes
  48. ******************************************************************************/
  49. /*!
  50. * @brief Get the UART instance from peripheral base address.
  51. *
  52. * @param base UART peripheral base address.
  53. * @return UART instance.
  54. */
  55. uint32_t UART_GetInstance(UART_Type *base);
  56. /*!
  57. * @brief Check whether the RX ring buffer is full.
  58. *
  59. * @param handle UART handle pointer.
  60. * @retval true RX ring buffer is full.
  61. * @retval false RX ring buffer is not full.
  62. */
  63. static bool UART_TransferIsRxRingBufferFull(uart_handle_t *handle);
  64. /*!
  65. * @brief Read RX register using non-blocking method.
  66. *
  67. * This function reads data from the TX register directly, upper layer must make
  68. * sure the RX register is full or TX FIFO has data before calling this function.
  69. *
  70. * @param base UART peripheral base address.
  71. * @param data Start addresss of the buffer to store the received data.
  72. * @param length Size of the buffer.
  73. */
  74. static void UART_ReadNonBlocking(UART_Type *base, uint8_t *data, size_t length);
  75. /*!
  76. * @brief Write to TX register using non-blocking method.
  77. *
  78. * This function writes data to the TX register directly, upper layer must make
  79. * sure the TX register is empty or TX FIFO has empty room before calling this function.
  80. *
  81. * @note This function does not check whether all the data has been sent out to bus,
  82. * so before disable TX, check kUART_TransmissionCompleteFlag to ensure the TX is
  83. * finished.
  84. *
  85. * @param base UART peripheral base address.
  86. * @param data Start addresss of the data to write.
  87. * @param length Size of the buffer to be sent.
  88. */
  89. static void UART_WriteNonBlocking(UART_Type *base, const uint8_t *data, size_t length);
  90. /*******************************************************************************
  91. * Variables
  92. ******************************************************************************/
  93. /* ARRAY of UART handle */
  94. #if (defined(UART8))
  95. #define UART_HANDLE_ARRAY_SIZE 8
  96. #else /* UART8 */
  97. #if (defined(UART7))
  98. #define UART_HANDLE_ARRAY_SIZE 7
  99. #else /* UART7 */
  100. #if (defined(UART6))
  101. #define UART_HANDLE_ARRAY_SIZE 6
  102. #else /* UART6 */
  103. #if (defined(UART5))
  104. #define UART_HANDLE_ARRAY_SIZE 5
  105. #else /* UART5 */
  106. #if (defined(UART4))
  107. #define UART_HANDLE_ARRAY_SIZE 4
  108. #else /* UART4 */
  109. #if (defined(UART3))
  110. #define UART_HANDLE_ARRAY_SIZE 3
  111. #else /* UART3 */
  112. #if (defined(UART2))
  113. #define UART_HANDLE_ARRAY_SIZE 2
  114. #else /* UART2 */
  115. #if (defined(UART1))
  116. #define UART_HANDLE_ARRAY_SIZE 1
  117. #else /* UART1 */
  118. #error No UART instance.
  119. #endif /* UART 1 */
  120. #endif /* UART 2 */
  121. #endif /* UART 3 */
  122. #endif /* UART 4 */
  123. #endif /* UART 5 */
  124. #endif /* UART 6 */
  125. #endif /* UART 7 */
  126. #endif /* UART 8 */
  127. static uart_handle_t *s_uartHandle[UART_HANDLE_ARRAY_SIZE];
  128. /* Array of UART peripheral base address. */
  129. static UART_Type *const s_uartBases[] = UART_BASE_PTRS;
  130. /* Array of UART IRQ number. */
  131. static const IRQn_Type s_uartIRQ[] = UART_IRQS;
  132. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  133. /* Array of UART clock name. */
  134. static const clock_ip_name_t s_uartClock[] = UART_CLOCKS;
  135. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  136. /* UART ISR for transactional APIs. */
  137. static uart_isr_t s_uartIsr;
  138. /*******************************************************************************
  139. * Code
  140. ******************************************************************************/
  141. uint32_t UART_GetInstance(UART_Type *base)
  142. {
  143. uint32_t instance;
  144. uint32_t uartArrayCount = (sizeof(s_uartBases) / sizeof(s_uartBases[0]));
  145. /* Find the instance index from base address mappings. */
  146. for (instance = 0; instance < uartArrayCount; instance++)
  147. {
  148. if (s_uartBases[instance] == base)
  149. {
  150. break;
  151. }
  152. }
  153. assert(instance < uartArrayCount);
  154. return instance;
  155. }
  156. size_t UART_TransferGetRxRingBufferLength(uart_handle_t *handle)
  157. {
  158. assert(handle);
  159. size_t size;
  160. if (handle->rxRingBufferTail > handle->rxRingBufferHead)
  161. {
  162. size = (size_t)(handle->rxRingBufferHead + handle->rxRingBufferSize - handle->rxRingBufferTail);
  163. }
  164. else
  165. {
  166. size = (size_t)(handle->rxRingBufferHead - handle->rxRingBufferTail);
  167. }
  168. return size;
  169. }
  170. static bool UART_TransferIsRxRingBufferFull(uart_handle_t *handle)
  171. {
  172. assert(handle);
  173. bool full;
  174. if (UART_TransferGetRxRingBufferLength(handle) == (handle->rxRingBufferSize - 1U))
  175. {
  176. full = true;
  177. }
  178. else
  179. {
  180. full = false;
  181. }
  182. return full;
  183. }
  184. status_t UART_Init(UART_Type *base, const uart_config_t *config, uint32_t srcClock_Hz)
  185. {
  186. /* Check argument */
  187. assert(!((NULL == base) || (NULL == config) || (0U == srcClock_Hz)));
  188. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  189. /* Enable uart clock */
  190. CLOCK_EnableClock(s_uartClock[UART_GetInstance(base)]);
  191. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  192. /* Disable UART Module. */
  193. UART_Disable(base);
  194. /* Reset the transmit and receive state machines, all FIFOs and register
  195. * USR1, USR2, UBIR, UBMR, UBRC, URXD, UTXD and UTS[6-3]. */
  196. UART_SoftwareReset(base);
  197. /* Set UART Module Register content to default value */
  198. base->UCR1 = 0x0;
  199. base->UCR2 = UART_UCR2_SRST_MASK;
  200. base->UCR3 = UART_UCR3_DSR_MASK | UART_UCR3_DCD_MASK | UART_UCR3_RI_MASK;
  201. base->UCR4 = UART_UCR4_CTSTL(32);
  202. base->UFCR = UART_UFCR_TXTL(2) | UART_UFCR_RXTL(1);
  203. base->UESC = UART_UESC_ESC_CHAR(0x2B);
  204. base->UTIM = 0x0;
  205. base->ONEMS = 0x0;
  206. base->UTS = UART_UTS_TXEMPTY_MASK | UART_UTS_RXEMPTY_MASK;
  207. base->UMCR = 0x0;
  208. /* Set UART data word length, stop bit count, parity mode and communication
  209. * direction according to uart init struct, disable RTS hardware flow control.
  210. */
  211. base->UCR2 |=
  212. ((uint32_t)UART_UCR2_WS(config->dataBitsCount) | (uint32_t)UART_UCR2_STPB(config->stopBitCount) |
  213. (uint32_t)(((config->parityMode) << UART_UCR2_PROE_SHIFT) & (UART_UCR2_PREN_MASK | UART_UCR2_PROE_MASK)) |
  214. (uint32_t)UART_UCR2_TXEN(config->enableTx) | (uint32_t)UART_UCR2_RXEN(config->enableRx) | UART_UCR2_IRTS_MASK);
  215. /* For imx family device, UARTs are used in MUXED mode, so that this bit should always be set.*/
  216. if (FSL_FEATURE_IUART_RXDMUXSEL)
  217. {
  218. base->UCR3 |= UART_UCR3_RXDMUXSEL_MASK;
  219. }
  220. /* Set TX/RX fifo water mark */
  221. UART_SetTxFifoWatermark(base, config->txFifoWatermark);
  222. UART_SetRxFifoWatermark(base, config->rxFifoWatermark);
  223. if (config->enableAutoBaudRate)
  224. {
  225. /* Start automatic baud rate detection */
  226. UART_EnableAutoBaudRate(base, true);
  227. }
  228. else if (config->baudRate_Bps)
  229. {
  230. /* Stop automatic baud rate detection */
  231. UART_EnableAutoBaudRate(base, false);
  232. /* Set BaudRate according to uart initialize struct. Baud Rate = Ref Freq / (16 * (UBMR + 1)/(UBIR+1)) */
  233. if (kStatus_Success != UART_SetBaudRate(base, config->baudRate_Bps, srcClock_Hz))
  234. {
  235. return kStatus_UART_BaudrateNotSupport;
  236. }
  237. }
  238. else
  239. {
  240. /* Stop automatic baud rate detection */
  241. UART_EnableAutoBaudRate(base, false);
  242. }
  243. /* Enable UART module */
  244. UART_Enable(base);
  245. return kStatus_Success;
  246. }
  247. void UART_Deinit(UART_Type *base)
  248. {
  249. /* Wait transmit FIFO buffer and shift register empty */
  250. while (UART_USR2_TXDC_MASK != (base->USR2 & UART_USR2_TXDC_MASK))
  251. {
  252. }
  253. /* Disable UART Module */
  254. UART_Disable(base);
  255. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  256. /* Disable uart clock */
  257. CLOCK_DisableClock(s_uartClock[UART_GetInstance(base)]);
  258. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  259. }
  260. void UART_GetDefaultConfig(uart_config_t *config)
  261. {
  262. assert(config);
  263. config->baudRate_Bps = 115200U;
  264. config->parityMode = kUART_ParityDisabled;
  265. config->dataBitsCount = kUART_EightDataBits;
  266. config->stopBitCount = kUART_OneStopBit;
  267. config->txFifoWatermark = 2;
  268. config->rxFifoWatermark = 1;
  269. config->enableAutoBaudRate = false;
  270. config->enableTx = false;
  271. config->enableRx = false;
  272. }
  273. /* This UART instantiation uses a slightly different baud rate calculation.
  274. * Baud Rate = Ref Freq / (16 * (UBMR + 1)/(UBIR+1)).
  275. * To get a baud rate, three register need to be writen, UFCR,UBMR and UBIR
  276. * At first, find the approximately maximum divisor of src_Clock and baudRate_Bps.
  277. * If the numerator and denominator are larger then register maximum value(0xFFFF),
  278. * both of numerator and denominator will be divided by the same value, which
  279. * will ensure numerator and denominator range from 0~maximum value(0xFFFF).
  280. * Then calculate UFCR and UBIR value from numerator, and get UBMR value from denominator.
  281. */
  282. status_t UART_SetBaudRate(UART_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz)
  283. {
  284. uint32_t numerator = 0u;
  285. uint32_t denominator = 0U;
  286. uint32_t divisor = 0U;
  287. uint32_t refFreqDiv = 0U;
  288. uint32_t divider = 1U;
  289. uint64_t baudDiff = 0U;
  290. uint64_t tempNumerator = 0U;
  291. uint32_t tempDenominator = 0u;
  292. /* get the approximately maximum divisor */
  293. numerator = srcClock_Hz;
  294. denominator = baudRate_Bps << 4;
  295. divisor = 1;
  296. while (denominator != 0)
  297. {
  298. divisor = denominator;
  299. denominator = numerator % denominator;
  300. numerator = divisor;
  301. }
  302. numerator = srcClock_Hz / divisor;
  303. denominator = (baudRate_Bps << 4) / divisor;
  304. /* numerator ranges from 1 ~ 7 * 64k */
  305. /* denominator ranges from 1 ~ 64k */
  306. if ((numerator > (UART_UBIR_INC_MASK * 7)) || (denominator > UART_UBIR_INC_MASK))
  307. {
  308. uint32_t m = (numerator - 1) / (UART_UBIR_INC_MASK * 7) + 1;
  309. uint32_t n = (denominator - 1) / UART_UBIR_INC_MASK + 1;
  310. uint32_t max = m > n ? m : n;
  311. numerator /= max;
  312. denominator /= max;
  313. if (0 == numerator)
  314. {
  315. numerator = 1;
  316. }
  317. if (0 == denominator)
  318. {
  319. denominator = 1;
  320. }
  321. }
  322. divider = (numerator - 1) / UART_UBIR_INC_MASK + 1;
  323. switch (divider)
  324. {
  325. case 1:
  326. refFreqDiv = 0x05;
  327. break;
  328. case 2:
  329. refFreqDiv = 0x04;
  330. break;
  331. case 3:
  332. refFreqDiv = 0x03;
  333. break;
  334. case 4:
  335. refFreqDiv = 0x02;
  336. break;
  337. case 5:
  338. refFreqDiv = 0x01;
  339. break;
  340. case 6:
  341. refFreqDiv = 0x00;
  342. break;
  343. case 7:
  344. refFreqDiv = 0x06;
  345. break;
  346. default:
  347. refFreqDiv = 0x05;
  348. break;
  349. }
  350. /* Compare the difference between baudRate_Bps and calculated baud rate.
  351. * Baud Rate = Ref Freq / (16 * (UBMR + 1)/(UBIR+1)).
  352. * baudDiff = (srcClock_Hz/divider)/( 16 * ((numerator / divider)/ denominator).
  353. */
  354. tempNumerator = srcClock_Hz;
  355. tempDenominator = (numerator << 4);
  356. divisor = 1;
  357. /* get the approximately maximum divisor */
  358. while (tempDenominator != 0)
  359. {
  360. divisor = tempDenominator;
  361. tempDenominator = tempNumerator % tempDenominator;
  362. tempNumerator = divisor;
  363. }
  364. tempNumerator = srcClock_Hz / divisor;
  365. tempDenominator = (numerator << 4) / divisor;
  366. baudDiff = (tempNumerator * denominator) / tempDenominator;
  367. baudDiff = (baudDiff >= baudRate_Bps) ? (baudDiff - baudRate_Bps) : (baudRate_Bps - baudDiff);
  368. if (baudDiff < (baudRate_Bps / 100) * 3)
  369. {
  370. base->UFCR &= ~UART_UFCR_RFDIV_MASK;
  371. base->UFCR |= UART_UFCR_RFDIV(refFreqDiv);
  372. base->UBIR = UART_UBIR_INC(denominator - 1);
  373. base->UBMR = UART_UBMR_MOD(numerator / divider - 1);
  374. base->ONEMS = UART_ONEMS_ONEMS(srcClock_Hz / (1000 * divider));
  375. return kStatus_Success;
  376. }
  377. else
  378. {
  379. return kStatus_UART_BaudrateNotSupport;
  380. }
  381. }
  382. void UART_EnableInterrupts(UART_Type *base, uint32_t mask)
  383. {
  384. assert(0x3F3FF73FU & mask);
  385. if (0X3F & mask)
  386. {
  387. base->UCR1 |= ((mask << UART_UCR1_ADEN_SHIFT) & UART_UCR1_ADEN_MASK) |
  388. (((mask >> 1) << UART_UCR1_TRDYEN_SHIFT) & UART_UCR1_TRDYEN_MASK) |
  389. (((mask >> 2) << UART_UCR1_IDEN_SHIFT) & UART_UCR1_IDEN_MASK) |
  390. (((mask >> 3) << UART_UCR1_RRDYEN_SHIFT) & UART_UCR1_RRDYEN_MASK) |
  391. (((mask >> 4) << UART_UCR1_TXMPTYEN_SHIFT) & UART_UCR1_TXMPTYEN_MASK) |
  392. (((mask >> 5) << UART_UCR1_RTSDEN_SHIFT) & UART_UCR1_RTSDEN_MASK);
  393. }
  394. if (0X700U & mask)
  395. {
  396. base->UCR2 |= (((mask >> 8) << UART_UCR2_ESCI_SHIFT) & UART_UCR2_ESCI_MASK) |
  397. (((mask >> 9) << UART_UCR2_RTSEN_SHIFT) & UART_UCR2_RTSEN_MASK) |
  398. (((mask >> 10) << UART_UCR2_ATEN_SHIFT) & UART_UCR2_ATEN_MASK);
  399. }
  400. if (0x3FF000 & mask)
  401. {
  402. base->UCR3 |= (((mask >> 12) << UART_UCR3_DTREN_SHIFT) & UART_UCR3_DTREN_MASK) |
  403. (((mask >> 13) << UART_UCR3_PARERREN_SHIFT) & UART_UCR3_PARERREN_MASK) |
  404. (((mask >> 14) << UART_UCR3_FRAERREN_SHIFT) & UART_UCR3_FRAERREN_MASK) |
  405. (((mask >> 15) << UART_UCR3_DCD_SHIFT) & UART_UCR3_DCD_MASK) |
  406. (((mask >> 16) << UART_UCR3_RI_SHIFT) & UART_UCR3_RI_MASK) |
  407. (((mask >> 17) << UART_UCR3_RXDSEN_SHIFT) & UART_UCR3_RXDSEN_MASK) |
  408. (((mask >> 18) << UART_UCR3_AIRINTEN_SHIFT) & UART_UCR3_AIRINTEN_MASK) |
  409. (((mask >> 19) << UART_UCR3_AWAKEN_SHIFT) & UART_UCR3_AWAKEN_MASK) |
  410. (((mask >> 20) << UART_UCR3_DTRDEN_SHIFT) & UART_UCR3_DTRDEN_MASK) |
  411. (((mask >> 21) << UART_UCR3_ACIEN_SHIFT) & UART_UCR3_ACIEN_MASK);
  412. }
  413. if (0x3F000000 & mask)
  414. {
  415. base->UCR4 |= (((mask >> 24) << UART_UCR4_ENIRI_SHIFT) & UART_UCR4_ENIRI_MASK) |
  416. (((mask >> 25) << UART_UCR4_WKEN_SHIFT) & UART_UCR4_WKEN_MASK) |
  417. (((mask >> 26) << UART_UCR4_TCEN_SHIFT) & UART_UCR4_TCEN_MASK) |
  418. (((mask >> 27) << UART_UCR4_BKEN_SHIFT) & UART_UCR4_BKEN_MASK) |
  419. (((mask >> 28) << UART_UCR4_OREN_SHIFT) & UART_UCR4_OREN_MASK) |
  420. (((mask >> 29) << UART_UCR4_DREN_SHIFT) & UART_UCR4_DREN_MASK);
  421. }
  422. }
  423. void UART_DisableInterrupts(UART_Type *base, uint32_t mask)
  424. {
  425. assert(0x3F3FF73FU & mask);
  426. if (0X3F & mask)
  427. {
  428. base->UCR1 &= ~(((mask << UART_UCR1_ADEN_SHIFT) & UART_UCR1_ADEN_MASK) |
  429. (((mask >> 1) << UART_UCR1_TRDYEN_SHIFT) & UART_UCR1_TRDYEN_MASK) |
  430. (((mask >> 2) << UART_UCR1_IDEN_SHIFT) & UART_UCR1_IDEN_MASK) |
  431. (((mask >> 3) << UART_UCR1_RRDYEN_SHIFT) & UART_UCR1_RRDYEN_MASK) |
  432. (((mask >> 4) << UART_UCR1_TXMPTYEN_SHIFT) & UART_UCR1_TXMPTYEN_MASK) |
  433. (((mask >> 5) << UART_UCR1_RTSDEN_SHIFT) & UART_UCR1_RTSDEN_MASK));
  434. }
  435. if (0X700U & mask)
  436. {
  437. base->UCR2 &= ~((((mask >> 8) << UART_UCR2_ESCI_SHIFT) & UART_UCR2_ESCI_MASK) |
  438. (((mask >> 9) << UART_UCR2_RTSEN_SHIFT) & UART_UCR2_RTSEN_MASK) |
  439. (((mask >> 10) << UART_UCR2_ATEN_SHIFT) & UART_UCR2_ATEN_MASK));
  440. }
  441. if (0x3FF000 & mask)
  442. {
  443. base->UCR3 &= ~((((mask >> 12) << UART_UCR3_DTREN_SHIFT) & UART_UCR3_DTREN_MASK) |
  444. (((mask >> 13) << UART_UCR3_PARERREN_SHIFT) & UART_UCR3_PARERREN_MASK) |
  445. (((mask >> 14) << UART_UCR3_FRAERREN_SHIFT) & UART_UCR3_FRAERREN_MASK) |
  446. (((mask >> 15) << UART_UCR3_DCD_SHIFT) & UART_UCR3_DCD_MASK) |
  447. (((mask >> 16) << UART_UCR3_RI_SHIFT) & UART_UCR3_RI_MASK) |
  448. (((mask >> 17) << UART_UCR3_RXDSEN_SHIFT) & UART_UCR3_RXDSEN_MASK) |
  449. (((mask >> 18) << UART_UCR3_AIRINTEN_SHIFT) & UART_UCR3_AIRINTEN_MASK) |
  450. (((mask >> 19) << UART_UCR3_AWAKEN_SHIFT) & UART_UCR3_AWAKEN_MASK) |
  451. (((mask >> 20) << UART_UCR3_DTRDEN_SHIFT) & UART_UCR3_DTRDEN_MASK) |
  452. (((mask >> 21) << UART_UCR3_ACIEN_SHIFT) & UART_UCR3_ACIEN_MASK));
  453. }
  454. if (0x3F000000 & mask)
  455. {
  456. base->UCR4 &= ~((((mask >> 24) << UART_UCR4_ENIRI_SHIFT) & UART_UCR4_ENIRI_MASK) |
  457. (((mask >> 25) << UART_UCR4_WKEN_SHIFT) & UART_UCR4_WKEN_MASK) |
  458. (((mask >> 26) << UART_UCR4_TCEN_SHIFT) & UART_UCR4_TCEN_MASK) |
  459. (((mask >> 27) << UART_UCR4_BKEN_SHIFT) & UART_UCR4_BKEN_MASK) |
  460. (((mask >> 28) << UART_UCR4_OREN_SHIFT) & UART_UCR4_OREN_MASK) |
  461. (((mask >> 29) << UART_UCR4_DREN_SHIFT) & UART_UCR4_DREN_MASK));
  462. }
  463. }
  464. uint32_t UART_GetEnabledInterrupts(UART_Type *base)
  465. {
  466. assert(base);
  467. uint32_t temp = 0U;
  468. /* Get enabled interrupts from UCR1 */
  469. temp |= ((base->UCR1 & UART_UCR1_ADEN_MASK) >> UART_UCR1_ADEN_SHIFT) |
  470. (((base->UCR1 & UART_UCR1_TRDYEN_MASK) >> UART_UCR1_TRDYEN_SHIFT) << 1) |
  471. (((base->UCR1 & UART_UCR1_IDEN_MASK) >> UART_UCR1_IDEN_SHIFT) << 2) |
  472. (((base->UCR1 & UART_UCR1_RRDYEN_MASK) >> UART_UCR1_RRDYEN_SHIFT) << 3) |
  473. (((base->UCR1 & UART_UCR1_TXMPTYEN_MASK) >> UART_UCR1_TXMPTYEN_SHIFT) << 4) |
  474. (((base->UCR1 & UART_UCR1_RTSDEN_MASK) >> UART_UCR1_RTSDEN_SHIFT) << 5);
  475. /* Get enabled interrupts from UCR2 */
  476. temp |= (((base->UCR2 & UART_UCR2_ESCI_MASK) >> UART_UCR2_ESCI_SHIFT) << 8) |
  477. (((base->UCR2 & UART_UCR2_RTSEN_MASK) >> UART_UCR2_RTSEN_SHIFT) << 9) |
  478. (((base->UCR2 & UART_UCR2_ATEN_MASK) >> UART_UCR2_ATEN_SHIFT) << 10);
  479. /* Get enabled interrupts from UCR3 */
  480. temp |= (((base->UCR3 & UART_UCR3_DTREN_MASK) >> UART_UCR3_DTREN_SHIFT) << 12) |
  481. (((base->UCR3 & UART_UCR3_PARERREN_MASK) >> UART_UCR3_PARERREN_SHIFT) << 13) |
  482. (((base->UCR3 & UART_UCR3_FRAERREN_MASK) >> UART_UCR3_FRAERREN_SHIFT) << 14) |
  483. (((base->UCR3 & UART_UCR3_DCD_MASK) >> UART_UCR3_DCD_SHIFT) << 15) |
  484. (((base->UCR3 & UART_UCR3_RI_MASK) >> UART_UCR3_RI_SHIFT) << 16) |
  485. (((base->UCR3 & UART_UCR3_RXDSEN_MASK) >> UART_UCR3_RXDSEN_SHIFT) << 17) |
  486. (((base->UCR3 & UART_UCR3_AIRINTEN_MASK) >> UART_UCR3_AIRINTEN_SHIFT) << 18) |
  487. (((base->UCR3 & UART_UCR3_AWAKEN_MASK) >> UART_UCR3_AWAKEN_SHIFT) << 19) |
  488. (((base->UCR3 & UART_UCR3_DTRDEN_MASK) >> UART_UCR3_DTRDEN_SHIFT) << 20) |
  489. (((base->UCR3 & UART_UCR3_ACIEN_MASK) >> UART_UCR3_ACIEN_SHIFT) << 21);
  490. /* Get enabled interrupts from UCR4 */
  491. temp |= (((base->UCR4 & UART_UCR4_ENIRI_MASK) >> UART_UCR4_ENIRI_SHIFT) << 24) |
  492. (((base->UCR4 & UART_UCR4_WKEN_MASK) >> UART_UCR4_WKEN_SHIFT) << 25) |
  493. (((base->UCR4 & UART_UCR4_TCEN_MASK) >> UART_UCR4_TCEN_SHIFT) << 26) |
  494. (((base->UCR4 & UART_UCR4_BKEN_MASK) >> UART_UCR4_BKEN_SHIFT) << 27) |
  495. (((base->UCR4 & UART_UCR4_OREN_MASK) >> UART_UCR4_OREN_SHIFT) << 28) |
  496. (((base->UCR4 & UART_UCR4_DREN_MASK) >> UART_UCR4_DREN_SHIFT) << 29);
  497. return temp;
  498. }
  499. bool UART_GetStatusFlag(UART_Type *base, uint32_t flag)
  500. {
  501. volatile uint32_t *uart_reg;
  502. uart_reg = (uint32_t *)((uint32_t)base + (flag >> 16));
  503. return (bool)(((*uart_reg) >> (flag & 0x1FU)) & 0x1U);
  504. }
  505. void UART_ClearStatusFlag(UART_Type *base, uint32_t flag)
  506. {
  507. volatile uint32_t *uart_reg = 0;
  508. uint32_t uart_mask = 0;
  509. uart_reg = (uint32_t *)((uint32_t)base + (flag >> 16));
  510. uart_mask = (1U << (flag & 0x1FU));
  511. *uart_reg = uart_mask;
  512. }
  513. void UART_EnableDMA(UART_Type *base, uint32_t dmaSource, bool enable)
  514. {
  515. volatile uint32_t *uart_reg = 0;
  516. uint32_t uart_mask = 0;
  517. uart_reg = (uint32_t *)((uint32_t)base + (dmaSource >> 16));
  518. uart_mask = (1U << (dmaSource & 0x1FU));
  519. if (enable)
  520. {
  521. *uart_reg |= uart_mask;
  522. }
  523. else
  524. {
  525. *uart_reg &= ~uart_mask;
  526. }
  527. }
  528. void UART_WriteBlocking(UART_Type *base, const uint8_t *data, size_t length)
  529. {
  530. assert(data);
  531. while (length--)
  532. {
  533. /* Wait for TX fifo valid.
  534. * This API can only ensure that the data is written into the data buffer but can't
  535. * ensure all data in the data buffer are sent into the transmit shift buffer.
  536. */
  537. while (!(base->USR2 & UART_USR2_TXDC_MASK))
  538. {
  539. }
  540. UART_WriteByte(base, *(data++));
  541. }
  542. }
  543. status_t UART_ReadBlocking(UART_Type *base, uint8_t *data, size_t length)
  544. {
  545. assert(data);
  546. while (length--)
  547. {
  548. /* Wait for receive data in URXD register is ready */
  549. while (!(base->USR2 & UART_USR2_RDR_MASK))
  550. {
  551. /* Parity error check for receiving character */
  552. if (base->USR1 & UART_USR1_PARITYERR_MASK)
  553. {
  554. UART_ClearStatusFlag(base, kUART_ParityErrorFlag);
  555. return kStatus_UART_ParityError;
  556. }
  557. /* Framing error check for receiving character */
  558. if (base->USR1 & UART_USR1_FRAMERR_MASK)
  559. {
  560. UART_ClearStatusFlag(base, kUART_FrameErrorFlag);
  561. return kStatus_UART_FramingError;
  562. }
  563. /* Over run check for receiving character */
  564. if (base->USR2 & UART_USR2_ORE_MASK)
  565. {
  566. UART_ClearStatusFlag(base, kUART_RxOverrunFlag);
  567. return kStatus_UART_RxHardwareOverrun;
  568. }
  569. }
  570. /* Read data from URXD */
  571. *(data++) = UART_ReadByte(base);
  572. }
  573. return kStatus_Success;
  574. }
  575. static void UART_WriteNonBlocking(UART_Type *base, const uint8_t *data, size_t length)
  576. {
  577. assert(data);
  578. size_t i;
  579. /* The Non Blocking write data API assume user have ensured there is enough space in
  580. * peripheral to write. UTXD register holds the parallel transmit data inputs. In 7-bit mode,
  581. * D7 is ignored. In 8-bit mode, all bits are used.
  582. */
  583. for (i = 0; i < length; i++)
  584. {
  585. base->UTXD = data[i] & UART_UTXD_TX_DATA_MASK;
  586. }
  587. }
  588. static void UART_ReadNonBlocking(UART_Type *base, uint8_t *data, size_t length)
  589. {
  590. assert(data);
  591. size_t i;
  592. /* The Non Blocking read data API assume user have ensured there is enough space in
  593. * peripheral to write. The URXD holds the received character,In 7-bit mode,
  594. * the most significant bit (MSB) is forced to 0.In 8-bit mode, all bits are active.
  595. */
  596. for (i = 0; i < length; i++)
  597. {
  598. data[i] = (uint8_t)((base->URXD & UART_URXD_RX_DATA_MASK) >> UART_URXD_RX_DATA_SHIFT);
  599. }
  600. }
  601. void UART_TransferCreateHandle(UART_Type *base,
  602. uart_handle_t *handle,
  603. uart_transfer_callback_t callback,
  604. void *userData)
  605. {
  606. assert(handle);
  607. uint32_t instance;
  608. /* Zero the handle. */
  609. memset(handle, 0, sizeof(*handle));
  610. /* Set the TX/RX state. */
  611. handle->rxState = kUART_RxIdle;
  612. handle->txState = kUART_TxIdle;
  613. /* Set the callback and user data. */
  614. handle->callback = callback;
  615. handle->userData = userData;
  616. /* Get instance from peripheral base address. */
  617. instance = UART_GetInstance(base);
  618. /* Save the handle in global variables to support the double weak mechanism. */
  619. s_uartHandle[instance - 1] = handle;
  620. s_uartIsr = UART_TransferHandleIRQ;
  621. /* Enable interrupt in NVIC. */
  622. EnableIRQ(s_uartIRQ[instance]);
  623. }
  624. void UART_TransferStartRingBuffer(UART_Type *base, uart_handle_t *handle, uint8_t *ringBuffer, size_t ringBufferSize)
  625. {
  626. assert(handle);
  627. assert(ringBuffer);
  628. /* Setup the ringbuffer address */
  629. handle->rxRingBuffer = ringBuffer;
  630. handle->rxRingBufferSize = ringBufferSize;
  631. handle->rxRingBufferHead = 0U;
  632. handle->rxRingBufferTail = 0U;
  633. /* Enable the interrupt to accept the data when user need the ring buffer. */
  634. UART_EnableInterrupts(base, kUART_RxReadyEnable | kUART_AgingTimerEnable | kUART_RxOverrunEnable |
  635. kUART_ParityErrorEnable | kUART_FrameErrorEnable);
  636. }
  637. void UART_TransferStopRingBuffer(UART_Type *base, uart_handle_t *handle)
  638. {
  639. assert(handle);
  640. if (handle->rxState == kUART_RxIdle)
  641. {
  642. UART_DisableInterrupts(base, kUART_RxReadyEnable | kUART_AgingTimerEnable | kUART_RxOverrunEnable |
  643. kUART_ParityErrorEnable | kUART_FrameErrorEnable);
  644. }
  645. handle->rxRingBuffer = NULL;
  646. handle->rxRingBufferSize = 0U;
  647. handle->rxRingBufferHead = 0U;
  648. handle->rxRingBufferTail = 0U;
  649. }
  650. status_t UART_TransferSendNonBlocking(UART_Type *base, uart_handle_t *handle, uart_transfer_t *xfer)
  651. {
  652. assert(handle);
  653. assert(xfer);
  654. assert(xfer->dataSize);
  655. assert(xfer->data);
  656. status_t status;
  657. /* Return error if current TX busy. */
  658. if (kUART_TxBusy == handle->txState)
  659. {
  660. status = kStatus_UART_TxBusy;
  661. }
  662. else
  663. {
  664. handle->txData = xfer->data;
  665. handle->txDataSize = xfer->dataSize;
  666. handle->txDataSizeAll = xfer->dataSize;
  667. handle->txState = kUART_TxBusy;
  668. /* Enable transmiter interrupt. */
  669. UART_EnableInterrupts(base, kUART_TxReadyEnable);
  670. status = kStatus_Success;
  671. }
  672. return status;
  673. }
  674. void UART_TransferAbortSend(UART_Type *base, uart_handle_t *handle)
  675. {
  676. assert(handle);
  677. UART_DisableInterrupts(base, kUART_TxEmptyEnable);
  678. handle->txDataSize = 0;
  679. handle->txState = kUART_TxIdle;
  680. }
  681. status_t UART_TransferGetSendCount(UART_Type *base, uart_handle_t *handle, uint32_t *count)
  682. {
  683. assert(handle);
  684. assert(count);
  685. if (kUART_TxIdle == handle->txState)
  686. {
  687. return kStatus_NoTransferInProgress;
  688. }
  689. *count = handle->txDataSizeAll - handle->txDataSize;
  690. return kStatus_Success;
  691. }
  692. status_t UART_TransferReceiveNonBlocking(UART_Type *base,
  693. uart_handle_t *handle,
  694. uart_transfer_t *xfer,
  695. size_t *receivedBytes)
  696. {
  697. assert(handle);
  698. assert(xfer);
  699. assert(xfer->data);
  700. assert(xfer->dataSize);
  701. uint32_t i;
  702. status_t status;
  703. /* How many bytes to copy from ring buffer to user memory. */
  704. size_t bytesToCopy = 0U;
  705. /* How many bytes to receive. */
  706. size_t bytesToReceive;
  707. /* How many bytes currently have received. */
  708. size_t bytesCurrentReceived;
  709. /* How to get data:
  710. 1. If RX ring buffer is not enabled, then save xfer->data and xfer->dataSize
  711. to uart handle, enable interrupt to store received data to xfer->data. When
  712. all data received, trigger callback.
  713. 2. If RX ring buffer is enabled and not empty, get data from ring buffer first.
  714. If there are enough data in ring buffer, copy them to xfer->data and return.
  715. If there are not enough data in ring buffer, copy all of them to xfer->data,
  716. save the xfer->data remained empty space to uart handle, receive data
  717. to this empty space and trigger callback when finished. */
  718. if (kUART_RxBusy == handle->rxState)
  719. {
  720. status = kStatus_UART_RxBusy;
  721. }
  722. else
  723. {
  724. bytesToReceive = xfer->dataSize;
  725. bytesCurrentReceived = 0U;
  726. /* If RX ring buffer is used. */
  727. if (handle->rxRingBuffer)
  728. {
  729. /* Disable UART RX IRQ, protect ring buffer. */
  730. UART_DisableInterrupts(base, kUART_RxReadyEnable | kUART_AgingTimerEnable);
  731. /* How many bytes in RX ring buffer currently. */
  732. bytesToCopy = UART_TransferGetRxRingBufferLength(handle);
  733. if (bytesToCopy)
  734. {
  735. bytesToCopy = MIN(bytesToReceive, bytesToCopy);
  736. bytesToReceive -= bytesToCopy;
  737. /* Copy data from ring buffer to user memory. */
  738. for (i = 0U; i < bytesToCopy; i++)
  739. {
  740. xfer->data[bytesCurrentReceived++] = handle->rxRingBuffer[handle->rxRingBufferTail];
  741. /* Wrap to 0. Not use modulo (%) because it might be large and slow. */
  742. if (handle->rxRingBufferTail + 1U == handle->rxRingBufferSize)
  743. {
  744. handle->rxRingBufferTail = 0U;
  745. }
  746. else
  747. {
  748. handle->rxRingBufferTail++;
  749. }
  750. }
  751. }
  752. /* If ring buffer does not have enough data, still need to read more data. */
  753. if (bytesToReceive)
  754. {
  755. /* No data in ring buffer, save the request to UART handle. */
  756. handle->rxData = xfer->data + bytesCurrentReceived;
  757. handle->rxDataSize = bytesToReceive;
  758. handle->rxDataSizeAll = bytesToReceive;
  759. handle->rxState = kUART_RxBusy;
  760. }
  761. /* Enable UART RX IRQ if previously enabled. */
  762. UART_EnableInterrupts(base, kUART_RxReadyEnable | kUART_AgingTimerEnable);
  763. /* Call user callback since all data are received. */
  764. if (0 == bytesToReceive)
  765. {
  766. if (handle->callback)
  767. {
  768. handle->callback(base, handle, kStatus_UART_RxIdle, handle->userData);
  769. }
  770. }
  771. }
  772. /* Ring buffer not used. */
  773. else
  774. {
  775. handle->rxData = xfer->data + bytesCurrentReceived;
  776. handle->rxDataSize = bytesToReceive;
  777. handle->rxDataSizeAll = bytesToReceive;
  778. handle->rxState = kUART_RxBusy;
  779. /* Enable RX/Rx overrun/framing error interrupt. */
  780. UART_EnableInterrupts(base, kUART_RxReadyEnable | kUART_AgingTimerEnable | kUART_RxOverrunEnable |
  781. kUART_ParityErrorEnable | kUART_FrameErrorEnable);
  782. }
  783. /* Return the how many bytes have read. */
  784. if (receivedBytes)
  785. {
  786. *receivedBytes = bytesCurrentReceived;
  787. }
  788. status = kStatus_Success;
  789. }
  790. return status;
  791. }
  792. void UART_TransferAbortReceive(UART_Type *base, uart_handle_t *handle)
  793. {
  794. assert(handle);
  795. /* Only abort the receive to handle->rxData, the RX ring buffer is still working. */
  796. if (!handle->rxRingBuffer)
  797. {
  798. /* Disable RX interrupt. */
  799. UART_DisableInterrupts(base, kUART_RxReadyEnable | kUART_AgingTimerEnable | kUART_RxOverrunEnable |
  800. kUART_ParityErrorEnable | kUART_FrameErrorEnable);
  801. }
  802. handle->rxDataSize = 0U;
  803. handle->rxState = kUART_RxIdle;
  804. }
  805. status_t UART_TransferGetReceiveCount(UART_Type *base, uart_handle_t *handle, uint32_t *count)
  806. {
  807. assert(handle);
  808. assert(count);
  809. if (kUART_RxIdle == handle->rxState)
  810. {
  811. return kStatus_NoTransferInProgress;
  812. }
  813. if (!count)
  814. {
  815. return kStatus_InvalidArgument;
  816. }
  817. *count = handle->rxDataSizeAll - handle->rxDataSize;
  818. return kStatus_Success;
  819. }
  820. void UART_TransferHandleIRQ(UART_Type *base, uart_handle_t *handle)
  821. {
  822. assert(handle);
  823. uint8_t count;
  824. uint8_t tempCount;
  825. /* If RX framing error */
  826. if (UART_USR1_FRAMERR_MASK & base->USR1)
  827. {
  828. /* Write 1 to clear framing error flag */
  829. base->USR1 |= UART_USR1_FRAMERR_MASK;
  830. handle->rxState = kUART_RxFramingError;
  831. handle->rxDataSize = 0U;
  832. /* Trigger callback. */
  833. if (handle->callback)
  834. {
  835. handle->callback(base, handle, kStatus_UART_FramingError, handle->userData);
  836. }
  837. }
  838. /* If RX parity error */
  839. if (UART_USR1_PARITYERR_MASK & base->USR1)
  840. {
  841. /* Write 1 to clear parity error flag. */
  842. base->USR1 |= UART_USR1_PARITYERR_MASK;
  843. handle->rxState = kUART_RxParityError;
  844. handle->rxDataSize = 0U;
  845. /* Trigger callback. */
  846. if (handle->callback)
  847. {
  848. handle->callback(base, handle, kStatus_UART_ParityError, handle->userData);
  849. }
  850. }
  851. /* If RX overrun. */
  852. if (UART_USR2_ORE_MASK & base->USR2)
  853. {
  854. /* Write 1 to clear overrun flag. */
  855. base->USR2 |= UART_USR2_ORE_MASK;
  856. /* Trigger callback. */
  857. if (handle->callback)
  858. {
  859. handle->callback(base, handle, kStatus_UART_RxHardwareOverrun, handle->userData);
  860. }
  861. }
  862. /* Receive data FIFO buffer reach the trigger level */
  863. if (((UART_USR1_RRDY_MASK)&base->USR1) && (UART_UCR1_RRDYEN_MASK & base->UCR1))
  864. {
  865. /* Get the size that stored in receive FIFO buffer for this interrupt. */
  866. count = ((base->UFCR & UART_UFCR_RXTL_MASK) >> UART_UFCR_RXTL_SHIFT);
  867. /* If count and handle->rxDataSize are not 0, first save data to handle->rxData. */
  868. while ((count) && (handle->rxDataSize))
  869. {
  870. tempCount = MIN(handle->rxDataSize, count);
  871. /* Using non block API to read the data from the registers. */
  872. UART_ReadNonBlocking(base, handle->rxData, tempCount);
  873. handle->rxData += tempCount;
  874. handle->rxDataSize -= tempCount;
  875. count -= tempCount;
  876. /* If all the data required for upper layer is ready, trigger callback. */
  877. if (!handle->rxDataSize)
  878. {
  879. handle->rxState = kUART_RxIdle;
  880. if (handle->callback)
  881. {
  882. handle->callback(base, handle, kStatus_UART_RxIdle, handle->userData);
  883. }
  884. }
  885. }
  886. /* If use RX ring buffer, receive data to ring buffer. */
  887. if (handle->rxRingBuffer)
  888. {
  889. while (count--)
  890. {
  891. /* If RX ring buffer is full, trigger callback to notify over run. */
  892. if (UART_TransferIsRxRingBufferFull(handle))
  893. {
  894. if (handle->callback)
  895. {
  896. handle->callback(base, handle, kStatus_UART_RxRingBufferOverrun, handle->userData);
  897. }
  898. }
  899. /* If ring buffer is still full after callback function, the oldest data is overrided. */
  900. if (UART_TransferIsRxRingBufferFull(handle))
  901. {
  902. /* Increase handle->rxRingBufferTail to make room for new data. */
  903. if (handle->rxRingBufferTail + 1U == handle->rxRingBufferSize)
  904. {
  905. handle->rxRingBufferTail = 0U;
  906. }
  907. else
  908. {
  909. handle->rxRingBufferTail++;
  910. }
  911. }
  912. /* Read data. */
  913. handle->rxRingBuffer[handle->rxRingBufferHead] =
  914. (uint8_t)((base->URXD & UART_URXD_RX_DATA_MASK) >> UART_URXD_RX_DATA_SHIFT);
  915. /* Increase handle->rxRingBufferHead. */
  916. if (handle->rxRingBufferHead + 1U == handle->rxRingBufferSize)
  917. {
  918. handle->rxRingBufferHead = 0U;
  919. }
  920. else
  921. {
  922. handle->rxRingBufferHead++;
  923. }
  924. }
  925. }
  926. else if (!handle->rxDataSize)
  927. {
  928. /* Disable RX interrupt/overrun interrupt/framing error interrupt */
  929. UART_DisableInterrupts(base, kUART_RxReadyEnable | kUART_AgingTimerEnable | kUART_RxOverrunEnable |
  930. kUART_ParityErrorEnable | kUART_FrameErrorEnable);
  931. }
  932. else
  933. {
  934. }
  935. }
  936. /* Receive FIFO buffer has been idle for a time of 8 characters, and FIFO data level
  937. * is less than RxFIFO threshold level.
  938. */
  939. if (((UART_USR1_AGTIM_MASK)&base->USR1) && (UART_UCR2_ATEN_MASK & base->UCR2))
  940. {
  941. /* If count and handle->rxDataSize are not 0, first save data to handle->rxData. */
  942. while ((base->USR2 & UART_USR2_RDR_MASK) && (handle->rxDataSize))
  943. {
  944. /* Read one data from the URXD registers. */
  945. *handle->rxData = UART_ReadByte(base);
  946. handle->rxData += 1;
  947. handle->rxDataSize -= 1;
  948. /* If all the data required for upper layer is ready, trigger callback. */
  949. if (!handle->rxDataSize)
  950. {
  951. handle->rxState = kUART_RxIdle;
  952. if (handle->callback)
  953. {
  954. handle->callback(base, handle, kStatus_UART_RxIdle, handle->userData);
  955. }
  956. }
  957. }
  958. /* If use RX ring buffer, receive data to ring buffer. */
  959. if (handle->rxRingBuffer)
  960. {
  961. while (base->USR2 & UART_USR2_RDR_MASK)
  962. {
  963. /* If RX ring buffer is full, trigger callback to notify over run. */
  964. if (UART_TransferIsRxRingBufferFull(handle))
  965. {
  966. if (handle->callback)
  967. {
  968. handle->callback(base, handle, kStatus_UART_RxRingBufferOverrun, handle->userData);
  969. }
  970. }
  971. /* If ring buffer is still full after callback function, the oldest data is overrided. */
  972. if (UART_TransferIsRxRingBufferFull(handle))
  973. {
  974. /* Increase handle->rxRingBufferTail to make room for new data. */
  975. if (handle->rxRingBufferTail + 1U == handle->rxRingBufferSize)
  976. {
  977. handle->rxRingBufferTail = 0U;
  978. }
  979. else
  980. {
  981. handle->rxRingBufferTail++;
  982. }
  983. }
  984. /* Read one data from URXD register. */
  985. handle->rxRingBuffer[handle->rxRingBufferHead] = UART_ReadByte(base);
  986. /* Increase handle->rxRingBufferHead. */
  987. if (handle->rxRingBufferHead + 1U == handle->rxRingBufferSize)
  988. {
  989. handle->rxRingBufferHead = 0U;
  990. }
  991. else
  992. {
  993. handle->rxRingBufferHead++;
  994. }
  995. }
  996. }
  997. /* If ring buffer is not used and rxDataSize is 0 */
  998. else if (!handle->rxDataSize)
  999. {
  1000. /* Disable RX interrupt/overrun interrupt/framing error interrupt */
  1001. UART_DisableInterrupts(base, kUART_RxReadyEnable | kUART_AgingTimerEnable | kUART_RxOverrunEnable |
  1002. kUART_ParityErrorEnable | kUART_FrameErrorEnable);
  1003. }
  1004. else
  1005. {
  1006. }
  1007. /* Clear aging timer flag for next interrupt */
  1008. UART_ClearStatusFlag(base, kUART_AgingTimerFlag);
  1009. }
  1010. /* If frame error or parity error happened, stop the RX interrupt when ues no ring buffer */
  1011. if (((handle->rxState == kUART_RxFramingError) || (handle->rxState == kUART_RxParityError)) &&
  1012. (!handle->rxRingBuffer))
  1013. {
  1014. /* Disable Receive ready interrupt, aging timer interrupt, receive over run interrupt,
  1015. * parity error interrupt and frame error interrupt.
  1016. */
  1017. UART_DisableInterrupts(base, kUART_RxReadyEnable | kUART_AgingTimerEnable | kUART_RxOverrunEnable |
  1018. kUART_ParityErrorEnable | kUART_FrameErrorEnable);
  1019. }
  1020. /* Send data register empty and the interrupt is enabled. */
  1021. if ((UART_USR1_TRDY_MASK & base->USR1) && (UART_UCR1_TRDYEN_MASK & base->UCR1))
  1022. {
  1023. /* Get the bytes that available at this moment. */
  1024. if (0U != ((base->UFCR & UART_UFCR_TXTL_MASK) >> UART_UFCR_TXTL_SHIFT))
  1025. {
  1026. if (UART_UTS_TXEMPTY_MASK & (base->UTS))
  1027. {
  1028. count = FSL_FEATURE_IUART_FIFO_SIZEn(base);
  1029. }
  1030. else
  1031. {
  1032. count =
  1033. FSL_FEATURE_IUART_FIFO_SIZEn(base) - ((base->UFCR & UART_UFCR_TXTL_MASK) >> UART_UFCR_TXTL_SHIFT);
  1034. }
  1035. }
  1036. else
  1037. {
  1038. count = 1;
  1039. }
  1040. while ((count) && (handle->txDataSize))
  1041. {
  1042. if (0U != ((base->UFCR & UART_UFCR_TXTL_MASK) >> UART_UFCR_TXTL_SHIFT))
  1043. {
  1044. tempCount = MIN(handle->txDataSize, count);
  1045. }
  1046. else
  1047. {
  1048. tempCount = 1;
  1049. }
  1050. /* Using non block API to write the data to the registers. */
  1051. UART_WriteNonBlocking(base, handle->txData, tempCount);
  1052. handle->txData += tempCount;
  1053. handle->txDataSize -= tempCount;
  1054. count -= tempCount;
  1055. /* If all the data are written to data register, TX finished. */
  1056. if (!handle->txDataSize)
  1057. {
  1058. handle->txState = kUART_TxIdle;
  1059. /* Disable TX FIFO buffer empty interrupt. */
  1060. UART_DisableInterrupts(base, kUART_TxReadyEnable);
  1061. /* Trigger callback.
  1062. * Note: This callback is called when all data have been send to TX FIFO,
  1063. * but this not mean transmit has completed, some data may still in
  1064. * FIFO buffer.
  1065. */
  1066. if (handle->callback)
  1067. {
  1068. handle->callback(base, handle, kStatus_UART_TxIdle, handle->userData);
  1069. }
  1070. }
  1071. }
  1072. }
  1073. }
  1074. #if defined(UART1)
  1075. void UART1_DriverIRQHandler(void)
  1076. {
  1077. s_uartIsr(UART1, s_uartHandle[0]);
  1078. }
  1079. #endif
  1080. #if defined(UART2)
  1081. void UART2_DriverIRQHandler(void)
  1082. {
  1083. s_uartIsr(UART2, s_uartHandle[1]);
  1084. }
  1085. #endif
  1086. #if defined(UART3)
  1087. void UART3_DriverIRQHandler(void)
  1088. {
  1089. s_uartIsr(UART3, s_uartHandle[2]);
  1090. }
  1091. #endif
  1092. #if defined(UART4)
  1093. void UART4_DriverIRQHandler(void)
  1094. {
  1095. s_uartIsr(UART4, s_uartHandle[3]);
  1096. }
  1097. #endif
  1098. #if defined(UART5)
  1099. void UART5_DriverIRQHandler(void)
  1100. {
  1101. s_uartIsr(UART5, s_uartHandle[4]);
  1102. }
  1103. #endif
  1104. #if defined(UART6)
  1105. void UART6_DriverIRQHandler(void)
  1106. {
  1107. s_uartIsr(UART6, s_uartHandle[5]);
  1108. }
  1109. #endif
  1110. #if defined(UART7)
  1111. void UART7_DriverIRQHandler(void)
  1112. {
  1113. s_uartIsr(UART7, s_uartHandle[6]);
  1114. }
  1115. #endif
  1116. #if defined(UART8)
  1117. void UART8_DriverIRQHandler(void)
  1118. {
  1119. s_uartIsr(UART8, s_uartHandle[7]);
  1120. }
  1121. #endif