cancel
Showing results for 
Search instead for 
Did you mean: 

[HELP]Getting Started Using WATCHDOG

dree86
Associate II
Posted on February 04, 2016 at 10:22

Hello,

I just learn about stm32 programming. I am using STM32L053 discovery board. Right now i learn about how implementing watchdog in my code. Here my code

int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
MX_GPIO_Init();
init_lcd_handler(); //in other file
watchdog_init();
lcd_msg_clr();
show_lcd_msg(0,0,''MP3_Module TEST!'');
show_lcd_msg(0,1,''STM32L053 Disco.'');
while (1)
{
lcd_routine();
HAL_WWDG_Refresh(&WwdgHandle, 127);
}
}
void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
/* Enable Power Control clock */
__PWR_CLK_ENABLE();
/* The voltage scaling allows optimizing the power consumption when the device is
clocked below the maximum system frequency, to update the voltage scaling value
regarding system frequency refer to product datasheet. */
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/* Enable HSI Oscillator and activate PLL with HSI as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
RCC_OscInitStruct.HSICalibrationValue = 0x10;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
clocks dividers */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
}
static void watchdog_init(void)
{
/*##-1- Check if the system has resumed from WWDG reset ####################*/
if(__HAL_RCC_GET_FLAG(RCC_FLAG_WWDGRST) != RESET)
/* Clear reset flags */
__HAL_RCC_CLEAR_RESET_FLAGS();
/*##-2- Configure the WWDG peripheral ######################################*/
/* WWDG clock counter = (PCLK1 (2MHz)/4096)/1) = 488 Hz (~2049 us)
WWDG Window value = 80 means that the WWDG counter should be refreshed only
when the counter is below 80 (and greater than 64) otherwise a reset will
be generated.
WWDG Counter value = 127, WWDG timeout = ~2049 us * 64 = 13 ms
In this case the refresh window is: ~2049 * (127-80) = 303 ms < refresh window < ~2049 * 64 = 13ms */
WwdgHandle.Instance = WWDG;
WwdgHandle.Init.Prescaler = WWDG_PRESCALER_1;
WwdgHandle.Init.Window = 100;
WwdgHandle.Init.Counter = 127;
if(HAL_WWDG_Init(&WwdgHandle) != HAL_OK)
{
/* Initialization Error */
//Error_Handler();
}
/*##-3- Start the WWDG #####################################################*/
if(HAL_WWDG_Start(&WwdgHandle) != HAL_OK)
{
//Error_Handler();
}
}

It seem my watchdog cannot be kicked and always reset the MCU. Always show blinky message MP3 Module TEST STM32L053 on lcd. Never go to lcd_routine(). How to set the proper value to initialize watchdog ? And I still confused about system clock configuration, its so many of it. Thank you
2 REPLIES 2
Nesrine M_O
Lead II
Posted on February 04, 2016 at 10:52

Hi _.adri,

I'd highly recommend you to take a look to the WWDG project under STM32Cube L0 package: 

STM32Cube_FW_L0_V1.4.0\Projects\STM32L053C8Discovery\Examples\WWDG\WWDG_Example

This example guides you through the different configuration steps by mean of HAL API to ensure WWDG counter update at regular period and simulate a software fault generating an MCU WWDG reset on expiry of a programmed time period.

-Syrine-

dree86
Associate II
Posted on February 05, 2016 at 11:26

Hello syrine

Thanks for your reply. I have been reading wwdg example code and notice this code part

/*##-2- Configure the WWDG peripheral ######################################*/
/* WWDG clock counter = (PCLK1 (2MHz)/4096)/1) = 488 Hz (~2049 us)
WWDG Window value = 80 means that the WWDG counter should be refreshed only
when the counter is below 80 (and greater than 64) otherwise a reset will
be generated.
WWDG Counter value = 127, WWDG timeout = ~2049 us * 64 = 13 ms
In this case the refresh window is: ~2049 * (127-80) = 303 ms < refresh window < ~2049 * 64 = 13ms */
WwdgHandle.Instance = WWDG;
WwdgHandle.Init.Prescaler = WWDG_PRESCALER_1;
WwdgHandle.Init.Window = 127;
WwdgHandle.Init.Counter = 127;

I set init window to 127 because I don't know exactly refresh time value. I set window same with example which is 80 and give HAL_Delay(97) before HAL_WWDG_Refresh(&WwdgHandle, 127); to check refresh time but it failed the dog still ''barking''. So i set to 127 to give span 0 ~ 131 ms and its work my dog always get kicked. Is it possible my source clock need to calibrate??