84 uint16_t bufferLength;
85 uint16_t currentBufferOffset;
88 #if defined(ADC_PAL_MPC574xP)
102 adc_group_state_t hwTrigGroupState[ADC_PAL_MAX_NUM_HW_GROUPS_EN];
103 adc_group_state_t swTrigGroupState;
104 uint32_t latestGroupIdx;
105 #if (defined(ADC_PAL_S32K1xx))
108 #if (defined(ADC_PAL_MPC574xC_G_R) || defined(ADC_PAL_MPC574xP))
109 uint32_t stateIdxMapping[ADC_PAL_TOTAL_NUM_GROUPS];
114 #if defined(ADC_PAL_S32K1xx)
116 #if (ADC_PAL_MAX_NUM_HW_GROUPS_EN != 1u)
117 #error "The current device supports maximum 1 HW triggered group enabled"
121 #define NUMBER_OF_ADC_PAL_INSTANCES ADC_INSTANCE_COUNT
123 #define ADC_PAL_PDB_CHAN (0u)
124 #define ADC_PAL_TRGMUX_IDX (0u)
125 #define ADC_PAL_MAX_CONVS_IN_GROUP (PDB_DLY_COUNT)
129 #if (ADC_INSTANCE_COUNT >= 1u)
132 #if (ADC_INSTANCE_COUNT >= 2u)
133 TRGMUX_TARGET_MODULE_PDB1_TRG_IN
140 static void ADC_ConfigGroup(
const uint32_t instance,
const uint32_t groupIdx,
const bool hwTriggerFlag);
141 static status_t ADC_StopGroupBlocking(
const uint32_t instance,
const uint32_t timeout);
143 #elif defined(ADC_PAL_MPC574xC_G_R)
147 #define NUMBER_OF_ADC_PAL_INSTANCES BCTU_INSTANCE_COUNT
149 #define ADC_PAL_MAX_CONVS_IN_GROUP_HW_TRIG (BCTU_LISTCHR__COUNT * 2u)
151 static status_t ADC_Init_MPC574xC_G_R(const uint32_t instIdx, const adc_config_t * const config);
152 static status_t ADC_ConfigHwTriggeredGroup(
const uint32_t instIdx,
const adc_group_config_t *
const groupConfig,
const uint8_t bctuStartListIdx);
153 static uint32_t ADC_GetGroupIdx(
const uint32_t instIdx,
const adc_trigger_source_t hwTrigSource);
155 #elif defined(ADC_PAL_MPC574xP)
159 #define NUMBER_OF_ADC_PAL_INSTANCES CTU_INSTANCE_COUNT
162 static CTU_Type *
const ctuBase[CTU_INSTANCE_COUNT] = CTU_BASE_PTRS;
170 #if (defined(ADC_PAL_MPC574xC_G_R) || defined(ADC_PAL_MPC574xP))
174 #if ADC_INSTANCE_COUNT > 1u
177 #if ADC_INSTANCE_COUNT > 2u
180 #if ADC_INSTANCE_COUNT > 3u
185 static inline uint8_t ADC_GetChanIdx(
const adc_input_chan_t adcPalInputChan);
186 static inline uint8_t ADC_GetAdcAbsoluteIdx(
const adc_input_chan_t adcPalInputChan);
187 static uint16_t ADC_GetUsedAdcInstances(
const uint32_t instance);
188 static void ADC_ConfigAdcMPC574x(
const uint32_t instIdx,
const adc_config_t *
const config);
189 static void ADC_ResetAdcMPC574x(
const uint32_t instIdx);
190 #if defined (CUSTOM_DEVASSERT) || defined (DEV_ERROR_DETECT)
191 static void ADC_CheckChanDuplicate(
const adc_config_t *
const config);
197 #if (defined(ADC_PAL_S32K1xx) || defined(ADC_PAL_MPC574xC_G_R) || defined(ADC_PAL_MPC574xP))
199 static inline adc_group_state_t * ADC_GetHwGroupStatePtr(
const uint32_t instIdx,
const uint32_t groupIdx);
200 static bool ADC_AnyHwTrigGroupActive(
const uint32_t instIdx);
201 static void ADC_PalStateUpdateStart(
const uint32_t instIdx,
const uint32_t groupIdx);
202 static void ADC_CallNotificationCb(
const adc_pal_state_t *
const palState,
const uint32_t groupIdx,
const adc_group_state_t *
const groupState);
235 uint32_t instIdx = instance->
instIdx;
236 DEV_ASSERT(instIdx < NUMBER_OF_ADC_PAL_INSTANCES);
238 adc_pal_state_t *
const palState = &(
adcPalState[instIdx]);
245 for(idx = 0; idx < ADC_PAL_MAX_NUM_HW_GROUPS_EN; idx++)
247 palState->hwTrigGroupState[idx].active =
false;
248 palState->hwTrigGroupState[idx].bufferLength = 0u;
249 palState->hwTrigGroupState[idx].currentBufferOffset = 0u;
250 palState->hwTrigGroupState[idx].notificationEn =
false;
252 palState->swTrigGroupState.active =
false;
253 palState->swTrigGroupState.bufferLength = 0u;
254 palState->swTrigGroupState.currentBufferOffset = 0u;
255 palState->swTrigGroupState.notificationEn =
false;
256 palState->latestGroupIdx = 0u;
258 #if defined(ADC_PAL_S32K1xx)
261 const extension_adc_s32k1xx_t *
const extension = (extension_adc_s32k1xx_t *)(config->
extension);
262 palState->pdbPrescaler = extension->pdbPrescaler;
264 status = ADC_Init_S32K1xx(instIdx, config);
266 #elif (defined(ADC_PAL_MPC574xC_G_R) || defined(ADC_PAL_MPC574xP))
269 uint8_t hwTrigGroupIdx = 0;
270 for(idx = 0; idx < palState->numGroups; idx++)
272 if(palState->groupArray[idx].hwTriggerSupport ==
true)
275 palState->stateIdxMapping[idx] = hwTrigGroupIdx;
282 palState->stateIdxMapping[idx] = 0u;
286 #if defined(ADC_PAL_MPC574xC_G_R)
288 status = ADC_Init_MPC574xC_G_R(instIdx, config);
292 status = ADC_Init_MPC574xP(instIdx, config);
314 uint32_t instIdx = instance->
instIdx;
315 DEV_ASSERT(instIdx < NUMBER_OF_ADC_PAL_INSTANCES);
317 adc_pal_state_t *
const palState = &(
adcPalState[instIdx]);
318 bool hwTrigGroupActive =
false;
322 hwTrigGroupActive = ADC_AnyHwTrigGroupActive(instIdx);
324 if((palState->swTrigGroupState.active ==
true) || (hwTrigGroupActive ==
true))
330 #if defined(ADC_PAL_S32K1xx)
348 #elif defined(ADC_PAL_MPC574xC_G_R)
351 for(idx = 0; idx < palState->numGroups; idx++)
353 palState->stateIdxMapping[idx] = 0u;
358 bctuStatus = BCTU_DRV_Reset(instIdx, 2u);
365 ADC_ResetAdcMPC574x(instIdx);
370 #elif defined(ADC_PAL_MPC574xP)
373 for(idx = 0; idx < palState->numGroups; idx++)
375 palState->stateIdxMapping[idx] = 0u;
378 CTU_DRV_Reset(instIdx);
380 ADC_ResetAdcMPC574x(instIdx);
387 palState->groupArray = NULL;
388 palState->numGroups = 0u;
389 for(idx = 0; idx < ADC_PAL_MAX_NUM_HW_GROUPS_EN; idx++)
391 palState->hwTrigGroupState[idx].active =
false;
393 palState->swTrigGroupState.active =
false;
394 palState->latestGroupIdx = 0u;
412 uint32_t instIdx = instance->
instIdx;
413 DEV_ASSERT(instIdx < NUMBER_OF_ADC_PAL_INSTANCES);
417 const adc_pal_state_t *
const palState = &(
adcPalState[instIdx]);
419 const adc_group_state_t *
const currentGroupState = ADC_GetHwGroupStatePtr(instIdx, groupIdx);
425 (void) currentGroupCfg;
427 if((palState->swTrigGroupState.active ==
true) || (currentGroupState->active ==
true))
435 ADC_PalStateUpdateStart(instIdx, groupIdx);
437 #if defined(ADC_PAL_S32K1xx)
445 ADC_ConfigGroup(instIdx, groupIdx,
true);
452 #elif defined(ADC_PAL_MPC574xC_G_R)
453 const uint8_t numChans = (palState->groupArray[groupIdx]).numChannels;
465 BCTU_DRV_SetLowPowerMode(instIdx,
false);
469 BCTU_DRV_EnableHwTrigger(instIdx, currentGroupCfg->
triggerSource);
471 #elif defined(ADC_PAL_MPC574xP)
473 const uint32_t internalTrigIdx = palState->stateIdxMapping[groupIdx];
474 const uint32_t shift = internalTrigIdx * CTU_THCR1_T1_ADCE_SHIFT;
475 CTU_Type *
const ctuInstBase = ctuBase[instIdx];
478 ctuInstBase->THCR1 |= (uint32_t)CTU_OUTPUT_TRIG_EN << shift;
481 CTU_DRV_EnableGeneralReload(instIdx);
504 uint32_t instIdx = instance->
instIdx;
505 adc_group_state_t *
const currentGroupState = ADC_GetHwGroupStatePtr(instIdx, groupIdx);
507 DEV_ASSERT(instIdx < NUMBER_OF_ADC_PAL_INSTANCES);
517 if((currentGroupState->active ==
true) && (status ==
STATUS_SUCCESS))
519 #if defined(ADC_PAL_S32K1xx)
524 status = ADC_StopGroupBlocking(instIdx, timeout);
526 #elif defined(ADC_PAL_MPC574xC_G_R)
527 const adc_pal_state_t *
const palState = &(
adcPalState[instIdx]);
528 const adc_trigger_source_t triggerSource = palState->groupArray[groupIdx].triggerSource;
529 const uint8_t numChans = palState->groupArray[groupIdx].numChannels;
530 uint32_t startTime, deltaTime;
547 BCTU_DRV_DisableHwTrigger(instIdx, triggerSource);
550 while((BCTU_DRV_IsConvRunning(instIdx, triggerSource) ==
true) && (deltaTime < timeout))
555 if(deltaTime >= timeout)
564 BCTU_DRV_SetLowPowerMode(instIdx,
true);
567 #elif defined(ADC_PAL_MPC574xP)
568 const adc_pal_state_t *
const palState = &(
adcPalState[instIdx]);
570 const uint32_t internalTrigIdx = palState->stateIdxMapping[groupIdx];
571 const uint32_t shift = internalTrigIdx * CTU_THCR1_T1_ADCE_SHIFT;
572 CTU_Type *
const ctuInstBase = ctuBase[instIdx];
579 ctuInstBase->THCR1 &= ~((uint32_t)CTU_OUTPUT_TRIG_EN << shift);
582 CTU_DRV_EnableGeneralReload(instIdx);
584 currentGroupState->active =
false;
588 #ifndef ADC_PAL_MPC574xP
591 currentGroupState->active =
false;
611 uint32_t instIdx = instance->
instIdx;
612 DEV_ASSERT(instIdx < NUMBER_OF_ADC_PAL_INSTANCES);
614 const adc_pal_state_t *
const palState = &(
adcPalState[instIdx]);
617 DEV_ASSERT(palState->groupArray[groupIdx].hwTriggerSupport ==
false);
621 if((ADC_AnyHwTrigGroupActive(instIdx) ==
true) || (palState->swTrigGroupState.active ==
true))
629 ADC_PalStateUpdateStart(instIdx, groupIdx);
631 #if defined(ADC_PAL_S32K1xx)
632 DEV_ASSERT(palState->groupArray[groupIdx].numChannels <= ADC_PAL_MAX_CONVS_IN_GROUP);
634 bool hwTriggerEnabled =
false;
641 ADC_ConfigGroup(instIdx, groupIdx, hwTriggerEnabled);
646 #elif (defined(ADC_PAL_MPC574xC_G_R) || defined(ADC_PAL_MPC574xP))
650 uint32_t currentChan;
655 #if defined(ADC_PAL_MPC574xP)
656 adcBase[adcIdx]->MCR &= ~(ADC_MCR_CTUEN(1u));
661 for(idx = 0u; idx < currentGroupCfg->
numChannels; idx++)
666 ADC_DRV_EnableChannel(adcIdx, ADC_CONV_CHAIN_NORMAL, currentChan);
669 ADC_DRV_StartConversion(adcIdx, ADC_CONV_CHAIN_NORMAL);
690 uint32_t instIdx = instance->
instIdx;
691 DEV_ASSERT(instIdx < NUMBER_OF_ADC_PAL_INSTANCES);
698 if(
adcPalState[instIdx].swTrigGroupState.active ==
true)
700 #if defined(ADC_PAL_S32K1xx)
708 status = ADC_StopGroupBlocking(instIdx, timeout);
710 #elif (defined(ADC_PAL_MPC574xC_G_R) || defined(ADC_PAL_MPC574xP))
714 uint32_t currentChan;
715 uint32_t startTime, deltaTime;
728 for(idx = 0u; idx < currentGroupCfg->
numChannels; idx++)
732 ADC_DRV_DisableChannel(adcIdx, ADC_CONV_CHAIN_NORMAL, currentChan);
736 while(((ADC_DRV_GetStatusFlags(adcIdx) & ADC_FLAG_NORMAL_STARTED) != 0u) && (deltaTime < timeout))
743 ADC_DRV_ClearStatusFlags(adcIdx, ADC_FLAG_NORMAL_ENDCHAIN);
745 ADC_Type *
const base = s_adcBase[instIdx];
746 for(idx = 0u; idx < ADC_CEOCFR_COUNT; idx++)
748 base->CEOCFR[idx] |= 0xFFFFFFFFu;
751 if(deltaTime >= timeout)
757 #if defined(ADC_PAL_MPC574xP)
758 adcBase[adcIdx]->MCR |= ADC_MCR_CTUEN(1u);
767 adcPalState[instIdx].swTrigGroupState.active =
false;
790 uint32_t instIdx = instance->
instIdx;
791 DEV_ASSERT(instIdx < NUMBER_OF_ADC_PAL_INSTANCES);
793 adc_pal_state_t *
const palState = &(
adcPalState[instIdx]);
795 adc_group_state_t * groupState;
803 groupState = ADC_GetHwGroupStatePtr(instIdx, groupIdx);
807 groupState = &(palState->swTrigGroupState);
810 if(groupState->active ==
false)
816 #if (defined(ADC_PAL_S32K1xx) || defined(ADC_PAL_MPC574xC_G_R) || defined(ADC_PAL_MPC574xP))
818 DEV_ASSERT(palState->groupArray[groupIdx].callback != NULL);
820 groupState->notificationEn =
true;
839 uint32_t instIdx = instance->
instIdx;
840 DEV_ASSERT(instIdx < NUMBER_OF_ADC_PAL_INSTANCES);
842 adc_pal_state_t *
const palState = &(
adcPalState[instIdx]);
844 adc_group_state_t * groupState;
852 groupState = ADC_GetHwGroupStatePtr(instIdx, groupIdx);
856 groupState = &(palState->swTrigGroupState);
859 if(groupState->active ==
false)
865 #if (defined(ADC_PAL_S32K1xx) || defined(ADC_PAL_MPC574xC_G_R) || defined(ADC_PAL_MPC574xP))
867 groupState->notificationEn =
false;
880 #if defined(ADC_PAL_S32K1xx)
885 const extension_adc_s32k1xx_t *
const extension = (extension_adc_s32k1xx_t *)(config->
extension);
891 #if defined (CUSTOM_DEVASSERT) || defined (DEV_ERROR_DETECT)
898 for(idx = 0u; idx <
adcPalState[instance].numGroups; idx++)
900 currentGroupCfg = &(
adcPalState[instance].groupArray[idx]);
910 for(idx0 = 0u; idx0 < currentGroupCfg->
numChannels; idx0++)
912 for(idx1 = idx0 + 1u; idx1 < currentGroupCfg->
numChannels; idx1++)
961 void ADC_S32K1xx_IrqHandler(
const uint32_t instIdx)
963 adc_pal_state_t *
const palState = &(
adcPalState[instIdx]);
964 const uint32_t currentGroupIdx = palState->latestGroupIdx;
965 uint8_t controlChanIdx = 0u;
967 adc_group_state_t * groupState;
969 activeGroupCfg = &(palState->groupArray[currentGroupIdx]);
973 groupState = &(palState->swTrigGroupState);
977 groupState = &(palState->hwTrigGroupState[0u]);
980 uint16_t * result = &(activeGroupCfg->
resultBuffer[groupState->currentBufferOffset]);
983 for(controlChanIdx = 0u; controlChanIdx < activeGroupCfg->
numChannels; controlChanIdx++)
989 groupState->currentBufferOffset = (uint16_t)((groupState->currentBufferOffset + activeGroupCfg->
numChannels) % groupState->bufferLength);
1001 groupState->active =
false;
1006 ADC_CallNotificationCb(palState, currentGroupIdx, groupState);
1044 for(idx = 1u; idx < currentGroupCfg->
numChannels; idx++)
1065 for(idx = 1u; idx < currentGroupCfg->
numChannels; idx++)
1079 for(idx = 0u; idx < currentGroupCfg->
numChannels; idx++)
1099 static void ADC_ConfigGroup(
const uint32_t instance,
const uint32_t groupIdx,
const bool hwTriggerFlag)
1104 if(hwTriggerFlag ==
true)
1116 ADC_ConfigPdbAndPretriggers(instance, pdbTrigSrc, currentGroupCfg);
1123 for(idx = 0u; idx < (currentGroupCfg->
numChannels - 1u); idx++)
1140 static status_t ADC_StopGroupBlocking(
const uint32_t instance,
const uint32_t timeout)
1142 const adc_pal_state_t *
const palState = &(
adcPalState[instance]);
1143 uint32_t startTime, deltaTime;
1158 for(idx = 0u; idx < palState->groupArray[palState->latestGroupIdx].numChannels; idx++)
1168 const ADC_Type *
const base = adcBase[instance];
1169 while((ADC_GetConvActiveFlag(base) ==
true) && (deltaTime < timeout))
1174 if(deltaTime >= timeout)
1186 #elif defined(ADC_PAL_MPC574xC_G_R)
1190 static status_t ADC_Init_MPC574xC_G_R(
const uint32_t instIdx,
const adc_config_t *
const config)
1193 bctu_config_t bctuCfg;
1197 ADC_ConfigAdcMPC574x(instIdx, config);
1200 status = BCTU_DRV_Reset(instIdx, 2u);
1207 BCTU_DRV_GetDefaultConfig(&bctuCfg);
1208 bctuCfg.lowPowerModeEn =
true;
1210 bctuCfg.newDataIntEnMask = (((uint8_t)1u << FEATURE_BCTU_NUM_ADC) - 1u);
1211 bctuCfg.listIntEn =
true;
1213 BCTU_DRV_Config(instIdx, &bctuCfg);
1215 uint8_t bctuNumChansList = 0u;
1219 for(idx = 0u; idx < config->
numGroups; idx++)
1226 status = ADC_ConfigHwTriggeredGroup(instIdx, currentGroup, bctuNumChansList);
1235 #if defined (CUSTOM_DEVASSERT) || defined (DEV_ERROR_DETECT)
1237 ADC_CheckChanDuplicate(config);
1242 BCTU_DRV_WriteGlobalTriggerEn(instIdx,
true);
1256 void ADC_MPC574xC_G_R_HwTrigIrqHandler(
const uint32_t instIdx,
const uint8_t adcIdx)
1258 const adc_pal_state_t *
const palState = &(
adcPalState[instIdx]);
1260 adc_group_state_t * groupState;
1261 uint32_t currentGroupIdx;
1262 bctu_conv_result_t bctuResult;
1266 BCTU_DRV_GetConvResult(instIdx, adcIdx, &bctuResult);
1268 currentGroupIdx = ADC_GetGroupIdx(instIdx, bctuResult.triggerIdx);
1270 groupState = ADC_GetHwGroupStatePtr(instIdx, currentGroupIdx);
1272 activeGroupCfg = &(palState->groupArray[currentGroupIdx]);
1274 result = &(activeGroupCfg->
resultBuffer[groupState->currentBufferOffset]);
1278 *result = bctuResult.adcData;
1281 groupState->currentBufferOffset = (groupState->currentBufferOffset + 1u) % groupState->bufferLength;
1283 BCTU_DRV_ClearStatusFlag(instIdx, adcIdx, BCTU_FLAG_NEW_DATA_AVAILABLE);
1284 DEV_ASSERT(BCTU_DRV_GetStatusFlag(instIdx, adcIdx, BCTU_FLAG_DATA_OVERRUN) ==
false);
1289 uint32_t numResRead = 0u;
1291 numResRead = ADC_DRV_GetConvResultsToArray(adcIdx, ADC_CONV_CHAIN_CTU, result, activeGroupCfg->
numChannels);
1295 groupState->currentBufferOffset = (groupState->currentBufferOffset + activeGroupCfg->
numChannels) % groupState->bufferLength;
1297 BCTU_DRV_ClearStatusFlag(instIdx, adcIdx, BCTU_FLAG_LIST_LAST_CONV);
1299 BCTU_DRV_ClearStatusFlag(instIdx, adcIdx, BCTU_FLAG_NEW_DATA_AVAILABLE);
1301 BCTU_DRV_ClearStatusFlag(instIdx, adcIdx, BCTU_FLAG_DATA_OVERRUN);
1305 ADC_CallNotificationCb(palState, currentGroupIdx, groupState);
1315 static status_t ADC_ConfigHwTriggeredGroup(
const uint32_t instIdx,
const adc_group_config_t *
const groupConfig,
const uint8_t bctuStartListIdx)
1318 bctu_trig_config_t bctuTrigConfig;
1321 const uint8_t numChans = groupConfig->
numChannels;
1327 bctuTrigConfig.adcTargetMask = (uint8_t)1u << (ADC_GetAdcAbsoluteIdx(inputChans[0]) % FEATURE_BCTU_NUM_ADC);
1331 #if defined (CUSTOM_DEVASSERT) || defined (DEV_ERROR_DETECT)
1334 static const BCTU_Type *
const bctuBase[BCTU_INSTANCE_COUNT] = BCTU_BASE_PTRS;
1335 const BCTU_Type * bctu;
1336 bctu = bctuBase[instIdx];
1347 bctuStatus = BCTU_DRV_ConfigTriggerSingle(instIdx, groupConfig->
triggerSource, &bctuTrigConfig, ADC_GetChanIdx(inputChans[0]));
1354 uint8_t adcChanIdx[ADC_PAL_MAX_CONVS_IN_GROUP_HW_TRIG];
1356 for(idx = 0u; idx < numChans; idx++)
1358 adcChanIdx[idx] = ADC_GetChanIdx(inputChans[idx]);
1360 #if defined (CUSTOM_DEVASSERT) || defined (DEV_ERROR_DETECT)
1361 const uint8_t firstChanIdx = ADC_GetAdcAbsoluteIdx(inputChans[0u]);
1363 DEV_ASSERT(ADC_GetAdcAbsoluteIdx(inputChans[idx]) == firstChanIdx);
1369 if((bctuStartListIdx + numChans) >= (uint8_t)ADC_PAL_MAX_CONVS_IN_GROUP_HW_TRIG)
1375 bctuStatus = BCTU_DRV_ConfigTriggerList(instIdx, groupConfig->
triggerSource, &bctuTrigConfig, bctuStartListIdx);
1378 BCTU_DRV_SetConvListArray(instIdx, bctuStartListIdx, adcChanIdx, numChans);
1392 static uint32_t ADC_GetGroupIdx(
const uint32_t instIdx,
const adc_trigger_source_t hwTrigSource)
1394 uint32_t groupIdx = 0u;
1396 const adc_pal_state_t *
const palState = &(
adcPalState[instIdx]);
1398 while((found ==
false) && (groupIdx < palState->numGroups))
1400 if((palState->groupArray[groupIdx].triggerSource == hwTrigSource) \
1401 && (palState->groupArray[groupIdx].hwTriggerSupport ==
true))
1414 #elif defined(ADC_PAL_MPC574xP)
1419 const extension_adc_mpc574xp_t *
const extension = (extension_adc_mpc574xp_t *)(config->
extension);
1421 ctu_config_t ctuCfg;
1425 static const IRQn_Type ctuFirstFifoIrqNums[CTU_INSTANCE_COUNT] = {
1427 #if CTU_INSTANCE_COUNT > 1u
1432 ADC_ConfigAdcMPC574x(instIdx, config);
1435 CTU_DRV_Reset(instIdx);
1437 uint8_t ctuNumChansList = 0u;
1438 uint8_t internalTrigIdx = 0u;
1439 uint8_t fifoIdx = 0u;
1440 uint16_t delay = 0u;
1441 ctu_trig_config_t trigConfig;
1442 ctu_res_fifo_config_t resFifoConfig;
1443 uint32_t inputTriggerSelectMask = 0u;
1447 for(idx = 0u; idx < config->
numGroups; idx++)
1454 if(internalTrigIdx >= CTU_FR_COUNT)
1463 #if defined (CUSTOM_DEVASSERT) || defined (DEV_ERROR_DETECT)
1466 static const uint8_t resultFifoLengths[CTU_FR_COUNT] = { 16u, 16u, 4u, 4u };
1488 trigConfig.cmdListStartAdr = ctuNumChansList;
1489 trigConfig.compareVal = delay;
1490 trigConfig.intEn =
false;
1491 trigConfig.outputTrigEnMask = CTU_OUTPUT_TRIG_ADC_CMD_EN;
1492 CTU_DRV_ConfigInternalTrigger(instIdx, internalTrigIdx, &trigConfig);
1497 fifoIdx = internalTrigIdx;
1498 resFifoConfig.dmaEn =
false;
1499 resFifoConfig.fifoIntEnMask = CTU_FIFO_OVERFLOW;
1500 resFifoConfig.fifoThreshold = (currentGroup->
numChannels - 1u);
1501 CTU_DRV_ConfigResultFifo(instIdx, fifoIdx, &resFifoConfig);
1504 const uint16_t fifoIrqNumU = ((uint16_t)ctuFirstFifoIrqNums[instIdx] + fifoIdx);
1510 uint8_t pos, adcPort;
1511 ctu_adc_cmd_t adcCmd;
1512 adcCmd.convMode = CTU_CONV_MODE_SINGLE;
1513 adcCmd.fifoIdx = fifoIdx;
1514 adcCmd.intEn =
false;
1515 adcCmd.selfTestEn =
false;
1518 adcPort = ADC_GetAdcAbsoluteIdx(currentGroup->
inputChannelArray[idx1]) % FEATURE_CTU_NUM_ADC;
1521 case 0u: adcCmd.adcPort = CTU_ADC_PORT_A;
break;
1522 case 1u: adcCmd.adcPort = CTU_ADC_PORT_B;
break;
1526 pos = trigConfig.cmdListStartAdr;
1527 CTU_DRV_ConfigAdcCmdList(instIdx, pos, &adcCmd, 1u);
1528 for(idx1 = 1u; idx1 < currentGroup->
numChannels; idx1++)
1530 CTU_Type *
const ctuInstBase = ctuBase[instIdx];
1538 pos = (trigConfig.cmdListStartAdr + idx1) % CTU_CHANNEL_COUNT;
1539 CTU_DRV_ConfigAdcCmdList(instIdx, pos, &adcCmd, 1u);
1543 ctuInstBase->CHANNEL[pos].CLR_A &= (~CTU_CLR_A_LC_MASK);
1550 CTU_DRV_GetDefaultConfig(&ctuCfg);
1551 ctuCfg.prescaler = extension->ctuPrescaler;
1552 ctuCfg.tgsMode = CTU_TGS_MODE_TRIGGERED;
1553 ctuCfg.inputTrigSelectMask = inputTriggerSelectMask;
1554 ctuCfg.tgsCounterCompareVal = CTU_TGSCCR_TGSCCV_MASK;
1555 ctuCfg.tgsCounterReloadVal = 0u;
1557 CTU_DRV_Config(instIdx, &ctuCfg);
1560 CTU_DRV_EnableGeneralReload(instIdx);
1563 CTU_DRV_SwMasterReloadSignal(instIdx);
1565 #if defined (CUSTOM_DEVASSERT) || defined (DEV_ERROR_DETECT)
1567 ADC_CheckChanDuplicate(config);
1581 void ADC_MPC574xP_HwTrigIrqHandler(
const uint32_t instIdx,
const uint8_t fifoIdx)
1583 adc_pal_state_t *
const palState = &(
adcPalState[instIdx]);
1585 adc_group_state_t * groupState;
1586 uint32_t currentGroupIdx;
1587 ctu_conv_result_t ctuResult;
1590 DEV_ASSERT(CTU_DRV_GetFifoStatusFlags(instIdx, fifoIdx) != CTU_FIFO_OVERRUN);
1592 CTU_DRV_GetConvResult(instIdx, fifoIdx, CTU_RESULT_ALIGN_RIGHT_UNSIGNED, &ctuResult);
1594 groupState = &(palState->hwTrigGroupState[fifoIdx]);
1596 currentGroupIdx = groupState->groupIdx;
1598 activeGroupCfg = &(palState->groupArray[currentGroupIdx]);
1600 result = &(activeGroupCfg->
resultBuffer[groupState->currentBufferOffset]);
1602 *result = ctuResult.convData;
1605 for(idx = 0u; idx < (activeGroupCfg->
numChannels - 1u); idx++)
1608 *result = CTU_DRV_GetConvData(instIdx, fifoIdx, CTU_RESULT_ALIGN_RIGHT_UNSIGNED);
1612 groupState->currentBufferOffset = (groupState->currentBufferOffset + activeGroupCfg->
numChannels) % groupState->bufferLength;
1615 ADC_CallNotificationCb(palState, currentGroupIdx, groupState);
1623 #if (defined(ADC_PAL_MPC574xC_G_R) || defined(ADC_PAL_MPC574xP))
1631 static inline uint8_t ADC_GetChanIdx(
const adc_input_chan_t adcPalInputChan)
1633 return (uint8_t)(adcPalInputChan & ADC_CHAN_CHAN_IDX_MASK);
1644 static inline uint8_t ADC_GetAdcAbsoluteIdx(
const adc_input_chan_t adcPalInputChan)
1646 return (uint8_t)((adcPalInputChan & ADC_CHAN_ADC_IDX_MASK) >> ADC_CHAN_ADC_IDX_OFFSET);
1658 static uint16_t ADC_GetUsedAdcInstances(
const uint32_t instance)
1660 const adc_pal_state_t *
const palState = &(
adcPalState[instance]);
1662 uint16_t usedAdcMask = 0u;
1665 for(idx = 0u; idx < palState->numGroups; idx++)
1667 currentGroup = &(palState->groupArray[idx]);
1670 usedAdcMask |= ((uint16_t)1u << ADC_GetAdcAbsoluteIdx(currentGroup->
inputChannelArray[0u]));
1684 void ADC_MPC574x_SwTrigIrqHandler(
const uint32_t instIdx,
const uint8_t adcIdx)
1686 adc_pal_state_t *
const palState = &(
adcPalState[instIdx]);
1687 adc_group_state_t *
const groupState = &(palState->swTrigGroupState);
1688 const uint32_t currentGroupIdx = palState->latestGroupIdx;
1689 const adc_group_config_t *
const activeGroupCfg = &(palState->groupArray[currentGroupIdx]);
1691 uint16_t * result = &(activeGroupCfg->
resultBuffer[groupState->currentBufferOffset]);
1696 uint32_t numResRead = 0u;
1698 numResRead = ADC_DRV_GetConvResultsToArray(adcIdx, ADC_CONV_CHAIN_NORMAL, result, activeGroupCfg->
numChannels);
1702 groupState->currentBufferOffset = (uint16_t)((groupState->currentBufferOffset + activeGroupCfg->
numChannels) % groupState->bufferLength);
1704 ADC_DRV_ClearStatusFlags(adcIdx, ADC_FLAG_NORMAL_ENDCHAIN);
1707 ADC_CallNotificationCb(palState, currentGroupIdx, groupState);
1711 ADC_DRV_StartConversion(adcIdx, ADC_CONV_CHAIN_NORMAL);
1715 #if defined(ADC_PAL_MPC574xP)
1716 adcBase[adcIdx]->MCR |= ADC_MCR_CTUEN(1u);
1719 groupState->active =
false;
1730 static void ADC_ConfigAdcMPC574x(
const uint32_t instIdx,
const adc_config_t *
const config)
1732 #if defined(ADC_PAL_MPC574xC_G_R)
1733 const extension_adc_mpc574xc_g_r_t *
const extension = (extension_adc_mpc574xc_g_r_t *)(config->
extension);
1734 #if defined (CUSTOM_DEVASSERT) || defined (DEV_ERROR_DETECT)
1735 const uint8_t numAdcPerTrigUnit = FEATURE_BCTU_NUM_ADC;
1737 #elif defined(ADC_PAL_MPC574xP)
1738 const extension_adc_mpc574xp_t *
const extension = (extension_adc_mpc574xp_t *)(config->
extension);
1739 #if defined (CUSTOM_DEVASSERT) || defined (DEV_ERROR_DETECT)
1740 const uint8_t numAdcPerTrigUnit = FEATURE_CTU_NUM_ADC;
1743 adc_conv_config_t adcCfg;
1746 ADC_DRV_GetDefaultConfigConverter(&adcCfg);
1750 adcCfg.clkSelect = extension->clkSelect;
1751 adcCfg.refSelect = extension->refSelect;
1752 adcCfg.autoClockOff = extension->autoClockOff;
1753 adcCfg.convMode = ADC_CONV_MODE_ONESHOT;
1754 #if defined(ADC_PAL_MPC574xC_G_R)
1756 adcCfg.ctuMode = ADC_CTU_MODE_TRIGGER;
1757 #elif defined(ADC_PAL_MPC574xP)
1759 adcCfg.ctuMode = ADC_CTU_MODE_CONTROL;
1761 adcCfg.overwriteEnable =
true;
1765 uint16_t activeAdcMask = ADC_GetUsedAdcInstances(instIdx);
1770 if((activeAdcMask & ((uint16_t)1u << idx)) != 0u)
1774 DEV_ASSERT(idx >= (instIdx * numAdcPerTrigUnit));
1775 DEV_ASSERT(idx < ((instIdx + 1u) * numAdcPerTrigUnit));
1778 status = ADC_DRV_DoCalibration(idx);
1784 ADC_DRV_EnableInterrupts(idx, ADC_FLAG_NORMAL_ENDCHAIN);
1796 static void ADC_ResetAdcMPC574x(
const uint32_t instIdx)
1798 #if defined(ADC_PAL_MPC574xC_G_R)
1799 const uint8_t numAdcPerTrigUnit = FEATURE_BCTU_NUM_ADC;
1800 #elif defined(ADC_PAL_MPC574xP)
1801 const uint8_t numAdcPerTrigUnit = FEATURE_CTU_NUM_ADC;
1808 uint16_t activeAdcMask = ADC_GetUsedAdcInstances(instIdx);
1809 const uint16_t startIdx = instIdx * numAdcPerTrigUnit;
1810 const uint16_t endIdx = (instIdx + 1u) * numAdcPerTrigUnit;
1811 for(idx = startIdx; idx < endIdx; idx++)
1813 if((activeAdcMask & ((uint16_t)1u << idx)) != 0u)
1821 #if defined (CUSTOM_DEVASSERT) || defined (DEV_ERROR_DETECT)
1831 static void ADC_CheckChanDuplicate(
const adc_config_t *
const config)
1833 uint8_t numOccurrences[ADC_CDR_COUNT] = {0};
1838 for(idx = 0u; idx < config->
numGroups; idx++)
1842 #if defined(ADC_PAL_MPC574xP)
1850 for(idx1 = 0u; idx1 < currentGroup->
numChannels; idx1++)
1853 numOccurrences[chanIdx] = 0u;
1856 for(idx1 = 0u; idx1 < currentGroup->
numChannels; idx1++)
1859 numOccurrences[chanIdx]++;
1861 for(idx1 = 0u; idx1 < currentGroup->
numChannels; idx1++)
1864 if(numOccurrences[chanIdx] > 1u)
1877 #if (defined(ADC_PAL_S32K1xx) || defined(ADC_PAL_MPC574xC_G_R) || defined(ADC_PAL_MPC574xP))
1888 static inline adc_group_state_t * ADC_GetHwGroupStatePtr(
const uint32_t instIdx,
const uint32_t groupIdx)
1890 #if defined(ADC_PAL_S32K1xx)
1893 return &(
adcPalState[instIdx].hwTrigGroupState[0u]);
1895 #elif (defined(ADC_PAL_MPC574xC_G_R) || defined(ADC_PAL_MPC574xP))
1897 adc_pal_state_t *
const palState = &(
adcPalState[instIdx]);
1898 const uint32_t stateIdx = palState->stateIdxMapping[groupIdx];
1899 return &(palState->hwTrigGroupState[stateIdx]);
1913 static bool ADC_AnyHwTrigGroupActive(
const uint32_t instIdx)
1915 #if defined(ADC_PAL_S32K1xx)
1917 return adcPalState[instIdx].hwTrigGroupState[0u].active;
1919 #elif (defined(ADC_PAL_MPC574xC_G_R) || defined(ADC_PAL_MPC574xP))
1921 bool active =
false;
1924 while((active ==
false) && (idx < ADC_PAL_MAX_NUM_HW_GROUPS_EN))
1926 if(
adcPalState[instIdx].hwTrigGroupState[idx].active ==
true)
1944 static void ADC_PalStateUpdateStart(
const uint32_t instIdx,
const uint32_t groupIdx)
1946 adc_pal_state_t * palState = &(
adcPalState[instIdx]);
1947 const adc_group_config_t *
const currentGroupCfg = &(palState->groupArray[groupIdx]);
1948 adc_group_state_t * groupState;
1952 groupState = ADC_GetHwGroupStatePtr(instIdx, groupIdx);
1956 groupState = &(palState->swTrigGroupState);
1959 palState->latestGroupIdx = groupIdx;
1960 groupState->active =
true;
1961 groupState->currentBufferOffset = 0u;
1962 #if defined(ADC_PAL_MPC574xP)
1963 groupState->groupIdx = groupIdx;
1966 if(currentGroupCfg->
callback != NULL)
1968 groupState->notificationEn =
true;
1972 groupState->notificationEn =
false;
1984 static void ADC_CallNotificationCb(
const adc_pal_state_t *
const palState,
const uint32_t groupIdx,
const adc_group_state_t *
const groupState)
1989 if(groupState->notificationEn)
1994 if(groupState->currentBufferOffset == 0u)
2000 cbInfo.
resultBufferTail = (uint16_t)(groupState->currentBufferOffset - 1u);
void PDB_DRV_SetAdcPreTriggerDelayValue(const uint32_t instance, const uint32_t chn, const uint32_t preChn, const uint32_t value)
Sets the ADC pre_trigger delay value in the PDB module.
static ADC_Type *const s_adcBase[ADC_INSTANCE_COUNT]
status_t ADC_StopGroupConversion(const adc_instance_t *const instance, const uint32_t groupIdx, const uint32_t timeout)
Stops the selected SW triggered ADC conversion group execution.
const adc_group_config_t * groupConfigArray
void ADC_DRV_AutoCalibration(const uint32_t instance)
Executes an Auto-Calibration.
status_t TRGMUX_DRV_SetTrigSourceForTargetModule(const uint32_t instance, const trgmux_trigger_source_t triggerSource, const trgmux_target_module_t targetModule)
Configure a source trigger for a selected target module.
uint32_t adcPreTriggerIdx
pdb_trigger_src_t
Defines the type of trigger source mode for the PDB.
void OSIF_TimeDelay(const uint32_t delay)
Delays execution for a number of milliseconds.
adc_voltage_reference_t voltageRef
bool preTriggerOutputEnable
status_t ADC_DisableNotification(const adc_instance_t *const instance, const uint32_t groupIdx)
Disables the notification callback for a configured group.
void PDB_DRV_ConfigAdcPreTrigger(const uint32_t instance, const uint32_t chn, const pdb_adc_pretrigger_config_t *configPtr)
Configures the ADC pre_trigger in the PDB module.
bool supplyMonitoringEnable
void ADC_DRV_ConfigConverter(const uint32_t instance, const adc_converter_config_t *const config)
Configures the converter with the given configuration structure.
adc_input_clock_t inputClock
void ADC_DRV_Reset(const uint32_t instance)
Resets the converter (sets all configurations to reset values)
uint32_t OSIF_GetMilliseconds(void)
Returns the number of miliseconds elapsed since starting the internal timer or starting the scheduler...
adc_clk_divide_t clockDivide
void ADC_DRV_GetChanResult(const uint32_t instance, const uint8_t chanIndex, uint16_t *const result)
Gets the last result for the selected control channel.
Defines the configuration structure for ADC PAL.
const adc_input_chan_t * inputChannelArray
bool preTriggerBackToBackEnable
Defines the type of structure for basic timer in PDB.
void ADC_DRV_ConfigChan(const uint32_t instance, const uint8_t chanIndex, const adc_chan_config_t *const config)
Configures the selected control channel with the given configuration structure.
adc_trigger_source_t triggerSource
void INT_SYS_DisableIRQ(IRQn_Type irqNumber)
Disables an interrupt for a given IRQ number.
Defines the configuration structure for an ADC PAL conversion group.
adc_delay_type_t delayType
status_t ADC_StartGroupConversion(const adc_instance_t *const instance, const uint32_t groupIdx)
Starts the execution of a selected SW triggered ADC conversion group.
status_t ADC_EnableHardwareTrigger(const adc_instance_t *const instance, const uint32_t groupIdx)
Enables the selected HW trigger for a conversion group, if the conversion group has support for HW tr...
status_t ADC_Deinit(const adc_instance_t *const instance)
Deinitializes the ADC PAL instance.
void ADC_DRV_InitConverterStruct(adc_converter_config_t *const config)
Initializes the converter configuration structure.
static adc_pal_state_t adcPalState[NUMBER_OF_ADC_PAL_INSTANCES]
pdb_clk_prescaler_mult_factor_t clkPreMultFactor
status_t
Status return codes. Common error codes will be a unified enumeration (C enum) that will contain all ...
IRQn_Type
Defines the Interrupt Numbers definitions.
pdb_load_value_mode_t loadValueMode
void PDB_DRV_SoftTriggerCmd(const uint32_t instance)
Triggers the PDB with a software trigger.
void PDB_DRV_LoadValuesCmd(const uint32_t instance)
Executes the command of loading values.
IRQn_Type ADC_DRV_GetInterruptNumber(const uint32_t instance)
Returns the interrupt number for the ADC instance.
void PDB_DRV_Deinit(const uint32_t instance)
De-initializes the PDB module.
Defines the converter configuration.
pdb_clk_prescaler_div_t clkPreDiv
#define ADC_INSTANCE_COUNT
pdb_trigger_src_t triggerInput
void PDB_DRV_Enable(const uint32_t instance)
Enables the PDB module.
void INT_SYS_EnableIRQ(IRQn_Type irqNumber)
Enables an interrupt for a given IRQ number.
status_t ADC_Init(const adc_instance_t *const instance, const adc_config_t *const config)
Initializes the ADC PAL instance.
status_t ADC_EnableNotification(const adc_instance_t *const instance, const uint32_t groupIdx)
Enables the notification callback for a configured group.
uint8_t numSetsResultBuffer
void PDB_DRV_Init(const uint32_t instance, const pdb_timer_config_t *userConfigPtr)
Initializes the PDB counter and triggers input.
pdb_clk_prescaler_div_t
Defines the type of prescaler divider for the PDB counter clock. Implements : pdb_clk_prescaler_div_t...
Defines the type of structure for configuring ADC's pre_trigger.
enum trgmux_target_module_e trgmux_target_module_t
Enumeration for target module of TRGMUX.
#define NUMBER_OF_ALT_CLOCKS
uint16_t resultBufferTail
status_t ADC_DisableHardwareTrigger(const adc_instance_t *const instance, const uint32_t groupIdx, const uint32_t timeout)
Disables the selected HW trigger for a conversion group, if the conversion group is HW triggered...
Structure storing PAL instance information.
adc_resolution_t resolution
adc_pretrigger_sel_t pretriggerSel
bool continuousModeEnable
adc_inputchannel_t channel
Defines the control channel configuration.