fsl_uart.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  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. #include "fsl_uart.h"
  35. /*******************************************************************************
  36. * Definitions
  37. ******************************************************************************/
  38. /* UART transfer state. */
  39. enum _uart_tansfer_states
  40. {
  41. kUART_TxIdle, /* TX idle. */
  42. kUART_TxBusy, /* TX busy. */
  43. kUART_RxIdle, /* RX idle. */
  44. kUART_RxBusy, /* RX busy. */
  45. kUART_RxFramingError, /* Rx framing error */
  46. kUART_RxParityError /* Rx parity error */
  47. };
  48. /* Typedef for interrupt handler. */
  49. typedef void (*uart_isr_t)(UART_Type *base, uart_handle_t *handle);
  50. /*******************************************************************************
  51. * Prototypes
  52. ******************************************************************************/
  53. /*!
  54. * @brief Get the UART instance from peripheral base address.
  55. *
  56. * @param base UART peripheral base address.
  57. * @return UART instance.
  58. */
  59. uint32_t UART_GetInstance(UART_Type *base);
  60. /*!
  61. * @brief Check whether the RX ring buffer is full.
  62. *
  63. * @param handle UART handle pointer.
  64. * @retval true RX ring buffer is full.
  65. * @retval false RX ring buffer is not full.
  66. */
  67. static bool UART_TransferIsRxRingBufferFull(uart_handle_t *handle);
  68. /*!
  69. * @brief Read RX register using non-blocking method.
  70. *
  71. * This function reads data from the TX register directly, upper layer must make
  72. * sure the RX register is full or TX FIFO has data before calling this function.
  73. *
  74. * @param base UART peripheral base address.
  75. * @param data Start addresss of the buffer to store the received data.
  76. * @param length Size of the buffer.
  77. */
  78. static void UART_ReadNonBlocking(UART_Type *base, uint8_t *data, size_t length);
  79. /*!
  80. * @brief Write to TX register using non-blocking method.
  81. *
  82. * This function writes data to the TX register directly, upper layer must make
  83. * sure the TX register is empty or TX FIFO has empty room before calling this function.
  84. *
  85. * @note This function does not check whether all the data has been sent out to bus,
  86. * so before disable TX, check kUART_TransmissionCompleteFlag to ensure the TX is
  87. * finished.
  88. *
  89. * @param base UART peripheral base address.
  90. * @param data Start addresss of the data to write.
  91. * @param length Size of the buffer to be sent.
  92. */
  93. static void UART_WriteNonBlocking(UART_Type *base, const uint8_t *data, size_t length);
  94. /*******************************************************************************
  95. * Variables
  96. ******************************************************************************/
  97. /* Array of UART handle. */
  98. #if (defined(UART5))
  99. #define UART_HANDLE_ARRAY_SIZE 6
  100. #else /* UART5 */
  101. #if (defined(UART4))
  102. #define UART_HANDLE_ARRAY_SIZE 5
  103. #else /* UART4 */
  104. #if (defined(UART3))
  105. #define UART_HANDLE_ARRAY_SIZE 4
  106. #else /* UART3 */
  107. #if (defined(UART2))
  108. #define UART_HANDLE_ARRAY_SIZE 3
  109. #else /* UART2 */
  110. #if (defined(UART1))
  111. #define UART_HANDLE_ARRAY_SIZE 2
  112. #else /* UART1 */
  113. #if (defined(UART0))
  114. #define UART_HANDLE_ARRAY_SIZE 1
  115. #else /* UART0 */
  116. #error No UART instance.
  117. #endif /* UART 0 */
  118. #endif /* UART 1 */
  119. #endif /* UART 2 */
  120. #endif /* UART 3 */
  121. #endif /* UART 4 */
  122. #endif /* UART 5 */
  123. static uart_handle_t *s_uartHandle[UART_HANDLE_ARRAY_SIZE];
  124. /* Array of UART peripheral base address. */
  125. static UART_Type *const s_uartBases[] = UART_BASE_PTRS;
  126. /* Array of UART IRQ number. */
  127. static const IRQn_Type s_uartIRQ[] = UART_RX_TX_IRQS;
  128. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  129. /* Array of UART clock name. */
  130. static const clock_ip_name_t s_uartClock[] = UART_CLOCKS;
  131. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  132. /* UART ISR for transactional APIs. */
  133. static uart_isr_t s_uartIsr;
  134. /*******************************************************************************
  135. * Code
  136. ******************************************************************************/
  137. uint32_t UART_GetInstance(UART_Type *base)
  138. {
  139. uint32_t instance;
  140. uint32_t uartArrayCount = (sizeof(s_uartBases) / sizeof(s_uartBases[0]));
  141. /* Find the instance index from base address mappings. */
  142. for (instance = 0; instance < uartArrayCount; instance++)
  143. {
  144. if (s_uartBases[instance] == base)
  145. {
  146. break;
  147. }
  148. }
  149. assert(instance < uartArrayCount);
  150. return instance;
  151. }
  152. size_t UART_TransferGetRxRingBufferLength(uart_handle_t *handle)
  153. {
  154. assert(handle);
  155. size_t size;
  156. if (handle->rxRingBufferTail > handle->rxRingBufferHead)
  157. {
  158. size = (size_t)(handle->rxRingBufferHead + handle->rxRingBufferSize - handle->rxRingBufferTail);
  159. }
  160. else
  161. {
  162. size = (size_t)(handle->rxRingBufferHead - handle->rxRingBufferTail);
  163. }
  164. return size;
  165. }
  166. static bool UART_TransferIsRxRingBufferFull(uart_handle_t *handle)
  167. {
  168. assert(handle);
  169. bool full;
  170. if (UART_TransferGetRxRingBufferLength(handle) == (handle->rxRingBufferSize - 1U))
  171. {
  172. full = true;
  173. }
  174. else
  175. {
  176. full = false;
  177. }
  178. return full;
  179. }
  180. status_t UART_Init(UART_Type *base, const uart_config_t *config, uint32_t srcClock_Hz)
  181. {
  182. assert(config);
  183. assert(config->baudRate_Bps);
  184. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  185. assert(FSL_FEATURE_UART_FIFO_SIZEn(base) >= config->txFifoWatermark);
  186. assert(FSL_FEATURE_UART_FIFO_SIZEn(base) >= config->rxFifoWatermark);
  187. #endif
  188. uint16_t sbr = 0;
  189. uint8_t temp = 0;
  190. uint32_t baudDiff = 0;
  191. /* Calculate the baud rate modulo divisor, sbr*/
  192. sbr = srcClock_Hz / (config->baudRate_Bps * 16);
  193. /* set sbrTemp to 1 if the sourceClockInHz can not satisfy the desired baud rate */
  194. if (sbr == 0)
  195. {
  196. sbr = 1;
  197. }
  198. #if defined(FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT) && FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT
  199. /* Determine if a fractional divider is needed to fine tune closer to the
  200. * desired baud, each value of brfa is in 1/32 increments,
  201. * hence the multiply-by-32. */
  202. uint32_t tempBaud = 0;
  203. uint16_t brfa = (2 * srcClock_Hz / (config->baudRate_Bps)) - 32 * sbr;
  204. /* Calculate the baud rate based on the temporary SBR values and BRFA */
  205. tempBaud = (srcClock_Hz * 2 / ((sbr * 32 + brfa)));
  206. baudDiff =
  207. (tempBaud > config->baudRate_Bps) ? (tempBaud - config->baudRate_Bps) : (config->baudRate_Bps - tempBaud);
  208. #else
  209. /* Calculate the baud rate based on the temporary SBR values */
  210. baudDiff = (srcClock_Hz / (sbr * 16)) - config->baudRate_Bps;
  211. /* Select the better value between sbr and (sbr + 1) */
  212. if (baudDiff > (config->baudRate_Bps - (srcClock_Hz / (16 * (sbr + 1)))))
  213. {
  214. baudDiff = config->baudRate_Bps - (srcClock_Hz / (16 * (sbr + 1)));
  215. sbr++;
  216. }
  217. #endif
  218. /* next, check to see if actual baud rate is within 3% of desired baud rate
  219. * based on the calculate SBR value */
  220. if (baudDiff > ((config->baudRate_Bps / 100) * 3))
  221. {
  222. /* Unacceptable baud rate difference of more than 3%*/
  223. return kStatus_UART_BaudrateNotSupport;
  224. }
  225. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  226. /* Enable uart clock */
  227. CLOCK_EnableClock(s_uartClock[UART_GetInstance(base)]);
  228. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  229. /* Disable UART TX RX before setting. */
  230. base->C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK);
  231. /* Write the sbr value to the BDH and BDL registers*/
  232. base->BDH = (base->BDH & ~UART_BDH_SBR_MASK) | (uint8_t)(sbr >> 8);
  233. base->BDL = (uint8_t)sbr;
  234. #if defined(FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT) && FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT
  235. /* Write the brfa value to the register*/
  236. base->C4 = (base->C4 & ~UART_C4_BRFA_MASK) | (brfa & UART_C4_BRFA_MASK);
  237. #endif
  238. /* Set bit count/parity mode/idle type. */
  239. temp = base->C1 & ~(UART_C1_PE_MASK | UART_C1_PT_MASK | UART_C1_M_MASK | UART_C1_ILT_MASK);
  240. temp |= UART_C1_ILT(config->idleType);
  241. if (kUART_ParityDisabled != config->parityMode)
  242. {
  243. temp |= (UART_C1_M_MASK | (uint8_t)config->parityMode);
  244. }
  245. base->C1 = temp;
  246. #if defined(FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT) && FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT
  247. /* Set stop bit per char */
  248. base->BDH = (base->BDH & ~UART_BDH_SBNS_MASK) | UART_BDH_SBNS((uint8_t)config->stopBitCount);
  249. #endif
  250. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  251. /* Set tx/rx FIFO watermark
  252. Note:
  253. Take care of the RX FIFO, RX interrupt request only assert when received bytes
  254. equal or more than RX water mark, there is potential issue if RX water
  255. mark larger than 1.
  256. For example, if RX FIFO water mark is 2, upper layer needs 5 bytes and
  257. 5 bytes are received. the last byte will be saved in FIFO but not trigger
  258. RX interrupt because the water mark is 2.
  259. */
  260. base->TWFIFO = config->txFifoWatermark;
  261. base->RWFIFO = config->rxFifoWatermark;
  262. /* Enable tx/rx FIFO */
  263. base->PFIFO |= (UART_PFIFO_TXFE_MASK | UART_PFIFO_RXFE_MASK);
  264. /* Flush FIFO */
  265. base->CFIFO |= (UART_CFIFO_TXFLUSH_MASK | UART_CFIFO_RXFLUSH_MASK);
  266. #endif
  267. #if defined(FSL_FEATURE_UART_HAS_MODEM_SUPPORT) && FSL_FEATURE_UART_HAS_MODEM_SUPPORT
  268. if (config->enableRxRTS)
  269. {
  270. /* Enable receiver RTS(request-to-send) function. */
  271. base->MODEM |= UART_MODEM_RXRTSE_MASK;
  272. }
  273. if (config->enableTxCTS)
  274. {
  275. /* Enable transmiter CTS(clear-to-send) function. */
  276. base->MODEM |= UART_MODEM_TXCTSE_MASK;
  277. }
  278. #endif
  279. /* Enable TX/RX base on configure structure. */
  280. temp = base->C2;
  281. if (config->enableTx)
  282. {
  283. temp |= UART_C2_TE_MASK;
  284. }
  285. if (config->enableRx)
  286. {
  287. temp |= UART_C2_RE_MASK;
  288. }
  289. base->C2 = temp;
  290. return kStatus_Success;
  291. }
  292. void UART_Deinit(UART_Type *base)
  293. {
  294. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  295. /* Wait tx FIFO send out*/
  296. while (0 != base->TCFIFO)
  297. {
  298. }
  299. #endif
  300. /* Wait last char shoft out */
  301. while (0 == (base->S1 & UART_S1_TC_MASK))
  302. {
  303. }
  304. /* Disable the module. */
  305. base->C2 = 0;
  306. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  307. /* Disable uart clock */
  308. CLOCK_DisableClock(s_uartClock[UART_GetInstance(base)]);
  309. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  310. }
  311. void UART_GetDefaultConfig(uart_config_t *config)
  312. {
  313. assert(config);
  314. config->baudRate_Bps = 115200U;
  315. config->parityMode = kUART_ParityDisabled;
  316. #if defined(FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT) && FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT
  317. config->stopBitCount = kUART_OneStopBit;
  318. #endif
  319. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  320. config->txFifoWatermark = 0;
  321. config->rxFifoWatermark = 1;
  322. #endif
  323. #if defined(FSL_FEATURE_UART_HAS_MODEM_SUPPORT) && FSL_FEATURE_UART_HAS_MODEM_SUPPORT
  324. config->enableRxRTS = false;
  325. config->enableTxCTS = false;
  326. #endif
  327. config->idleType = kUART_IdleTypeStartBit;
  328. config->enableTx = false;
  329. config->enableRx = false;
  330. }
  331. status_t UART_SetBaudRate(UART_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz)
  332. {
  333. assert(baudRate_Bps);
  334. uint16_t sbr = 0;
  335. uint32_t baudDiff = 0;
  336. uint8_t oldCtrl;
  337. /* Calculate the baud rate modulo divisor, sbr*/
  338. sbr = srcClock_Hz / (baudRate_Bps * 16);
  339. /* set sbrTemp to 1 if the sourceClockInHz can not satisfy the desired baud rate */
  340. if (sbr == 0)
  341. {
  342. sbr = 1;
  343. }
  344. #if defined(FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT) && FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT
  345. /* Determine if a fractional divider is needed to fine tune closer to the
  346. * desired baud, each value of brfa is in 1/32 increments,
  347. * hence the multiply-by-32. */
  348. uint32_t tempBaud = 0;
  349. uint16_t brfa = (2 * srcClock_Hz / (baudRate_Bps)) - 32 * sbr;
  350. /* Calculate the baud rate based on the temporary SBR values and BRFA */
  351. tempBaud = (srcClock_Hz * 2 / ((sbr * 32 + brfa)));
  352. baudDiff = (tempBaud > baudRate_Bps) ? (tempBaud - baudRate_Bps) : (baudRate_Bps - tempBaud);
  353. #else
  354. /* Calculate the baud rate based on the temporary SBR values */
  355. baudDiff = (srcClock_Hz / (sbr * 16)) - baudRate_Bps;
  356. /* Select the better value between sbr and (sbr + 1) */
  357. if (baudDiff > (baudRate_Bps - (srcClock_Hz / (16 * (sbr + 1)))))
  358. {
  359. baudDiff = baudRate_Bps - (srcClock_Hz / (16 * (sbr + 1)));
  360. sbr++;
  361. }
  362. #endif
  363. /* next, check to see if actual baud rate is within 3% of desired baud rate
  364. * based on the calculate SBR value */
  365. if (baudDiff < ((baudRate_Bps / 100) * 3))
  366. {
  367. /* Store C2 before disable Tx and Rx */
  368. oldCtrl = base->C2;
  369. /* Disable UART TX RX before setting. */
  370. base->C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK);
  371. /* Write the sbr value to the BDH and BDL registers*/
  372. base->BDH = (base->BDH & ~UART_BDH_SBR_MASK) | (uint8_t)(sbr >> 8);
  373. base->BDL = (uint8_t)sbr;
  374. #if defined(FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT) && FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT
  375. /* Write the brfa value to the register*/
  376. base->C4 = (base->C4 & ~UART_C4_BRFA_MASK) | (brfa & UART_C4_BRFA_MASK);
  377. #endif
  378. /* Restore C2. */
  379. base->C2 = oldCtrl;
  380. return kStatus_Success;
  381. }
  382. else
  383. {
  384. /* Unacceptable baud rate difference of more than 3%*/
  385. return kStatus_UART_BaudrateNotSupport;
  386. }
  387. }
  388. void UART_EnableInterrupts(UART_Type *base, uint32_t mask)
  389. {
  390. mask &= kUART_AllInterruptsEnable;
  391. /* The interrupt mask is combined by control bits from several register: ((CFIFO<<24) | (C3<<16) | (C2<<8) |(BDH))
  392. */
  393. base->BDH |= mask;
  394. base->C2 |= (mask >> 8);
  395. base->C3 |= (mask >> 16);
  396. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  397. base->CFIFO |= (mask >> 24);
  398. #endif
  399. }
  400. void UART_DisableInterrupts(UART_Type *base, uint32_t mask)
  401. {
  402. mask &= kUART_AllInterruptsEnable;
  403. /* The interrupt mask is combined by control bits from several register: ((CFIFO<<24) | (C3<<16) | (C2<<8) |(BDH))
  404. */
  405. base->BDH &= ~mask;
  406. base->C2 &= ~(mask >> 8);
  407. base->C3 &= ~(mask >> 16);
  408. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  409. base->CFIFO &= ~(mask >> 24);
  410. #endif
  411. }
  412. uint32_t UART_GetEnabledInterrupts(UART_Type *base)
  413. {
  414. uint32_t temp;
  415. temp = base->BDH | ((uint32_t)(base->C2) << 8) | ((uint32_t)(base->C3) << 16);
  416. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  417. temp |= ((uint32_t)(base->CFIFO) << 24);
  418. #endif
  419. return temp & kUART_AllInterruptsEnable;
  420. }
  421. uint32_t UART_GetStatusFlags(UART_Type *base)
  422. {
  423. uint32_t status_flag;
  424. status_flag = base->S1 | ((uint32_t)(base->S2) << 8);
  425. #if defined(FSL_FEATURE_UART_HAS_EXTENDED_DATA_REGISTER_FLAGS) && FSL_FEATURE_UART_HAS_EXTENDED_DATA_REGISTER_FLAGS
  426. status_flag |= ((uint32_t)(base->ED) << 16);
  427. #endif
  428. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  429. status_flag |= ((uint32_t)(base->SFIFO) << 24);
  430. #endif
  431. return status_flag;
  432. }
  433. status_t UART_ClearStatusFlags(UART_Type *base, uint32_t mask)
  434. {
  435. uint8_t reg = base->S2;
  436. status_t status;
  437. #if defined(FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT
  438. reg &= ~(UART_S2_RXEDGIF_MASK | UART_S2_LBKDIF_MASK);
  439. #else
  440. reg &= ~UART_S2_RXEDGIF_MASK;
  441. #endif
  442. base->S2 = reg | (uint8_t)(mask >> 8);
  443. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  444. base->SFIFO = (uint8_t)(mask >> 24);
  445. #endif
  446. if (mask & (kUART_IdleLineFlag | kUART_NoiseErrorFlag | kUART_FramingErrorFlag | kUART_ParityErrorFlag))
  447. {
  448. /* Read base->D to clear the flags. */
  449. (void)base->S1;
  450. (void)base->D;
  451. }
  452. if (mask & kUART_RxOverrunFlag)
  453. {
  454. /* Read base->D to clear the flags and Flush all data in FIFO. */
  455. (void)base->S1;
  456. (void)base->D;
  457. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  458. /* Flush FIFO date, otherwise FIFO pointer will be in unknown state. */
  459. base->CFIFO |= UART_CFIFO_RXFLUSH_MASK;
  460. #endif
  461. }
  462. /* If some flags still pending. */
  463. if (mask & UART_GetStatusFlags(base))
  464. {
  465. /* Some flags can only clear or set by the hardware itself, these flags are: kUART_TxDataRegEmptyFlag,
  466. kUART_TransmissionCompleteFlag, kUART_RxDataRegFullFlag, kUART_RxActiveFlag, kUART_NoiseErrorInRxDataRegFlag,
  467. kUART_ParityErrorInRxDataRegFlag, kUART_TxFifoEmptyFlag, kUART_RxFifoEmptyFlag. */
  468. status = kStatus_UART_FlagCannotClearManually;
  469. }
  470. else
  471. {
  472. status = kStatus_Success;
  473. }
  474. return status;
  475. }
  476. void UART_WriteBlocking(UART_Type *base, const uint8_t *data, size_t length)
  477. {
  478. /* This API can only ensure that the data is written into the data buffer but can't
  479. ensure all data in the data buffer are sent into the transmit shift buffer. */
  480. while (length--)
  481. {
  482. while (!(base->S1 & UART_S1_TDRE_MASK))
  483. {
  484. }
  485. base->D = *(data++);
  486. }
  487. }
  488. static void UART_WriteNonBlocking(UART_Type *base, const uint8_t *data, size_t length)
  489. {
  490. assert(data);
  491. size_t i;
  492. /* The Non Blocking write data API assume user have ensured there is enough space in
  493. peripheral to write. */
  494. for (i = 0; i < length; i++)
  495. {
  496. base->D = data[i];
  497. }
  498. }
  499. status_t UART_ReadBlocking(UART_Type *base, uint8_t *data, size_t length)
  500. {
  501. assert(data);
  502. uint32_t statusFlag;
  503. while (length--)
  504. {
  505. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  506. while (!base->RCFIFO)
  507. #else
  508. while (!(base->S1 & UART_S1_RDRF_MASK))
  509. #endif
  510. {
  511. statusFlag = UART_GetStatusFlags(base);
  512. if (statusFlag & kUART_RxOverrunFlag)
  513. {
  514. return kStatus_UART_RxHardwareOverrun;
  515. }
  516. if (statusFlag & kUART_NoiseErrorFlag)
  517. {
  518. return kStatus_UART_NoiseError;
  519. }
  520. if (statusFlag & kUART_FramingErrorFlag)
  521. {
  522. return kStatus_UART_FramingError;
  523. }
  524. if (statusFlag & kUART_ParityErrorFlag)
  525. {
  526. return kStatus_UART_ParityError;
  527. }
  528. }
  529. *(data++) = base->D;
  530. }
  531. return kStatus_Success;
  532. }
  533. static void UART_ReadNonBlocking(UART_Type *base, uint8_t *data, size_t length)
  534. {
  535. assert(data);
  536. size_t i;
  537. /* The Non Blocking read data API assume user have ensured there is enough space in
  538. peripheral to write. */
  539. for (i = 0; i < length; i++)
  540. {
  541. data[i] = base->D;
  542. }
  543. }
  544. void UART_TransferCreateHandle(UART_Type *base,
  545. uart_handle_t *handle,
  546. uart_transfer_callback_t callback,
  547. void *userData)
  548. {
  549. assert(handle);
  550. uint32_t instance;
  551. /* Zero the handle. */
  552. memset(handle, 0, sizeof(*handle));
  553. /* Set the TX/RX state. */
  554. handle->rxState = kUART_RxIdle;
  555. handle->txState = kUART_TxIdle;
  556. /* Set the callback and user data. */
  557. handle->callback = callback;
  558. handle->userData = userData;
  559. /* Get instance from peripheral base address. */
  560. instance = UART_GetInstance(base);
  561. /* Save the handle in global variables to support the double weak mechanism. */
  562. s_uartHandle[instance] = handle;
  563. s_uartIsr = UART_TransferHandleIRQ;
  564. /* Enable interrupt in NVIC. */
  565. EnableIRQ(s_uartIRQ[instance]);
  566. }
  567. void UART_TransferStartRingBuffer(UART_Type *base, uart_handle_t *handle, uint8_t *ringBuffer, size_t ringBufferSize)
  568. {
  569. assert(handle);
  570. assert(ringBuffer);
  571. /* Setup the ringbuffer address */
  572. handle->rxRingBuffer = ringBuffer;
  573. handle->rxRingBufferSize = ringBufferSize;
  574. handle->rxRingBufferHead = 0U;
  575. handle->rxRingBufferTail = 0U;
  576. /* Enable the interrupt to accept the data when user need the ring buffer. */
  577. UART_EnableInterrupts(
  578. base, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable | kUART_FramingErrorInterruptEnable);
  579. /* Enable parity error interrupt when parity mode is enable*/
  580. if (UART_C1_PE_MASK & base->C1)
  581. {
  582. UART_EnableInterrupts(base, kUART_ParityErrorInterruptEnable);
  583. }
  584. }
  585. void UART_TransferStopRingBuffer(UART_Type *base, uart_handle_t *handle)
  586. {
  587. assert(handle);
  588. if (handle->rxState == kUART_RxIdle)
  589. {
  590. UART_DisableInterrupts(base, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable |
  591. kUART_FramingErrorInterruptEnable);
  592. /* Disable parity error interrupt when parity mode is enable*/
  593. if (UART_C1_PE_MASK & base->C1)
  594. {
  595. UART_DisableInterrupts(base, kUART_ParityErrorInterruptEnable);
  596. }
  597. }
  598. handle->rxRingBuffer = NULL;
  599. handle->rxRingBufferSize = 0U;
  600. handle->rxRingBufferHead = 0U;
  601. handle->rxRingBufferTail = 0U;
  602. }
  603. status_t UART_TransferSendNonBlocking(UART_Type *base, uart_handle_t *handle, uart_transfer_t *xfer)
  604. {
  605. assert(handle);
  606. assert(xfer);
  607. assert(xfer->dataSize);
  608. assert(xfer->data);
  609. status_t status;
  610. /* Return error if current TX busy. */
  611. if (kUART_TxBusy == handle->txState)
  612. {
  613. status = kStatus_UART_TxBusy;
  614. }
  615. else
  616. {
  617. handle->txData = xfer->data;
  618. handle->txDataSize = xfer->dataSize;
  619. handle->txDataSizeAll = xfer->dataSize;
  620. handle->txState = kUART_TxBusy;
  621. /* Enable transmiter interrupt. */
  622. UART_EnableInterrupts(base, kUART_TxDataRegEmptyInterruptEnable);
  623. status = kStatus_Success;
  624. }
  625. return status;
  626. }
  627. void UART_TransferAbortSend(UART_Type *base, uart_handle_t *handle)
  628. {
  629. assert(handle);
  630. UART_DisableInterrupts(base, kUART_TxDataRegEmptyInterruptEnable | kUART_TransmissionCompleteInterruptEnable);
  631. handle->txDataSize = 0;
  632. handle->txState = kUART_TxIdle;
  633. }
  634. status_t UART_TransferGetSendCount(UART_Type *base, uart_handle_t *handle, uint32_t *count)
  635. {
  636. assert(handle);
  637. assert(count);
  638. if (kUART_TxIdle == handle->txState)
  639. {
  640. return kStatus_NoTransferInProgress;
  641. }
  642. *count = handle->txDataSizeAll - handle->txDataSize;
  643. return kStatus_Success;
  644. }
  645. status_t UART_TransferReceiveNonBlocking(UART_Type *base,
  646. uart_handle_t *handle,
  647. uart_transfer_t *xfer,
  648. size_t *receivedBytes)
  649. {
  650. assert(handle);
  651. assert(xfer);
  652. assert(xfer->data);
  653. assert(xfer->dataSize);
  654. uint32_t i;
  655. status_t status;
  656. /* How many bytes to copy from ring buffer to user memory. */
  657. size_t bytesToCopy = 0U;
  658. /* How many bytes to receive. */
  659. size_t bytesToReceive;
  660. /* How many bytes currently have received. */
  661. size_t bytesCurrentReceived;
  662. /* How to get data:
  663. 1. If RX ring buffer is not enabled, then save xfer->data and xfer->dataSize
  664. to uart handle, enable interrupt to store received data to xfer->data. When
  665. all data received, trigger callback.
  666. 2. If RX ring buffer is enabled and not empty, get data from ring buffer first.
  667. If there are enough data in ring buffer, copy them to xfer->data and return.
  668. If there are not enough data in ring buffer, copy all of them to xfer->data,
  669. save the xfer->data remained empty space to uart handle, receive data
  670. to this empty space and trigger callback when finished. */
  671. if (kUART_RxBusy == handle->rxState)
  672. {
  673. status = kStatus_UART_RxBusy;
  674. }
  675. else
  676. {
  677. bytesToReceive = xfer->dataSize;
  678. bytesCurrentReceived = 0U;
  679. /* If RX ring buffer is used. */
  680. if (handle->rxRingBuffer)
  681. {
  682. /* Disable UART RX IRQ, protect ring buffer. */
  683. UART_DisableInterrupts(base, kUART_RxDataRegFullInterruptEnable);
  684. /* How many bytes in RX ring buffer currently. */
  685. bytesToCopy = UART_TransferGetRxRingBufferLength(handle);
  686. if (bytesToCopy)
  687. {
  688. bytesToCopy = MIN(bytesToReceive, bytesToCopy);
  689. bytesToReceive -= bytesToCopy;
  690. /* Copy data from ring buffer to user memory. */
  691. for (i = 0U; i < bytesToCopy; i++)
  692. {
  693. xfer->data[bytesCurrentReceived++] = handle->rxRingBuffer[handle->rxRingBufferTail];
  694. /* Wrap to 0. Not use modulo (%) because it might be large and slow. */
  695. if (handle->rxRingBufferTail + 1U == handle->rxRingBufferSize)
  696. {
  697. handle->rxRingBufferTail = 0U;
  698. }
  699. else
  700. {
  701. handle->rxRingBufferTail++;
  702. }
  703. }
  704. }
  705. /* If ring buffer does not have enough data, still need to read more data. */
  706. if (bytesToReceive)
  707. {
  708. /* No data in ring buffer, save the request to UART handle. */
  709. handle->rxData = xfer->data + bytesCurrentReceived;
  710. handle->rxDataSize = bytesToReceive;
  711. handle->rxDataSizeAll = bytesToReceive;
  712. handle->rxState = kUART_RxBusy;
  713. }
  714. /* Enable UART RX IRQ if previously enabled. */
  715. UART_EnableInterrupts(base, kUART_RxDataRegFullInterruptEnable);
  716. /* Call user callback since all data are received. */
  717. if (0 == bytesToReceive)
  718. {
  719. if (handle->callback)
  720. {
  721. handle->callback(base, handle, kStatus_UART_RxIdle, handle->userData);
  722. }
  723. }
  724. }
  725. /* Ring buffer not used. */
  726. else
  727. {
  728. handle->rxData = xfer->data + bytesCurrentReceived;
  729. handle->rxDataSize = bytesToReceive;
  730. handle->rxDataSizeAll = bytesToReceive;
  731. handle->rxState = kUART_RxBusy;
  732. /* Enable RX/Rx overrun/framing error/idle line interrupt. */
  733. UART_EnableInterrupts(base, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable |
  734. kUART_FramingErrorInterruptEnable | kUART_IdleLineInterruptEnable);
  735. /* Enable parity error interrupt when parity mode is enable*/
  736. if (UART_C1_PE_MASK & base->C1)
  737. {
  738. UART_EnableInterrupts(base, kUART_ParityErrorInterruptEnable);
  739. }
  740. }
  741. /* Return the how many bytes have read. */
  742. if (receivedBytes)
  743. {
  744. *receivedBytes = bytesCurrentReceived;
  745. }
  746. status = kStatus_Success;
  747. }
  748. return status;
  749. }
  750. void UART_TransferAbortReceive(UART_Type *base, uart_handle_t *handle)
  751. {
  752. assert(handle);
  753. /* Only abort the receive to handle->rxData, the RX ring buffer is still working. */
  754. if (!handle->rxRingBuffer)
  755. {
  756. /* Disable RX interrupt. */
  757. UART_DisableInterrupts(base, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable |
  758. kUART_FramingErrorInterruptEnable | kUART_IdleLineInterruptEnable);
  759. /* Disable parity error interrupt when parity mode is enable*/
  760. if (UART_C1_PE_MASK & base->C1)
  761. {
  762. UART_DisableInterrupts(base, kUART_ParityErrorInterruptEnable);
  763. }
  764. }
  765. handle->rxDataSize = 0U;
  766. handle->rxState = kUART_RxIdle;
  767. }
  768. status_t UART_TransferGetReceiveCount(UART_Type *base, uart_handle_t *handle, uint32_t *count)
  769. {
  770. assert(handle);
  771. assert(count);
  772. if (kUART_RxIdle == handle->rxState)
  773. {
  774. return kStatus_NoTransferInProgress;
  775. }
  776. if (!count)
  777. {
  778. return kStatus_InvalidArgument;
  779. }
  780. *count = handle->rxDataSizeAll - handle->rxDataSize;
  781. return kStatus_Success;
  782. }
  783. void UART_TransferHandleIRQ(UART_Type *base, uart_handle_t *handle)
  784. {
  785. assert(handle);
  786. uint8_t count;
  787. uint8_t tempCount;
  788. /* If RX framing error */
  789. if (UART_S1_FE_MASK & base->S1)
  790. {
  791. /* Read base->D to clear framing error flag, otherwise the RX does not work. */
  792. while (base->S1 & UART_S1_RDRF_MASK)
  793. {
  794. (void)base->D;
  795. }
  796. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  797. /* Flush FIFO date, otherwise FIFO pointer will be in unknown state. */
  798. base->CFIFO |= UART_CFIFO_RXFLUSH_MASK;
  799. #endif
  800. handle->rxState = kUART_RxFramingError;
  801. handle->rxDataSize = 0U;
  802. /* Trigger callback. */
  803. if (handle->callback)
  804. {
  805. handle->callback(base, handle, kStatus_UART_FramingError, handle->userData);
  806. }
  807. }
  808. /* If RX parity error */
  809. if (UART_S1_PF_MASK & base->S1)
  810. {
  811. /* Read base->D to clear parity error flag, otherwise the RX does not work. */
  812. while (base->S1 & UART_S1_RDRF_MASK)
  813. {
  814. (void)base->D;
  815. }
  816. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  817. /* Flush FIFO date, otherwise FIFO pointer will be in unknown state. */
  818. base->CFIFO |= UART_CFIFO_RXFLUSH_MASK;
  819. #endif
  820. handle->rxState = kUART_RxParityError;
  821. handle->rxDataSize = 0U;
  822. /* Trigger callback. */
  823. if (handle->callback)
  824. {
  825. handle->callback(base, handle, kStatus_UART_ParityError, handle->userData);
  826. }
  827. }
  828. /* If RX overrun. */
  829. if (UART_S1_OR_MASK & base->S1)
  830. {
  831. /* Read base->D to clear overrun flag, otherwise the RX does not work. */
  832. while (base->S1 & UART_S1_RDRF_MASK)
  833. {
  834. (void)base->D;
  835. }
  836. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  837. /* Flush FIFO date, otherwise FIFO pointer will be in unknown state. */
  838. base->CFIFO |= UART_CFIFO_RXFLUSH_MASK;
  839. #endif
  840. /* Trigger callback. */
  841. if (handle->callback)
  842. {
  843. handle->callback(base, handle, kStatus_UART_RxHardwareOverrun, handle->userData);
  844. }
  845. }
  846. /* If IDLE line was detected. */
  847. if ((UART_S1_IDLE_MASK & base->S1) && (UART_C2_ILIE_MASK & base->C2))
  848. {
  849. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  850. /* If still some data in the FIFO, read out these data to user data buffer. */
  851. count = base->RCFIFO;
  852. /* If handle->rxDataSize is not 0, first save data to handle->rxData. */
  853. while ((count) && (handle->rxDataSize))
  854. {
  855. tempCount = MIN(handle->rxDataSize, count);
  856. /* Using non block API to read the data from the registers. */
  857. UART_ReadNonBlocking(base, handle->rxData, tempCount);
  858. handle->rxData += tempCount;
  859. handle->rxDataSize -= tempCount;
  860. count -= tempCount;
  861. /* If all the data required for upper layer is ready, trigger callback. */
  862. if (!handle->rxDataSize)
  863. {
  864. handle->rxState = kUART_RxIdle;
  865. /* Disable RX interrupt/overrun interrupt/fram error/idle line detected interrupt */
  866. UART_DisableInterrupts(base, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable |
  867. kUART_FramingErrorInterruptEnable);
  868. /* Disable parity error interrupt when parity mode is enable*/
  869. if (UART_C1_PE_MASK & base->C1)
  870. {
  871. UART_DisableInterrupts(base, kUART_ParityErrorInterruptEnable);
  872. }
  873. if (handle->callback)
  874. {
  875. handle->callback(base, handle, kStatus_UART_RxIdle, handle->userData);
  876. }
  877. }
  878. }
  879. #endif
  880. /* To clear IDLE, read UART status S1 with IDLE set and then read D.*/
  881. while (UART_S1_IDLE_MASK & base->S1)
  882. {
  883. (void)base->D;
  884. }
  885. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  886. /* Flush FIFO date, otherwise FIFO pointer will be in unknown state. */
  887. base->CFIFO |= UART_CFIFO_RXFLUSH_MASK;
  888. #endif
  889. /* If rxDataSize is 0, disable idle line interrupt.*/
  890. if (!(handle->rxDataSize))
  891. {
  892. UART_DisableInterrupts(base, kUART_IdleLineInterruptEnable);
  893. }
  894. /* If callback is not NULL and rxDataSize is not 0. */
  895. if ((handle->callback) && (handle->rxDataSize))
  896. {
  897. handle->callback(base, handle, kStatus_UART_IdleLineDetected, handle->userData);
  898. }
  899. }
  900. /* Receive data register full */
  901. if ((UART_S1_RDRF_MASK & base->S1) && (UART_C2_RIE_MASK & base->C2))
  902. {
  903. /* Get the size that can be stored into buffer for this interrupt. */
  904. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  905. count = base->RCFIFO;
  906. #else
  907. count = 1;
  908. #endif
  909. /* If handle->rxDataSize is not 0, first save data to handle->rxData. */
  910. while ((count) && (handle->rxDataSize))
  911. {
  912. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  913. tempCount = MIN(handle->rxDataSize, count);
  914. #else
  915. tempCount = 1;
  916. #endif
  917. /* Using non block API to read the data from the registers. */
  918. UART_ReadNonBlocking(base, handle->rxData, tempCount);
  919. handle->rxData += tempCount;
  920. handle->rxDataSize -= tempCount;
  921. count -= tempCount;
  922. /* If all the data required for upper layer is ready, trigger callback. */
  923. if (!handle->rxDataSize)
  924. {
  925. handle->rxState = kUART_RxIdle;
  926. if (handle->callback)
  927. {
  928. handle->callback(base, handle, kStatus_UART_RxIdle, handle->userData);
  929. }
  930. }
  931. }
  932. /* If use RX ring buffer, receive data to ring buffer. */
  933. if (handle->rxRingBuffer)
  934. {
  935. while (count--)
  936. {
  937. /* If RX ring buffer is full, trigger callback to notify over run. */
  938. if (UART_TransferIsRxRingBufferFull(handle))
  939. {
  940. if (handle->callback)
  941. {
  942. handle->callback(base, handle, kStatus_UART_RxRingBufferOverrun, handle->userData);
  943. }
  944. }
  945. /* If ring buffer is still full after callback function, the oldest data is overrided. */
  946. if (UART_TransferIsRxRingBufferFull(handle))
  947. {
  948. /* Increase handle->rxRingBufferTail to make room for new data. */
  949. if (handle->rxRingBufferTail + 1U == handle->rxRingBufferSize)
  950. {
  951. handle->rxRingBufferTail = 0U;
  952. }
  953. else
  954. {
  955. handle->rxRingBufferTail++;
  956. }
  957. }
  958. /* Read data. */
  959. handle->rxRingBuffer[handle->rxRingBufferHead] = base->D;
  960. /* Increase handle->rxRingBufferHead. */
  961. if (handle->rxRingBufferHead + 1U == handle->rxRingBufferSize)
  962. {
  963. handle->rxRingBufferHead = 0U;
  964. }
  965. else
  966. {
  967. handle->rxRingBufferHead++;
  968. }
  969. }
  970. }
  971. else if (!handle->rxDataSize)
  972. {
  973. /* Disable RX interrupt/overrun interrupt/fram error/idle line detected interrupt */
  974. UART_DisableInterrupts(base, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable |
  975. kUART_FramingErrorInterruptEnable);
  976. /* Disable parity error interrupt when parity mode is enable*/
  977. if (UART_C1_PE_MASK & base->C1)
  978. {
  979. UART_DisableInterrupts(base, kUART_ParityErrorInterruptEnable);
  980. }
  981. }
  982. else
  983. {
  984. }
  985. }
  986. /* If framing error or parity error happened, stop the RX interrupt when ues no ring buffer */
  987. if (((handle->rxState == kUART_RxFramingError) || (handle->rxState == kUART_RxParityError)) &&
  988. (!handle->rxRingBuffer))
  989. {
  990. UART_DisableInterrupts(base, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable |
  991. kUART_FramingErrorInterruptEnable | kUART_IdleLineInterruptEnable);
  992. /* Disable parity error interrupt when parity mode is enable*/
  993. if (UART_C1_PE_MASK & base->C1)
  994. {
  995. UART_DisableInterrupts(base, kUART_ParityErrorInterruptEnable);
  996. }
  997. }
  998. /* Send data register empty and the interrupt is enabled. */
  999. if ((base->S1 & UART_S1_TDRE_MASK) && (base->C2 & UART_C2_TIE_MASK))
  1000. {
  1001. /* Get the bytes that available at this moment. */
  1002. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  1003. count = FSL_FEATURE_UART_FIFO_SIZEn(base) - base->TCFIFO;
  1004. #else
  1005. count = 1;
  1006. #endif
  1007. while ((count) && (handle->txDataSize))
  1008. {
  1009. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  1010. tempCount = MIN(handle->txDataSize, count);
  1011. #else
  1012. tempCount = 1;
  1013. #endif
  1014. /* Using non block API to write the data to the registers. */
  1015. UART_WriteNonBlocking(base, handle->txData, tempCount);
  1016. handle->txData += tempCount;
  1017. handle->txDataSize -= tempCount;
  1018. count -= tempCount;
  1019. /* If all the data are written to data register, TX finished. */
  1020. if (!handle->txDataSize)
  1021. {
  1022. handle->txState = kUART_TxIdle;
  1023. /* Disable TX register empty interrupt. */
  1024. base->C2 = (base->C2 & ~UART_C2_TIE_MASK);
  1025. /* Trigger callback. */
  1026. if (handle->callback)
  1027. {
  1028. handle->callback(base, handle, kStatus_UART_TxIdle, handle->userData);
  1029. }
  1030. }
  1031. }
  1032. }
  1033. }
  1034. void UART_TransferHandleErrorIRQ(UART_Type *base, uart_handle_t *handle)
  1035. {
  1036. /* To be implemented by User. */
  1037. }
  1038. #if defined(UART0)
  1039. #if ((!(defined(FSL_FEATURE_SOC_LPSCI_COUNT))) || \
  1040. ((defined(FSL_FEATURE_SOC_LPSCI_COUNT)) && (FSL_FEATURE_SOC_LPSCI_COUNT == 0)))
  1041. void UART0_DriverIRQHandler(void)
  1042. {
  1043. s_uartIsr(UART0, s_uartHandle[0]);
  1044. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1045. exception return operation might vector to incorrect interrupt */
  1046. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1047. __DSB();
  1048. #endif
  1049. }
  1050. void UART0_RX_TX_DriverIRQHandler(void)
  1051. {
  1052. UART0_DriverIRQHandler();
  1053. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1054. exception return operation might vector to incorrect interrupt */
  1055. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1056. __DSB();
  1057. #endif
  1058. }
  1059. #endif
  1060. #endif
  1061. #if defined(UART1)
  1062. void UART1_DriverIRQHandler(void)
  1063. {
  1064. s_uartIsr(UART1, s_uartHandle[1]);
  1065. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1066. exception return operation might vector to incorrect interrupt */
  1067. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1068. __DSB();
  1069. #endif
  1070. }
  1071. void UART1_RX_TX_DriverIRQHandler(void)
  1072. {
  1073. UART1_DriverIRQHandler();
  1074. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1075. exception return operation might vector to incorrect interrupt */
  1076. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1077. __DSB();
  1078. #endif
  1079. }
  1080. #endif
  1081. #if defined(UART2)
  1082. void UART2_DriverIRQHandler(void)
  1083. {
  1084. s_uartIsr(UART2, s_uartHandle[2]);
  1085. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1086. exception return operation might vector to incorrect interrupt */
  1087. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1088. __DSB();
  1089. #endif
  1090. }
  1091. void UART2_RX_TX_DriverIRQHandler(void)
  1092. {
  1093. UART2_DriverIRQHandler();
  1094. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1095. exception return operation might vector to incorrect interrupt */
  1096. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1097. __DSB();
  1098. #endif
  1099. }
  1100. #endif
  1101. #if defined(UART3)
  1102. void UART3_DriverIRQHandler(void)
  1103. {
  1104. s_uartIsr(UART3, s_uartHandle[3]);
  1105. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1106. exception return operation might vector to incorrect interrupt */
  1107. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1108. __DSB();
  1109. #endif
  1110. }
  1111. void UART3_RX_TX_DriverIRQHandler(void)
  1112. {
  1113. UART3_DriverIRQHandler();
  1114. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1115. exception return operation might vector to incorrect interrupt */
  1116. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1117. __DSB();
  1118. #endif
  1119. }
  1120. #endif
  1121. #if defined(UART4)
  1122. void UART4_DriverIRQHandler(void)
  1123. {
  1124. s_uartIsr(UART4, s_uartHandle[4]);
  1125. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1126. exception return operation might vector to incorrect interrupt */
  1127. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1128. __DSB();
  1129. #endif
  1130. }
  1131. void UART4_RX_TX_DriverIRQHandler(void)
  1132. {
  1133. UART4_DriverIRQHandler();
  1134. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1135. exception return operation might vector to incorrect interrupt */
  1136. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1137. __DSB();
  1138. #endif
  1139. }
  1140. #endif
  1141. #if defined(UART5)
  1142. void UART5_DriverIRQHandler(void)
  1143. {
  1144. s_uartIsr(UART5, s_uartHandle[5]);
  1145. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1146. exception return operation might vector to incorrect interrupt */
  1147. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1148. __DSB();
  1149. #endif
  1150. }
  1151. void UART5_RX_TX_DriverIRQHandler(void)
  1152. {
  1153. UART5_DriverIRQHandler();
  1154. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  1155. exception return operation might vector to incorrect interrupt */
  1156. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  1157. __DSB();
  1158. #endif
  1159. }
  1160. #endif