File indexing completed on 2025-05-11 08:22:59
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "fsl_flexio_i2s.h"
0010
0011
0012 #ifndef FSL_COMPONENT_ID
0013 #define FSL_COMPONENT_ID "platform.drivers.flexio_i2s"
0014 #endif
0015
0016
0017
0018
0019
0020 enum
0021 {
0022 kFLEXIO_I2S_Busy = 0x0U,
0023 kFLEXIO_I2S_Idle,
0024 };
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 static void FLEXIO_I2S_ReadNonBlocking(FLEXIO_I2S_Type *base, uint8_t bitWidth, uint8_t *rxData, size_t size);
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 static void FLEXIO_I2S_WriteNonBlocking(FLEXIO_I2S_Type *base, uint8_t bitWidth, uint8_t *txData, size_t size);
0049
0050
0051
0052
0053
0054
0055
0056
0057 static uint32_t FLEXIO_I2S_GetInstance(FLEXIO_I2S_Type *base)
0058 {
0059 return FLEXIO_GetInstance(base->flexioBase);
0060 }
0061
0062 static void FLEXIO_I2S_WriteNonBlocking(FLEXIO_I2S_Type *base, uint8_t bitWidth, uint8_t *txData, size_t size)
0063 {
0064 uint32_t i = 0;
0065 uint8_t j = 0;
0066 uint8_t bytesPerWord = bitWidth / 8U;
0067 uint32_t data = 0;
0068 uint32_t temp = 0;
0069
0070 for (i = 0; i < size / bytesPerWord; i++)
0071 {
0072 for (j = 0; j < bytesPerWord; j++)
0073 {
0074 temp = (uint32_t)(*txData);
0075 data |= (temp << (8U * j));
0076 txData++;
0077 }
0078 base->flexioBase->SHIFTBUFBIS[base->txShifterIndex] = data << (32U - bitWidth);
0079 data = 0;
0080 }
0081 }
0082
0083 static void FLEXIO_I2S_ReadNonBlocking(FLEXIO_I2S_Type *base, uint8_t bitWidth, uint8_t *rxData, size_t size)
0084 {
0085 uint32_t i = 0;
0086 uint8_t j = 0;
0087 uint8_t bytesPerWord = bitWidth / 8U;
0088 uint32_t data = 0;
0089
0090 for (i = 0; i < size / bytesPerWord; i++)
0091 {
0092 data = (base->flexioBase->SHIFTBUFBIS[base->rxShifterIndex]);
0093 for (j = 0; j < bytesPerWord; j++)
0094 {
0095 *rxData = (uint8_t)((data >> (8U * j)) & 0xFFU);
0096 rxData++;
0097 }
0098 }
0099 }
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115 void FLEXIO_I2S_Init(FLEXIO_I2S_Type *base, const flexio_i2s_config_t *config)
0116 {
0117 assert((base != NULL) && (config != NULL));
0118
0119 flexio_shifter_config_t shifterConfig = {0};
0120 flexio_timer_config_t timerConfig = {0};
0121
0122 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
0123
0124 CLOCK_EnableClock(s_flexioClocks[FLEXIO_I2S_GetInstance(base)]);
0125 #endif
0126
0127
0128 FLEXIO_Reset(base->flexioBase);
0129
0130
0131 shifterConfig.timerSelect = base->bclkTimerIndex;
0132 shifterConfig.pinSelect = base->txPinIndex;
0133 shifterConfig.timerPolarity = config->txTimerPolarity;
0134 shifterConfig.pinConfig = kFLEXIO_PinConfigOutput;
0135 shifterConfig.pinPolarity = config->txPinPolarity;
0136 shifterConfig.shifterMode = kFLEXIO_ShifterModeTransmit;
0137 shifterConfig.inputSource = kFLEXIO_ShifterInputFromPin;
0138 shifterConfig.shifterStop = kFLEXIO_ShifterStopBitDisable;
0139 if (config->masterSlave == kFLEXIO_I2S_Master)
0140 {
0141 shifterConfig.shifterStart = kFLEXIO_ShifterStartBitDisabledLoadDataOnShift;
0142 }
0143 else
0144 {
0145 shifterConfig.shifterStart = kFLEXIO_ShifterStartBitDisabledLoadDataOnEnable;
0146 }
0147
0148 FLEXIO_SetShifterConfig(base->flexioBase, base->txShifterIndex, &shifterConfig);
0149
0150
0151 shifterConfig.timerSelect = base->bclkTimerIndex;
0152 shifterConfig.pinSelect = base->rxPinIndex;
0153 shifterConfig.timerPolarity = config->rxTimerPolarity;
0154 shifterConfig.pinConfig = kFLEXIO_PinConfigOutputDisabled;
0155 shifterConfig.pinPolarity = config->rxPinPolarity;
0156 shifterConfig.shifterMode = kFLEXIO_ShifterModeReceive;
0157 shifterConfig.inputSource = kFLEXIO_ShifterInputFromPin;
0158 shifterConfig.shifterStop = kFLEXIO_ShifterStopBitDisable;
0159 shifterConfig.shifterStart = kFLEXIO_ShifterStartBitDisabledLoadDataOnEnable;
0160
0161 FLEXIO_SetShifterConfig(base->flexioBase, base->rxShifterIndex, &shifterConfig);
0162
0163
0164 if (config->masterSlave == kFLEXIO_I2S_Master)
0165 {
0166 timerConfig.triggerSelect = FLEXIO_TIMER_TRIGGER_SEL_PININPUT(base->txPinIndex);
0167 timerConfig.triggerPolarity = kFLEXIO_TimerTriggerPolarityActiveHigh;
0168 timerConfig.triggerSource = kFLEXIO_TimerTriggerSourceExternal;
0169 timerConfig.pinConfig = kFLEXIO_PinConfigOutput;
0170 timerConfig.pinSelect = base->fsPinIndex;
0171 timerConfig.pinPolarity = config->fsPinPolarity;
0172 timerConfig.timerMode = kFLEXIO_TimerModeSingle16Bit;
0173 timerConfig.timerOutput = kFLEXIO_TimerOutputOneNotAffectedByReset;
0174 timerConfig.timerDecrement = kFLEXIO_TimerDecSrcOnFlexIOClockShiftTimerOutput;
0175 timerConfig.timerReset = kFLEXIO_TimerResetNever;
0176 timerConfig.timerDisable = kFLEXIO_TimerDisableNever;
0177 timerConfig.timerEnable = kFLEXIO_TimerEnableOnPrevTimerEnable;
0178 timerConfig.timerStart = kFLEXIO_TimerStartBitDisabled;
0179 timerConfig.timerStop = kFLEXIO_TimerStopBitDisabled;
0180 }
0181 else
0182 {
0183 timerConfig.triggerSelect = FLEXIO_TIMER_TRIGGER_SEL_PININPUT(base->bclkPinIndex);
0184 timerConfig.triggerPolarity = kFLEXIO_TimerTriggerPolarityActiveHigh;
0185 timerConfig.triggerSource = kFLEXIO_TimerTriggerSourceInternal;
0186 timerConfig.pinConfig = kFLEXIO_PinConfigOutputDisabled;
0187 timerConfig.pinSelect = base->fsPinIndex;
0188 timerConfig.pinPolarity = config->fsPinPolarity;
0189 timerConfig.timerMode = kFLEXIO_TimerModeSingle16Bit;
0190 timerConfig.timerOutput = kFLEXIO_TimerOutputOneNotAffectedByReset;
0191 timerConfig.timerDecrement = kFLEXIO_TimerDecSrcOnTriggerInputShiftTriggerInput;
0192 timerConfig.timerReset = kFLEXIO_TimerResetNever;
0193 timerConfig.timerDisable = kFLEXIO_TimerDisableOnTimerCompare;
0194 timerConfig.timerEnable = kFLEXIO_TimerEnableOnPinRisingEdge;
0195 timerConfig.timerStart = kFLEXIO_TimerStartBitDisabled;
0196 timerConfig.timerStop = kFLEXIO_TimerStopBitDisabled;
0197 }
0198 FLEXIO_SetTimerConfig(base->flexioBase, base->fsTimerIndex, &timerConfig);
0199
0200
0201 if (config->masterSlave == kFLEXIO_I2S_Master)
0202 {
0203 timerConfig.triggerSelect = FLEXIO_TIMER_TRIGGER_SEL_SHIFTnSTAT(base->txShifterIndex);
0204 timerConfig.triggerPolarity = kFLEXIO_TimerTriggerPolarityActiveLow;
0205 timerConfig.triggerSource = kFLEXIO_TimerTriggerSourceInternal;
0206 timerConfig.pinSelect = base->bclkPinIndex;
0207 timerConfig.pinConfig = kFLEXIO_PinConfigOutput;
0208 timerConfig.pinPolarity = config->bclkPinPolarity;
0209 timerConfig.timerMode = kFLEXIO_TimerModeDual8BitBaudBit;
0210 timerConfig.timerOutput = kFLEXIO_TimerOutputOneNotAffectedByReset;
0211 timerConfig.timerDecrement = kFLEXIO_TimerDecSrcOnFlexIOClockShiftTimerOutput;
0212 timerConfig.timerReset = kFLEXIO_TimerResetNever;
0213 timerConfig.timerDisable = kFLEXIO_TimerDisableNever;
0214 timerConfig.timerEnable = kFLEXIO_TimerEnableOnTriggerHigh;
0215 timerConfig.timerStart = kFLEXIO_TimerStartBitEnabled;
0216 timerConfig.timerStop = kFLEXIO_TimerStopBitDisabled;
0217 }
0218 else
0219 {
0220 timerConfig.triggerSelect = FLEXIO_TIMER_TRIGGER_SEL_TIMn(base->fsTimerIndex);
0221 timerConfig.triggerPolarity = kFLEXIO_TimerTriggerPolarityActiveHigh;
0222 timerConfig.triggerSource = kFLEXIO_TimerTriggerSourceInternal;
0223 timerConfig.pinSelect = base->bclkPinIndex;
0224 timerConfig.pinConfig = kFLEXIO_PinConfigOutputDisabled;
0225 timerConfig.pinPolarity = config->bclkPinPolarity;
0226 timerConfig.timerMode = kFLEXIO_TimerModeSingle16Bit;
0227 timerConfig.timerOutput = kFLEXIO_TimerOutputOneNotAffectedByReset;
0228 timerConfig.timerDecrement = kFLEXIO_TimerDecSrcOnPinInputShiftPinInput;
0229 timerConfig.timerReset = kFLEXIO_TimerResetNever;
0230 timerConfig.timerDisable = kFLEXIO_TimerDisableOnTimerCompareTriggerLow;
0231 timerConfig.timerEnable = kFLEXIO_TimerEnableOnPinRisingEdgeTriggerHigh;
0232 timerConfig.timerStart = kFLEXIO_TimerStartBitDisabled;
0233 timerConfig.timerStop = kFLEXIO_TimerStopBitDisabled;
0234 }
0235 FLEXIO_SetTimerConfig(base->flexioBase, base->bclkTimerIndex, &timerConfig);
0236
0237
0238 if (config->enableI2S)
0239 {
0240 base->flexioBase->CTRL |= FLEXIO_CTRL_FLEXEN_MASK;
0241 }
0242 else
0243 {
0244 base->flexioBase->CTRL &= ~FLEXIO_CTRL_FLEXEN_MASK;
0245 }
0246 }
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257 void FLEXIO_I2S_GetDefaultConfig(flexio_i2s_config_t *config)
0258 {
0259
0260 (void)memset(config, 0, sizeof(*config));
0261
0262 config->masterSlave = kFLEXIO_I2S_Master;
0263 config->enableI2S = true;
0264 config->txPinPolarity = kFLEXIO_PinActiveHigh;
0265 config->rxPinPolarity = kFLEXIO_PinActiveHigh;
0266 config->bclkPinPolarity = kFLEXIO_PinActiveHigh;
0267 config->fsPinPolarity = kFLEXIO_PinActiveLow;
0268 config->txTimerPolarity = kFLEXIO_ShifterTimerPolarityOnPositive;
0269 config->rxTimerPolarity = kFLEXIO_ShifterTimerPolarityOnNegitive;
0270 }
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280 void FLEXIO_I2S_Deinit(FLEXIO_I2S_Type *base)
0281 {
0282 base->flexioBase->SHIFTCFG[base->txShifterIndex] = 0;
0283 base->flexioBase->SHIFTCTL[base->txShifterIndex] = 0;
0284 base->flexioBase->SHIFTCFG[base->rxShifterIndex] = 0;
0285 base->flexioBase->SHIFTCTL[base->rxShifterIndex] = 0;
0286 base->flexioBase->TIMCFG[base->fsTimerIndex] = 0;
0287 base->flexioBase->TIMCMP[base->fsTimerIndex] = 0;
0288 base->flexioBase->TIMCTL[base->fsTimerIndex] = 0;
0289 base->flexioBase->TIMCFG[base->bclkTimerIndex] = 0;
0290 base->flexioBase->TIMCMP[base->bclkTimerIndex] = 0;
0291 base->flexioBase->TIMCTL[base->bclkTimerIndex] = 0;
0292 }
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302 void FLEXIO_I2S_EnableInterrupts(FLEXIO_I2S_Type *base, uint32_t mask)
0303 {
0304 if ((mask & (uint32_t)kFLEXIO_I2S_TxDataRegEmptyInterruptEnable) != 0UL)
0305 {
0306 FLEXIO_EnableShifterStatusInterrupts(base->flexioBase, 1UL << base->txShifterIndex);
0307 }
0308 if ((mask & (uint32_t)kFLEXIO_I2S_RxDataRegFullInterruptEnable) != 0UL)
0309 {
0310 FLEXIO_EnableShifterStatusInterrupts(base->flexioBase, 1UL << base->rxShifterIndex);
0311 }
0312 }
0313
0314
0315
0316
0317
0318
0319
0320 uint32_t FLEXIO_I2S_GetStatusFlags(FLEXIO_I2S_Type *base)
0321 {
0322 uint32_t status = 0;
0323 status = ((FLEXIO_GetShifterStatusFlags(base->flexioBase) & (1UL << base->txShifterIndex)) >> base->txShifterIndex);
0324 status |=
0325 (((FLEXIO_GetShifterStatusFlags(base->flexioBase) & (1UL << base->rxShifterIndex)) >> (base->rxShifterIndex))
0326 << 1U);
0327 return status;
0328 }
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338 void FLEXIO_I2S_DisableInterrupts(FLEXIO_I2S_Type *base, uint32_t mask)
0339 {
0340 if ((mask & (uint32_t)kFLEXIO_I2S_TxDataRegEmptyInterruptEnable) != 0UL)
0341 {
0342 FLEXIO_DisableShifterStatusInterrupts(base->flexioBase, 1UL << base->txShifterIndex);
0343 }
0344 if ((mask & (uint32_t)kFLEXIO_I2S_RxDataRegFullInterruptEnable) != 0UL)
0345 {
0346 FLEXIO_DisableShifterStatusInterrupts(base->flexioBase, 1UL << base->rxShifterIndex);
0347 }
0348 }
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360 void FLEXIO_I2S_MasterSetFormat(FLEXIO_I2S_Type *base, flexio_i2s_format_t *format, uint32_t srcClock_Hz)
0361 {
0362 uint32_t timDiv = srcClock_Hz / (format->sampleRate_Hz * format->bitWidth * 2U);
0363 uint32_t bclkDiv = 0;
0364
0365
0366 if ((timDiv % 2UL) != 0UL)
0367 {
0368 timDiv += 1U;
0369 }
0370
0371 base->flexioBase->TIMCMP[base->fsTimerIndex] = FLEXIO_TIMCMP_CMP(format->bitWidth * timDiv - 1U);
0372
0373
0374 bclkDiv = ((timDiv / 2U - 1U) | ((format->bitWidth * 2UL - 1UL) << 8U));
0375 base->flexioBase->TIMCMP[base->bclkTimerIndex] = FLEXIO_TIMCMP_CMP(bclkDiv);
0376 }
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387 void FLEXIO_I2S_SlaveSetFormat(FLEXIO_I2S_Type *base, flexio_i2s_format_t *format)
0388 {
0389
0390 base->flexioBase->TIMCMP[base->fsTimerIndex] = FLEXIO_TIMCMP_CMP(format->bitWidth * 4UL - 3UL);
0391
0392
0393 base->flexioBase->TIMCMP[base->bclkTimerIndex] = FLEXIO_TIMCMP_CMP(format->bitWidth * 2UL - 1UL);
0394 }
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408 status_t FLEXIO_I2S_WriteBlocking(FLEXIO_I2S_Type *base, uint8_t bitWidth, uint8_t *txData, size_t size)
0409 {
0410 uint32_t i = 0;
0411 uint8_t bytesPerWord = bitWidth / 8U;
0412 #if I2S_RETRY_TIMES
0413 uint32_t waitTimes = I2S_RETRY_TIMES;
0414 #endif
0415
0416 for (i = 0; i < size / bytesPerWord; i++)
0417 {
0418
0419 #if I2S_RETRY_TIMES
0420 waitTimes = I2S_RETRY_TIMES;
0421 while (((FLEXIO_I2S_GetStatusFlags(base) & (uint32_t)kFLEXIO_I2S_TxDataRegEmptyFlag) == 0UL) &&
0422 (--waitTimes != 0U))
0423 {
0424 }
0425 if (waitTimes == 0U)
0426 {
0427 return kStatus_FLEXIO_I2S_Timeout;
0428 }
0429 #else
0430 while ((FLEXIO_I2S_GetStatusFlags(base) & (uint32_t)kFLEXIO_I2S_TxDataRegEmptyFlag) == 0UL)
0431 {
0432 }
0433 #endif
0434
0435 FLEXIO_I2S_WriteNonBlocking(base, bitWidth, txData, bytesPerWord);
0436 txData = (uint8_t *)((uint32_t)txData + bytesPerWord);
0437 }
0438
0439
0440 #if I2S_RETRY_TIMES
0441 waitTimes = I2S_RETRY_TIMES;
0442 while (((FLEXIO_I2S_GetStatusFlags(base) & (uint32_t)kFLEXIO_I2S_TxDataRegEmptyFlag) == 0UL) && (--waitTimes != 0U))
0443 {
0444 }
0445 if (waitTimes == 0U)
0446 {
0447 return kStatus_FLEXIO_I2S_Timeout;
0448 }
0449 #else
0450 while ((FLEXIO_I2S_GetStatusFlags(base) & (uint32_t)kFLEXIO_I2S_TxDataRegEmptyFlag) == 0UL)
0451 {
0452 }
0453 #endif
0454
0455 return kStatus_Success;
0456 }
0457
0458
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469
0470 status_t FLEXIO_I2S_ReadBlocking(FLEXIO_I2S_Type *base, uint8_t bitWidth, uint8_t *rxData, size_t size)
0471 {
0472 uint32_t i = 0;
0473 uint8_t bytesPerWord = bitWidth / 8U;
0474 #if I2S_RETRY_TIMES
0475 uint32_t waitTimes = I2S_RETRY_TIMES;
0476 #endif
0477
0478 for (i = 0; i < size / bytesPerWord; i++)
0479 {
0480
0481 #if I2S_RETRY_TIMES
0482 waitTimes = I2S_RETRY_TIMES;
0483 while ((!((FLEXIO_GetShifterStatusFlags(base->flexioBase) & (1UL << base->rxShifterIndex)) != 0UL)) &&
0484 (--waitTimes != 0U))
0485 {
0486 }
0487 if (waitTimes == 0U)
0488 {
0489 return kStatus_FLEXIO_I2S_Timeout;
0490 }
0491 #else
0492 while (!((FLEXIO_GetShifterStatusFlags(base->flexioBase) & (1UL << base->rxShifterIndex)) != 0UL))
0493 {
0494 }
0495 #endif
0496
0497 FLEXIO_I2S_ReadNonBlocking(base, bitWidth, rxData, bytesPerWord);
0498 rxData = (uint8_t *)((uint32_t)rxData + bytesPerWord);
0499 }
0500 return kStatus_Success;
0501 }
0502
0503
0504
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514
0515 void FLEXIO_I2S_TransferTxCreateHandle(FLEXIO_I2S_Type *base,
0516 flexio_i2s_handle_t *handle,
0517 flexio_i2s_callback_t callback,
0518 void *userData)
0519 {
0520 assert(handle != NULL);
0521
0522 IRQn_Type flexio_irqs[] = FLEXIO_IRQS;
0523
0524
0525 (void)memset(handle, 0, sizeof(*handle));
0526
0527
0528 handle->callback = callback;
0529 handle->userData = userData;
0530
0531
0532 (void)FLEXIO_RegisterHandleIRQ(base, handle, FLEXIO_I2S_TransferTxHandleIRQ);
0533
0534
0535 handle->state = (uint32_t)kFLEXIO_I2S_Idle;
0536
0537
0538 (void)EnableIRQ(flexio_irqs[FLEXIO_I2S_GetInstance(base)]);
0539 }
0540
0541
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553 void FLEXIO_I2S_TransferRxCreateHandle(FLEXIO_I2S_Type *base,
0554 flexio_i2s_handle_t *handle,
0555 flexio_i2s_callback_t callback,
0556 void *userData)
0557 {
0558 assert(handle != NULL);
0559
0560 IRQn_Type flexio_irqs[] = FLEXIO_IRQS;
0561
0562
0563 (void)memset(handle, 0, sizeof(*handle));
0564
0565
0566 handle->callback = callback;
0567 handle->userData = userData;
0568
0569
0570 (void)FLEXIO_RegisterHandleIRQ(base, handle, FLEXIO_I2S_TransferRxHandleIRQ);
0571
0572
0573 handle->state = (uint32_t)kFLEXIO_I2S_Idle;
0574
0575
0576 (void)EnableIRQ(flexio_irqs[FLEXIO_I2S_GetInstance(base)]);
0577 }
0578
0579
0580
0581
0582
0583
0584
0585
0586
0587
0588
0589
0590 void FLEXIO_I2S_TransferSetFormat(FLEXIO_I2S_Type *base,
0591 flexio_i2s_handle_t *handle,
0592 flexio_i2s_format_t *format,
0593 uint32_t srcClock_Hz)
0594 {
0595 assert((handle != NULL) && (format != NULL));
0596
0597
0598 handle->bitWidth = format->bitWidth;
0599
0600
0601 if (srcClock_Hz != 0UL)
0602 {
0603
0604 FLEXIO_I2S_MasterSetFormat(base, format, srcClock_Hz);
0605 }
0606 else
0607 {
0608 FLEXIO_I2S_SlaveSetFormat(base, format);
0609 }
0610 }
0611
0612
0613
0614
0615
0616
0617
0618
0619
0620
0621
0622
0623
0624
0625
0626 status_t FLEXIO_I2S_TransferSendNonBlocking(FLEXIO_I2S_Type *base,
0627 flexio_i2s_handle_t *handle,
0628 flexio_i2s_transfer_t *xfer)
0629 {
0630 assert(handle != NULL);
0631
0632
0633 if (handle->queue[handle->queueUser].data != NULL)
0634 {
0635 return kStatus_FLEXIO_I2S_QueueFull;
0636 }
0637 if ((xfer->dataSize == 0U) || (xfer->data == NULL))
0638 {
0639 return kStatus_InvalidArgument;
0640 }
0641
0642
0643 handle->queue[handle->queueUser].data = xfer->data;
0644 handle->queue[handle->queueUser].dataSize = xfer->dataSize;
0645 handle->transferSize[handle->queueUser] = xfer->dataSize;
0646 handle->queueUser = (handle->queueUser + 1U) % FLEXIO_I2S_XFER_QUEUE_SIZE;
0647
0648
0649 handle->state = (uint32_t)kFLEXIO_I2S_Busy;
0650
0651 FLEXIO_I2S_EnableInterrupts(base, kFLEXIO_I2S_TxDataRegEmptyInterruptEnable);
0652
0653
0654 FLEXIO_I2S_Enable(base, true);
0655
0656 return kStatus_Success;
0657 }
0658
0659
0660
0661
0662
0663
0664
0665
0666
0667
0668
0669
0670
0671
0672
0673 status_t FLEXIO_I2S_TransferReceiveNonBlocking(FLEXIO_I2S_Type *base,
0674 flexio_i2s_handle_t *handle,
0675 flexio_i2s_transfer_t *xfer)
0676 {
0677 assert(handle != NULL);
0678
0679
0680 if (handle->queue[handle->queueUser].data != NULL)
0681 {
0682 return kStatus_FLEXIO_I2S_QueueFull;
0683 }
0684
0685 if ((xfer->dataSize == 0U) || (xfer->data == NULL))
0686 {
0687 return kStatus_InvalidArgument;
0688 }
0689
0690
0691 handle->queue[handle->queueUser].data = xfer->data;
0692 handle->queue[handle->queueUser].dataSize = xfer->dataSize;
0693 handle->transferSize[handle->queueUser] = xfer->dataSize;
0694 handle->queueUser = (handle->queueUser + 1U) % FLEXIO_I2S_XFER_QUEUE_SIZE;
0695
0696
0697 handle->state = (uint32_t)kFLEXIO_I2S_Busy;
0698
0699
0700 FLEXIO_I2S_EnableInterrupts(base, kFLEXIO_I2S_RxDataRegFullInterruptEnable);
0701
0702
0703 FLEXIO_I2S_Enable(base, true);
0704
0705 return kStatus_Success;
0706 }
0707
0708
0709
0710
0711
0712
0713
0714
0715
0716
0717 void FLEXIO_I2S_TransferAbortSend(FLEXIO_I2S_Type *base, flexio_i2s_handle_t *handle)
0718 {
0719 assert(handle != NULL);
0720
0721
0722 FLEXIO_I2S_DisableInterrupts(base, kFLEXIO_I2S_TxDataRegEmptyInterruptEnable);
0723 handle->state = (uint32_t)kFLEXIO_I2S_Idle;
0724
0725
0726 (void)memset(handle->queue, 0, sizeof(flexio_i2s_transfer_t) * FLEXIO_I2S_XFER_QUEUE_SIZE);
0727 handle->queueDriver = 0;
0728 handle->queueUser = 0;
0729 }
0730
0731
0732
0733
0734
0735
0736
0737
0738
0739
0740 void FLEXIO_I2S_TransferAbortReceive(FLEXIO_I2S_Type *base, flexio_i2s_handle_t *handle)
0741 {
0742 assert(handle != NULL);
0743
0744
0745 FLEXIO_I2S_DisableInterrupts(base, kFLEXIO_I2S_RxDataRegFullInterruptEnable);
0746 handle->state = (uint32_t)kFLEXIO_I2S_Idle;
0747
0748
0749 (void)memset(handle->queue, 0, sizeof(flexio_i2s_transfer_t) * FLEXIO_I2S_XFER_QUEUE_SIZE);
0750 handle->queueDriver = 0;
0751 handle->queueUser = 0;
0752 }
0753
0754
0755
0756
0757
0758
0759
0760
0761
0762
0763 status_t FLEXIO_I2S_TransferGetSendCount(FLEXIO_I2S_Type *base, flexio_i2s_handle_t *handle, size_t *count)
0764 {
0765 assert(handle != NULL);
0766
0767 status_t status = kStatus_Success;
0768 uint8_t queueDriver = handle->queueDriver;
0769
0770 if (handle->state != (uint32_t)kFLEXIO_I2S_Busy)
0771 {
0772 status = kStatus_NoTransferInProgress;
0773 }
0774 else
0775 {
0776 *count = (handle->transferSize[queueDriver] - handle->queue[queueDriver].dataSize);
0777 }
0778
0779 return status;
0780 }
0781
0782
0783
0784
0785
0786
0787
0788
0789
0790
0791 status_t FLEXIO_I2S_TransferGetReceiveCount(FLEXIO_I2S_Type *base, flexio_i2s_handle_t *handle, size_t *count)
0792 {
0793 assert(handle != NULL);
0794
0795 status_t status = kStatus_Success;
0796 uint8_t queueDriver = handle->queueDriver;
0797
0798 if (handle->state != (uint32_t)kFLEXIO_I2S_Busy)
0799 {
0800 status = kStatus_NoTransferInProgress;
0801 }
0802 else
0803 {
0804 *count = (handle->transferSize[queueDriver] - handle->queue[queueDriver].dataSize);
0805 }
0806
0807 return status;
0808 }
0809
0810
0811
0812
0813
0814
0815
0816 void FLEXIO_I2S_TransferTxHandleIRQ(void *i2sBase, void *i2sHandle)
0817 {
0818 assert(i2sHandle != NULL);
0819
0820 flexio_i2s_handle_t *handle = (flexio_i2s_handle_t *)i2sHandle;
0821 FLEXIO_I2S_Type *base = (FLEXIO_I2S_Type *)i2sBase;
0822 uint8_t *buffer = handle->queue[handle->queueDriver].data;
0823 uint8_t dataSize = handle->bitWidth / 8U;
0824
0825
0826 if ((FLEXIO_GetShifterErrorFlags(base->flexioBase) & (1UL << base->txShifterIndex)) != 0UL)
0827 {
0828 FLEXIO_ClearShifterErrorFlags(base->flexioBase, (1UL << base->txShifterIndex));
0829 }
0830
0831 if (((FLEXIO_I2S_GetStatusFlags(base) & (uint32_t)kFLEXIO_I2S_TxDataRegEmptyFlag) != 0UL) &&
0832 (handle->queue[handle->queueDriver].data != NULL))
0833 {
0834 FLEXIO_I2S_WriteNonBlocking(base, handle->bitWidth, buffer, dataSize);
0835
0836
0837 handle->queue[handle->queueDriver].dataSize -= dataSize;
0838 handle->queue[handle->queueDriver].data =
0839 (uint8_t *)((uint32_t)handle->queue[handle->queueDriver].data + dataSize);
0840 }
0841
0842
0843 if ((handle->queue[handle->queueDriver].dataSize == 0U) && (handle->queue[handle->queueDriver].data != NULL))
0844 {
0845 (void)memset(&handle->queue[handle->queueDriver], 0, sizeof(flexio_i2s_transfer_t));
0846 handle->queueDriver = (handle->queueDriver + 1U) % FLEXIO_I2S_XFER_QUEUE_SIZE;
0847 if (handle->callback != NULL)
0848 {
0849 (handle->callback)(base, handle, kStatus_Success, handle->userData);
0850 }
0851 }
0852
0853
0854 if (handle->queue[handle->queueDriver].data == NULL)
0855 {
0856 FLEXIO_I2S_TransferAbortSend(base, handle);
0857 }
0858 }
0859
0860
0861
0862
0863
0864
0865
0866 void FLEXIO_I2S_TransferRxHandleIRQ(void *i2sBase, void *i2sHandle)
0867 {
0868 assert(i2sHandle != NULL);
0869
0870 flexio_i2s_handle_t *handle = (flexio_i2s_handle_t *)i2sHandle;
0871 FLEXIO_I2S_Type *base = (FLEXIO_I2S_Type *)i2sBase;
0872 uint8_t *buffer = handle->queue[handle->queueDriver].data;
0873 uint8_t dataSize = handle->bitWidth / 8U;
0874
0875
0876 if (((FLEXIO_I2S_GetStatusFlags(base) & (uint32_t)kFLEXIO_I2S_RxDataRegFullFlag) != 0UL) &&
0877 (handle->queue[handle->queueDriver].data != NULL))
0878 {
0879 FLEXIO_I2S_ReadNonBlocking(base, handle->bitWidth, buffer, dataSize);
0880
0881
0882 handle->queue[handle->queueDriver].dataSize -= dataSize;
0883 handle->queue[handle->queueDriver].data =
0884 (uint8_t *)((uint32_t)handle->queue[handle->queueDriver].data + dataSize);
0885 }
0886
0887
0888 if ((handle->queue[handle->queueDriver].dataSize == 0U) && (handle->queue[handle->queueDriver].data != NULL))
0889 {
0890 (void)memset(&handle->queue[handle->queueDriver], 0, sizeof(flexio_i2s_transfer_t));
0891 handle->queueDriver = (handle->queueDriver + 1U) % FLEXIO_I2S_XFER_QUEUE_SIZE;
0892 if (handle->callback != NULL)
0893 {
0894 (handle->callback)(base, handle, kStatus_Success, handle->userData);
0895 }
0896 }
0897
0898
0899 if (handle->queue[handle->queueDriver].data == NULL)
0900 {
0901 FLEXIO_I2S_TransferAbortReceive(base, handle);
0902 }
0903 }