⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 STM32串口操作源代码 STM32串口操作源代码 STM32串口操作源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
void DMA_Configuration(void)
{
  DMA_InitTypeDef DMA_InitStructure;

  /*DMA Channel2 USART3 TX*/
  DMA_DeInit(DMA_Channel2);  
  DMA_InitStructure.DMA_PeripheralBaseAddr = USART3_DR_Base;
  DMA_InitStructure.DMA_MemoryBaseAddr = (u32)TxBuffer3;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;  /*read from  ram*/
  DMA_InitStructure.DMA_BufferSize = TxBufferSize3;	 /*if content is 0,stop TX*/
  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_VeryHigh;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA_Channel4, &DMA_InitStructure);

  /*DMA Channel3 USART3 RX*/
  DMA_DeInit(DMA_Channel3);  
  DMA_InitStructure.DMA_PeripheralBaseAddr = USART3_DR_Base;
  DMA_InitStructure.DMA_MemoryBaseAddr = (u32)RxBuffer3;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = RxBufferSize3;
  DMA_Init(DMA_Channel5, &DMA_InitStructure);
  
  /* DMA Channel6 (triggered by USART2 Rx event) Config */
  DMA_DeInit(DMA_Channel6);  
  DMA_InitStructure.DMA_PeripheralBaseAddr = USART2_DR_Base;
  DMA_InitStructure.DMA_MemoryBaseAddr = (u32)RxBuffer2;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;   /*read from device*/
  DMA_InitStructure.DMA_BufferSize = RxBufferSize2;
  DMA_Init(DMA_Channel6, &DMA_InitStructure);

  /* DMA Channel7 (triggered by USART2 Tx event) Config */
  DMA_DeInit(DMA_Channel7);  
  DMA_InitStructure.DMA_PeripheralBaseAddr = USART2_DR_Base;
  DMA_InitStructure.DMA_MemoryBaseAddr = (u32)TxBuffer2;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;/*read  from ram*/
  DMA_InitStructure.DMA_BufferSize = TxBufferSize2;  
  DMA_Init(DMA_Channel7, &DMA_InitStructure);
}

/*transmit one  byte to UART1*/
void TransmitByte( unsigned char senddata )
{
	unsigned char temphead;
    temphead = ( UART1_TxHead + 1 ) ;
	UART1_TX_BUFFER[temphead]=senddata;
	UART1_TxHead = temphead; /* store new index */
	/*if(Flag_UART1_SendOK==0)
	
	{
		Flag_UART1_SendOK=1;
	}	*/
}
void SendReturnFrame(unsigned char suc)
{
	//发送帧头 0x7e
	TransmitByte( 0x7e);
	//发送类型 REV_OK 0x01
	TransmitByte( REV_OK);
	//发送长度
	
	TransmitByte( 00);
	TransmitByte( 01);
	//发送数据 1 成功  2 失败
	TransmitByte( suc);
	//发送异或和校验
	AddDatatoFrame(0x01^0x00^0x01^suc); 
	
	//发送帧尾 0x7e
	TransmitByte( 0x7e);
}
void AddDatatoFrame(unsigned char framedata)
{
	if (framedata==0x7e)
	{
		TransmitByte(0x7d); 
		TransmitByte(0x5e); 
	}
	else if (framedata==0x7d)
	{
		TransmitByte(0x7d); 
		TransmitByte(0x5d); 
	}
	else
		TransmitByte(framedata); 
}

/*read one byte from RXBUFFER*/	 
unsigned char ReceiveByte(void)
{
    unsigned char temptail;
	while(UART1_RxHead==UART1_RxTail)
	;
	temptail=(UART1_RxTail+1)&0xFF;
	UART1_RxTail=temptail;
	return UART1_RX_BUFFER[temptail];
}
/*check if there is data in the RXBUFER*/
unsigned char IsDataInRxBuffer()
{
    return(UART1_RxHead!=UART1_RxTail) ;
}
/*some data from UART1 is to UART2,some is to UART3,the other is to CAN,
classify the data by IDadress and send to its corresponding BUFFER,such as TXBUFFER2/TXBUFFER3/CANBUFFER*/
void  DistriUART1Data(void)
{
    static  unsigned int datalen=0; //接收到的有效数据长度
	static  unsigned char  flag0x7d=0;//转义标志位
	static  unsigned char  flamehead=0; //帧头存在标志位
    unsigned char err=0;
	unsigned char i;
	
	unsigned char uctemp;
	while ( IsDataInRxBuffer()) //fifo中有数据
	{		
		uctemp=ReceiveByte();
		
		if (uctemp==0x7e) //帧截断标志
		{
			flamehead=1;
			if (flag0x7d) //0x7d 0x7e 帧错误
			{
				err=1;
			}
			else if (datalen) //帧结束	 7e+ID+type+datalenHi+datalenlow+data+checksum+7e
			{
				UART1_REVDATA[datalen]=uctemp;
				datalen++;
			//	unsigned int len=(UART1_REVDATA[3]<<8)|UART1_REVDATA[4];
			//	if ((datalen==len+7) && CheckSum(&UART1_REVDATA[2], datalen-2)) //校验异或和
			//	{
				
					//datalen=0;
					if (UART1_REVDATA[1]==Port_CAN_ID)
					{
					  //组装对应的CAN报文往CAN口发送
					}
					else  if(UART1_REVDATA[1]==Port_UART2_ID)	//利用DMA向对应串口发送数据
					{
					   for(i=0;i<datalen;i++)
					       TxBuffer2[i]=UART1_REVDATA[i];
						   datalen=0;
					}
					else 
					{
					   for(i=0;i<datalen;i++)
					       TxBuffer3[i]=UART1_REVDATA[i];
						   datalen=0;
					}
			//	}
			//	else //帧错误
			//		err=1;				
			}
			else
			{
			    	UART1_REVDATA[datalen]=uctemp;
					datalen++;
			}
						
		}
		else
		{
			if (flamehead)
			{
				if (flag0x7d) //存在转义标志
				{
					flag0x7d=0;
					if (uctemp==0x5e)
					{
						UART1_REVDATA[datalen]=uctemp;
						datalen++;
					}
					else if (uctemp==0x5d)
					{
						UART1_REVDATA[datalen]=uctemp;
						datalen++;
					}
					else //帧错误
					{
						flamehead=0; //需要重新找帧头
						err=1;
					}
				}
				else
				{
					if (uctemp==0x7d) //转义字符
					{
						flag0x7d=1;
						UART1_REVDATA[datalen]=uctemp;
						datalen++;
					}
					else
					{
						UART1_REVDATA[datalen]=uctemp;
						datalen++;
					}
				}
				if (datalen>380)
				{
					//err 溢出
					flamehead=0; //需要重新找帧头
					err=1;
				}		
			}
			
		}
		
		if (err)
		{
			datalen=0; //清除标志位
			flag0x7d=0;
			
			SendReturnFrame(2);  //发送接收错误回应帧
			err=0;
		}
		
	}			
}


  void TxUART2ComingData(void)
  {
       unsigned int uilen;
	   unsigned int i;
       if(DMA_GetFlagStatus(DMA_FLAG_TC6) != RESET)	   //channel6 is UART2 Rx data

		{	
		    DMA_ClearFlag(DMA_FLAG_TC6);
			DMA_Cmd(DMA_Channel6, DISABLE);   //prohibit  channel 6 for a little time
		    uilen=countof(RxBuffer2)
			for(i=0;i<uilen;i++)
			TransmitByte(RxBuffer2[i]);
			DMA_Cmd(DMA_Channel6, ENABLE);
		}
	      
  }

  void TxUART3ComingData(void)
  {
       unsigned int uilen;
	   unsigned int i;
       if(DMA_GetFlagStatus(DMA_FLAG_TC3) != RESET)	   //channel6 is UART3 Rx data

		{	
		    DMA_ClearFlag(DMA_FLAG_TC3);
			DMA_Cmd(DMA_Channel3, DISABLE);   //prohibit  channel 3 for a little time
		    uilen=countof(RxBuffer3)
			for(i=0;i<uilen;i++)
			TransmitByte(RxBuffer3[i]);
			DMA_Cmd(DMA_Channel3, ENABLE);
		}
      
  }
  void TxCANComingdata(void)

/*****************************************************************************
*
*   Function name : CheckSum
*
*   Returns :       1 数据校验正确 ; 0 数据校验失败
*
*   Parameters :    pdatas 指向待校验数据的指针
*
*   Purpose :       通过异或和检查数据是否正确
*
*****************************************************************************/
unsigned char CheckSum(const unsigned char *pdatas,int len)
{
	unsigned int i;
	unsigned char uctemp=0;
	for (i=0 ; i<len;i++)
	{
		uctemp^=*pdatas;
		pdatas++;
	}
	if (uctemp==*pdatas)
	{
		return 1;
	}
	else
		return 0;
}



#ifdef  DEBUG
/*******************************************************************************
* Function Name  : assert_failed
* Description    : Reports the name of the source file and the source line number
*                  where the assert error has occurred.
* Input          : - file: pointer to the source file name
*                  - line: assert error line source number
* Output         : None
* Return         : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -