#include "hw.h"

#ifdef __GNUC__
/* With GCC, small printf (option LD Linker->Libraries->Small printf
   set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar( int ch )
#else
#define PUTCHAR_PROTOTYPE int fputc( int ch, FILE *f )
#endif /* __GNUC__ */

uart_type_t uart_1;	//定义一个串口接收



/**
  * @brief  初始化串口
  * @param  None
  * @retval None
  */
void UartInit(void)
{
	//初始化串口接收
	uart_1.FLAG_CMD_PARSE = 0;
	uart_1.len_rx = 0;
	memset(uart_1.buff_rx, '\0', AT_UART_MX_RX_LEN);
	
	//使能时钟，初始化GPIO
	GPIO_InitTypeDef GPIO_InitStruct;
	USART_InitTypeDef USART_InitStruct;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1,ENABLE);
	
	GPIO_InitStruct.GPIO_Pin = USART_TX_PIN;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;   			// 复用推挽输出
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(USART_TX_PORT, &GPIO_InitStruct);  
	
	GPIO_InitStruct.GPIO_Pin = USART_RX_PIN;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;  // 浮空输入
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_Init(USART_RX_PORT, &GPIO_InitStruct);
	
	
	USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;  
	USART_InitStruct.USART_Parity = USART_Parity_No;   						
	USART_InitStruct.USART_BaudRate = 115200;   								
	USART_InitStruct.USART_StopBits = USART_StopBits_1;   				
	USART_InitStruct.USART_WordLength = USART_WordLength_8b;   		
	USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;   //无硬件数据流控制
	USART_Init(USART1,&USART_InitStruct); 

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
	NVIC_Init(&NVIC_InitStructure);
    
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);		// 使能串口接收中断
	
	USART_Cmd(USART1, ENABLE);
	
}


void UartDeInit( void )
{
    USART_Cmd(USART1,DISABLE);
	
		RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1,ENABLE);
		RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1,DISABLE);
}


/**
  * @brief 重定向 打印函数
  * @param ch 要发送的数据 
  */
PUTCHAR_PROTOTYPE
{
	USART_SendData(USART1, (uint8_t)ch);   
	while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);   
	
	return ch;
}

/**
  * @brief 串口中断处理
  */
void USART1_IRQHandler(void)
{
	uint8_t data;
	if(USART_GetFlagStatus(USART1,USART_IT_RXNE) != RESET)
	{
		data = USART_ReceiveData(USART1);
		if(uart_1.len_rx >= AT_UART_MX_RX_LEN - 1)
		{
			//数据超出接收缓冲区大小
			printf("[%s()-%d]cmd buf overflow\r\n", __func__, __LINE__);
			memset(uart_1.buff_rx,0,AT_UART_MX_RX_LEN);
			uart_1.len_rx = 0;
		}
		
		if(uart_1.FLAG_CMD_PARSE)	//还在处理上一条指令，标志还没被复位
		{	
			if(data == '\n')
			{
				//这条指令发送完毕
				printf("\r\ncmd busy\r\n");
			}
		}
		else
		{
			uart_1.buff_rx[uart_1.len_rx] = data;
			uart_1.len_rx++;
			if(data == '\n')
			{
				uart_1.FLAG_CMD_PARSE = 1;	//标志置1开始解析指令
			}
		}
	}
}




#ifdef USE_FULL_ASSERT
/**
   * @brief Reports the name of the source file and the source line number
   * where the assert_param error has occurred.
   * @param file: pointer to the source file name
   * @param line: assert_param error line source number
   * @retval None
   */
void assert_failed( uint8_t* file, uint32_t line )
{
  /* USER CODE BEGIN 6 */
  /* 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) */
  /* USER CODE END 6 */
}
#endif
