#include "at32f403a_407.h"
#include "update.h"
#include "common_config.h"
#include "FreeRTOS.h"
#include "task.h"
#include "fr_timers.h"
#include <stdio.h>


#define RESET_TIM_DELAY     configTICK_RATE_HZ*3 


TimerHandle_t reset_timer_handle;
void cb_reset_timer(TimerHandle_t timer);


//
void update_create_timer(void)
{
    reset_timer_handle = xTimerCreate("reset_timer", RESET_TIM_DELAY, 
                                      pdFALSE, (void *)0, cb_reset_timer);
}

//
void update_tim_start(void)
{
    xTimerStart(reset_timer_handle, 0); 
}

//
void update_set_flag(uint16_t val)
{
    bpr_data_write(BACKUP_IAP_LOAD_MODE, val);
}

//
void update_reset_boot_try(void)
{
    bpr_data_write(BACKUP_IAP_BOOT_TRY, 0);
}

//
void cb_reset_timer(TimerHandle_t timer)
{
    DBG printf("Reset timer callback. Switch to IAP\r\n");
    update_set_flag(1);
    NVIC_SystemReset();
    
}
