stm32 circular queue (two receiving and one sending)

Quote https://blog.csdn.net/it_is_time_to/article/details/78486257

 

statement

#include "stm32f10x.h"
#include <stdio.h>
#Define buffersize 2048 cache space
u8 dat[250]={0};                           array
uint32_t usart1_in  = 0;                   Team leader
uint32_t usart1_out = 0;                   Team tail
u8 RevBuff1[BUFFSIZE] = {0};
u8 flag = 0,flag2 = 0;





Interrupt function 1

u8 i,temp,h,f;
void USART1_IRQHandler(void)                	
	{
		
	 if(USART_GetFlagStatus(USART1,USART_IT_RXNE)!=RESET)        //
		{
          dat[i] = USART_ReceiveData(USART1);
		  i++;
        } 
		
	 else if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)  //Idle interrupt 
		{
		  temp=USART1->SR;
		  temp=USART1->DR;
	      temp=temp;
		  flag2 = 1;       //One frame receiving completed
		}
} 

Interrupt function 2

void USART2_IRQHandler(void)                   
{
    u8 res1 = 0;
    if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
    {

        res1 = USART_ReceiveData(USART2);                //Copy received data
        USART_ClearITPendingBit(USART2, USART_IT_RXNE);
        RevBuff1[usart1_in++] = res1;                    //Data queued
        if (usart1_in >= BUFFSIZE)                       //The queue head is larger than the cache size clear
		{  usart1_in = 0;  }
        flag = 1;	                                     //Save complete flag
    }
} 

Main function

In my project, the data in the array is the most important, followed by the data in the queue. When sending, it must be sent in the main function, so that one frame will go out, but the data in the queue on the other side will be spliced.

int main(void)
{
  //u8 dat;
  u16 t;
  RCC_Configuration();
  NVIC_Configuration();
  USART1_Configuration();
  while(1)
  {
		
		if(flag2 == 1)	                  //Idle interrupt generation
			 {				 
					 flag = 0;            //Queue stop sending
						for(h=0;h<i;h++)
					 { 				
						USART_SendData(USART3, dat[h]);
						while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET); 
					 }
					 i=0;
					 flag = 1;             //Data release is completed in idle interrupt, open queue data release
					 flag2 = 0;            //Close
				}
		
		
		
		
		if (flag == 1)               //Release of queue array
    {   
			f=0;                    
		 if (usart1_out != usart1_in)     //In the case of unequal head and tail, there is data stored in RecBuff1
		 {       
				 if (usart1_out < usart1_in) //If the end of the team is smaller than the head of the team, send the data between the end of the team and the head of the team directly
				  {    
							for (t = usart1_out; t < usart1_in; t++)
						    {
									while(USART_GetFlagStatus(USART3,USART_FLAG_TXE)!=SET);
									USART_SendData(USART3, RevBuff1[t]);        
							  }  
							usart1_out = usart1_in;     
							flag = 0;
				  } 
				  //If the end of the team is larger than the head of the team, send the data from the end of the team to RecBuff1[BUFFSIZE] first
                 //Then send the element between RecBuff1[0] and the team header
                 else
					{
						
						 for (t = usart1_out; t < BUFFSIZE; t++) 
						   {
									while(USART_GetFlagStatus(USART3,USART_FLAG_TXE)!=SET);
									USART_SendData(USART3, RevBuff1[t]);        
							 }  
							for (t = 0; t < usart1_in; t++) 
							 {
								 while(USART_GetFlagStatus(USART3,USART_FLAG_TXE)!=SET);
								 USART_SendData(USART3, RevBuff1[t]);  
							 }
							usart1_out = 0;          
							flag = 0;
				  }
		   } 
       //f=1;	 		 
      }
		 
  }

}

The data in the team has an interval of 40ms, a size of 200-264 bytes, and an interval of 1s in the array. The GPS signal is a bit bug gy at the beginning and the end, and a bit of data will be lost.

 

 

Complete works at https://download.csdn.net/download/zhao199643/10581716

Posted on Fri, 10 Jan 2020 12:19:27 -0500 by Oxymen