#include "bsp_usart.h"
#include "system.h"
#include "sht3x.h"


/**
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval None
  */
void USART_RCC_Configuration(void)
{
	/* Enable GPIO clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_GPIOC|RCC_AHBPeriph_GPIOD|RCC_AHBPeriph_GPIOF, ENABLE);
  
  /* Enable 8xUSARTs Clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB1Periph_USART3|RCC_APB1Periph_USART4|RCC_APB1Periph_USART5, ENABLE);  
  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6|RCC_APB2Periph_USART7|RCC_APB2Periph_USART8|RCC_APB2Periph_USART1|RCC_APB2Periph_SYSCFG,ENABLE);
}


/**
  * @brief  Configures the different GPIO ports.
  * @param  None
  * @retval None
  */
void GPIO_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	//DEBUG_USART1 引脚初始化
	//连接到外设
	GPIO_PinAFConfig(DEBUG_USART_RX_GPIO_PORT,DEBUG_USART_RX_SOURCE,DEBUG_USART_RX_AF);
	GPIO_PinAFConfig(DEBUG_USART_TX_GPIO_PORT,DEBUG_USART_TX_SOURCE,DEBUG_USART_TX_AF);
	
	//将引脚配置为AF pushpull
	GPIO_InitStructure.GPIO_Pin = DEBUG_USART_RX_PIN | DEBUG_USART_TX_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd =   GPIO_PuPd_NOPULL;
	GPIO_Init(DEBUG_USART_RX_GPIO_PORT,&GPIO_InitStructure);
	
	
	//NH3_USART2 引脚初始化
	//连接到外设
	GPIO_PinAFConfig(NH3_USART_RX_GPIO_PORT,NH3_USART_RX_SOURCE,NH3_USART_RX_AF);
	GPIO_PinAFConfig(NH3_USART_TX_GPIO_PORT,NH3_USART_TX_SOURCE,NH3_USART_TX_AF);
	
	//将引脚配置为AF pushpull
	GPIO_InitStructure.GPIO_Pin = NH3_USART_RX_PIN | NH3_USART_TX_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(NH3_USART_RX_GPIO_PORT,&GPIO_InitStructure);
	
	
	//CH2O_USART3 引脚初始化
	//连接到外设
	GPIO_PinAFConfig(CH2O_USART_RX_GPIO_PORT,CH2O_USART_RX_SOURCE,CH2O_USART_RX_AF);
	GPIO_PinAFConfig(CH2O_USART_TX_GPIO_PORT,CH2O_USART_TX_SOURCE,CH2O_USART_TX_AF);
	
	//将引脚配置为AF pushpull
	GPIO_InitStructure.GPIO_Pin = CH2O_USART_RX_PIN | CH2O_USART_TX_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(CH2O_USART_RX_GPIO_PORT,&GPIO_InitStructure);
	
	
	
	//PM25_USART4 引脚初始化
	//连接到外设
	GPIO_PinAFConfig(PM25_USART_RX_GPIO_PORT,PM25_USART_RX_SOURCE,PM25_USART_RX_AF);
	GPIO_PinAFConfig(PM25_USART_TX_GPIO_PORT,PM25_USART_TX_SOURCE,PM25_USART_TX_AF);
	
	//将引脚配置为AF pushpull
	GPIO_InitStructure.GPIO_Pin = PM25_USART_RX_PIN | PM25_USART_TX_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(PM25_USART_RX_GPIO_PORT,&GPIO_InitStructure);
	
	
	
	//H2S_USART5 引脚初始化
	//连接到外设
	GPIO_PinAFConfig(H2S_USART_RX_GPIO_PORT,H2S_USART_RX_SOURCE,H2S_USART_RX_AF);
	GPIO_PinAFConfig(H2S_USART_TX_GPIO_PORT,H2S_USART_TX_SOURCE,H2S_USART_TX_AF);
	
	//将引脚配置为AF pushpull
	GPIO_InitStructure.GPIO_Pin = H2S_USART_RX_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(H2S_USART_RX_GPIO_PORT,&GPIO_InitStructure);
	
	
	//将引脚配置为AF 
	GPIO_InitStructure.GPIO_Pin = H2S_USART_TX_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(H2S_USART_TX_GPIO_PORT,&GPIO_InitStructure);
	
	
	
	//CO2_USART6 引脚初始化
	//连接到外设
	GPIO_PinAFConfig(CO2_USART_RX_GPIO_PORT,CO2_USART_RX_SOURCE,CO2_USART_RX_AF);
	GPIO_PinAFConfig(CO2_USART_TX_GPIO_PORT,CO2_USART_TX_SOURCE,CO2_USART_TX_AF);
	
	//将引脚配置为AF pushpull
	GPIO_InitStructure.GPIO_Pin = CO2_USART_RX_PIN | CO2_USART_TX_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(CO2_USART_RX_GPIO_PORT,&GPIO_InitStructure);
	
	
	//SP3485_USART7 引脚初始化
	//连接到外设
	GPIO_PinAFConfig(WIFI_USART_RX_GPIO_PORT,WIFI_USART_RX_SOURCE,WIFI_USART_RX_AF);
	GPIO_PinAFConfig(WIFI_USART_TX_GPIO_PORT,WIFI_USART_TX_SOURCE,WIFI_USART_TX_AF);
	
	//将引脚配置为AF pushpull
	GPIO_InitStructure.GPIO_Pin = WIFI_USART_RX_PIN | WIFI_USART_TX_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(WIFI_USART_RX_GPIO_PORT,&GPIO_InitStructure);
	
	
	//LORA_USART8 引脚初始化
	//连接到外设
	GPIO_PinAFConfig(LORA_USART_RX_GPIO_PORT,LORA_USART_RX_SOURCE,LORA_USART_RX_AF);
	GPIO_PinAFConfig(LORA_USART_TX_GPIO_PORT,LORA_USART_TX_SOURCE,LORA_USART_TX_AF);
	
	//将引脚配置为AF pushpull
	GPIO_InitStructure.GPIO_Pin = LORA_USART_RX_PIN | LORA_USART_TX_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(LORA_USART_RX_GPIO_PORT,&GPIO_InitStructure);
	
	
	//TVOC引脚配置
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
	
	//LORA_RST引脚配置
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	
	//PM25_RST引脚配置
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_Init(GPIOC,&GPIO_InitStructure);
	
	GPIO_SetBits(GPIOC,GPIO_Pin_2);
	GPIO_SetBits(GPIOC,GPIO_Pin_3);
	
	//WIFI_RST引脚配置
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
	GPIO_SetBits(GPIOB,GPIO_Pin_15);
}


/**
  * @brief  Configures USART1-8
  * @param  None
  * @retval None
  */
void USART_Configuration(void)
{
	USART_InitTypeDef USART_InitStructure;
	
	/* DEBUG_USART configuration */
	USART_InitStructure.USART_BaudRate = DEBUG_USART_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(DEBUG_USART,&USART_InitStructure);
	/* Disable DEBUG_USART Receive interrupts */
	USART_ITConfig(DEBUG_USART,USART_IT_RXNE,DISABLE);
	/* Enable the DEBUG_USART */
	USART_Cmd(DEBUG_USART,ENABLE);
	
	
	/* NH3_USART configuration */
	USART_InitStructure.USART_BaudRate = NH3_USART_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(NH3_USART,&USART_InitStructure);
	/* Enable NH3_USART Receive interrupts */
	USART_ITConfig(NH3_USART,USART_IT_RXNE,DISABLE);
	/* Enable the NH3_USART */
	USART_Cmd(NH3_USART,ENABLE);
	
	
	/* CH2O_USART configuration */
	USART_InitStructure.USART_BaudRate = CH2O_USART_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(CH2O_USART,&USART_InitStructure);
	/* Enable CH2O_USART Receive interrupts */
	USART_ITConfig(CH2O_USART,USART_IT_RXNE,DISABLE);

	/* Enable the CH2O_USART */
	USART_Cmd(CH2O_USART,ENABLE);
	
	/* PM25_USART configuration */
	USART_InitStructure.USART_BaudRate = PM25_USART_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(PM25_USART,&USART_InitStructure);
	/* Enable PM25_USART Receive interrupts */
	USART_ITConfig(PM25_USART,USART_IT_RXNE,DISABLE);
	/* Enable the PM25_USART */
	USART_Cmd(PM25_USART,ENABLE);
	
	
	
	/* H2S_USART configuration */
	USART_InitStructure.USART_BaudRate = H2S_USART_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(H2S_USART,&USART_InitStructure);
	/* Enable H2S_USART Receive interrupts */
	USART_ITConfig(H2S_USART,USART_IT_RXNE,DISABLE);
	/* Enable the H2S_USART */
	USART_Cmd(H2S_USART,ENABLE);
	
	
	
	/* CO2_USART configuration */
	USART_InitStructure.USART_BaudRate = CO2_USART_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(CO2_USART,&USART_InitStructure);
	/* Enable CO2_USART Receive interrupts */
	USART_ITConfig(CO2_USART,USART_IT_RXNE,ENABLE);
	/* Enable the CO2_USART */
	USART_Cmd(CO2_USART,ENABLE);
	
	
	
	/* WIFI_USART configuration */
	USART_InitStructure.USART_BaudRate = WIFI_USART_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(WIFI_USART,&USART_InitStructure);
	/* Enable WIFI_USART Receive interrupts */
	USART_ITConfig(WIFI_USART,USART_IT_RXNE,ENABLE);
	/* Enable the WIFI_USART */
	USART_Cmd(WIFI_USART,DISABLE);
	
	
	
	/* LORA_USART configuration */
	USART_InitStructure.USART_BaudRate = LORA_USART_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(LORA_USART,&USART_InitStructure);
	/* Enable LORA_USART Receive interrupts */
	USART_ITConfig(LORA_USART,USART_IT_RXNE,DISABLE);
	/* Enable the LORA_USART */
	USART_Cmd(LORA_USART,ENABLE);
	
}

/**
  * @brief  Configures the nested vectored interrupt controller.
  * @param  None
  * @retval None
  */
void NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
	
	/* DEBUG_USART IRQ Channel configuration */
	NVIC_InitStructure.NVIC_IRQChannel = DEBUG_USART_IRQ;
	NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	/* NH3_USART IRQ Channel configuration */
	NVIC_InitStructure.NVIC_IRQChannel = NH3_USART_IRQ;
	NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	/* CH2O_USART IRQ Channel configuration */
	NVIC_InitStructure.NVIC_IRQChannel = CH2O_USART_IRQ;
	NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	/* PM25_USART IRQ Channel configuration */
	NVIC_InitStructure.NVIC_IRQChannel = PM25_USART_IRQ;
	NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	/* H2S_USART IRQ Channel configuration */
	NVIC_InitStructure.NVIC_IRQChannel = H2S_USART_IRQ;
	NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	/* CO2_USART IRQ Channel configuration */
	NVIC_InitStructure.NVIC_IRQChannel = CO2_USART_IRQ;
	NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	/* SP3485_USART IRQ Channel configuration */
	NVIC_InitStructure.NVIC_IRQChannel = WIFI_USART_IRQ;
	NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	/* LORA_USART IRQ Channel configuration */
	NVIC_InitStructure.NVIC_IRQChannel = LORA_USART_IRQ;
	NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
}


void EXTI_Button_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	EXTI_InitTypeDef EXTI_InitStructure;
	
	//按键引脚配置
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
	
	//EXTI配置
	EXTI_InitStructure.EXTI_Line = EXTI_Line0;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure);
	
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB,EXTI_PinSource0);
	
	
	/* Button0 IRQ Channel configuration */
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = EXTI0_1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
}


/*******************************************************************************
**函数名称:UsartxSendDataByte(USART_TypeDef* USARTx,uint16_t Data)
**功能描述:串口发送一个字节
**入口参数:USART_TypeDef* USARTx 串口句柄
						uint16_t Data					要发送的数据
**输出:无
*******************************************************************************/
void UsartxSendDataByte(USART_TypeDef* USARTx,uint16_t Data)
{
	USART_SendData(USARTx,Data);
  while(USART_GetFlagStatus(USARTx,USART_FLAG_TXE)==RESET);
}

/*******************************************************************************
**函数名称:UsartxSendDataStr(USART_TypeDef* USARTx,const uint8_t *Data,uint32_t len)
**功能描述:串口发送字符串
**入口参数:USART_TypeDef* USARTx 串口句柄
						uint8_t *Data					要发送的数据
						uint32_t len					数据长度
**输出:无
*******************************************************************************/
void UsartxSendDataStr(USART_TypeDef* USARTx,const uint8_t *Data,uint32_t len)
{
	while(len--)
	{
		USART_SendData(USARTx,*(Data++));
		while(USART_GetFlagStatus(USARTx,USART_FLAG_TXE)==RESET);
	}
}