Files
checker_slave/source/elec_det/interface/EWChecker.c
2023-10-10 18:03:38 +08:00

412 lines
10 KiB
C

#include "EWChecker.h"
#include "driver/EWDriver.h"
#include "base/delay.h"
#include "hardware/adc_cfg.h"
#include "base/utility.h"
#include "hardware/power.h"
#include "hardware/timer_cfg.h"
#include "debug.h"
#include "commend.h"
#include "mystring.h"
#include "mystdlib.h"
#define UPDATA_PACK_LEN 128
#define UPDATA_BASE_ADDR 0x1000
#define UPDATA_DATA_LEN (12*1024)
#define UPDATA_FLAG_ADDR (0x7c)
//#define UPDATA_FLAG ((uint8_t []){0x99,0x66,0xaa,0x55})
#define UPDATA_FLAG ((uint8_t []){0xaa,0xbb,0xcc,0xdd})
#define UPDATA_CRC_ALL ((uint32_t *)(MC_CODE_ADDR+16*1024))[0]
#define UPDATA_CRC_APP ((uint32_t *)(MC_CODE_ADDR+16*1024))[1]
// 擦除
static uint8_t EW_bootErease(void)
{
uint8_t data[3]={0xfe,0x00,0x00};
uint8_t read[4]={0};
uint8_t ret=0;
DMod_SendBytesXor(data,2,1);
delay_ms(10);
ret=DMod_ReadBytesXor(read,4,100);
DBG_LOG("ret=%d,dat=%02x,%02x,%02x,%02x,",ret,read[0],read[1],read[2],read[3]);
return ret;
}
static int cmd_jwt_boot_erase(list_def *argv)
{
int ret=0;
ret=EW_bootErease();
return ret;
}
commend_export(jwt_boot_erase,cmd_jwt_boot_erase,"jwt srase in boot")
// 跳转
static uint8_t EW_bootJump(void)
{
uint8_t data[3]={0xf9,0x00,0x00};
uint8_t read[4]={0};
uint8_t ret=0;
DMod_SendBytesXor(data,2,1);
ret=DMod_ReadBytesXor(read,4,100);
DBG_LOG("ret=%d,dat=%02x,%02x,%02x,%02x,",ret,read[0],read[1],read[2],read[3]);
return ret;
}
static int cmd_jwt_boot_jump(list_def *argv)
{
int ret=0;
ret=EW_bootJump();
return ret;
}
commend_export(jwt_boot_jump,cmd_jwt_boot_jump,"jwt jump in boot")
// 充电
static uint8_t EW_bootCharg(void)
{
uint8_t data[4]={0xf8,0x01,0x01,0x00};
uint8_t read[4]={0};
uint8_t ret=0;
DMod_SendBytesXor(data,3,1);
delay_ms(2);
ret=DMod_ReadBytesXor(read,4,100);
DBG_LOG("ret=%d,dat=%02x,%02x,%02x,%02x,",ret,read[0],read[1],read[2],read[3]);
return ret;
}
static int cmd_jwt_boot_charg(list_def *argv)
{
int ret=0;
ret=EW_bootCharg();
return ret;
}
commend_export(jwt_boot_charg,cmd_jwt_boot_charg,"jwt charg in boot")
// 放电
static uint8_t EW_bootDisCharg(void)
{
uint8_t data[4]={0xf8,0x01,0x00,0x00};
uint8_t read[4]={0};
uint8_t ret=0;
DMod_SendBytesXor(data,3,1);
ret=DMod_ReadBytesXor(read,4,100);
DBG_LOG("ret=%d,dat=%02x,%02x,%02x,%02x,",ret,read[0],read[1],read[2],read[3]);
return ret;
}
static int cmd_jwt_boot_discharg(list_def *argv)
{
int ret=0;
ret=EW_bootDisCharg();
return ret;
}
commend_export(jwt_boot_discharg,cmd_jwt_boot_discharg,"jwt discharg in boot")
// 发送数据
// len 最长为128字节
static uint8_t EW_bootWrite(uint16_t addr,uint8_t *d,uint16_t len)
{
uint8_t ret=0;
uint8_t read[4]={0};
uint8_t *buf=rt_malloc(len+2+2+1);
if(buf==RT_NULL) return 1;
buf[0]=0xfc;
buf[1]=2+len;
buf[2]=addr&0xff;
buf[3]=addr>>8;
rt_memcpy(&buf[4],d,len);
LED2_Out=0;
DMod_SendBytesXor(buf,len+2+2,1);
LED2_Out=1;
ret=DMod_ReadBytesXor(read,4,100);
rt_free(buf);
if(ret){
DBG_LOG("ret=%d,dat=%02x,%02x,%02x,%02x,",ret,read[0],read[1],read[2],read[3]);
}
return ret;
}
// 校验
static uint8_t EW_bootCheckCrc(uint16_t adr_start,uint16_t adr_end,uint32_t *crc)
{
uint8_t ret=0;
uint8_t cmd[7]={0xfb,0x04,adr_start&0xff,adr_start>>8,adr_end&0xff,adr_end>>8};
uint8_t read[4+4]={0};
DMod_SendBytesXor(cmd,6,1);
delay_ms(10);
ret=DMod_ReadBytesXor(read,8,100);
if(ret==0){
if(crc){
*crc=(read[3]<<24)|(read[4]<<16)|(read[5]<<8)|(read[6]<<0);
}
}
DBG_LOG("addr_start=0x%04x,addr_end=0x%04x.",adr_start,adr_end);
DBG_LOG("ret=%d,dat=%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,",
ret,read[0],read[1],read[2],read[3],read[4],read[5],read[6],read[7]);
return ret;
}
static int cmd_jwt_boot_checkcrc(list_def *argv)
{
int ret=0;
uint32_t crc=0;
ret=EW_bootCheckCrc(UPDATA_BASE_ADDR,UPDATA_BASE_ADDR+UPDATA_DATA_LEN,&crc);
cmd_print("jwt crc32=0x%04x.",crc);
//crc=Crc32Calu((uint32_t *)(MC_CODE_ADDR+UPDATA_BASE_ADDR),UPDATA_DATA_LEN);
crc=UPDATA_CRC_APP;
cmd_print("local crc32=0x%04x.",crc);
return ret;
}
commend_export(jwt_boot_checkcrc,cmd_jwt_boot_checkcrc,"jwt checkcrc in boot")
// 读取
static uint8_t EW_bootRead(uint16_t adr,uint8_t *buf,uint16_t len)
{
uint8_t ret=0;
uint8_t cmd[7]={0xfa,0x03,adr&0xff,adr>>8,len};
uint16_t read_len=2+len+1+1;
uint8_t *read=rt_malloc(read_len);
if(read==RT_NULL) return 1;
DMod_SendBytesXor(cmd,6,1);
ret=DMod_ReadBytesXor(read,read_len,100);
rt_memset(buf,0,len);
if(ret==0)
{
rt_memcpy(buf,&read[3],len);
}
DBG_LOG("ret=%d,dat=%02x,%02x,%02x,%02x,",ret,read[0],read[1],read[2],read[3]);
rt_free(read);
return ret;
}
// app:通信测试
static int EW_appCommTest(void)
{
uint8_t data[4]={0x11,0x22};
uint8_t ret;
DBG_LOG("send:%02x,%02x.",data[0],data[1]);
ret=EW_CommTest(data,2,300);
DBG_LOG("ret=%d,dat=%02x,%02x,%02x,%02x,",ret,data[0],data[1],data[2],data[3]);
return ret;
}
static int cmd_jwt_app_commtest(list_def *argv)
{
int ret=0;
ret=EW_appCommTest();
return ret;
}
commend_export(jwt_app_commtest,cmd_jwt_app_commtest,"jwt commtest in app")
// app:读取数据
static int EW_appRead(void)
{
uint8_t read_buf[4]={0};
int ret=0;
uint8_t addr=(uint8_t)(UPDATA_FLAG_ADDR/4);
DBG_LOG("addr=0x%02x.",addr);
ret=EW_ReadMTP(1,addr,read_buf,4);
DBG_LOG("ret=%d,dat=%02x,%02x,%02x,%02x,",ret,read_buf[0],read_buf[1],read_buf[2],read_buf[3]);
}
static int cmd_jwt_app_read(list_def *argv)
{
int ret=0;
ret=EW_appRead();
return ret;
}
commend_export(jwt_app_read,cmd_jwt_app_read,"jwt read in app")
// 升级
void EW_Updata(void)
{
Checker_RunCfg_st *cfg=&checker_runcfg;
uint8_t ret=0;
uint8_t read_buf[4]={0};
uint8_t *data=(uint8_t *)MC_CODE_ADDR+UPDATA_BASE_ADDR;
uint16_t len=UPDATA_DATA_LEN;
uint16_t addr=UPDATA_BASE_ADDR;
uint8_t pack_len=UPDATA_PACK_LEN;
uint32_t crc=0;
uint32_t crc_module=0;
delay_ms(100);
ret=EW_bootCharg();
if(ret) {ret=1;goto err;}
delay_ms(6000);
ret=EW_bootErease();
if(ret) {ret=2;goto err;}
delay_ms(600);
while(len>0){
pack_len=len>UPDATA_PACK_LEN?UPDATA_PACK_LEN:len;
LED1_Out=!LED1_Out;
ret=EW_bootWrite(addr,data,pack_len);
addr+=pack_len;
data+=pack_len;
len-=pack_len;
if(ret) {ret=3;goto err;}
delay_ms(1);
}
//crc=Crc32Calu((uint32_t *)(MC_CODE_ADDR+UPDATA_BASE_ADDR),UPDATA_DATA_LEN);
crc=UPDATA_CRC_APP;
DBG_LOG("local crc:0x%04x.",crc);
ret=EW_bootCheckCrc(UPDATA_BASE_ADDR,UPDATA_BASE_ADDR+UPDATA_DATA_LEN,&crc_module);
DBG_LOG("module crc:0x%04x.",crc_module);
if(ret) {ret=4;goto err;}
if(crc!=crc_module){ret=5;goto err;}
delay_ms(50);
ret=EW_bootJump();
if(ret) {ret=6;goto err;}
delay_ms(6000);
ret=EW_EnWriteMTP(0,1);
if(ret) {ret=7;goto err;}
DBG_LOG("EW_EnWriteMTP success.");
delay_ms(1);
ret=EW_WriteMTP(1,(uint8_t)(UPDATA_FLAG_ADDR/4),UPDATA_FLAG,4);
if(ret) {ret=8;goto err;}
DBG_LOG("EW_WriteMTP success.");
delay_ms(1);
ret=EW_ReadMTP(1,(uint8_t)(UPDATA_FLAG_ADDR/4),read_buf,4);
if(ret) {ret=9;goto err;}
if(memcmp(read_buf,UPDATA_FLAG,4)!=0){
DBG_LOG("read_buf=0x%02d,0x%02d,0x%02d,0x%02d.",read_buf[0],read_buf[1],read_buf[2],read_buf[3]);
ret=10;goto err;
}
DBG_LOG("EW_ReadMTP check success.");
delay_ms(1);
XTBUS_OFF;
delay_ms(1000);
XTBUS_ON;
delay_ms(6000);
ret=EW_appCommTest();
if(ret) {ret=11;goto err;}
DBG_LOG("EW_appCommTest success.");
err:
DBG_LOG("ret=%d",ret);
Checker_MaskResult(ret,checker_runcfg.task_info.runindex);
}
void EW_Test_PowerOFF(void)
{
uint8_t uc_rtv = 0;
delay_os_ms(100);
XTBUS_OFF;
PowerCalibration_set(POWER_DEF_V,45);
POWER_OFF;
Checker_MaskResult(uc_rtv,checker_runcfg.task_info.runindex);
}
void EW_Empty(void)
{
Checker_RunCfg_st *cfg=&checker_runcfg;
int count=cfg->rtv_count;
while(count > 0)
{
cfg->Test_Rtv[cfg->rtv_index++] = 0;
count--;
}
Checker_MaskResult(0,cfg->task_info.runindex);
}
void EW_Test_SetBusV(void)
{
Checker_RunCfg_st *cfg=&checker_runcfg;
uint8_t uc_rtv = 0;
uint16_t us_h_v,us_m_v;
uint16_t power_old;
us_h_v = cfg->params[0];
power_old=us_h_v;
if(us_h_v < 55)
{
uc_rtv = 1;
}else{
us_m_v = 55;
uc_rtv |= PowerCalibration_set(us_h_v,us_m_v);
XTBUS_ON
us_m_v = ((us_h_v/80)+2)*60;
delay_ms(us_m_v);
us_h_v = Power_GetBousV();
Checker_SetRtv(&us_h_v,cfg->rtv_count);
}
if(uc_rtv){
DBG_WARN("bus power init failed.h");
}
if((us_h_v<power_old-10)||(us_h_v>power_old+10)){
DBG_WARN("power set fialed,want=%d,reality=%d.",power_old,us_h_v);
}
Checker_MaskResult(uc_rtv,cfg->task_info.runindex);
}
CheckerTask ewtaskArray[CHECKER_MAXID_COUNT] ={
Checker_PowerPrapare, //0 电源准备
EW_Empty, //1 上电充能
EW_Test_SetBusV, //2 设置总线电压
EW_Empty, //3 获取总线电流
EW_Empty, //4 扫描UID
EW_Empty, //5 写配置参数
EW_Empty, //6 验证配置
EW_Empty, //7 模拟注码
EW_Empty, //8 充能统计
EW_Empty, //9 写现场值 网络id 延时
EW_Empty, //10比对现场值
EW_Empty, //11 桥丝通断检测
EW_Empty, //12 电容容量统计
EW_Empty, //13 延时等待
EW_Empty, //14 写管壳号/工厂信息
EW_Empty, //15 写UID
EW_Empty, //16 写密码
EW_Empty, //17 写入/检测备份区标志
EW_Empty, //18 读取备份区数据
EW_Empty, //19 校准
EW_Empty, //20 使能通讯末电流采集
EW_Empty, //21 获取通讯末电流
EW_Empty, //22 放电
EW_Empty, //23 在线检测
EW_Empty, //24 状态检测
EW_Empty, //25 起爆
EW_Empty, //26 复位
EW_Test_PowerOFF, //27 关总线
EW_Empty, //28 芯片锁存
EW_Empty, //29 使能赋码设备
EW_Empty, //30 在线检测
EW_Empty, //31 密码验证
EW_Empty, //32 加载芯片配置
EW_Empty, //33 电容压差测试
Checker_ResistorSample,//34 桥丝电阻测试
EW_Empty, //35 检测过程中注码
EW_Empty, //36 验证注码
EW_Empty, //37 模块在线检测
EW_Updata, //38 升级
(void*)0 //数组结束
};