#include <serial_at_cmd_app.h>
|
#include <global_param.h>
|
#include <string.h>
|
#include <stdio.h>
|
#include "Usart.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[100] = {0};
|
|
typedef enum
|
{
|
BLE_RECV_STATE_IDLE = 0,
|
BLE_RECV_STATE_MAC,
|
BLE_RECV_STATE_RSSI,
|
//BLE_RECV_STATE_LF,
|
} E_BLERecvState;
|
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 UsartParseDataHandler(uint8_t data)
|
{
|
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:
|
//´ÓmUsartReceivePackÖжÁÈ¡pack_length³¤¶ÈµÄ×Ö½Ú£¬·Åµ½È«¾Ö±äÁ¿ÖÐ,¸³Öµ±£´æµÄ²ÎÊý²¢ÇÒ´æÈëflash
|
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();
|
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;
|
}
|
|
}
|
|
void Usart2ParseDataHandler(uint8_t data)//UARTÀ¶ÑÀÊý¾Ý·ÖÎö´¦Àí
|
{
|
//´òÓ¡½ÓÊÕµ½µÄÀ¶ÑÀÄ£¿éÐÅÏ¢
|
//printf("Õâ¸öÊÇ´«ÈëµÄÊý¾Ý%c",data);
|
// static UsartRecvPackState usart2_receive_state = UsartReceiveWaitHead0;
|
//static UsartRecvPackState usart2_receive_state = UsartReceiveWaitData;
|
|
static ST_BLERecv BLE_recvive;//´´½¨À¶ÑÀ״̬½á¹¹Ìå
|
|
|
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 dev_id[14],dev_ssi[5],i,k=0;
|
// //char char_mac[14],char_ssi[6];
|
// int j=0;
|
// //uint16_t temp_16,temp_16_id,temp_16_distance;
|
// uint8_t temp8_H,temp8_L;
|
// //float temp_float;
|
// //double ssi_double;//,distance_double;
|
// //int32_t distance_int;
|
// uint16_t Label_id=0; //±êÇ©id
|
|
|
//½ÓÊÕÊý¾Ý¿ªÊ¼·ÖÎö
|
mUsart2ReceivePack[index] = data; //charÊý×é´«½øÀ´²ÎÊýdata
|
mUsart2ReceivePack_before = mUsart2ReceivePack_now;
|
mUsart2ReceivePack_now=data;
|
index++;
|
if( mUsart2ReceivePack_before == 0x0D && mUsart2ReceivePack_now==0x0A )//½ÓÊÕÊý¾Ýµ½¡°0x0D 0x0A¡±½áÊø
|
{
|
mUsart2ReceivePack[index]=0;
|
//printf("0D0A½øÈëºóµÄÊý¾ÝΪ%sµ½´Ë½áÊø",mUsart2ReceivePack);
|
// if(strncmp("RF-CRAZY",(char*)mUsart2ReceivePack,8)==0)
|
// {
|
// BLE_recvive.m_eState=BLE_RECV_STATE_MAC;//Èç¹ûΪÃû×ÖºóÅжϲ¢¸ÄΪMAC½ÓÊÕ״̬
|
// }
|
//
|
switch(BLE_recvive.m_eState)
|
{ case BLE_RECV_STATE_IDLE:
|
if(strncmp("RF-CRAZY",(char*)mUsart2ReceivePack,8)==0)
|
BLE_recvive.m_eState=BLE_RECV_STATE_MAC;//Èç¹ûΪÃû×ÖºóÅжϲ¢¸ÄΪMAC½ÓÊÕ״̬
|
break;
|
case BLE_RECV_STATE_MAC://MAC½ÓÊÜ´¦Àí״̬
|
strcpy(BLE_recvive.m_macHeader,(char*)mUsart2ReceivePack+5);//¸´ÖƵ½À¶ÑÀ½á¹¹ÌåÖÐ,+5ÊÇΪÁËÌø¹ýMAC×Ö·ûºÍðºÅδ´¦Àí0D0A
|
//´¦ÀíMAC×Ö·û²»Èû»ÐÐ
|
char*p=BLE_recvive.m_macHeader+strlen(BLE_recvive.m_macHeader)-2;
|
*p='\0';
|
//printf("´¦Àí¹ýºóµÄMACΪ%s",BLE_recvive.m_macHeader);
|
BLE_recvive.m_eState=BLE_RECV_STATE_RSSI;
|
break;
|
case BLE_RECV_STATE_RSSI://RSSI½ÓÊÕ´¦Àí״̬
|
strcpy(BLE_recvive.m_rssiHeader2,(char*)mUsart2ReceivePack+6);//¸´ÖƵ½À¶ÑÀ½á¹¹ÌåÖÐ,+5ÊÇΪÁËÌø¹ýMAC×Ö·ûºÍðºÅδ´¦Àí0D0A
|
//printf(" ´¦Àí¹ýºóµÄRSSIΪ%s",BLE_recvive.m_rssiHeader2);
|
BLE_recvive.m_eState=BLE_RECV_STATE_IDLE;
|
break;
|
}
|
|
// if(mUsart2ReceivePack[0] ==0x2B ) //¡°+¡±É¨Ãèµ½¿ªÊ¼»ò½áÊø×Ö·û´®µÄÊ׸ö×Ö·û
|
// {
|
// if (mUsart2ReceivePack[5] ==0x20)
|
// {
|
// printf("UARTɨÃè½áÊø :+SCAN END\r\n"); //±¾´ÎɨÃè½áÊø
|
// CT_sum++; //°üÐòÖµ
|
// if(CT_sum>255) CT_sum=0;
|
//
|
// //·¢ËÍÊý¾Ý»º´æ
|
// Lora_TXD_bff[0] = 0x55;
|
// Lora_TXD_bff[1] = 0xAA;
|
// Lora_TXD_bff[2] = 0x12;
|
// Lora_TXD_bff[3] = 8+4*numb_base; //Êý¾Ý³¤¶È 8+4*»ùÕ¾ÊýÁ¿
|
//
|
// Label_id=Label_id_local; //±êÇ©id
|
// memcpy(&Lora_TXD_bff[4],&Label_id ,2);
|
//
|
// Lora_TXD_bff[6] = CT_sum; //±êÇ©°üÐò
|
// Lora_TXD_bff[7] = 0x32; //°´¼ü/µçÁ¿,²âÊÔÊý¾Ý50
|
// Lora_TXD_bff[8] = 0; //LORAµÈ´ýÓ¦´ð
|
|
// Lora_TXD_bff[9]= numb_base; //»ùÕ¾ÊýÁ¿
|
|
// for(i=10;i<(10+numb_base*2);i=i+2)
|
// {
|
// memcpy(&Lora_TXD_bff[i+0] ,&data_buff[(i-10)/2][0] ,2); //»ùÕ¾ID
|
// memcpy(&Lora_TXD_bff[i+numb_base*2],&data_buff[(i-10)/2][1] ,2); //»ùÕ¾¾àÀë
|
// }
|
// for( i = 2;i<(4+8+4*numb_base); i++) //È¥°üÍ·ºó УÑé ÀÛ¼ÓÈ¡·´
|
// {
|
// checksum += Lora_TXD_bff[i];
|
// }
|
// checksum = ~checksum;
|
// memcpy(&Lora_TXD_bff[2+8+4*numb_base],&checksum ,2); //ºÍÈ¡·´ УÑé
|
//
|
// Radio_init(); //»½ÐÑ Îª·¢ËÍ״̬
|
// Radio.Send( Lora_TXD_bff, (4+8+4*numb_base)); //LORAÄ£¿éÉÏ´«Êý¾Ý
|
//
|
|
// //»ùÕ¾¾àÀë ÇåÁã
|
// for(i=0;i<numb_base;i++)
|
// {
|
// //for(k=1;k<2;k++) //Çå¾àÀëÊý¾Ý
|
// for(k=0;k<2;k++) //ÇåIDºÅºÍ¾àÀëÊý¾Ý
|
// {
|
// data_buff[i][k]=0;
|
// }
|
// }
|
// numb_base =0;
|
// data_buff_start =0;
|
//
|
// printf("\r\n");
|
// //delay_ms(5);
|
// Uart2_SendString( Uart2_str); //·¢ÏÂÒ»´ÎɨÃèÖ¸Áî
|
// }
|
//
|
// else if(mUsart2ReceivePack[5] ==0x5F) //¡°_"Ï»®Ïß
|
// {
|
// printf("UARTɨÃ迪ʼ :+SCAN_MANU:\r\n");
|
// }
|
// }
|
// else
|
// {
|
// j=memcmp(id_cmpare,mUsart2ReceivePack,8); //±È½ÏÄ¿±êÀ¶ÑÀµØÖ· ǰ4¸ö×Ö½Ú
|
// if(j==0)//¸úÄ¿±êÀ¶ÑÀmacµØÖ·Ïàͬ
|
// {
|
//
|
// //¿½±´macµØÖ·
|
// for(i=0;i<12;i++)
|
// {
|
// char_mac[i] = mUsart2ReceivePack[i];
|
// }
|
// char_mac[12]='\0';
|
//
|
//
|
// //¿½±´ÐźÅ
|
// for(i=15;i<18;i++)
|
// {
|
// char_ssi[i-15] = mUsart2ReceivePack[i];
|
// }
|
// if(mUsart2ReceivePack[18]==0x0D || mUsart2ReceivePack[18]==0x20) //ÐźÅÊÇ-Á½Î»Êý
|
// {
|
// char_ssi[3]= '\0' ;
|
// char_broadcast_data[0]= '\0';
|
// if (mUsart2ReceivePack[18]==0x0D) //ÐźÅÊÇ-Á½Î»Êý£¬Î޹㲥ÄÚÈÝ
|
// {
|
// CT_satrt=0;
|
// }
|
// else if(mUsart2ReceivePack[18]==0x20) //ÐźÅÊÇ-Á½Î»Êý£¬Óй㲥ÄÚÈÝ,¿Õ¸ñºóһλ¿ªÊ¼£¬µ½0x0D ½áÊø¡£
|
// {
|
// CT_satrt=19;
|
// }
|
// ssi_double = -((char_ssi[1]-0x30)*10 + (char_ssi[2]-0x30)); //2λÐźÅÖµint
|
// }
|
// else if(mUsart2ReceivePack[18]>=0x30 && mUsart2ReceivePack[18]<=0x39 ) //ÐźÅÊÇ-ÈýλÊý
|
// {
|
// char_ssi[3]=mUsart2ReceivePack[18];
|
// char_ssi[4]= '\0' ;
|
// char_broadcast_data[0]= '\0';
|
// if (mUsart2ReceivePack[19]==0x0D) //ÐźÅÊÇ-ÈýλÊý£¬Î޹㲥ÄÚÈÝ
|
// {
|
// CT_satrt=0;
|
// }
|
// else if(mUsart2ReceivePack[19]==0x20) //ÐźÅÊÇ-ÈýλÊý£¬Óй㲥ÄÚÈÝ,¿Õ¸ñºóһλ¿ªÊ¼£¬µ½0x0D ½áÊø¡£
|
// {
|
// CT_satrt=20;
|
// }
|
// ssi_double = -((char_ssi[1]-0x30)*100 + (char_ssi[2]-0x30)*10 + (char_ssi[3]-0x30));//3λÐźÅÖµint
|
// }
|
//
|
// printf("mac= %s ",char_mac);
|
// printf("ssi= %s ",char_ssi);
|
|
// //»ùÕ¾ID´¦Àí
|
// temp_16_id = (char_mac[8]-0x30)*1000 + (char_mac[9]-0x30)*100 + (char_mac[10]-0x30)*10 + (char_mac[11]-0x30);
|
// printf("ID = %d ",temp_16_id );
|
// //»ùÕ¾¾àÀë´¦Àí
|
// temp_16_distance = 0.003083 * exp(-0.09909*ssi_double)*100;//µ¥Î»cm
|
// printf("¾àÀë = %d cm \r\n",temp_16_distance );
|
|
// //±È½ÏIDºÅ²¢¿½±´Êý¾Ýµ½»º´æÇø
|
// for(i=0;i<data_buff_MAX;i++)
|
// {
|
// if(data_buff[i][0] == temp_16_id) //¼ì²âµ½ÒÑ´æID
|
// {
|
// data_buff[i][1] = temp_16_distance;
|
// data_buff_start=0;
|
// break;
|
// }
|
// else if (data_buff[i][0]==0)
|
// {
|
// data_buff_start=i+1;
|
// numb_base=data_buff_start; //¿½±´É¨Ãèµ½»ùÕ¾µÄÊýÁ¿
|
// break;
|
// }
|
// }
|
// if(data_buff_start!=0)
|
// {
|
// data_buff[data_buff_start-1][0] = temp_16_id;
|
// data_buff[data_buff_start-1][1] = temp_16_distance;
|
//
|
// }
|
// }
|
// }
|
index=0;
|
mUsart2ReceivePack_before=0;
|
mUsart2ReceivePack_now=0;
|
}
|
//printf("´¦Àí¹ýºóµÄÊý×éΪ%s\n",mUsart2ReceivePack);
|
}
|