/*********************************************************************
* @brief : WDT Driver source file
* @note :
* @par history:
* Date Author Description
* 2023-08-22 Issta Creat File
*********************************************************************/
#include "MCAL_Wdt.h"
#include "IfxCpu.h"
#include "IfxScuWdt.h"
#include "IfxScuCcu.h"
static uint16 WDT_CalcReload(IfxScu_WDTCON1_IR Frequency, uint16 u16Period_ms);
/*********************************************************************
* @brief :
* @note : 16384: max 10737ms 256: max 168ms 64: max 42ms
* @param :
* @return :
*********************************************************************/
static uint16 WDT_CalcReload(IfxScu_WDTCON1_IR Frequency, uint16 u16Period_ms)
{
float32 FreqDiv = 0.0f;
uint16 Reload = 0U;
if(IfxScu_WDTCON1_IR_divBy16384 == Frequency)
{
FreqDiv = 16384.0f;
}
else if(IfxScu_WDTCON1_IR_divBy256 == Frequency)
{
FreqDiv = 256.0f;
}
else if(IfxScu_WDTCON1_IR_divBy64 == Frequency)
{
FreqDiv = 64.0f;
}
else
{
/*Do nothing! */
}
/* Note here below */
Reload = (uint16)(((IfxScuCcu_getSpbFrequency() / 1000.0f) * (float32)u16Period_ms) / FreqDiv);
Reload = 0xFFFF - Reload; /* period = ((2^16- startvalue ) * divider) / fSPB */
return Reload; /* 800: 0xECED 1000: 0xE828 1600: 0xD9DA 2000: 0xD050 */
}
/*********************************************************************
* @brief :
* @note :
* @param :
* @return :
*********************************************************************/
void SafetyWDT_Init(uint16 u16Period_ms)
{
IfxScuWdt_Config WdtConfig;
IfxScuWdt_initConfig(&WdtConfig);
WdtConfig.inputFrequency = IfxScu_WDTCON1_IR_divBy16384;
/* wdgConfig.password = 0x0BB2; */
WdtConfig.reload = WDT_CalcReload(WdtConfig.inputFrequency, u16Period_ms);
/*wdgConfig.clrInternalResetFlag = TRUE;*/ /* Enable Clear Internal Reset Flag */
IfxScuWdt_initSafetyWatchdog(&MODULE_SCU.WDTS, &WdtConfig);
}
/*********************************************************************
* @brief :
* @note :
* @param :
* @return :
*********************************************************************/
void CpuWDT_Init(uint16 u16Period_ms)
{
IfxScuWdt_Config WdtConfig;
IfxScuWdt_initConfig(&WdtConfig);
WdtConfig.inputFrequency = IfxScu_WDTCON1_IR_divBy16384;
Ifx_SCU_WDTCPU* cpu_wdt = &MODULE_SCU.WDTCPU[(unsigned int)IfxCpu_getCoreIndex()];
/* wdgConfig.password = 0x0AA1; */
WdtConfig.reload = WDT_CalcReload(WdtConfig.inputFrequency, u16Period_ms);
/* Initialize CPU Watchdog with parameters from IfxScuWdt_Config structure */
IfxScuWdt_initCpuWatchdog(cpu_wdt, &WdtConfig);
}
/*********************************************************************
* @brief :
* @note :
* @param :
* @return :
*********************************************************************/
void SafetyWDT_Enable(uint16 u16Period_ms)
{
IfxScuWdt_enableSafetyWatchdog(IfxScuWdt_getSafetyWatchdogPassword());
IfxScuWdt_changeSafetyWatchdogReload(IfxScuWdt_getSafetyWatchdogPassword(),\
WDT_CalcReload(IfxScu_WDTCON1_IR_divBy16384, u16Period_ms));
IfxScuWdt_serviceSafetyWatchdog(IfxScuWdt_getSafetyWatchdogPassword());
}
/*********************************************************************
* @brief :
* @note :
* @param :
* @return :
*********************************************************************/
void SafetyWDT_Disable(void)
{
IfxScuWdt_disableSafetyWatchdog(IfxScuWdt_getSafetyWatchdogPassword());
}
/*********************************************************************
* @brief :
* @note :
* @param :
* @return :
*********************************************************************/
void CpuWDT_Enable(uint16 u16Period_ms)
{
IfxScuWdt_enableCpuWatchdog(IfxScuWdt_getCpuWatchdogPassword());
IfxScuWdt_changeCpuWatchdogReload(IfxScuWdt_getCpuWatchdogPassword(),\
WDT_CalcReload(IfxScu_WDTCON1_IR_divBy16384, u16Period_ms));
IfxScuWdt_serviceCpuWatchdog(IfxScuWdt_getCpuWatchdogPassword());
}
/*********************************************************************
* @brief :
* @note :
* @param :
* @return :
*********************************************************************/
void CpuWDT_Disable(void)
{
IfxScuWdt_disableCpuWatchdog(IfxScuWdt_getCpuWatchdogPassword());
}
/*********************************************************************
* @brief :
* @note :
* @param :
* @return :
*********************************************************************/
void CpuWDT_Service(void)
{
IfxScuWdt_serviceCpuWatchdog(IfxScuWdt_getCpuWatchdogPassword()); /* Continuously service CPU0WDT */
}
/*********************************************************************
* @brief :
* @note :
* @param :
* @return :
*********************************************************************/
void SafetyWDT_Service(void)
{
IfxScuWdt_serviceSafetyWatchdog(IfxScuWdt_getSafetyWatchdogPassword());
}