fsl_lpuart.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283
  1. /*
  2. * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2021 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_lpuart.h"
  9. /*******************************************************************************
  10. * Definitions
  11. ******************************************************************************/
  12. /* Component ID definition, used by tools. */
  13. #ifndef FSL_COMPONENT_ID
  14. #define FSL_COMPONENT_ID "platform.drivers.lpuart"
  15. #endif
  16. /* LPUART transfer state. */
  17. enum
  18. {
  19. kLPUART_TxIdle, /*!< TX idle. */
  20. kLPUART_TxBusy, /*!< TX busy. */
  21. kLPUART_RxIdle, /*!< RX idle. */
  22. kLPUART_RxBusy /*!< RX busy. */
  23. };
  24. /*******************************************************************************
  25. * Prototypes
  26. ******************************************************************************/
  27. /*!
  28. * @brief Check whether the RX ring buffer is full.
  29. *
  30. * @userData handle LPUART handle pointer.
  31. * @retval true RX ring buffer is full.
  32. * @retval false RX ring buffer is not full.
  33. */
  34. static bool LPUART_TransferIsRxRingBufferFull(LPUART_Type *base, lpuart_handle_t *handle);
  35. /*!
  36. * @brief Write to TX register using non-blocking method.
  37. *
  38. * This function writes data to the TX register directly, upper layer must make
  39. * sure the TX register is empty or TX FIFO has empty room before calling this function.
  40. *
  41. * @note This function does not check whether all the data has been sent out to bus,
  42. * so before disable TX, check kLPUART_TransmissionCompleteFlag to ensure the TX is
  43. * finished.
  44. *
  45. * @param base LPUART peripheral base address.
  46. * @param data Start address of the data to write.
  47. * @param length Size of the buffer to be sent.
  48. */
  49. static void LPUART_WriteNonBlocking(LPUART_Type *base, const uint8_t *data, size_t length);
  50. /*!
  51. * @brief Read RX register using non-blocking method.
  52. *
  53. * This function reads data from the TX register directly, upper layer must make
  54. * sure the RX register is full or TX FIFO has data before calling this function.
  55. *
  56. * @param base LPUART peripheral base address.
  57. * @param data Start address of the buffer to store the received data.
  58. * @param length Size of the buffer.
  59. */
  60. static void LPUART_ReadNonBlocking(LPUART_Type *base, uint8_t *data, size_t length);
  61. /*******************************************************************************
  62. * Variables
  63. ******************************************************************************/
  64. /* Array of LPUART peripheral base address. */
  65. static LPUART_Type *const s_lpuartBases[] = LPUART_BASE_PTRS;
  66. /* Array of LPUART handle. */
  67. void *s_lpuartHandle[ARRAY_SIZE(s_lpuartBases)];
  68. /* Array of LPUART IRQ number. */
  69. #if defined(FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ) && FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ
  70. static const IRQn_Type s_lpuartRxIRQ[] = LPUART_RX_IRQS;
  71. const IRQn_Type s_lpuartTxIRQ[] = LPUART_TX_IRQS;
  72. #else
  73. const IRQn_Type s_lpuartIRQ[] = LPUART_RX_TX_IRQS;
  74. #endif
  75. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  76. /* Array of LPUART clock name. */
  77. static const clock_ip_name_t s_lpuartClock[] = LPUART_CLOCKS;
  78. #if defined(LPUART_PERIPH_CLOCKS)
  79. /* Array of LPUART functional clock name. */
  80. static const clock_ip_name_t s_lpuartPeriphClocks[] = LPUART_PERIPH_CLOCKS;
  81. #endif
  82. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  83. /* LPUART ISR for transactional APIs. */
  84. #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
  85. lpuart_isr_t s_lpuartIsr = (lpuart_isr_t)DefaultISR;
  86. #else
  87. lpuart_isr_t s_lpuartIsr;
  88. #endif
  89. /*******************************************************************************
  90. * Code
  91. ******************************************************************************/
  92. /*!
  93. * brief Get the LPUART instance from peripheral base address.
  94. *
  95. * param base LPUART peripheral base address.
  96. * return LPUART instance.
  97. */
  98. uint32_t LPUART_GetInstance(LPUART_Type *base)
  99. {
  100. uint32_t instance;
  101. /* Find the instance index from base address mappings. */
  102. for (instance = 0U; instance < ARRAY_SIZE(s_lpuartBases); instance++)
  103. {
  104. if (s_lpuartBases[instance] == base)
  105. {
  106. break;
  107. }
  108. }
  109. assert(instance < ARRAY_SIZE(s_lpuartBases));
  110. return instance;
  111. }
  112. /*!
  113. * brief Get the length of received data in RX ring buffer.
  114. *
  115. * userData handle LPUART handle pointer.
  116. * return Length of received data in RX ring buffer.
  117. */
  118. size_t LPUART_TransferGetRxRingBufferLength(LPUART_Type *base, lpuart_handle_t *handle)
  119. {
  120. assert(NULL != handle);
  121. size_t size;
  122. size_t tmpRxRingBufferSize = handle->rxRingBufferSize;
  123. uint16_t tmpRxRingBufferTail = handle->rxRingBufferTail;
  124. uint16_t tmpRxRingBufferHead = handle->rxRingBufferHead;
  125. if (tmpRxRingBufferTail > tmpRxRingBufferHead)
  126. {
  127. size = ((size_t)tmpRxRingBufferHead + tmpRxRingBufferSize - (size_t)tmpRxRingBufferTail);
  128. }
  129. else
  130. {
  131. size = ((size_t)tmpRxRingBufferHead - (size_t)tmpRxRingBufferTail);
  132. }
  133. return size;
  134. }
  135. static bool LPUART_TransferIsRxRingBufferFull(LPUART_Type *base, lpuart_handle_t *handle)
  136. {
  137. assert(NULL != handle);
  138. bool full;
  139. if (LPUART_TransferGetRxRingBufferLength(base, handle) == (handle->rxRingBufferSize - 1U))
  140. {
  141. full = true;
  142. }
  143. else
  144. {
  145. full = false;
  146. }
  147. return full;
  148. }
  149. static void LPUART_WriteNonBlocking(LPUART_Type *base, const uint8_t *data, size_t length)
  150. {
  151. assert(NULL != data);
  152. size_t i;
  153. /* The Non Blocking write data API assume user have ensured there is enough space in
  154. peripheral to write. */
  155. for (i = 0; i < length; i++)
  156. {
  157. base->DATA = data[i];
  158. }
  159. }
  160. static void LPUART_ReadNonBlocking(LPUART_Type *base, uint8_t *data, size_t length)
  161. {
  162. assert(NULL != data);
  163. size_t i;
  164. #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
  165. uint32_t ctrl = base->CTRL;
  166. bool isSevenDataBits = (((ctrl & LPUART_CTRL_M7_MASK) != 0U) ||
  167. (((ctrl & LPUART_CTRL_M_MASK) == 0U) && ((ctrl & LPUART_CTRL_PE_MASK) != 0U)));
  168. #endif
  169. /* The Non Blocking read data API assume user have ensured there is enough space in
  170. peripheral to write. */
  171. for (i = 0; i < length; i++)
  172. {
  173. #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
  174. if (isSevenDataBits)
  175. {
  176. data[i] = (uint8_t)(base->DATA & 0x7FU);
  177. }
  178. else
  179. {
  180. data[i] = (uint8_t)base->DATA;
  181. }
  182. #else
  183. data[i] = (uint8_t)(base->DATA);
  184. #endif
  185. }
  186. }
  187. /*!
  188. * brief Initializes an LPUART instance with the user configuration structure and the peripheral clock.
  189. *
  190. * This function configures the LPUART module with user-defined settings. Call the LPUART_GetDefaultConfig() function
  191. * to configure the configuration structure and get the default configuration.
  192. * The example below shows how to use this API to configure the LPUART.
  193. * code
  194. * lpuart_config_t lpuartConfig;
  195. * lpuartConfig.baudRate_Bps = 115200U;
  196. * lpuartConfig.parityMode = kLPUART_ParityDisabled;
  197. * lpuartConfig.dataBitsCount = kLPUART_EightDataBits;
  198. * lpuartConfig.isMsb = false;
  199. * lpuartConfig.stopBitCount = kLPUART_OneStopBit;
  200. * lpuartConfig.txFifoWatermark = 0;
  201. * lpuartConfig.rxFifoWatermark = 1;
  202. * LPUART_Init(LPUART1, &lpuartConfig, 20000000U);
  203. * endcode
  204. *
  205. * param base LPUART peripheral base address.
  206. * param config Pointer to a user-defined configuration structure.
  207. * param srcClock_Hz LPUART clock source frequency in HZ.
  208. * retval kStatus_LPUART_BaudrateNotSupport Baudrate is not support in current clock source.
  209. * retval kStatus_Success LPUART initialize succeed
  210. */
  211. status_t LPUART_Init(LPUART_Type *base, const lpuart_config_t *config, uint32_t srcClock_Hz)
  212. {
  213. assert(NULL != config);
  214. assert(0U < config->baudRate_Bps);
  215. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  216. assert((uint8_t)FSL_FEATURE_LPUART_FIFO_SIZEn(base) > config->txFifoWatermark);
  217. assert((uint8_t)FSL_FEATURE_LPUART_FIFO_SIZEn(base) > config->rxFifoWatermark);
  218. #endif
  219. status_t status = kStatus_Success;
  220. uint32_t temp;
  221. uint16_t sbr, sbrTemp;
  222. uint8_t osr, osrTemp;
  223. uint32_t tempDiff, calculatedBaud, baudDiff;
  224. /* This LPUART instantiation uses a slightly different baud rate calculation
  225. * The idea is to use the best OSR (over-sampling rate) possible
  226. * Note, OSR is typically hard-set to 16 in other LPUART instantiations
  227. * loop to find the best OSR value possible, one that generates minimum baudDiff
  228. * iterate through the rest of the supported values of OSR */
  229. baudDiff = config->baudRate_Bps;
  230. osr = 0U;
  231. sbr = 0U;
  232. for (osrTemp = 4U; osrTemp <= 32U; osrTemp++)
  233. {
  234. /* calculate the temporary sbr value */
  235. sbrTemp = (uint16_t)((srcClock_Hz * 10U / (config->baudRate_Bps * (uint32_t)osrTemp) + 5U) / 10U);
  236. /*set sbrTemp to 1 if the sourceClockInHz can not satisfy the desired baud rate*/
  237. if (sbrTemp == 0U)
  238. {
  239. sbrTemp = 1U;
  240. }
  241. /* Calculate the baud rate based on the temporary OSR and SBR values */
  242. calculatedBaud = (srcClock_Hz / ((uint32_t)osrTemp * (uint32_t)sbrTemp));
  243. tempDiff = calculatedBaud > config->baudRate_Bps ? (calculatedBaud - config->baudRate_Bps) :
  244. (config->baudRate_Bps - calculatedBaud);
  245. if (tempDiff <= baudDiff)
  246. {
  247. baudDiff = tempDiff;
  248. osr = osrTemp; /* update and store the best OSR value calculated */
  249. sbr = sbrTemp; /* update store the best SBR value calculated */
  250. }
  251. }
  252. /* Check to see if actual baud rate is within 3% of desired baud rate
  253. * based on the best calculate OSR value */
  254. if (baudDiff > ((config->baudRate_Bps / 100U) * 3U))
  255. {
  256. /* Unacceptable baud rate difference of more than 3%*/
  257. status = kStatus_LPUART_BaudrateNotSupport;
  258. }
  259. else
  260. {
  261. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  262. uint32_t instance = LPUART_GetInstance(base);
  263. /* Enable lpuart clock */
  264. (void)CLOCK_EnableClock(s_lpuartClock[instance]);
  265. #if defined(LPUART_PERIPH_CLOCKS)
  266. (void)CLOCK_EnableClock(s_lpuartPeriphClocks[instance]);
  267. #endif
  268. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  269. #if defined(FSL_FEATURE_LPUART_HAS_GLOBAL) && FSL_FEATURE_LPUART_HAS_GLOBAL
  270. /*Reset all internal logic and registers, except the Global Register */
  271. LPUART_SoftwareReset(base);
  272. #else
  273. /* Disable LPUART TX RX before setting. */
  274. base->CTRL &= ~(LPUART_CTRL_TE_MASK | LPUART_CTRL_RE_MASK);
  275. #endif
  276. temp = base->BAUD;
  277. /* Acceptable baud rate, check if OSR is between 4x and 7x oversampling.
  278. * If so, then "BOTHEDGE" sampling must be turned on */
  279. if ((osr > 3U) && (osr < 8U))
  280. {
  281. temp |= LPUART_BAUD_BOTHEDGE_MASK;
  282. }
  283. /* program the osr value (bit value is one less than actual value) */
  284. temp &= ~LPUART_BAUD_OSR_MASK;
  285. temp |= LPUART_BAUD_OSR((uint32_t)osr - 1UL);
  286. /* write the sbr value to the BAUD registers */
  287. temp &= ~LPUART_BAUD_SBR_MASK;
  288. base->BAUD = temp | LPUART_BAUD_SBR(sbr);
  289. /* Set bit count and parity mode. */
  290. base->BAUD &= ~LPUART_BAUD_M10_MASK;
  291. temp = base->CTRL & ~(LPUART_CTRL_PE_MASK | LPUART_CTRL_PT_MASK | LPUART_CTRL_M_MASK | LPUART_CTRL_ILT_MASK |
  292. LPUART_CTRL_IDLECFG_MASK);
  293. temp |= (uint8_t)config->parityMode | LPUART_CTRL_IDLECFG(config->rxIdleConfig) |
  294. LPUART_CTRL_ILT(config->rxIdleType);
  295. #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
  296. if (kLPUART_SevenDataBits == config->dataBitsCount)
  297. {
  298. if (kLPUART_ParityDisabled != config->parityMode)
  299. {
  300. temp &= ~LPUART_CTRL_M7_MASK; /* Seven data bits and one parity bit */
  301. }
  302. else
  303. {
  304. temp |= LPUART_CTRL_M7_MASK;
  305. }
  306. }
  307. else
  308. #endif
  309. {
  310. if (kLPUART_ParityDisabled != config->parityMode)
  311. {
  312. temp |= LPUART_CTRL_M_MASK; /* Eight data bits and one parity bit */
  313. }
  314. }
  315. base->CTRL = temp;
  316. #if defined(FSL_FEATURE_LPUART_HAS_STOP_BIT_CONFIG_SUPPORT) && FSL_FEATURE_LPUART_HAS_STOP_BIT_CONFIG_SUPPORT
  317. /* set stop bit per char */
  318. temp = base->BAUD & ~LPUART_BAUD_SBNS_MASK;
  319. base->BAUD = temp | LPUART_BAUD_SBNS((uint8_t)config->stopBitCount);
  320. #endif
  321. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  322. /* Set tx/rx WATER watermark
  323. Note:
  324. Take care of the RX FIFO, RX interrupt request only assert when received bytes
  325. equal or more than RX water mark, there is potential issue if RX water
  326. mark larger than 1.
  327. For example, if RX FIFO water mark is 2, upper layer needs 5 bytes and
  328. 5 bytes are received. the last byte will be saved in FIFO but not trigger
  329. RX interrupt because the water mark is 2.
  330. */
  331. base->WATER = (((uint32_t)(config->rxFifoWatermark) << 16U) | config->txFifoWatermark);
  332. /* Enable tx/rx FIFO */
  333. base->FIFO |= (LPUART_FIFO_TXFE_MASK | LPUART_FIFO_RXFE_MASK);
  334. /* Flush FIFO */
  335. base->FIFO |= (LPUART_FIFO_TXFLUSH_MASK | LPUART_FIFO_RXFLUSH_MASK);
  336. #endif
  337. /* Clear all status flags */
  338. temp = (LPUART_STAT_RXEDGIF_MASK | LPUART_STAT_IDLE_MASK | LPUART_STAT_OR_MASK | LPUART_STAT_NF_MASK |
  339. LPUART_STAT_FE_MASK | LPUART_STAT_PF_MASK);
  340. #if defined(FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT
  341. temp |= LPUART_STAT_LBKDIF_MASK;
  342. #endif
  343. #if defined(FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING) && FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING
  344. temp |= (LPUART_STAT_MA1F_MASK | LPUART_STAT_MA2F_MASK);
  345. #endif
  346. #if defined(FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT) && FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT
  347. /* Set the CTS configuration/TX CTS source. */
  348. base->MODIR |= LPUART_MODIR_TXCTSC(config->txCtsConfig) | LPUART_MODIR_TXCTSSRC(config->txCtsSource);
  349. if (true == config->enableRxRTS)
  350. {
  351. /* Enable the receiver RTS(request-to-send) function. */
  352. base->MODIR |= LPUART_MODIR_RXRTSE_MASK;
  353. }
  354. if (true == config->enableTxCTS)
  355. {
  356. /* Enable the CTS(clear-to-send) function. */
  357. base->MODIR |= LPUART_MODIR_TXCTSE_MASK;
  358. }
  359. #endif
  360. /* Set data bits order. */
  361. if (true == config->isMsb)
  362. {
  363. temp |= LPUART_STAT_MSBF_MASK;
  364. }
  365. else
  366. {
  367. temp &= ~LPUART_STAT_MSBF_MASK;
  368. }
  369. base->STAT |= temp;
  370. /* Enable TX/RX base on configure structure. */
  371. temp = base->CTRL;
  372. if (true == config->enableTx)
  373. {
  374. temp |= LPUART_CTRL_TE_MASK;
  375. }
  376. if (true == config->enableRx)
  377. {
  378. temp |= LPUART_CTRL_RE_MASK;
  379. }
  380. base->CTRL = temp;
  381. }
  382. return status;
  383. }
  384. /*!
  385. * brief Deinitializes a LPUART instance.
  386. *
  387. * This function waits for transmit to complete, disables TX and RX, and disables the LPUART clock.
  388. *
  389. * param base LPUART peripheral base address.
  390. */
  391. void LPUART_Deinit(LPUART_Type *base)
  392. {
  393. uint32_t temp;
  394. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  395. /* Wait tx FIFO send out*/
  396. while (0U != ((base->WATER & LPUART_WATER_TXCOUNT_MASK) >> LPUART_WATER_TXWATER_SHIFT))
  397. {
  398. }
  399. #endif
  400. /* Wait last char shift out */
  401. while (0U == (base->STAT & LPUART_STAT_TC_MASK))
  402. {
  403. }
  404. /* Clear all status flags */
  405. temp = (LPUART_STAT_RXEDGIF_MASK | LPUART_STAT_IDLE_MASK | LPUART_STAT_OR_MASK | LPUART_STAT_NF_MASK |
  406. LPUART_STAT_FE_MASK | LPUART_STAT_PF_MASK);
  407. #if defined(FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT
  408. temp |= LPUART_STAT_LBKDIF_MASK;
  409. #endif
  410. #if defined(FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING) && FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING
  411. temp |= (LPUART_STAT_MA1F_MASK | LPUART_STAT_MA2F_MASK);
  412. #endif
  413. base->STAT |= temp;
  414. /* Disable the module. */
  415. base->CTRL = 0U;
  416. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  417. uint32_t instance = LPUART_GetInstance(base);
  418. /* Disable lpuart clock */
  419. (void)CLOCK_DisableClock(s_lpuartClock[instance]);
  420. #if defined(LPUART_PERIPH_CLOCKS)
  421. (void)CLOCK_DisableClock(s_lpuartPeriphClocks[instance]);
  422. #endif
  423. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  424. }
  425. /*!
  426. * brief Gets the default configuration structure.
  427. *
  428. * This function initializes the LPUART configuration structure to a default value. The default
  429. * values are:
  430. * lpuartConfig->baudRate_Bps = 115200U;
  431. * lpuartConfig->parityMode = kLPUART_ParityDisabled;
  432. * lpuartConfig->dataBitsCount = kLPUART_EightDataBits;
  433. * lpuartConfig->isMsb = false;
  434. * lpuartConfig->stopBitCount = kLPUART_OneStopBit;
  435. * lpuartConfig->txFifoWatermark = 0;
  436. * lpuartConfig->rxFifoWatermark = 1;
  437. * lpuartConfig->rxIdleType = kLPUART_IdleTypeStartBit;
  438. * lpuartConfig->rxIdleConfig = kLPUART_IdleCharacter1;
  439. * lpuartConfig->enableTx = false;
  440. * lpuartConfig->enableRx = false;
  441. *
  442. * param config Pointer to a configuration structure.
  443. */
  444. void LPUART_GetDefaultConfig(lpuart_config_t *config)
  445. {
  446. assert(NULL != config);
  447. /* Initializes the configure structure to zero. */
  448. (void)memset(config, 0, sizeof(*config));
  449. config->baudRate_Bps = 115200U;
  450. config->parityMode = kLPUART_ParityDisabled;
  451. config->dataBitsCount = kLPUART_EightDataBits;
  452. config->isMsb = false;
  453. #if defined(FSL_FEATURE_LPUART_HAS_STOP_BIT_CONFIG_SUPPORT) && FSL_FEATURE_LPUART_HAS_STOP_BIT_CONFIG_SUPPORT
  454. config->stopBitCount = kLPUART_OneStopBit;
  455. #endif
  456. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  457. config->txFifoWatermark = 0U;
  458. config->rxFifoWatermark = 0U;
  459. #endif
  460. #if defined(FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT) && FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT
  461. config->enableRxRTS = false;
  462. config->enableTxCTS = false;
  463. config->txCtsConfig = kLPUART_CtsSampleAtStart;
  464. config->txCtsSource = kLPUART_CtsSourcePin;
  465. #endif
  466. config->rxIdleType = kLPUART_IdleTypeStartBit;
  467. config->rxIdleConfig = kLPUART_IdleCharacter1;
  468. config->enableTx = false;
  469. config->enableRx = false;
  470. }
  471. /*!
  472. * brief Sets the LPUART instance baudrate.
  473. *
  474. * This function configures the LPUART module baudrate. This function is used to update
  475. * the LPUART module baudrate after the LPUART module is initialized by the LPUART_Init.
  476. * code
  477. * LPUART_SetBaudRate(LPUART1, 115200U, 20000000U);
  478. * endcode
  479. *
  480. * param base LPUART peripheral base address.
  481. * param baudRate_Bps LPUART baudrate to be set.
  482. * param srcClock_Hz LPUART clock source frequency in HZ.
  483. * retval kStatus_LPUART_BaudrateNotSupport Baudrate is not supported in the current clock source.
  484. * retval kStatus_Success Set baudrate succeeded.
  485. */
  486. status_t LPUART_SetBaudRate(LPUART_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz)
  487. {
  488. assert(0U < baudRate_Bps);
  489. status_t status = kStatus_Success;
  490. uint32_t temp, oldCtrl;
  491. uint16_t sbr, sbrTemp;
  492. uint8_t osr, osrTemp;
  493. uint32_t tempDiff, calculatedBaud, baudDiff;
  494. /* This LPUART instantiation uses a slightly different baud rate calculation
  495. * The idea is to use the best OSR (over-sampling rate) possible
  496. * Note, OSR is typically hard-set to 16 in other LPUART instantiations
  497. * loop to find the best OSR value possible, one that generates minimum baudDiff
  498. * iterate through the rest of the supported values of OSR */
  499. baudDiff = baudRate_Bps;
  500. osr = 0U;
  501. sbr = 0U;
  502. for (osrTemp = 4U; osrTemp <= 32U; osrTemp++)
  503. {
  504. /* calculate the temporary sbr value */
  505. sbrTemp = (uint16_t)((srcClock_Hz * 10U / (baudRate_Bps * (uint32_t)osrTemp) + 5U) / 10U);
  506. /*set sbrTemp to 1 if the sourceClockInHz can not satisfy the desired baud rate*/
  507. if (sbrTemp == 0U)
  508. {
  509. sbrTemp = 1U;
  510. }
  511. /* Calculate the baud rate based on the temporary OSR and SBR values */
  512. calculatedBaud = srcClock_Hz / ((uint32_t)osrTemp * (uint32_t)sbrTemp);
  513. tempDiff = calculatedBaud > baudRate_Bps ? (calculatedBaud - baudRate_Bps) : (baudRate_Bps - calculatedBaud);
  514. if (tempDiff <= baudDiff)
  515. {
  516. baudDiff = tempDiff;
  517. osr = osrTemp; /* update and store the best OSR value calculated */
  518. sbr = sbrTemp; /* update store the best SBR value calculated */
  519. }
  520. }
  521. /* Check to see if actual baud rate is within 3% of desired baud rate
  522. * based on the best calculate OSR value */
  523. if (baudDiff < (uint32_t)((baudRate_Bps / 100U) * 3U))
  524. {
  525. /* Store CTRL before disable Tx and Rx */
  526. oldCtrl = base->CTRL;
  527. /* Disable LPUART TX RX before setting. */
  528. base->CTRL &= ~(LPUART_CTRL_TE_MASK | LPUART_CTRL_RE_MASK);
  529. temp = base->BAUD;
  530. /* Acceptable baud rate, check if OSR is between 4x and 7x oversampling.
  531. * If so, then "BOTHEDGE" sampling must be turned on */
  532. if ((osr > 3U) && (osr < 8U))
  533. {
  534. temp |= LPUART_BAUD_BOTHEDGE_MASK;
  535. }
  536. /* program the osr value (bit value is one less than actual value) */
  537. temp &= ~LPUART_BAUD_OSR_MASK;
  538. temp |= LPUART_BAUD_OSR((uint32_t)osr - 1UL);
  539. /* write the sbr value to the BAUD registers */
  540. temp &= ~LPUART_BAUD_SBR_MASK;
  541. base->BAUD = temp | LPUART_BAUD_SBR(sbr);
  542. /* Restore CTRL. */
  543. base->CTRL = oldCtrl;
  544. }
  545. else
  546. {
  547. /* Unacceptable baud rate difference of more than 3%*/
  548. status = kStatus_LPUART_BaudrateNotSupport;
  549. }
  550. return status;
  551. }
  552. /*!
  553. * brief Enable 9-bit data mode for LPUART.
  554. *
  555. * This function set the 9-bit mode for LPUART module. The 9th bit is not used for parity thus can be modified by user.
  556. *
  557. * param base LPUART peripheral base address.
  558. * param enable true to enable, flase to disable.
  559. */
  560. void LPUART_Enable9bitMode(LPUART_Type *base, bool enable)
  561. {
  562. assert(base != NULL);
  563. uint32_t temp = 0U;
  564. if (enable)
  565. {
  566. /* Set LPUART_CTRL_M for 9-bit mode, clear LPUART_CTRL_PE to disable parity. */
  567. temp = base->CTRL & ~((uint32_t)LPUART_CTRL_PE_MASK | (uint32_t)LPUART_CTRL_M_MASK);
  568. temp |= (uint32_t)LPUART_CTRL_M_MASK;
  569. base->CTRL = temp;
  570. }
  571. else
  572. {
  573. /* Clear LPUART_CTRL_M. */
  574. base->CTRL &= ~(uint32_t)LPUART_CTRL_M_MASK;
  575. }
  576. #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
  577. /* Clear LPUART_CTRL_M7 to disable 7-bit mode. */
  578. base->CTRL &= ~(uint32_t)LPUART_CTRL_M7_MASK;
  579. #endif
  580. #if defined(FSL_FEATURE_LPUART_HAS_10BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_10BIT_DATA_SUPPORT
  581. /* Clear LPUART_BAUD_M10 to disable 10-bit mode. */
  582. base->BAUD &= ~(uint32_t)LPUART_BAUD_M10_MASK;
  583. #endif
  584. }
  585. /*!
  586. * brief Transmit an address frame in 9-bit data mode.
  587. *
  588. * param base LPUART peripheral base address.
  589. * param address LPUART slave address.
  590. */
  591. void LPUART_SendAddress(LPUART_Type *base, uint8_t address)
  592. {
  593. assert(base != NULL);
  594. uint32_t temp = base->DATA & 0xFFFFFC00UL;
  595. temp |= ((uint32_t)address | (1UL << LPUART_DATA_R8T8_SHIFT));
  596. base->DATA = temp;
  597. }
  598. /*!
  599. * brief Enables LPUART interrupts according to a provided mask.
  600. *
  601. * This function enables the LPUART interrupts according to a provided mask. The mask
  602. * is a logical OR of enumeration members. See the ref _lpuart_interrupt_enable.
  603. * This examples shows how to enable TX empty interrupt and RX full interrupt:
  604. * code
  605. * LPUART_EnableInterrupts(LPUART1,kLPUART_TxDataRegEmptyInterruptEnable | kLPUART_RxDataRegFullInterruptEnable);
  606. * endcode
  607. *
  608. * param base LPUART peripheral base address.
  609. * param mask The interrupts to enable. Logical OR of ref _uart_interrupt_enable.
  610. */
  611. void LPUART_EnableInterrupts(LPUART_Type *base, uint32_t mask)
  612. {
  613. /* Only consider the real interrupt enable bits. */
  614. mask &= (uint32_t)kLPUART_AllInterruptEnable;
  615. /* Check int enable bits in base->BAUD */
  616. uint32_t tempReg = base->BAUD;
  617. #if defined(FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT
  618. tempReg |= ((mask << 8U) & LPUART_BAUD_LBKDIE_MASK);
  619. /* Clear bit 7 from mask */
  620. mask &= ~(uint32_t)kLPUART_LinBreakInterruptEnable;
  621. #endif
  622. tempReg |= ((mask << 8U) & LPUART_BAUD_RXEDGIE_MASK);
  623. /* Clear bit 6 from mask */
  624. mask &= ~(uint32_t)kLPUART_RxActiveEdgeInterruptEnable;
  625. base->BAUD = tempReg;
  626. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  627. /* Check int enable bits in base->FIFO */
  628. base->FIFO = (base->FIFO & ~(LPUART_FIFO_TXOF_MASK | LPUART_FIFO_RXUF_MASK)) |
  629. (mask & (LPUART_FIFO_TXOFE_MASK | LPUART_FIFO_RXUFE_MASK));
  630. /* Clear bit 9 and bit 8 from mask */
  631. mask &= ~((uint32_t)kLPUART_TxFifoOverflowInterruptEnable | (uint32_t)kLPUART_RxFifoUnderflowInterruptEnable);
  632. #endif
  633. /* Check int enable bits in base->CTRL */
  634. base->CTRL |= mask;
  635. }
  636. /*!
  637. * brief Disables LPUART interrupts according to a provided mask.
  638. *
  639. * This function disables the LPUART interrupts according to a provided mask. The mask
  640. * is a logical OR of enumeration members. See ref _lpuart_interrupt_enable.
  641. * This example shows how to disable the TX empty interrupt and RX full interrupt:
  642. * code
  643. * LPUART_DisableInterrupts(LPUART1,kLPUART_TxDataRegEmptyInterruptEnable | kLPUART_RxDataRegFullInterruptEnable);
  644. * endcode
  645. *
  646. * param base LPUART peripheral base address.
  647. * param mask The interrupts to disable. Logical OR of ref _lpuart_interrupt_enable.
  648. */
  649. void LPUART_DisableInterrupts(LPUART_Type *base, uint32_t mask)
  650. {
  651. /* Only consider the real interrupt enable bits. */
  652. mask &= (uint32_t)kLPUART_AllInterruptEnable;
  653. /* Check int enable bits in base->BAUD */
  654. uint32_t tempReg = base->BAUD;
  655. #if defined(FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT
  656. tempReg &= ~((mask << 8U) & LPUART_BAUD_LBKDIE_MASK);
  657. /* Clear bit 7 from mask */
  658. mask &= ~(uint32_t)kLPUART_LinBreakInterruptEnable;
  659. #endif
  660. tempReg &= ~((mask << 8U) & LPUART_BAUD_RXEDGIE_MASK);
  661. /* Clear bit 6 from mask */
  662. mask &= ~(uint32_t)kLPUART_RxActiveEdgeInterruptEnable;
  663. base->BAUD = tempReg;
  664. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  665. /* Check int enable bits in base->FIFO */
  666. base->FIFO = (base->FIFO & ~(LPUART_FIFO_TXOF_MASK | LPUART_FIFO_RXUF_MASK)) &
  667. ~(mask & (LPUART_FIFO_TXOFE_MASK | LPUART_FIFO_RXUFE_MASK));
  668. /* Clear bit 9 and bit 8 from mask */
  669. mask &= ~((uint32_t)kLPUART_TxFifoOverflowInterruptEnable | (uint32_t)kLPUART_RxFifoUnderflowInterruptEnable);
  670. #endif
  671. /* Check int enable bits in base->CTRL */
  672. base->CTRL &= ~mask;
  673. }
  674. /*!
  675. * brief Gets enabled LPUART interrupts.
  676. *
  677. * This function gets the enabled LPUART interrupts. The enabled interrupts are returned
  678. * as the logical OR value of the enumerators ref _lpuart_interrupt_enable. To check
  679. * a specific interrupt enable status, compare the return value with enumerators
  680. * in ref _lpuart_interrupt_enable.
  681. * For example, to check whether the TX empty interrupt is enabled:
  682. * code
  683. * uint32_t enabledInterrupts = LPUART_GetEnabledInterrupts(LPUART1);
  684. *
  685. * if (kLPUART_TxDataRegEmptyInterruptEnable & enabledInterrupts)
  686. * {
  687. * ...
  688. * }
  689. * endcode
  690. *
  691. * param base LPUART peripheral base address.
  692. * return LPUART interrupt flags which are logical OR of the enumerators in ref _lpuart_interrupt_enable.
  693. */
  694. uint32_t LPUART_GetEnabledInterrupts(LPUART_Type *base)
  695. {
  696. /* Check int enable bits in base->CTRL */
  697. uint32_t temp = (uint32_t)(base->CTRL & (uint32_t)kLPUART_AllInterruptEnable);
  698. /* Check int enable bits in base->BAUD */
  699. temp = (temp & ~(uint32_t)kLPUART_RxActiveEdgeInterruptEnable) | ((base->BAUD & LPUART_BAUD_RXEDGIE_MASK) >> 8U);
  700. #if defined(FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT
  701. temp = (temp & ~(uint32_t)kLPUART_LinBreakInterruptEnable) | ((base->BAUD & LPUART_BAUD_LBKDIE_MASK) >> 8U);
  702. #endif
  703. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  704. /* Check int enable bits in base->FIFO */
  705. temp =
  706. (temp & ~((uint32_t)kLPUART_TxFifoOverflowInterruptEnable | (uint32_t)kLPUART_RxFifoUnderflowInterruptEnable)) |
  707. (base->FIFO & (LPUART_FIFO_TXOFE_MASK | LPUART_FIFO_RXUFE_MASK));
  708. #endif
  709. return temp;
  710. }
  711. /*!
  712. * brief Gets LPUART status flags.
  713. *
  714. * This function gets all LPUART status flags. The flags are returned as the logical
  715. * OR value of the enumerators ref _lpuart_flags. To check for a specific status,
  716. * compare the return value with enumerators in the ref _lpuart_flags.
  717. * For example, to check whether the TX is empty:
  718. * code
  719. * if (kLPUART_TxDataRegEmptyFlag & LPUART_GetStatusFlags(LPUART1))
  720. * {
  721. * ...
  722. * }
  723. * endcode
  724. *
  725. * param base LPUART peripheral base address.
  726. * return LPUART status flags which are ORed by the enumerators in the _lpuart_flags.
  727. */
  728. uint32_t LPUART_GetStatusFlags(LPUART_Type *base)
  729. {
  730. uint32_t temp;
  731. temp = base->STAT;
  732. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  733. temp |= (base->FIFO &
  734. (LPUART_FIFO_TXEMPT_MASK | LPUART_FIFO_RXEMPT_MASK | LPUART_FIFO_TXOF_MASK | LPUART_FIFO_RXUF_MASK)) >>
  735. 16U;
  736. #endif
  737. /* Only keeps the status bits */
  738. temp &= (uint32_t)kLPUART_AllFlags;
  739. return temp;
  740. }
  741. /*!
  742. * brief Clears status flags with a provided mask.
  743. *
  744. * This function clears LPUART status flags with a provided mask. Automatically cleared flags
  745. * can't be cleared by this function.
  746. * Flags that can only cleared or set by hardware are:
  747. * kLPUART_TxDataRegEmptyFlag, kLPUART_TransmissionCompleteFlag, kLPUART_RxDataRegFullFlag,
  748. * kLPUART_RxActiveFlag, kLPUART_NoiseErrorInRxDataRegFlag, kLPUART_ParityErrorInRxDataRegFlag,
  749. * kLPUART_TxFifoEmptyFlag,kLPUART_RxFifoEmptyFlag
  750. * Note: This API should be called when the Tx/Rx is idle, otherwise it takes no effects.
  751. *
  752. * param base LPUART peripheral base address.
  753. * param mask the status flags to be cleared. The user can use the enumerators in the
  754. * _lpuart_status_flag_t to do the OR operation and get the mask.
  755. * return 0 succeed, others failed.
  756. * retval kStatus_LPUART_FlagCannotClearManually The flag can't be cleared by this function but
  757. * it is cleared automatically by hardware.
  758. * retval kStatus_Success Status in the mask are cleared.
  759. */
  760. status_t LPUART_ClearStatusFlags(LPUART_Type *base, uint32_t mask)
  761. {
  762. uint32_t temp;
  763. status_t status;
  764. /* Only deal with the clearable flags */
  765. mask &= (uint32_t)kLPUART_AllClearFlags;
  766. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  767. /* Status bits in FIFO register */
  768. if ((mask & ((uint32_t)kLPUART_TxFifoOverflowFlag | (uint32_t)kLPUART_RxFifoUnderflowFlag)) != 0U)
  769. {
  770. /* Get the FIFO register value and mask the rx/tx FIFO flush bits and the status bits that can be W1C in case
  771. they are written 1 accidentally. */
  772. temp = (uint32_t)base->FIFO;
  773. temp &= (uint32_t)(
  774. ~(LPUART_FIFO_TXFLUSH_MASK | LPUART_FIFO_RXFLUSH_MASK | LPUART_FIFO_TXOF_MASK | LPUART_FIFO_RXUF_MASK));
  775. temp |= (mask << 16U) & (LPUART_FIFO_TXOF_MASK | LPUART_FIFO_RXUF_MASK);
  776. base->FIFO = temp;
  777. }
  778. #endif
  779. /* Status bits in STAT register */
  780. /* First get the STAT register value and mask all the bits that not represent status, then OR with the status bit
  781. * that is to be W1C */
  782. temp = (base->STAT & 0x3E000000UL) | mask;
  783. base->STAT = temp;
  784. /* If some flags still pending. */
  785. if (0U != (mask & LPUART_GetStatusFlags(base)))
  786. {
  787. status = kStatus_LPUART_FlagCannotClearManually;
  788. }
  789. else
  790. {
  791. status = kStatus_Success;
  792. }
  793. return status;
  794. }
  795. /*!
  796. * brief Writes to the transmitter register using a blocking method.
  797. *
  798. * This function polls the transmitter register, first waits for the register to be empty or TX FIFO to have room,
  799. * and writes data to the transmitter buffer, then waits for the data to be sent out to bus.
  800. *
  801. * param base LPUART peripheral base address.
  802. * param data Start address of the data to write.
  803. * param length Size of the data to write.
  804. * retval kStatus_LPUART_Timeout Transmission timed out and was aborted.
  805. * retval kStatus_Success Successfully wrote all data.
  806. */
  807. status_t LPUART_WriteBlocking(LPUART_Type *base, const uint8_t *data, size_t length)
  808. {
  809. assert(NULL != data);
  810. const uint8_t *dataAddress = data;
  811. size_t transferSize = length;
  812. #if UART_RETRY_TIMES
  813. uint32_t waitTimes;
  814. #endif
  815. while (0U != transferSize)
  816. {
  817. #if UART_RETRY_TIMES
  818. waitTimes = UART_RETRY_TIMES;
  819. while ((0U == (base->STAT & LPUART_STAT_TDRE_MASK)) && (0U != --waitTimes))
  820. #else
  821. while (0U == (base->STAT & LPUART_STAT_TDRE_MASK))
  822. #endif
  823. {
  824. }
  825. #if UART_RETRY_TIMES
  826. if (0U == waitTimes)
  827. {
  828. return kStatus_LPUART_Timeout;
  829. }
  830. #endif
  831. base->DATA = *(dataAddress);
  832. dataAddress++;
  833. transferSize--;
  834. }
  835. /* Ensure all the data in the transmit buffer are sent out to bus. */
  836. #if UART_RETRY_TIMES
  837. waitTimes = UART_RETRY_TIMES;
  838. while ((0U == (base->STAT & LPUART_STAT_TC_MASK)) && (0U != --waitTimes))
  839. #else
  840. while (0U == (base->STAT & LPUART_STAT_TC_MASK))
  841. #endif
  842. {
  843. }
  844. #if UART_RETRY_TIMES
  845. if (0U == waitTimes)
  846. {
  847. return kStatus_LPUART_Timeout;
  848. }
  849. #endif
  850. return kStatus_Success;
  851. }
  852. /*!
  853. * brief Reads the receiver data register using a blocking method.
  854. *
  855. * This function polls the receiver register, waits for the receiver register full or receiver FIFO
  856. * has data, and reads data from the TX register.
  857. *
  858. * param base LPUART peripheral base address.
  859. * param data Start address of the buffer to store the received data.
  860. * param length Size of the buffer.
  861. * retval kStatus_LPUART_RxHardwareOverrun Receiver overrun happened while receiving data.
  862. * retval kStatus_LPUART_NoiseError Noise error happened while receiving data.
  863. * retval kStatus_LPUART_FramingError Framing error happened while receiving data.
  864. * retval kStatus_LPUART_ParityError Parity error happened while receiving data.
  865. * retval kStatus_LPUART_Timeout Transmission timed out and was aborted.
  866. * retval kStatus_Success Successfully received all data.
  867. */
  868. status_t LPUART_ReadBlocking(LPUART_Type *base, uint8_t *data, size_t length)
  869. {
  870. assert(NULL != data);
  871. status_t status = kStatus_Success;
  872. uint32_t statusFlag;
  873. uint8_t *dataAddress = data;
  874. #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
  875. uint32_t ctrl = base->CTRL;
  876. bool isSevenDataBits = (((ctrl & LPUART_CTRL_M7_MASK) != 0U) ||
  877. (((ctrl & LPUART_CTRL_M_MASK) == 0U) && ((ctrl & LPUART_CTRL_PE_MASK) != 0U)));
  878. #endif
  879. #if UART_RETRY_TIMES
  880. uint32_t waitTimes;
  881. #endif
  882. while (0U != (length--))
  883. {
  884. #if UART_RETRY_TIMES
  885. waitTimes = UART_RETRY_TIMES;
  886. #endif
  887. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  888. while (0U == ((base->WATER & LPUART_WATER_RXCOUNT_MASK) >> LPUART_WATER_RXCOUNT_SHIFT))
  889. #else
  890. while (0U == (base->STAT & LPUART_STAT_RDRF_MASK))
  891. #endif
  892. {
  893. #if UART_RETRY_TIMES
  894. if (0U == --waitTimes)
  895. {
  896. status = kStatus_LPUART_Timeout;
  897. break;
  898. }
  899. #endif
  900. statusFlag = LPUART_GetStatusFlags(base);
  901. if (0U != (statusFlag & (uint32_t)kLPUART_RxOverrunFlag))
  902. {
  903. status = ((kStatus_Success == LPUART_ClearStatusFlags(base, (uint32_t)kLPUART_RxOverrunFlag)) ?
  904. (kStatus_LPUART_RxHardwareOverrun) :
  905. (kStatus_LPUART_FlagCannotClearManually));
  906. /* Other error flags(FE, NF, and PF) are prevented from setting once OR is set, no need to check other
  907. * error flags*/
  908. break;
  909. }
  910. if (0U != (statusFlag & (uint32_t)kLPUART_ParityErrorFlag))
  911. {
  912. status = ((kStatus_Success == LPUART_ClearStatusFlags(base, (uint32_t)kLPUART_ParityErrorFlag)) ?
  913. (kStatus_LPUART_ParityError) :
  914. (kStatus_LPUART_FlagCannotClearManually));
  915. }
  916. if (0U != (statusFlag & (uint32_t)kLPUART_FramingErrorFlag))
  917. {
  918. status = ((kStatus_Success == LPUART_ClearStatusFlags(base, (uint32_t)kLPUART_FramingErrorFlag)) ?
  919. (kStatus_LPUART_FramingError) :
  920. (kStatus_LPUART_FlagCannotClearManually));
  921. }
  922. if (0U != (statusFlag & (uint32_t)kLPUART_NoiseErrorFlag))
  923. {
  924. status = ((kStatus_Success == LPUART_ClearStatusFlags(base, (uint32_t)kLPUART_NoiseErrorFlag)) ?
  925. (kStatus_LPUART_NoiseError) :
  926. (kStatus_LPUART_FlagCannotClearManually));
  927. }
  928. if (kStatus_Success != status)
  929. {
  930. break;
  931. }
  932. }
  933. if (kStatus_Success == status)
  934. {
  935. #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
  936. if (isSevenDataBits)
  937. {
  938. *(dataAddress) = (uint8_t)(base->DATA & 0x7FU);
  939. dataAddress++;
  940. }
  941. else
  942. {
  943. *(dataAddress) = (uint8_t)base->DATA;
  944. dataAddress++;
  945. }
  946. #else
  947. *(dataAddress) = (uint8_t)base->DATA;
  948. dataAddress++;
  949. #endif
  950. }
  951. else
  952. {
  953. break;
  954. }
  955. }
  956. return status;
  957. }
  958. /*!
  959. * brief Initializes the LPUART handle.
  960. *
  961. * This function initializes the LPUART handle, which can be used for other LPUART
  962. * transactional APIs. Usually, for a specified LPUART instance,
  963. * call this API once to get the initialized handle.
  964. *
  965. * The LPUART driver supports the "background" receiving, which means that user can set up
  966. * an RX ring buffer optionally. Data received is stored into the ring buffer even when the
  967. * user doesn't call the LPUART_TransferReceiveNonBlocking() API. If there is already data received
  968. * in the ring buffer, the user can get the received data from the ring buffer directly.
  969. * The ring buffer is disabled if passing NULL as p ringBuffer.
  970. *
  971. * param base LPUART peripheral base address.
  972. * param handle LPUART handle pointer.
  973. * param callback Callback function.
  974. * param userData User data.
  975. */
  976. void LPUART_TransferCreateHandle(LPUART_Type *base,
  977. lpuart_handle_t *handle,
  978. lpuart_transfer_callback_t callback,
  979. void *userData)
  980. {
  981. assert(NULL != handle);
  982. uint32_t instance;
  983. #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
  984. uint32_t ctrl = base->CTRL;
  985. bool isSevenDataBits = (((ctrl & LPUART_CTRL_M7_MASK) != 0U) ||
  986. (((ctrl & LPUART_CTRL_M_MASK) == 0U) && ((ctrl & LPUART_CTRL_PE_MASK) != 0U)));
  987. #endif
  988. /* Zero the handle. */
  989. (void)memset(handle, 0, sizeof(lpuart_handle_t));
  990. /* Set the TX/RX state. */
  991. handle->rxState = (uint8_t)kLPUART_RxIdle;
  992. handle->txState = (uint8_t)kLPUART_TxIdle;
  993. /* Set the callback and user data. */
  994. handle->callback = callback;
  995. handle->userData = userData;
  996. #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
  997. /* Initial seven data bits flag */
  998. handle->isSevenDataBits = isSevenDataBits;
  999. #endif
  1000. /* Get instance from peripheral base address. */
  1001. instance = LPUART_GetInstance(base);
  1002. /* Save the handle in global variables to support the double weak mechanism. */
  1003. s_lpuartHandle[instance] = handle;
  1004. s_lpuartIsr = LPUART_TransferHandleIRQ;
  1005. /* Enable interrupt in NVIC. */
  1006. #if defined(FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ) && FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ
  1007. (void)EnableIRQ(s_lpuartRxIRQ[instance]);
  1008. (void)EnableIRQ(s_lpuartTxIRQ[instance]);
  1009. #else
  1010. (void)EnableIRQ(s_lpuartIRQ[instance]);
  1011. #endif
  1012. }
  1013. /*!
  1014. * brief Sets up the RX ring buffer.
  1015. *
  1016. * This function sets up the RX ring buffer to a specific UART handle.
  1017. *
  1018. * When the RX ring buffer is used, data received is stored into the ring buffer even when
  1019. * the user doesn't call the UART_TransferReceiveNonBlocking() API. If there is already data received
  1020. * in the ring buffer, the user can get the received data from the ring buffer directly.
  1021. *
  1022. * note When using RX ring buffer, one byte is reserved for internal use. In other
  1023. * words, if p ringBufferSize is 32, then only 31 bytes are used for saving data.
  1024. *
  1025. * param base LPUART peripheral base address.
  1026. * param handle LPUART handle pointer.
  1027. * param ringBuffer Start address of ring buffer for background receiving. Pass NULL to disable the ring buffer.
  1028. * param ringBufferSize size of the ring buffer.
  1029. */
  1030. void LPUART_TransferStartRingBuffer(LPUART_Type *base,
  1031. lpuart_handle_t *handle,
  1032. uint8_t *ringBuffer,
  1033. size_t ringBufferSize)
  1034. {
  1035. assert(NULL != handle);
  1036. assert(NULL != ringBuffer);
  1037. /* Setup the ring buffer address */
  1038. handle->rxRingBuffer = ringBuffer;
  1039. handle->rxRingBufferSize = ringBufferSize;
  1040. handle->rxRingBufferHead = 0U;
  1041. handle->rxRingBufferTail = 0U;
  1042. /* Disable and re-enable the global interrupt to protect the interrupt enable register during read-modify-wrte. */
  1043. uint32_t irqMask = DisableGlobalIRQ();
  1044. /* Enable the interrupt to accept the data when user need the ring buffer. */
  1045. base->CTRL |= (uint32_t)(LPUART_CTRL_RIE_MASK | LPUART_CTRL_ORIE_MASK);
  1046. EnableGlobalIRQ(irqMask);
  1047. }
  1048. /*!
  1049. * brief Aborts the background transfer and uninstalls the ring buffer.
  1050. *
  1051. * This function aborts the background transfer and uninstalls the ring buffer.
  1052. *
  1053. * param base LPUART peripheral base address.
  1054. * param handle LPUART handle pointer.
  1055. */
  1056. void LPUART_TransferStopRingBuffer(LPUART_Type *base, lpuart_handle_t *handle)
  1057. {
  1058. assert(NULL != handle);
  1059. if (handle->rxState == (uint8_t)kLPUART_RxIdle)
  1060. {
  1061. /* Disable and re-enable the global interrupt to protect the interrupt enable register during read-modify-wrte.
  1062. */
  1063. uint32_t irqMask = DisableGlobalIRQ();
  1064. base->CTRL &= ~(uint32_t)(LPUART_CTRL_RIE_MASK | LPUART_CTRL_ORIE_MASK);
  1065. EnableGlobalIRQ(irqMask);
  1066. }
  1067. handle->rxRingBuffer = NULL;
  1068. handle->rxRingBufferSize = 0U;
  1069. handle->rxRingBufferHead = 0U;
  1070. handle->rxRingBufferTail = 0U;
  1071. }
  1072. /*!
  1073. * brief Transmits a buffer of data using the interrupt method.
  1074. *
  1075. * This function send data using an interrupt method. This is a non-blocking function, which
  1076. * returns directly without waiting for all data written to the transmitter register. When
  1077. * all data is written to the TX register in the ISR, the LPUART driver calls the callback
  1078. * function and passes the ref kStatus_LPUART_TxIdle as status parameter.
  1079. *
  1080. * note The kStatus_LPUART_TxIdle is passed to the upper layer when all data are written
  1081. * to the TX register. However, there is no check to ensure that all the data sent out. Before disabling the TX,
  1082. * check the kLPUART_TransmissionCompleteFlag to ensure that the transmit is finished.
  1083. *
  1084. * param base LPUART peripheral base address.
  1085. * param handle LPUART handle pointer.
  1086. * param xfer LPUART transfer structure, see #lpuart_transfer_t.
  1087. * retval kStatus_Success Successfully start the data transmission.
  1088. * retval kStatus_LPUART_TxBusy Previous transmission still not finished, data not all written to the TX register.
  1089. * retval kStatus_InvalidArgument Invalid argument.
  1090. */
  1091. status_t LPUART_TransferSendNonBlocking(LPUART_Type *base, lpuart_handle_t *handle, lpuart_transfer_t *xfer)
  1092. {
  1093. assert(NULL != handle);
  1094. assert(NULL != xfer);
  1095. assert(NULL != xfer->txData);
  1096. assert(0U != xfer->dataSize);
  1097. status_t status;
  1098. /* Return error if current TX busy. */
  1099. if ((uint8_t)kLPUART_TxBusy == handle->txState)
  1100. {
  1101. status = kStatus_LPUART_TxBusy;
  1102. }
  1103. else
  1104. {
  1105. handle->txData = xfer->txData;
  1106. handle->txDataSize = xfer->dataSize;
  1107. handle->txDataSizeAll = xfer->dataSize;
  1108. handle->txState = (uint8_t)kLPUART_TxBusy;
  1109. /* Disable and re-enable the global interrupt to protect the interrupt enable register during read-modify-wrte.
  1110. */
  1111. uint32_t irqMask = DisableGlobalIRQ();
  1112. /* Enable transmitter interrupt. */
  1113. base->CTRL |= (uint32_t)LPUART_CTRL_TIE_MASK;
  1114. EnableGlobalIRQ(irqMask);
  1115. status = kStatus_Success;
  1116. }
  1117. return status;
  1118. }
  1119. /*!
  1120. * brief Aborts the interrupt-driven data transmit.
  1121. *
  1122. * This function aborts the interrupt driven data sending. The user can get the remainBtyes to find out
  1123. * how many bytes are not sent out.
  1124. *
  1125. * param base LPUART peripheral base address.
  1126. * param handle LPUART handle pointer.
  1127. */
  1128. void LPUART_TransferAbortSend(LPUART_Type *base, lpuart_handle_t *handle)
  1129. {
  1130. assert(NULL != handle);
  1131. /* Disable and re-enable the global interrupt to protect the interrupt enable register during read-modify-wrte. */
  1132. uint32_t irqMask = DisableGlobalIRQ();
  1133. base->CTRL &= ~(uint32_t)(LPUART_CTRL_TIE_MASK | LPUART_CTRL_TCIE_MASK);
  1134. EnableGlobalIRQ(irqMask);
  1135. handle->txDataSize = 0;
  1136. handle->txState = (uint8_t)kLPUART_TxIdle;
  1137. }
  1138. /*!
  1139. * brief Gets the number of bytes that have been sent out to bus.
  1140. *
  1141. * This function gets the number of bytes that have been sent out to bus by an interrupt method.
  1142. *
  1143. * param base LPUART peripheral base address.
  1144. * param handle LPUART handle pointer.
  1145. * param count Send bytes count.
  1146. * retval kStatus_NoTransferInProgress No send in progress.
  1147. * retval kStatus_InvalidArgument Parameter is invalid.
  1148. * retval kStatus_Success Get successfully through the parameter \p count;
  1149. */
  1150. status_t LPUART_TransferGetSendCount(LPUART_Type *base, lpuart_handle_t *handle, uint32_t *count)
  1151. {
  1152. assert(NULL != handle);
  1153. assert(NULL != count);
  1154. status_t status = kStatus_Success;
  1155. size_t tmptxDataSize = handle->txDataSize;
  1156. if ((uint8_t)kLPUART_TxIdle == handle->txState)
  1157. {
  1158. status = kStatus_NoTransferInProgress;
  1159. }
  1160. else
  1161. {
  1162. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  1163. *count = handle->txDataSizeAll - tmptxDataSize -
  1164. ((base->WATER & LPUART_WATER_TXCOUNT_MASK) >> LPUART_WATER_TXCOUNT_SHIFT);
  1165. #else
  1166. if ((base->STAT & (uint32_t)kLPUART_TxDataRegEmptyFlag) != 0U)
  1167. {
  1168. *count = handle->txDataSizeAll - tmptxDataSize;
  1169. }
  1170. else
  1171. {
  1172. *count = handle->txDataSizeAll - tmptxDataSize - 1U;
  1173. }
  1174. #endif
  1175. }
  1176. return status;
  1177. }
  1178. /*!
  1179. * brief Receives a buffer of data using the interrupt method.
  1180. *
  1181. * This function receives data using an interrupt method. This is a non-blocking function
  1182. * which returns without waiting to ensure that all data are received.
  1183. * If the RX ring buffer is used and not empty, the data in the ring buffer is copied and
  1184. * the parameter p receivedBytes shows how many bytes are copied from the ring buffer.
  1185. * After copying, if the data in the ring buffer is not enough for read, the receive
  1186. * request is saved by the LPUART driver. When the new data arrives, the receive request
  1187. * is serviced first. When all data is received, the LPUART driver notifies the upper layer
  1188. * through a callback function and passes a status parameter ref kStatus_UART_RxIdle.
  1189. * For example, the upper layer needs 10 bytes but there are only 5 bytes in ring buffer.
  1190. * The 5 bytes are copied to xfer->data, which returns with the
  1191. * parameter p receivedBytes set to 5. For the remaining 5 bytes, the newly arrived data is
  1192. * saved from xfer->data[5]. When 5 bytes are received, the LPUART driver notifies the upper layer.
  1193. * If the RX ring buffer is not enabled, this function enables the RX and RX interrupt
  1194. * to receive data to xfer->data. When all data is received, the upper layer is notified.
  1195. *
  1196. * param base LPUART peripheral base address.
  1197. * param handle LPUART handle pointer.
  1198. * param xfer LPUART transfer structure, see #uart_transfer_t.
  1199. * param receivedBytes Bytes received from the ring buffer directly.
  1200. * retval kStatus_Success Successfully queue the transfer into the transmit queue.
  1201. * retval kStatus_LPUART_RxBusy Previous receive request is not finished.
  1202. * retval kStatus_InvalidArgument Invalid argument.
  1203. */
  1204. status_t LPUART_TransferReceiveNonBlocking(LPUART_Type *base,
  1205. lpuart_handle_t *handle,
  1206. lpuart_transfer_t *xfer,
  1207. size_t *receivedBytes)
  1208. {
  1209. assert(NULL != handle);
  1210. assert(NULL != xfer);
  1211. assert(NULL != xfer->rxData);
  1212. assert(0U != xfer->dataSize);
  1213. uint32_t i;
  1214. status_t status;
  1215. uint32_t irqMask;
  1216. /* How many bytes to copy from ring buffer to user memory. */
  1217. size_t bytesToCopy = 0U;
  1218. /* How many bytes to receive. */
  1219. size_t bytesToReceive;
  1220. /* How many bytes currently have received. */
  1221. size_t bytesCurrentReceived;
  1222. /* How to get data:
  1223. 1. If RX ring buffer is not enabled, then save xfer->data and xfer->dataSize
  1224. to lpuart handle, enable interrupt to store received data to xfer->data. When
  1225. all data received, trigger callback.
  1226. 2. If RX ring buffer is enabled and not empty, get data from ring buffer first.
  1227. If there are enough data in ring buffer, copy them to xfer->data and return.
  1228. If there are not enough data in ring buffer, copy all of them to xfer->data,
  1229. save the xfer->data remained empty space to lpuart handle, receive data
  1230. to this empty space and trigger callback when finished. */
  1231. if ((uint8_t)kLPUART_RxBusy == handle->rxState)
  1232. {
  1233. status = kStatus_LPUART_RxBusy;
  1234. }
  1235. else
  1236. {
  1237. bytesToReceive = xfer->dataSize;
  1238. bytesCurrentReceived = 0;
  1239. /* If RX ring buffer is used. */
  1240. if (NULL != handle->rxRingBuffer)
  1241. {
  1242. /* Disable and re-enable the global interrupt to protect the interrupt enable register during
  1243. * read-modify-wrte. */
  1244. irqMask = DisableGlobalIRQ();
  1245. /* Disable LPUART RX IRQ, protect ring buffer. */
  1246. base->CTRL &= ~(uint32_t)(LPUART_CTRL_RIE_MASK | LPUART_CTRL_ORIE_MASK);
  1247. EnableGlobalIRQ(irqMask);
  1248. /* How many bytes in RX ring buffer currently. */
  1249. bytesToCopy = LPUART_TransferGetRxRingBufferLength(base, handle);
  1250. if (0U != bytesToCopy)
  1251. {
  1252. bytesToCopy = MIN(bytesToReceive, bytesToCopy);
  1253. bytesToReceive -= bytesToCopy;
  1254. /* Copy data from ring buffer to user memory. */
  1255. for (i = 0U; i < bytesToCopy; i++)
  1256. {
  1257. xfer->rxData[bytesCurrentReceived] = handle->rxRingBuffer[handle->rxRingBufferTail];
  1258. bytesCurrentReceived++;
  1259. /* Wrap to 0. Not use modulo (%) because it might be large and slow. */
  1260. if (((uint32_t)handle->rxRingBufferTail + 1U) == handle->rxRingBufferSize)
  1261. {
  1262. handle->rxRingBufferTail = 0U;
  1263. }
  1264. else
  1265. {
  1266. handle->rxRingBufferTail++;
  1267. }
  1268. }
  1269. }
  1270. /* If ring buffer does not have enough data, still need to read more data. */
  1271. if (0U != bytesToReceive)
  1272. {
  1273. /* No data in ring buffer, save the request to LPUART handle. */
  1274. handle->rxData = &xfer->rxData[bytesCurrentReceived];
  1275. handle->rxDataSize = bytesToReceive;
  1276. handle->rxDataSizeAll = xfer->dataSize;
  1277. handle->rxState = (uint8_t)kLPUART_RxBusy;
  1278. }
  1279. /* Disable and re-enable the global interrupt to protect the interrupt enable register during
  1280. * read-modify-wrte. */
  1281. irqMask = DisableGlobalIRQ();
  1282. /* Re-enable LPUART RX IRQ. */
  1283. base->CTRL |= (uint32_t)(LPUART_CTRL_RIE_MASK | LPUART_CTRL_ORIE_MASK);
  1284. EnableGlobalIRQ(irqMask);
  1285. /* Call user callback since all data are received. */
  1286. if (0U == bytesToReceive)
  1287. {
  1288. if (NULL != handle->callback)
  1289. {
  1290. handle->callback(base, handle, kStatus_LPUART_RxIdle, handle->userData);
  1291. }
  1292. }
  1293. }
  1294. /* Ring buffer not used. */
  1295. else
  1296. {
  1297. handle->rxData = &xfer->rxData[bytesCurrentReceived];
  1298. handle->rxDataSize = bytesToReceive;
  1299. handle->rxDataSizeAll = bytesToReceive;
  1300. handle->rxState = (uint8_t)kLPUART_RxBusy;
  1301. /* Disable and re-enable the global interrupt to protect the interrupt enable register during
  1302. * read-modify-wrte. */
  1303. irqMask = DisableGlobalIRQ();
  1304. /* Enable RX interrupt. */
  1305. base->CTRL |= (uint32_t)(LPUART_CTRL_RIE_MASK | LPUART_CTRL_ILIE_MASK | LPUART_CTRL_ORIE_MASK);
  1306. EnableGlobalIRQ(irqMask);
  1307. }
  1308. /* Return the how many bytes have read. */
  1309. if (NULL != receivedBytes)
  1310. {
  1311. *receivedBytes = bytesCurrentReceived;
  1312. }
  1313. status = kStatus_Success;
  1314. }
  1315. return status;
  1316. }
  1317. /*!
  1318. * brief Aborts the interrupt-driven data receiving.
  1319. *
  1320. * This function aborts the interrupt-driven data receiving. The user can get the remainBytes to find out
  1321. * how many bytes not received yet.
  1322. *
  1323. * param base LPUART peripheral base address.
  1324. * param handle LPUART handle pointer.
  1325. */
  1326. void LPUART_TransferAbortReceive(LPUART_Type *base, lpuart_handle_t *handle)
  1327. {
  1328. assert(NULL != handle);
  1329. /* Only abort the receive to handle->rxData, the RX ring buffer is still working. */
  1330. if (NULL == handle->rxRingBuffer)
  1331. {
  1332. /* Disable and re-enable the global interrupt to protect the interrupt enable register during read-modify-wrte.
  1333. */
  1334. uint32_t irqMask = DisableGlobalIRQ();
  1335. /* Disable RX interrupt. */
  1336. base->CTRL &= ~(uint32_t)(LPUART_CTRL_RIE_MASK | LPUART_CTRL_ILIE_MASK | LPUART_CTRL_ORIE_MASK);
  1337. EnableGlobalIRQ(irqMask);
  1338. }
  1339. handle->rxDataSize = 0U;
  1340. handle->rxState = (uint8_t)kLPUART_RxIdle;
  1341. }
  1342. /*!
  1343. * brief Gets the number of bytes that have been received.
  1344. *
  1345. * This function gets the number of bytes that have been received.
  1346. *
  1347. * param base LPUART peripheral base address.
  1348. * param handle LPUART handle pointer.
  1349. * param count Receive bytes count.
  1350. * retval kStatus_NoTransferInProgress No receive in progress.
  1351. * retval kStatus_InvalidArgument Parameter is invalid.
  1352. * retval kStatus_Success Get successfully through the parameter \p count;
  1353. */
  1354. status_t LPUART_TransferGetReceiveCount(LPUART_Type *base, lpuart_handle_t *handle, uint32_t *count)
  1355. {
  1356. assert(NULL != handle);
  1357. assert(NULL != count);
  1358. status_t status = kStatus_Success;
  1359. size_t tmprxDataSize = handle->rxDataSize;
  1360. if ((uint8_t)kLPUART_RxIdle == handle->rxState)
  1361. {
  1362. status = kStatus_NoTransferInProgress;
  1363. }
  1364. else
  1365. {
  1366. *count = handle->rxDataSizeAll - tmprxDataSize;
  1367. }
  1368. return status;
  1369. }
  1370. /*!
  1371. * brief LPUART IRQ handle function.
  1372. *
  1373. * This function handles the LPUART transmit and receive IRQ request.
  1374. *
  1375. * param base LPUART peripheral base address.
  1376. * param irqHandle LPUART handle pointer.
  1377. */
  1378. void LPUART_TransferHandleIRQ(LPUART_Type *base, void *irqHandle)
  1379. {
  1380. assert(NULL != irqHandle);
  1381. uint8_t count;
  1382. uint8_t tempCount;
  1383. uint32_t status = LPUART_GetStatusFlags(base);
  1384. uint32_t enabledInterrupts = LPUART_GetEnabledInterrupts(base);
  1385. uint16_t tpmRxRingBufferHead;
  1386. uint32_t tpmData;
  1387. uint32_t irqMask;
  1388. lpuart_handle_t *handle = (lpuart_handle_t *)irqHandle;
  1389. /* If RX overrun. */
  1390. if ((uint32_t)kLPUART_RxOverrunFlag == ((uint32_t)kLPUART_RxOverrunFlag & status))
  1391. {
  1392. /* Clear overrun flag, otherwise the RX does not work. */
  1393. base->STAT = ((base->STAT & 0x3FE00000U) | LPUART_STAT_OR_MASK);
  1394. /* Trigger callback. */
  1395. if (NULL != (handle->callback))
  1396. {
  1397. handle->callback(base, handle, kStatus_LPUART_RxHardwareOverrun, handle->userData);
  1398. }
  1399. }
  1400. /* If IDLE flag is set and the IDLE interrupt is enabled. */
  1401. if ((0U != ((uint32_t)kLPUART_IdleLineFlag & status)) &&
  1402. (0U != ((uint32_t)kLPUART_IdleLineInterruptEnable & enabledInterrupts)))
  1403. {
  1404. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  1405. count = ((uint8_t)((base->WATER & LPUART_WATER_RXCOUNT_MASK) >> LPUART_WATER_RXCOUNT_SHIFT));
  1406. while ((0U != handle->rxDataSize) && (0U != count))
  1407. {
  1408. tempCount = (uint8_t)MIN(handle->rxDataSize, count);
  1409. /* Using non block API to read the data from the registers. */
  1410. LPUART_ReadNonBlocking(base, handle->rxData, tempCount);
  1411. handle->rxData = &handle->rxData[tempCount];
  1412. handle->rxDataSize -= tempCount;
  1413. count -= tempCount;
  1414. /* If rxDataSize is 0, invoke rx idle callback.*/
  1415. if (0U == (handle->rxDataSize))
  1416. {
  1417. handle->rxState = (uint8_t)kLPUART_RxIdle;
  1418. if (NULL != handle->callback)
  1419. {
  1420. handle->callback(base, handle, kStatus_LPUART_RxIdle, handle->userData);
  1421. }
  1422. }
  1423. }
  1424. #endif
  1425. /* Clear IDLE flag.*/
  1426. base->STAT = ((base->STAT & 0x3FE00000U) | LPUART_STAT_IDLE_MASK);
  1427. /* If rxDataSize is 0, disable rx ready, overrun and idle line interrupt.*/
  1428. if (0U == handle->rxDataSize)
  1429. {
  1430. /* Disable and re-enable the global interrupt to protect the interrupt enable register during
  1431. * read-modify-wrte. */
  1432. irqMask = DisableGlobalIRQ();
  1433. base->CTRL &= ~(uint32_t)(LPUART_CTRL_RIE_MASK | LPUART_CTRL_ILIE_MASK | LPUART_CTRL_ORIE_MASK);
  1434. EnableGlobalIRQ(irqMask);
  1435. }
  1436. /* Invoke callback if callback is not NULL and rxDataSize is not 0. */
  1437. else if (NULL != handle->callback)
  1438. {
  1439. handle->callback(base, handle, kStatus_LPUART_IdleLineDetected, handle->userData);
  1440. }
  1441. else
  1442. {
  1443. /* Avoid MISRA 15.7 */
  1444. }
  1445. }
  1446. /* Receive data register full */
  1447. if ((0U != ((uint32_t)kLPUART_RxDataRegFullFlag & status)) &&
  1448. (0U != ((uint32_t)kLPUART_RxDataRegFullInterruptEnable & enabledInterrupts)))
  1449. {
  1450. /* Get the size that can be stored into buffer for this interrupt. */
  1451. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  1452. count = ((uint8_t)((base->WATER & LPUART_WATER_RXCOUNT_MASK) >> LPUART_WATER_RXCOUNT_SHIFT));
  1453. #else
  1454. count = 1;
  1455. #endif
  1456. /* If handle->rxDataSize is not 0, first save data to handle->rxData. */
  1457. while ((0U != handle->rxDataSize) && (0U != count))
  1458. {
  1459. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  1460. tempCount = (uint8_t)MIN(handle->rxDataSize, count);
  1461. #else
  1462. tempCount = 1;
  1463. #endif
  1464. /* Using non block API to read the data from the registers. */
  1465. LPUART_ReadNonBlocking(base, handle->rxData, tempCount);
  1466. handle->rxData = &handle->rxData[tempCount];
  1467. handle->rxDataSize -= tempCount;
  1468. count -= tempCount;
  1469. /* If all the data required for upper layer is ready, trigger callback. */
  1470. if (0U == handle->rxDataSize)
  1471. {
  1472. handle->rxState = (uint8_t)kLPUART_RxIdle;
  1473. if (NULL != handle->callback)
  1474. {
  1475. handle->callback(base, handle, kStatus_LPUART_RxIdle, handle->userData);
  1476. }
  1477. }
  1478. }
  1479. /* If use RX ring buffer, receive data to ring buffer. */
  1480. if (NULL != handle->rxRingBuffer)
  1481. {
  1482. while (0U != count--)
  1483. {
  1484. /* If RX ring buffer is full, trigger callback to notify over run. */
  1485. if (LPUART_TransferIsRxRingBufferFull(base, handle))
  1486. {
  1487. if (NULL != handle->callback)
  1488. {
  1489. handle->callback(base, handle, kStatus_LPUART_RxRingBufferOverrun, handle->userData);
  1490. }
  1491. }
  1492. /* If ring buffer is still full after callback function, the oldest data is overridden. */
  1493. if (LPUART_TransferIsRxRingBufferFull(base, handle))
  1494. {
  1495. /* Increase handle->rxRingBufferTail to make room for new data. */
  1496. if (((uint32_t)handle->rxRingBufferTail + 1U) == handle->rxRingBufferSize)
  1497. {
  1498. handle->rxRingBufferTail = 0U;
  1499. }
  1500. else
  1501. {
  1502. handle->rxRingBufferTail++;
  1503. }
  1504. }
  1505. /* Read data. */
  1506. tpmRxRingBufferHead = handle->rxRingBufferHead;
  1507. tpmData = base->DATA;
  1508. #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
  1509. if (handle->isSevenDataBits)
  1510. {
  1511. handle->rxRingBuffer[tpmRxRingBufferHead] = (uint8_t)(tpmData & 0x7FU);
  1512. }
  1513. else
  1514. {
  1515. handle->rxRingBuffer[tpmRxRingBufferHead] = (uint8_t)tpmData;
  1516. }
  1517. #else
  1518. handle->rxRingBuffer[tpmRxRingBufferHead] = (uint8_t)tpmData;
  1519. #endif
  1520. /* Increase handle->rxRingBufferHead. */
  1521. if (((uint32_t)handle->rxRingBufferHead + 1U) == handle->rxRingBufferSize)
  1522. {
  1523. handle->rxRingBufferHead = 0U;
  1524. }
  1525. else
  1526. {
  1527. handle->rxRingBufferHead++;
  1528. }
  1529. }
  1530. }
  1531. /* If no receive requst pending, stop RX interrupt. */
  1532. else if (0U == handle->rxDataSize)
  1533. {
  1534. /* Disable and re-enable the global interrupt to protect the interrupt enable register during
  1535. * read-modify-wrte. */
  1536. irqMask = DisableGlobalIRQ();
  1537. base->CTRL &= ~(uint32_t)(LPUART_CTRL_RIE_MASK | LPUART_CTRL_ORIE_MASK | LPUART_CTRL_ILIE_MASK);
  1538. EnableGlobalIRQ(irqMask);
  1539. }
  1540. else
  1541. {
  1542. }
  1543. }
  1544. /* Send data register empty and the interrupt is enabled. */
  1545. if ((0U != ((uint32_t)kLPUART_TxDataRegEmptyFlag & status)) &&
  1546. (0U != ((uint32_t)kLPUART_TxDataRegEmptyInterruptEnable & enabledInterrupts)))
  1547. {
  1548. /* Get the bytes that available at this moment. */
  1549. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  1550. count = (uint8_t)FSL_FEATURE_LPUART_FIFO_SIZEn(base) -
  1551. (uint8_t)((base->WATER & LPUART_WATER_TXCOUNT_MASK) >> LPUART_WATER_TXCOUNT_SHIFT);
  1552. #else
  1553. count = 1;
  1554. #endif
  1555. while ((0U != handle->txDataSize) && (0U != count))
  1556. {
  1557. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  1558. tempCount = (uint8_t)MIN(handle->txDataSize, count);
  1559. #else
  1560. tempCount = 1;
  1561. #endif
  1562. /* Using non block API to write the data to the registers. */
  1563. LPUART_WriteNonBlocking(base, handle->txData, tempCount);
  1564. handle->txData = &handle->txData[tempCount];
  1565. handle->txDataSize -= tempCount;
  1566. count -= tempCount;
  1567. /* If all the data are written to data register, notify user with the callback, then TX finished. */
  1568. if (0U == handle->txDataSize)
  1569. {
  1570. /* Disable and re-enable the global interrupt to protect the interrupt enable register during
  1571. * read-modify-wrte. */
  1572. irqMask = DisableGlobalIRQ();
  1573. /* Disable TX register empty interrupt and enable transmission completion interrupt. */
  1574. base->CTRL = (base->CTRL & ~LPUART_CTRL_TIE_MASK) | LPUART_CTRL_TCIE_MASK;
  1575. EnableGlobalIRQ(irqMask);
  1576. }
  1577. }
  1578. }
  1579. /* Transmission complete and the interrupt is enabled. */
  1580. if ((0U != ((uint32_t)kLPUART_TransmissionCompleteFlag & status)) &&
  1581. (0U != ((uint32_t)kLPUART_TransmissionCompleteInterruptEnable & enabledInterrupts)))
  1582. {
  1583. /* Set txState to idle only when all data has been sent out to bus. */
  1584. handle->txState = (uint8_t)kLPUART_TxIdle;
  1585. /* Disable and re-enable the global interrupt to protect the interrupt enable register during read-modify-wrte.
  1586. */
  1587. irqMask = DisableGlobalIRQ();
  1588. /* Disable transmission complete interrupt. */
  1589. base->CTRL &= ~(uint32_t)LPUART_CTRL_TCIE_MASK;
  1590. EnableGlobalIRQ(irqMask);
  1591. /* Trigger callback. */
  1592. if (NULL != handle->callback)
  1593. {
  1594. handle->callback(base, handle, kStatus_LPUART_TxIdle, handle->userData);
  1595. }
  1596. }
  1597. }
  1598. /*!
  1599. * brief LPUART Error IRQ handle function.
  1600. *
  1601. * This function handles the LPUART error IRQ request.
  1602. *
  1603. * param base LPUART peripheral base address.
  1604. * param irqHandle LPUART handle pointer.
  1605. */
  1606. void LPUART_TransferHandleErrorIRQ(LPUART_Type *base, void *irqHandle)
  1607. {
  1608. /* To be implemented by User. */
  1609. }
  1610. #if defined(FSL_FEATURE_LPUART_HAS_SHARED_IRQ0_IRQ1) && FSL_FEATURE_LPUART_HAS_SHARED_IRQ0_IRQ1
  1611. #if defined(FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ) && FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ
  1612. void LPUART0_LPUART1_RX_DriverIRQHandler(void);
  1613. void LPUART0_LPUART1_RX_DriverIRQHandler(void)
  1614. {
  1615. /* If handle is registered, treat the transfer function is enabled. */
  1616. if (NULL != s_lpuartHandle[0])
  1617. {
  1618. s_lpuartIsr(LPUART0, s_lpuartHandle[0]);
  1619. }
  1620. if (NULL != s_lpuartHandle[1])
  1621. {
  1622. s_lpuartIsr(LPUART1, s_lpuartHandle[1]);
  1623. }
  1624. SDK_ISR_EXIT_BARRIER;
  1625. }
  1626. void LPUART0_LPUART1_TX_DriverIRQHandler(void);
  1627. void LPUART0_LPUART1_TX_DriverIRQHandler(void)
  1628. {
  1629. /* If handle is registered, treat the transfer function is enabled. */
  1630. if (NULL != s_lpuartHandle[0])
  1631. {
  1632. s_lpuartIsr(LPUART0, s_lpuartHandle[0]);
  1633. }
  1634. if (NULL != s_lpuartHandle[1])
  1635. {
  1636. s_lpuartIsr(LPUART1, s_lpuartHandle[1]);
  1637. }
  1638. SDK_ISR_EXIT_BARRIER;
  1639. }
  1640. #else
  1641. void LPUART0_LPUART1_DriverIRQHandler(void);
  1642. void LPUART0_LPUART1_DriverIRQHandler(void)
  1643. {
  1644. /* If handle is registered, treat the transfer function is enabled. */
  1645. if (NULL != s_lpuartHandle[0])
  1646. {
  1647. s_lpuartIsr(LPUART0, s_lpuartHandle[0]);
  1648. }
  1649. if (NULL != s_lpuartHandle[1])
  1650. {
  1651. s_lpuartIsr(LPUART1, s_lpuartHandle[1]);
  1652. }
  1653. SDK_ISR_EXIT_BARRIER;
  1654. }
  1655. #endif
  1656. #endif
  1657. #if defined(LPUART0)
  1658. #if !(defined(FSL_FEATURE_LPUART_HAS_SHARED_IRQ0_IRQ1) && FSL_FEATURE_LPUART_HAS_SHARED_IRQ0_IRQ1)
  1659. #if defined(FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ) && FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ
  1660. void LPUART0_TX_DriverIRQHandler(void);
  1661. void LPUART0_TX_DriverIRQHandler(void)
  1662. {
  1663. s_lpuartIsr(LPUART0, s_lpuartHandle[0]);
  1664. SDK_ISR_EXIT_BARRIER;
  1665. }
  1666. void LPUART0_RX_DriverIRQHandler(void);
  1667. void LPUART0_RX_DriverIRQHandler(void)
  1668. {
  1669. s_lpuartIsr(LPUART0, s_lpuartHandle[0]);
  1670. SDK_ISR_EXIT_BARRIER;
  1671. }
  1672. #else
  1673. void LPUART0_DriverIRQHandler(void);
  1674. void LPUART0_DriverIRQHandler(void)
  1675. {
  1676. s_lpuartIsr(LPUART0, s_lpuartHandle[0]);
  1677. SDK_ISR_EXIT_BARRIER;
  1678. }
  1679. #endif
  1680. #endif
  1681. #endif
  1682. #if defined(LPUART1)
  1683. #if !(defined(FSL_FEATURE_LPUART_HAS_SHARED_IRQ0_IRQ1) && FSL_FEATURE_LPUART_HAS_SHARED_IRQ0_IRQ1)
  1684. #if defined(FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ) && FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ
  1685. void LPUART1_TX_DriverIRQHandler(void);
  1686. void LPUART1_TX_DriverIRQHandler(void)
  1687. {
  1688. s_lpuartIsr(LPUART1, s_lpuartHandle[1]);
  1689. SDK_ISR_EXIT_BARRIER;
  1690. }
  1691. void LPUART1_RX_DriverIRQHandler(void);
  1692. void LPUART1_RX_DriverIRQHandler(void)
  1693. {
  1694. s_lpuartIsr(LPUART1, s_lpuartHandle[1]);
  1695. SDK_ISR_EXIT_BARRIER;
  1696. }
  1697. #else
  1698. void LPUART1_DriverIRQHandler(void);
  1699. void LPUART1_DriverIRQHandler(void)
  1700. {
  1701. s_lpuartIsr(LPUART1, s_lpuartHandle[1]);
  1702. SDK_ISR_EXIT_BARRIER;
  1703. }
  1704. #endif
  1705. #endif
  1706. #endif
  1707. #if defined(LPUART2)
  1708. #if defined(FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ) && FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ
  1709. void LPUART2_TX_DriverIRQHandler(void);
  1710. void LPUART2_TX_DriverIRQHandler(void)
  1711. {
  1712. s_lpuartIsr(LPUART2, s_lpuartHandle[2]);
  1713. SDK_ISR_EXIT_BARRIER;
  1714. }
  1715. void LPUART2_RX_DriverIRQHandler(void);
  1716. void LPUART2_RX_DriverIRQHandler(void)
  1717. {
  1718. s_lpuartIsr(LPUART2, s_lpuartHandle[2]);
  1719. SDK_ISR_EXIT_BARRIER;
  1720. }
  1721. #else
  1722. void LPUART2_DriverIRQHandler(void);
  1723. void LPUART2_DriverIRQHandler(void)
  1724. {
  1725. s_lpuartIsr(LPUART2, s_lpuartHandle[2]);
  1726. SDK_ISR_EXIT_BARRIER;
  1727. }
  1728. #endif
  1729. #endif
  1730. #if defined(LPUART3)
  1731. #if defined(FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ) && FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ
  1732. void LPUART3_TX_DriverIRQHandler(void);
  1733. void LPUART3_TX_DriverIRQHandler(void)
  1734. {
  1735. s_lpuartIsr(LPUART3, s_lpuartHandle[3]);
  1736. SDK_ISR_EXIT_BARRIER;
  1737. }
  1738. void LPUART3_RX_DriverIRQHandler(void);
  1739. void LPUART3_RX_DriverIRQHandler(void)
  1740. {
  1741. s_lpuartIsr(LPUART3, s_lpuartHandle[3]);
  1742. SDK_ISR_EXIT_BARRIER;
  1743. }
  1744. #else
  1745. void LPUART3_DriverIRQHandler(void);
  1746. void LPUART3_DriverIRQHandler(void)
  1747. {
  1748. s_lpuartIsr(LPUART3, s_lpuartHandle[3]);
  1749. SDK_ISR_EXIT_BARRIER;
  1750. }
  1751. #endif
  1752. #endif
  1753. #if defined(LPUART4)
  1754. #if defined(FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ) && FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ
  1755. void LPUART4_TX_DriverIRQHandler(void);
  1756. void LPUART4_TX_DriverIRQHandler(void)
  1757. {
  1758. s_lpuartIsr(LPUART4, s_lpuartHandle[4]);
  1759. SDK_ISR_EXIT_BARRIER;
  1760. }
  1761. void LPUART4_RX_DriverIRQHandler(void);
  1762. void LPUART4_RX_DriverIRQHandler(void)
  1763. {
  1764. s_lpuartIsr(LPUART4, s_lpuartHandle[4]);
  1765. SDK_ISR_EXIT_BARRIER;
  1766. }
  1767. #else
  1768. void LPUART4_DriverIRQHandler(void);
  1769. void LPUART4_DriverIRQHandler(void)
  1770. {
  1771. s_lpuartIsr(LPUART4, s_lpuartHandle[4]);
  1772. SDK_ISR_EXIT_BARRIER;
  1773. }
  1774. #endif
  1775. #endif
  1776. #if defined(LPUART5)
  1777. #if defined(FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ) && FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ
  1778. void LPUART5_TX_DriverIRQHandler(void);
  1779. void LPUART5_TX_DriverIRQHandler(void)
  1780. {
  1781. s_lpuartIsr(LPUART5, s_lpuartHandle[5]);
  1782. SDK_ISR_EXIT_BARRIER;
  1783. }
  1784. void LPUART5_RX_DriverIRQHandler(void);
  1785. void LPUART5_RX_DriverIRQHandler(void)
  1786. {
  1787. s_lpuartIsr(LPUART5, s_lpuartHandle[5]);
  1788. SDK_ISR_EXIT_BARRIER;
  1789. }
  1790. #else
  1791. void LPUART5_DriverIRQHandler(void);
  1792. void LPUART5_DriverIRQHandler(void)
  1793. {
  1794. s_lpuartIsr(LPUART5, s_lpuartHandle[5]);
  1795. SDK_ISR_EXIT_BARRIER;
  1796. }
  1797. #endif
  1798. #endif
  1799. #if defined(LPUART6)
  1800. #if defined(FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ) && FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ
  1801. void LPUART6_TX_DriverIRQHandler(void);
  1802. void LPUART6_TX_DriverIRQHandler(void)
  1803. {
  1804. s_lpuartIsr(LPUART6, s_lpuartHandle[6]);
  1805. SDK_ISR_EXIT_BARRIER;
  1806. }
  1807. void LPUART6_RX_DriverIRQHandler(void);
  1808. void LPUART6_RX_DriverIRQHandler(void)
  1809. {
  1810. s_lpuartIsr(LPUART6, s_lpuartHandle[6]);
  1811. SDK_ISR_EXIT_BARRIER;
  1812. }
  1813. #else
  1814. void LPUART6_DriverIRQHandler(void);
  1815. void LPUART6_DriverIRQHandler(void)
  1816. {
  1817. s_lpuartIsr(LPUART6, s_lpuartHandle[6]);
  1818. SDK_ISR_EXIT_BARRIER;
  1819. }
  1820. #endif
  1821. #endif
  1822. #if defined(LPUART7)
  1823. #if defined(FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ) && FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ
  1824. void LPUART7_TX_DriverIRQHandler(void);
  1825. void LPUART7_TX_DriverIRQHandler(void)
  1826. {
  1827. s_lpuartIsr(LPUART7, s_lpuartHandle[7]);
  1828. SDK_ISR_EXIT_BARRIER;
  1829. }
  1830. void LPUART7_RX_DriverIRQHandler(void);
  1831. void LPUART7_RX_DriverIRQHandler(void)
  1832. {
  1833. s_lpuartIsr(LPUART7, s_lpuartHandle[7]);
  1834. SDK_ISR_EXIT_BARRIER;
  1835. }
  1836. #else
  1837. void LPUART7_DriverIRQHandler(void);
  1838. void LPUART7_DriverIRQHandler(void)
  1839. {
  1840. s_lpuartIsr(LPUART7, s_lpuartHandle[7]);
  1841. SDK_ISR_EXIT_BARRIER;
  1842. }
  1843. #endif
  1844. #endif
  1845. #if defined(LPUART8)
  1846. #if defined(FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ) && FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ
  1847. void LPUART8_TX_DriverIRQHandler(void);
  1848. void LPUART8_TX_DriverIRQHandler(void)
  1849. {
  1850. s_lpuartIsr(LPUART8, s_lpuartHandle[8]);
  1851. SDK_ISR_EXIT_BARRIER;
  1852. }
  1853. void LPUART8_RX_DriverIRQHandler(void);
  1854. void LPUART8_RX_DriverIRQHandler(void)
  1855. {
  1856. s_lpuartIsr(LPUART8, s_lpuartHandle[8]);
  1857. SDK_ISR_EXIT_BARRIER;
  1858. }
  1859. #else
  1860. void LPUART8_DriverIRQHandler(void);
  1861. void LPUART8_DriverIRQHandler(void)
  1862. {
  1863. s_lpuartIsr(LPUART8, s_lpuartHandle[8]);
  1864. SDK_ISR_EXIT_BARRIER;
  1865. }
  1866. #endif
  1867. #endif
  1868. #if defined(LPUART9)
  1869. #if defined(FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ) && FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ
  1870. void LPUART9_TX_DriverIRQHandler(void);
  1871. void LPUART9_TX_DriverIRQHandler(void)
  1872. {
  1873. s_lpuartIsr(LPUART9, s_lpuartHandle[9]);
  1874. SDK_ISR_EXIT_BARRIER;
  1875. }
  1876. void LPUART9_RX_DriverIRQHandler(void);
  1877. void LPUART9_RX_DriverIRQHandler(void)
  1878. {
  1879. s_lpuartIsr(LPUART9, s_lpuartHandle[9]);
  1880. SDK_ISR_EXIT_BARRIER;
  1881. }
  1882. #else
  1883. void LPUART9_DriverIRQHandler(void);
  1884. void LPUART9_DriverIRQHandler(void)
  1885. {
  1886. s_lpuartIsr(LPUART9, s_lpuartHandle[9]);
  1887. SDK_ISR_EXIT_BARRIER;
  1888. }
  1889. #endif
  1890. #endif
  1891. #if defined(LPUART10)
  1892. #if defined(FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ) && FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ
  1893. void LPUART10_TX_DriverIRQHandler(void);
  1894. void LPUART10_TX_DriverIRQHandler(void)
  1895. {
  1896. s_lpuartIsr(LPUART10, s_lpuartHandle[10]);
  1897. SDK_ISR_EXIT_BARRIER;
  1898. }
  1899. void LPUART10_RX_DriverIRQHandler(void);
  1900. void LPUART10_RX_DriverIRQHandler(void)
  1901. {
  1902. s_lpuartIsr(LPUART10, s_lpuartHandle[10]);
  1903. SDK_ISR_EXIT_BARRIER;
  1904. }
  1905. #else
  1906. void LPUART10_DriverIRQHandler(void);
  1907. void LPUART10_DriverIRQHandler(void)
  1908. {
  1909. s_lpuartIsr(LPUART10, s_lpuartHandle[10]);
  1910. SDK_ISR_EXIT_BARRIER;
  1911. }
  1912. #endif
  1913. #endif
  1914. #if defined(LPUART11)
  1915. #if defined(FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ) && FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ
  1916. void LPUART11_TX_DriverIRQHandler(void);
  1917. void LPUART11_TX_DriverIRQHandler(void)
  1918. {
  1919. s_lpuartIsr(LPUART11, s_lpuartHandle[11]);
  1920. SDK_ISR_EXIT_BARRIER;
  1921. }
  1922. void LPUART11_RX_DriverIRQHandler(void);
  1923. void LPUART11_RX_DriverIRQHandler(void)
  1924. {
  1925. s_lpuartIsr(LPUART11, s_lpuartHandle[11]);
  1926. SDK_ISR_EXIT_BARRIER;
  1927. }
  1928. #else
  1929. void LPUART11_DriverIRQHandler(void);
  1930. void LPUART11_DriverIRQHandler(void)
  1931. {
  1932. s_lpuartIsr(LPUART11, s_lpuartHandle[11]);
  1933. SDK_ISR_EXIT_BARRIER;
  1934. }
  1935. #endif
  1936. #endif
  1937. #if defined(LPUART12)
  1938. #if defined(FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ) && FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ
  1939. void LPUART12_TX_DriverIRQHandler(void);
  1940. void LPUART12_TX_DriverIRQHandler(void)
  1941. {
  1942. s_lpuartIsr(LPUART12, s_lpuartHandle[12]);
  1943. SDK_ISR_EXIT_BARRIER;
  1944. }
  1945. void LPUART12_RX_DriverIRQHandler(void);
  1946. void LPUART12_RX_DriverIRQHandler(void)
  1947. {
  1948. s_lpuartIsr(LPUART12, s_lpuartHandle[12]);
  1949. SDK_ISR_EXIT_BARRIER;
  1950. }
  1951. #else
  1952. void LPUART12_DriverIRQHandler(void);
  1953. void LPUART12_DriverIRQHandler(void)
  1954. {
  1955. s_lpuartIsr(LPUART12, s_lpuartHandle[12]);
  1956. SDK_ISR_EXIT_BARRIER;
  1957. }
  1958. #endif
  1959. #endif
  1960. #if defined(CM4_0__LPUART)
  1961. void M4_0_LPUART_DriverIRQHandler(void);
  1962. void M4_0_LPUART_DriverIRQHandler(void)
  1963. {
  1964. s_lpuartIsr(CM4_0__LPUART, s_lpuartHandle[LPUART_GetInstance(CM4_0__LPUART)]);
  1965. SDK_ISR_EXIT_BARRIER;
  1966. }
  1967. #endif
  1968. #if defined(CM4_1__LPUART)
  1969. void M4_1_LPUART_DriverIRQHandler(void);
  1970. void M4_1_LPUART_DriverIRQHandler(void)
  1971. {
  1972. s_lpuartIsr(CM4_1__LPUART, s_lpuartHandle[LPUART_GetInstance(CM4_1__LPUART)]);
  1973. SDK_ISR_EXIT_BARRIER;
  1974. }
  1975. #endif
  1976. #if defined(CM4__LPUART)
  1977. void M4_LPUART_DriverIRQHandler(void);
  1978. void M4_LPUART_DriverIRQHandler(void)
  1979. {
  1980. s_lpuartIsr(CM4__LPUART, s_lpuartHandle[LPUART_GetInstance(CM4__LPUART)]);
  1981. SDK_ISR_EXIT_BARRIER;
  1982. }
  1983. #endif
  1984. #if defined(DMA__LPUART0)
  1985. void DMA_UART0_INT_DriverIRQHandler(void);
  1986. void DMA_UART0_INT_DriverIRQHandler(void)
  1987. {
  1988. s_lpuartIsr(DMA__LPUART0, s_lpuartHandle[LPUART_GetInstance(DMA__LPUART0)]);
  1989. SDK_ISR_EXIT_BARRIER;
  1990. }
  1991. #endif
  1992. #if defined(DMA__LPUART1)
  1993. void DMA_UART1_INT_DriverIRQHandler(void);
  1994. void DMA_UART1_INT_DriverIRQHandler(void)
  1995. {
  1996. s_lpuartIsr(DMA__LPUART1, s_lpuartHandle[LPUART_GetInstance(DMA__LPUART1)]);
  1997. SDK_ISR_EXIT_BARRIER;
  1998. }
  1999. #endif
  2000. #if defined(DMA__LPUART2)
  2001. void DMA_UART2_INT_DriverIRQHandler(void);
  2002. void DMA_UART2_INT_DriverIRQHandler(void)
  2003. {
  2004. s_lpuartIsr(DMA__LPUART2, s_lpuartHandle[LPUART_GetInstance(DMA__LPUART2)]);
  2005. SDK_ISR_EXIT_BARRIER;
  2006. }
  2007. #endif
  2008. #if defined(DMA__LPUART3)
  2009. void DMA_UART3_INT_DriverIRQHandler(void);
  2010. void DMA_UART3_INT_DriverIRQHandler(void)
  2011. {
  2012. s_lpuartIsr(DMA__LPUART3, s_lpuartHandle[LPUART_GetInstance(DMA__LPUART3)]);
  2013. SDK_ISR_EXIT_BARRIER;
  2014. }
  2015. #endif
  2016. #if defined(DMA__LPUART4)
  2017. void DMA_UART4_INT_DriverIRQHandler(void);
  2018. void DMA_UART4_INT_DriverIRQHandler(void)
  2019. {
  2020. s_lpuartIsr(DMA__LPUART4, s_lpuartHandle[LPUART_GetInstance(DMA__LPUART4)]);
  2021. SDK_ISR_EXIT_BARRIER;
  2022. }
  2023. #endif
  2024. #if defined(ADMA__LPUART0)
  2025. void ADMA_UART0_INT_DriverIRQHandler(void);
  2026. void ADMA_UART0_INT_DriverIRQHandler(void)
  2027. {
  2028. s_lpuartIsr(ADMA__LPUART0, s_lpuartHandle[LPUART_GetInstance(ADMA__LPUART0)]);
  2029. SDK_ISR_EXIT_BARRIER;
  2030. }
  2031. #endif
  2032. #if defined(ADMA__LPUART1)
  2033. void ADMA_UART1_INT_DriverIRQHandler(void);
  2034. void ADMA_UART1_INT_DriverIRQHandler(void)
  2035. {
  2036. s_lpuartIsr(ADMA__LPUART1, s_lpuartHandle[LPUART_GetInstance(ADMA__LPUART1)]);
  2037. SDK_ISR_EXIT_BARRIER;
  2038. }
  2039. #endif
  2040. #if defined(ADMA__LPUART2)
  2041. void ADMA_UART2_INT_DriverIRQHandler(void);
  2042. void ADMA_UART2_INT_DriverIRQHandler(void)
  2043. {
  2044. s_lpuartIsr(ADMA__LPUART2, s_lpuartHandle[LPUART_GetInstance(ADMA__LPUART2)]);
  2045. SDK_ISR_EXIT_BARRIER;
  2046. }
  2047. #endif
  2048. #if defined(ADMA__LPUART3)
  2049. void ADMA_UART3_INT_DriverIRQHandler(void);
  2050. void ADMA_UART3_INT_DriverIRQHandler(void)
  2051. {
  2052. s_lpuartIsr(ADMA__LPUART3, s_lpuartHandle[LPUART_GetInstance(ADMA__LPUART3)]);
  2053. SDK_ISR_EXIT_BARRIER;
  2054. }
  2055. #endif