MAX_CARLINK_A270S/MXC_A27-PCB4.5-270S/app/moto/protocol/user_protocol.c

640 lines
15 KiB
C
Raw Permalink Normal View History

2025-01-21 16:49:37 +08:00
#include "awtk.h"
#include "user_protocol.h"
#include "gpio_protocol.h"
#include "moto_config.h"
#include "data_port.h"
#include "FreeRTOS.h"
#include "chip.h"
#include "board.h"
#include "keypad.h"
#include "moto_adc.h"
extern double total_mile;
extern uint32_t flash_trip_mile;
extern uint32_t flash_total_mile;
uint8_t data_error_flag = 0;
#define KEY_DATA_PRESS 0x01 // 短按
#define KEY_DATA_LONG_PRESS 0x02 // 长按
enum
{
LV_KEY_OPTION = 113, // 0x11
LV_KEY_SELECT = 119, // 0x12
LV_KEY_OTHER1 = 122, // 0x12
};
uint8_t save_data = 0;
#define ADC_VPLTAGE_MIN 1666
#define ADC_VPLTAGE_MAX 2486
int adc_light = 0;
int adc_voltage = 0;
extern uint32_t fml_stamp_to_time(uint32_t timep , uint32_t time[]);
extern uint8_t light_buffer[];
extern double cell_buffer[];
extern uint8_t data_storage;
extern uint32_t tire_front_time;
extern uint32_t tire_rear_time;
uint8_t flash_flag = 1;//收到device信息标志后置为0
// 封装的函数,根据位位置获取对应的值
uint8_t getBitValue(uint8_t count, int bitPosition) {
return (count >> bitPosition) & 0x1;
}
// 封装的函数获取2个bit组合成的值
uint8_t getBit2Value(uint8_t count, int bitPosition) {
return (count >> bitPosition) & 0x3;
}
// 封装的函数获取3个bit组合成的值
uint8_t getBit3Value(uint8_t count, int bitPosition) {
return (count >> bitPosition) & 0x7;
}
// 封装的函数获取4个bit组合成的值
uint8_t getBit4Value(uint8_t count, int bitPosition) {
return (count >> bitPosition) & 0xf;
}
2025-02-11 18:48:14 +08:00
// 封装的函数获取5个bit组合成的值
uint8_t getBit5Value(uint8_t count, int bitPosition) {
return (count >> bitPosition) & 0x1f;
}
2025-01-21 16:49:37 +08:00
double adc_voltage_calculation(void){
double value = 0;
if(adc_voltage<=ADC_VPLTAGE_MIN)
value = 11.0;
else if(adc_voltage>=ADC_VPLTAGE_MAX)
value = 16.5;
else
value = (adc_voltage - ADC_VPLTAGE_MIN) * (16.5 - 11.0) / (ADC_VPLTAGE_MAX - ADC_VPLTAGE_MIN) + 11.0;
// printf("adc_voltage = %d value = %lf.\r\n",adc_voltage,value);
return value;
}
extern uint8_t send_flag;
//MOTO通讯 设备信息+时间戳解析协议
void device_data_analysis(uint8_t *buf){
printf("device information............................................\r\n");
uint8_t data;
uint8_t sum1,sum2,sum3,sum4;
uint32_t save_total_mile,save_trip_mile;
uint32_t sum;
int i=0;
char equipment[16] = {0};
char produce[5] = {0};
char mac[6] = {0};
data = *(buf++);
// if(data!=0xAA)
// return;
data = *(buf++);
// if(data!=0x55)
// return;
//设备相关信息
data = *(buf++);//0x01
data = *(buf++);//0x1C 总长度22+6mac
//设备编号占16
DEBUG_PRINT("equipment > ");
for(i=0;i<16;i++){
data = *(buf++);//0x16
DEBUG_PRINT("%02x ",data);
equipment[i] = data;
}
DEBUG_PRINT("\n");
Set_device_equipment_num(equipment);
//产品型号占5
DEBUG_PRINT("produce > ");
for(i=0;i<5;i++){
data = *(buf++);
DEBUG_PRINT("%02x ",data);
produce[i] = data;
}
DEBUG_PRINT("\n");
Set_device_produce_num(produce);
//客户编号
data = *(buf++);
DEBUG_PRINT("client > %02x \n",data);
Set_device_client_num(data);
//mac占6
DEBUG_PRINT("mac > ");
for(i=0;i<6;i++){
data = *(buf++);
DEBUG_PRINT("%02x ",data);
mac[i] = data;
}
DEBUG_PRINT("\n");
Set_device_mac(mac);
//平台信息
data = *(buf++);//0x02
//DEBUG_PRINT("0x02 -- %02x .\n",data);
data = *(buf++);//0x03
//DEBUG_PRINT("0x03 -- %02x .\n",data);
//BottomData platform[3];
data = *(buf++);//平台ID
DEBUG_PRINT("bottom_id > %02x \n",data);
Set_device_bottom_id(data);
data = *(buf++);//主版本
DEBUG_PRINT("bottom_softwar_host > %02x \n",data);
Set_device_bottom_softwar_host(data);
data = *(buf++);//次版本
DEBUG_PRINT("bottom_softwar_order > %02x \n",data);
Set_device_bottom_softwar_order(data);
//Set_device_bottom_data(platform);
//平台认证
data = *(buf++);//0x03
//DEBUG_PRINT("0x03 -- %02x .\n",data);
data = *(buf++);//0x14
//DEBUG_PRINT("0x14 -- %02x .\n",data);
DEBUG_PRINT("voucher > ");
char voucher[20] = {0};
for(i=0;i<20;i++){
data = *(buf++);
DEBUG_PRINT("%02x ",data);
voucher[i] = data;
}
DEBUG_PRINT("\n");
Set_device_voucher(voucher);
//同步时间
data = *(buf++);//0x04
//DEBUG_PRINT("0x04 -- %02x .\n",data);
data = *(buf++);//0x04
//DEBUG_PRINT("0x04 -- %02x .\n",data);
//DEBUG_PRINT("time stamp >> ");
int time_transfer[6];
sum1 = *(buf++);
sum2 = *(buf++);
sum3 = *(buf++);
sum4 = *(buf++);
//sum = sum1 | sum2<<8 | sum3<<16 | sum4<<24;
sum = sum1<<24 | sum2<<16 | sum3<<8 | sum4;
DEBUG_PRINT("time > %08x \n",sum);
tire_rear_time = sum;
tire_front_time = sum;
// if(sum<1706198400)
// sum = 1706198400;
fml_stamp_to_time(sum,time_transfer);
Uart_set_time(time_transfer);//去设置时间并不再发送询问时间指令
//Set_sys_ttrb_time(10);
data = *(buf++);//0x05
//DEBUG_PRINT("0x05 -- %02x .\n",data);
data = *(buf++);//0x02
//DEBUG_PRINT("0x02 -- %02x .\n",data);
data = *(buf++);
DEBUG_PRINT("device_moto_type %02x \n",data);
Set_device_moto_type(data);
data = *(buf++);
DEBUG_PRINT("device_screen_type %02x \n",data);
Set_device_screen_type(data);
data = *(buf++);//0x06
//DEBUG_PRINT("0x06 -- %02x .\n",data);
data = *(buf++);//0x01
//DEBUG_PRINT("0x01 -- %02x .\n",data);
data = *(buf++);
DEBUG_PRINT("device_ability %02x .\n",data);
Set_device_ability(data);
data = *(buf++);//0x07
//DEBUG_PRINT("0x06 -- %02x .\n",data);
data = *(buf++);//0x02
//DEBUG_PRINT("0x01 -- %02x .\n",data);
data = *(buf++);//硬件主版本
Set_device_hardware_version_host(data);
data = *(buf++);//硬件次版本
Set_device_hardware_version_order(data);
data = *(buf++);//0x08
//DEBUG_PRINT("0x06 -- %02x .\n",data);
data = *(buf++);//0x08
//DEBUG_PRINT("0x01 -- %02x .\n",data);
#ifdef DATA_CAN
//里程
sum = 0;
sum1 = *(buf++)&0xFF;
sum2 = *(buf++)&0xFF;
sum3 = *(buf++)&0xFF;
sum4 = *(buf++)&0xFF;
sum = sum1<<24 | sum2<<16 | sum3<<8 | sum4;
save_trip_mile =sum;
//小计里程
sum = 0;
sum = 0;
sum1 = *(buf++)&0xFF;
sum2 = *(buf++)&0xFF;
sum3 = *(buf++)&0xFF;
sum4 = *(buf++)&0xFF;
sum = sum1<<24 | sum2<<16 | sum3<<8 | sum4;
save_total_mile =sum;
printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\r\n");
printf("Power outage trip mile =%d , total mile =%d .\n",save_trip_mile,save_total_mile);
if(save_trip_mile>save_total_mile)
save_trip_mile = 0;
Set_sys_trip_mileage(save_trip_mile);
Set_sys_total_mileage(save_total_mile);
if(save_total_mile < flash_trip_mile){
flash_trip_mile = save_total_mile;
Set_sys_trip_mileage(save_total_mile);
}
flash_trip_mile = save_trip_mile;
flash_total_mile = save_total_mile;
#endif
total_mile = 0;
flash_flag=0;
data_error_flag = 0;
flash_flag=0;
send_flag = 0;
}
int test_light = 0;
extern uint8_t bt_communication_heartbeat;//时间检测
//MOTO通讯协议
void data_analysis(uint8_t *buf){
// printf("data_analysis-------------.\r\n");
uint32_t data;
uint32_t sum,sum1,sum2,sum3,sum4;
volatile double value=0;
uint8_t result = 0;
//一些标志
uint8_t temp = 0;
uint8_t oil = 0;
uint8_t light = 0;
uint8_t data_type = 0;
double cell = 0;
save_data = 0;
data = *(buf++);
// if(data!=0xAA && data!=0xAA)
// return;
data_type = *(buf++);
// if(data_type!=0x11 && data_type!=0x19)
// return;
//checklen=data;
data = *(buf++);
// if(data!=0x03)
// return;
data = *(buf++);
// if(data!=0x50)
// return;
data = *(buf++);
// if(data!=0x0D && data!=0x15)
// return;
//按键 高set 低mod
data = *(buf++);
2025-02-11 18:48:14 +08:00
// #ifdef KEY_EXCHANGE
// if(JUDGE_BIN_4(data))
// Key_Distinction(KEY_DATA_PRESS,LV_KEY_OPTION);
// else if(JUDGE_BIN_5(data))
// Key_Distinction(KEY_DATA_LONG_PRESS,LV_KEY_OPTION);
// else if(JUDGE_BIN_0(data))
// Key_Distinction(KEY_DATA_PRESS,LV_KEY_SELECT);
// else if(JUDGE_BIN_1(data))
// Key_Distinction(KEY_DATA_LONG_PRESS,LV_KEY_SELECT);
// else if(JUDGE_BIN_7(data)){
// Key_Distinction(KEY_DATA_PRESS,LV_KEY_OTHER1);
// }
// #else
// if(JUDGE_BIN_0(data))
// Key_Distinction(KEY_DATA_PRESS,LV_KEY_OPTION);
// else if(JUDGE_BIN_1(data))
// Key_Distinction(KEY_DATA_LONG_PRESS,LV_KEY_OPTION);
// else if(JUDGE_BIN_4(data))
// Key_Distinction(KEY_DATA_PRESS,LV_KEY_SELECT);
// else if(JUDGE_BIN_5(data))
// Key_Distinction(KEY_DATA_LONG_PRESS,LV_KEY_SELECT);
// else if(JUDGE_BIN_7(data)){
// Key_Distinction(KEY_DATA_PRESS,LV_KEY_OTHER1);
// }
// #endif
2025-01-21 16:49:37 +08:00
//速度
data = *(buf++);
#ifndef DATA_CAN
Set_sys_velocity(data);
#endif
//if(data>5&& abs_data!=1)
//abs_data = 1;
//转速
sum = 0;
sum = *(buf++)&0xFF;
//sum = (*(buf++)&0xFF)<<8 | sum;
sum = (*(buf++)&0xFF) | sum<<8;
#ifndef DATA_CAN
Set_sys_veer_velocity(sum);
#endif
if(data_type==0x19){
//里程
sum = 0;
sum1 = *(buf++)&0xFF;
sum2 = *(buf++)&0xFF;
sum3 = *(buf++)&0xFF;
sum4 = *(buf++)&0xFF;
//sum = sum1 | sum2<<8 | sum3<<16 | sum4<<24;
sum = sum1<<24 | sum2<<16 | sum3<<8 | sum4;
// value = (int)(sum/100);
// value = value/10;
Set_sys_trip_mileage(sum);
//小计里程
sum = 0;
sum = 0;
sum1 = *(buf++)&0xFF;
sum2 = *(buf++)&0xFF;
sum3 = *(buf++)&0xFF;
sum4 = *(buf++)&0xFF;
//sum = sum1 | sum2<<8 | sum3<<16 | sum4<<24;
sum = sum1<<24 | sum2<<16 | sum3<<8 | sum4;
/*sum = *(buf++)&0xFF;
sum = (*(buf++)&0xFF)<<8 | sum;
sum = (*(buf++)&0xFF)<<16 | sum;
sum = (*(buf++)&0xFF)<<24 | sum;*/
//sum = (int)sum/1000;
Set_sys_total_mileage(sum);
}
//灯
data = *(buf++);
switch(data%4){
case 0:Set_sys_veer(0);break;
case 1:Set_sys_veer(2);break;
case 2:Set_sys_veer(1);break;
case 3:Set_sys_veer(3);break;
default:break;
}
if(JUDGE_BIN_2(data))
Set_sys_lamplight(1);//远光灯
else
Set_sys_lamplight(0);//关闭远光灯
//风扇灯
if(JUDGE_BIN_3(data))
Set_sys_warning_fan(1);//风扇
else
Set_sys_warning_fan(0);//关闭风扇
//abs故障灯
if(JUDGE_BIN_4(data))
Set_sys_warning_abs(1);//abs故障灯
else
Set_sys_warning_abs(0);//关闭abs
#ifndef DATA_CAN
//tcs图标
if(JUDGE_BIN_5(data)){
Set_sys_warning_tcs(1);//tcs图标
}else{
Set_sys_warning_tcs(0);//tcs图标
}
#else
//机油压力报警
if(JUDGE_BIN_5(data)){
Set_sys_warning_machine_oil(1);//tcs图标
}else{
Set_sys_warning_machine_oil(0);//tcs图标
}
#endif
#ifndef DATA_CAN
if(JUDGE_BIN_6(data))
Set_sys_warning_engine(1);//引擎报警灯
else
Set_sys_warning_engine(0);//关闭引擎报警灯
#endif
if(JUDGE_BIN_7(data))
save_data = 1;//保存数据的标志
//档位
data = *(buf++);
2025-02-11 18:48:14 +08:00
// if(data == 0){
// Set_sys_grade(0);
// }else if(data < 8){
// Set_sys_grade(data);
// }
2025-01-21 16:49:37 +08:00
//油量
oil = *(buf++);
//温度
temp = *(buf++);
//电池电压
value = 0;
sum = *(buf++)&0xFF;
//value = (*(buf++)&0xFF)<<8 | sum;
value = (*(buf++)&0xFF) | sum<<8;
cell = value/1000;
//光感阻值 测试范围57-1000
sum = 0;
sum = *(buf++)&0xFF;
sum = (*(buf++)&0xFF) | sum<<8;
Set_sys_sensor_light(sum);
if(sum<200)
light = sum/2;
else
light = 100;
data = *(buf++);
// if(data)
// printf("data = %d.\r\n",data);
result = ((data >> 5) & 0x03);
#ifndef DATA_CAN
if(JUDGE_BIN_0(data)){//收到小计清零,不再发送
DEBUG_PRINT("Received subtotal reset, no more sending.\n");
Set_sys_odo_reset(10);
}
#else
if(JUDGE_BIN_7(data)){//收到关机保存指令(停用)//小计清零,不再发送
DEBUG_PRINT("Received subtotal reset, no more sending.\n");
Set_sys_odo_reset(10);
}
if(JUDGE_BIN_0(data)){//收到蓝牙升级信号不发送can数据
DEBUG_PRINT("Receive Bluetooth upgrade signal and do not send CAN data.\n");
Set_sys_bt_upgrade(1);
}
#endif
if(JUDGE_BIN_1(data)){//需要询问时间
DEBUG_PRINT("start time select.\n");
Set_sys_ttrb(0);
}
if(JUDGE_BIN_2(data)){//收到故障码,不再发送
DEBUG_PRINT("Received fault code, no longer sending.\n");
Set_sys_fault_code(0);
}
if(JUDGE_BIN_3(data)){//收到时间,不再发送
DEBUG_PRINT("Received time, no longer sending.\n");
Set_sys_ttrb_time(10);//630发送时间给蓝牙后蓝牙回复收到信号
}
if(JUDGE_BIN_4(data)){//收到胎压信息,不再发送
DEBUG_PRINT("Received tire pressure command, no longer sending.\n");
Set_sys_tire_pressure_mesg(10);
}
#ifdef DATA_CAN
switch(result){
case 1:
printf("result 1> start time select.\r\n");
Set_sys_ttrb(0);
break;
case 2:
printf("result 2> start time select.\r\n");
Set_sys_ttrb(0);
break;
default:
break;
}
#endif
if(save_data){
// printf("save_data.\r\n");
bt_communication_heartbeat = 3;
Set_sys_gas(oil);
Set_sys_temp(temp);
Set_sys_voltage(cell);
light_buffer[data_storage] = light;
data_storage++;
if(data_storage>=SAVE_DATA_SIZE){//满足存储大小 计算均值
data_storage=0;
}
}
}
#ifndef DATA_CAN
void SendDataToBT(uint8_t *pBuff){
printf("Send>6.\r\n");
memset(pBuff, 0, 6); //buff初始化
uint8_t Tag_length = 4; //包头偏移4个字节
static uint8_t heartbeat_flag=0;
//帧头部分
pBuff[0] = 0xAA;
pBuff[1] = 0x03;
pBuff[2] = 0x06;
pBuff[3] = 0x01;
pBuff[4] = heartbeat_flag;
uint8_t count = 0;
for(uint8_t i=0;i<5;i++){
count += pBuff[i+2];
}
pBuff[5] = count & 0xFF;
// printf("write = %d.",heartbeat_flag);
heartbeat_flag++;
// return pBuff;
}
#else
uint8_t Lights_required_for_converting_to_BT(void){
uint8_t value = 0;
//刷新标志
//if(Get_sys_warning_tcs())
//tcs
if(Get_sys_warning_tcs())
value += 1;
//引擎
if(Get_sys_warning_engine())
value += 2;
return value;
}
void SendDataToBT(uint8_t *pBuff){
// printf("Send>21.\r\n");
memset(pBuff, 0, 21); //buff初始化
uint8_t Tag_length = 4; //包头偏移4个字节
uint32_t buf32_value;
uint16_t buf16_value;
uint8_t buf8_value;
// uint16_t checksum;
//帧头部分
pBuff[0] = 0xAA;
pBuff[1] = 0x12;
pBuff[2] = 0x04;
pBuff[3] = 0x10;
//数据部分
buf16_value = Get_sys_velocity();
pBuff[Tag_length+0] = buf16_value & 0xFF;
buf16_value = Get_sys_veer_velocity();
pBuff[Tag_length+1] = (buf16_value >> 8) & 0xFF;
pBuff[Tag_length+2] = buf16_value & 0xFF;
buf8_value = Lights_required_for_converting_to_BT();
pBuff[Tag_length+3] = buf8_value &0xFF;
if(Get_sys_trip_mileage() > Get_sys_total_mileage()){
extern void Send_software_version(void);
Send_software_version();
data_error_flag = 1;
return;
}
buf32_value = Get_sys_trip_mileage();
// printf("buf trip =%d,",Get_sys_trip_mileage());
pBuff[Tag_length+4] = (buf32_value >> 24) & 0xFF;
pBuff[Tag_length+5] = (buf32_value >> 16) & 0xFF;
pBuff[Tag_length+6] = (buf32_value >> 8) & 0xFF;
pBuff[Tag_length+7] = buf32_value & 0xFF;
buf32_value = Get_sys_total_mileage();
// printf("buf odo =%d.",Get_sys_total_mileage());
pBuff[Tag_length+8] = (buf32_value >> 24) & 0xFF;
pBuff[Tag_length+9] = (buf32_value >> 16) & 0xFF;
pBuff[Tag_length+10] = (buf32_value >> 8) & 0xFF;
pBuff[Tag_length+11] = buf32_value & 0xFF;
buf16_value = Get_sys_now_defect_code();
pBuff[Tag_length+12] = (buf16_value >> 8) & 0xFF;
pBuff[Tag_length+13] = buf16_value & 0xFF;
buf16_value = Get_sys_his_defect_code();
pBuff[Tag_length+14] = (buf16_value >> 8) & 0xFF;
pBuff[Tag_length+15] = buf16_value & 0xFF;
// checksum = bt_sumcrc(pBuff,21);
uint8_t count = 0;
for(uint8_t i=0;i<18;i++){
count += pBuff[i+2];
// printf("pBuff = %x .\r\n",pBuff[i+2]);
}
pBuff[Tag_length+16] = count & 0xFF;
// return pBuff;
}
#endif