| | |
| | | #define Lora_TXD_bff_MAX 220 //发送缓存区大小 >4+8+4*基站数量+2=14+4*基站数量 |
| | | |
| | | uint8_t mUsartReceivePack[100] = {0}; |
| | | uint8_t mUsart2ReceivePack[150] = {0}; |
| | | double jd,wd; |
| | | int analysis_num,pos_state; |
| | | uint8_t state5V_prase_flag,gps_prase_flag=1; |
| | | extern void IO_LED_control_change(uint8_t data); |
| | | extern void IO_control_init(void); |
| | | extern void blink_led(uint8_t*state); |
| | | extern void updata_led_power_state(void); |
| | | extern uint8_t gps_success_state; |
| | | extern uint8_t gps_success_state,gps_enable_flag,gps_need_data_flag,gps_timeout_flag; |
| | | extern uint16_t gps_wait_count; |
| | | uint8_t mUsart2ReceivePack[100] = {0}; |
| | | |
| | | typedef enum |
| | | { |
| | | BLE_RECV_STATE_IDLE = 0, |
| | |
| | | BLE_RECV_STATE_RSSI, |
| | | //BLE_RECV_STATE_LF, |
| | | } E_BLERecvState; |
| | | static char gps_header[20]; |
| | | typedef struct |
| | | { |
| | | char m_pData[100]; |
| | | uint32_t m_u32Len; |
| | | } GGA_DataStruct; |
| | | typedef struct |
| | | { |
| | | E_BLERecvState m_eState; |
| | |
| | | uint8_t mUsart2ReceivePack_before , mUsart2ReceivePack_now; |
| | | uint8_t j_ct=0,CT_satrt_temp=0,CT_satrt=0,numb_base=0 ; |
| | | |
| | | uint8_t id_cmpare[12]= {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x30, 0x30, 0x30, 0x31}; //123456780001//测试//RC2202A: |
| | | uint8_t id_cmpare[12]={0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x30, 0x30, 0x30, 0x31};//123456780001//测试//RC2202A: |
| | | uint32_t CT_sum=0; |
| | | char char_broadcast_data[80];//广播数据 |
| | | |
| | |
| | | uint16_t temp_16,temp_16_id,temp_16_distance,data_buff_start; |
| | | |
| | | |
| | | uint16_t Checksum_u16(uint8_t* pdata, uint32_t len) |
| | | uint16_t Checksum_u16(uint8_t* pdata, uint32_t len) |
| | | { |
| | | uint16_t sum = 0; |
| | | uint32_t i; |
| | |
| | | sum = ~sum; |
| | | return sum; |
| | | } |
| | | |
| | | void SendComMap0(uint8_t data_length, uint8_t index)//表示成功读取的回应包 |
| | | { |
| | | static uint8_t send_frame[EUART_RX_BUF_SIZE]; |
| | | uint16_t checksum = 0; |
| | | send_frame[0] = 0x55; |
| | | send_frame[1] = 0xAA; |
| | | send_frame[2] = 0x03; |
| | | send_frame[3] = data_length+5; |
| | | send_frame[4] = CMD_REPLY; |
| | | send_frame[5] = index; |
| | | send_frame[6] = data_length; |
| | | memcpy(&send_frame[7], &g_com_map[index], data_length); |
| | | for(int i = 0; i<(data_length+5); i++) |
| | | { |
| | | checksum += send_frame[2+i]; |
| | | } |
| | | checksum = Checksum_u16(&send_frame[2],5+data_length); |
| | | memcpy(&send_frame[7+data_length],&checksum,2); |
| | | |
| | | uart_send(UART_ID0, send_frame,data_length+9, NULL); |
| | | } |
| | | void SendComMap(uint8_t data_length, uint8_t index)//表示成功读取的回应包 |
| | | { |
| | | static uint8_t send_frame[EUART_RX_BUF_SIZE]; |
| | | uint16_t checksum = 0; |
| | | send_frame[0] = 0x55; |
| | | send_frame[1] = 0xAA; |
| | | send_frame[2] = 0x03; |
| | | send_frame[3] = data_length+5; |
| | | send_frame[4] = CMD_REPLY; |
| | | send_frame[5] = index; |
| | | send_frame[6] = data_length; |
| | | memcpy(&send_frame[7], &g_com_map[index], data_length); |
| | | for(int i = 0; i<(data_length+5); i++) |
| | | { |
| | | checksum += send_frame[2+i]; |
| | | } |
| | | checksum = Checksum_u16(&send_frame[2],5+data_length); |
| | | memcpy(&send_frame[7+data_length],&checksum,2); |
| | | |
| | | uart_send(UART_ID1, send_frame,data_length+9, NULL); |
| | | static uint8_t send_frame[EUART_RX_BUF_SIZE]; |
| | | uint16_t checksum = 0; |
| | | send_frame[0] = 0x55; |
| | | send_frame[1] = 0xAA; |
| | | send_frame[2] = 0x03; |
| | | send_frame[3] = data_length+5; |
| | | send_frame[4] = CMD_REPLY; |
| | | send_frame[5] = index; |
| | | send_frame[6] = data_length; |
| | | memcpy(&send_frame[7], &g_com_map[index], data_length); |
| | | for(int i = 0; i<(data_length+5); i++) |
| | | { |
| | | checksum += send_frame[2+i]; |
| | | } |
| | | checksum = Checksum_u16(&send_frame[2],5+data_length); |
| | | memcpy(&send_frame[7+data_length],&checksum,2); |
| | | |
| | | uart_send(UART_ID1, send_frame,data_length+9, NULL); |
| | | } |
| | | void UpdateProcess(uint8_t index) |
| | | { |
| | |
| | | __disable_irq(); |
| | | result = flash_open(FLASH_ID0,NULL); |
| | | if(!result)//打开成功 |
| | | flash_erase(FLASH_ID0,APP_CONFIG_IAPFLAG_SECTOR_ADDR,FLASH_SECTOR_SIZE); |
| | | result = flash_write_nbytes(FLASH_ID0,APP_CONFIG_IAPFLAG_MAP,(uint8_t*)&tmp,2); |
| | | flash_erase(FLASH_ID0,APP_CONFIG_IAPFLAG_SECTOR_ADDR,FLASH_SECTOR_SIZE); |
| | | result = flash_write_nbytes(FLASH_ID0,APP_CONFIG_IAPFLAG_MAP,(uint8_t*)&tmp,2); |
| | | __enable_irq(); |
| | | Serial0PutString("进入升级模式\r\n"); |
| | | g_com_map[CNT_UPDATE]=0; |
| | | save_com_map_to_flash(); |
| | | delay_us(100000); |
| | | NVIC_SystemReset(); //软复位回到bootloader |
| | | NVIC_SystemReset(); //软复位回到bootloader |
| | | } |
| | | if(index == 2*CNT_REBOOT) |
| | | { g_com_map[CNT_REBOOT]=0; |
| | | { g_com_map[CNT_REBOOT]=0; |
| | | g_com_map[MAP_SIGN_INDEX]=0; |
| | | save_com_map_to_flash(); |
| | | delay_us(100000); |
| | | NVIC_SystemReset(); //软复位回到bootloader |
| | | delay_us(100000); |
| | | NVIC_SystemReset(); //软复位回到bootloader |
| | | } |
| | | if(index == 2*CNT_RESTART) |
| | | { |
| | | g_com_map[CNT_RESTART]=0; |
| | | save_com_map_to_flash(); |
| | | delay_us(100000); |
| | | NVIC_SystemReset(); //软复位回到bootloader |
| | | delay_us(100000); |
| | | NVIC_SystemReset(); //软复位回到bootloader |
| | | } |
| | | |
| | | } |
| | | extern uint8_t enable_sleep_count,sleep_flag; |
| | | void UsartParseDataHandler(uint8_t data) |
| | | { |
| | | if(state5V_prase_flag) |
| | | { //升级程序 |
| | | static UsartRecvPackState usart_receive_state = UsartReceiveWaitHead0; |
| | | uint16_t checksum = 0; |
| | | static uint8_t pack_datalen = 0,pack_length = 0,pack_index = 0,pack_msgtype = 0,pack_cmd = CMD_READ; |
| | | static uint8_t index = 0; |
| | | static UsartRecvPackState usart_receive_state = UsartReceiveWaitHead0; |
| | | uint16_t checksum = 0; |
| | | static uint8_t pack_datalen = 0,pack_length = 0,pack_index = 0,pack_msgtype = 0,pack_cmd = CMD_READ; |
| | | static uint8_t index = 0; |
| | | |
| | | |
| | | |
| | | |
| | | if(usart_receive_state == UsartReceiveWaitChecksum) { //若收到校验和包 |
| | | checksum = 0; |
| | | for(int i = 0; i<pack_length-5; i++) { |
| | | checksum += mUsartReceivePack[i]; |
| | | } |
| | | checksum += pack_cmd; |
| | | checksum += pack_length; |
| | | checksum += pack_index; |
| | | checksum += pack_datalen; |
| | | checksum += pack_msgtype; |
| | | if(((data + checksum)&0xff) == 0xFF) //校验通过 |
| | | { |
| | | switch(pack_cmd) |
| | | { |
| | | case CMD_WRITE: |
| | | //从mUsartReceivePack中读取pack_length长度的字节,放到全局变量中,赋值保存的参数并且存入flash |
| | | enable_sleep_count=0; |
| | | memcpy((uint8_t*)&g_com_map + pack_index, mUsartReceivePack, pack_datalen); |
| | | |
| | | if(mUsartReceivePack[0]==1) |
| | | UpdateProcess(pack_index); |
| | | //返回一个error状态 |
| | | //SendComMap(pack_datalen,pack_index); |
| | | save_com_map_to_flash(); |
| | | //delay_ms(100); |
| | | NVIC_SystemReset(); |
| | | break; |
| | | case CMD_READ: |
| | | enable_sleep_count=0; |
| | | //read包中data字节,即mUsartReceivePack[0]表示数据长度; |
| | | //从g_com_data结构体中的第index位置读取长度为mUsartReceivePack[0]的字节,发送出来 |
| | | SendComMap(pack_datalen,pack_index>>1); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | } |
| | | usart_receive_state = UsartReceiveWaitHead0; |
| | | pack_index = 0; |
| | | pack_length = 0; |
| | | index=0; |
| | | } else if((usart_receive_state == UsartReceiveWaitData) ) { //若果收到的是正常通讯包 |
| | | mUsartReceivePack[index] = data; |
| | | index++; |
| | | if(index == pack_length-5) { //如果收到的index与长度相等 |
| | | usart_receive_state = UsartReceiveWaitChecksum; |
| | | } |
| | | } else if(usart_receive_state == UsartReceiveWaitDataLen) { //收到指令类型字节 |
| | | pack_datalen = data; |
| | | usart_receive_state = UsartReceiveWaitData; |
| | | }else if(usart_receive_state == UsartReceiveWaitIndex) { //收到指令类型字节 |
| | | pack_index = data; |
| | | usart_receive_state = UsartReceiveWaitDataLen; |
| | | } else if(usart_receive_state == UsartReceiveWaitCMD) { //收到指令类型字节 |
| | | pack_cmd = data; |
| | | usart_receive_state = UsartReceiveWaitIndex; |
| | | } else if(usart_receive_state == UsartReceiveWaitLength) { //收到长度字节 |
| | | |
| | | pack_length = data; |
| | | pack_index = 0; |
| | | usart_receive_state = UsartReceiveWaitCMD; |
| | | |
| | | } else if((usart_receive_state == UsartReceiveWaitHead0) && (data == 0x55)) { //收到第一个包头 |
| | | usart_receive_state = UsartReceiveWaitHead1; |
| | | } else if((usart_receive_state == UsartReceiveWaitHead1) && (data == 0xAA)) { //收到第二个包头 |
| | | usart_receive_state = UsartReceiveWaitMsgType; |
| | | }else if ((usart_receive_state == UsartReceiveWaitMsgType) && (data == 0x3)) { |
| | | usart_receive_state = UsartReceiveWaitLength; |
| | | pack_msgtype = data; |
| | | } |
| | | else { |
| | | usart_receive_state = UsartReceiveWaitHead0; |
| | | pack_index = 0; |
| | | pack_length = 0; |
| | | } |
| | | |
| | | if(usart_receive_state == UsartReceiveWaitChecksum) |
| | | { //若收到校验和包 |
| | | checksum = 0; |
| | | for(int i = 0; i<pack_length-5; i++) { |
| | | checksum += mUsartReceivePack[i]; |
| | | } |
| | | checksum += pack_cmd; |
| | | checksum += pack_length; |
| | | checksum += pack_index; |
| | | checksum += pack_datalen; |
| | | checksum += pack_msgtype; |
| | | if(((data + checksum)&0xff) == 0xFF) //校验通过 |
| | | { |
| | | switch(pack_cmd) |
| | | { |
| | | case CMD_WRITE: |
| | | //从mUsartReceivePack中读取pack_length长度的字节,放到全局变量中,赋值保存的参数并且存入flash |
| | | memcpy((uint8_t*)&g_com_map + pack_index, mUsartReceivePack, pack_datalen); |
| | | if(mUsartReceivePack[0]==1) |
| | | UpdateProcess(pack_index); |
| | | //返回一个error状态 |
| | | //SendComMap(pack_datalen,pack_index); |
| | | save_com_map_to_flash(); |
| | | //delay_ms(100); |
| | | NVIC_SystemReset(); |
| | | break; |
| | | case CMD_READ: |
| | | //read包中data字节,即mUsartReceivePack[0]表示数据长度; |
| | | //从g_com_data结构体中的第index位置读取长度为mUsartReceivePack[0]的字节,发送出来 |
| | | SendComMap(pack_datalen,pack_index>>1); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | } |
| | | usart_receive_state = UsartReceiveWaitHead0; |
| | | pack_index = 0; |
| | | pack_length = 0; |
| | | index=0; |
| | | } else if((usart_receive_state == UsartReceiveWaitData) ) { //若果收到的是正常通讯包 |
| | | mUsartReceivePack[index] = data; |
| | | index++; |
| | | if(index == pack_length-5) { //如果收到的index与长度相等 |
| | | usart_receive_state = UsartReceiveWaitChecksum; |
| | | } |
| | | } else if(usart_receive_state == UsartReceiveWaitDataLen) { //收到指令类型字节 |
| | | pack_datalen = data; |
| | | usart_receive_state = UsartReceiveWaitData; |
| | | } else if(usart_receive_state == UsartReceiveWaitIndex) { //收到指令类型字节 |
| | | pack_index = data; |
| | | usart_receive_state = UsartReceiveWaitDataLen; |
| | | } else if(usart_receive_state == UsartReceiveWaitCMD) { //收到指令类型字节 |
| | | pack_cmd = data; |
| | | usart_receive_state = UsartReceiveWaitIndex; |
| | | } else if(usart_receive_state == UsartReceiveWaitLength) { //收到长度字节 |
| | | |
| | | pack_length = data; |
| | | pack_index = 0; |
| | | usart_receive_state = UsartReceiveWaitCMD; |
| | | |
| | | } else if((usart_receive_state == UsartReceiveWaitHead0) && (data == 0x55)) { //收到第一个包头 |
| | | usart_receive_state = UsartReceiveWaitHead1; |
| | | } else if((usart_receive_state == UsartReceiveWaitHead1) && (data == 0xAA)) { //收到第二个包头 |
| | | usart_receive_state = UsartReceiveWaitMsgType; |
| | | } else if ((usart_receive_state == UsartReceiveWaitMsgType) && (data == 0x3)) { |
| | | usart_receive_state = UsartReceiveWaitLength; |
| | | pack_msgtype = data; |
| | | } |
| | | else { |
| | | usart_receive_state = UsartReceiveWaitHead0; |
| | | pack_index = 0; |
| | | pack_length = 0; |
| | | } |
| | | } else if(gps_prase_flag) |
| | | { |
| | | //GPS解析数据 |
| | | // static ST_BLERecv BLE_recvive; |
| | | GGA_DataStruct jdrecv,wdrecv,Posstate; |
| | | static uint8_t index = 0; |
| | | //接收数据开始分析 |
| | | mUsart2ReceivePack[index] = data; //char数组传进来参数data |
| | | mUsart2ReceivePack_before = mUsart2ReceivePack_now; |
| | | mUsart2ReceivePack_now=data; |
| | | index++; |
| | | if( mUsart2ReceivePack_before == 0x0D && mUsart2ReceivePack_now==0x0A )//接收数据到“0x0D 0x0A”结束 |
| | | { |
| | | //解析该条GPS报文 |
| | | analysis_num=sscanf((char*)mUsart2ReceivePack,"$%[^,],%*[^,],%[^,],%*[^,],%[^,],%*[^,],%[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*s",gps_header,wdrecv.m_pData,jdrecv.m_pData,Posstate.m_pData); |
| | | if(!memcmp(gps_header,"GNGGA",5)) |
| | | { |
| | | wd=strtod(wdrecv.m_pData,NULL); |
| | | jd=strtod(jdrecv.m_pData,NULL); |
| | | pos_state=atoi(Posstate.m_pData); |
| | | } else { |
| | | wd=0; |
| | | jd=0; |
| | | } |
| | | blink_led(&gps_success_state); |
| | | if(pos_state!=0) |
| | | { |
| | | //4g.jd=jd; |
| | | //4g.wd=wd; |
| | | gps_timeout_flag=1;//不超时接收状态 |
| | | gps_need_data_flag=0;//接收数据完成 |
| | | gps_wait_count=0;//清0接收状态 |
| | | // gps_enable_flag=0;//收到有效数据关闭GPS |
| | | } |
| | | //memset(mUsart2ReceivePack,0,sizeof(mUsart2ReceivePack)); |
| | | memset(wdrecv.m_pData,0,sizeof(wdrecv.m_pData)); |
| | | memset(jdrecv.m_pData,0,sizeof(jdrecv.m_pData)); |
| | | memset(Posstate.m_pData,0,sizeof(Posstate.m_pData)); |
| | | index=0; |
| | | mUsart2ReceivePack_before=0; |
| | | mUsart2ReceivePack_now=0; |
| | | } |
| | | } |
| | | } |
| | | void uart0_send_ComMap_to_BLE(uint8_t data_length, uint8_t index) |
| | | { |
| | | static uint8_t send_frame[100]; |
| | | uint16_t checksum = 0; |
| | | send_frame[0] = 0x55; |
| | | send_frame[1] = 0xAA; |
| | | send_frame[2] = 0x40; |
| | | send_frame[3] = CMD_REPLY; |
| | | send_frame[4] = data_length+6; |
| | | send_frame[5] = index; |
| | | memcpy(&send_frame[6], &g_com_map[index], data_length); |
| | | checksum = Checksum_u16(&send_frame[2],5+data_length); |
| | | memcpy(&send_frame[6+data_length],&checksum,2); |
| | | |
| | | uart_send(UART_ID0, send_frame,data_length+8, NULL); |
| | | } |
| | | void Send_Commap_mk_to_ble(uint8_t data_length, uint8_t index) |
| | | { |
| | | static uint8_t send_frame[100]; |
| | | uint16_t checksum = 0; |
| | | send_frame[0] = 0x55; |
| | | send_frame[1] = 0xAA; |
| | | send_frame[2] = 0x40; |
| | | send_frame[3] = CMD_WRITE; |
| | | send_frame[4] = data_length+6; |
| | | send_frame[5] = index; |
| | | memcpy(&send_frame[6], &g_com_map[index], data_length); |
| | | checksum = Checksum_u16(&send_frame[2],5+data_length); |
| | | memcpy(&send_frame[6+data_length],&checksum,2); |
| | | |
| | | uart_send(UART_ID0, send_frame,data_length+8, NULL); |
| | | } |
| | | |
| | | void read_com_map(uint8_t data_length, uint8_t index) |
| | | { |
| | | static uint8_t send_frame[100]; |
| | | uint16_t checksum = 0; |
| | | send_frame[0] = 0x55; |
| | | send_frame[1] = 0xAA; |
| | | send_frame[2] = 0x40; |
| | | send_frame[3] = CMD_READ; |
| | | send_frame[4] = data_length; |
| | | send_frame[5] = index; |
| | | checksum = Checksum_u16(&send_frame[2],5+data_length); |
| | | uart_send(UART_ID0, send_frame,6, NULL); |
| | | } |
| | | void Usart0ParseDataHandler(uint8_t data)//UART蓝牙数据分析处理 |
| | | { |
| | | // 全局变量 |
| | | 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; |
| | | |
| | | 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 == 0x40) { |
| | | pack_cmd_type = data; |
| | | usart0_receive_state = Usart0ReceiveWaitMsgType; |
| | | send_frame[frame_index++] = data; |
| | | } else { |
| | | usart0_receive_state = Usart0ReceiveWaitHead0; |
| | | } |
| | | break; |
| | | |
| | | case Usart0ReceiveWaitMsgType: |
| | | if(data == 0x2) { // 写消息类型 |
| | | 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 = Usart0ReceiveWaitParamAddr; |
| | | send_frame[frame_index++] = data; |
| | | break; |
| | | |
| | | case Usart0ReceiveWaitParamAddr: |
| | | pack_param_addr = data; |
| | | usart0_receive_state = Usart0ReceiveWaitData; |
| | | send_frame[frame_index++] = data; |
| | | break; |
| | | |
| | | case Usart0ReceiveWaitData: |
| | | mUsart2ReceivePack[index++] = data; |
| | | send_frame[frame_index++] = data; |
| | | if(index == pack_data_len-6) { |
| | | usart0_receive_state = Usart0ReceiveWaitChecksum; |
| | | pack_datalen=pack_data_len-6; |
| | | } |
| | | break; |
| | | |
| | | case Usart0ReceiveWaitChecksum: |
| | | pack_checksum = data << 8; // 高字节 |
| | | pack_checksum |= data; // 低字节 |
| | | |
| | | // 计算校验和 (从第3个字节开始,长度为5+数据长度) |
| | | calculated_checksum = Checksum_u16(&send_frame[2],pack_datalen+5); |
| | | |
| | | // 验证校验和 |
| | | if(pack_checksum == calculated_checksum) { |
| | | // 校验通过,处理数据 |
| | | switch(pack_cmd_type) |
| | | { |
| | | case CMD_WRITE: |
| | | //从mUsartReceivePack中读取pack_length长度的字节,放到全局变量中,赋值保存的参数并且存入flash |
| | | |
| | | memcpy((uint8_t*)&g_com_map + pack_param_addr, mUsart2ReceivePack, pack_datalen); |
| | | |
| | | //返回一个error状态 |
| | | //SendComMap(pack_datalen,pack_index); |
| | | save_com_map_to_flash(); |
| | | //delay_ms(100); |
| | | // NVIC_SystemReset(); |
| | | break; |
| | | case CMD_READ: |
| | | Send_Commap_mk_to_ble(pack_datalen,pack_param_addr); |
| | | |
| | | 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; |
| | | } |
| | | |
| | | |
| | | // if(usart_receive_state == UsartReceiveWaitChecksum) { //若收到校验和包 |
| | | // checksum = 0; |
| | | // for(int i = 0; i<pack_length-5; i++) { |
| | | // checksum += mUsart2ReceivePack[i]; |
| | | // } |
| | | // checksum += pack_cmd; |
| | | // checksum += pack_length; |
| | | // checksum += pack_index; |
| | | // checksum += pack_datalen; |
| | | // checksum += pack_msgtype; |
| | | // if(((data + checksum)&0xff) == 0xFF) //校验通过 |
| | | // { |
| | | // switch(pack_cmd) |
| | | // { |
| | | // case CMD_WRITE: |
| | | // //从mUsartReceivePack中读取pack_length长度的字节,放到全局变量中,赋值保存的参数并且存入flash |
| | | // enable_sleep_count=0; |
| | | // memcpy((uint8_t*)&g_com_map + pack_index, mUsart2ReceivePack, pack_datalen); |
| | | // |
| | | // if(mUsart2ReceivePack[0]==1) |
| | | // UpdateProcess(pack_index); |
| | | // //返回一个error状态 |
| | | // //SendComMap(pack_datalen,pack_index); |
| | | // save_com_map_to_flash(); |
| | | // //delay_ms(100); |
| | | // //NVIC_SystemReset(); |
| | | // break; |
| | | // case CMD_READ: |
| | | // //read包中data字节,即mUsartReceivePack[0]表示数据长度; |
| | | // //从g_com_data结构体中的第index位置读取长度为mUsartReceivePack[0]的字节,发送出来 |
| | | // SendComMap0(pack_datalen,pack_index>>1); |
| | | // break; |
| | | // default: |
| | | // break; |
| | | // } |
| | | // } |
| | | // usart_receive_state = UsartReceiveWaitHead0; |
| | | // pack_index = 0; |
| | | // pack_length = 0; |
| | | // index=0; |
| | | // } else if((usart_receive_state == UsartReceiveWaitData) ) { //若果收到的是正常通讯包 |
| | | // mUsart2ReceivePack[index] = data; |
| | | // index++; |
| | | // if(index == pack_length-5) { //如果收到的index与长度相等 |
| | | // usart_receive_state = UsartReceiveWaitChecksum; |
| | | // } |
| | | // } else if(usart_receive_state == UsartReceiveWaitDataLen) { //收到指令类型字节 |
| | | // pack_datalen = data; |
| | | // usart_receive_state = UsartReceiveWaitData; |
| | | // }else if(usart_receive_state == UsartReceiveWaitIndex) { //收到指令类型字节 |
| | | // pack_index = data; |
| | | // usart_receive_state = UsartReceiveWaitDataLen; |
| | | // } else if(usart_receive_state == UsartReceiveWaitCMD) { //收到指令类型字节 |
| | | // pack_cmd = data; |
| | | // usart_receive_state = UsartReceiveWaitIndex; |
| | | // } else if(usart_receive_state == UsartReceiveWaitLength) { //收到长度字节 |
| | | // |
| | | // pack_length = data; |
| | | // pack_index = 0; |
| | | // usart_receive_state = UsartReceiveWaitCMD; |
| | | // |
| | | // } else if((usart_receive_state == UsartReceiveWaitHead0) && (data == 0x55)) { //收到第一个包头 |
| | | // usart_receive_state = UsartReceiveWaitHead1; |
| | | // } else if((usart_receive_state == UsartReceiveWaitHead1) && (data == 0xAA)) { //收到第二个包头 |
| | | // usart_receive_state = UsartReceiveWaitMsgType; |
| | | // }else if ((usart_receive_state == UsartReceiveWaitMsgType) && (data == 0x3)) { |
| | | // usart_receive_state = UsartReceiveWaitLength; |
| | | // pack_msgtype = data; |
| | | // } |
| | | // else { |
| | | // usart_receive_state = UsartReceiveWaitHead0; |
| | | // pack_index = 0; |
| | | // pack_length = 0; |
| | | // } |
| | | } |
| | | |
| | | |