#include "BspUsart1.h"
#include "bsp.h"

void (*receChar)(u8 ch) = NULL;

#if 1
#pragma import(__use_no_semihosting)             
//标准库需要的支持函数                 
struct __FILE 
{ 
	int handle; 
}; 

FILE __stdout;       
//定义_sys_exit()以避免使用半主机模式    
_sys_exit(int x) 
{ 
	x = x; 
} 
//重定义fputc函数 
int fputc(int ch, FILE *f)
{      
	while((USART1->SR&0X40)==0);//循环发送,直到发送完毕   
    USART1->DR = (u8) ch;      
	return ch;
}
#endif 

////重定义fputc函数(串口2可使用printf)
//int fputc(int ch, FILE *f)
//{      
//	while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET)
//  USART_SendData(USART1,(uint8_t)ch);   
//	return ch;
//}
//#endif 

////使用microLib的方法
//int fputc(int ch, FILE *f)
//{
//	USART_SendData(USART1,(uint8_t)ch);
//	while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET)
//	return ch;
//}
//int GetKey(void)
//{
//    while(!(USART1->SR & USART_FLAG_RXNE))
//    return ((int)(USART1->DR & 0x1FF));
//}

//初始化串口1 //bound:波特率
void USART1_Init(u32 bound)
{
	GPIO_InitTypeDef GpioInitdef;
    USART_InitTypeDef UsartInitdef;
    NVIC_InitTypeDef NvicInitdef;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);  

    USART_DeInit(USART1);

    USART_StructInit( &UsartInitdef );
    UsartInitdef.USART_BaudRate = bound;
    UsartInitdef.USART_StopBits = USART_StopBits_1;
    UsartInitdef.USART_WordLength = USART_WordLength_8b;
    UsartInitdef.USART_Parity = USART_Parity_No;

    UsartInitdef.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(USART1, &UsartInitdef);

    GpioInitdef.GPIO_Pin = GPIO_Pin_9; //
    GpioInitdef.GPIO_Speed = GPIO_Speed_10MHz;
    GpioInitdef.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GpioInitdef);

    GpioInitdef.GPIO_Pin = GPIO_Pin_10; //
    GpioInitdef.GPIO_Speed = GPIO_Speed_10MHz;
    GpioInitdef.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GpioInitdef);

    //USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

    NvicInitdef.NVIC_IRQChannel = USART1_IRQn;
    NvicInitdef.NVIC_IRQChannelPreemptionPriority = 2;
    NvicInitdef.NVIC_IRQChannelSubPriority = 2;
    NvicInitdef.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init( &NvicInitdef );

    USART_Cmd(USART1, ENABLE);
}

void BspUsart1Close(void)
{
//    while (eTXIdle != TRUE);
    USART_Cmd(USART1, DISABLE);

    USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
    USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
    USART_ITConfig(USART1, USART_IT_TC, DISABLE);

    USART_ClearITPendingBit(USART1, USART_IT_TXE);
    USART_ClearITPendingBit(USART1, USART_IT_TC);
    USART_ClearITPendingBit(USART1, USART_IT_RXNE);	
}

void USART1_SendByte(unsigned char c)//串口1发送一个字符
{
	USART_SendData(USART1, c);
	while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );
}

//发送len个字节. //buf:发送区首地址//len:发送的字节数
void Usart1_Send(u8 *buf,u8 len)
{
	u8 i;
  	for(i=0;i<len;i++)//循环发送数据
	{		   
		while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);	  
		USART_SendData(USART1,buf[i]);
	}	 
	while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);		
}

//设置串口回调函数
void BspUsart1IRQCallBack(void *fun)
{
    receChar = (void (*)(u8))fun;
}

void USART1_IRQHandler(void)
{
    if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
    {
        u8 ch;
        
        USART_ClearITPendingBit(USART1, USART_IT_RXNE);	
        ch = USART_ReceiveData(USART1);
        if (receChar != NULL)
		{
            (*receChar)(ch);
		}
    }	
}
