//----------------------------------------------------------------------------- /// \file SysVolt.c /// /// \brief Generic part of SysVolt implementation /// /// \descr /// //----------------------------------------------------------------------------- #define _SYS_VOLT_ // for inclusion of private headers //============================================================================= // Includes //============================================================================= #include "Platform_Types.h" #include "SysVoltPrjCfg.h" #include "Swc/SysVolt/Api/SysVolt_Rte.h" #include "Compiler.h" #include "Rte_ctasSysVolt.h" #include "SysVolt/SysVolt.h" #undef RTE_APPLICATION_HEADER_FILE #include "Swc/CodM/Api/CodM.h" //============================================================================= // Types //============================================================================= // a result set contains all variables necessary to build a valid result typedef struct { tieSysVoltGrade eVoltageRangeResult; tieSysVoltGrade ePreviousVoltageRange; uint16 unDebounceCnt; }tSysV_ResultSet; STATIC_AL uint8 ucInitStatus = 0; STATIC_AL tSysV_ResultSet SysV_Results[NumOfSysVoltModules]; tisSysVolt_Result eVoltagests_Result; tisCodingStatus eCodingDataStatus; CONST_AL tRomPara_Profiles *SysVoltCfgProfile_Point = &(applicationCodingData.SysVoltConfig.Profiles[0]); //============================================================================= // Functions //============================================================================= //----------------------------------------------------------------------------- /// \brief SysVolt_UpdateCoding /// /// \descr take the threshold from CodM. /// /// \param void /// /// \return void //----------------------------------------------------------------------------- void SysVolt_UpdateCoding(void) { uint8 ProFileIndex = 0u; uint8 VoltageLevel_Index = 0u; uint8 SysVolt_ProfileId = 0u; //Point to the start address of the configuration data for (ProFileIndex = 0u; ProFileIndex < SYSV_SUPPORT_PROFILE_MAX_NUM; ProFileIndex++) { if (SysVoltCfgProfile_Point->ProfileId != 0xFF) { SysVolt_ProfileId = (SysVoltCfgProfile_Point + ProFileIndex)->ProfileId; //Take the Worse Debounce time from the CodM module SysV_Configs[SysVolt_ProfileId].unDebounceCnt_RangeGetsWorse = SYSV_MS_TO_CNT((SysVoltCfgProfile_Point + ProFileIndex)->Worse_Debounce_ms); //Take the Better Debounce time from the CodM module SysV_Configs[SysVolt_ProfileId].unDebounceCnt_RangeGetsBetter = SYSV_MS_TO_CNT((SysVoltCfgProfile_Point + ProFileIndex)->Better_Debounce_ms); //Take the ADC input channel SysV_Configs[SysVolt_ProfileId].Channelidx = (SysVoltCfgProfile_Point + ProFileIndex)->InputChanel; SysV_Configs[SysVolt_ProfileId].ProfileId = (SysVoltCfgProfile_Point + ProFileIndex)->ProfileId; SysV_Configs[SysVolt_ProfileId].InputFilterType = (SysVoltCfgProfile_Point + ProFileIndex)->InputFilterType; SysV_Configs[SysVolt_ProfileId].Plausible_LowThreshold = (SysVoltCfgProfile_Point + ProFileIndex)->Plausible_LowThreshold; SysV_Configs[SysVolt_ProfileId].Plausible_UpThreshold = (SysVoltCfgProfile_Point + ProFileIndex)->Plausible_UpThreshold; //Take the different levels of voltage thresholds for (VoltageLevel_Index = 0; VoltageLevel_Index < SYSV_VOLTAGE_LEVELS_NUM; VoltageLevel_Index++) { if ((SysVoltCfgProfile_Point + ProFileIndex)->GradeThresholds_mV[VoltageLevel_Index] != 0) { SysV_Configs[SysVolt_ProfileId].pRangeCfg[VoltageLevel_Index].unVoltRangeUpperLimit_mV = (SysVoltCfgProfile_Point + ProFileIndex)->GradeThresholds_mV[VoltageLevel_Index]; } } } else { break; } } } /*static uint16 ulVoltage = 0;*/ uint8 SysVoltResult_Get(uint8 index,tisSysVolt_Result* voltget) { uint8 ReturnValue = RTE_E_INVALID; if((index < NumOfSysVoltModules)&&(voltget != NULL_PTR)) { voltget->usVoltage = (SysV_Configs[index].RteReadVoltage); voltget->eGrade = (tieVoltageGrade)((SysV_Configs[index].RteWritevoltstatus)); voltget->eSigQty = SysV_Configs[index].SysVoltQty; ReturnValue = RTE_E_OK; } else if(index >= NumOfSysVoltModules) { ReturnValue = RTE_E_OUT_OF_RANGE; } else if(voltget == NULL_PTR) { ReturnValue = RTE_E_INVALID; } else { //do nothing; } return ReturnValue; } STATIC_AL void SysVolt_Read_Voltage(void) { uint8 Rte_Qty = RTE_E_OK; uint8 ulConfigIdx; // index in the array of configured modules if(ucInitStatus == 0) { ucInitStatus = 1; } else { for (ulConfigIdx = 0; ulConfigIdx < NumOfSysVoltModules; ulConfigIdx++) { Rte_Qty = Rte_Call_PpaclIoHwAbUserVoltage_Get(SysV_Configs[ulConfigIdx].Channelidx, &SysV_Configs[ulConfigIdx].RteReadVoltage); if(Rte_Qty == RTE_E_OK) { SysV_Configs[ulConfigIdx].SysVoltQty = SIG_VALID; } else { SysV_Configs[ulConfigIdx].SysVoltQty = SIG_INVALID; } } } //SysVolt_Qty_Get();// the Qty processed } STATIC_AL void SysVolt_State (uint8 index , tieSysVoltGrade state) { (SysV_Configs[index].RteWritevoltstatus) = state; } /* static void SysVolt_Qty_Get(void)// the Qat processed { for (ulConfigIdx = 0; ulConfigIdx < NumOfSysVoltModules; ulConfigIdx++) { eVoltagests_Result.tieSigQty =*(SysV_Configs[ulConfigIdx].SysVoltQty); Rte_Call_PpasvSysVoltSysVoltResult_Get(ulConfigIdx,eVoltagests_Result); } } */ //----------------------------------------------------------------------------- /// \brief SysVolt_Init /// /// \descr Initialization, reading first time the ADC and setting the range levels /// to the value that is matching with the configured Level voltage /// values. /// \param void /// /// \return void //----------------------------------------------------------------------------- void SysVolt_Init(void) { uint8 ulConfigIdx; // index in the array of configured modules uint8 ulVoltRangeIdx; // index in the array of voltage ranges tieSysVoltGrade eInitialVoltageRange; // initial range, depending on the measured voltage uint16 unReadVoltage_mv; // current voltage value tieSysVoltGrade VoltRangeResult_Rte; // voltage range result in format of RTE eCodingDataStatus.eCodingStatus = 0xFF; Rte_Read_PpareCodMCodingStatus_sCodingStatus(&eCodingDataStatus);// Check coding status if(eCodingDataStatus.eCodingStatus == CODING_STATUS_VALID) { SysVolt_UpdateCoding(); } SysVolt_Read_Voltage(); // Initialize the level with the value matching to the actual voltage. for (ulConfigIdx = 0; ulConfigIdx < NumOfSysVoltModules; ulConfigIdx++) { // get voltage value via RTE /*(void)SysV_Configs[ulConfigIdx].RteReadVoltage(&unReadVoltage_mv);*/ unReadVoltage_mv = (SysV_Configs[ulConfigIdx].RteReadVoltage); // initialize with the highest voltage range eInitialVoltageRange = SysV_Configs[ulConfigIdx].pRangeCfg[SysV_Configs[ulConfigIdx].ucNumOfRanges-1].VoltRangeName; // check if we find a lower voltage range for (ulVoltRangeIdx = 0; ulVoltRangeIdx < SysV_Configs[ulConfigIdx].ucNumOfRanges; ulVoltRangeIdx++) { if ((unReadVoltage_mv < SysV_Configs[ulConfigIdx].pRangeCfg[ulVoltRangeIdx].unVoltRangeUpperLimit_mV) && (SysV_Configs[ulConfigIdx].pRangeCfg[ulVoltRangeIdx].unVoltRangeUpperLimit_mV != 0 )) { // matching voltage range found eInitialVoltageRange = SysV_Configs[ulConfigIdx].pRangeCfg[ulVoltRangeIdx].VoltRangeName; break; } } // end of loop over voltage ranges // init all values of the configuration SysV_Results[ulConfigIdx].ePreviousVoltageRange = eInitialVoltageRange; SysV_Results[ulConfigIdx].eVoltageRangeResult = eInitialVoltageRange; SysV_Results[ulConfigIdx].unDebounceCnt = SYSV_DEBOUNCE_CNT_ELAPSED; // debounce count set to elapsed // inform about the initial voltage range if ((SysVolt_eLow3 <= SysV_Configs[ulConfigIdx].RteWritevoltstatus) &&(SysV_Configs[ulConfigIdx].RteWritevoltstatus < NumOfSysVoltGrades)) { // convert to RTE format VoltRangeResult_Rte = /*(uint8)*/eInitialVoltageRange; // write to RTE /*(void)SysV_Configs[ulConfigIdx].RteWritevoltstatus(&VoltRangeResult_Rte);*/ SysVolt_State( ulConfigIdx , VoltRangeResult_Rte ); } } // End of loop over configurations } //----------------------------------------------------------------------------- /// \brief SysVolt_Cycle10ms /// /// \descr Cyclic function, called as runnable every 10 ms /// handles all cyclic tasks of SysVolt /// /// \param void /// /// \return void //----------------------------------------------------------------------------- void SysVolt_Cycle10ms(void) { uint8 ulConfigIdx; // index in the array of configured modules uint8 ulVoltRangeIdx; // index in the array of voltage ranges uint16 unReadVoltage_mv; // voltage value read from ADC in millivolts boolean bIsDirectionUp; // TRUE if voltage range is increasing, FALSE if decreasing tieSysVoltGrade eNewVoltageRange; // the new voltage derived from unReadVoltage_mv tieSysVoltGrade VoltRangeResult_Rte; // voltage range result in format of RTE SysVolt_Read_Voltage (); for (ulConfigIdx = 0; ulConfigIdx < NumOfSysVoltModules; ulConfigIdx++) { // get voltage value via RTE //(void)SysV_Configs[ulConfigIdx].RteReadVoltage(&unReadVoltage_mv); unReadVoltage_mv = (SysV_Configs[ulConfigIdx].RteReadVoltage); // initialize with the highest voltage range eNewVoltageRange = SysV_Configs[ulConfigIdx].pRangeCfg[SysV_Configs[ulConfigIdx].ucNumOfRanges-1].VoltRangeName; // check if we find a lower voltage range for (ulVoltRangeIdx = 0; ulVoltRangeIdx < SysV_Configs[ulConfigIdx].ucNumOfRanges; ulVoltRangeIdx++) { if ((unReadVoltage_mv < SysV_Configs[ulConfigIdx].pRangeCfg[ulVoltRangeIdx].unVoltRangeUpperLimit_mV)&&(SysV_Configs[ulConfigIdx].pRangeCfg[ulVoltRangeIdx].unVoltRangeUpperLimit_mV != 0 )) { // matching voltage range found eNewVoltageRange = SysV_Configs[ulConfigIdx].pRangeCfg[ulVoltRangeIdx].VoltRangeName; break; } } // end of loop over voltage ranges // are we in the same voltage range like in last cycle? if (SysV_Results[ulConfigIdx].ePreviousVoltageRange == eNewVoltageRange) { // decrement debounce counter if still possible if (SysV_Results[ulConfigIdx].unDebounceCnt != SYSV_DEBOUNCE_CNT_ELAPSED) { SysV_Results[ulConfigIdx].unDebounceCnt--; } } else // voltage range changed { // check direction bIsDirectionUp = (boolean)(eNewVoltageRange > SysV_Results[ulConfigIdx].eVoltageRangeResult ? TRUE : FALSE); // reload debounce counter depending on direction // are we getting worse? if ( ((eNewVoltageRange > SysVolt_eNormal) && (bIsDirectionUp == TRUE )) || ((eNewVoltageRange < SysVolt_eNormal) && (bIsDirectionUp == FALSE )) ) { SysV_Results[ulConfigIdx].unDebounceCnt = SysV_Configs[ulConfigIdx].unDebounceCnt_RangeGetsWorse; } else { SysV_Results[ulConfigIdx].unDebounceCnt = SysV_Configs[ulConfigIdx].unDebounceCnt_RangeGetsBetter; } } // check for state change if ((SysV_Results[ulConfigIdx].unDebounceCnt == SYSV_DEBOUNCE_CNT_ELAPSED) && ((SysV_Results[ulConfigIdx].eVoltageRangeResult) != eNewVoltageRange)) { // debounce time elapsed and new state is different to old state // -> change state SysV_Results[ulConfigIdx].eVoltageRangeResult = eNewVoltageRange; // call range change indication callback if ((SysVolt_eLow3 <= SysV_Configs[ulConfigIdx].RteWritevoltstatus) && (SysV_Configs[ulConfigIdx].RteWritevoltstatus < NumOfSysVoltGrades)) { // convert to RTE format VoltRangeResult_Rte = eNewVoltageRange; // write to RTE /*(void)SysV_Configs[ulConfigIdx].RteWritevoltstatus(&VoltRangeResult_Rte);*/ SysVolt_State( ulConfigIdx , VoltRangeResult_Rte ); } } // save voltage range for next run SysV_Results[ulConfigIdx].ePreviousVoltageRange = eNewVoltageRange; }// End of loop over configurations } //----------------------------------------------------------------------------- /// \brief initialization runnable /// /// \descr Call init function Sysmon_Init() /// Calling once at start up. /// /// \param void /// /// \return void //----------------------------------------------------------------------------- void Rte_riSysVoltInit(void) { SysVolt_Init(); } //----------------------------------------------------------------------------- /// \brief cyclic runnable /// /// \descr Call cyclic function Sysmon_Cycle10ms() every 10ms /// /// \param void /// /// \return void //----------------------------------------------------------------------------- void Rte_rpSysVolt10ms(void) { SysVolt_Cycle10ms(); } /* EOF */