150 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			150 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
 | 
						||
#include "dev_backup.h"
 | 
						||
#include "stm32f4xx.h"
 | 
						||
#include "board.h"
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
// 备份结构体,一共32个字节
 | 
						||
typedef struct{
 | 
						||
  uint32_t state;// 程序状态
 | 
						||
  uint32_t tick;// 运行时间
 | 
						||
  uint32_t reboot_type;// 复位类型
 | 
						||
  uint32_t reboot_curr;// 最后一次复位标志
 | 
						||
  void (*wdog_refresh)(void);// 刷新看门口
 | 
						||
  uint32_t reboot_times;// 软件重启次数
 | 
						||
}back_def;
 | 
						||
 | 
						||
 | 
						||
#define g_back ((back_def *)0x20000000)
 | 
						||
 | 
						||
 | 
						||
// 重启到app
 | 
						||
void bk_reboot_app(void)
 | 
						||
{
 | 
						||
  g_back->reboot_type=REBOOT_APP_TO_APP;
 | 
						||
  NVIC_SystemReset();
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
// 重启到boot
 | 
						||
void bk_reboot_boot(void)
 | 
						||
{
 | 
						||
  g_back->reboot_type=REBOOT_BOOT_TO_BOOT;
 | 
						||
  NVIC_SystemReset();
 | 
						||
}
 | 
						||
 | 
						||
// 重启并升级
 | 
						||
void bk_reboot_updata(void)
 | 
						||
{
 | 
						||
  g_back->reboot_type=REBOOT_APP_TO_BOOT;
 | 
						||
  NVIC_SystemReset();
 | 
						||
}
 | 
						||
 | 
						||
// 重启并引导到app
 | 
						||
void bk_reboot_guide(void)
 | 
						||
{
 | 
						||
  g_back->reboot_type=REBOOT_BOOT_TO_APP;
 | 
						||
  NVIC_SystemReset();
 | 
						||
}
 | 
						||
 | 
						||
// 断言失败重启
 | 
						||
void bk_reboot_param_err(void)
 | 
						||
{
 | 
						||
  g_back->reboot_type=REBOOT_PARAM_ERR;
 | 
						||
  NVIC_SystemReset();
 | 
						||
}
 | 
						||
 | 
						||
// 硬件错误重启
 | 
						||
void bk_reboot_hard_err(void)
 | 
						||
{
 | 
						||
  g_back->reboot_type=REBOOT_HARD_ERR;
 | 
						||
  NVIC_SystemReset();
 | 
						||
}
 | 
						||
 | 
						||
// 获取重启类型
 | 
						||
uint32_t bk_get_boottype(void)
 | 
						||
{
 | 
						||
  return g_back->reboot_type;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
 | 
						||
static int bk_soft_reboot(void);
 | 
						||
void bk_init(void)
 | 
						||
{
 | 
						||
  g_back->reboot_curr=g_back->reboot_type;
 | 
						||
  g_back->reboot_type=REBOOT_INIT;
 | 
						||
  g_back->wdog_refresh=0;
 | 
						||
  
 | 
						||
  // 每软件重启一次就增加计数
 | 
						||
  if(bk_soft_reboot())
 | 
						||
  {
 | 
						||
    g_back->reboot_times++;
 | 
						||
  }else{
 | 
						||
    g_back->reboot_times=0;
 | 
						||
  }
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
typedef struct{
 | 
						||
  uint32_t type;
 | 
						||
  const char *str;
 | 
						||
}reboot_type_str;
 | 
						||
 | 
						||
static const reboot_type_str g_reboot_str[]={
 | 
						||
  REBOOT_APP_TO_APP,"app restart",
 | 
						||
  REBOOT_APP_TO_BOOT,"app reboot to boot",
 | 
						||
  REBOOT_BOOT_TO_APP,"boot guide to app",
 | 
						||
  REBOOT_BOOT_TO_BOOT,"boot restart",
 | 
						||
  REBOOT_PARAM_ERR,"param err then reboot",
 | 
						||
  REBOOT_HARD_ERR,"hard err then reboot",
 | 
						||
  REBOOT_INIT,"watch dog died then reboot",
 | 
						||
};
 | 
						||
 | 
						||
 | 
						||
// 获取最后一次复位原因
 | 
						||
const char *bk_get_currtype(void)
 | 
						||
{
 | 
						||
  uint32_t type=g_back->reboot_curr;
 | 
						||
  for(int i=0;i<LENGTH(g_reboot_str);i++)
 | 
						||
  {
 | 
						||
    if(type==g_reboot_str[i].type)
 | 
						||
      return g_reboot_str[i].str;
 | 
						||
  }
 | 
						||
  return "poweron then started";
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
// 非上电启动返回1
 | 
						||
static int bk_soft_reboot(void)
 | 
						||
{
 | 
						||
  uint32_t type=g_back->reboot_curr;
 | 
						||
  for(int i=0;i<LENGTH(g_reboot_str);i++)
 | 
						||
  {
 | 
						||
    if(type==g_reboot_str[i].type)
 | 
						||
      return 1;
 | 
						||
  }
 | 
						||
  return 0;
 | 
						||
}
 | 
						||
 | 
						||
// 重启次数
 | 
						||
uint32_t bk_reboot_times(void)
 | 
						||
{
 | 
						||
  return g_back->reboot_times;
 | 
						||
}
 | 
						||
 | 
						||
void bk_set_wdog_fun(void (*fun)(void))
 | 
						||
{
 | 
						||
  g_back->wdog_refresh=fun;
 | 
						||
}
 | 
						||
void *bk_wdog_fun(void)
 | 
						||
{
 | 
						||
  return g_back->wdog_refresh;
 | 
						||
}
 | 
						||
 | 
						||
 |