#include "rtc_wdt.h"

const char alarm_tip[] = "\n----------------->> alarm <<--------------------\n";
const char wdt_tip[] = "\n----------------->> EDT<<--------------------\n";

AT(.com_text.isr)
void rtc_isr(void)
{
    if (rtc_get_flag(RTC_FLAG_ALM) != RESET)        //RTC闹钟中断
    {
        rtc_clear_flag(RTC_FLAG_ALM);
        printk(alarm_tip);
    }
    else if ((WDTCON&WDT_FLAG_PENDING) != RESET)    //看门狗中断
    {
        WDTCON &= (uint32_t)~0x0f;
        WDTCON |= (uint32_t)0x0a;
        printf(wdt_tip);
//        WDT_RST();    //软件复位函数
    }

}

void rtc_calendar_init(void)
{
    rtc_clk_typedef clk_struct;
    clock_gate0_cmd(CLOCK_GATE0_RTCC, ENABLE);

    clk_struct.external_enable = DISABLE;
    clk_struct.clk_value_flag = RTC_32K_CLK_MODE;
    clk_struct.clk_source_sel = RTC_RC2M_SOURCE;
    rtc_clk_config(&clk_struct);

    /*设置rtc中断时间*/
    RTCCNT = 10;    //
    RTCALM = 15;    //6s后进行中断-只中断一次
    rtc_pic_config(rtc_isr, 0, RTC_IT_ALM, ENABLE);
    /***************/
}

/************看门狗初始化-需要与RTC放同一天中断函数里************/
void wdt_test_init(void)
{
    wdt_reset_cmd(DISABLE);
    wdt_it_cmd(ENABLE);
    wdt_cmd(ENABLE);

    wdt_time_select(WDT_TIME_2048MS);   //看门狗设置为2s中断

    sys_irq_init(RTC_LVD_WDT_IRQn,0,rtc_isr);
}
