仓库迁移
This commit is contained in:
149
source/dev/dev_backup.c
Normal file
149
source/dev/dev_backup.c
Normal file
@@ -0,0 +1,149 @@
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
|
34
source/dev/dev_backup.h
Normal file
34
source/dev/dev_backup.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef dev_backup_h__
|
||||
#define dev_backup_h__
|
||||
|
||||
#include "stdint.h"
|
||||
|
||||
|
||||
// 定义复位类型
|
||||
#define REBOOT_APP_TO_APP 0xabcd
|
||||
#define REBOOT_APP_TO_BOOT 0xbcde
|
||||
#define REBOOT_BOOT_TO_APP 0x1234
|
||||
#define REBOOT_BOOT_TO_BOOT 0x2345
|
||||
#define REBOOT_PARAM_ERR 0xaaaa
|
||||
#define REBOOT_HARD_ERR 0xbbbb
|
||||
#define REBOOT_INIT 0xffff
|
||||
|
||||
|
||||
void bk_reboot_app(void);
|
||||
void bk_reboot_boot(void);
|
||||
void bk_reboot_updata(void);
|
||||
void bk_reboot_guide(void);
|
||||
void bk_reboot_param_err(void);
|
||||
void bk_reboot_hard_err(void);
|
||||
void bk_init(void);
|
||||
uint32_t bk_get_boottype(void);
|
||||
const char *bk_get_currtype(void);
|
||||
void bk_set_wdog_fun(void (*fun)(void));
|
||||
void *bk_wdog_fun(void);
|
||||
uint32_t bk_reboot_times(void);
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
287
source/dev/dev_flash.c
Normal file
287
source/dev/dev_flash.c
Normal file
@@ -0,0 +1,287 @@
|
||||
|
||||
|
||||
#include "rtthread.h"
|
||||
#include "stm32f4xx.h"
|
||||
#include "board.h"
|
||||
#include "dev_flash.h"
|
||||
#include "debug.h"
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
#ifndef RT_THREAD
|
||||
|
||||
#define rt_interrupt_enter()
|
||||
#define rt_interrupt_leave()
|
||||
#define rt_mutex_create(...) 0
|
||||
#define rt_mutex_delete(...)
|
||||
#define rt_mutex_take(...)
|
||||
#define rt_mutex_release(...)
|
||||
|
||||
#endif
|
||||
|
||||
typedef struct{
|
||||
void *mutex;
|
||||
}self_def;
|
||||
|
||||
static self_def g_self;
|
||||
|
||||
|
||||
|
||||
typedef struct{
|
||||
uint32_t *addr;
|
||||
uint32_t sector;
|
||||
}flash_item;
|
||||
|
||||
const static flash_item g_flash_addr_table[]={
|
||||
{(uint32_t *)0x08000000,FLASH_Sector_0},
|
||||
{(uint32_t *)0x08004000,FLASH_Sector_1},
|
||||
{(uint32_t *)0x08008000,FLASH_Sector_2},
|
||||
{(uint32_t *)0x0800c000,FLASH_Sector_3},
|
||||
{(uint32_t *)0x08010000,FLASH_Sector_4},
|
||||
{(uint32_t *)0x08020000,FLASH_Sector_5},
|
||||
{(uint32_t *)0x08040000,FLASH_Sector_6},
|
||||
{(uint32_t *)0x08060000,FLASH_Sector_7},
|
||||
{(uint32_t *)0x08080000,FLASH_Sector_8},
|
||||
{(uint32_t *)0x080a0000,FLASH_Sector_9},
|
||||
{(uint32_t *)0x080c0000,FLASH_Sector_10},
|
||||
{(uint32_t *)0x080e0000,FLASH_Sector_11},
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#define FLASH_CLEAR_FLAG() \
|
||||
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR|\
|
||||
FLASH_FLAG_PGAERR|FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR|FLASH_FLAG_RDERR);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 擦除指定扇区,返回0成功,-1失败
|
||||
static int flash_erase(uint32_t FLASH_Sector)
|
||||
{
|
||||
self_def *s=&g_self;
|
||||
FLASH_Status ret;
|
||||
if(s->mutex==0){
|
||||
s->mutex=rt_mutex_create("flash_mutex",RT_IPC_FLAG_FIFO);
|
||||
}
|
||||
rt_mutex_take(s->mutex,RT_WAITING_FOREVER);
|
||||
FLASH_Unlock();
|
||||
FLASH_CLEAR_FLAG();
|
||||
FLASH_DataCacheCmd(DISABLE);
|
||||
ret=FLASH_EraseSector(FLASH_Sector,VoltageRange_3);
|
||||
rt_mutex_release(s->mutex);
|
||||
if(ret!=FLASH_COMPLETE)
|
||||
{
|
||||
DBG_WARN("flash earse failed.ret=%d",ret);
|
||||
return -1;
|
||||
}else{
|
||||
DBG_LOG("flash earse success.ret=%d",ret);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 操作结束
|
||||
int flash_operate_end(void)
|
||||
{
|
||||
FLASH_Status state;
|
||||
int ret=0;
|
||||
self_def *s=&g_self;
|
||||
#ifdef RT_THREAD
|
||||
param_check(s->mutex);
|
||||
#endif
|
||||
rt_mutex_take(s->mutex,RT_WAITING_FOREVER);
|
||||
FLASH_DataCacheCmd(ENABLE);
|
||||
FLASH_Lock();
|
||||
rt_mutex_release(s->mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 参数区16K
|
||||
int flash_erase_param(void)
|
||||
{
|
||||
int ret;
|
||||
ret=flash_erase(FLASH_Sector_1);
|
||||
if(ret!=0) return -1;
|
||||
return 0;
|
||||
}
|
||||
//程序区1 256K
|
||||
int flash_erase_app1(void)
|
||||
{
|
||||
int ret;
|
||||
ret=flash_erase(FLASH_Sector_5);
|
||||
if(ret!=0) return -1;
|
||||
ret=flash_erase(FLASH_Sector_6);
|
||||
if(ret!=0) return -1;
|
||||
return 0;
|
||||
}
|
||||
//程序区2 256K
|
||||
int flash_erase_app2(void)
|
||||
{
|
||||
int ret;
|
||||
ret=flash_erase(FLASH_Sector_7);
|
||||
if(ret!=0) return -1;
|
||||
ret=flash_erase(FLASH_Sector_8);
|
||||
if(ret!=0) return -1;
|
||||
return 0;
|
||||
}
|
||||
//小板程序区 128K
|
||||
int flash_erase_slave(void)
|
||||
{
|
||||
int ret;
|
||||
ret=flash_erase(FLASH_Sector_9);
|
||||
if(ret!=0) return -1;
|
||||
return 0;
|
||||
}
|
||||
//方案区 16K
|
||||
int flash_erase_scheme(void)
|
||||
{
|
||||
int ret;
|
||||
ret=flash_erase(FLASH_Sector_2);
|
||||
if(ret!=0) return -1;
|
||||
return 0;
|
||||
}
|
||||
//脚本区 128K
|
||||
int flash_erase_lua(void)
|
||||
{
|
||||
int ret;
|
||||
ret=flash_erase(FLASH_Sector_11);
|
||||
if(ret!=0) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
uint32_t *flash_get_param(void)
|
||||
{
|
||||
return (void *)0x08004000;
|
||||
}
|
||||
|
||||
//uint32_t *flash_get_app1(void)
|
||||
//{
|
||||
// return (void *)0x08020000;
|
||||
//}
|
||||
|
||||
uint32_t *flash_get_app2(void)
|
||||
{
|
||||
return (void *)0x08060000;
|
||||
}
|
||||
|
||||
uint32_t *flash_get_slave(void)
|
||||
{
|
||||
return (void *)0x080a0000;
|
||||
}
|
||||
|
||||
uint32_t *flash_get_scheme(void)
|
||||
{
|
||||
return (void *)0x08008000;
|
||||
}
|
||||
|
||||
uint32_t *flash_get_lua(void)
|
||||
{
|
||||
return (void *)0x080e0000;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int flash_write(uint8_t *addr,const uint8_t *data,int len)
|
||||
{
|
||||
FLASH_Status state;
|
||||
int ret=0;
|
||||
self_def *s=&g_self;
|
||||
if((uint32_t)data%4)
|
||||
{
|
||||
DBG_WARN("write addr err.");
|
||||
return -1;
|
||||
}
|
||||
uint32_t *t=(uint32_t *)data;
|
||||
uint32_t flash_addr=(uint32_t)addr;
|
||||
#ifdef RT_THREAD
|
||||
param_check(s->mutex);
|
||||
#endif
|
||||
rt_mutex_take(s->mutex,RT_WAITING_FOREVER);
|
||||
|
||||
//DBG_LOG("write flash addr=%08x",flash_addr);
|
||||
len+=3;// len不是4字节对齐时强行增加到4字节
|
||||
for(int i=0;i<len/4;i++)
|
||||
{
|
||||
//FLASH_CLEAR_FLAG();
|
||||
//state=FLASH_ProgramByte((uint32_t)addr,data[i]);
|
||||
state=FLASH_ProgramWord(flash_addr,t[i]);
|
||||
flash_addr+=4;
|
||||
if(state!=FLASH_COMPLETE)
|
||||
{
|
||||
DBG_WARN("flash write failed.ret=%d",state);
|
||||
DBG_WARN("FLASH->SR=%08X",FLASH->SR);
|
||||
ret=-1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(memcmp(addr,data,(len/4)*4)!=0)
|
||||
{
|
||||
DBG_WARN("addr=%08x write/read data not equate.",addr);
|
||||
ret=-2;
|
||||
}
|
||||
rt_mutex_release(s->mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
uint8_t *flash_get_rom(rom_head **head)
|
||||
{
|
||||
uint8_t *src=(uint8_t *)flash_get_app2();
|
||||
flash_file *file=(flash_file *)flash_get_app2();
|
||||
rom_head *h=(rom_head *)(src+FLASH_FILE_HEAD_SIZE);
|
||||
uint8_t *rom=src+ROM_HEAD_SIZE+FLASH_FILE_HEAD_SIZE;
|
||||
if(file->file_size==0xffffffff){
|
||||
return 0;
|
||||
}
|
||||
if(head) *head=h;
|
||||
return rom;
|
||||
}
|
||||
|
||||
int flash_updata_app(uint8_t *rom,uint32_t size)
|
||||
{
|
||||
uint8_t *dst=(uint8_t *)0x08020000;
|
||||
flash_erase_app1();
|
||||
return flash_write(dst,rom,size);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// 保存参数
|
||||
int flash_save_param(sys_param_def *par)
|
||||
{
|
||||
uint8_t *dst=(uint8_t *)flash_get_param();
|
||||
uint8_t *src=(uint8_t *)par;
|
||||
int size=sizeof(sys_param_def);
|
||||
flash_erase_param();
|
||||
return flash_write(dst,src,size);
|
||||
}
|
||||
|
||||
|
||||
const sys_param_def *sys_param(void)
|
||||
{
|
||||
return (sys_param_def *)flash_get_param();
|
||||
}
|
||||
|
||||
|
||||
|
||||
const scheme_def *check_scheme(void)
|
||||
{
|
||||
uint8_t *d=(uint8_t *)flash_get_scheme();
|
||||
return (scheme_def *)(d+FLASH_FILE_HEAD_SIZE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
101
source/dev/dev_flash.h
Normal file
101
source/dev/dev_flash.h
Normal file
@@ -0,0 +1,101 @@
|
||||
#ifndef dev_flash_h__
|
||||
#define dev_flash_h__
|
||||
|
||||
|
||||
#include "stdint.h"
|
||||
|
||||
|
||||
|
||||
__packed
|
||||
typedef struct{
|
||||
uint32_t file_size;//文件长度,不包括文件名
|
||||
char name[120];
|
||||
int used; // 初始值为0xffffffff,每使用一次添加一个0
|
||||
}flash_file;
|
||||
|
||||
#define FLASH_FILE_HEAD_SIZE 128
|
||||
|
||||
|
||||
|
||||
int flash_erase_param(void);
|
||||
int flash_erase_app1(void);
|
||||
int flash_erase_app2(void);
|
||||
int flash_erase_slave(void);
|
||||
int flash_erase_scheme(void);
|
||||
int flash_erase_lua(void);
|
||||
uint32_t *flash_get_param(void);
|
||||
//uint32_t *flash_get_app1(void);
|
||||
uint32_t *flash_get_app2(void);
|
||||
uint32_t *flash_get_slave(void);
|
||||
uint32_t *flash_get_scheme(void);
|
||||
uint32_t *flash_get_lua(void);
|
||||
int flash_write(uint8_t *addr,const uint8_t *data,int len);
|
||||
int flash_operate_end(void);
|
||||
|
||||
|
||||
|
||||
|
||||
__packed
|
||||
typedef struct{
|
||||
uint32_t size;
|
||||
char pack_time[20];
|
||||
uint32_t crc;
|
||||
uint8_t local_ip[4];
|
||||
uint8_t host_ip[4];
|
||||
int host_port;
|
||||
char host_if[8];
|
||||
}rom_head;
|
||||
|
||||
|
||||
#define ROM_HEAD_SIZE 128
|
||||
|
||||
|
||||
uint8_t *flash_get_rom(rom_head **head);
|
||||
|
||||
int flash_updata_app(uint8_t *rom,uint32_t size);
|
||||
|
||||
|
||||
|
||||
|
||||
// 系统参数
|
||||
__packed
|
||||
typedef struct{
|
||||
char pack_time[20];// 打包时间
|
||||
char host_if[8];// 主机接口
|
||||
char device_type[12];// 设备类型 ,批检仪还是赋码仪
|
||||
uint8_t mac[8];// mac地址
|
||||
uint8_t local_ip[4];// 本机ip地址
|
||||
uint8_t host_ip[4];// 主机ip地址
|
||||
int host_port;// 主机端口
|
||||
int local_cmd_port;// 本机命令端口
|
||||
int host_log_port;// 主机log端口
|
||||
int local_id;// 本机识别号
|
||||
}sys_param_def;
|
||||
|
||||
|
||||
|
||||
int flash_save_param(sys_param_def *par);
|
||||
const sys_param_def *sys_param(void);
|
||||
|
||||
|
||||
|
||||
// 方案参数
|
||||
__packed
|
||||
typedef struct{
|
||||
uint8_t slave_data[2048];
|
||||
uint32_t plan_id;
|
||||
uint32_t timeout_m;
|
||||
uint32_t range_num;
|
||||
__packed
|
||||
struct{
|
||||
uint32_t max;
|
||||
uint32_t min;
|
||||
}range[0];
|
||||
}scheme_def;
|
||||
|
||||
|
||||
const scheme_def *check_scheme(void);
|
||||
|
||||
|
||||
#endif
|
||||
|
21
source/dev/dev_watchdog.c
Normal file
21
source/dev/dev_watchdog.c
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
#include "stm32f4xx.h"
|
||||
#include "board.h"
|
||||
|
||||
|
||||
|
||||
// 开始watch dog
|
||||
void wdog_start(void)
|
||||
{
|
||||
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
|
||||
IWDG_SetPrescaler(IWDG_Prescaler_32);
|
||||
IWDG_Enable();
|
||||
}
|
||||
|
||||
// 更新看门狗
|
||||
void wdog_refresh(void)
|
||||
{
|
||||
IWDG_ReloadCounter();
|
||||
}
|
||||
|
||||
|
15
source/dev/dev_watchdog.h
Normal file
15
source/dev/dev_watchdog.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef dev_watchdog_h__
|
||||
#define dev_watchdog_h__
|
||||
|
||||
|
||||
void wdog_start(void);
|
||||
void wdog_refresh(void);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user