bleSDK_expansion_board/projects/blezongkong/src/sys_config.c

100 lines
3.2 KiB
C

#include "b6x.h"
#include "regs.h"
#include "drvs.h"
#include "dbg.h"
#include "sftmr.h"
#include "CRCxx.h"
#include "bledef.h"
#include "app.h"
#include "prf_sess.h"
#include "sys_config.h"
#define DBG_CONF_EN 1
#if (DBG_CONF_EN)
#include "dbg.h"
#define DEBUG(format, ...) debug("[CONF]" format "\r\n", ##__VA_ARGS__)
#else
#define DEBUG(format, ...)
#define debugHex(dat, len)
#endif
SYS_CONF_t sys_conf;
// __attribute__((section("ram_func.fshc.")))
// static void flash_sector_erase(uint32_t offset)
// {
// GLOBAL_INT_DISABLE();
// while (SYSCFG->ACC_CCR_BUSY);
// fshc_erase(offset, FSH_CMD_ER_SECTOR);
// GLOBAL_INT_RESTORE();
// }
// 写配置
void write_cfg(SYS_CONF_t *sys_config_info_t){
flash_page_erase(CONF_OFFSET_ADDR & 0x00FFFF00);
sys_config_info_t->CRC16 = crc16_modbus((uint8_t *)sys_config_info_t,sizeof(SYS_CONF_t)-2); //计算CRC16
DEBUG("Write: CRC16:%#04x",sys_config_info_t->CRC16);
flash_write(CONF_OFFSET_ADDR & 0x00FFFF00, (uint32_t *)sys_config_info_t,sizeof(SYS_CONF_t)/sizeof(uint32_t));
}
// 读配置 // 返回 0 成功 1 数据校验错误
uint8_t read_cfg(SYS_CONF_t *sys_config_info_t){
uint16_t crc16;
flash_read(CONF_OFFSET_ADDR & 0x00FFFF00, (uint32_t *)sys_config_info_t,sizeof(SYS_CONF_t)/sizeof(uint32_t));
crc16 = crc16_modbus((uint8_t *)sys_config_info_t,sizeof(SYS_CONF_t)-2); //计算CRC16
DEBUG("READ: CRC16:%#04x ,CRC16_CAL:%#04x",sys_config_info_t->CRC16 ,crc16);
return ((sys_config_info_t->CRC16 == crc16) ? 0 : 1);
}
#define BANK_A_BASE (0x18004000)
#define BANK_B_BASE (0x18020000)
void conf_init(void){
uint32_t curr_code_addr = RD_32(0x18000008);
DEBUG("Curr Addr:0x%x", curr_code_addr);
if(curr_code_addr == BANK_A_BASE){
sys_conf.VER_type =1;
}else if(curr_code_addr == BANK_B_BASE){
sys_conf.VER_type =0;
}
// read config
if( read_cfg(&sys_conf) || (sys_conf.VERSION != SOFTWARE_ID)){
DEBUG("Read Config :CRC_ERROR ");//读取配置文件失败//使用默认配置
sys_conf.VERSION =SOFTWARE_ID;
sys_conf.Modbus_addr =D_Modbus_addr;
sys_conf.Manager_sLim = D_Manager_sLim;
sys_conf.Tourist_sLim = D_Tourist_sLim;
sys_conf.Speed_Cut_sLim = D_Speed_Cut_sLim;//(自动减速时油门极限)
sys_conf.Brake_DLimit = D_Brake_DLimit;//自动刹车距离(前进)
sys_conf.Speed_Cut_DLimit = D_Speed_Cut_DLimit;//自动减速距离
sys_conf.Brake_DLimit_B = D_Brake_DLimit_B;//自动刹车距离(后退)
sys_conf.Speed_Cut_DLimit_B = D_Speed_Cut_DLimit_B;//自动减速距离
DEBUG("write Default Config!!!");// 写入默认配置
write_cfg(&sys_conf);
}
DEBUG("\nsys_conf:lenght=%d",sizeof(sys_conf));
DEBUG("VERSION:%#04X",sys_conf.VERSION);
DEBUG("Modbus_addr:%#04X",sys_conf.Modbus_addr);
DEBUG("Manager_sLim:%d",sys_conf.Manager_sLim);
DEBUG("Tourist_sLim:%d",sys_conf.Tourist_sLim);
DEBUG("Speed_Cut_sLim:%d",sys_conf.Speed_Cut_sLim);
DEBUG("Brake_DLimit:%d",sys_conf.Brake_DLimit);
DEBUG("Speed_Cut_DLimit:%d",sys_conf.Speed_Cut_DLimit);
DEBUG("Brake_DLimit_B:%d",sys_conf.Brake_DLimit_B);
DEBUG("Speed_Cut_DLimit_B:%d\n",sys_conf.Speed_Cut_DLimit_B);
}