| | |
| | | #define CMD_READ 1 |
| | | #define CMD_WRITE 2 |
| | | #define CMD_REPLY 3 |
| | | #define EUART_RX_BUF_SIZE 100 |
| | | #define EUART_RX_BUF_SIZE 200 |
| | | typedef enum{ UsartReceiveWaitHead0, |
| | | UsartReceiveWaitHead1, |
| | | UsartReceiveWaitMsgType, |
| | |
| | | UsartReceiveWaitIndex, |
| | | UsartReceiveWaitDataLen, |
| | | UsartReceiveWaitData, |
| | | UsartReceiveWaitChecksum |
| | | UsartReceiveWaitChecksum0, |
| | | UsartReceiveWaitChecksum1 |
| | | }UsartRecvPackState; |
| | | |
| | | uint8_t mUsartReceivePack[100] = {0}; |
| | |
| | | send_frame[5] = index; |
| | | send_frame[6] = data_length; |
| | | memcpy(&send_frame[7], &g_com_map[index>>1], 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); |
| | | |
| | |
| | | } |
| | | u8 remotetag_para[30]; |
| | | u8 remotesend_state=0; |
| | | u16 rec_checksum; |
| | | void UsartParseDataHandler(uint8_t data) |
| | | { |
| | | static UsartRecvPackState usart_receive_state = UsartReceiveWaitHead0; |
| | |
| | | |
| | | |
| | | |
| | | if(usart_receive_state == UsartReceiveWaitChecksum) { //è¥æ¶å°æ ¡éªåå
|
| | | if(usart_receive_state == UsartReceiveWaitChecksum1) { //è¥æ¶å°æ ¡éªåå
. |
| | | checksum = 0; |
| | | for(int i = 0; i<pack_length-5; i++) { |
| | | checksum += mUsartReceivePack[i]; |
| | |
| | | checksum += pack_index; |
| | | checksum += pack_datalen; |
| | | checksum += pack_msgtype; |
| | | if(((data + checksum)&0xff) == 0xFF) //æ ¡éªéè¿ |
| | | checksum = ~checksum; |
| | | rec_checksum = rec_checksum|(data<<8); |
| | | if(rec_checksum == checksum) //æ ¡éªéè¿ |
| | | { |
| | | switch(pack_msgtype) |
| | | { |
| | |
| | | { |
| | | case CMD_WRITE: |
| | | //ä»mUsartReceivePackä¸è¯»åpack_lengthé¿åº¦çåèï¼æ¾å°å
¨å±åéä¸ |
| | | memcpy((uint8_t*)&g_com_map + pack_index, mUsartReceivePack, pack_datalen); |
| | | if(pack_index<512) |
| | | // if(pack_index>100) |
| | | { |
| | | memcpy((uint8_t*)&g_com_map + pack_index, mUsartReceivePack, pack_datalen); |
| | | //è¿åä¸ä¸ªerrorç¶æ |
| | | //SendComMap(pack_datalen,pack_index); |
| | | save_com_map_to_flash(); |
| | | SendConfigConfirm(pack_msgtype,pack_length); |
| | | Program_Init(); |
| | | delay_ms(500); |
| | | SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软å¤ä½åå°bootloader |
| | | |
| | | }else{ |
| | | memcpy((uint8_t*)&g_com_map + pack_index, mUsartReceivePack, pack_datalen); |
| | | //è¿åä¸ä¸ªerrorç¶æ |
| | | //SendComMap(pack_datalen,pack_index); |
| | | //save_com_map_to_flash(); |
| | | SendConfigConfirm(pack_msgtype,pack_length); |
| | | |
| | | } |
| | | break; |
| | | case CMD_READ: |
| | | //readå
ä¸dataåèï¼å³mUsartReceivePack[0]è¡¨ç¤ºæ°æ®é¿åº¦ï¼ |
| | |
| | | break; |
| | | } |
| | | break; |
| | | case MSG_WTAG: |
| | | remotesend_state++; |
| | | if(remotesend_state>14) |
| | | remotesend_state=1; |
| | | memcpy(remotetag_para,mUsartReceivePack,REMOTEPARA_LEN); |
| | | break; |
| | | |
| | | } |
| | | } |
| | |
| | | pack_index = 0; |
| | | pack_length = 0; |
| | | index=0; |
| | | } else if((usart_receive_state == UsartReceiveWaitData) ) { //è¥ææ¶å°çæ¯æ£å¸¸é讯å
|
| | | } else if((usart_receive_state == UsartReceiveWaitChecksum0) ) { |
| | | rec_checksum = data; |
| | | usart_receive_state = UsartReceiveWaitChecksum1; |
| | | }else if((usart_receive_state == UsartReceiveWaitData) ) { //è¥ææ¶å°çæ¯æ£å¸¸é讯å
|
| | | mUsartReceivePack[index] = data; |
| | | index++; |
| | | if(index == pack_length-5) { //妿æ¶å°çindexä¸é¿åº¦ç¸ç |
| | | usart_receive_state = UsartReceiveWaitChecksum; |
| | | usart_receive_state = UsartReceiveWaitChecksum0; |
| | | } |
| | | } else if(usart_receive_state == UsartReceiveWaitDataLen) { //æ¶å°æä»¤ç±»ååè |
| | | pack_datalen = data; |