File indexing completed on 2025-05-11 08:23:00
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "fsl_lpi2c.h"
0010 #include <stdlib.h>
0011 #include <string.h>
0012
0013
0014
0015
0016
0017
0018 #ifndef FSL_COMPONENT_ID
0019 #define FSL_COMPONENT_ID "platform.drivers.lpi2c"
0020 #endif
0021
0022
0023 enum
0024 {
0025 kTxDataCmd = LPI2C_MTDR_CMD(0x0U),
0026 kRxDataCmd = LPI2C_MTDR_CMD(0X1U),
0027 kStopCmd = LPI2C_MTDR_CMD(0x2U),
0028 kStartCmd = LPI2C_MTDR_CMD(0x4U),
0029 };
0030
0031
0032
0033
0034
0035
0036 enum
0037 {
0038 kDefaultTxWatermark = 0,
0039 kDefaultRxWatermark = 0,
0040 };
0041
0042
0043 enum
0044 {
0045 kIdleState = 0,
0046 kSendCommandState,
0047 kIssueReadCommandState,
0048 kTransferDataState,
0049 kStopState,
0050 kWaitForCompletionState,
0051 };
0052
0053
0054
0055
0056
0057 typedef struct _lpi2c_state_machine_param
0058 {
0059 bool state_complete;
0060 size_t rxCount;
0061 size_t txCount;
0062 uint32_t status;
0063 } lpi2c_state_machine_param_t;
0064
0065
0066 typedef void (*lpi2c_slave_isr_t)(LPI2C_Type *base, lpi2c_slave_handle_t *handle);
0067
0068
0069
0070
0071 static uint32_t LPI2C_GetCyclesForWidth(
0072 uint32_t sourceClock_Hz, uint32_t width_ns, uint32_t minCycles, uint32_t maxCycles, uint32_t prescaler);
0073
0074 static status_t LPI2C_MasterWaitForTxReady(LPI2C_Type *base);
0075
0076 static status_t LPI2C_RunTransferStateMachine(LPI2C_Type *base, lpi2c_master_handle_t *handle, bool *isDone);
0077
0078 static void LPI2C_InitTransferStateMachine(lpi2c_master_handle_t *handle);
0079
0080 static status_t LPI2C_SlaveCheckAndClearError(LPI2C_Type *base, uint32_t flags);
0081
0082 static void LPI2C_CommonIRQHandler(LPI2C_Type *base, uint32_t instance);
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092 static void LPI2C_TransferStateMachineSendCommand(LPI2C_Type *base,
0093 lpi2c_master_handle_t *handle,
0094 lpi2c_state_machine_param_t *stateParams);
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104 static void LPI2C_TransferStateMachineReadCommand(LPI2C_Type *base,
0105 lpi2c_master_handle_t *handle,
0106 lpi2c_state_machine_param_t *stateParams);
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116 static void LPI2C_TransferStateMachineTransferData(LPI2C_Type *base,
0117 lpi2c_master_handle_t *handle,
0118 lpi2c_state_machine_param_t *stateParams);
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129 static void LPI2C_TransferStateMachineStopState(LPI2C_Type *base,
0130 lpi2c_master_handle_t *handle,
0131 lpi2c_state_machine_param_t *stateParams,
0132 bool *isDone);
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143 static void LPI2C_TransferStateMachineWaitState(LPI2C_Type *base,
0144 lpi2c_master_handle_t *handle,
0145 lpi2c_state_machine_param_t *stateParams,
0146 bool *isDone);
0147
0148
0149
0150
0151
0152
0153 static LPI2C_Type *const kLpi2cBases[] = LPI2C_BASE_PTRS;
0154
0155
0156
0157 IRQn_Type const kLpi2cIrqs[] = LPI2C_IRQS;
0158
0159 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
0160
0161 static clock_ip_name_t const kLpi2cClocks[] = LPI2C_CLOCKS;
0162
0163 #if defined(LPI2C_PERIPH_CLOCKS)
0164
0165 static const clock_ip_name_t kLpi2cPeriphClocks[] = LPI2C_PERIPH_CLOCKS;
0166 #endif
0167
0168 #endif
0169
0170
0171
0172 lpi2c_master_isr_t s_lpi2cMasterIsr;
0173
0174
0175
0176 void *s_lpi2cMasterHandle[ARRAY_SIZE(kLpi2cBases)];
0177
0178
0179 static lpi2c_slave_isr_t s_lpi2cSlaveIsr;
0180
0181
0182 static lpi2c_slave_handle_t *s_lpi2cSlaveHandle[ARRAY_SIZE(kLpi2cBases)];
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197 uint32_t LPI2C_GetInstance(LPI2C_Type *base)
0198 {
0199 uint32_t instance;
0200 for (instance = 0U; instance < ARRAY_SIZE(kLpi2cBases); ++instance)
0201 {
0202 if (kLpi2cBases[instance] == base)
0203 {
0204 break;
0205 }
0206 }
0207
0208 assert(instance < ARRAY_SIZE(kLpi2cBases));
0209 return instance;
0210 }
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220 static uint32_t LPI2C_GetCyclesForWidth(
0221 uint32_t sourceClock_Hz, uint32_t width_ns, uint32_t minCycles, uint32_t maxCycles, uint32_t prescaler)
0222 {
0223 assert(sourceClock_Hz > 0U);
0224
0225 uint32_t divider = 1U;
0226
0227 while (prescaler != 0U)
0228 {
0229 divider *= 2U;
0230 prescaler--;
0231 }
0232
0233 uint32_t busCycle_ns = 1000000U / (sourceClock_Hz / divider / 1000U);
0234
0235 uint32_t cycles = (width_ns * 10U / busCycle_ns + 5U) / 10U;
0236
0237
0238 if (cycles < minCycles)
0239 {
0240 cycles = minCycles;
0241 }
0242
0243 if (cycles > maxCycles)
0244 {
0245 cycles = maxCycles;
0246 }
0247
0248 return cycles;
0249 }
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262 status_t LPI2C_MasterCheckAndClearError(LPI2C_Type *base, uint32_t status)
0263 {
0264 status_t result = kStatus_Success;
0265
0266
0267
0268 status &= (uint32_t)kLPI2C_MasterErrorFlags;
0269 if (0U != status)
0270 {
0271
0272 if (0U != (status & (uint32_t)kLPI2C_MasterPinLowTimeoutFlag))
0273 {
0274 result = kStatus_LPI2C_PinLowTimeout;
0275 }
0276 else if (0U != (status & (uint32_t)kLPI2C_MasterArbitrationLostFlag))
0277 {
0278 result = kStatus_LPI2C_ArbitrationLost;
0279 }
0280 else if (0U != (status & (uint32_t)kLPI2C_MasterNackDetectFlag))
0281 {
0282 result = kStatus_LPI2C_Nak;
0283 }
0284 else if (0U != (status & (uint32_t)kLPI2C_MasterFifoErrFlag))
0285 {
0286 result = kStatus_LPI2C_FifoError;
0287 }
0288 else
0289 {
0290 ;
0291 }
0292
0293
0294 LPI2C_MasterClearStatusFlags(base, status);
0295
0296
0297 base->MCR |= LPI2C_MCR_RRF_MASK | LPI2C_MCR_RTF_MASK;
0298 }
0299 else
0300 {
0301 ;
0302 }
0303
0304 return result;
0305 }
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316 static status_t LPI2C_MasterWaitForTxReady(LPI2C_Type *base)
0317 {
0318 status_t result = kStatus_Success;
0319 uint32_t status;
0320 size_t txCount;
0321 size_t txFifoSize = (size_t)FSL_FEATURE_LPI2C_FIFO_SIZEn(base);
0322
0323 #if I2C_RETRY_TIMES != 0U
0324 uint32_t waitTimes = I2C_RETRY_TIMES;
0325 #endif
0326 do
0327 {
0328
0329 LPI2C_MasterGetFifoCounts(base, NULL, &txCount);
0330 txCount = txFifoSize - txCount;
0331
0332
0333 status = LPI2C_MasterGetStatusFlags(base);
0334 result = LPI2C_MasterCheckAndClearError(base, status);
0335 if (kStatus_Success != result)
0336 {
0337 break;
0338 }
0339 #if I2C_RETRY_TIMES != 0U
0340 waitTimes--;
0341 } while ((0U == txCount) && (0U != waitTimes));
0342
0343 if (0U == waitTimes)
0344 {
0345 result = kStatus_LPI2C_Timeout;
0346 }
0347 #else
0348 } while (0U == txCount);
0349 #endif
0350
0351 return result;
0352 }
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364 status_t LPI2C_CheckForBusyBus(LPI2C_Type *base)
0365 {
0366 status_t ret = kStatus_Success;
0367
0368 uint32_t status = LPI2C_MasterGetStatusFlags(base);
0369 if ((0U != (status & (uint32_t)kLPI2C_MasterBusBusyFlag)) && (0U == (status & (uint32_t)kLPI2C_MasterBusyFlag)))
0370 {
0371 ret = kStatus_LPI2C_Busy;
0372 }
0373
0374 return ret;
0375 }
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401 void LPI2C_MasterGetDefaultConfig(lpi2c_master_config_t *masterConfig)
0402 {
0403
0404 (void)memset(masterConfig, 0, sizeof(*masterConfig));
0405
0406 masterConfig->enableMaster = true;
0407 masterConfig->debugEnable = false;
0408 masterConfig->enableDoze = true;
0409 masterConfig->ignoreAck = false;
0410 masterConfig->pinConfig = kLPI2C_2PinOpenDrain;
0411 masterConfig->baudRate_Hz = 100000U;
0412 masterConfig->busIdleTimeout_ns = 0U;
0413 masterConfig->pinLowTimeout_ns = 0U;
0414 masterConfig->sdaGlitchFilterWidth_ns = 0U;
0415 masterConfig->sclGlitchFilterWidth_ns = 0U;
0416 masterConfig->hostRequest.enable = false;
0417 masterConfig->hostRequest.source = kLPI2C_HostRequestExternalPin;
0418 masterConfig->hostRequest.polarity = kLPI2C_HostRequestPinActiveHigh;
0419 }
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434 void LPI2C_MasterInit(LPI2C_Type *base, const lpi2c_master_config_t *masterConfig, uint32_t sourceClock_Hz)
0435 {
0436 uint32_t prescaler;
0437 uint32_t cycles;
0438 uint32_t cfgr2;
0439 uint32_t value;
0440
0441 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
0442
0443 uint32_t instance = LPI2C_GetInstance(base);
0444
0445
0446 (void)CLOCK_EnableClock(kLpi2cClocks[instance]);
0447 #if defined(LPI2C_PERIPH_CLOCKS)
0448
0449 CLOCK_EnableClock(kLpi2cPeriphClocks[instance]);
0450 #endif
0451
0452 #endif
0453
0454
0455 LPI2C_MasterReset(base);
0456
0457
0458 base->MCR = LPI2C_MCR_DBGEN(masterConfig->debugEnable) | LPI2C_MCR_DOZEN(!(masterConfig->enableDoze));
0459
0460
0461 value = base->MCFGR0;
0462 value &= (~(LPI2C_MCFGR0_HREN_MASK | LPI2C_MCFGR0_HRPOL_MASK | LPI2C_MCFGR0_HRSEL_MASK));
0463 value |= LPI2C_MCFGR0_HREN(masterConfig->hostRequest.enable) |
0464 LPI2C_MCFGR0_HRPOL(masterConfig->hostRequest.polarity) |
0465 LPI2C_MCFGR0_HRSEL(masterConfig->hostRequest.source);
0466 base->MCFGR0 = value;
0467
0468
0469 value = base->MCFGR1;
0470 value &= ~(LPI2C_MCFGR1_PINCFG_MASK | LPI2C_MCFGR1_IGNACK_MASK);
0471 value |= LPI2C_MCFGR1_PINCFG(masterConfig->pinConfig);
0472 value |= LPI2C_MCFGR1_IGNACK(masterConfig->ignoreAck);
0473 base->MCFGR1 = value;
0474
0475 LPI2C_MasterSetWatermarks(base, (size_t)kDefaultTxWatermark, (size_t)kDefaultRxWatermark);
0476
0477
0478 cfgr2 = base->MCFGR2;
0479 if (0U != (masterConfig->sdaGlitchFilterWidth_ns))
0480 {
0481
0482
0483 cycles = LPI2C_GetCyclesForWidth(sourceClock_Hz, masterConfig->sdaGlitchFilterWidth_ns, 1U,
0484 (LPI2C_MCFGR2_FILTSDA_MASK >> LPI2C_MCFGR2_FILTSDA_SHIFT), 0U);
0485 cfgr2 &= ~LPI2C_MCFGR2_FILTSDA_MASK;
0486 cfgr2 |= LPI2C_MCFGR2_FILTSDA(cycles);
0487 }
0488 if (0U != masterConfig->sclGlitchFilterWidth_ns)
0489 {
0490
0491
0492 cycles = LPI2C_GetCyclesForWidth(sourceClock_Hz, masterConfig->sclGlitchFilterWidth_ns, 1U,
0493 (LPI2C_MCFGR2_FILTSCL_MASK >> LPI2C_MCFGR2_FILTSCL_SHIFT), 0U);
0494 cfgr2 &= ~LPI2C_MCFGR2_FILTSCL_MASK;
0495 cfgr2 |= LPI2C_MCFGR2_FILTSCL(cycles);
0496 }
0497 base->MCFGR2 = cfgr2;
0498
0499
0500
0501 LPI2C_MasterSetBaudRate(base, sourceClock_Hz, masterConfig->baudRate_Hz);
0502
0503
0504
0505 prescaler = (base->MCFGR1 & LPI2C_MCFGR1_PRESCALE_MASK) >> LPI2C_MCFGR1_PRESCALE_SHIFT;
0506
0507 if (0U != (masterConfig->busIdleTimeout_ns))
0508 {
0509
0510
0511 cycles = LPI2C_GetCyclesForWidth(sourceClock_Hz, masterConfig->busIdleTimeout_ns, 1U,
0512 (LPI2C_MCFGR2_BUSIDLE_MASK >> LPI2C_MCFGR2_BUSIDLE_SHIFT), prescaler);
0513 base->MCFGR2 = (base->MCFGR2 & (~LPI2C_MCFGR2_BUSIDLE_MASK)) | LPI2C_MCFGR2_BUSIDLE(cycles);
0514 }
0515 if (0U != masterConfig->pinLowTimeout_ns)
0516 {
0517
0518
0519 cycles = LPI2C_GetCyclesForWidth(sourceClock_Hz, masterConfig->pinLowTimeout_ns / 256U, 1U,
0520 (LPI2C_MCFGR2_BUSIDLE_MASK >> LPI2C_MCFGR2_BUSIDLE_SHIFT), prescaler);
0521 base->MCFGR3 = (base->MCFGR3 & ~LPI2C_MCFGR3_PINLOW_MASK) | LPI2C_MCFGR3_PINLOW(cycles);
0522 }
0523
0524 LPI2C_MasterEnable(base, masterConfig->enableMaster);
0525 }
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535 void LPI2C_MasterDeinit(LPI2C_Type *base)
0536 {
0537
0538 LPI2C_MasterReset(base);
0539
0540 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
0541
0542 uint32_t instance = LPI2C_GetInstance(base);
0543
0544
0545 (void)CLOCK_DisableClock(kLpi2cClocks[instance]);
0546 #if defined(LPI2C_PERIPH_CLOCKS)
0547
0548 CLOCK_DisableClock(kLpi2cPeriphClocks[instance]);
0549 #endif
0550
0551 #endif
0552 }
0553
0554
0555
0556
0557
0558
0559
0560 void LPI2C_MasterConfigureDataMatch(LPI2C_Type *base, const lpi2c_data_match_config_t *matchConfig)
0561 {
0562
0563 bool wasEnabled = (0U != ((base->MCR & LPI2C_MCR_MEN_MASK) >> LPI2C_MCR_MEN_SHIFT));
0564 LPI2C_MasterEnable(base, false);
0565
0566 base->MCFGR1 = (base->MCFGR1 & ~LPI2C_MCFGR1_MATCFG_MASK) | LPI2C_MCFGR1_MATCFG(matchConfig->matchMode);
0567 base->MCFGR0 = (base->MCFGR0 & ~LPI2C_MCFGR0_RDMO_MASK) | LPI2C_MCFGR0_RDMO(matchConfig->rxDataMatchOnly);
0568 base->MDMR = LPI2C_MDMR_MATCH0(matchConfig->match0) | LPI2C_MDMR_MATCH1(matchConfig->match1);
0569
0570
0571 if (wasEnabled)
0572 {
0573 LPI2C_MasterEnable(base, true);
0574 }
0575 }
0576
0577
0578
0579
0580
0581
0582
0583
0584
0585
0586
0587
0588
0589
0590
0591 void LPI2C_MasterSetBaudRate(LPI2C_Type *base, uint32_t sourceClock_Hz, uint32_t baudRate_Hz)
0592 {
0593 bool wasEnabled;
0594 uint8_t filtScl = (uint8_t)((base->MCFGR2 & LPI2C_MCFGR2_FILTSCL_MASK) >> LPI2C_MCFGR2_FILTSCL_SHIFT);
0595
0596 uint8_t divider = 1U;
0597 uint8_t bestDivider = 1U;
0598 uint8_t prescale = 0U;
0599 uint8_t bestPre = 0U;
0600
0601 uint8_t clkCycle;
0602 uint8_t bestclkCycle = 0U;
0603
0604 uint32_t absError = 0U;
0605 uint32_t bestError = 0xffffffffu;
0606 uint32_t computedRate;
0607
0608 uint32_t tmpReg = 0U;
0609
0610
0611 wasEnabled = (0U != ((base->MCR & LPI2C_MCR_MEN_MASK) >> LPI2C_MCR_MEN_SHIFT));
0612 LPI2C_MasterEnable(base, false);
0613
0614
0615
0616
0617 for (prescale = 0U; prescale <= 7U; prescale++)
0618 {
0619
0620 clkCycle = (uint8_t)((10U * sourceClock_Hz / divider / baudRate_Hz + 5U) / 10U - (2U + filtScl) / divider - 2U);
0621
0622
0623
0624
0625
0626
0627
0628
0629
0630
0631 if (clkCycle > (120U - (2U + filtScl) / divider))
0632 {
0633 divider *= 2U;
0634 continue;
0635 }
0636
0637 computedRate = (sourceClock_Hz / (uint32_t)divider) /
0638 ((uint32_t)clkCycle + 2U + (2U + (uint32_t)filtScl) / (uint32_t)divider);
0639 absError = baudRate_Hz > computedRate ? baudRate_Hz - computedRate : computedRate - baudRate_Hz;
0640 if (absError < bestError)
0641 {
0642 bestPre = prescale;
0643 bestDivider = divider;
0644 bestclkCycle = clkCycle;
0645 bestError = absError;
0646
0647
0648 if (absError == 0U)
0649 {
0650 break;
0651 }
0652 }
0653 divider *= 2U;
0654 }
0655
0656
0657
0658
0659
0660
0661
0662
0663
0664
0665
0666
0667
0668 uint8_t tmpHigh = (bestclkCycle - (2U + filtScl) / bestDivider) / 2U;
0669 while (tmpHigh > (bestclkCycle - 52U * sourceClock_Hz / baudRate_Hz / bestDivider / 100U + 1U))
0670 {
0671 tmpHigh = tmpHigh - 1U;
0672 }
0673
0674
0675
0676
0677
0678 uint8_t tmpHold = (uint8_t)(sourceClock_Hz / baudRate_Hz / bestDivider / 2U) - 1U;
0679
0680
0681
0682 uint8_t tmpDataVd = (uint8_t)(sourceClock_Hz / baudRate_Hz / bestDivider / 4U) - 1U;
0683
0684
0685
0686
0687
0688 if ((sourceClock_Hz / baudRate_Hz / 20U) > (bestDivider + 2U))
0689 {
0690
0691 uint8_t filtSda = (uint8_t)((base->MCFGR2 & LPI2C_MCFGR2_FILTSDA_MASK) >> LPI2C_MCFGR2_FILTSDA_SHIFT);
0692 if (filtSda < (sourceClock_Hz / baudRate_Hz / 20U - bestDivider - 2U))
0693 {
0694 filtSda = (uint8_t)(sourceClock_Hz / baudRate_Hz / 20U) - bestDivider - 2U;
0695 }
0696 base->MCFGR2 = (base->MCFGR2 & ~LPI2C_MCFGR2_FILTSDA_MASK) | LPI2C_MCFGR2_FILTSDA(filtSda);
0697 }
0698
0699
0700 tmpReg = LPI2C_MCCR0_CLKHI((uint32_t)tmpHigh) |
0701 LPI2C_MCCR0_CLKLO((uint32_t)((uint32_t)bestclkCycle - (uint32_t)tmpHigh)) |
0702 LPI2C_MCCR0_SETHOLD((uint32_t)tmpHold) | LPI2C_MCCR0_DATAVD((uint32_t)tmpDataVd);
0703 base->MCCR0 = tmpReg;
0704
0705
0706 base->MCFGR1 = (base->MCFGR1 & ~LPI2C_MCFGR1_PRESCALE_MASK) | LPI2C_MCFGR1_PRESCALE(bestPre);
0707
0708
0709 if (wasEnabled)
0710 {
0711 LPI2C_MasterEnable(base, true);
0712 }
0713 }
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723
0724
0725
0726
0727
0728
0729
0730 status_t LPI2C_MasterStart(LPI2C_Type *base, uint8_t address, lpi2c_direction_t dir)
0731 {
0732
0733 status_t result = LPI2C_CheckForBusyBus(base);
0734 if (kStatus_Success == result)
0735 {
0736
0737 LPI2C_MasterClearStatusFlags(base, (uint32_t)kLPI2C_MasterClearFlags);
0738
0739
0740 base->MCFGR1 &= ~LPI2C_MCFGR1_AUTOSTOP_MASK;
0741
0742
0743 result = LPI2C_MasterWaitForTxReady(base);
0744 if (kStatus_Success == result)
0745 {
0746
0747 base->MTDR = (uint32_t)kStartCmd | (((uint32_t)address << 1U) | (uint32_t)dir);
0748 }
0749 }
0750
0751 return result;
0752 }
0753
0754
0755
0756
0757
0758
0759
0760
0761
0762
0763
0764
0765
0766
0767 status_t LPI2C_MasterStop(LPI2C_Type *base)
0768 {
0769
0770 status_t result = LPI2C_MasterWaitForTxReady(base);
0771 if (kStatus_Success == result)
0772 {
0773
0774 base->MTDR = (uint32_t)kStopCmd;
0775
0776
0777
0778 #if I2C_RETRY_TIMES != 0U
0779 uint32_t waitTimes = I2C_RETRY_TIMES;
0780 #endif
0781
0782 #if I2C_RETRY_TIMES != 0U
0783 while ((result == kStatus_Success) && (0U != waitTimes))
0784 {
0785 waitTimes--;
0786 #else
0787 while (result == kStatus_Success)
0788 {
0789 #endif
0790 uint32_t status = LPI2C_MasterGetStatusFlags(base);
0791
0792
0793 result = LPI2C_MasterCheckAndClearError(base, status);
0794
0795
0796 if ((0U != (status & (uint32_t)kLPI2C_MasterStopDetectFlag)) &&
0797 (0U != (status & (uint32_t)kLPI2C_MasterTxReadyFlag)))
0798 {
0799 LPI2C_MasterClearStatusFlags(base, (uint32_t)kLPI2C_MasterStopDetectFlag);
0800 break;
0801 }
0802 }
0803
0804 #if I2C_RETRY_TIMES != 0U
0805 if (0U == waitTimes)
0806 {
0807 result = kStatus_LPI2C_Timeout;
0808 }
0809 #endif
0810 }
0811
0812 return result;
0813 }
0814
0815
0816
0817
0818
0819
0820
0821
0822
0823
0824
0825
0826
0827
0828 status_t LPI2C_MasterReceive(LPI2C_Type *base, void *rxBuff, size_t rxSize)
0829 {
0830 assert(NULL != rxBuff);
0831
0832 status_t result = kStatus_Success;
0833 uint8_t *buf;
0834 size_t tmpRxSize = rxSize;
0835 #if I2C_RETRY_TIMES != 0U
0836 uint32_t waitTimes;
0837 #endif
0838
0839
0840 if (rxSize > ((size_t)256 * (size_t)FSL_FEATURE_LPI2C_FIFO_SIZEn(base)))
0841 {
0842 return kStatus_InvalidArgument;
0843 }
0844
0845
0846 if (rxSize != 0U)
0847 {
0848
0849 result = LPI2C_MasterWaitForTxReady(base);
0850 if (kStatus_Success == result)
0851 {
0852
0853
0854
0855 while (tmpRxSize != 0U)
0856 {
0857 if (tmpRxSize > 256U)
0858 {
0859 base->MTDR = (uint32_t)(kRxDataCmd) | (uint32_t)LPI2C_MTDR_DATA(0xFFU);
0860 tmpRxSize -= 256U;
0861 }
0862 else
0863 {
0864 base->MTDR = (uint32_t)(kRxDataCmd) | (uint32_t)LPI2C_MTDR_DATA(tmpRxSize - 1U);
0865 tmpRxSize = 0U;
0866 }
0867 }
0868
0869
0870 buf = (uint8_t *)rxBuff;
0871 while (0U != (rxSize--))
0872 {
0873 #if I2C_RETRY_TIMES != 0U
0874 waitTimes = I2C_RETRY_TIMES;
0875 #endif
0876
0877
0878
0879 uint32_t value = 0U;
0880 do
0881 {
0882
0883 result = LPI2C_MasterCheckAndClearError(base, LPI2C_MasterGetStatusFlags(base));
0884 if (kStatus_Success != result)
0885 {
0886 break;
0887 }
0888
0889 value = base->MRDR;
0890 #if I2C_RETRY_TIMES != 0U
0891 waitTimes--;
0892 } while ((0U != (value & LPI2C_MRDR_RXEMPTY_MASK)) && (0U != waitTimes));
0893 if (0U == waitTimes)
0894 {
0895 result = kStatus_LPI2C_Timeout;
0896 }
0897 #else
0898 } while (0U != (value & LPI2C_MRDR_RXEMPTY_MASK));
0899 #endif
0900 if ((status_t)kStatus_Success != result)
0901 {
0902 break;
0903 }
0904
0905 *buf++ = (uint8_t)(value & LPI2C_MRDR_DATA_MASK);
0906 }
0907 }
0908 }
0909
0910 return result;
0911 }
0912
0913
0914
0915
0916
0917
0918
0919
0920
0921
0922
0923
0924
0925
0926
0927
0928
0929
0930 status_t LPI2C_MasterSend(LPI2C_Type *base, void *txBuff, size_t txSize)
0931 {
0932 status_t result = kStatus_Success;
0933 uint8_t *buf = (uint8_t *)txBuff;
0934
0935 assert(NULL != txBuff);
0936
0937
0938 while (0U != (txSize--))
0939 {
0940
0941 result = LPI2C_MasterWaitForTxReady(base);
0942 if (kStatus_Success != result)
0943 {
0944 break;
0945 }
0946
0947
0948 base->MTDR = *buf++;
0949 }
0950
0951 return result;
0952 }
0953
0954
0955
0956
0957
0958
0959
0960
0961
0962
0963
0964
0965
0966
0967
0968
0969 status_t LPI2C_MasterTransferBlocking(LPI2C_Type *base, lpi2c_master_transfer_t *transfer)
0970 {
0971 assert(NULL != transfer);
0972 assert(transfer->subaddressSize <= sizeof(transfer->subaddress));
0973
0974 status_t result = kStatus_Success;
0975 uint16_t commandBuffer[7];
0976 uint32_t cmdCount = 0U;
0977
0978
0979 if ((transfer->direction == kLPI2C_Read) &&
0980 (transfer->dataSize > ((size_t)256 * (size_t)FSL_FEATURE_LPI2C_FIFO_SIZEn(base))))
0981 {
0982 return kStatus_InvalidArgument;
0983 }
0984
0985
0986 LPI2C_MasterEnable(base, true);
0987 LPI2C_SlaveEnable(base, false);
0988
0989
0990 result = LPI2C_CheckForBusyBus(base);
0991 if (kStatus_Success == result)
0992 {
0993
0994 LPI2C_MasterClearStatusFlags(base, (uint32_t)kLPI2C_MasterClearFlags);
0995
0996
0997 base->MCFGR1 &= ~LPI2C_MCFGR1_AUTOSTOP_MASK;
0998
0999 lpi2c_direction_t direction = (0U != transfer->subaddressSize) ? kLPI2C_Write : transfer->direction;
1000 if (0U == (transfer->flags & (uint32_t)kLPI2C_TransferNoStartFlag))
1001 {
1002 commandBuffer[cmdCount++] =
1003 (uint16_t)kStartCmd |
1004 (uint16_t)((uint16_t)((uint16_t)transfer->slaveAddress << 1U) | (uint16_t)direction);
1005 }
1006
1007
1008 if (0U != transfer->subaddressSize)
1009 {
1010 uint32_t subaddressRemaining = transfer->subaddressSize;
1011 while (0U != subaddressRemaining--)
1012 {
1013 uint8_t subaddressByte = (uint8_t)((transfer->subaddress >> (8U * subaddressRemaining)) & 0xffU);
1014 commandBuffer[cmdCount++] = subaddressByte;
1015 }
1016 }
1017
1018
1019 if ((0U != transfer->dataSize) && (transfer->direction == kLPI2C_Read))
1020 {
1021
1022 if (direction == kLPI2C_Write)
1023 {
1024 commandBuffer[cmdCount++] =
1025 (uint16_t)kStartCmd |
1026 (uint16_t)((uint16_t)((uint16_t)transfer->slaveAddress << 1U) | (uint16_t)kLPI2C_Read);
1027 }
1028 }
1029
1030
1031 uint32_t index = 0U;
1032 while (0U != cmdCount--)
1033 {
1034
1035 result = LPI2C_MasterWaitForTxReady(base);
1036 if (kStatus_Success != result)
1037 {
1038 break;
1039 }
1040
1041
1042 base->MTDR = commandBuffer[index];
1043 index++;
1044 }
1045
1046 if (kStatus_Success == result)
1047 {
1048
1049 if ((transfer->direction == kLPI2C_Write) && (transfer->dataSize > 0U))
1050 {
1051
1052 result = LPI2C_MasterSend(base, transfer->data, transfer->dataSize);
1053 }
1054
1055
1056 if ((transfer->direction == kLPI2C_Read) && (transfer->dataSize > 0U))
1057 {
1058 result = LPI2C_MasterReceive(base, transfer->data, transfer->dataSize);
1059 }
1060
1061 if (kStatus_Success == result)
1062 {
1063 if ((transfer->flags & (uint32_t)kLPI2C_TransferNoStopFlag) == 0U)
1064 {
1065 result = LPI2C_MasterStop(base);
1066 }
1067 }
1068 }
1069 }
1070
1071 return result;
1072 }
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091 void LPI2C_MasterTransferCreateHandle(LPI2C_Type *base,
1092 lpi2c_master_handle_t *handle,
1093 lpi2c_master_transfer_callback_t callback,
1094 void *userData)
1095 {
1096 uint32_t instance;
1097
1098 assert(NULL != handle);
1099
1100
1101 (void)memset(handle, 0, sizeof(*handle));
1102
1103
1104 instance = LPI2C_GetInstance(base);
1105
1106
1107 handle->completionCallback = callback;
1108 handle->userData = userData;
1109
1110
1111 s_lpi2cMasterHandle[instance] = handle;
1112
1113
1114 s_lpi2cMasterIsr = LPI2C_MasterTransferHandleIRQ;
1115
1116
1117 LPI2C_MasterDisableInterrupts(base, (uint32_t)kLPI2C_MasterIrqFlags);
1118
1119
1120
1121
1122 (void)EnableIRQ(kLpi2cIrqs[instance]);
1123 }
1124
1125 static void LPI2C_TransferStateMachineSendCommand(LPI2C_Type *base,
1126 lpi2c_master_handle_t *handle,
1127 lpi2c_state_machine_param_t *stateParams)
1128 {
1129 assert(stateParams != NULL);
1130 uint16_t sendval;
1131
1132
1133 if (0U == (stateParams->txCount)--)
1134 {
1135 stateParams->state_complete = true;
1136 return;
1137 }
1138
1139
1140 sendval = ((uint16_t)handle->buf[0]) | (((uint16_t)handle->buf[1]) << 8U);
1141 base->MTDR = sendval;
1142 handle->buf++;
1143 handle->buf++;
1144
1145
1146 if (--handle->remainingBytes == 0U)
1147 {
1148
1149 if (0U != handle->transfer.dataSize)
1150 {
1151
1152 handle->state = (uint8_t)kTransferDataState;
1153 handle->buf = (uint8_t *)handle->transfer.data;
1154 handle->remainingBytes = (uint16_t)handle->transfer.dataSize;
1155 if (handle->transfer.direction == kLPI2C_Read)
1156 {
1157
1158 LPI2C_MasterDisableInterrupts(base, (uint32_t)kLPI2C_MasterTxReadyFlag);
1159
1160
1161
1162 size_t tmpRxSize = handle->transfer.dataSize;
1163 while (tmpRxSize != 0U)
1164 {
1165 LPI2C_MasterGetFifoCounts(base, NULL, &stateParams->txCount);
1166 while ((size_t)FSL_FEATURE_LPI2C_FIFO_SIZEn(base) == stateParams->txCount)
1167 {
1168 LPI2C_MasterGetFifoCounts(base, NULL, &stateParams->txCount);
1169 }
1170
1171 if (tmpRxSize > 256U)
1172 {
1173 base->MTDR = (uint32_t)(kRxDataCmd) | (uint32_t)LPI2C_MTDR_DATA(0xFFU);
1174 tmpRxSize -= 256U;
1175 }
1176 else
1177 {
1178 base->MTDR = (uint32_t)(kRxDataCmd) | (uint32_t)LPI2C_MTDR_DATA(tmpRxSize - 1U);
1179 tmpRxSize = 0U;
1180 }
1181 }
1182 }
1183 }
1184 else
1185 {
1186
1187 handle->state = (uint8_t)kStopState;
1188 }
1189 }
1190 }
1191
1192 static void LPI2C_TransferStateMachineReadCommand(LPI2C_Type *base,
1193 lpi2c_master_handle_t *handle,
1194 lpi2c_state_machine_param_t *stateParams)
1195 {
1196 assert(stateParams != NULL);
1197
1198
1199 if (0U == (stateParams->txCount)--)
1200 {
1201 stateParams->state_complete = true;
1202 return;
1203 }
1204
1205 base->MTDR = (uint32_t)kRxDataCmd | LPI2C_MTDR_DATA(handle->transfer.dataSize - 1U);
1206
1207
1208 handle->state = (uint8_t)kTransferDataState;
1209 if (handle->transfer.direction == kLPI2C_Read)
1210 {
1211
1212 LPI2C_MasterDisableInterrupts(base, (uint32_t)kLPI2C_MasterTxReadyFlag);
1213 }
1214 }
1215
1216 static void LPI2C_TransferStateMachineTransferData(LPI2C_Type *base,
1217 lpi2c_master_handle_t *handle,
1218 lpi2c_state_machine_param_t *stateParams)
1219 {
1220 assert(stateParams != NULL);
1221
1222 if (handle->transfer.direction == kLPI2C_Write)
1223 {
1224
1225 if (0U == stateParams->txCount--)
1226 {
1227 stateParams->state_complete = true;
1228 return;
1229 }
1230
1231
1232 base->MTDR = *(handle->buf)++;
1233 }
1234 else
1235 {
1236
1237
1238 if (0U == stateParams->rxCount--)
1239 {
1240 stateParams->state_complete = true;
1241 return;
1242 }
1243
1244
1245 *(handle->buf)++ = (uint8_t)(base->MRDR & LPI2C_MRDR_DATA_MASK);
1246 }
1247
1248
1249 if (--handle->remainingBytes == 0U)
1250 {
1251 if (handle->transfer.direction == kLPI2C_Write)
1252 {
1253 stateParams->state_complete = true;
1254 }
1255 handle->state = (uint8_t)kStopState;
1256 }
1257 }
1258
1259 static void LPI2C_TransferStateMachineStopState(LPI2C_Type *base,
1260 lpi2c_master_handle_t *handle,
1261 lpi2c_state_machine_param_t *stateParams,
1262 bool *isDone)
1263 {
1264 assert(stateParams != NULL);
1265
1266
1267 if ((handle->transfer.flags & (uint32_t)kLPI2C_TransferNoStopFlag) == 0U)
1268 {
1269
1270 if (0U == (stateParams->txCount)--)
1271 {
1272 stateParams->state_complete = true;
1273 return;
1274 }
1275
1276 base->MTDR = (uint32_t)kStopCmd;
1277 }
1278 else
1279 {
1280
1281 if (handle->transfer.direction == kLPI2C_Read)
1282 {
1283 *isDone = true;
1284 }
1285 stateParams->state_complete = true;
1286 }
1287 handle->state = (uint8_t)kWaitForCompletionState;
1288 }
1289
1290 static void LPI2C_TransferStateMachineWaitState(LPI2C_Type *base,
1291 lpi2c_master_handle_t *handle,
1292 lpi2c_state_machine_param_t *stateParams,
1293 bool *isDone)
1294 {
1295 assert(stateParams != NULL);
1296
1297 if ((handle->transfer.flags & (uint32_t)kLPI2C_TransferNoStopFlag) == 0U)
1298 {
1299
1300 if (0U != ((stateParams->status) & (uint32_t)kLPI2C_MasterStopDetectFlag))
1301 {
1302 *isDone = true;
1303 }
1304 }
1305 else
1306 {
1307
1308
1309 if ((handle->transfer.direction == kLPI2C_Write) && ((base->MFSR & LPI2C_MFSR_TXCOUNT_MASK) == 0U))
1310 {
1311
1312 *isDone = true;
1313 }
1314 }
1315 stateParams->state_complete = true;
1316 }
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328 static status_t LPI2C_RunTransferStateMachine(LPI2C_Type *base, lpi2c_master_handle_t *handle, bool *isDone)
1329 {
1330 assert(NULL != base && NULL != handle && NULL != isDone);
1331
1332 status_t result = kStatus_Success;
1333 lpi2c_state_machine_param_t stateParams;
1334 (void)memset(&stateParams, 0, sizeof(stateParams));
1335
1336 stateParams.state_complete = false;
1337
1338
1339 *isDone = false;
1340
1341
1342 stateParams.status = LPI2C_MasterGetStatusFlags(base);
1343
1344
1345 LPI2C_MasterGetFifoCounts(base, &stateParams.rxCount, &stateParams.txCount);
1346
1347
1348
1349
1350 if (handle->remainingBytes == 0U)
1351 {
1352
1353
1354 if (((handle->transfer).dataSize != 0U) &&
1355 ((stateParams.txCount == 0U) ||
1356 (((stateParams.txCount) == 1U) && (handle->state == (uint8_t)kWaitForCompletionState) &&
1357 (((handle->transfer).flags & (uint32_t)kLPI2C_TransferNoStopFlag) == 0U))))
1358 {
1359 (stateParams.status) &= ~(uint32_t)kLPI2C_MasterNackDetectFlag;
1360 }
1361 }
1362
1363 result = LPI2C_MasterCheckAndClearError(base, stateParams.status);
1364
1365 if (kStatus_Success == result)
1366 {
1367
1368 stateParams.txCount = (size_t)FSL_FEATURE_LPI2C_FIFO_SIZEn(base) - stateParams.txCount;
1369
1370 while (!stateParams.state_complete)
1371 {
1372
1373 switch (handle->state)
1374 {
1375 case (uint8_t)kSendCommandState:
1376 LPI2C_TransferStateMachineSendCommand(base, handle, &stateParams);
1377 break;
1378
1379 case (uint8_t)kIssueReadCommandState:
1380 LPI2C_TransferStateMachineReadCommand(base, handle, &stateParams);
1381 break;
1382
1383 case (uint8_t)kTransferDataState:
1384 LPI2C_TransferStateMachineTransferData(base, handle, &stateParams);
1385 break;
1386
1387 case (uint8_t)kStopState:
1388 LPI2C_TransferStateMachineStopState(base, handle, &stateParams, isDone);
1389 break;
1390
1391 case (uint8_t)kWaitForCompletionState:
1392 LPI2C_TransferStateMachineWaitState(base, handle, &stateParams, isDone);
1393 break;
1394 default:
1395 assert(false);
1396 break;
1397 }
1398 }
1399 }
1400 return result;
1401 }
1402
1403
1404
1405
1406
1407 static void LPI2C_InitTransferStateMachine(lpi2c_master_handle_t *handle)
1408 {
1409 lpi2c_master_transfer_t *xfer = &handle->transfer;
1410
1411
1412 if (0U != (xfer->flags & (uint32_t)kLPI2C_TransferNoStartFlag))
1413 {
1414 if (xfer->direction == kLPI2C_Read)
1415 {
1416
1417 handle->state = (uint8_t)kIssueReadCommandState;
1418 }
1419 else
1420 {
1421
1422 handle->state = (uint8_t)kTransferDataState;
1423 }
1424
1425 handle->buf = (uint8_t *)xfer->data;
1426 handle->remainingBytes = (uint16_t)xfer->dataSize;
1427 }
1428 else
1429 {
1430 uint16_t *cmd = (uint16_t *)&handle->commandBuffer;
1431 uint32_t cmdCount = 0U;
1432
1433
1434
1435 lpi2c_direction_t direction = (0U != xfer->subaddressSize) ? kLPI2C_Write : xfer->direction;
1436
1437
1438 cmd[cmdCount++] =
1439 (uint16_t)kStartCmd | (uint16_t)((uint16_t)((uint16_t)xfer->slaveAddress << 1U) | (uint16_t)direction);
1440
1441
1442 if (0U != xfer->subaddressSize)
1443 {
1444 uint32_t subaddressRemaining = xfer->subaddressSize;
1445 while (0U != (subaddressRemaining--))
1446 {
1447 uint8_t subaddressByte = (uint8_t)((xfer->subaddress >> (8U * subaddressRemaining)) & 0xffU);
1448 cmd[cmdCount++] = subaddressByte;
1449 }
1450 }
1451
1452
1453 if ((0U != xfer->dataSize) && (xfer->direction == kLPI2C_Read))
1454 {
1455
1456 if (direction == kLPI2C_Write)
1457 {
1458 cmd[cmdCount++] = (uint16_t)kStartCmd |
1459 (uint16_t)((uint16_t)((uint16_t)xfer->slaveAddress << 1U) | (uint16_t)kLPI2C_Read);
1460 }
1461 }
1462
1463
1464 handle->state = (uint8_t)kSendCommandState;
1465 handle->remainingBytes = (uint16_t)cmdCount;
1466 handle->buf = (uint8_t *)&handle->commandBuffer;
1467 }
1468 }
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480 status_t LPI2C_MasterTransferNonBlocking(LPI2C_Type *base,
1481 lpi2c_master_handle_t *handle,
1482 lpi2c_master_transfer_t *transfer)
1483 {
1484 assert(NULL != handle);
1485 assert(NULL != transfer);
1486 assert(transfer->subaddressSize <= sizeof(transfer->subaddress));
1487
1488 status_t result;
1489
1490
1491 if ((transfer->direction == kLPI2C_Read) &&
1492 (transfer->dataSize > (256U * (uint32_t)FSL_FEATURE_LPI2C_FIFO_SIZEn(base))))
1493 {
1494 return kStatus_InvalidArgument;
1495 }
1496
1497
1498 if (handle->state != (uint8_t)kIdleState)
1499 {
1500 result = kStatus_LPI2C_Busy;
1501 }
1502 else
1503 {
1504 result = LPI2C_CheckForBusyBus(base);
1505 }
1506
1507 if ((status_t)kStatus_Success == result)
1508 {
1509
1510 LPI2C_MasterEnable(base, true);
1511 LPI2C_SlaveEnable(base, false);
1512
1513
1514 LPI2C_MasterDisableInterrupts(base, (uint32_t)kLPI2C_MasterIrqFlags);
1515
1516
1517 base->MCR |= LPI2C_MCR_RRF_MASK | LPI2C_MCR_RTF_MASK;
1518
1519
1520 handle->transfer = *transfer;
1521
1522
1523 LPI2C_InitTransferStateMachine(handle);
1524
1525
1526 LPI2C_MasterClearStatusFlags(base, (uint32_t)kLPI2C_MasterClearFlags);
1527
1528
1529 base->MCFGR1 &= ~LPI2C_MCFGR1_AUTOSTOP_MASK;
1530
1531
1532 LPI2C_MasterEnableInterrupts(base, (uint32_t)kLPI2C_MasterIrqFlags);
1533 }
1534
1535 return result;
1536 }
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546 status_t LPI2C_MasterTransferGetCount(LPI2C_Type *base, lpi2c_master_handle_t *handle, size_t *count)
1547 {
1548 status_t result = kStatus_Success;
1549
1550 assert(NULL != handle);
1551
1552 if (NULL == count)
1553 {
1554 result = kStatus_InvalidArgument;
1555 }
1556
1557
1558 else if (handle->state == (uint8_t)kIdleState)
1559 {
1560 *count = 0;
1561 result = kStatus_NoTransferInProgress;
1562 }
1563 else
1564 {
1565 uint8_t state;
1566 uint16_t remainingBytes;
1567 uint32_t dataSize;
1568
1569
1570
1571 uint32_t irqs = LPI2C_MasterGetEnabledInterrupts(base);
1572 LPI2C_MasterDisableInterrupts(base, irqs);
1573 state = handle->state;
1574 remainingBytes = handle->remainingBytes;
1575 dataSize = handle->transfer.dataSize;
1576 LPI2C_MasterEnableInterrupts(base, irqs);
1577
1578
1579 switch (state)
1580 {
1581 case (uint8_t)kIdleState:
1582 case (uint8_t)kSendCommandState:
1583 case (uint8_t)
1584 kIssueReadCommandState:
1585 *count = 0;
1586 break;
1587
1588 case (uint8_t)kTransferDataState:
1589 *count = dataSize - remainingBytes;
1590 break;
1591
1592 case (uint8_t)kStopState:
1593 case (uint8_t)kWaitForCompletionState:
1594 default:
1595 *count = dataSize;
1596 break;
1597 }
1598 }
1599
1600 return result;
1601 }
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614 void LPI2C_MasterTransferAbort(LPI2C_Type *base, lpi2c_master_handle_t *handle)
1615 {
1616 if (handle->state != (uint8_t)kIdleState)
1617 {
1618
1619 LPI2C_MasterDisableInterrupts(base, (uint32_t)kLPI2C_MasterIrqFlags);
1620
1621
1622 base->MCR |= LPI2C_MCR_RRF_MASK | LPI2C_MCR_RTF_MASK;
1623
1624
1625 if ((LPI2C_MasterGetStatusFlags(base) & ((uint32_t)kLPI2C_MasterStopDetectFlag |
1626 (uint32_t)kLPI2C_MasterBusyFlag)) == (uint32_t)kLPI2C_MasterBusyFlag)
1627 {
1628
1629 base->MTDR = (uint32_t)kStopCmd;
1630 }
1631
1632
1633 handle->state = (uint8_t)kIdleState;
1634 }
1635 }
1636
1637
1638
1639
1640
1641
1642
1643
1644 void LPI2C_MasterTransferHandleIRQ(LPI2C_Type *base, void *lpi2cMasterHandle)
1645 {
1646 assert(lpi2cMasterHandle != NULL);
1647
1648 lpi2c_master_handle_t *handle = (lpi2c_master_handle_t *)lpi2cMasterHandle;
1649 bool isDone = false;
1650 status_t result;
1651
1652
1653 if (NULL != handle)
1654 {
1655 if (handle->state != (uint8_t)kIdleState)
1656 {
1657 result = LPI2C_RunTransferStateMachine(base, handle, &isDone);
1658
1659 if ((result != kStatus_Success) || isDone)
1660 {
1661
1662 if (result != kStatus_Success)
1663 {
1664 LPI2C_MasterTransferAbort(base, handle);
1665 }
1666
1667
1668 LPI2C_MasterDisableInterrupts(base, (uint32_t)kLPI2C_MasterIrqFlags);
1669
1670
1671 handle->state = (uint8_t)kIdleState;
1672
1673
1674 if (NULL != handle->completionCallback)
1675 {
1676 handle->completionCallback(base, handle, result, handle->userData);
1677 }
1678 }
1679 }
1680 }
1681 }
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714 void LPI2C_SlaveGetDefaultConfig(lpi2c_slave_config_t *slaveConfig)
1715 {
1716
1717 (void)memset(slaveConfig, 0, sizeof(*slaveConfig));
1718
1719 slaveConfig->enableSlave = true;
1720 slaveConfig->address0 = 0U;
1721 slaveConfig->address1 = 0U;
1722 slaveConfig->addressMatchMode = kLPI2C_MatchAddress0;
1723 slaveConfig->filterDozeEnable = true;
1724 slaveConfig->filterEnable = true;
1725 slaveConfig->enableGeneralCall = false;
1726 slaveConfig->sclStall.enableAck = false;
1727 slaveConfig->sclStall.enableTx = true;
1728 slaveConfig->sclStall.enableRx = true;
1729 slaveConfig->sclStall.enableAddress = false;
1730 slaveConfig->ignoreAck = false;
1731 slaveConfig->enableReceivedAddressRead = false;
1732 slaveConfig->sdaGlitchFilterWidth_ns = 0U;
1733 slaveConfig->sclGlitchFilterWidth_ns = 0U;
1734 slaveConfig->dataValidDelay_ns = 0U;
1735
1736
1737
1738 slaveConfig->clockHoldTime_ns = 250U;
1739 }
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753 void LPI2C_SlaveInit(LPI2C_Type *base, const lpi2c_slave_config_t *slaveConfig, uint32_t sourceClock_Hz)
1754 {
1755 uint32_t tmpReg;
1756 uint32_t tmpCycle;
1757
1758 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
1759
1760 uint32_t instance = LPI2C_GetInstance(base);
1761
1762
1763 (void)CLOCK_EnableClock(kLpi2cClocks[instance]);
1764 #if defined(LPI2C_PERIPH_CLOCKS)
1765
1766 CLOCK_EnableClock(kLpi2cPeriphClocks[instance]);
1767 #endif
1768
1769 #endif
1770
1771
1772 LPI2C_SlaveReset(base);
1773
1774
1775 base->SAMR = LPI2C_SAMR_ADDR0(slaveConfig->address0) | LPI2C_SAMR_ADDR1(slaveConfig->address1);
1776
1777 base->SCFGR1 =
1778 LPI2C_SCFGR1_ADDRCFG(slaveConfig->addressMatchMode) | LPI2C_SCFGR1_IGNACK(slaveConfig->ignoreAck) |
1779 LPI2C_SCFGR1_RXCFG(slaveConfig->enableReceivedAddressRead) | LPI2C_SCFGR1_GCEN(slaveConfig->enableGeneralCall) |
1780 LPI2C_SCFGR1_ACKSTALL(slaveConfig->sclStall.enableAck) | LPI2C_SCFGR1_TXDSTALL(slaveConfig->sclStall.enableTx) |
1781 LPI2C_SCFGR1_RXSTALL(slaveConfig->sclStall.enableRx) |
1782 LPI2C_SCFGR1_ADRSTALL(slaveConfig->sclStall.enableAddress);
1783
1784
1785
1786 tmpReg = LPI2C_SCFGR2_FILTSDA(
1787 LPI2C_GetCyclesForWidth(sourceClock_Hz, slaveConfig->sdaGlitchFilterWidth_ns, 4U,
1788 (LPI2C_SCFGR2_FILTSDA_MASK >> LPI2C_SCFGR2_FILTSDA_SHIFT) + 3U, 0U) -
1789 3U);
1790
1791
1792
1793 tmpCycle = LPI2C_GetCyclesForWidth(sourceClock_Hz, slaveConfig->sclGlitchFilterWidth_ns, 4U,
1794 (LPI2C_SCFGR2_FILTSCL_MASK >> LPI2C_SCFGR2_FILTSCL_SHIFT) + 3U, 0U);
1795 tmpReg |= LPI2C_SCFGR2_FILTSCL(tmpCycle - 3U);
1796
1797
1798
1799 tmpReg |= LPI2C_SCFGR2_DATAVD(
1800 LPI2C_GetCyclesForWidth(sourceClock_Hz, slaveConfig->dataValidDelay_ns, tmpCycle,
1801 tmpCycle + (LPI2C_SCFGR2_DATAVD_MASK >> LPI2C_SCFGR2_DATAVD_SHIFT), 0U) -
1802 tmpCycle);
1803
1804
1805
1806 base->SCFGR2 =
1807 tmpReg | LPI2C_SCFGR2_CLKHOLD(
1808 LPI2C_GetCyclesForWidth(sourceClock_Hz, slaveConfig->clockHoldTime_ns, 3U,
1809 (LPI2C_SCFGR2_CLKHOLD_MASK >> LPI2C_SCFGR2_CLKHOLD_SHIFT) + 3U, 0U) -
1810 3U);
1811
1812
1813 base->SCR = LPI2C_SCR_FILTDZ(!slaveConfig->filterDozeEnable) | LPI2C_SCR_FILTEN(slaveConfig->filterEnable) |
1814 LPI2C_SCR_SEN(slaveConfig->enableSlave);
1815 }
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825 void LPI2C_SlaveDeinit(LPI2C_Type *base)
1826 {
1827 LPI2C_SlaveReset(base);
1828
1829 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
1830
1831 uint32_t instance = LPI2C_GetInstance(base);
1832
1833
1834 (void)CLOCK_DisableClock(kLpi2cClocks[instance]);
1835
1836 #if defined(LPI2C_PERIPH_CLOCKS)
1837
1838 CLOCK_DisableClock(kLpi2cPeriphClocks[instance]);
1839 #endif
1840
1841 #endif
1842 }
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852 static status_t LPI2C_SlaveCheckAndClearError(LPI2C_Type *base, uint32_t flags)
1853 {
1854 status_t result = kStatus_Success;
1855
1856 flags &= (uint32_t)kLPI2C_SlaveErrorFlags;
1857 if (0U != flags)
1858 {
1859 if (0U != (flags & (uint32_t)kLPI2C_SlaveBitErrFlag))
1860 {
1861 result = kStatus_LPI2C_BitError;
1862 }
1863 else if (0U != (flags & (uint32_t)kLPI2C_SlaveFifoErrFlag))
1864 {
1865 result = kStatus_LPI2C_FifoError;
1866 }
1867 else
1868 {
1869 ;
1870 }
1871
1872
1873 LPI2C_SlaveClearStatusFlags(base, flags);
1874 }
1875 else
1876 {
1877 ;
1878 }
1879
1880 return result;
1881 }
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892 status_t LPI2C_SlaveSend(LPI2C_Type *base, void *txBuff, size_t txSize, size_t *actualTxSize)
1893 {
1894 status_t result = kStatus_Success;
1895 uint8_t *buf = (uint8_t *)txBuff;
1896 size_t remaining = txSize;
1897
1898 assert(NULL != txBuff);
1899
1900 #if I2C_RETRY_TIMES != 0U
1901 uint32_t waitTimes = I2C_RETRY_TIMES;
1902 #endif
1903
1904
1905 LPI2C_SlaveClearStatusFlags(base,
1906 (uint32_t)kLPI2C_SlaveStopDetectFlag | (uint32_t)kLPI2C_SlaveRepeatedStartDetectFlag);
1907
1908 while (0U != remaining)
1909 {
1910 uint32_t flags;
1911
1912
1913 do
1914 {
1915
1916 flags = LPI2C_SlaveGetStatusFlags(base);
1917 result = LPI2C_SlaveCheckAndClearError(base, flags);
1918 if (kStatus_Success != result)
1919 {
1920 if (NULL != actualTxSize)
1921 {
1922 *actualTxSize = txSize - remaining;
1923 }
1924 break;
1925 }
1926 #if I2C_RETRY_TIMES != 0U
1927 waitTimes--;
1928 } while ((0U == (flags & ((uint32_t)kLPI2C_SlaveTxReadyFlag | (uint32_t)kLPI2C_SlaveStopDetectFlag |
1929 (uint32_t)kLPI2C_SlaveRepeatedStartDetectFlag))) &&
1930 (0U != waitTimes));
1931 if (0U == waitTimes)
1932 {
1933 result = kStatus_LPI2C_Timeout;
1934 }
1935 #else
1936 } while (0U == (flags & ((uint32_t)kLPI2C_SlaveTxReadyFlag | (uint32_t)kLPI2C_SlaveStopDetectFlag |
1937 (uint32_t)kLPI2C_SlaveRepeatedStartDetectFlag)));
1938 #endif
1939
1940 if (kStatus_Success != result)
1941 {
1942 break;
1943 }
1944
1945
1946 if (0U != (flags & (uint32_t)kLPI2C_SlaveTxReadyFlag))
1947 {
1948 base->STDR = *buf++;
1949 --remaining;
1950 }
1951
1952
1953 if ((0U != (flags & ((uint32_t)kLPI2C_SlaveStopDetectFlag | (uint32_t)kLPI2C_SlaveRepeatedStartDetectFlag))) &&
1954 (remaining != 0U))
1955 {
1956 LPI2C_SlaveClearStatusFlags(
1957 base, (uint32_t)kLPI2C_SlaveStopDetectFlag | (uint32_t)kLPI2C_SlaveRepeatedStartDetectFlag);
1958 break;
1959 }
1960 }
1961
1962 if (NULL != actualTxSize)
1963 {
1964 *actualTxSize = txSize - remaining;
1965 }
1966
1967 return result;
1968 }
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979 status_t LPI2C_SlaveReceive(LPI2C_Type *base, void *rxBuff, size_t rxSize, size_t *actualRxSize)
1980 {
1981 status_t result = kStatus_Success;
1982 uint8_t *buf = (uint8_t *)rxBuff;
1983 size_t remaining = rxSize;
1984
1985 assert(NULL != rxBuff);
1986
1987 #if I2C_RETRY_TIMES != 0U
1988 uint32_t waitTimes = I2C_RETRY_TIMES;
1989 #endif
1990
1991
1992 LPI2C_SlaveClearStatusFlags(base,
1993 (uint32_t)kLPI2C_SlaveStopDetectFlag | (uint32_t)kLPI2C_SlaveRepeatedStartDetectFlag);
1994
1995 while (0U != remaining)
1996 {
1997 uint32_t flags;
1998
1999
2000 do
2001 {
2002
2003 flags = LPI2C_SlaveGetStatusFlags(base);
2004 result = LPI2C_SlaveCheckAndClearError(base, flags);
2005 if (kStatus_Success != result)
2006 {
2007 if (NULL != actualRxSize)
2008 {
2009 *actualRxSize = rxSize - remaining;
2010 }
2011 break;
2012 }
2013 #if I2C_RETRY_TIMES != 0U
2014 waitTimes--;
2015 } while ((0U == (flags & ((uint32_t)kLPI2C_SlaveRxReadyFlag | (uint32_t)kLPI2C_SlaveStopDetectFlag |
2016 (uint32_t)kLPI2C_SlaveRepeatedStartDetectFlag))) &&
2017 (0U != waitTimes));
2018 if (0U == waitTimes)
2019 {
2020 result = kStatus_LPI2C_Timeout;
2021 }
2022 #else
2023 } while (0U == (flags & ((uint32_t)kLPI2C_SlaveRxReadyFlag | (uint32_t)kLPI2C_SlaveStopDetectFlag |
2024 (uint32_t)kLPI2C_SlaveRepeatedStartDetectFlag)));
2025 #endif
2026
2027 if ((status_t)kStatus_Success != result)
2028 {
2029 break;
2030 }
2031
2032
2033 if (0U != (flags & (uint32_t)kLPI2C_SlaveRxReadyFlag))
2034 {
2035 *buf++ = (uint8_t)(base->SRDR & LPI2C_SRDR_DATA_MASK);
2036 --remaining;
2037 }
2038
2039
2040 if ((0U != (flags & ((uint32_t)kLPI2C_SlaveStopDetectFlag | (uint32_t)kLPI2C_SlaveRepeatedStartDetectFlag))) &&
2041 (remaining != 0U))
2042 {
2043 LPI2C_SlaveClearStatusFlags(
2044 base, (uint32_t)kLPI2C_SlaveStopDetectFlag | (uint32_t)kLPI2C_SlaveRepeatedStartDetectFlag);
2045 break;
2046 }
2047 }
2048
2049 if (NULL != actualRxSize)
2050 {
2051 *actualRxSize = rxSize - remaining;
2052 }
2053
2054 return result;
2055 }
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073 void LPI2C_SlaveTransferCreateHandle(LPI2C_Type *base,
2074 lpi2c_slave_handle_t *handle,
2075 lpi2c_slave_transfer_callback_t callback,
2076 void *userData)
2077 {
2078 uint32_t instance;
2079
2080 assert(NULL != handle);
2081
2082
2083 (void)memset(handle, 0, sizeof(*handle));
2084
2085
2086 instance = LPI2C_GetInstance(base);
2087
2088
2089 handle->callback = callback;
2090 handle->userData = userData;
2091
2092
2093 s_lpi2cSlaveHandle[instance] = handle;
2094
2095
2096 s_lpi2cSlaveIsr = LPI2C_SlaveTransferHandleIRQ;
2097
2098
2099 LPI2C_SlaveDisableInterrupts(base, (uint32_t)kLPI2C_SlaveIrqFlags);
2100 (void)EnableIRQ(kLpi2cIrqs[instance]);
2101
2102
2103 base->STAR = LPI2C_STAR_TXNACK_MASK;
2104 }
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130 status_t LPI2C_SlaveTransferNonBlocking(LPI2C_Type *base, lpi2c_slave_handle_t *handle, uint32_t eventMask)
2131 {
2132 status_t result = kStatus_Success;
2133
2134 assert(NULL != handle);
2135
2136
2137 if (handle->isBusy)
2138 {
2139 result = kStatus_LPI2C_Busy;
2140 }
2141 else
2142 {
2143
2144 LPI2C_MasterEnable(base, false);
2145 LPI2C_SlaveEnable(base, true);
2146
2147 uint32_t status = LPI2C_SlaveGetStatusFlags(base);
2148 if ((0U != (status & (uint32_t)kLPI2C_SlaveBusBusyFlag)) && (0U == (status & (uint32_t)kLPI2C_SlaveBusyFlag)))
2149 {
2150 result = kStatus_LPI2C_Busy;
2151 }
2152 }
2153
2154 if ((status_t)kStatus_Success == result)
2155 {
2156
2157 LPI2C_SlaveDisableInterrupts(base, (uint32_t)kLPI2C_SlaveIrqFlags);
2158
2159
2160 (void)memset(&handle->transfer, 0, sizeof(handle->transfer));
2161
2162
2163 handle->isBusy = true;
2164
2165
2166 handle->eventMask = eventMask | (uint32_t)kLPI2C_SlaveTransmitEvent | (uint32_t)kLPI2C_SlaveReceiveEvent;
2167
2168
2169 base->STAR = 0U;
2170
2171
2172 LPI2C_SlaveClearStatusFlags(base, (uint32_t)kLPI2C_SlaveClearFlags);
2173
2174
2175 LPI2C_SlaveEnableInterrupts(base, (uint32_t)kLPI2C_SlaveIrqFlags);
2176 }
2177
2178 return result;
2179 }
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190 status_t LPI2C_SlaveTransferGetCount(LPI2C_Type *base, lpi2c_slave_handle_t *handle, size_t *count)
2191 {
2192 status_t status = kStatus_Success;
2193
2194 assert(NULL != handle);
2195
2196 if (count == NULL)
2197 {
2198 status = kStatus_InvalidArgument;
2199 }
2200
2201
2202 else if (!handle->isBusy)
2203 {
2204 *count = 0;
2205 status = kStatus_NoTransferInProgress;
2206 }
2207
2208
2209 else
2210 {
2211 *count = handle->transferredCount;
2212 }
2213
2214 return status;
2215 }
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225 void LPI2C_SlaveTransferAbort(LPI2C_Type *base, lpi2c_slave_handle_t *handle)
2226 {
2227 assert(NULL != handle);
2228
2229
2230 if (handle->isBusy)
2231 {
2232
2233 LPI2C_SlaveDisableInterrupts(base, (uint32_t)kLPI2C_SlaveIrqFlags);
2234
2235
2236 base->STAR = LPI2C_STAR_TXNACK_MASK;
2237
2238
2239 (void)memset(&handle->transfer, 0, sizeof(handle->transfer));
2240
2241
2242 handle->isBusy = false;
2243 }
2244 }
2245
2246
2247
2248
2249
2250
2251
2252
2253 void LPI2C_SlaveTransferHandleIRQ(LPI2C_Type *base, lpi2c_slave_handle_t *handle)
2254 {
2255 uint32_t flags;
2256 lpi2c_slave_transfer_t *xfer;
2257
2258
2259 if (NULL != handle)
2260 {
2261 xfer = &handle->transfer;
2262
2263
2264 flags = LPI2C_SlaveGetStatusFlags(base);
2265
2266 if (0U != (flags & ((uint32_t)kLPI2C_SlaveBitErrFlag | (uint32_t)kLPI2C_SlaveFifoErrFlag)))
2267 {
2268 xfer->event = kLPI2C_SlaveCompletionEvent;
2269 xfer->completionStatus = LPI2C_SlaveCheckAndClearError(base, flags);
2270
2271 if ((0U != (handle->eventMask & (uint32_t)kLPI2C_SlaveCompletionEvent)) && (NULL != handle->callback))
2272 {
2273 handle->callback(base, xfer, handle->userData);
2274 }
2275 }
2276 else
2277 {
2278 if (0U !=
2279 (flags & (((uint32_t)kLPI2C_SlaveRepeatedStartDetectFlag) | ((uint32_t)kLPI2C_SlaveStopDetectFlag))))
2280 {
2281 xfer->event = (0U != (flags & (uint32_t)kLPI2C_SlaveRepeatedStartDetectFlag)) ?
2282 kLPI2C_SlaveRepeatedStartEvent :
2283 kLPI2C_SlaveCompletionEvent;
2284 xfer->receivedAddress = 0U;
2285 xfer->completionStatus = kStatus_Success;
2286 xfer->transferredCount = handle->transferredCount;
2287
2288 if (xfer->event == kLPI2C_SlaveCompletionEvent)
2289 {
2290 handle->isBusy = false;
2291 }
2292
2293 if (handle->wasTransmit)
2294 {
2295
2296
2297
2298 --xfer->transferredCount;
2299 handle->wasTransmit = false;
2300 }
2301
2302
2303 LPI2C_SlaveClearStatusFlags(base, flags & ((uint32_t)kLPI2C_SlaveRepeatedStartDetectFlag |
2304 (uint32_t)kLPI2C_SlaveStopDetectFlag));
2305
2306
2307 base->STAR = 0U;
2308
2309 if ((0U != (handle->eventMask & (uint32_t)xfer->event)) && (NULL != handle->callback))
2310 {
2311 handle->callback(base, xfer, handle->userData);
2312 }
2313
2314 if (0U != (flags & (uint32_t)kLPI2C_SlaveStopDetectFlag))
2315 {
2316
2317 (void)memset(&handle->transfer, 0, sizeof(handle->transfer));
2318 }
2319 }
2320 if (0U != (flags & (uint32_t)kLPI2C_SlaveAddressValidFlag))
2321 {
2322 xfer->event = kLPI2C_SlaveAddressMatchEvent;
2323 xfer->receivedAddress = (uint8_t)(base->SASR & LPI2C_SASR_RADDR_MASK);
2324
2325
2326 handle->isBusy = true;
2327 if ((0U != (handle->eventMask & (uint32_t)kLPI2C_SlaveAddressMatchEvent)) && (NULL != handle->callback))
2328 {
2329 handle->callback(base, xfer, handle->userData);
2330 }
2331 }
2332 if (0U != (flags & (uint32_t)kLPI2C_SlaveTransmitAckFlag))
2333 {
2334 xfer->event = kLPI2C_SlaveTransmitAckEvent;
2335
2336 if ((0U != (handle->eventMask & (uint32_t)kLPI2C_SlaveTransmitAckEvent)) && (NULL != handle->callback))
2337 {
2338 handle->callback(base, xfer, handle->userData);
2339 }
2340 }
2341
2342
2343 if (0U != (flags & (uint32_t)kLPI2C_SlaveTxReadyFlag))
2344 {
2345 handle->wasTransmit = true;
2346
2347
2348 if ((NULL == xfer->data) || (0U == xfer->dataSize))
2349 {
2350 xfer->event = kLPI2C_SlaveTransmitEvent;
2351 if (NULL != handle->callback)
2352 {
2353 handle->callback(base, xfer, handle->userData);
2354 }
2355
2356
2357 handle->transferredCount = 0U;
2358 }
2359
2360
2361 if ((NULL != xfer->data) && (0U != xfer->dataSize))
2362 {
2363 base->STDR = *xfer->data++;
2364 --xfer->dataSize;
2365 ++handle->transferredCount;
2366 }
2367 }
2368 if (0U != (flags & (uint32_t)kLPI2C_SlaveRxReadyFlag))
2369 {
2370
2371 if ((NULL == xfer->data) || (0U == xfer->dataSize))
2372 {
2373 xfer->event = kLPI2C_SlaveReceiveEvent;
2374 if (NULL != handle->callback)
2375 {
2376 handle->callback(base, xfer, handle->userData);
2377 }
2378
2379
2380 handle->transferredCount = 0U;
2381 }
2382
2383
2384 if ((NULL != xfer->data) && (0U != xfer->dataSize))
2385 {
2386 *xfer->data++ = (uint8_t)base->SRDR;
2387 --xfer->dataSize;
2388 ++handle->transferredCount;
2389 }
2390 else
2391 {
2392
2393 base->STAR = LPI2C_STAR_TXNACK_MASK;
2394 }
2395 }
2396 }
2397 }
2398 }
2399
2400 #if !(defined(FSL_FEATURE_I2C_HAS_NO_IRQ) && FSL_FEATURE_I2C_HAS_NO_IRQ)
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411 static void LPI2C_CommonIRQHandler(LPI2C_Type *base, uint32_t instance)
2412 {
2413
2414 if ((0U != (base->MCR & LPI2C_MCR_MEN_MASK)) && (NULL != s_lpi2cMasterIsr))
2415 {
2416
2417 s_lpi2cMasterIsr(base, s_lpi2cMasterHandle[instance]);
2418 }
2419
2420
2421 if ((0U != (base->SCR & LPI2C_SCR_SEN_MASK)) && (NULL != s_lpi2cSlaveIsr))
2422 {
2423
2424 s_lpi2cSlaveIsr(base, s_lpi2cSlaveHandle[instance]);
2425 }
2426 SDK_ISR_EXIT_BARRIER;
2427 }
2428 #endif
2429
2430 #if defined(LPI2C0)
2431
2432 void LPI2C0_DriverIRQHandler(void);
2433 void LPI2C0_DriverIRQHandler(void)
2434 {
2435 LPI2C_CommonIRQHandler(LPI2C0, 0U);
2436 }
2437 #endif
2438
2439 #if defined(LPI2C1)
2440
2441 void LPI2C1_DriverIRQHandler(void);
2442 void LPI2C1_DriverIRQHandler(void)
2443 {
2444 LPI2C_CommonIRQHandler(LPI2C1, 1U);
2445 }
2446 #endif
2447
2448 #if defined(LPI2C2)
2449
2450 void LPI2C2_DriverIRQHandler(void);
2451 void LPI2C2_DriverIRQHandler(void)
2452 {
2453 LPI2C_CommonIRQHandler(LPI2C2, 2U);
2454 }
2455 #endif
2456
2457 #if defined(LPI2C3)
2458
2459 void LPI2C3_DriverIRQHandler(void);
2460 void LPI2C3_DriverIRQHandler(void)
2461 {
2462 LPI2C_CommonIRQHandler(LPI2C3, 3U);
2463 }
2464 #endif
2465
2466 #if defined(LPI2C4)
2467
2468 void LPI2C4_DriverIRQHandler(void);
2469 void LPI2C4_DriverIRQHandler(void)
2470 {
2471 LPI2C_CommonIRQHandler(LPI2C4, 4U);
2472 }
2473 #endif
2474
2475 #if defined(LPI2C5)
2476
2477 void LPI2C5_DriverIRQHandler(void);
2478 void LPI2C5_DriverIRQHandler(void)
2479 {
2480 LPI2C_CommonIRQHandler(LPI2C5, 5U);
2481 }
2482 #endif
2483
2484 #if defined(LPI2C6)
2485
2486 void LPI2C6_DriverIRQHandler(void);
2487 void LPI2C6_DriverIRQHandler(void)
2488 {
2489 LPI2C_CommonIRQHandler(LPI2C6, 6U);
2490 }
2491 #endif
2492
2493 #if defined(CM4_0__LPI2C)
2494
2495 void M4_0_LPI2C_DriverIRQHandler(void);
2496 void M4_0_LPI2C_DriverIRQHandler(void)
2497 {
2498 LPI2C_CommonIRQHandler(CM4_0__LPI2C, LPI2C_GetInstance(CM4_0__LPI2C));
2499 }
2500 #endif
2501
2502 #if defined(CM4__LPI2C)
2503
2504 void M4_LPI2C_DriverIRQHandler(void);
2505 void M4_LPI2C_DriverIRQHandler(void)
2506 {
2507 LPI2C_CommonIRQHandler(CM4__LPI2C, LPI2C_GetInstance(CM4__LPI2C));
2508 }
2509 #endif
2510
2511 #if defined(CM4_1__LPI2C)
2512
2513 void M4_1_LPI2C_DriverIRQHandler(void);
2514 void M4_1_LPI2C_DriverIRQHandler(void)
2515 {
2516 LPI2C_CommonIRQHandler(CM4_1__LPI2C, LPI2C_GetInstance(CM4_1__LPI2C));
2517 }
2518 #endif
2519
2520 #if defined(DMA__LPI2C0)
2521
2522 void DMA_I2C0_INT_DriverIRQHandler(void);
2523 void DMA_I2C0_INT_DriverIRQHandler(void)
2524 {
2525 LPI2C_CommonIRQHandler(DMA__LPI2C0, LPI2C_GetInstance(DMA__LPI2C0));
2526 }
2527 #endif
2528
2529 #if defined(DMA__LPI2C1)
2530
2531 void DMA_I2C1_INT_DriverIRQHandler(void);
2532 void DMA_I2C1_INT_DriverIRQHandler(void)
2533 {
2534 LPI2C_CommonIRQHandler(DMA__LPI2C1, LPI2C_GetInstance(DMA__LPI2C1));
2535 }
2536 #endif
2537
2538 #if defined(DMA__LPI2C2)
2539
2540 void DMA_I2C2_INT_DriverIRQHandler(void);
2541 void DMA_I2C2_INT_DriverIRQHandler(void)
2542 {
2543 LPI2C_CommonIRQHandler(DMA__LPI2C2, LPI2C_GetInstance(DMA__LPI2C2));
2544 }
2545 #endif
2546
2547 #if defined(DMA__LPI2C3)
2548
2549 void DMA_I2C3_INT_DriverIRQHandler(void);
2550 void DMA_I2C3_INT_DriverIRQHandler(void)
2551 {
2552 LPI2C_CommonIRQHandler(DMA__LPI2C3, LPI2C_GetInstance(DMA__LPI2C3));
2553 }
2554 #endif
2555
2556 #if defined(DMA__LPI2C4)
2557
2558 void DMA_I2C4_INT_DriverIRQHandler(void);
2559 void DMA_I2C4_INT_DriverIRQHandler(void)
2560 {
2561 LPI2C_CommonIRQHandler(DMA__LPI2C4, LPI2C_GetInstance(DMA__LPI2C4));
2562 }
2563 #endif
2564
2565 #if defined(ADMA__LPI2C0)
2566
2567 void ADMA_I2C0_INT_DriverIRQHandler(void);
2568 void ADMA_I2C0_INT_DriverIRQHandler(void)
2569 {
2570 LPI2C_CommonIRQHandler(ADMA__LPI2C0, LPI2C_GetInstance(ADMA__LPI2C0));
2571 }
2572 #endif
2573
2574 #if defined(ADMA__LPI2C1)
2575
2576 void ADMA_I2C1_INT_DriverIRQHandler(void);
2577 void ADMA_I2C1_INT_DriverIRQHandler(void)
2578 {
2579 LPI2C_CommonIRQHandler(ADMA__LPI2C1, LPI2C_GetInstance(ADMA__LPI2C1));
2580 }
2581 #endif
2582
2583 #if defined(ADMA__LPI2C2)
2584
2585 void ADMA_I2C2_INT_DriverIRQHandler(void);
2586 void ADMA_I2C2_INT_DriverIRQHandler(void)
2587 {
2588 LPI2C_CommonIRQHandler(ADMA__LPI2C2, LPI2C_GetInstance(ADMA__LPI2C2));
2589 }
2590 #endif
2591
2592 #if defined(ADMA__LPI2C3)
2593
2594 void ADMA_I2C3_INT_DriverIRQHandler(void);
2595 void ADMA_I2C3_INT_DriverIRQHandler(void)
2596 {
2597 LPI2C_CommonIRQHandler(ADMA__LPI2C3, LPI2C_GetInstance(ADMA__LPI2C3));
2598 }
2599 #endif
2600
2601 #if defined(ADMA__LPI2C4)
2602
2603 void ADMA_I2C4_INT_DriverIRQHandler(void);
2604 void ADMA_I2C4_INT_DriverIRQHandler(void)
2605 {
2606 LPI2C_CommonIRQHandler(ADMA__LPI2C4, LPI2C_GetInstance(ADMA__LPI2C4));
2607 }
2608 #endif