stm32 物联网 lorawan ra-08_ra-08h

#include "bsp.h"  
#define CHAR_TO_UPPER(ch)   ((ch >= 'a' && ch <= 'z')?(ch-0x20):ch)
unsigned char Hexbuf[512];

char LORA_TX[512],LORA_RX[512];
int LORA_TX_length,LORA_RX_length;
char LORA_DATA_buf[512];
char LORA_COM_buf[512];
char LORA_SEND_buf[512];

char LORA_AT_state;
char LORA_state;
char LORA_SEND0,LORA_SEND1,LORA_SEND2,LORA_SEND3;
char LORA_Connections;														//LORAÁ¬½Ó±êÖ¾    				0£ºÎ´Á¬½Ó  						
char LORA_TEMP_str[32];

char U4_TX_data[512],U4_RX_data[512];
unsigned char U4_TX_flag,U4_RX_flag;
u16 U4_Tx_Counter,U4_Rx_Counter;
struct                                
{    
	u16 LORA_T_Head;           
	u16 LORA_T_Tail;
	u16 LORA_T_Lenght;
	char LORA_T_Buf[4][512];
	int LORA_T_Len[4];
}LORA_T_Ring;

void LORA_T_Ring_Init(void)
{
	LORA_T_Ring.LORA_T_Head=0;           
	LORA_T_Ring.LORA_T_Tail=0;
	LORA_T_Ring.LORA_T_Lenght=0;
	LORA_T_Ring.LORA_T_Len[0]=0;
	LORA_T_Ring.LORA_T_Len[1]=0;	
	LORA_T_Ring.LORA_T_Len[2]=0;	
	LORA_T_Ring.LORA_T_Len[3]=0;		
}
unsigned char LORA_T_Write_Ring(char *data,unsigned int len)
{	
	if(LORA_T_Ring.LORA_T_Lenght >= 4) 
 {
	return 0;
 }
 LORA_T_Ring.LORA_T_Len[LORA_T_Ring.LORA_T_Tail]=len;
 memcpy(LORA_T_Ring.LORA_T_Buf[LORA_T_Ring.LORA_T_Tail],data,len);
 LORA_T_Ring.LORA_T_Tail = (LORA_T_Ring.LORA_T_Tail+1)%4;
 LORA_T_Ring.LORA_T_Lenght++;
 return 1;
}
unsigned char LORA_T_Read_Ring(char *Data)
{
 int len;
 if(LORA_T_Ring.LORA_T_Lenght == 0)
 {
  return 0;
 }
 len=LORA_T_Ring.LORA_T_Len[LORA_T_Ring.LORA_T_Head];
 memcpy(Data,LORA_T_Ring.LORA_T_Buf[LORA_T_Ring.LORA_T_Head],len);
 memset(LORA_T_Ring.LORA_T_Buf[LORA_T_Ring.LORA_T_Head],0,len);
 LORA_T_Ring.LORA_T_Len[LORA_T_Ring.LORA_T_Head]=0;
 LORA_T_Ring.LORA_T_Head = (LORA_T_Ring.LORA_T_Head+1)%4;
 LORA_T_Ring.LORA_T_Lenght--;
 return len;
}
struct                                
{    
	u16 LORA_R_Head;           
	u16 LORA_R_Tail;
	u16 LORA_R_Lenght;
	char LORA_R_Buf[4][512];
	int LORA_R_Len[4];
}LORA_R_Ring;

void LORA_R_Ring_Init(void)
{
	LORA_R_Ring.LORA_R_Head=0;           
	LORA_R_Ring.LORA_R_Tail=0;
	LORA_R_Ring.LORA_R_Lenght=0;
	LORA_R_Ring.LORA_R_Len[0]=0;
	LORA_R_Ring.LORA_R_Len[1]=0;	
	LORA_R_Ring.LORA_R_Len[2]=0;	
	LORA_R_Ring.LORA_R_Len[3]=0;		
}
unsigned char LORA_R_Write_Ring(char *data,unsigned int len)
{	
	if(LORA_R_Ring.LORA_R_Lenght >= 4) 
 {
	return 0;
 }
 LORA_R_Ring.LORA_R_Len[LORA_R_Ring.LORA_R_Tail]=len;
 memcpy(LORA_R_Ring.LORA_R_Buf[LORA_R_Ring.LORA_R_Tail],data,len);
 LORA_R_Ring.LORA_R_Tail = (LORA_R_Ring.LORA_R_Tail+1)%4;
 LORA_R_Ring.LORA_R_Lenght++;
 return 1;
}
unsigned char LORA_R_Read_Ring(char *Data)
{
 int len;
 if(LORA_R_Ring.LORA_R_Lenght == 0)
 {
  return 0;
 }
 len=LORA_R_Ring.LORA_R_Len[LORA_R_Ring.LORA_R_Head];
 memcpy(Data,LORA_R_Ring.LORA_R_Buf[LORA_R_Ring.LORA_R_Head],len);
 memset(LORA_R_Ring.LORA_R_Buf[LORA_R_Ring.LORA_R_Head],0,len);
 LORA_R_Ring.LORA_R_Len[LORA_R_Ring.LORA_R_Head]=0;
 LORA_R_Ring.LORA_R_Head = (LORA_R_Ring.LORA_R_Head+1)%4;
 LORA_R_Ring.LORA_R_Lenght--;
 return len;
}
unsigned char Ascii2Hex(unsigned char *hex, unsigned char *ascii, unsigned char asciiLen)
{
    unsigned char i,ch,value;
    value = 0;
    for(i=0;i<(asciiLen>>1);i++) {
        ch = CHAR_TO_UPPER(ascii[i*2]);
        if(ch >= '0' && ch <= '9') {
            value = ch -'0';
        }
        else if(ch >= 'A' && ch <= 'F') {
            value = ch - 'A' + 0x0A;
        }
        else {
            return i;
        }
        hex[i] = (value<<4);
        ch = CHAR_TO_UPPER(ascii[i*2+1]);
        if(ch >= '0' && ch <= '9') {
            value = ch -'0';
        }
        else if(ch >= 'A' && ch <= 'F') {
            value = ch - 'A' + 0x0A;
        }
        else {
            return i;
        }
        hex[i] += value;
    }
    return i;
}
void UART4_Config(void)
{		
	int i,j;
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef   NVIC_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4 , ENABLE);
	
  /* Configure USART4 Tx (PC.10) as alternate function push-pull */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_InitStructure);    
	/* Configure USART4 Rx (PC.11) as input floating */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOC, &GPIO_InitStructure);
	
	USART_InitStructure.USART_BaudRate = 9600;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No ;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(UART4, &USART_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;     
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;     
	NVIC_InitStructure.NVIC_IRQChannelSubPriority=4;     
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;     
	NVIC_Init(&NVIC_InitStructure);
 
	USART_ITConfig(UART4, USART_IT_IDLE , ENABLE); 
	USART_ITConfig(UART4, USART_IT_RXNE , ENABLE);  
	USART_Cmd(UART4, ENABLE);		
}

void U4_SendString(char *buf1)		  
{
	unsigned int i;
	unsigned int len;
	len=strlen(buf1);

	for(i=0;iSR;        
		num = UART4->DR;
		                                                  
		U4_RX_flag=1;       														//½ÓÊÕÊý¾Ý±ê־λÖÃ1  
		
		if(buf=strstr(U4_RX_data, "OK+RECV:"))
		{
			Ascii2Hex(Hexbuf, buf+14,4);
			num=Hexbuf[0];
			Ascii2Hex(Hexbuf, buf+17,2*num);
			UART2_T_Write_Ring(Hexbuf,num);	
			UART3_T_Write_Ring(Hexbuf,num);
			UART5_T_Write_Ring(Hexbuf,num);
		}
		LORA_R_Write_Ring(U4_RX_data,U4_Rx_Counter);
		U4_Rx_Counter=0;
		USART_ClearITPendingBit(UART4,USART_IT_IDLE);  //ÇåUSART_IT_IDLE±êÖ¾  
	}
	if(USART_GetITStatus(UART4,USART_IT_RXNE) != RESET)     
	{        
		U4_RX_data[U4_Rx_Counter++] =USART_ReceiveData(UART4);    //
		if(U4_Rx_Counter>512)U4_Rx_Counter=0;
		USART_ClearITPendingBit(UART4,USART_IT_RXNE);                                    //ÇåUSART_IT_IDLE±êÖ¾                 
	}
}
void LED2_OPEN(void)
{
	GPIO_WriteBit(GPIOE, GPIO_Pin_14, Bit_SET);
}
void LED2_CLOSE(void)
{
	GPIO_WriteBit(GPIOE, GPIO_Pin_14, Bit_RESET);
}
void LORA_ATTR(int time1,int time2,int time3,char * buf1,char * buf2,char * buf3,char * buf4,char * buf5,char * buf6,char r ,char w)
{
 unsigned int len;
	
	switch(LORA_AT_state)
	{
		case state_idle:
			
			if(LORA_Delay_Timer>time1)
			{
				LORA_Delay_Timer=0;
				LORA_Timeslimite++;
				len=strlen(buf1);
				USART4_Send(buf1,len);
				LORA_AT_state=state_run;
			}
			break;
		case state_run:
			LED2_OPEN();
			LORA_Delay_Timer=0;
			if(LORA_R_Read_Ring(LORA_DATA_buf)>0)
			{
				if((strstr(LORA_DATA_buf, buf2))&&(strlen(buf2)))  //×Ö·û´®±È½Ï²»ÄÜÓÐ00£¬¹Ø±Õ»ØÏÔ±ÜÃâÊý¾ÝÖеÄ00Ó°Ïì±È½Ï
				{
					LORA_AT_state=state_right; 
				}
				else if((strstr(LORA_DATA_buf, buf3))&&(strlen(buf3)))
				{
					LORA_AT_state=state_right; 
				} 
				else if((strstr(LORA_DATA_buf, buf4))&&(strlen(buf4)))
				{
					LORA_AT_state=state_wrong; 
				}
				else if((strstr(LORA_DATA_buf, buf5))&&(strlen(buf5)))
				{
					LORA_AT_state=state_wrong; 
				} 
				else if((strstr(LORA_DATA_buf, buf6))&&(strlen(buf6)))
				{
					LORA_AT_state=state_wrong; 
				}
			}
			if(LORA_Timeslimite>time2)
			{
				LORA_AT_state=state_wrong;
			}
			if(LORA_overtime>time3)
			{
				LORA_AT_state=state_wrong;
			}
			break;
		case state_right:
			LORA_state=r;
			LORA_AT_state=state_idle;
			LORA_Delay_Timer=0;
			LORA_overtime=0;
			LORA_Timeslimite=0;
			LED2_CLOSE();
			memset(LORA_DATA_buf,0,512);
			break;
		case state_wrong:
			LORA_state=w;
			LORA_AT_state=state_idle;
			LORA_Delay_Timer=0;
			LORA_overtime=0;
			LORA_Timeslimite=0;
			LED2_CLOSE();
			memset(LORA_DATA_buf,0,512);
			break;
		default:  
			break;
	}
}
void LORA_RST(void)
{
	int i,j;
	GPIO_WriteBit(GPIOB, GPIO_Pin_0, Bit_RESET);
	for(i=0;i<1000;i++)for(j=0;j<1000;j++);
	GPIO_WriteBit(GPIOB, GPIO_Pin_0, Bit_SET);
	for(i=0;i<1000;i++)for(j=0;j<1000;j++);
}

/*********************************************END OF FILE**********************/
#ifndef __BSP_UART4_H
#define	__BSP_UART4_H
void LORA_T_Ring_Init(void);
unsigned char LORA_T_Write_Ring(char *data,unsigned int len);
unsigned char LORA_T_Read_Ring(char *Data);
void LORA_R_Ring_Init(void);
unsigned char LORA_R_Write_Ring(char *data,unsigned int len);
unsigned char LORA_R_Read_Ring(char *Data);
unsigned char Ascii2Hex(unsigned char *hex, unsigned char *ascii, unsigned char asciiLen);
void UART4_Config(void);
void U4_SendString(char *buf1);	  
void USART4_Send(char *buf1,char len);	
void LORA_R_Ring_Init(void);
void LORA_T_Ring_Init(void);
void LED2_OPEN(void);
void LED2_CLOSE(void);
void LORA_ATTR(int time1,int time2,int time3,char * buf1,char * buf2,char * buf3,char * buf4,char * buf5,char * buf6,char r ,char w);
void LORA_RST(void);
#endif /* __BSP_UART4_H */

你可能感兴趣的:(stm32,物联网,嵌入式硬件)