1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360 |
- #include "fsl_uart.h"
- enum _uart_tansfer_states
- {
- kUART_TxIdle,
- kUART_TxBusy,
- kUART_RxIdle,
- kUART_RxBusy,
- kUART_RxFramingError,
- kUART_RxParityError
- };
- typedef void (*uart_isr_t)(UART_Type *base, uart_handle_t *handle);
- uint32_t UART_GetInstance(UART_Type *base);
- static bool UART_TransferIsRxRingBufferFull(uart_handle_t *handle);
- static void UART_ReadNonBlocking(UART_Type *base, uint8_t *data, size_t length);
- static void UART_WriteNonBlocking(UART_Type *base, const uint8_t *data, size_t length);
- #if (defined(UART5))
- #define UART_HANDLE_ARRAY_SIZE 6
- #else
- #if (defined(UART4))
- #define UART_HANDLE_ARRAY_SIZE 5
- #else
- #if (defined(UART3))
- #define UART_HANDLE_ARRAY_SIZE 4
- #else
- #if (defined(UART2))
- #define UART_HANDLE_ARRAY_SIZE 3
- #else
- #if (defined(UART1))
- #define UART_HANDLE_ARRAY_SIZE 2
- #else
- #if (defined(UART0))
- #define UART_HANDLE_ARRAY_SIZE 1
- #else
- #error No UART instance.
- #endif
- #endif
- #endif
- #endif
- #endif
- #endif
- static uart_handle_t *s_uartHandle[UART_HANDLE_ARRAY_SIZE];
- static UART_Type *const s_uartBases[] = UART_BASE_PTRS;
- static const IRQn_Type s_uartIRQ[] = UART_RX_TX_IRQS;
- #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
- static const clock_ip_name_t s_uartClock[] = UART_CLOCKS;
- #endif
- static uart_isr_t s_uartIsr;
- uint32_t UART_GetInstance(UART_Type *base)
- {
- uint32_t instance;
- uint32_t uartArrayCount = (sizeof(s_uartBases) / sizeof(s_uartBases[0]));
-
- for (instance = 0; instance < uartArrayCount; instance++)
- {
- if (s_uartBases[instance] == base)
- {
- break;
- }
- }
- assert(instance < uartArrayCount);
- return instance;
- }
- size_t UART_TransferGetRxRingBufferLength(uart_handle_t *handle)
- {
- assert(handle);
- size_t size;
- if (handle->rxRingBufferTail > handle->rxRingBufferHead)
- {
- size = (size_t)(handle->rxRingBufferHead + handle->rxRingBufferSize - handle->rxRingBufferTail);
- }
- else
- {
- size = (size_t)(handle->rxRingBufferHead - handle->rxRingBufferTail);
- }
- return size;
- }
- static bool UART_TransferIsRxRingBufferFull(uart_handle_t *handle)
- {
- assert(handle);
- bool full;
- if (UART_TransferGetRxRingBufferLength(handle) == (handle->rxRingBufferSize - 1U))
- {
- full = true;
- }
- else
- {
- full = false;
- }
- return full;
- }
- status_t UART_Init(UART_Type *base, const uart_config_t *config, uint32_t srcClock_Hz)
- {
- assert(config);
- assert(config->baudRate_Bps);
- #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
- assert(FSL_FEATURE_UART_FIFO_SIZEn(base) >= config->txFifoWatermark);
- assert(FSL_FEATURE_UART_FIFO_SIZEn(base) >= config->rxFifoWatermark);
- #endif
- uint16_t sbr = 0;
- uint8_t temp = 0;
- uint32_t baudDiff = 0;
-
- sbr = srcClock_Hz / (config->baudRate_Bps * 16);
-
- if (sbr == 0)
- {
- sbr = 1;
- }
- #if defined(FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT) && FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT
-
- uint32_t tempBaud = 0;
- uint16_t brfa = (2 * srcClock_Hz / (config->baudRate_Bps)) - 32 * sbr;
-
- tempBaud = (srcClock_Hz * 2 / ((sbr * 32 + brfa)));
- baudDiff =
- (tempBaud > config->baudRate_Bps) ? (tempBaud - config->baudRate_Bps) : (config->baudRate_Bps - tempBaud);
- #else
-
- baudDiff = (srcClock_Hz / (sbr * 16)) - config->baudRate_Bps;
-
- if (baudDiff > (config->baudRate_Bps - (srcClock_Hz / (16 * (sbr + 1)))))
- {
- baudDiff = config->baudRate_Bps - (srcClock_Hz / (16 * (sbr + 1)));
- sbr++;
- }
- #endif
-
- if (baudDiff > ((config->baudRate_Bps / 100) * 3))
- {
-
- return kStatus_UART_BaudrateNotSupport;
- }
- #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
-
- CLOCK_EnableClock(s_uartClock[UART_GetInstance(base)]);
- #endif
-
- base->C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK);
-
- base->BDH = (base->BDH & ~UART_BDH_SBR_MASK) | (uint8_t)(sbr >> 8);
- base->BDL = (uint8_t)sbr;
- #if defined(FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT) && FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT
-
- base->C4 = (base->C4 & ~UART_C4_BRFA_MASK) | (brfa & UART_C4_BRFA_MASK);
- #endif
-
- temp = base->C1 & ~(UART_C1_PE_MASK | UART_C1_PT_MASK | UART_C1_M_MASK | UART_C1_ILT_MASK);
- temp |= UART_C1_ILT(config->idleType);
- if (kUART_ParityDisabled != config->parityMode)
- {
- temp |= (UART_C1_M_MASK | (uint8_t)config->parityMode);
- }
- base->C1 = temp;
- #if defined(FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT) && FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT
-
- base->BDH = (base->BDH & ~UART_BDH_SBNS_MASK) | UART_BDH_SBNS((uint8_t)config->stopBitCount);
- #endif
- #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
-
- base->TWFIFO = config->txFifoWatermark;
- base->RWFIFO = config->rxFifoWatermark;
-
- base->PFIFO |= (UART_PFIFO_TXFE_MASK | UART_PFIFO_RXFE_MASK);
-
- base->CFIFO |= (UART_CFIFO_TXFLUSH_MASK | UART_CFIFO_RXFLUSH_MASK);
- #endif
- #if defined(FSL_FEATURE_UART_HAS_MODEM_SUPPORT) && FSL_FEATURE_UART_HAS_MODEM_SUPPORT
- if (config->enableRxRTS)
- {
-
- base->MODEM |= UART_MODEM_RXRTSE_MASK;
- }
- if (config->enableTxCTS)
- {
-
- base->MODEM |= UART_MODEM_TXCTSE_MASK;
- }
- #endif
-
- temp = base->C2;
- if (config->enableTx)
- {
- temp |= UART_C2_TE_MASK;
- }
- if (config->enableRx)
- {
- temp |= UART_C2_RE_MASK;
- }
- base->C2 = temp;
- return kStatus_Success;
- }
- void UART_Deinit(UART_Type *base)
- {
- #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
-
- while (0 != base->TCFIFO)
- {
- }
- #endif
-
- while (0 == (base->S1 & UART_S1_TC_MASK))
- {
- }
-
- base->C2 = 0;
- #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
-
- CLOCK_DisableClock(s_uartClock[UART_GetInstance(base)]);
- #endif
- }
- void UART_GetDefaultConfig(uart_config_t *config)
- {
- assert(config);
- config->baudRate_Bps = 115200U;
- config->parityMode = kUART_ParityDisabled;
- #if defined(FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT) && FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT
- config->stopBitCount = kUART_OneStopBit;
- #endif
- #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
- config->txFifoWatermark = 0;
- config->rxFifoWatermark = 1;
- #endif
- #if defined(FSL_FEATURE_UART_HAS_MODEM_SUPPORT) && FSL_FEATURE_UART_HAS_MODEM_SUPPORT
- config->enableRxRTS = false;
- config->enableTxCTS = false;
- #endif
- config->idleType = kUART_IdleTypeStartBit;
- config->enableTx = false;
- config->enableRx = false;
- }
- status_t UART_SetBaudRate(UART_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz)
- {
- assert(baudRate_Bps);
- uint16_t sbr = 0;
- uint32_t baudDiff = 0;
- uint8_t oldCtrl;
-
- sbr = srcClock_Hz / (baudRate_Bps * 16);
-
- if (sbr == 0)
- {
- sbr = 1;
- }
- #if defined(FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT) && FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT
-
- uint32_t tempBaud = 0;
- uint16_t brfa = (2 * srcClock_Hz / (baudRate_Bps)) - 32 * sbr;
-
- tempBaud = (srcClock_Hz * 2 / ((sbr * 32 + brfa)));
- baudDiff = (tempBaud > baudRate_Bps) ? (tempBaud - baudRate_Bps) : (baudRate_Bps - tempBaud);
- #else
-
- baudDiff = (srcClock_Hz / (sbr * 16)) - baudRate_Bps;
-
- if (baudDiff > (baudRate_Bps - (srcClock_Hz / (16 * (sbr + 1)))))
- {
- baudDiff = baudRate_Bps - (srcClock_Hz / (16 * (sbr + 1)));
- sbr++;
- }
- #endif
-
- if (baudDiff < ((baudRate_Bps / 100) * 3))
- {
-
- oldCtrl = base->C2;
-
- base->C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK);
-
- base->BDH = (base->BDH & ~UART_BDH_SBR_MASK) | (uint8_t)(sbr >> 8);
- base->BDL = (uint8_t)sbr;
- #if defined(FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT) && FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT
-
- base->C4 = (base->C4 & ~UART_C4_BRFA_MASK) | (brfa & UART_C4_BRFA_MASK);
- #endif
-
- base->C2 = oldCtrl;
- return kStatus_Success;
- }
- else
- {
-
- return kStatus_UART_BaudrateNotSupport;
- }
- }
- void UART_EnableInterrupts(UART_Type *base, uint32_t mask)
- {
- mask &= kUART_AllInterruptsEnable;
-
- base->BDH |= mask;
- base->C2 |= (mask >> 8);
- base->C3 |= (mask >> 16);
- #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
- base->CFIFO |= (mask >> 24);
- #endif
- }
- void UART_DisableInterrupts(UART_Type *base, uint32_t mask)
- {
- mask &= kUART_AllInterruptsEnable;
-
- base->BDH &= ~mask;
- base->C2 &= ~(mask >> 8);
- base->C3 &= ~(mask >> 16);
- #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
- base->CFIFO &= ~(mask >> 24);
- #endif
- }
- uint32_t UART_GetEnabledInterrupts(UART_Type *base)
- {
- uint32_t temp;
- temp = base->BDH | ((uint32_t)(base->C2) << 8) | ((uint32_t)(base->C3) << 16);
- #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
- temp |= ((uint32_t)(base->CFIFO) << 24);
- #endif
- return temp & kUART_AllInterruptsEnable;
- }
- uint32_t UART_GetStatusFlags(UART_Type *base)
- {
- uint32_t status_flag;
- status_flag = base->S1 | ((uint32_t)(base->S2) << 8);
- #if defined(FSL_FEATURE_UART_HAS_EXTENDED_DATA_REGISTER_FLAGS) && FSL_FEATURE_UART_HAS_EXTENDED_DATA_REGISTER_FLAGS
- status_flag |= ((uint32_t)(base->ED) << 16);
- #endif
- #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
- status_flag |= ((uint32_t)(base->SFIFO) << 24);
- #endif
- return status_flag;
- }
- status_t UART_ClearStatusFlags(UART_Type *base, uint32_t mask)
- {
- uint8_t reg = base->S2;
- status_t status;
- #if defined(FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT
- reg &= ~(UART_S2_RXEDGIF_MASK | UART_S2_LBKDIF_MASK);
- #else
- reg &= ~UART_S2_RXEDGIF_MASK;
- #endif
- base->S2 = reg | (uint8_t)(mask >> 8);
- #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
- base->SFIFO = (uint8_t)(mask >> 24);
- #endif
- if (mask & (kUART_IdleLineFlag | kUART_NoiseErrorFlag | kUART_FramingErrorFlag | kUART_ParityErrorFlag))
- {
-
- (void)base->S1;
- (void)base->D;
- }
- if (mask & kUART_RxOverrunFlag)
- {
-
- (void)base->S1;
- (void)base->D;
- #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
-
- base->CFIFO |= UART_CFIFO_RXFLUSH_MASK;
- #endif
- }
-
- if (mask & UART_GetStatusFlags(base))
- {
-
- status = kStatus_UART_FlagCannotClearManually;
- }
- else
- {
- status = kStatus_Success;
- }
- return status;
- }
- void UART_WriteBlocking(UART_Type *base, const uint8_t *data, size_t length)
- {
-
- while (length--)
- {
- while (!(base->S1 & UART_S1_TDRE_MASK))
- {
- }
- base->D = *(data++);
- }
- }
- static void UART_WriteNonBlocking(UART_Type *base, const uint8_t *data, size_t length)
- {
- assert(data);
- size_t i;
-
- for (i = 0; i < length; i++)
- {
- base->D = data[i];
- }
- }
- status_t UART_ReadBlocking(UART_Type *base, uint8_t *data, size_t length)
- {
- assert(data);
- uint32_t statusFlag;
- while (length--)
- {
- #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
- while (!base->RCFIFO)
- #else
- while (!(base->S1 & UART_S1_RDRF_MASK))
- #endif
- {
- statusFlag = UART_GetStatusFlags(base);
- if (statusFlag & kUART_RxOverrunFlag)
- {
- return kStatus_UART_RxHardwareOverrun;
- }
- if (statusFlag & kUART_NoiseErrorFlag)
- {
- return kStatus_UART_NoiseError;
- }
- if (statusFlag & kUART_FramingErrorFlag)
- {
- return kStatus_UART_FramingError;
- }
- if (statusFlag & kUART_ParityErrorFlag)
- {
- return kStatus_UART_ParityError;
- }
- }
- *(data++) = base->D;
- }
- return kStatus_Success;
- }
- static void UART_ReadNonBlocking(UART_Type *base, uint8_t *data, size_t length)
- {
- assert(data);
- size_t i;
-
- for (i = 0; i < length; i++)
- {
- data[i] = base->D;
- }
- }
- void UART_TransferCreateHandle(UART_Type *base,
- uart_handle_t *handle,
- uart_transfer_callback_t callback,
- void *userData)
- {
- assert(handle);
- uint32_t instance;
-
- memset(handle, 0, sizeof(*handle));
-
- handle->rxState = kUART_RxIdle;
- handle->txState = kUART_TxIdle;
-
- handle->callback = callback;
- handle->userData = userData;
-
- instance = UART_GetInstance(base);
-
- s_uartHandle[instance] = handle;
- s_uartIsr = UART_TransferHandleIRQ;
-
- EnableIRQ(s_uartIRQ[instance]);
- }
- void UART_TransferStartRingBuffer(UART_Type *base, uart_handle_t *handle, uint8_t *ringBuffer, size_t ringBufferSize)
- {
- assert(handle);
- assert(ringBuffer);
-
- handle->rxRingBuffer = ringBuffer;
- handle->rxRingBufferSize = ringBufferSize;
- handle->rxRingBufferHead = 0U;
- handle->rxRingBufferTail = 0U;
-
- UART_EnableInterrupts(
- base, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable | kUART_FramingErrorInterruptEnable);
-
- if (UART_C1_PE_MASK & base->C1)
- {
- UART_EnableInterrupts(base, kUART_ParityErrorInterruptEnable);
- }
- }
- void UART_TransferStopRingBuffer(UART_Type *base, uart_handle_t *handle)
- {
- assert(handle);
- if (handle->rxState == kUART_RxIdle)
- {
- UART_DisableInterrupts(base, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable |
- kUART_FramingErrorInterruptEnable);
-
- if (UART_C1_PE_MASK & base->C1)
- {
- UART_DisableInterrupts(base, kUART_ParityErrorInterruptEnable);
- }
- }
- handle->rxRingBuffer = NULL;
- handle->rxRingBufferSize = 0U;
- handle->rxRingBufferHead = 0U;
- handle->rxRingBufferTail = 0U;
- }
- status_t UART_TransferSendNonBlocking(UART_Type *base, uart_handle_t *handle, uart_transfer_t *xfer)
- {
- assert(handle);
- assert(xfer);
- assert(xfer->dataSize);
- assert(xfer->data);
- status_t status;
-
- if (kUART_TxBusy == handle->txState)
- {
- status = kStatus_UART_TxBusy;
- }
- else
- {
- handle->txData = xfer->data;
- handle->txDataSize = xfer->dataSize;
- handle->txDataSizeAll = xfer->dataSize;
- handle->txState = kUART_TxBusy;
-
- UART_EnableInterrupts(base, kUART_TxDataRegEmptyInterruptEnable);
- status = kStatus_Success;
- }
- return status;
- }
- void UART_TransferAbortSend(UART_Type *base, uart_handle_t *handle)
- {
- assert(handle);
- UART_DisableInterrupts(base, kUART_TxDataRegEmptyInterruptEnable | kUART_TransmissionCompleteInterruptEnable);
- handle->txDataSize = 0;
- handle->txState = kUART_TxIdle;
- }
- status_t UART_TransferGetSendCount(UART_Type *base, uart_handle_t *handle, uint32_t *count)
- {
- assert(handle);
- assert(count);
- if (kUART_TxIdle == handle->txState)
- {
- return kStatus_NoTransferInProgress;
- }
- *count = handle->txDataSizeAll - handle->txDataSize;
- return kStatus_Success;
- }
- status_t UART_TransferReceiveNonBlocking(UART_Type *base,
- uart_handle_t *handle,
- uart_transfer_t *xfer,
- size_t *receivedBytes)
- {
- assert(handle);
- assert(xfer);
- assert(xfer->data);
- assert(xfer->dataSize);
- uint32_t i;
- status_t status;
-
- size_t bytesToCopy = 0U;
-
- size_t bytesToReceive;
-
- size_t bytesCurrentReceived;
-
- if (kUART_RxBusy == handle->rxState)
- {
- status = kStatus_UART_RxBusy;
- }
- else
- {
- bytesToReceive = xfer->dataSize;
- bytesCurrentReceived = 0U;
-
- if (handle->rxRingBuffer)
- {
-
- UART_DisableInterrupts(base, kUART_RxDataRegFullInterruptEnable);
-
- bytesToCopy = UART_TransferGetRxRingBufferLength(handle);
- if (bytesToCopy)
- {
- bytesToCopy = MIN(bytesToReceive, bytesToCopy);
- bytesToReceive -= bytesToCopy;
-
- for (i = 0U; i < bytesToCopy; i++)
- {
- xfer->data[bytesCurrentReceived++] = handle->rxRingBuffer[handle->rxRingBufferTail];
-
- if (handle->rxRingBufferTail + 1U == handle->rxRingBufferSize)
- {
- handle->rxRingBufferTail = 0U;
- }
- else
- {
- handle->rxRingBufferTail++;
- }
- }
- }
-
- if (bytesToReceive)
- {
-
- handle->rxData = xfer->data + bytesCurrentReceived;
- handle->rxDataSize = bytesToReceive;
- handle->rxDataSizeAll = bytesToReceive;
- handle->rxState = kUART_RxBusy;
- }
-
- UART_EnableInterrupts(base, kUART_RxDataRegFullInterruptEnable);
-
- if (0 == bytesToReceive)
- {
- if (handle->callback)
- {
- handle->callback(base, handle, kStatus_UART_RxIdle, handle->userData);
- }
- }
- }
-
- else
- {
- handle->rxData = xfer->data + bytesCurrentReceived;
- handle->rxDataSize = bytesToReceive;
- handle->rxDataSizeAll = bytesToReceive;
- handle->rxState = kUART_RxBusy;
-
- UART_EnableInterrupts(base, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable |
- kUART_FramingErrorInterruptEnable | kUART_IdleLineInterruptEnable);
-
- if (UART_C1_PE_MASK & base->C1)
- {
- UART_EnableInterrupts(base, kUART_ParityErrorInterruptEnable);
- }
- }
-
- if (receivedBytes)
- {
- *receivedBytes = bytesCurrentReceived;
- }
- status = kStatus_Success;
- }
- return status;
- }
- void UART_TransferAbortReceive(UART_Type *base, uart_handle_t *handle)
- {
- assert(handle);
-
- if (!handle->rxRingBuffer)
- {
-
- UART_DisableInterrupts(base, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable |
- kUART_FramingErrorInterruptEnable | kUART_IdleLineInterruptEnable);
-
- if (UART_C1_PE_MASK & base->C1)
- {
- UART_DisableInterrupts(base, kUART_ParityErrorInterruptEnable);
- }
- }
- handle->rxDataSize = 0U;
- handle->rxState = kUART_RxIdle;
- }
- status_t UART_TransferGetReceiveCount(UART_Type *base, uart_handle_t *handle, uint32_t *count)
- {
- assert(handle);
- assert(count);
- if (kUART_RxIdle == handle->rxState)
- {
- return kStatus_NoTransferInProgress;
- }
- if (!count)
- {
- return kStatus_InvalidArgument;
- }
- *count = handle->rxDataSizeAll - handle->rxDataSize;
- return kStatus_Success;
- }
- void UART_TransferHandleIRQ(UART_Type *base, uart_handle_t *handle)
- {
- assert(handle);
- uint8_t count;
- uint8_t tempCount;
-
- if (UART_S1_FE_MASK & base->S1)
- {
-
- while (base->S1 & UART_S1_RDRF_MASK)
- {
- (void)base->D;
- }
- #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
-
- base->CFIFO |= UART_CFIFO_RXFLUSH_MASK;
- #endif
- handle->rxState = kUART_RxFramingError;
- handle->rxDataSize = 0U;
-
- if (handle->callback)
- {
- handle->callback(base, handle, kStatus_UART_FramingError, handle->userData);
- }
- }
-
- if (UART_S1_PF_MASK & base->S1)
- {
-
- while (base->S1 & UART_S1_RDRF_MASK)
- {
- (void)base->D;
- }
- #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
-
- base->CFIFO |= UART_CFIFO_RXFLUSH_MASK;
- #endif
- handle->rxState = kUART_RxParityError;
- handle->rxDataSize = 0U;
-
- if (handle->callback)
- {
- handle->callback(base, handle, kStatus_UART_ParityError, handle->userData);
- }
- }
-
- if (UART_S1_OR_MASK & base->S1)
- {
-
- while (base->S1 & UART_S1_RDRF_MASK)
- {
- (void)base->D;
- }
- #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
-
- base->CFIFO |= UART_CFIFO_RXFLUSH_MASK;
- #endif
-
- if (handle->callback)
- {
- handle->callback(base, handle, kStatus_UART_RxHardwareOverrun, handle->userData);
- }
- }
-
- if ((UART_S1_IDLE_MASK & base->S1) && (UART_C2_ILIE_MASK & base->C2))
- {
- #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
-
- count = base->RCFIFO;
-
- while ((count) && (handle->rxDataSize))
- {
- tempCount = MIN(handle->rxDataSize, count);
-
- UART_ReadNonBlocking(base, handle->rxData, tempCount);
- handle->rxData += tempCount;
- handle->rxDataSize -= tempCount;
- count -= tempCount;
-
- if (!handle->rxDataSize)
- {
- handle->rxState = kUART_RxIdle;
-
- UART_DisableInterrupts(base, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable |
- kUART_FramingErrorInterruptEnable);
-
- if (UART_C1_PE_MASK & base->C1)
- {
- UART_DisableInterrupts(base, kUART_ParityErrorInterruptEnable);
- }
- if (handle->callback)
- {
- handle->callback(base, handle, kStatus_UART_RxIdle, handle->userData);
- }
- }
- }
- #endif
-
- while (UART_S1_IDLE_MASK & base->S1)
- {
- (void)base->D;
- }
- #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
-
- base->CFIFO |= UART_CFIFO_RXFLUSH_MASK;
- #endif
-
- if (!(handle->rxDataSize))
- {
- UART_DisableInterrupts(base, kUART_IdleLineInterruptEnable);
- }
-
- if ((handle->callback) && (handle->rxDataSize))
- {
- handle->callback(base, handle, kStatus_UART_IdleLineDetected, handle->userData);
- }
- }
-
- if ((UART_S1_RDRF_MASK & base->S1) && (UART_C2_RIE_MASK & base->C2))
- {
- #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
- count = base->RCFIFO;
- #else
- count = 1;
- #endif
-
- while ((count) && (handle->rxDataSize))
- {
- #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
- tempCount = MIN(handle->rxDataSize, count);
- #else
- tempCount = 1;
- #endif
-
- UART_ReadNonBlocking(base, handle->rxData, tempCount);
- handle->rxData += tempCount;
- handle->rxDataSize -= tempCount;
- count -= tempCount;
-
- if (!handle->rxDataSize)
- {
- handle->rxState = kUART_RxIdle;
- if (handle->callback)
- {
- handle->callback(base, handle, kStatus_UART_RxIdle, handle->userData);
- }
- }
- }
-
- if (handle->rxRingBuffer)
- {
- while (count--)
- {
-
- if (UART_TransferIsRxRingBufferFull(handle))
- {
- if (handle->callback)
- {
- handle->callback(base, handle, kStatus_UART_RxRingBufferOverrun, handle->userData);
- }
- }
-
- if (UART_TransferIsRxRingBufferFull(handle))
- {
-
- if (handle->rxRingBufferTail + 1U == handle->rxRingBufferSize)
- {
- handle->rxRingBufferTail = 0U;
- }
- else
- {
- handle->rxRingBufferTail++;
- }
- }
-
- handle->rxRingBuffer[handle->rxRingBufferHead] = base->D;
-
- if (handle->rxRingBufferHead + 1U == handle->rxRingBufferSize)
- {
- handle->rxRingBufferHead = 0U;
- }
- else
- {
- handle->rxRingBufferHead++;
- }
- }
- }
- else if (!handle->rxDataSize)
- {
-
- UART_DisableInterrupts(base, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable |
- kUART_FramingErrorInterruptEnable);
-
- if (UART_C1_PE_MASK & base->C1)
- {
- UART_DisableInterrupts(base, kUART_ParityErrorInterruptEnable);
- }
- }
- else
- {
- }
- }
-
- if (((handle->rxState == kUART_RxFramingError) || (handle->rxState == kUART_RxParityError)) &&
- (!handle->rxRingBuffer))
- {
- UART_DisableInterrupts(base, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable |
- kUART_FramingErrorInterruptEnable | kUART_IdleLineInterruptEnable);
-
- if (UART_C1_PE_MASK & base->C1)
- {
- UART_DisableInterrupts(base, kUART_ParityErrorInterruptEnable);
- }
- }
-
- if ((base->S1 & UART_S1_TDRE_MASK) && (base->C2 & UART_C2_TIE_MASK))
- {
- #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
- count = FSL_FEATURE_UART_FIFO_SIZEn(base) - base->TCFIFO;
- #else
- count = 1;
- #endif
- while ((count) && (handle->txDataSize))
- {
- #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
- tempCount = MIN(handle->txDataSize, count);
- #else
- tempCount = 1;
- #endif
-
- UART_WriteNonBlocking(base, handle->txData, tempCount);
- handle->txData += tempCount;
- handle->txDataSize -= tempCount;
- count -= tempCount;
-
- if (!handle->txDataSize)
- {
- handle->txState = kUART_TxIdle;
-
- base->C2 = (base->C2 & ~UART_C2_TIE_MASK);
-
- if (handle->callback)
- {
- handle->callback(base, handle, kStatus_UART_TxIdle, handle->userData);
- }
- }
- }
- }
- }
- void UART_TransferHandleErrorIRQ(UART_Type *base, uart_handle_t *handle)
- {
-
- }
- #if defined(UART0)
- #if ((!(defined(FSL_FEATURE_SOC_LPSCI_COUNT))) || \
- ((defined(FSL_FEATURE_SOC_LPSCI_COUNT)) && (FSL_FEATURE_SOC_LPSCI_COUNT == 0)))
- void UART0_DriverIRQHandler(void)
- {
- s_uartIsr(UART0, s_uartHandle[0]);
-
- #if defined __CORTEX_M && (__CORTEX_M == 4U)
- __DSB();
- #endif
- }
- void UART0_RX_TX_DriverIRQHandler(void)
- {
- UART0_DriverIRQHandler();
-
- #if defined __CORTEX_M && (__CORTEX_M == 4U)
- __DSB();
- #endif
- }
- #endif
- #endif
- #if defined(UART1)
- void UART1_DriverIRQHandler(void)
- {
- s_uartIsr(UART1, s_uartHandle[1]);
-
- #if defined __CORTEX_M && (__CORTEX_M == 4U)
- __DSB();
- #endif
- }
- void UART1_RX_TX_DriverIRQHandler(void)
- {
- UART1_DriverIRQHandler();
-
- #if defined __CORTEX_M && (__CORTEX_M == 4U)
- __DSB();
- #endif
- }
- #endif
- #if defined(UART2)
- void UART2_DriverIRQHandler(void)
- {
- s_uartIsr(UART2, s_uartHandle[2]);
-
- #if defined __CORTEX_M && (__CORTEX_M == 4U)
- __DSB();
- #endif
- }
- void UART2_RX_TX_DriverIRQHandler(void)
- {
- UART2_DriverIRQHandler();
-
- #if defined __CORTEX_M && (__CORTEX_M == 4U)
- __DSB();
- #endif
- }
- #endif
- #if defined(UART3)
- void UART3_DriverIRQHandler(void)
- {
- s_uartIsr(UART3, s_uartHandle[3]);
-
- #if defined __CORTEX_M && (__CORTEX_M == 4U)
- __DSB();
- #endif
- }
- void UART3_RX_TX_DriverIRQHandler(void)
- {
- UART3_DriverIRQHandler();
-
- #if defined __CORTEX_M && (__CORTEX_M == 4U)
- __DSB();
- #endif
- }
- #endif
- #if defined(UART4)
- void UART4_DriverIRQHandler(void)
- {
- s_uartIsr(UART4, s_uartHandle[4]);
-
- #if defined __CORTEX_M && (__CORTEX_M == 4U)
- __DSB();
- #endif
- }
- void UART4_RX_TX_DriverIRQHandler(void)
- {
- UART4_DriverIRQHandler();
-
- #if defined __CORTEX_M && (__CORTEX_M == 4U)
- __DSB();
- #endif
- }
- #endif
- #if defined(UART5)
- void UART5_DriverIRQHandler(void)
- {
- s_uartIsr(UART5, s_uartHandle[5]);
-
- #if defined __CORTEX_M && (__CORTEX_M == 4U)
- __DSB();
- #endif
- }
- void UART5_RX_TX_DriverIRQHandler(void)
- {
- UART5_DriverIRQHandler();
-
- #if defined __CORTEX_M && (__CORTEX_M == 4U)
- __DSB();
- #endif
- }
- #endif
|