#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"
|
|
|
|
//#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};
|
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;
|
extern uint8_t gps_success_state,gps_enable_flag,gps_need_data_flag,gps_timeout_flag;
|
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;
|
#define buff_lenth 1024
|
#define MAX_BASE_STATIONS 10
|
uint8_t DMA_RXBuf_BT[200]={0};
|
uint8_t Forward_BT_buff[buff_lenth]={0};
|
uint8_t report_ancnum_bt=0;
|
|
void UsartParseDataHandler(uint8_t data)
|
{
|
uint8_t buff[buff_lenth] = {0};
|
uint8_t length = 0;
|
uint8_t base_station_count = 0;
|
memcpy(buff, m_EUART_DMA_RXBuf, 1024);
|
//memcpy(buff, Forward_BT_buff, 1024);
|
// ±éÀú
|
for (int n = 0; n < 1024 - 6; n++)
|
{
|
// °üÍ· 0x55 0xAA 0x41
|
if (buff[n] == 0x55 && buff[n+1] == 0xAA && buff[n+2] == 0x41)
|
{
|
report_ancnum_bt=buff[n+4];
|
length = buff[n+3];
|
if (n + 6 + report_ancnum_bt * 6 + length > 1024) {
|
continue; // Èç¹ûÊý¾ÝÔ½½ç£¬Ìø¹ý¸Ã°ü
|
}
|
|
// ½«Êý¾Ý¸´ÖƵ½ DMA »º³åÇø
|
memcpy(DMA_RXBuf_BT, &buff[n+4], length);
|
|
// ΪÁ˱ÜÃâ¶à´ÎÖØ¸´½âÎöͬһ¸öÊý¾Ý°ü£¬¿ÉÒÔÔÚÕâÀïÌí¼ÓÌøÔ¾
|
n += 6 + report_ancnum_bt * 6 + length - 1; // Ìø¹ýµ±Ç°Êý¾Ý°ü
|
}
|
}
|
|
// Çå³ýÁÙʱ»º³åÇø
|
memset(buff, 0, buff_lenth);
|
|
// 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;
|
|
|
|
|
// 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 {
|
// 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;
|
//
|
// //½ÓÊÕÊý¾Ý¿ªÊ¼·ÖÎö
|
// 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))
|
// {
|
// if(g_com_map[MODBUS_MODE])
|
// LOG_INFO(TRACE_MODULE_APP,"%s", mUsart2ReceivePack);
|
// d_value = strtod(wdrecv.m_pData,NULL);
|
// if(d_value>1)
|
// {
|
// blink_led(&gps_success_state);
|
// wd=strtod(wdrecv.m_pData,NULL);
|
// jd=strtod(jdrecv.m_pData,NULL);
|
// pos_state=atoi(Posstate.m_pData);
|
// }
|
// }
|
// index = 0;
|
// if(pos_state!=0)
|
// {
|
// //4g.jd=jd;
|
// //4g.wd=wd;
|
// gps_timeout_flag=1;//²»³¬Ê±½ÓÊÕ״̬
|
// gps_need_data_flag=0;//½ÓÊÕÊý¾ÝÍê³É
|
// gps_wait_count=0;//Çå0½ÓÊÕ״̬
|
//// gps_enable_flag=0;//ÊÕµ½ÓÐЧÊý¾Ý¹Ø±ÕGPS
|
// }
|
// 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;
|
// }
|
// }
|
|
}
|