2024-08-19 01:14:21 +08:00
|
|
|
|
#include "b6x.h"
|
|
|
|
|
#include "drvs.h"
|
|
|
|
|
#include "dbg.h"
|
|
|
|
|
#include "rbuf.h"
|
|
|
|
|
#include "CRCxx.h"
|
|
|
|
|
|
|
|
|
|
#include "sys_config.h"
|
|
|
|
|
|
|
|
|
|
#include "app_PAD.h"
|
|
|
|
|
|
|
|
|
|
// 用于PAD通信处理
|
|
|
|
|
|
|
|
|
|
PAD_daraframe_t PAD_daraframe;
|
|
|
|
|
volatile uint8_t Rec_state =0;
|
|
|
|
|
volatile uint8_t Rec_data_cnt =0;
|
|
|
|
|
|
|
|
|
|
static void PAD_Data_Hande(void){
|
|
|
|
|
uint8_t ret_data[MAX_LEN]={0,0};
|
|
|
|
|
uint8_t length=0;
|
2024-08-19 08:46:40 +08:00
|
|
|
|
|
|
|
|
|
switch(PAD_daraframe.reg_addr){
|
|
|
|
|
case 0x01: // 写入系统配置
|
|
|
|
|
if(PAD_daraframe.cmd_id == 0x06){// 写入
|
|
|
|
|
write_cfg(&sys_conf);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 0x02: // 管理员模式油门极限寄存器(Unit:%)
|
|
|
|
|
if(PAD_daraframe.cmd_id == 0x03){// 读取
|
|
|
|
|
// ret_data[length++] =0;
|
|
|
|
|
ret_data[length++] =sys_conf.Manager_sLim;
|
|
|
|
|
// if(0 == --PAD_daraframe.length){
|
|
|
|
|
break;
|
|
|
|
|
// }
|
|
|
|
|
}else if(PAD_daraframe.cmd_id == 0x06){// 写入
|
|
|
|
|
sys_conf.Manager_sLim =PAD_daraframe.data[0];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 0x03: // 游客模式油门极限寄存器(Unit:%)
|
|
|
|
|
if(PAD_daraframe.cmd_id == 0x03){// 读取
|
|
|
|
|
// ret_data[length++] =0;
|
|
|
|
|
ret_data[length++] =sys_conf.Tourist_sLim;
|
|
|
|
|
// if(0 == --PAD_daraframe.length){
|
|
|
|
|
break;
|
|
|
|
|
// }
|
|
|
|
|
}else if(PAD_daraframe.cmd_id == 0x06){// 写入
|
|
|
|
|
sys_conf.Tourist_sLim =PAD_daraframe.data[0];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 0x04: // 减速油门极限寄存器(Unit:%)(自动减速时油门极限)
|
|
|
|
|
if(PAD_daraframe.cmd_id == 0x03){// 读取
|
|
|
|
|
// ret_data[length++] =0;
|
|
|
|
|
ret_data[length++] =sys_conf.Speed_Cut_sLim;
|
|
|
|
|
// if(0 == --PAD_daraframe.length){
|
|
|
|
|
break;
|
|
|
|
|
// }
|
|
|
|
|
}else if(PAD_daraframe.cmd_id == 0x06){// 写入
|
|
|
|
|
sys_conf.Speed_Cut_sLim =PAD_daraframe.data[0];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 0x05: // 刹车距离极限寄存器(前进)(Unit:mm)
|
|
|
|
|
if(PAD_daraframe.cmd_id == 0x03){// 读取
|
|
|
|
|
ret_data[length++] =sys_conf.Brake_DLimit & 0x00FF;
|
|
|
|
|
ret_data[length++] =sys_conf.Brake_DLimit >> 8;
|
|
|
|
|
// if(0 == --PAD_daraframe.length){
|
|
|
|
|
break;
|
|
|
|
|
// }
|
|
|
|
|
}else if(PAD_daraframe.cmd_id == 0x06){// 写入
|
|
|
|
|
sys_conf.Brake_DLimit =(((uint16_t)PAD_daraframe.data[1]) << 8) | PAD_daraframe.data[0];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 0x06: // 减速距离极限寄存器(Unit:mm)
|
|
|
|
|
if(PAD_daraframe.cmd_id == 0x03){// 读取
|
|
|
|
|
ret_data[length++] =sys_conf.Speed_Cut_DLimit & 0x00FF;
|
|
|
|
|
ret_data[length++] =sys_conf.Speed_Cut_DLimit >> 8;
|
|
|
|
|
// if(0 == --PAD_daraframe.length){
|
|
|
|
|
break;
|
|
|
|
|
// }
|
|
|
|
|
}else if(PAD_daraframe.cmd_id == 0x06){// 写入
|
|
|
|
|
sys_conf.Speed_Cut_DLimit =(((uint16_t)PAD_daraframe.data[1]) << 8) | PAD_daraframe.data[0];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 0x07: // 刹车距离极限寄存器(前进)(Unit:mm)
|
|
|
|
|
if(PAD_daraframe.cmd_id == 0x03){// 读取
|
|
|
|
|
ret_data[length++] =sys_conf.Brake_DLimit_B & 0x00FF;
|
|
|
|
|
ret_data[length++] =sys_conf.Brake_DLimit_B >> 8;
|
|
|
|
|
// if(0 == --PAD_daraframe.length){
|
|
|
|
|
break;
|
|
|
|
|
// }
|
|
|
|
|
}else if(PAD_daraframe.cmd_id == 0x06){// 写入
|
|
|
|
|
sys_conf.Brake_DLimit_B =(((uint16_t)PAD_daraframe.data[1]) << 8) | PAD_daraframe.data[0];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 0x08: // 减速距离极限寄存器(Unit:mm)
|
|
|
|
|
if(PAD_daraframe.cmd_id == 0x03){// 读取
|
|
|
|
|
ret_data[length++] =sys_conf.Speed_Cut_DLimit_B & 0x00FF;
|
|
|
|
|
ret_data[length++] =sys_conf.Speed_Cut_DLimit_B >> 8;
|
|
|
|
|
// if(0 == --PAD_daraframe.length){
|
|
|
|
|
break;
|
|
|
|
|
// }
|
|
|
|
|
}else if(PAD_daraframe.cmd_id == 0x06){// 写入
|
|
|
|
|
sys_conf.Speed_Cut_DLimit_B = (((uint16_t)PAD_daraframe.data[1]) << 8) | PAD_daraframe.data[0];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 0x09: //PAD模式寄存器
|
|
|
|
|
if(PAD_daraframe.cmd_id == 0x03){// 读取
|
|
|
|
|
// ret_data[length++] =0;
|
|
|
|
|
ret_data[length++] =sys_sta.Pmode;
|
|
|
|
|
// if(0 == --PAD_daraframe.length){
|
|
|
|
|
break;
|
|
|
|
|
// }
|
|
|
|
|
}else if(PAD_daraframe.cmd_id == 0x06){// 写入
|
|
|
|
|
sys_sta.Pmode = PAD_daraframe.data[0] & 0x03;//0 退出模式//1 进入游客模式//2 进入管理员模式
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 0x0A: //系统模式寄存器
|
|
|
|
|
if(PAD_daraframe.cmd_id == 0x03){// 读取
|
|
|
|
|
// ret_data[length++] =0;
|
|
|
|
|
ret_data[length++] =sys_sta.Smode;
|
|
|
|
|
// if(0 == --PAD_daraframe.length){
|
|
|
|
|
break;
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
case 0x0B: //0 关闭12V输出//1 开启12V输出
|
|
|
|
|
if(PAD_daraframe.cmd_id == 0x03){// 读取
|
|
|
|
|
// ret_data[length++] =0;
|
|
|
|
|
ret_data[length++] =sys_sta.O_12V;
|
|
|
|
|
// if(0 == --PAD_daraframe.length){
|
|
|
|
|
break;
|
|
|
|
|
// }
|
|
|
|
|
}else if(PAD_daraframe.cmd_id == 0x06){// 写入
|
|
|
|
|
sys_sta.O_12V = PAD_daraframe.data[0] & 0x01;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-08-19 01:14:21 +08:00
|
|
|
|
break;
|
2024-08-19 08:46:40 +08:00
|
|
|
|
// default :
|
|
|
|
|
// app_PAD_Sendcmd(PAD_daraframe.cmd_id,PAD_daraframe.reg_addr,PAD_daraframe.data,PAD_daraframe.length);
|
2024-08-19 01:14:21 +08:00
|
|
|
|
// break;
|
|
|
|
|
}
|
2024-08-19 08:46:40 +08:00
|
|
|
|
app_PAD_Sendcmd(PAD_daraframe.cmd_id ,PAD_daraframe.reg_addr ,ret_data ,length);
|
2024-08-19 01:14:21 +08:00
|
|
|
|
Rec_state =0;//数据处理完要清零,准备接收下一帧数据
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void app_PAD_Receive(uint8_t data){
|
|
|
|
|
uint8_t *frame = (uint8_t *)&PAD_daraframe;
|
|
|
|
|
|
|
|
|
|
switch(Rec_state){
|
|
|
|
|
case 0://header
|
|
|
|
|
if ((data == (uint8_t)(FRAME_HEADER >> 8)) && (PAD_daraframe.header == 0x0)) {
|
|
|
|
|
PAD_daraframe.header = (FRAME_HEADER >> 8);
|
|
|
|
|
} else if ((data == (uint8_t)(FRAME_HEADER & 0x00FF)) && (PAD_daraframe.header == (uint8_t)(FRAME_HEADER >> 8))) {
|
|
|
|
|
PAD_daraframe.header = ((uint16_t)((FRAME_HEADER << 8)|(FRAME_HEADER >> 8)));//0xA100;
|
|
|
|
|
PAD_daraframe.length = 0;
|
|
|
|
|
Rec_state = 1;//to cmd id
|
|
|
|
|
//debug("header");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 1://cmd_id
|
|
|
|
|
//debug("rxcmd:%x", data);
|
|
|
|
|
PAD_daraframe.cmd_id = data;
|
|
|
|
|
Rec_state = 2;//to device addr
|
|
|
|
|
break;
|
|
|
|
|
case 2://reg addr
|
|
|
|
|
//debug("reg_addr");
|
|
|
|
|
PAD_daraframe.reg_addr = data;
|
|
|
|
|
Rec_state = 3;//to data length
|
|
|
|
|
break;
|
|
|
|
|
case 3://data length
|
|
|
|
|
//debug("length");
|
|
|
|
|
PAD_daraframe.length = (data < MAX_LEN) ? data : MAX_LEN;
|
|
|
|
|
if (PAD_daraframe.length == 0) {
|
|
|
|
|
Rec_state = 5;//to crc
|
|
|
|
|
} else {
|
|
|
|
|
Rec_state = 4;//to data
|
|
|
|
|
Rec_data_cnt = 0;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 4://data
|
|
|
|
|
//debug("data");
|
|
|
|
|
if (Rec_data_cnt < PAD_daraframe.length) {
|
|
|
|
|
PAD_daraframe.data[Rec_data_cnt++] = data;
|
|
|
|
|
}
|
|
|
|
|
if (Rec_data_cnt == PAD_daraframe.length) {
|
|
|
|
|
Rec_data_cnt = 0;
|
|
|
|
|
Rec_state = 5;//to crc
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 5://CRC8
|
|
|
|
|
PAD_daraframe.crc = crc8_maxim(frame, 5 + PAD_daraframe.length);
|
2024-08-19 08:46:40 +08:00
|
|
|
|
#if !PAD_CRC8_EN
|
2024-08-19 01:14:21 +08:00
|
|
|
|
PAD_daraframe.crc = data;
|
|
|
|
|
#endif
|
|
|
|
|
//计算并验证crc8
|
|
|
|
|
if (PAD_daraframe.crc == data) {
|
|
|
|
|
Rec_state = 6;//to ack
|
|
|
|
|
//debug("crc correct cmd:%x", PAD_daraframe.cmd_id);
|
|
|
|
|
} else {
|
|
|
|
|
Rec_state = 0;//to haeder
|
|
|
|
|
PAD_daraframe.header = 0;
|
|
|
|
|
//debug("crc error cmd:%x-%x", PAD_daraframe.cmd_id, PAD_daraframe.crc);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// uart_putc(UART1_PORT,data);//debug
|
|
|
|
|
// uart_putc(UART1_PORT,'0'+Rec_state);
|
|
|
|
|
if(Rec_state == 6){
|
|
|
|
|
PAD_daraframe.header = 0;
|
|
|
|
|
Rec_state = 7;//数据处理完,需要将状态位归零,才会接收下一帧数据,即:uart1_state =0时。
|
|
|
|
|
PAD_Data_Hande();
|
|
|
|
|
Rec_state =0;
|
|
|
|
|
/*
|
|
|
|
|
rx handler codes....
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//发送命令
|
|
|
|
|
void app_PAD_Sendcmd(volatile uint8_t cmd_id ,volatile uint8_t reg_addr ,volatile uint8_t *data, volatile uint8_t length){
|
|
|
|
|
PAD_daraframe_t frame;
|
|
|
|
|
uint8_t *cmd_data =(uint8_t *)&frame;
|
|
|
|
|
uint8_t i;
|
|
|
|
|
|
|
|
|
|
frame.header = ((uint16_t)((FRAME_HEADER << 8)|(FRAME_HEADER >> 8)));//0xA100
|
|
|
|
|
frame.cmd_id = cmd_id;
|
|
|
|
|
frame.reg_addr = reg_addr;
|
|
|
|
|
frame.length = length < MAX_LEN ? length:MAX_LEN;
|
|
|
|
|
|
|
|
|
|
for(i = 0;i < frame.length ;i ++){
|
|
|
|
|
frame.data[i] = * data++;
|
|
|
|
|
}
|
|
|
|
|
length =5 + frame.length;
|
|
|
|
|
frame.crc = crc8_maxim(cmd_data, length);
|
|
|
|
|
while(length--)
|
|
|
|
|
uart_putc(UART1_PORT, (uint32_t)(*cmd_data ++));
|
|
|
|
|
uart_putc(UART1_PORT, (uint32_t)(frame.crc));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|