/*******************************************************************************
 **                       
 **文 件 名:bsp_wdog.c
 **创建日期:2015.10.15
 **文件说明:
 **修改记录:
 *******************************************************************************/
#include <stddef.h>
#include "drv_wdog.h"
#include "stm32f4xx.h"

#define KR_KEY_Reload               ((uint16_t)0xAAAA)
#define KR_KEY_Enable               ((uint16_t)0xCCCC)
#define IWDG_Prescaler_64           ((uint8_t)0x04)
#define IWDG_WriteAccess_Enable     ((uint16_t)0x5555)
#define IWDG_WriteAccess_Disable    ((uint16_t)0x0000)


//每个tick时间 1000/(40000/64) = 1.6s
void drvInitWdog(uint16_t idog_tick)
{
    //使能对IWDG->PR和IWDG->RLR的写                                                   
    IWDG->KR = IWDG_WriteAccess_Enable;
    
    //40kHz/64= 625Hz  
    IWDG->PR = IWDG_Prescaler_64;
    if(idog_tick > 4095)
        idog_tick = 4095;
    IWDG->RLR = idog_tick;
    //必须reload 否则时间不对
    IWDG->KR = KR_KEY_Reload; 
    IWDG->KR = KR_KEY_Enable;
    IWDG->KR = IWDG_WriteAccess_Disable;	
}

void drvFeedWdog(void)
{    
    IWDG->KR = KR_KEY_Reload;
}

