| | |
| | | memcpy(&send_frame[8],&set_mk_time,2); |
| | | checksum=Checksum_u16(&send_frame[2],8); |
| | | memcpy(&send_frame[10],&checksum,2); |
| | | //LOG_INFO(TRACE_MODULE_APP,"发给ble的checksum %#x\r\n",checksum); |
| | | uart_send(UART_ID0, send_frame,12, NULL); |
| | | uart_send(UART_ID1, send_frame,12, NULL); |
| | | } |
| | | extern uint8_t bat_percent; |
| | | extern uint32_t dev_id; |
| | | |
| | | void Usart0ParseDataHandler(uint8_t data)//UART蓝牙数据分析处理 |
| | | { |
| | | // // 全局变量 |
| | | //static Usart0ReceiveState usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | // 全局变量 |
| | | static Usart0ReceiveState usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | |
| | | //static uint8_t index = 0; // 缓冲区索引 |
| | | //static uint8_t pack_cmd_type = 0; // 指令类型 |
| | | //static uint8_t pack_msg_type = 0; // 消息类型 |
| | | //static uint8_t pack_data_len = 0; // 数据长度 |
| | | //static uint8_t pack_param_addr = 0; // 参数地址 |
| | | //static uint16_t pack_checksum = 0; // 接收到的校验和 |
| | | //static uint16_t calculated_checksum = 0; // 计算得到的校验和 |
| | | //static uint8_t send_frame[256]; // 用于计算校验和的帧 |
| | | //static uint8_t frame_index = 0; // 帧缓冲区索引 |
| | | //static uint8_t pack_datalen = 0; |
| | | // |
| | | // LOG_INFO(TRACE_MODULE_APP,"收到 %#x\r\n",data); |
| | | // switch(usart0_receive_state) { |
| | | // case Usart0ReceiveWaitHead0: |
| | | // if(data == 0x55) { |
| | | // usart0_receive_state = Usart0ReceiveWaitHead1; |
| | | // // 初始化帧缓冲区 |
| | | // frame_index = 0; |
| | | // send_frame[frame_index++] = data; |
| | | // } |
| | | // break; |
| | | // |
| | | // case Usart0ReceiveWaitHead1: |
| | | // if(data == 0xAA) { |
| | | // usart0_receive_state = Usart0ReceiveWaitCmdType; |
| | | // send_frame[frame_index++] = data; |
| | | // } else { |
| | | // usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | // } |
| | | // break; |
| | | // |
| | | // case Usart0ReceiveWaitCmdType: |
| | | // if(data == 64) { |
| | | // pack_cmd_type = data; |
| | | // usart0_receive_state = Usart0ReceiveWaitMsgType; |
| | | // send_frame[frame_index++] = data; |
| | | // } else { |
| | | // usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | // } |
| | | // break; |
| | | // |
| | | // case Usart0ReceiveWaitMsgType: |
| | | // if(data == 0x2||data==0x1) { // 读写回复消息类型 |
| | | // pack_msg_type = data; |
| | | // usart0_receive_state = Usart0ReceiveWaitDataLen; |
| | | // send_frame[frame_index++] = data; |
| | | // } else { |
| | | // usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | // } |
| | | // break; |
| | | // |
| | | // case Usart0ReceiveWaitDataLen: |
| | | // pack_data_len = data; |
| | | // index = 0; |
| | | // usart0_receive_state = Usart0ReceiveWaitData; |
| | | static uint8_t index = 0; // 缓冲区索引 |
| | | static uint8_t pack_cmd_type = 0; // 指令类型 |
| | | static uint8_t pack_msg_type = 0; // 消息类型 |
| | | static uint8_t pack_data_len = 0; // 数据长度 |
| | | static uint8_t pack_param_addr = 0; // 参数地址 |
| | | static uint16_t pack_checksum = 0; // 接收到的校验和 |
| | | static uint16_t calculated_checksum = 0; // 计算得到的校验和 |
| | | static uint8_t send_frame[256]; // 用于计算校验和的帧 |
| | | static uint8_t frame_index = 0; // 帧缓冲区索引 |
| | | static uint8_t pack_datalen = 0; |
| | | |
| | | LOG_INFO(TRACE_MODULE_APP,"收到 %#x\r\n",data); |
| | | switch(usart0_receive_state) { |
| | | case Usart0ReceiveWaitHead0: |
| | | if(data == 0x55) { |
| | | usart0_receive_state = Usart0ReceiveWaitHead1; |
| | | // 初始化帧缓冲区 |
| | | frame_index = 0; |
| | | send_frame[frame_index++] = data; |
| | | } |
| | | break; |
| | | |
| | | case Usart0ReceiveWaitHead1: |
| | | if(data == 0xAA) { |
| | | usart0_receive_state = Usart0ReceiveWaitCmdType; |
| | | send_frame[frame_index++] = data; |
| | | } else { |
| | | usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | } |
| | | break; |
| | | |
| | | case Usart0ReceiveWaitCmdType: |
| | | if(data == 64) { |
| | | pack_cmd_type = data; |
| | | usart0_receive_state = Usart0ReceiveWaitMsgType; |
| | | send_frame[frame_index++] = data; |
| | | } else { |
| | | usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | } |
| | | break; |
| | | |
| | | case Usart0ReceiveWaitMsgType: |
| | | if(data == 0x2||data==0x1) { // 读写回复消息类型 |
| | | pack_msg_type = data; |
| | | usart0_receive_state = Usart0ReceiveWaitDataLen; |
| | | send_frame[frame_index++] = data; |
| | | } else { |
| | | usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | } |
| | | break; |
| | | |
| | | case Usart0ReceiveWaitDataLen: |
| | | pack_data_len = data; |
| | | index = 0; |
| | | usart0_receive_state = Usart0ReceiveWaitData; |
| | | send_frame[frame_index++] = data; |
| | | break; |
| | | |
| | | // case Usart0ReceiveWaitParamAddr: |
| | | // pack_param_addr = data; |
| | | // if(pack_msg_type==CMD_READ) |
| | | // { |
| | | // usart0_receive_state = Usart0ReceiveWaitChecksum1; |
| | | // pack_datalen=pack_data_len-6; |
| | | // }else{ |
| | | // usart0_receive_state = Usart0ReceiveWaitData; |
| | | // } |
| | | // send_frame[frame_index++] = data; |
| | | // break; |
| | | // |
| | | //// case Usart0ReceiveWaitParamAddr: |
| | | //// pack_param_addr = data; |
| | | //// if(pack_msg_type==CMD_READ) |
| | | //// { |
| | | //// usart0_receive_state = Usart0ReceiveWaitChecksum1; |
| | | //// pack_datalen=pack_data_len-6; |
| | | //// }else{ |
| | | //// usart0_receive_state = Usart0ReceiveWaitData; |
| | | //// } |
| | | //// send_frame[frame_index++] = data; |
| | | //// break; |
| | | // |
| | | // case Usart0ReceiveWaitData: |
| | | // send_frame[frame_index++] = data; |
| | | // mUsart2ReceivePack[index++] = data; |
| | | // |
| | | // if(index == pack_data_len-3) { |
| | | // usart0_receive_state = Usart0ReceiveWaitChecksum1; |
| | | // //pack_datalen=pack_data_len; |
| | | // } |
| | | // break; |
| | | // |
| | | // case Usart0ReceiveWaitChecksum1: |
| | | // pack_checksum = data; // 高字节 |
| | | // pack_checksum_test = data; // 高字节 |
| | | // send_frame[frame_index++] = data; |
| | | // usart0_receive_state = Usart0ReceiveWaitChecksum2; |
| | | // LOG_INFO(TRACE_MODULE_APP,"收到校验1 %#x\r\n",data); |
| | | // break; |
| | | // case Usart0ReceiveWaitChecksum2: |
| | | // pack_checksum |= data<<8; // 低字节 |
| | | // pack_checksum_test |= data<<8; // 低字节 |
| | | // LOG_INFO(TRACE_MODULE_APP,"收到校验2 %#x\r\n",data); |
| | | // // 计算校验和 (从第3个字节开始,长度为4+数据长度) |
| | | //// if(pack_msg_type!=CMD_REPLY) |
| | | //// { |
| | | // calculated_checksum = Checksum_u16(&send_frame[2],pack_data_len); |
| | | //// }else{ |
| | | //// //calculated_checksum = Checksum_u16(&send_frame[2],4); |
| | | //// |
| | | //// } |
| | | // test_calculate=calculated_checksum; |
| | | // LOG_INFO(TRACE_MODULE_APP,"收到校验 %#x,%#x\r\n",calculated_checksum,pack_checksum); |
| | | // // 验证校验和 |
| | | // if(pack_checksum == calculated_checksum) { |
| | | // // 校验通过,处理数据 |
| | | // switch(pack_msg_type) |
| | | // { |
| | | // case CMD_BLE_TO_MK: |
| | | // memcpy(&dev_id,&mUsart2ReceivePack[0],2); |
| | | // bat_percent=mUsart2ReceivePack[2]; |
| | | // memcpy(&set_mk_time,&mUsart2ReceivePack[3],2); |
| | | // memcpy(&g_com_map[DEV_ID],&dev_id,2); |
| | | // memcpy(&g_com_map[SET_START_TIME],&set_mk_time,2); |
| | | // Send_Reply_To_Ble(); |
| | | // LOG_INFO(TRACE_MODULE_APP,"收到蓝牙数据 set_mk_time %d,dev_id %04x,bat_percent %d\r\n",set_mk_time,dev_id,bat_percent); |
| | | // save_com_map_to_flash(); |
| | | // //memcpy((uint8_t*)&g_com_map + pack_param_addr*2, mUsart2ReceivePack, pack_datalen); |
| | | // |
| | | |
| | | case Usart0ReceiveWaitData: |
| | | send_frame[frame_index++] = data; |
| | | mUsart2ReceivePack[index++] = data; |
| | | |
| | | if(index == pack_data_len-3) { |
| | | usart0_receive_state = Usart0ReceiveWaitChecksum1; |
| | | //pack_datalen=pack_data_len; |
| | | } |
| | | break; |
| | | |
| | | case Usart0ReceiveWaitChecksum1: |
| | | pack_checksum = data; // 高字节 |
| | | pack_checksum_test = data; // 高字节 |
| | | send_frame[frame_index++] = data; |
| | | usart0_receive_state = Usart0ReceiveWaitChecksum2; |
| | | LOG_INFO(TRACE_MODULE_APP,"收到校验1 %#x\r\n",data); |
| | | break; |
| | | case Usart0ReceiveWaitChecksum2: |
| | | pack_checksum |= data<<8; // 低字节 |
| | | pack_checksum_test |= data<<8; // 低字节 |
| | | LOG_INFO(TRACE_MODULE_APP,"收到校验2 %#x\r\n",data); |
| | | // 计算校验和 (从第3个字节开始,长度为4+数据长度) |
| | | // if(pack_msg_type!=CMD_REPLY) |
| | | // { |
| | | calculated_checksum = Checksum_u16(&send_frame[2],pack_data_len); |
| | | // }else{ |
| | | // //calculated_checksum = Checksum_u16(&send_frame[2],4); |
| | | // |
| | | // } |
| | | test_calculate=calculated_checksum; |
| | | LOG_INFO(TRACE_MODULE_APP,"收到校验 %#x,%#x\r\n",calculated_checksum,pack_checksum); |
| | | // 验证校验和 |
| | | if(pack_checksum == calculated_checksum) { |
| | | // 校验通过,处理数据 |
| | | switch(pack_msg_type) |
| | | { |
| | | case CMD_BLE_TO_MK: |
| | | memcpy(&dev_id,&mUsart2ReceivePack[0],2); |
| | | bat_percent=mUsart2ReceivePack[2]; |
| | | memcpy(&set_mk_time,&mUsart2ReceivePack[3],2); |
| | | memcpy(&g_com_map[DEV_ID],&dev_id,2); |
| | | memcpy(&g_com_map[SET_START_TIME],&set_mk_time,2); |
| | | Send_Reply_To_Ble(); |
| | | LOG_INFO(TRACE_MODULE_APP,"收到蓝牙数据 set_mk_time %d,dev_id %04x,bat_percent %d\r\n",set_mk_time,dev_id,bat_percent); |
| | | save_com_map_to_flash(); |
| | | //memcpy((uint8_t*)&g_com_map + pack_param_addr*2, mUsart2ReceivePack, pack_datalen); |
| | | |
| | | |
| | | break; |
| | | // case CMD_MK_TO_BLE: |
| | | // //uart0_send_ComMap_to_BLE(pack_datalen,pack_param_addr); |
| | | // |
| | | // break; |
| | | //// case CMD_MK_TO_BLE: |
| | | //// //uart0_send_ComMap_to_BLE(pack_datalen,pack_param_addr); |
| | | //// |
| | | //// case CMD_REPLY: |
| | | //// memcpy((uint8_t*)&g_com_map + pack_param_addr, mUsart2ReceivePack, pack_datalen); |
| | | //// save_com_map_to_flash(); |
| | | //// break; |
| | | ////// case CMD_REPLY: |
| | | ////// memcpy((uint8_t*)&g_com_map + pack_param_addr, mUsart2ReceivePack, pack_datalen); |
| | | ////// save_com_map_to_flash(); |
| | | ////// break; |
| | | // default: |
| | | // break; |
| | | // } |
| | | // } |
| | | // |
| | | // // 重置状态机 |
| | | // usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | // index = 0; |
| | | // frame_index = 0; |
| | | // break; |
| | | // |
| | | // default: |
| | | // usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | // index = 0; |
| | | // frame_index = 0; |
| | | // break; |
| | | // } |
| | | default: |
| | | break; |
| | | } |
| | | } |
| | | |
| | | // 重置状态机 |
| | | usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | index = 0; |
| | | frame_index = 0; |
| | | break; |
| | | |
| | | default: |
| | | usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | index = 0; |
| | | frame_index = 0; |
| | | break; |
| | | } |
| | | |
| | | } |