Minh Nguyen Van

There is a confusion in function IWDG_Config():

Discussion created by Minh Nguyen Van on Jan 13, 2018

From RM0016_STM8S series and STM8AF series 8-bit microcontroller datasheet, page 124/595, the Fomular is:

But as we have in "STM8S_StdPeriph_Lib"

static void IWDG_Config(void)

{

/**
* @brief Configures the IWDG to generate a Reset if it is not refreshed at the
* correct time.
* @param None
* @retval None
*/
static void IWDG_Config(void)
{
   /* Enable IWDG (the LSI oscillator will be enabled by hardware) */
   IWDG_Enable();

   /* IWDG timeout equal to 250 ms (the timeout may varies due to LSI frequency
      dispersion) */
   /* Enable write access to IWDG_PR and IWDG_RLR registers */
   IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);

   /* IWDG counter clock: LSI/128 */
   IWDG_SetPrescaler(IWDG_Prescaler_128);

   /* Set counter reload value to obtain 250ms IWDG Timeout.
      Counter Reload Value = 250ms/IWDG counter clock period
                                          = 250ms / (LSI/128)
                                          = 0.25s / (LsiFreq/128)
                                          = LsiFreq/(128 * 4)
                                          = LsiFreq/512
   */
   IWDG_SetReload((uint8_t)(LsiFreq/512));

   /* Reload IWDG counter */
   IWDG_ReloadCounter();
}

/***********************************************************************************************/

It has to be 

R=T/(2*TLSI*P)

  =250ms/(2*TLSI*128)

  =250ms* LsiFreq /(2*128)

  =0.25* LsiFreq /(2*128)

  = LsiFreq /(2*128*4)

  = LsiFreq /1024

So IWDG_SetReload((uint8_t)(LsiFreq/1024));

[If we still use IWDG_SetReload((uint8_t)(LsiFreq/512));, the result we have is 500ms IWDG timeout period]

<Minh Nguyen Van>

Outcomes