fsl_flexio_uart.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "fsl_flexio_uart.h"
  35. /*******************************************************************************
  36. * Definitions
  37. ******************************************************************************/
  38. /* Component ID definition, used by tools. */
  39. #ifndef FSL_COMPONENT_ID
  40. #define FSL_COMPONENT_ID "platform.drivers.flexio_uart"
  41. #endif
  42. /*<! @brief uart transfer state. */
  43. enum _flexio_uart_transfer_states
  44. {
  45. kFLEXIO_UART_TxIdle, /* TX idle. */
  46. kFLEXIO_UART_TxBusy, /* TX busy. */
  47. kFLEXIO_UART_RxIdle, /* RX idle. */
  48. kFLEXIO_UART_RxBusy /* RX busy. */
  49. };
  50. /*******************************************************************************
  51. * Prototypes
  52. ******************************************************************************/
  53. /*!
  54. * @brief Get the length of received data in RX ring buffer.
  55. *
  56. * @param handle FLEXIO UART handle pointer.
  57. * @return Length of received data in RX ring buffer.
  58. */
  59. static size_t FLEXIO_UART_TransferGetRxRingBufferLength(flexio_uart_handle_t *handle);
  60. /*!
  61. * @brief Check whether the RX ring buffer is full.
  62. *
  63. * @param handle FLEXIO UART handle pointer.
  64. * @retval true RX ring buffer is full.
  65. * @retval false RX ring buffer is not full.
  66. */
  67. static bool FLEXIO_UART_TransferIsRxRingBufferFull(flexio_uart_handle_t *handle);
  68. /*******************************************************************************
  69. * Codes
  70. ******************************************************************************/
  71. static uint32_t FLEXIO_UART_GetInstance(FLEXIO_UART_Type *base)
  72. {
  73. return FLEXIO_GetInstance(base->flexioBase);
  74. }
  75. static size_t FLEXIO_UART_TransferGetRxRingBufferLength(flexio_uart_handle_t *handle)
  76. {
  77. size_t size;
  78. if (handle->rxRingBufferTail > handle->rxRingBufferHead)
  79. {
  80. size = (size_t)(handle->rxRingBufferHead + handle->rxRingBufferSize - handle->rxRingBufferTail);
  81. }
  82. else
  83. {
  84. size = (size_t)(handle->rxRingBufferHead - handle->rxRingBufferTail);
  85. }
  86. return size;
  87. }
  88. static bool FLEXIO_UART_TransferIsRxRingBufferFull(flexio_uart_handle_t *handle)
  89. {
  90. bool full;
  91. if (FLEXIO_UART_TransferGetRxRingBufferLength(handle) == (handle->rxRingBufferSize - 1U))
  92. {
  93. full = true;
  94. }
  95. else
  96. {
  97. full = false;
  98. }
  99. return full;
  100. }
  101. status_t FLEXIO_UART_Init(FLEXIO_UART_Type *base, const flexio_uart_config_t *userConfig, uint32_t srcClock_Hz)
  102. {
  103. assert(base && userConfig);
  104. flexio_shifter_config_t shifterConfig;
  105. flexio_timer_config_t timerConfig;
  106. uint32_t ctrlReg = 0;
  107. uint16_t timerDiv = 0;
  108. uint16_t timerCmp = 0;
  109. status_t result = kStatus_Success;
  110. /* Clear the shifterConfig & timerConfig struct. */
  111. memset(&shifterConfig, 0, sizeof(shifterConfig));
  112. memset(&timerConfig, 0, sizeof(timerConfig));
  113. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  114. /* Ungate flexio clock. */
  115. CLOCK_EnableClock(s_flexioClocks[FLEXIO_UART_GetInstance(base)]);
  116. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  117. /* Configure FLEXIO UART */
  118. ctrlReg = base->flexioBase->CTRL;
  119. ctrlReg &= ~(FLEXIO_CTRL_DOZEN_MASK | FLEXIO_CTRL_DBGE_MASK | FLEXIO_CTRL_FASTACC_MASK | FLEXIO_CTRL_FLEXEN_MASK);
  120. ctrlReg |= (FLEXIO_CTRL_DBGE(userConfig->enableInDebug) | FLEXIO_CTRL_FASTACC(userConfig->enableFastAccess) |
  121. FLEXIO_CTRL_FLEXEN(userConfig->enableUart));
  122. if (!userConfig->enableInDoze)
  123. {
  124. ctrlReg |= FLEXIO_CTRL_DOZEN_MASK;
  125. }
  126. base->flexioBase->CTRL = ctrlReg;
  127. /* Do hardware configuration. */
  128. /* 1. Configure the shifter 0 for tx. */
  129. shifterConfig.timerSelect = base->timerIndex[0];
  130. shifterConfig.timerPolarity = kFLEXIO_ShifterTimerPolarityOnPositive;
  131. shifterConfig.pinConfig = kFLEXIO_PinConfigOutput;
  132. shifterConfig.pinSelect = base->TxPinIndex;
  133. shifterConfig.pinPolarity = kFLEXIO_PinActiveHigh;
  134. shifterConfig.shifterMode = kFLEXIO_ShifterModeTransmit;
  135. shifterConfig.inputSource = kFLEXIO_ShifterInputFromPin;
  136. shifterConfig.shifterStop = kFLEXIO_ShifterStopBitHigh;
  137. shifterConfig.shifterStart = kFLEXIO_ShifterStartBitLow;
  138. FLEXIO_SetShifterConfig(base->flexioBase, base->shifterIndex[0], &shifterConfig);
  139. /*2. Configure the timer 0 for tx. */
  140. timerConfig.triggerSelect = FLEXIO_TIMER_TRIGGER_SEL_SHIFTnSTAT(base->shifterIndex[0]);
  141. timerConfig.triggerPolarity = kFLEXIO_TimerTriggerPolarityActiveLow;
  142. timerConfig.triggerSource = kFLEXIO_TimerTriggerSourceInternal;
  143. timerConfig.pinConfig = kFLEXIO_PinConfigOutputDisabled;
  144. timerConfig.pinSelect = base->TxPinIndex;
  145. timerConfig.pinPolarity = kFLEXIO_PinActiveHigh;
  146. timerConfig.timerMode = kFLEXIO_TimerModeDual8BitBaudBit;
  147. timerConfig.timerOutput = kFLEXIO_TimerOutputOneNotAffectedByReset;
  148. timerConfig.timerDecrement = kFLEXIO_TimerDecSrcOnFlexIOClockShiftTimerOutput;
  149. timerConfig.timerReset = kFLEXIO_TimerResetNever;
  150. timerConfig.timerDisable = kFLEXIO_TimerDisableOnTimerCompare;
  151. timerConfig.timerEnable = kFLEXIO_TimerEnableOnTriggerHigh;
  152. timerConfig.timerStop = kFLEXIO_TimerStopBitEnableOnTimerDisable;
  153. timerConfig.timerStart = kFLEXIO_TimerStartBitEnabled;
  154. timerDiv = srcClock_Hz / userConfig->baudRate_Bps;
  155. timerDiv = timerDiv / 2 - 1;
  156. if (timerDiv > 0xFFU)
  157. {
  158. result = kStatus_InvalidArgument;
  159. }
  160. timerCmp = ((uint32_t)(userConfig->bitCountPerChar * 2 - 1)) << 8U;
  161. timerCmp |= timerDiv;
  162. timerConfig.timerCompare = timerCmp;
  163. FLEXIO_SetTimerConfig(base->flexioBase, base->timerIndex[0], &timerConfig);
  164. /* 3. Configure the shifter 1 for rx. */
  165. shifterConfig.timerSelect = base->timerIndex[1];
  166. shifterConfig.timerPolarity = kFLEXIO_ShifterTimerPolarityOnNegitive;
  167. shifterConfig.pinConfig = kFLEXIO_PinConfigOutputDisabled;
  168. shifterConfig.pinSelect = base->RxPinIndex;
  169. shifterConfig.pinPolarity = kFLEXIO_PinActiveHigh;
  170. shifterConfig.shifterMode = kFLEXIO_ShifterModeReceive;
  171. shifterConfig.inputSource = kFLEXIO_ShifterInputFromPin;
  172. shifterConfig.shifterStop = kFLEXIO_ShifterStopBitHigh;
  173. shifterConfig.shifterStart = kFLEXIO_ShifterStartBitLow;
  174. FLEXIO_SetShifterConfig(base->flexioBase, base->shifterIndex[1], &shifterConfig);
  175. /* 4. Configure the timer 1 for rx. */
  176. timerConfig.triggerSelect = FLEXIO_TIMER_TRIGGER_SEL_PININPUT(base->RxPinIndex);
  177. timerConfig.triggerPolarity = kFLEXIO_TimerTriggerPolarityActiveHigh;
  178. timerConfig.triggerSource = kFLEXIO_TimerTriggerSourceExternal;
  179. timerConfig.pinConfig = kFLEXIO_PinConfigOutputDisabled;
  180. timerConfig.pinSelect = base->RxPinIndex;
  181. timerConfig.pinPolarity = kFLEXIO_PinActiveLow;
  182. timerConfig.timerMode = kFLEXIO_TimerModeDual8BitBaudBit;
  183. timerConfig.timerOutput = kFLEXIO_TimerOutputOneAffectedByReset;
  184. timerConfig.timerDecrement = kFLEXIO_TimerDecSrcOnFlexIOClockShiftTimerOutput;
  185. timerConfig.timerReset = kFLEXIO_TimerResetOnTimerPinRisingEdge;
  186. timerConfig.timerDisable = kFLEXIO_TimerDisableOnTimerCompare;
  187. timerConfig.timerEnable = kFLEXIO_TimerEnableOnPinRisingEdge;
  188. timerConfig.timerStop = kFLEXIO_TimerStopBitEnableOnTimerDisable;
  189. timerConfig.timerStart = kFLEXIO_TimerStartBitEnabled;
  190. timerConfig.timerCompare = timerCmp;
  191. FLEXIO_SetTimerConfig(base->flexioBase, base->timerIndex[1], &timerConfig);
  192. return result;
  193. }
  194. void FLEXIO_UART_Deinit(FLEXIO_UART_Type *base)
  195. {
  196. base->flexioBase->SHIFTCFG[base->shifterIndex[0]] = 0;
  197. base->flexioBase->SHIFTCTL[base->shifterIndex[0]] = 0;
  198. base->flexioBase->SHIFTCFG[base->shifterIndex[1]] = 0;
  199. base->flexioBase->SHIFTCTL[base->shifterIndex[1]] = 0;
  200. base->flexioBase->TIMCFG[base->timerIndex[0]] = 0;
  201. base->flexioBase->TIMCMP[base->timerIndex[0]] = 0;
  202. base->flexioBase->TIMCTL[base->timerIndex[0]] = 0;
  203. base->flexioBase->TIMCFG[base->timerIndex[1]] = 0;
  204. base->flexioBase->TIMCMP[base->timerIndex[1]] = 0;
  205. base->flexioBase->TIMCTL[base->timerIndex[1]] = 0;
  206. /* Clear the shifter flag. */
  207. base->flexioBase->SHIFTSTAT = (1U << base->shifterIndex[0]);
  208. base->flexioBase->SHIFTSTAT = (1U << base->shifterIndex[1]);
  209. /* Clear the timer flag. */
  210. base->flexioBase->TIMSTAT = (1U << base->timerIndex[0]);
  211. base->flexioBase->TIMSTAT = (1U << base->timerIndex[1]);
  212. }
  213. void FLEXIO_UART_GetDefaultConfig(flexio_uart_config_t *userConfig)
  214. {
  215. assert(userConfig);
  216. userConfig->enableUart = true;
  217. userConfig->enableInDoze = false;
  218. userConfig->enableInDebug = true;
  219. userConfig->enableFastAccess = false;
  220. /* Default baud rate 115200. */
  221. userConfig->baudRate_Bps = 115200U;
  222. /* Default bit count at 8. */
  223. userConfig->bitCountPerChar = kFLEXIO_UART_8BitsPerChar;
  224. }
  225. void FLEXIO_UART_EnableInterrupts(FLEXIO_UART_Type *base, uint32_t mask)
  226. {
  227. if (mask & kFLEXIO_UART_TxDataRegEmptyInterruptEnable)
  228. {
  229. FLEXIO_EnableShifterStatusInterrupts(base->flexioBase, 1U << base->shifterIndex[0]);
  230. }
  231. if (mask & kFLEXIO_UART_RxDataRegFullInterruptEnable)
  232. {
  233. FLEXIO_EnableShifterStatusInterrupts(base->flexioBase, 1U << base->shifterIndex[1]);
  234. }
  235. }
  236. void FLEXIO_UART_DisableInterrupts(FLEXIO_UART_Type *base, uint32_t mask)
  237. {
  238. if (mask & kFLEXIO_UART_TxDataRegEmptyInterruptEnable)
  239. {
  240. FLEXIO_DisableShifterStatusInterrupts(base->flexioBase, 1U << base->shifterIndex[0]);
  241. }
  242. if (mask & kFLEXIO_UART_RxDataRegFullInterruptEnable)
  243. {
  244. FLEXIO_DisableShifterStatusInterrupts(base->flexioBase, 1U << base->shifterIndex[1]);
  245. }
  246. }
  247. uint32_t FLEXIO_UART_GetStatusFlags(FLEXIO_UART_Type *base)
  248. {
  249. uint32_t status = 0;
  250. status =
  251. ((FLEXIO_GetShifterStatusFlags(base->flexioBase) & (1U << base->shifterIndex[0])) >> base->shifterIndex[0]);
  252. status |=
  253. (((FLEXIO_GetShifterStatusFlags(base->flexioBase) & (1U << base->shifterIndex[1])) >> (base->shifterIndex[1]))
  254. << 1U);
  255. status |=
  256. (((FLEXIO_GetShifterErrorFlags(base->flexioBase) & (1U << base->shifterIndex[1])) >> (base->shifterIndex[1]))
  257. << 2U);
  258. return status;
  259. }
  260. void FLEXIO_UART_ClearStatusFlags(FLEXIO_UART_Type *base, uint32_t mask)
  261. {
  262. if (mask & kFLEXIO_UART_TxDataRegEmptyFlag)
  263. {
  264. FLEXIO_ClearShifterStatusFlags(base->flexioBase, 1U << base->shifterIndex[0]);
  265. }
  266. if (mask & kFLEXIO_UART_RxDataRegFullFlag)
  267. {
  268. FLEXIO_ClearShifterStatusFlags(base->flexioBase, 1U << base->shifterIndex[1]);
  269. }
  270. if (mask & kFLEXIO_UART_RxOverRunFlag)
  271. {
  272. FLEXIO_ClearShifterErrorFlags(base->flexioBase, 1U << base->shifterIndex[1]);
  273. }
  274. }
  275. void FLEXIO_UART_WriteBlocking(FLEXIO_UART_Type *base, const uint8_t *txData, size_t txSize)
  276. {
  277. assert(txData);
  278. assert(txSize);
  279. while (txSize--)
  280. {
  281. /* Wait until data transfer complete. */
  282. while (!(FLEXIO_GetShifterStatusFlags(base->flexioBase) & (1U << base->shifterIndex[0])))
  283. {
  284. }
  285. base->flexioBase->SHIFTBUF[base->shifterIndex[0]] = *txData++;
  286. }
  287. }
  288. void FLEXIO_UART_ReadBlocking(FLEXIO_UART_Type *base, uint8_t *rxData, size_t rxSize)
  289. {
  290. assert(rxData);
  291. assert(rxSize);
  292. while (rxSize--)
  293. {
  294. /* Wait until data transfer complete. */
  295. while (!(FLEXIO_UART_GetStatusFlags(base) & kFLEXIO_UART_RxDataRegFullFlag))
  296. {
  297. }
  298. *rxData++ = base->flexioBase->SHIFTBUFBYS[base->shifterIndex[1]];
  299. }
  300. }
  301. status_t FLEXIO_UART_TransferCreateHandle(FLEXIO_UART_Type *base,
  302. flexio_uart_handle_t *handle,
  303. flexio_uart_transfer_callback_t callback,
  304. void *userData)
  305. {
  306. assert(handle);
  307. IRQn_Type flexio_irqs[] = FLEXIO_IRQS;
  308. /* Zero the handle. */
  309. memset(handle, 0, sizeof(*handle));
  310. /* Set the TX/RX state. */
  311. handle->rxState = kFLEXIO_UART_RxIdle;
  312. handle->txState = kFLEXIO_UART_TxIdle;
  313. /* Set the callback and user data. */
  314. handle->callback = callback;
  315. handle->userData = userData;
  316. /* Enable interrupt in NVIC. */
  317. EnableIRQ(flexio_irqs[FLEXIO_UART_GetInstance(base)]);
  318. /* Save the context in global variables to support the double weak mechanism. */
  319. return FLEXIO_RegisterHandleIRQ(base, handle, FLEXIO_UART_TransferHandleIRQ);
  320. }
  321. void FLEXIO_UART_TransferStartRingBuffer(FLEXIO_UART_Type *base,
  322. flexio_uart_handle_t *handle,
  323. uint8_t *ringBuffer,
  324. size_t ringBufferSize)
  325. {
  326. assert(handle);
  327. /* Setup the ringbuffer address */
  328. if (ringBuffer)
  329. {
  330. handle->rxRingBuffer = ringBuffer;
  331. handle->rxRingBufferSize = ringBufferSize;
  332. handle->rxRingBufferHead = 0U;
  333. handle->rxRingBufferTail = 0U;
  334. /* Enable the interrupt to accept the data when user need the ring buffer. */
  335. FLEXIO_UART_EnableInterrupts(base, kFLEXIO_UART_RxDataRegFullInterruptEnable);
  336. }
  337. }
  338. void FLEXIO_UART_TransferStopRingBuffer(FLEXIO_UART_Type *base, flexio_uart_handle_t *handle)
  339. {
  340. assert(handle);
  341. if (handle->rxState == kFLEXIO_UART_RxIdle)
  342. {
  343. FLEXIO_UART_DisableInterrupts(base, kFLEXIO_UART_RxDataRegFullInterruptEnable);
  344. }
  345. handle->rxRingBuffer = NULL;
  346. handle->rxRingBufferSize = 0U;
  347. handle->rxRingBufferHead = 0U;
  348. handle->rxRingBufferTail = 0U;
  349. }
  350. status_t FLEXIO_UART_TransferSendNonBlocking(FLEXIO_UART_Type *base,
  351. flexio_uart_handle_t *handle,
  352. flexio_uart_transfer_t *xfer)
  353. {
  354. status_t status;
  355. /* Return error if xfer invalid. */
  356. if ((0U == xfer->dataSize) || (NULL == xfer->data))
  357. {
  358. return kStatus_InvalidArgument;
  359. }
  360. /* Return error if current TX busy. */
  361. if (kFLEXIO_UART_TxBusy == handle->txState)
  362. {
  363. status = kStatus_FLEXIO_UART_TxBusy;
  364. }
  365. else
  366. {
  367. handle->txData = xfer->data;
  368. handle->txDataSize = xfer->dataSize;
  369. handle->txDataSizeAll = xfer->dataSize;
  370. handle->txState = kFLEXIO_UART_TxBusy;
  371. /* Enable transmiter interrupt. */
  372. FLEXIO_UART_EnableInterrupts(base, kFLEXIO_UART_TxDataRegEmptyInterruptEnable);
  373. status = kStatus_Success;
  374. }
  375. return status;
  376. }
  377. void FLEXIO_UART_TransferAbortSend(FLEXIO_UART_Type *base, flexio_uart_handle_t *handle)
  378. {
  379. /* Disable the transmitter and disable the interrupt. */
  380. FLEXIO_UART_DisableInterrupts(base, kFLEXIO_UART_TxDataRegEmptyInterruptEnable);
  381. handle->txDataSize = 0;
  382. handle->txState = kFLEXIO_UART_TxIdle;
  383. }
  384. status_t FLEXIO_UART_TransferGetSendCount(FLEXIO_UART_Type *base, flexio_uart_handle_t *handle, size_t *count)
  385. {
  386. assert(handle);
  387. assert(count);
  388. if (kFLEXIO_UART_TxIdle == handle->txState)
  389. {
  390. return kStatus_NoTransferInProgress;
  391. }
  392. *count = handle->txDataSizeAll - handle->txDataSize;
  393. return kStatus_Success;
  394. }
  395. status_t FLEXIO_UART_TransferReceiveNonBlocking(FLEXIO_UART_Type *base,
  396. flexio_uart_handle_t *handle,
  397. flexio_uart_transfer_t *xfer,
  398. size_t *receivedBytes)
  399. {
  400. uint32_t i;
  401. status_t status;
  402. /* How many bytes to copy from ring buffer to user memory. */
  403. size_t bytesToCopy = 0U;
  404. /* How many bytes to receive. */
  405. size_t bytesToReceive;
  406. /* How many bytes currently have received. */
  407. size_t bytesCurrentReceived;
  408. /* Return error if xfer invalid. */
  409. if ((0U == xfer->dataSize) || (NULL == xfer->data))
  410. {
  411. return kStatus_InvalidArgument;
  412. }
  413. /* How to get data:
  414. 1. If RX ring buffer is not enabled, then save xfer->data and xfer->dataSize
  415. to uart handle, enable interrupt to store received data to xfer->data. When
  416. all data received, trigger callback.
  417. 2. If RX ring buffer is enabled and not empty, get data from ring buffer first.
  418. If there are enough data in ring buffer, copy them to xfer->data and return.
  419. If there are not enough data in ring buffer, copy all of them to xfer->data,
  420. save the xfer->data remained empty space to uart handle, receive data
  421. to this empty space and trigger callback when finished. */
  422. if (kFLEXIO_UART_RxBusy == handle->rxState)
  423. {
  424. status = kStatus_FLEXIO_UART_RxBusy;
  425. }
  426. else
  427. {
  428. bytesToReceive = xfer->dataSize;
  429. bytesCurrentReceived = 0U;
  430. /* If RX ring buffer is used. */
  431. if (handle->rxRingBuffer)
  432. {
  433. /* Disable FLEXIO_UART RX IRQ, protect ring buffer. */
  434. FLEXIO_UART_DisableInterrupts(base, kFLEXIO_UART_RxDataRegFullInterruptEnable);
  435. /* How many bytes in RX ring buffer currently. */
  436. bytesToCopy = FLEXIO_UART_TransferGetRxRingBufferLength(handle);
  437. if (bytesToCopy)
  438. {
  439. bytesToCopy = MIN(bytesToReceive, bytesToCopy);
  440. bytesToReceive -= bytesToCopy;
  441. /* Copy data from ring buffer to user memory. */
  442. for (i = 0U; i < bytesToCopy; i++)
  443. {
  444. xfer->data[bytesCurrentReceived++] = handle->rxRingBuffer[handle->rxRingBufferTail];
  445. /* Wrap to 0. Not use modulo (%) because it might be large and slow. */
  446. if (handle->rxRingBufferTail + 1U == handle->rxRingBufferSize)
  447. {
  448. handle->rxRingBufferTail = 0U;
  449. }
  450. else
  451. {
  452. handle->rxRingBufferTail++;
  453. }
  454. }
  455. }
  456. /* If ring buffer does not have enough data, still need to read more data. */
  457. if (bytesToReceive)
  458. {
  459. /* No data in ring buffer, save the request to UART handle. */
  460. handle->rxData = xfer->data + bytesCurrentReceived;
  461. handle->rxDataSize = bytesToReceive;
  462. handle->rxDataSizeAll = bytesToReceive;
  463. handle->rxState = kFLEXIO_UART_RxBusy;
  464. }
  465. /* Enable FLEXIO_UART RX IRQ if previously enabled. */
  466. FLEXIO_UART_EnableInterrupts(base, kFLEXIO_UART_RxDataRegFullInterruptEnable);
  467. }
  468. /* Ring buffer not used. */
  469. else
  470. {
  471. handle->rxData = xfer->data + bytesCurrentReceived;
  472. handle->rxDataSize = bytesToReceive;
  473. handle->rxDataSizeAll = bytesToReceive;
  474. handle->rxState = kFLEXIO_UART_RxBusy;
  475. /* Enable RX interrupt. */
  476. FLEXIO_UART_EnableInterrupts(base, kFLEXIO_UART_RxDataRegFullInterruptEnable);
  477. }
  478. /* Return the how many bytes have read. */
  479. if (receivedBytes)
  480. {
  481. *receivedBytes = bytesCurrentReceived;
  482. }
  483. status = kStatus_Success;
  484. }
  485. return status;
  486. }
  487. void FLEXIO_UART_TransferAbortReceive(FLEXIO_UART_Type *base, flexio_uart_handle_t *handle)
  488. {
  489. /* Only abort the receive to handle->rxData, the RX ring buffer is still working. */
  490. if (!handle->rxRingBuffer)
  491. {
  492. /* Disable RX interrupt. */
  493. FLEXIO_UART_DisableInterrupts(base, kFLEXIO_UART_RxDataRegFullInterruptEnable);
  494. }
  495. handle->rxDataSize = 0U;
  496. handle->rxState = kFLEXIO_UART_RxIdle;
  497. }
  498. status_t FLEXIO_UART_TransferGetReceiveCount(FLEXIO_UART_Type *base, flexio_uart_handle_t *handle, size_t *count)
  499. {
  500. assert(handle);
  501. assert(count);
  502. if (kFLEXIO_UART_RxIdle == handle->rxState)
  503. {
  504. return kStatus_NoTransferInProgress;
  505. }
  506. *count = handle->rxDataSizeAll - handle->rxDataSize;
  507. return kStatus_Success;
  508. }
  509. void FLEXIO_UART_TransferHandleIRQ(void *uartType, void *uartHandle)
  510. {
  511. uint8_t count = 1;
  512. FLEXIO_UART_Type *base = (FLEXIO_UART_Type *)uartType;
  513. flexio_uart_handle_t *handle = (flexio_uart_handle_t *)uartHandle;
  514. /* Read the status back. */
  515. uint8_t status = FLEXIO_UART_GetStatusFlags(base);
  516. /* If RX overrun. */
  517. if (kFLEXIO_UART_RxOverRunFlag & status)
  518. {
  519. /* Clear Overrun flag. */
  520. FLEXIO_UART_ClearStatusFlags(base, kFLEXIO_UART_RxOverRunFlag);
  521. /* Trigger callback. */
  522. if (handle->callback)
  523. {
  524. handle->callback(base, handle, kStatus_FLEXIO_UART_RxHardwareOverrun, handle->userData);
  525. }
  526. }
  527. /* Receive data register full */
  528. if ((kFLEXIO_UART_RxDataRegFullFlag & status) && (base->flexioBase->SHIFTSIEN & (1U << base->shifterIndex[1])))
  529. {
  530. /* If handle->rxDataSize is not 0, first save data to handle->rxData. */
  531. if (handle->rxDataSize)
  532. {
  533. /* Using non block API to read the data from the registers. */
  534. FLEXIO_UART_ReadByte(base, handle->rxData);
  535. handle->rxDataSize--;
  536. handle->rxData++;
  537. count--;
  538. /* If all the data required for upper layer is ready, trigger callback. */
  539. if (!handle->rxDataSize)
  540. {
  541. handle->rxState = kFLEXIO_UART_RxIdle;
  542. if (handle->callback)
  543. {
  544. handle->callback(base, handle, kStatus_FLEXIO_UART_RxIdle, handle->userData);
  545. }
  546. }
  547. }
  548. if (handle->rxRingBuffer)
  549. {
  550. if (count)
  551. {
  552. /* If RX ring buffer is full, trigger callback to notify over run. */
  553. if (FLEXIO_UART_TransferIsRxRingBufferFull(handle))
  554. {
  555. if (handle->callback)
  556. {
  557. handle->callback(base, handle, kStatus_FLEXIO_UART_RxRingBufferOverrun, handle->userData);
  558. }
  559. }
  560. /* If ring buffer is still full after callback function, the oldest data is overrided. */
  561. if (FLEXIO_UART_TransferIsRxRingBufferFull(handle))
  562. {
  563. /* Increase handle->rxRingBufferTail to make room for new data. */
  564. if (handle->rxRingBufferTail + 1U == handle->rxRingBufferSize)
  565. {
  566. handle->rxRingBufferTail = 0U;
  567. }
  568. else
  569. {
  570. handle->rxRingBufferTail++;
  571. }
  572. }
  573. /* Read data. */
  574. handle->rxRingBuffer[handle->rxRingBufferHead] = base->flexioBase->SHIFTBUFBYS[base->shifterIndex[1]];
  575. /* Increase handle->rxRingBufferHead. */
  576. if (handle->rxRingBufferHead + 1U == handle->rxRingBufferSize)
  577. {
  578. handle->rxRingBufferHead = 0U;
  579. }
  580. else
  581. {
  582. handle->rxRingBufferHead++;
  583. }
  584. }
  585. }
  586. /* If no receive requst pending, stop RX interrupt. */
  587. else if (!handle->rxDataSize)
  588. {
  589. FLEXIO_UART_DisableInterrupts(base, kFLEXIO_UART_RxDataRegFullInterruptEnable);
  590. }
  591. else
  592. {
  593. }
  594. }
  595. /* Send data register empty and the interrupt is enabled. */
  596. if ((kFLEXIO_UART_TxDataRegEmptyFlag & status) && (base->flexioBase->SHIFTSIEN & (1U << base->shifterIndex[0])))
  597. {
  598. if (handle->txDataSize)
  599. {
  600. /* Using non block API to write the data to the registers. */
  601. FLEXIO_UART_WriteByte(base, handle->txData);
  602. handle->txData++;
  603. handle->txDataSize--;
  604. count--;
  605. /* If all the data are written to data register, TX finished. */
  606. if (!handle->txDataSize)
  607. {
  608. handle->txState = kFLEXIO_UART_TxIdle;
  609. /* Disable TX register empty interrupt. */
  610. FLEXIO_UART_DisableInterrupts(base, kFLEXIO_UART_TxDataRegEmptyInterruptEnable);
  611. /* Trigger callback. */
  612. if (handle->callback)
  613. {
  614. handle->callback(base, handle, kStatus_FLEXIO_UART_TxIdle, handle->userData);
  615. }
  616. }
  617. }
  618. }
  619. }