| | |
| | | #include "dw_app.h" |
| | | #include "RTC.h" |
| | | |
| | | #define MSG_RW 3 |
| | | #define MSG_WTAG 6 |
| | | |
| | | #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}; |
| | | |
| | | uint8_t mUsartReceivePack[100] = {0}; |
| | | uint8_t send_frame[200]; |
| | | void SendConfigConfirm(uint8_t pack_msgtype,uint8_t pack_length) |
| | | { |
| | | uint16_t checksum = 0; |
| | | send_frame[0] = 0x55; |
| | | send_frame[1] = 0xAA; |
| | | send_frame[2] = 0x09; |
| | | send_frame[3] = 8; |
| | | send_frame[4] = pack_msgtype; |
| | | send_frame[5] = pack_length; |
| | | |
| | | checksum = Checksum_u16(&send_frame[2],8); |
| | | memcpy(&send_frame[10],&checksum,2); |
| | | |
| | | UART_PushFrame(send_frame, 12); |
| | | } |
| | | 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[4] = CMD_REPLY; |
| | | send_frame[5] = index; |
| | | send_frame[6] = data_length; |
| | | memcpy(&send_frame[7], &g_com_map[index>>2], 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]; |
| | |
| | | |
| | | UART_PushFrame(send_frame, data_length+9); |
| | | } |
| | | |
| | | 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_cmd) |
| | | switch(pack_msgtype) |
| | | { |
| | | case CMD_WRITE: |
| | | //ä»mUsartReceivePackä¸è¯»åpack_lengthé¿åº¦çåèï¼æ¾å°å
¨å±åéä¸ |
| | | memcpy((uint8_t*)&g_com_map + pack_index, mUsartReceivePack, pack_datalen); |
| | | //è¿åä¸ä¸ªerrorç¶æ |
| | | //SendComMap(pack_datalen,pack_index); |
| | | save_com_map_to_flash(); |
| | | delay_ms(100); |
| | | NVIC_SystemReset(); |
| | | case MSG_RW: |
| | | switch(pack_cmd) |
| | | { |
| | | case CMD_WRITE: |
| | | //ä»mUsartReceivePackä¸è¯»åpack_lengthé¿åº¦çåèï¼æ¾å°å
¨å±åéä¸ |
| | | 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); |
| | | 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]è¡¨ç¤ºæ°æ®é¿åº¦ï¼ |
| | | //ä»g_com_dataç»æä½ä¸ç第indexä½ç½®è¯»åé¿åº¦ä¸ºmUsartReceivePack[0]çåèï¼åéåºæ¥ |
| | | SendComMap(pack_datalen,pack_index); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | break; |
| | | case CMD_READ: |
| | | //readå
ä¸dataåèï¼å³mUsartReceivePack[0]è¡¨ç¤ºæ°æ®é¿åº¦ï¼ |
| | | //ä»g_com_dataç»æä½ä¸ç第indexä½ç½®è¯»åé¿åº¦ä¸ºmUsartReceivePack[0]çåèï¼åéåºæ¥ |
| | | SendComMap(pack_datalen,pack_index); |
| | | case MSG_WTAG: |
| | | remotesend_state++; |
| | | if(remotesend_state>14) |
| | | remotesend_state=1; |
| | | memcpy(remotetag_para,mUsartReceivePack,REMOTEPARA_LEN); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | |
| | | } |
| | | } |
| | | usart_receive_state = UsartReceiveWaitHead0; |
| | | 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; |
| | |
| | | usart_receive_state = UsartReceiveWaitHead1; |
| | | } else if((usart_receive_state == UsartReceiveWaitHead1) && (data == 0xAA)) { //æ¶å°ç¬¬äºä¸ªå
头 |
| | | usart_receive_state = UsartReceiveWaitMsgType; |
| | | }else if ((usart_receive_state == UsartReceiveWaitMsgType) && (data == 0x3)) { |
| | | }else if ((usart_receive_state == UsartReceiveWaitMsgType)) { |
| | | usart_receive_state = UsartReceiveWaitLength; |
| | | pack_msgtype = data; |
| | | } |