WXK
2025-03-14 63bf8ea65ca11349d5fae93580f91a87d3633dba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <serial_at_cmd_app.h>
#include <global_param.h>
#include <string.h>
#include <stdio.h>
#include "Usart.h"
#include "mk_flash.h"
//#include "dw_app.h"
//#include "UsartII.h"
//#include "main.h"
#include "board.h"
//#include "Spi.h"
//#include "radio.h"
#include "DBG.h"
 
 
//#define EUART_RX_BUF_SIZE 100
#define Label_id_local    1     //±êÇ©id
#define data_buff_MAX     50    //»ùÕ¾ÊýÁ¿
#define Lora_TXD_bff_MAX  220   //·¢ËÍ»º´æÇø´óС  >4+8+4*»ùÕ¾ÊýÁ¿+2=14+4*»ùÕ¾ÊýÁ¿
 
uint8_t mUsartReceivePack[100] = {0};
uint8_t mUsart2ReceivePack[150] = {0};
uint8_t GPS_GGAmessage[150]={0};
uint8_t GPS_GSVmessage[150]={0};
double jd,wd;
int analysis_num,pos_state;
uint8_t state5V_prase_flag=1,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,gps_open_flag;
extern uint8_t gps_success_state,gps_enable_flag,gps_need_data_flag,gps_timeout_flag,gps_power_state;
extern uint16_t gps_wait_count;
typedef enum
{
    BLE_RECV_STATE_IDLE = 0,
    BLE_RECV_STATE_MAC,
    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;
    char m_macHeader[100],m_rssiHeader2[100];
    uint32_t m_u32HeaderLen,m_u32Header2Len;
 
    char m_acRecvBuf[128];
    uint32_t m_u32RecvLen;
} ST_BLERecv;
 
 
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£º
uint32_t CT_sum=0;
char char_broadcast_data[80];//¹ã²¥Êý¾Ý
 
uint8_t  Lora_TXD_bff[Lora_TXD_bff_MAX]; //lora·¢ËÍ»º´æÇø
uint16_t data_buff[data_buff_MAX][2];    //»ùÕ¾Êý¾Ý»º´æÇø
 
char  char_mac[14],char_ssi[6];
//int32_t distance_int;
double ssi_double;
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 sum = 0;
    uint32_t i;
    for(i=0; i<len; i++)
        sum += pdata[i];
    sum = ~sum;
    return sum;
}
 
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);
}
void UpdateProcess(uint8_t index)
{
    if(index == 2*CNT_UPDATE)
    {
        uint32_t result = 0;
        uint16_t tmp = 0xAAAA;
        __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);
        __enable_irq();
        Serial0PutString("½øÈëÉý¼¶Ä£Ê½\r\n");
        g_com_map[CNT_UPDATE]=0;
        save_com_map_to_flash();
        delay_us(100000);
        NVIC_SystemReset(); //Èí¸´Î»»Øµ½bootloader
    }
    if(index == 2*CNT_REBOOT)
    {   g_com_map[CNT_REBOOT]=0;
        g_com_map[MAP_SIGN_INDEX]=0;
        save_com_map_to_flash();
        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
    }
 
}
double d_value;
uint8_t shell_receive_state;
//static HIDO_UINT8 l_u8GPSBuff[512];
//static HIDO_UINT32 l_u8GPSLen = 0;
//static HIDO_UINT32 l_u8GPSRecvTick = 0;
void UsartParseDataHandler(uint8_t data)
{
    if(state5V_prase_flag&&!g_com_map[MODBUS_MODE])
    {   //Éý¼¶³ÌÐò
        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 shell_receive_state;
 
 
 
        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:
                  if(pack_index==MODBUS_MODE*2)
                  {
                      Uart1GpsRecDebugSend();                    
                      g_com_map[MODBUS_MODE] = 1;
                      return;
                  }
                    //´Ó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 if ((shell_receive_state == 0) && (data == 0x03)) {
            shell_receive_state = 1;
            uart_send(UART_ID1, data,1, NULL);
        } else if ((shell_receive_state == 1) && (data == 0x03)) {
            shell_receive_state = 2;
            uart_send(UART_ID1, data,1, NULL);
        } else if ((shell_receive_state == 2) && (data == 0x03)) {
          DBG_SetMode(DBG_MODE_SHELL);
            uart_send(UART_ID1, data,1, NULL);
            shell_receive_state=0;
        }
        else {
            usart_receive_state = UsartReceiveWaitHead0;
            pack_index = 0;
            pack_length = 0;
        }
    } else if(gps_prase_flag)
    {
//        static uint8_t index = 0;
//GPS½âÎöÊý¾Ý
//    static ST_BLERecv BLE_recvive;
//        GGA_DataStruct jdrecv,wdrecv,Posstate;
        GPS_RecvFsm(data);
//        l_u8GPSBuff[l_u8GPSLen++] = data;
//        if(l_u8GPSLen >= sizeof(l_u8GPSBuff))
//        {
//            NTRIPApp_ReportGGA(l_u8GPSBuff, l_u8GPSLen);
//            l_u8GPSLen = 0;
//        }
        
//        l_u8GPSRecvTick = HIDO_TimerGetTick();
//        //½ÓÊÕÊý¾Ý¿ªÊ¼·ÖÎö
//        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))
//            { 
//                            memcpy(GPS_GGAmessage,mUsart2ReceivePack,strlen(mUsart2ReceivePack)-2);
//                   
//                    wd=strtod(wdrecv.m_pData,NULL);
//                    jd=strtod(jdrecv.m_pData,NULL);
//                                        if(wd!=0)
//                    pos_state=atoi(Posstate.m_pData);
//                                if(pos_state!=0)
//                                {
//                                pos_state=0;//·ÀÖ¹¶à´Î½øÈë
//                                }
//                        
//            }
////                        if(!memcmp(gps_header,"GBGSV",5))
////                        {memcpy(GPS_GSVmessage,mUsart2ReceivePack,strlen(mUsart2ReceivePack)-2);
////                        GPS_ParseGSV(GPS_GSVmessage,strlen(GPS_GSVmessage));
////                        
////                        }
//            
//            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;
//        }
    }
}