AnsweredAssumed Answered

[HELP]Getting Started Using WATCHDOG

Question asked by _.Adri on Feb 4, 2016
Latest reply on Feb 5, 2016 by _.Adri
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 = 131.13 ms
     In this case the refresh window is: ~2049 * (127-80) = 96.303 ms < refresh window < ~2049 * 64 = 131.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

Outcomes