/*--------------------------------------------------------------------------------------
*  @file     USARTSetup.c
*  @author   ZhangJing
*  @version  base on stm32f0x   
*  @date     2015.09.11
*  @brief    串口驱动
---------------------------------------------------------------------------------------*/

#include "stm32f10x_gpio.h"
#include "TypeDefine.h"
#include "USARTSetup.h"
#include "stm32f10x_dma.h"

uint8_t	usart1XmitBuffer[USART1_XMIT_LEN];	//USART1发送缓冲
uint8_t	usart1RecvBuffer[USART1_RECV_LEN];	//USART1接收缓冲
uint8_t	usart1RecvCnt = 0;	//USART1接收计数

uint8_t	usart2XmitBuffer[USART2_XMIT_LEN];	//USART2发送缓冲
uint8_t	usart2RecvBuffer[USART2_RECV_LEN];	//USART2接收缓冲
uint8_t	usart2RecvCnt = 0;	//USART2接收计数

uint8_t	usart3XmitBuffer[USART3_XMIT_LEN];	//USART3发送缓冲
uint8_t	usart3RecvBuffer[USART3_RECV_LEN];	//USART3接收缓冲
uint8_t	usart3RecvCnt = 0;	//USART3接收计数

/*******************************************************************************
*  Function:	USART2_DMAinit()				                                                           
*  Object:		串口1初始化函数
*  输入:			无
*  输出:			无	                                     
*  备注:			与PC通信     
********************************************************************************/
void USART2_DMAinit(void)
{	
	/*DMA_InitTypeDef  DMA_InitStructure;         
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);  
  // DMA channel6configuration 
  DMA_DeInit(DMA1_Channel6);  
  DMA_InitStructure.DMA_PeripheralBaseAddr       =(u32)(&USART2->DR);  //外设地址  
  DMA_InitStructure.DMA_MemoryBaseAddr           =(u32)usart2RecvBuffer;  
  DMA_InitStructure.DMA_DIR                      =DMA_DIR_PeripheralSRC;   //外设作为目的地址   //DMA_DIR_PeripheralSRC;   //外设作为DMA的源端  
  DMA_InitStructure.DMA_BufferSize               =50; //BufferSize;      //传输大小  
  DMA_InitStructure.DMA_PeripheralInc            =DMA_PeripheralInc_Disable; //外设递增模式禁止   DMA_PeripheralInc_Enable;            //外设地址增加  
  DMA_InitStructure.DMA_MemoryInc                =DMA_MemoryInc_Enable;   //内存地址自增  
  DMA_InitStructure.DMA_PeripheralDataSize       =DMA_PeripheralDataSize_Byte; //传输方式：字节   DMA_PeripheralDataSize_Word;    //字（32位）  
  DMA_InitStructure.DMA_MemoryDataSize           =DMA_PeripheralDataSize_Byte;  //内存存储方式：字节  DMA_MemoryDataSize_Word;  
  DMA_InitStructure.DMA_Mode                     =DMA_Mode_Normal;  //DMA_Mode_Normal 正常模式，只传送一次;  DMA_Mode_Circular:循环模式，不停的传送;  
  DMA_InitStructure.DMA_Priority                 =DMA_Priority_High;  
  DMA_InitStructure.DMA_M2M                      =DMA_M2M_Disable;             //DMA_M2M_Enable;      
  DMA_Init(DMA1_Channel6,&DMA_InitStructure);     
  // Enable DMA Channel4Transfer Complete interrupt
 // DMA_ITConfig(DMA1_Channel6,DMA_IT_TC, ENABLE);    
  //采用DMA方式发送  
 // USART_DMACmd(USART2,USART_DMAReq_Rx,ENABLE);         
 // DMA_ITConfig(DMA1_Channel6,DMA_IT_TC, ENABLE);  

  // DMA_Cmd(DMA1_Channel6,ENABLE);  //启动通道6*/
	
}
/********************************************************************************
*  Function:	USART1Init						                                                           
*  Object:		串口1初始化函数
*  输入:			无
*  输出:			无	                                     
*  备注:			与PC通信              
*********************************************************************************/
void USART1_Init(uint32_t BaudRate)
{  
	GPIO_InitTypeDef  GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;	
	
 	//USART1配置
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	

 	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
 	GPIO_Init(GPIOA, &GPIO_InitStructure);

 	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
 	GPIO_Init(GPIOA, &GPIO_InitStructure); 

 	RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1,ENABLE);//复位串口1
 	RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1,DISABLE);//停止复位
  
 	USART_InitStructure.USART_BaudRate = BaudRate;//一般设置为9600;
 	USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8位数据长度
 	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); //初始化串口1
   
 	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //使能串口1中断
 	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //先占优先级2级
 	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级2级
 	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
 	NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
  
 	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启中断 
 	USART_Cmd(USART1, ENABLE);                    //使能串口 
	
	
}

/********************************************************************************
*  Function:	USART2Init						                                                           
*  Object:		串口2初始化函数
*  输入:			无
*  输出:			无	                                     
*  备注:			与Zigbee通信              
*********************************************************************************/
void USART2_Init(uint32_t BaudRate)
{  
	/*GPIO_InitTypeDef  GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	//USART2配置
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	USART2_DMAinit();//调用USART2 DMA配置函数
	
	//USART2管脚配置
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;			
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;			
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   
	GPIO_Init(GPIOA, &GPIO_InitStructure);			// TXIO 		
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;			 	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_Init(GPIOA, &GPIO_InitStructure); 			// RXIO 
	
	RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,ENABLE);//复位串口2
	RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,DISABLE);//停止复位
	
	USART_InitStructure.USART_BaudRate = BaudRate;//设置串口波特率
	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(USART2, &USART_InitStructure); //配置入结构体	
	
	
	
	//中断设置			 	
	NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //使能串口2中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级2级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级2级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
	NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器	

	//USART_ITConfig(USART2, (USART_IT_RXNE | USART_IT_IDLE), ENABLE);//开启中断 
	//中断配置  
    USART_ITConfig(USART2,USART_IT_TC,DISABLE);  
    USART_ITConfig(USART2,USART_IT_RXNE,DISABLE);  
  USART_ITConfig(USART2,USART_IT_IDLE,ENABLE); 
	  //采用DMA方式发送  
  USART_DMACmd(USART2,USART_DMAReq_Rx,ENABLE);   
  //DMA_Cmd(DMA1_Channel6,ENABLE);  //启动通道6	
	DMA_Cmd(DMA1_Channel6,DISABLE);     //失能DMA功能
	USART_Cmd(USART2, ENABLE);                    //使能串口 */
	GPIO_InitTypeDef  GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	//USART2配置
	/* Enable USART clock */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	//USART2管脚配置
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;			
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;			
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   
	GPIO_Init(GPIOA, &GPIO_InitStructure);			/* TXIO */			
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;			 	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_Init(GPIOA, &GPIO_InitStructure); 			/* RXIO */
	
	RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,ENABLE);//复位串口2
	RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,DISABLE);//停止复位
	
	USART_InitStructure.USART_BaudRate = BaudRate;//设置串口波特率
	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(USART2, &USART_InitStructure); //配置入结构体	
	
	//中断设置			 	
	NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //使能串口2中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级2级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级2级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
	NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器	

	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启中断 
	USART_Cmd(USART2, ENABLE);                    //使能串口 
}

/********************************************************************************
*  Function:	USART3Init						                                                           
*  Object:		串口3初始化函数
*  输入:			无
*  输出:			无	                                     
*  备注:			与从机通信              
*********************************************************************************/
void USART3_Init(uint32_t BaudRate)
{  
	GPIO_InitTypeDef  GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	//USART3配置 	
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
	
	
 	//USART3管脚配置
 	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;			
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;			
 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   
 	GPIO_Init(GPIOB, &GPIO_InitStructure);			/* TXIO */			
 	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;			 	
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 	GPIO_Init(GPIOB, &GPIO_InitStructure); 			/* RXIO */
 	
 	RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3,ENABLE);//复位串口3
 	RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3,DISABLE);//停止复位
 	
 	USART_InitStructure.USART_BaudRate = BaudRate;//设置串口波特率
 	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(USART3, &USART_InitStructure); //配置入结构体
// 	
// 	//中断设置			 	
 	NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; //使能串口3中断
 	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //先占优先级2级
 	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级2级
 	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
 	NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器

 	USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启中断 
 	USART_Cmd(USART3, ENABLE); 	
}	

/********************************************************************************
*  Function:	USART1Send						                                                           
*  Object:		USART1发送函数
*  输入:			uint8_t *xmitDataBuf 待发送数据缓存区
*							uint32_t xmitLen		 发送数据长度
*  输出:			无	                                     
*  备注:			1、根据xmitLen设定长度发送 xmitDataBuf中的数据           
*********************************************************************************/
void USART1Send( uint8_t *xmitDataBuf, uint32_t xmitLen )
{
	uint8_t i;
	for( i = 0; i < xmitLen ; i++ )
	{
		while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);	//发送完成  
		USART_SendData(USART1,*(xmitDataBuf++));
	}
	while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);		
}

/********************************************************************************
*  Function:	USART2Send						                                                           
*  Object:		USART2发送函数
*  输入:			uint8_t *xmitDataBuf 待发送数据缓存区
*							uint32_t xmitLen		 发送数据长度
*  输出:			无	                                     
*  备注:			1、根据xmitLen设定长度发送 xmitDataBuf中的数据                 
*********************************************************************************/
void USART2Send( uint8_t *xmitDataBuf, uint32_t xmitLen )
{
	uint8_t i;
	for( i = 0; i < xmitLen ; i++ )
	{
		while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);	//发送完成  
		USART_SendData(USART2,*(xmitDataBuf++));
	}
	while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);	
}

/********************************************************************************
*  Function:	USART3Send						                                                           
*  Object:		USART3发送函数
*  输入:			uint8_t *xmitDataBuf 待发送数据缓存区
*							uint32_t xmitLen		 发送数据长度
*  输出:			无	                                     
*  备注:			1、根据xmitLen设定长度发送 xmitDataBuf中的数据                 
*********************************************************************************/
void USART3Send( uint8_t *xmitDataBuf, uint32_t xmitLen )
{
	uint8_t i;
	for( i = 0; i < xmitLen ; i++ )
	{
		while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);	//发送完成  
		USART_SendData(USART3,*(xmitDataBuf++));
	}
	while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);	
}
