| | |
| | | #include "dw_app.h" |
| | | #include "RTC.h" |
| | | |
| | | #define MSG_RW 3 |
| | | #define MSG_WTAG 6 |
| | | #define MSG_USERDATA 0x10 |
| | | |
| | | #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; |
| | | OUT485_ENABLE; |
| | | checksum = Checksum_u16(&send_frame[2],8); |
| | | memcpy(&send_frame[10],&checksum,2); |
| | | |
| | | USART_puts(send_frame, 12); |
| | | OUT485_DISABLE; |
| | | } |
| | | 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; |
| | |
| | | |
| | | UART_PushFrame(send_frame, data_length+9); |
| | | } |
| | | u8 remotetag_para[30],remotetag_paralen,userdata_len,userdata[50]; |
| | | |
| | | u8 remotesend_state=0; |
| | | u16 rec_checksum,userdata_targetid; |
| | | u32 getuserdata_tick; |
| | | 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?????,??????? |
| | | 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 |
| | | 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: |
| | | |
| | | case MSG_USERDATA: |
| | | userdata_targetid = pack_cmd|pack_index<<8; |
| | | userdata_len = pack_datalen; |
| | | //memcpy(&userdata_targetid,mUsartReceivePack,2); |
| | | if(userdata_len==USERDATA_LEN) |
| | | { |
| | | memcpy(userdata,&userdata_targetid,2); |
| | | userdata[2] = userdata_len; |
| | | memcpy(&userdata[3],&mUsartReceivePack,USERDATA_LEN); |
| | | getuserdata_tick = time32_incr; |
| | | SendConfigConfirm(pack_msgtype,pack_length); |
| | | } |
| | | 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; |
| | | if(index == pack_length-5) { //?????index????? |
| | | usart_receive_state = UsartReceiveWaitChecksum0; |
| | | } |
| | | } else if(usart_receive_state == UsartReceiveWaitDataLen) { //æ¶å°æä»¤ç±»ååè |
| | | } else if(usart_receive_state == UsartReceiveWaitDataLen) { //???????? |
| | | pack_datalen = data; |
| | | usart_receive_state = UsartReceiveWaitData; |
| | | }else if(usart_receive_state == UsartReceiveWaitIndex) { //æ¶å°æä»¤ç±»ååè |
| | | }else if(usart_receive_state == UsartReceiveWaitIndex) { //???????? |
| | | pack_index = data; |
| | | usart_receive_state = UsartReceiveWaitDataLen; |
| | | } else if(usart_receive_state == UsartReceiveWaitCMD) { //æ¶å°æä»¤ç±»ååè |
| | | } else if(usart_receive_state == UsartReceiveWaitCMD) { //???????? |
| | | pack_cmd = data; |
| | | usart_receive_state = UsartReceiveWaitIndex; |
| | | } else if(usart_receive_state == UsartReceiveWaitLength) { //æ¶å°é¿åº¦åè |
| | | } else if(usart_receive_state == UsartReceiveWaitLength) { //?????? |
| | | |
| | | pack_length = data; |
| | | pack_index = 0; |
| | | usart_receive_state = UsartReceiveWaitCMD; |
| | | |
| | | } else if((usart_receive_state == UsartReceiveWaitHead0) && (data == 0x55)) { //æ¶å°ç¬¬ä¸ä¸ªå
头 |
| | | } else if((usart_receive_state == UsartReceiveWaitHead0) && (data == 0x55)) { //??????? |
| | | usart_receive_state = UsartReceiveWaitHead1; |
| | | } else if((usart_receive_state == UsartReceiveWaitHead1) && (data == 0xAA)) { //æ¶å°ç¬¬äºä¸ªå
头 |
| | | } 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; |
| | | } |
| | |
| | | pack_length = 0; |
| | | } |
| | | |
| | | } |
| | | } |