STM32 物联网 4G CAT1 SIMCOM A7680C 源码

基于状态机编写4G模块驱动函数


#include "bsp.h"  
char LTE_TX[512],LTE_RX[512];
int LTE_TX_length,LTE_RX_length;
char U1_TX_data[512],U1_RX_data[512];
char LTE_DATA_buf[512];
char LTE_COM_buf[512];
char LTE_SEND_buf[512];

unsigned char U1_TX_flag,U1_RX_flag;
u16 U1_Tx_Counter,U1_Rx_Counter;
char LTE_AT_state;
char LTE_state;
char LTE_SEND0,LTE_SEND1,LTE_SEND2,LTE_SEND3;
char LTE_Connections;														//LTEÁ¬½Ó±êÖ¾    				0£ºÎ´Á¬½Ó  						
char LTE_TEMP_str[32];

void UART1_Config(void)
{		
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef   NVIC_InitStructure;
	DMA_InitTypeDef    DMA_Initstructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOE, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
	RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2 , ENABLE); 	
//4G POWER	
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOE, ENABLE);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;   
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_Init(GPIOE, &GPIO_InitStructure);			
	GPIO_ResetBits(GPIOE, GPIO_Pin_1);  
//4G_PWRKEY	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 ;	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;   
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_Init(GPIOE, &GPIO_InitStructure);
  GPIO_SetBits(GPIOE, GPIO_Pin_3);             
//4G_RESET		
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;   
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	GPIO_ResetBits(GPIOB, GPIO_Pin_1);    
//4G_RELAY	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;   
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_Init(GPIOE, &GPIO_InitStructure);
  GPIO_SetBits(GPIOE, GPIO_Pin_8);
	GPIO_SetBits(GPIOE, GPIO_Pin_9);  
	
/* USART1 GPIO config */
	/* Configure USART1 Tx (PA.09) as alternate function push-pull */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);    
	/* Configure USART1 Rx (PA.10) as input floating */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA, &GPIO_InitStructure);	

	/* USART1 mode config */
	USART_InitStructure.USART_BaudRate = 115200;
	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(USART1, &USART_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;     
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;     
	NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;     
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;     
	NVIC_Init(&NVIC_InitStructure);
	
	/*uart1 RX DMAÅäÖÃ*/    
	DMA_Initstructure.DMA_PeripheralBaseAddr =  (u32)(&USART1->DR);    
	DMA_Initstructure.DMA_MemoryBaseAddr     = (u32)U1_RX_data;    
	DMA_Initstructure.DMA_DIR = DMA_DIR_PeripheralSRC;    
	DMA_Initstructure.DMA_BufferSize = 512;    
	DMA_Initstructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;    
	DMA_Initstructure.DMA_MemoryInc =DMA_MemoryInc_Enable;    
	DMA_Initstructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;    
	DMA_Initstructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;    
	DMA_Initstructure.DMA_Mode = DMA_Mode_Normal;    
	DMA_Initstructure.DMA_Priority = DMA_Priority_High;    
	DMA_Initstructure.DMA_M2M = DMA_M2M_Disable;    
	DMA_Init(DMA1_Channel5,&DMA_Initstructure);       //Æô¶¯DMA    
	DMA_Cmd(DMA1_Channel5,ENABLE);
	
	USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);     
	USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
	USART_Cmd(USART1, ENABLE);
}

struct                                //×÷Òµ²ÉÑù
{    
	u16 LTE_Head;           
	u16 LTE_Tail;
	u16 LTE_Lenght;
	char LTE_Buf[4][512];
	int LTE_Len[4];
}LTE_Ring;

void LTE_Ring_Init(void)
{
	LTE_Ring.LTE_Head=0;           
	LTE_Ring.LTE_Tail=0;
	LTE_Ring.LTE_Lenght=0;
	LTE_Ring.LTE_Len[0]=0;
	LTE_Ring.LTE_Len[1]=0;	
	LTE_Ring.LTE_Len[2]=0;	
	LTE_Ring.LTE_Len[3]=0;		
}
unsigned char LTE_Write_Ring(char *data,unsigned int len)
{	
	if(LTE_Ring.LTE_Lenght >= 4) 
 {
	return 0;
 }
 LTE_Ring.LTE_Len[LTE_Ring.LTE_Tail]=len;
 memcpy(LTE_Ring.LTE_Buf[LTE_Ring.LTE_Tail],data,len);
 LTE_Ring.LTE_Tail = (LTE_Ring.LTE_Tail+1)%4;
 LTE_Ring.LTE_Lenght++;
 return 1;
}
unsigned char LTE_Read_Ring(char *Data)
{
 int len;
 if(LTE_Ring.LTE_Lenght == 0)
 {
  return 0;
 }
 len=LTE_Ring.LTE_Len[LTE_Ring.LTE_Head];
 memcpy(Data,LTE_Ring.LTE_Buf[LTE_Ring.LTE_Head],len);
 memset(LTE_Ring.LTE_Buf[LTE_Ring.LTE_Head],0,len);
 LTE_Ring.LTE_Len[LTE_Ring.LTE_Head]=0;
 LTE_Ring.LTE_Head = (LTE_Ring.LTE_Head+1)%4;
 LTE_Ring.LTE_Lenght--;
 return len;
}

struct                                //×÷Òµ²ÉÑù
{    
	u16 LTE_Head;           
	u16 LTE_Tail;
	u16 LTE_Lenght;
	char LTE_Buf[4][512];
	int LTE_Len[4];
}LTE1_T_Ring;
void LTE1_T_Ring_Init(void)
{
	LTE1_T_Ring.LTE_Head=0;           
	LTE1_T_Ring.LTE_Tail=0;
	LTE1_T_Ring.LTE_Lenght=0;
	LTE1_T_Ring.LTE_Len[0]=0;
	LTE1_T_Ring.LTE_Len[1]=0;	
	LTE1_T_Ring.LTE_Len[2]=0;	
	LTE1_T_Ring.LTE_Len[3]=0;		
}
unsigned char LTE1_T_Write_Ring(char *data,unsigned int len)
{	
	if(LTE1_T_Ring.LTE_Lenght >= 4) 
 {
	return 0;
 }
 LTE1_T_Ring.LTE_Len[LTE1_T_Ring.LTE_Tail]=len;
 memcpy(LTE1_T_Ring.LTE_Buf[LTE1_T_Ring.LTE_Tail],data,len);
 LTE1_T_Ring.LTE_Tail = (LTE1_T_Ring.LTE_Tail+1)%4;
 LTE1_T_Ring.LTE_Lenght++;
 return 1;
}
unsigned char LTE1_T_Read_Ring(char *Data)
{
 int len;
 if(LTE1_T_Ring.LTE_Lenght == 0)
 {
  return 0;
 }
 len=LTE1_T_Ring.LTE_Len[LTE1_T_Ring.LTE_Head];
 memcpy(Data,LTE1_T_Ring.LTE_Buf[LTE1_T_Ring.LTE_Head],len);
 memset(LTE1_T_Ring.LTE_Buf[LTE1_T_Ring.LTE_Head],0,len);
 LTE1_T_Ring.LTE_Len[LTE1_T_Ring.LTE_Head]=0;
 LTE1_T_Ring.LTE_Head = (LTE1_T_Ring.LTE_Head+1)%4;
 LTE1_T_Ring.LTE_Lenght--;
 return len;
}
struct                                //×÷Òµ²ÉÑù
{    
	u16 LTE_Head;           
	u16 LTE_Tail;
	u16 LTE_Lenght;
	char LTE_Buf[4][512];
	int LTE_Len[4];
}LTE2_T_Ring;
void LTE2_T_Ring_Init(void)
{
	LTE2_T_Ring.LTE_Head=0;           
	LTE2_T_Ring.LTE_Tail=0;
	LTE2_T_Ring.LTE_Lenght=0;
	LTE2_T_Ring.LTE_Len[0]=0;
	LTE2_T_Ring.LTE_Len[1]=0;	
	LTE2_T_Ring.LTE_Len[2]=0;	
	LTE2_T_Ring.LTE_Len[3]=0;		
}
unsigned char LTE2_T_Write_Ring(char *data,unsigned int len)
{	
	if(LTE2_T_Ring.LTE_Lenght >= 4) 
 {
	return 0;
 }
 LTE2_T_Ring.LTE_Len[LTE2_T_Ring.LTE_Tail]=len;
 memcpy(LTE2_T_Ring.LTE_Buf[LTE2_T_Ring.LTE_Tail],data,len);
 LTE2_T_Ring.LTE_Tail = (LTE2_T_Ring.LTE_Tail+1)%4;
 LTE2_T_Ring.LTE_Lenght++;
 return 1;
}
unsigned char LTE2_T_Read_Ring(char *Data)
{
 int len;
 if(LTE2_T_Ring.LTE_Lenght == 0)
 {
  return 0;
 }
 len=LTE2_T_Ring.LTE_Len[LTE2_T_Ring.LTE_Head];
 memcpy(Data,LTE2_T_Ring.LTE_Buf[LTE2_T_Ring.LTE_Head],len);
 memset(LTE2_T_Ring.LTE_Buf[LTE2_T_Ring.LTE_Head],0,len);
 LTE2_T_Ring.LTE_Len[LTE2_T_Ring.LTE_Head]=0;
 LTE2_T_Ring.LTE_Head = (LTE2_T_Ring.LTE_Head+1)%4;
 LTE2_T_Ring.LTE_Lenght--;
 return len;
}
struct                                //×÷Òµ²ÉÑù
{    
	u16 LTE_Head;           
	u16 LTE_Tail;
	u16 LTE_Lenght;
	char LTE_Buf[4][512];
	int LTE_Len[4];
}LTE3_T_Ring;
void LTE3_T_Ring_Init(void)
{
	LTE3_T_Ring.LTE_Head=0;           
	LTE3_T_Ring.LTE_Tail=0;
	LTE3_T_Ring.LTE_Lenght=0;
	LTE3_T_Ring.LTE_Len[0]=0;
	LTE3_T_Ring.LTE_Len[1]=0;	
	LTE3_T_Ring.LTE_Len[2]=0;	
	LTE3_T_Ring.LTE_Len[3]=0;		
}
unsigned char LTE3_T_Write_Ring(char *data,unsigned int len)
{	
	if(LTE3_T_Ring.LTE_Lenght >= 4) 
 {
	return 0;
 }
 LTE3_T_Ring.LTE_Len[LTE3_T_Ring.LTE_Tail]=len;
 memcpy(LTE3_T_Ring.LTE_Buf[LTE3_T_Ring.LTE_Tail],data,len);
 LTE3_T_Ring.LTE_Tail = (LTE3_T_Ring.LTE_Tail+1)%4;
 LTE3_T_Ring.LTE_Lenght++;
 return 1;
}
unsigned char LTE3_T_Read_Ring(char *Data)
{
 int len;
 if(LTE3_T_Ring.LTE_Lenght == 0)
 {
  return 0;
 }
 len=LTE3_T_Ring.LTE_Len[LTE3_T_Ring.LTE_Head];
 memcpy(Data,LTE3_T_Ring.LTE_Buf[LTE3_T_Ring.LTE_Head],len);
 memset(LTE3_T_Ring.LTE_Buf[LTE3_T_Ring.LTE_Head],0,len);
 LTE3_T_Ring.LTE_Len[LTE3_T_Ring.LTE_Head]=0;
 LTE3_T_Ring.LTE_Head = (LTE3_T_Ring.LTE_Head+1)%4;
 LTE3_T_Ring.LTE_Lenght--;
 return len;
}
void USART1_IRQHandler(void)
{
	unsigned int i,j;
  char *buf;
	if(USART_GetITStatus(USART1,USART_IT_IDLE) == SET)     
	{                                                 
		USART1->SR;        
		USART1->DR;
		DMA_Cmd(DMA1_Channel5,DISABLE);                        
		U1_Rx_Counter = 512 - DMA_GetCurrDataCounter(DMA1_Channel5);        
		if(buf=strstr(U1_RX_data, "RECV FROM:"))
		{
			if(buf=strstr(U1_RX_data, "114.116.6.187:10000"))
			{
				if(buf=strstr(U1_RX_data, "+IPD"))
				{
					for(i=0;i<512;i++)
					{
						if(buf[i]==0x0d)
						{
							if((buf[25]==0x70)&&(buf[26]==0x01)&&(buf[29]==0x01))  //+2µÚÒ»¸ö×Ö½Ú
							{
								for(j=0;j<30;j++)
								{
									LTE_IP[j]=buf[j+32];
								}
								LTE_PORT=(buf[62]<<8)+buf[63];
								for(j=0;j<4;j++)
								{
									local_ip[j]=buf[j+64];
								}
								for(j=0;j<4;j++)
								{
									mask_ip[j]=buf[j+68];
								}
								for(j=0;j<4;j++)
								{
									gateway_ip[j]=buf[j+72];
								}
								for(j=0;j<32;j++)
								{
									Lorawan_EUI[j]=buf[j+83];
								}
								for(j=0;j<32;j++)
								{
									Lorawan_DevAddr[j]=buf[j+115];
								}
								LTE_Connections=1;
							}
							if((buf[25]==0x70)&&(buf[26]==0x01)&&(buf[29]==0x00))
							{
								LTE_Connections=0;
							}
							break;
						}
					}
				}				
			}
			if(buf=strstr(U1_RX_data, "114.116.6.187:20000"))
			{
				if(buf=strstr(U1_RX_data, "+IPD"))
				{
					for(i=0;i<512;i++)
					{
						if(buf[i]==0x0d)
						{
							if(i==5)UART2_T_Write_Ring((buf+7),(buf[4]-0x30));//1×Ö½Ú
							if(i==6)UART2_T_Write_Ring((buf+8),(((buf[4]-0x30)*10)+(buf[5]-0x30)));//2×Ö½Ú
							if(i==7)UART2_T_Write_Ring((buf+9),(((buf[4]-0x30)*100)+((buf[5]-0x30)*10)+(buf[6]-0x30)));//3×Ö½Ú
							if(i==8)UART2_T_Write_Ring((buf+10),(((buf[4]-0x30)*1000)+((buf[5]-0x30)*100)+((buf[6]-0x30)*10)+(buf[7]-0x30)));//4×Ö½Ú
							break;
						}
					}	
				}
			}
			if(buf=strstr(U1_RX_data, "114.116.6.187:30000"))
			{
				if(buf=strstr(U1_RX_data, "+IPD"))
				{
					for(i=0;i<512;i++)
					{
						if(buf[i]==0x0d)
						{
							if(i==5)UART3_T_Write_Ring((buf+7),(buf[4]-0x30));//1×Ö½Ú
							if(i==6)UART3_T_Write_Ring((buf+8),(((buf[4]-0x30)*10)+(buf[5]-0x30)));//2×Ö½Ú
							if(i==7)UART3_T_Write_Ring((buf+9),(((buf[4]-0x30)*100)+((buf[5]-0x30)*10)+(buf[6]-0x30)));//3×Ö½Ú
							if(i==8)UART3_T_Write_Ring((buf+10),(((buf[4]-0x30)*1000)+((buf[5]-0x30)*100)+((buf[6]-0x30)*10)+(buf[7]-0x30)));//4×Ö½Ú
							break;
						}
					}	
				}
			}
			if(buf=strstr(U1_RX_data, "114.116.6.187:40000"))
			{
				if(buf=strstr(U1_RX_data, "+IPD"))
				{
					for(i=0;i<512;i++)
					{
						if(buf[i]==0x0d)
						{
							if(i==5)UART5_T_Write_Ring((buf+7),(buf[4]-0x30));//1×Ö½Ú
							if(i==6)UART5_T_Write_Ring((buf+8),(((buf[4]-0x30)*10)+(buf[5]-0x30)));//2×Ö½Ú
							if(i==7)UART5_T_Write_Ring((buf+9),(((buf[4]-0x30)*100)+((buf[5]-0x30)*10)+(buf[6]-0x30)));//3×Ö½Ú
							if(i==8)UART5_T_Write_Ring((buf+10),(((buf[4]-0x30)*1000)+((buf[5]-0x30)*100)+((buf[6]-0x30)*10)+(buf[7]-0x30)));//4×Ö½Ú
							break;
						}
					}		
				}
			}
		}
		else
		{
			LTE_Write_Ring(U1_RX_data,U1_Rx_Counter);
		}
		DMA1_Channel5->CNDTR = 512;                                    
		DMA_Cmd(DMA1_Channel5,ENABLE);                       
	}
}
void U1_SendString(char *buf1)		  
{
	unsigned int i;
	unsigned int len;
	len=strlen(buf1);

	for(i=0;i<len;i++)
	{
		while(USART_GetFlagStatus(USART1,USART_FLAG_TC)== RESET);
		USART_SendData(USART1,buf1[i]);
	}
}
void USART1_Send(char *buf,unsigned int len)
{
	unsigned int i;
	for(i=0;i<len;i++)
	{
		while(USART_GetFlagStatus(USART1,USART_FLAG_TC)== RESET);
		USART_SendData(USART1,buf[i]);
	}
}
void LED1_OPEN(void)
{
	GPIO_WriteBit(GPIOE, GPIO_Pin_13, Bit_SET);
}
void LED1_CLOSE(void)
{
	GPIO_WriteBit(GPIOE, GPIO_Pin_13, Bit_RESET);
}
void LTE_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(LTE_AT_state)
	{
		case state_idle:
			
			if(LTE_Delay_Timer>time1)
			{
				LTE_Delay_Timer=0;
				LTE_Timeslimite++;
				len=strlen(buf1);
				USART1_Send(buf1,len);
				LTE_AT_state=state_run;
			}
			break;
		case state_run:
			LED1_OPEN();
			LTE_Delay_Timer=0;
			if(LTE_Read_Ring(LTE_DATA_buf)>0)
			{
				if((strstr(LTE_DATA_buf, buf2))&&(strlen(buf2)))  //×Ö·û´®±È½Ï²»ÄÜÓÐ00£¬¹Ø±Õ»ØÏÔ±ÜÃâÊý¾ÝÖеÄ00Ó°Ïì±È½Ï
				{
					LTE_AT_state=state_right; 
				}
				else if((strstr(LTE_DATA_buf, buf3))&&(strlen(buf3)))
				{
					LTE_AT_state=state_right; 
				} 
				else if((strstr(LTE_DATA_buf, buf4))&&(strlen(buf4)))
				{
					LTE_AT_state=state_wrong; 
				}
				else if((strstr(LTE_DATA_buf, buf5))&&(strlen(buf5)))
				{
					LTE_AT_state=state_wrong; 
				} 
				else if((strstr(LTE_DATA_buf, buf6))&&(strlen(buf6)))
				{
					LTE_AT_state=state_wrong; 
				}
			}
			if(LTE_Timeslimite>time2)
			{
				LTE_AT_state=state_wrong;
			}
			if(LTE_overtime>time3)
			{
				LTE_AT_state=state_wrong;
			}
			break;
		case state_right:
			LTE_state=r;
			LTE_AT_state=state_idle;
			LTE_Delay_Timer=0;
			LTE_overtime=0;
			LTE_Timeslimite=0;
			LED1_CLOSE();
			memset(LTE_DATA_buf,0,512);
			break;
		case state_wrong:
			LTE_state=w;
			LTE_AT_state=state_idle;
			LTE_Delay_Timer=0;
			LTE_overtime=0;
			LTE_Timeslimite=0;
			LED1_CLOSE();
			memset(LTE_DATA_buf,0,512);
			break;
		default:  
			break;
	}
}
void LTE_DATATR(int time1,int time2,int time3,char * buf1,int len,char * buf2,char * buf3,char * buf4,char * buf5,char * buf6,char r ,char w)
{
	switch(LTE_AT_state)
	{
		case state_idle:
			
			if(LTE_Delay_Timer>time1)
			{
				LTE_Delay_Timer=0;
				LTE_Timeslimite++;
				USART1_Send(buf1,len);
				LTE_AT_state=state_run;
			}
			break;
		case state_run:
			LED1_OPEN();
			LTE_Delay_Timer=0;
			if(LTE_Read_Ring(LTE_DATA_buf)>0)
			{
				if((strstr(LTE_DATA_buf, buf2))&&(strlen(buf2)))   //×Ö·û´®±È½Ï²»ÄÜÓÐ00£¬¹Ø±Õ»ØÏÔ±ÜÃâÊý¾ÝÖеÄ00Ó°Ïì±È½Ï
				{
					LTE_AT_state=state_right; 
				}
				else if((strstr(LTE_DATA_buf, buf3))&&(strlen(buf3)))
				{
					LTE_AT_state=state_right; 
				} 
				else if((strstr(LTE_DATA_buf, buf4))&&(strlen(buf4)))
				{
					LTE_AT_state=state_wrong; 
				}
				else if((strstr(LTE_DATA_buf, buf5))&&(strlen(buf5)))
				{
					LTE_AT_state=state_wrong; 
				} 
				else if((strstr(LTE_DATA_buf, buf6))&&(strlen(buf6)))
				{
					LTE_AT_state=state_wrong; 
				}
			}
			if(LTE_Timeslimite>time2)
			{
				LTE_AT_state=state_wrong;
			}
			if(LTE_overtime>time3)
			{
				LTE_AT_state=state_wrong;
			}
			break;
		case state_right:
			LTE_state=r;
			LTE_AT_state=state_idle;
			LTE_Delay_Timer=0;
			LTE_overtime=0;
			LTE_Timeslimite=0;
			LED1_CLOSE();
			memset(LTE_DATA_buf,0,512);
			break;
		case state_wrong:
			LTE_state=w;
			LTE_AT_state=state_idle;
			LTE_Delay_Timer=0;
			LTE_overtime=0;
			LTE_Timeslimite=0;
			LED1_CLOSE();
			memset(LTE_DATA_buf,0,512);
			break;
		default:  
			break;
	}
}
void LTE_POWER(void)
{
	int i,j;
	GPIO_WriteBit(GPIOE, GPIO_Pin_1, Bit_SET);
	for(i=0;i<1000;i++)for(j=0;j<1000;j++);
	GPIO_WriteBit(GPIOE, GPIO_Pin_1, Bit_RESET);
	for(i=0;i<1000;i++)for(j=0;j<1000;j++);
}
void LTE_KEY(void)
{
	int i,j;
	GPIO_WriteBit(GPIOE, GPIO_Pin_3, Bit_RESET);
	for(i=0;i<1000;i++)for(j=0;j<1000;j++);
	GPIO_WriteBit(GPIOE, GPIO_Pin_3, Bit_SET);
	for(i=0;i<1000;i++)for(j=0;j<1000;j++);
}
void LTE_RST(void)
{
	int i,j;
	GPIO_WriteBit(GPIOB, GPIO_Pin_1, Bit_RESET);
	for(i=0;i<1000;i++)for(j=0;j<1000;j++);
	GPIO_WriteBit(GPIOB, GPIO_Pin_1, Bit_SET);
	for(i=0;i<1000;i++)for(j=0;j<1000;j++);
}

/*********************************************END OF FILE**********************/

.h文件

#ifndef __BSP_UART1_H
#define	__BSP_UART1_H

void UART1_Config(void);
void LTE_Ring_Init(void);
unsigned char LTE_Write_Ring(char *data,unsigned int len);
unsigned char LTE_Read_Ring(char *Data);
void LTE1_T_Ring_Init(void);
unsigned char LTE1_T_Write_Ring(char *data,unsigned int len);
unsigned char LTE1_T_Read_Ring(char *Data);
void LTE2_T_Ring_Init(void);
unsigned char LTE2_T_Write_Ring(char *data,unsigned int len);
unsigned char LTE2_T_Read_Ring(char *Data);
void LTE3_T_Ring_Init(void);
unsigned char LTE3_T_Write_Ring(char *data,unsigned int len);
unsigned char LTE3_T_Read_Ring(char *Data);
void U1_SendString(char *buf1);
void USART1_Send(char *buf,unsigned int len);
void LED1_OPEN(void);
void LED1_CLOSE(void);
void LTE_ATTR(int time1,int time2,int time3,char * buf1,char * buf2,char * buf3,char * buf4,char * buf5,char * buf6,char r ,char w);
void LTE_DATATR(int time1,int time2,int time3,char * buf1,int len,char * buf2,char * buf3,char * buf4,char * buf5,char * buf6,char r ,char w);
void LTE_POWER(void);
void LTE_KEY(void);
void LTE_RST(void);
#endif /* __BSP_UART1_H */

首先,需要了解SIMCOM A7670C是一款基于3G网络的通信模块,需要使用串口进行通信。因此,我们需要编写一个C语言的串口驱动程序来控制SIMCOM A7670C模块。 以下是一个简单的串口驱动程序示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <termios.h> #define BAUDRATE B115200 #define MODEMDEVICE "/dev/ttyUSB0" #define _POSIX_SOURCE 1 int open_serial_port() { int fd; struct termios newtio; fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY); if (fd < 0) { perror("open_serial_port: Unable to open serial port"); exit(1); } memset(&newtio, 0, sizeof(newtio)); newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD; newtio.c_iflag = IGNPAR; newtio.c_oflag = 0; newtio.c_lflag = 0; newtio.c_cc[VTIME] = 0; newtio.c_cc[VMIN] = 1; tcflush(fd, TCIFLUSH); tcsetattr(fd, TCSANOW, &newtio); return fd; } void write_serial_port(int fd, char *data) { int len = strlen(data); write(fd, data, len); } void read_serial_port(int fd, char *buffer, int buffer_size) { int bytes_read = read(fd, buffer, buffer_size); buffer[bytes_read] = '\0'; } int main() { int fd = open_serial_port(); char *command = "AT\r\n"; write_serial_port(fd, command); char response[256]; read_serial_port(fd, response, sizeof(response)); printf("Response: %s\n", response); close(fd); return 0; } ``` 这个驱动程序打开了一个串口设备文件,设置了波特率和其他串口参数,然后可以通过write_serial_port函数写入AT指令,通过read_serial_port函数读取SIMCOM A7670C模块的响应。 这只是一个简单的示例,你需要根据具体的需求来修改和完善代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值