#include "USART.H"
struct WIFI wifi;

void USART1_Config(u32 bound)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);	
  
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure; 


    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,ENABLE);
 

    //USART1_TX   GPIOA.9
    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);            

    //USART1_RX      GPIOA.10初始化
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;               
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;    
    GPIO_Init(GPIOA,&GPIO_InitStructure);                    

    //Usart1 NVIC 配置
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3 ;  
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;       
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;       
    NVIC_Init(&NVIC_InitStructure);         

    //USART 初始化设置
    USART_InitStructure.USART_BaudRate = bound;                   
    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);        

    USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);   
	USART_ITConfig(USART1,USART_IT_IDLE,ENABLE);   
		
    USART_Cmd(USART1,ENABLE);                     
}
//Debug 
void USART2_Config( u32 bound )
{
	/* GPIO端口设置 */
	GPIO_InitTypeDef	GPIO_InitStructure;
	USART_InitTypeDef	USART_InitStructure;
	NVIC_InitTypeDef	NVIC_InitStructure;

	RCC_APB2PeriphClockCmd(  RCC_APB2Periph_GPIOA, ENABLE ); 
	RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2, ENABLE );                         /* 使能USART2，GPIOA时钟 */

	/* PA2 TXD2 */
	GPIO_InitStructure.GPIO_Pin	= GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Speed	= GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode	= GPIO_Mode_AF_PP;
	GPIO_Init( GPIOA, &GPIO_InitStructure );

	/* PA3 RXD2 */
	GPIO_InitStructure.GPIO_Pin	= GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Mode	= GPIO_Mode_IN_FLOATING;
	GPIO_Init( GPIOA, &GPIO_InitStructure );


	/* USART 初始化设置 */
	USART_InitStructure.USART_BaudRate		= bound;                                /* 串口波特率 */
	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( USART2, &USART_InitStructure );                                             /* 初始化串口1 */
	
	USART_Cmd( USART2, ENABLE );                                                            /* 使能串口 2 */

}



void USART1_IRQHandler(void)  
{  
	// 接收中断的时候收集数据包

    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
    {
        USART_ClearITPendingBit(USART1, USART_IT_RXNE); //只USART_ReceiveData也可以

       wifi.BUFF[ wifi.buff_length++]= USART_ReceiveData(USART1);
	}
    // 空闲中断的时候说明串口没数据了，收集完成
    else if(USART_GetFlagStatus(USART1, USART_FLAG_IDLE) != RESET)
    {
			USART1->DR;                 //置位
			wifi.buff_flag = 1;
			
    }
} 






//重定向C库函数printf到串口，重定向后可使用printf函数
int fputc(int ch,FILE *f)
{
	/* 发送一个字节数据到串口 */
	USART_SendData(USART2,(uint8_t) ch);
	while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
	return (ch);
}

/* USART1字符串发送函数*/
void USART1_SEND(u8* data,u16 length)
{
	u16 i;
  for( i=0;i<length;i++)
	{
	USART_SendData(USART1,data[i]);
	while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
	}
}

/* USART2字符串发送函数*/
void USART2_SEND(u8 * data,u16 length)
{
	u16 i;
  for( i=0;i<length;i++)
	{
	USART_SendData(USART2,data[i]);
	while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
	}
}