| | |
| | | #include <string.h> |
| | | #include <stdio.h> |
| | | #include "dw_app.h" |
| | | #include "RTC.h" |
| | | |
| | | uint8_t m_frame_data[MAX_FRAME_LEN] = {0}; |
| | | uint8_t m_frame_data_len = 0; |
| | | #define MSG_RW 3 |
| | | #define MSG_WTAG 6 |
| | | #define MSG_PWTAG 8 |
| | | #define MSG_REGTAGRESPONSE 0x0b |
| | | |
| | | void ParseFrame(void) |
| | | #define CMD_READ 1 |
| | | #define CMD_WRITE 2 |
| | | #define CMD_REPLY 3 |
| | | |
| | | struct pwtag_structure pwtag; |
| | | typedef enum{ UsartReceiveWaitHead0, |
| | | UsartReceiveWaitHead1, |
| | | UsartReceiveWaitMsgType, |
| | | UsartReceiveWaitLength, |
| | | UsartReceiveWaitCMD, |
| | | UsartReceiveWaitIndex, |
| | | UsartReceiveWaitDataLen, |
| | | UsartReceiveWaitData, |
| | | UsartReceiveWaitChecksum |
| | | }UsartRecvPackState; |
| | | uint8_t mUsartReceivePack[100] = {0}; |
| | | uint8_t send_frame[200]; |
| | | void SendConfigConfirm(uint8_t pack_msgtype,uint8_t pack_length) |
| | | { |
| | | if(!memcmp(m_frame_data, "DIS1", 4)) |
| | | { |
| | | g_com_map[ALARM_DISTANCE1] =0; |
| | | for(uint8_t i=5; i < m_frame_data_len; i++) |
| | | { |
| | | if(m_frame_data[i]<58 && m_frame_data[i]>47) |
| | | { |
| | | g_com_map[ALARM_DISTANCE1] = g_com_map[ALARM_DISTANCE1]*10 + m_frame_data[i]- '0'; |
| | | } |
| | | else |
| | | { |
| | | printf("Error: Wrong DISTANCE.\r\n"); |
| | | break; |
| | | } |
| | | } |
| | | printf("When distance1 < %d cm, alarm1 is on.\n", g_com_map[ALARM_DISTANCE1]); |
| | | save_com_map_to_flash(); |
| | | } |
| | | else if(!memcmp(m_frame_data, "DIS2", 4)) |
| | | { |
| | | g_com_map[ALARM_DISTANCE2] =0; |
| | | for(uint8_t i=5; i < m_frame_data_len; i++) |
| | | { |
| | | if(m_frame_data[i]<58 && m_frame_data[i]>47) |
| | | { |
| | | g_com_map[ALARM_DISTANCE2] = g_com_map[ALARM_DISTANCE2]*10 + m_frame_data[i]- '0'; |
| | | } |
| | | else |
| | | { |
| | | printf("Error: Wrong DISTANCE.\r\n"); |
| | | break; |
| | | } |
| | | } |
| | | printf("When distance2 < %d cm, alarm2 is on.\n", g_com_map[ALARM_DISTANCE2]); |
| | | save_com_map_to_flash(); |
| | | } |
| | | else if(!memcmp(m_frame_data, "DIS3", 4)) |
| | | { |
| | | g_com_map[ALARM_DISTANCE3] =0; |
| | | for(uint8_t i=5; i < m_frame_data_len; i++) |
| | | { |
| | | if(m_frame_data[i]<58 && m_frame_data[i]>47) |
| | | { |
| | | g_com_map[ALARM_DISTANCE3] = g_com_map[ALARM_DISTANCE3]*10 + m_frame_data[i]- '0'; |
| | | } |
| | | else |
| | | { |
| | | printf("Error: Wrong DISTANCE.\r\n"); |
| | | break; |
| | | } |
| | | } |
| | | printf("When distance3 < %d cm, alarm3 is on.\n", g_com_map[ALARM_DISTANCE3]); |
| | | save_com_map_to_flash(); |
| | | } |
| | | else if(!memcmp(m_frame_data, "DEV", 3)) |
| | | { |
| | | g_com_map[ALARM_DEV] = m_frame_data[4]-'0'; |
| | | printf("Alarm device is %d. \n", g_com_map[ALARM_DEV]); |
| | | save_com_map_to_flash(); |
| | | } |
| | | else if(!memcmp(m_frame_data, "ID", 2)) |
| | | { |
| | | g_com_map[DEV_ID] = 0; |
| | | for(uint8_t i=3; i < m_frame_data_len; i++) |
| | | { |
| | | if(m_frame_data[i]<58 && m_frame_data[i]>47) |
| | | { |
| | | g_com_map[DEV_ID] = g_com_map[DEV_ID]*10 + m_frame_data[i]- '0'; |
| | | } |
| | | else |
| | | { |
| | | printf("Error: Wrong ID.\r\n"); |
| | | break; |
| | | } |
| | | } |
| | | Dw1000_App_Init(); |
| | | printf("set dev id = %d. \r\n", g_com_map[DEV_ID]); |
| | | save_com_map_to_flash(); |
| | | } |
| | | } |
| | | 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) |
| | | { |
| | | 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>>1], data_length); |
| | | |
| | | checksum = Checksum_u16(&send_frame[2],5+data_length); |
| | | memcpy(&send_frame[7+data_length],&checksum,2); |
| | | |
| | | UART_PushFrame(send_frame, data_length+9); |
| | | } |
| | | u8 remotetag_para[30],remotetag_paralen; |
| | | u8 remotesend_state=0; |
| | | uint16_t configremotetagID; |
| | | struct regtag_structure regtag_map; |
| | | void UsartParseDataHandler(uint8_t data) |
| | | { |
| | | static uint8_t s_usart_state = 0; |
| | | static uint8_t s_data_pos; |
| | | 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; |
| | | uint8_t i; |
| | | |
| | | |
| | | switch(s_usart_state) |
| | | { |
| | | case 0: |
| | | if(data=='A') s_usart_state= 1; |
| | | break; |
| | | |
| | | case 1: |
| | | if(data=='T') s_usart_state = 2; |
| | | break; |
| | | if(usart_receive_state == UsartReceiveWaitChecksum) { //è¥æ¶å°æ ¡éªåå
|
| | | checksum = 0; |
| | | for(int i = 0; i<pack_length-2; i++) { |
| | | checksum += mUsartReceivePack[i]; |
| | | } |
| | | checksum += pack_msgtype; |
| | | checksum += pack_length; |
| | | |
| | | if(((data + checksum)&0xff) == 0xFF) //æ ¡éªéè¿ |
| | | { |
| | | switch(pack_msgtype) |
| | | { |
| | | case MSG_RW: |
| | | pack_cmd = mUsartReceivePack[0]; |
| | | pack_index = mUsartReceivePack[1]; |
| | | pack_datalen = mUsartReceivePack[2]; |
| | | switch(pack_cmd) |
| | | { |
| | | case CMD_WRITE: |
| | | //ä»mUsartReceivePackä¸è¯»åpack_lengthé¿åº¦çåèï¼æ¾å°å
¨å±åéä¸ |
| | | memcpy((uint8_t*)&g_com_map + pack_index, &mUsartReceivePack[3], pack_datalen); |
| | | //è¿åä¸ä¸ª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); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | break; |
| | | case MSG_WTAG: //ä¿®æ¹æ ç¾åæ° |
| | | remotesend_state=1; |
| | | memcpy(&configremotetagID,&mUsartReceivePack[0],2); |
| | | //configremotetagID = pack_cmd|pack_index<<8; |
| | | //remotetag_para[0] = mUsartReceivePack[2]; |
| | | remotetag_paralen = mUsartReceivePack[4]+3; |
| | | memcpy(&remotetag_para[1],&mUsartReceivePack[3],remotetag_paralen); |
| | | break; |
| | | case MSG_PWTAG: //æ¹éä¿®æ¹æ ç¾åæ° |
| | | pwtag.groupnum = mUsartReceivePack[0]; |
| | | pwtag.index = mUsartReceivePack[1]; |
| | | pwtag.remain_time = 5; |
| | | for(i=0;i<pwtag.groupnum;i++) |
| | | { |
| | | memcpy(pwtag.groupid[i],&mUsartReceivePack[i*6+2],4); |
| | | memcpy(&pwtag.group_interval[i],&mUsartReceivePack[i*6+6],2); |
| | | } |
| | | break; |
| | | case MSG_REGTAGRESPONSE: |
| | | regtag_map.index++; |
| | | if(regtag_map.index==MAX_REGTAGNUM) |
| | | {regtag_map.index = 0;} |
| | | memcpy(®tag_map.tagid[regtag_map.index],&mUsartReceivePack[0],2); |
| | | memcpy(®tag_map.tag_frequency[regtag_map.index],&mUsartReceivePack[2],2); |
| | | memcpy(®tag_map.tag_slotpos[regtag_map.index],&mUsartReceivePack[4],2); |
| | | break; |
| | | } |
| | | SendConfigConfirm(pack_msgtype,pack_length); |
| | | } |
| | | 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-2) { //妿æ¶å°çindexä¸é¿åº¦ç¸ç |
| | | usart_receive_state = UsartReceiveWaitChecksum; |
| | | } |
| | | } else if(usart_receive_state == UsartReceiveWaitLength) { //æ¶å°é¿åº¦åè |
| | | |
| | | case 2: |
| | | if(data=='+') |
| | | { |
| | | s_usart_state = 3; |
| | | s_data_pos = 0; |
| | | m_frame_data_len = 0; |
| | | } |
| | | break; |
| | | |
| | | case 3: |
| | | m_frame_data_len = s_data_pos; |
| | | m_frame_data[s_data_pos++] = data; |
| | | if(data == 0x0d) |
| | | { |
| | | s_usart_state = 4; |
| | | } |
| | | if(s_data_pos >= MAX_FRAME_LEN) //妿è¶
è¿æå¤§é¿åº¦ |
| | | { |
| | | m_frame_data_len = MAX_FRAME_LEN; |
| | | ParseFrame(); |
| | | s_usart_state = 0; |
| | | } |
| | | break; |
| | | |
| | | case 4: |
| | | if(data == 0x0a) |
| | | { |
| | | ParseFrame(); |
| | | s_usart_state = 0; |
| | | } |
| | | break; |
| | | |
| | | default: break; |
| | | pack_length = data; |
| | | pack_index = 0; |
| | | usart_receive_state = UsartReceiveWaitData; |
| | | |
| | | } 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)) { |
| | | usart_receive_state = UsartReceiveWaitLength; |
| | | pack_msgtype = data; |
| | | } |
| | | else { |
| | | usart_receive_state = UsartReceiveWaitHead0; |
| | | pack_index = 0; |
| | | pack_length = 0; |
| | | } |
| | | |
| | | } |