#include "config.h"


volatile u8 SensorCount2TXTask_Flag = 1;//SensorCount接受任务标志位，1允许运行
volatile u8 SensorCount_DMA_RX_Len  = 23;
/*_____ D E F I N I T I O N __________________________________________________*/


/****************************************************
函数名：SensorCount_IO_Configuration
功能  ：SensorCount ModBus通讯部分用到的IO初始化
参数  ：无
返回值：无
****************************************************/
void SensorCount_IO_Configuration(void)
{
 	GPIO_InitTypeDef GPIO_InitStructure;
  	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
	
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5);  
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5);
	
	
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
	GPIO_Init(GPIOD, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
  GPIO_Init(GPIOD, &GPIO_InitStructure);//初始化
	
	Sensor2_RS485_RxEN;
}
/****************************************************
函数名：SensorCount_NVIC_Configuration
功能  ：SensorCount ModBus通讯Usart1和发送DMA中断配置
参数  ：无
返回值：无
****************************************************/
void SensorCount_NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;	//定义中断配置结构体
	
	//USART5_DMA

	NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream7_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream0_IRQn;	
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;		//
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;	
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;		
	NVIC_Init(&NVIC_InitStructure);	

}

/*************************************************
函数名  ：SensorCount_USART_Configuration（）
功能    : 串口初始化
传入参数：无
返回值  ：无
**************************************************/
void SensorCount_USART_Configuration(u32 Baud,u8 Check)
{
  USART_InitTypeDef USART_InitStructure;
	  
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);
	USART_InitStructure.USART_BaudRate =Baud;// Baud;//?????
	
	if(Check == Even)
	{
		USART_InitStructure.USART_WordLength = USART_WordLength_9b;
		USART_InitStructure.USART_Parity = USART_Parity_Even;
		}
	else if(Check == None)
	{
		USART_InitStructure.USART_WordLength = USART_WordLength_8b;
		USART_InitStructure.USART_Parity = USART_Parity_No;
		}
		
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_Init(UART5, &USART_InitStructure);
	USART_Cmd(UART5, ENABLE);
	 
	
	USART_DMACmd(UART5, USART_DMAReq_Tx | USART_DMAReq_Rx,ENABLE);
	
}
/*************************************************
函数名  ：DMA_Configuration（）
功能    ：DMA初始化
传入参数：无
返回值  ：无
**************************************************/
void SensorCount_DMA_Configuration(void)
{
    DMA_InitTypeDef DMA_InitStructure;
 
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);  

		/*******************发送TX*********************/
	  DMA_DeInit(DMA1_Stream7);		
		DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;   
		//DMA_InitStructure.DMA_BufferSize = UART_DMA_SEND_LEN;   
		DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;  
		DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;  
		DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;  
		DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;  
		DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;  
		DMA_InitStructure.DMA_Priority = DMA_Priority_High;  
		DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;  
		DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;  
		DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;  
		DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; 
		DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&UART5->DR; 	
		DMA_InitStructure.DMA_Channel = DMA_Channel_4; 
			
		
		DMA_Init(DMA1_Stream7, &DMA_InitStructure); 		 
		DMA_ITConfig(DMA1_Stream7, DMA_IT_TC, ENABLE);	
		//DMA_Cmd(DMA1_Stream3, ENABLE);
		
		/*******************RX*********************/
	  DMA_DeInit(DMA1_Stream0);		
		DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;   
		DMA_InitStructure.DMA_BufferSize = 23;   
		DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;  
		DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;  
		DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;  
		DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;  
		DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;  
		DMA_InitStructure.DMA_Priority = DMA_Priority_High;  
		DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;  
		DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;  
		DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;  
		DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; 
		DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&UART5->DR; 	
		DMA_InitStructure.DMA_Channel = DMA_Channel_4; 
		DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)YWYData.SensorMail.Buff;	
		
		
		DMA_Init(DMA1_Stream0, &DMA_InitStructure); 	
    DMA_ITConfig(DMA1_Stream0, DMA_IT_TC, ENABLE);
    DMA_ClearFlag(DMA1_Stream0, DMA_IT_TCIF0);		
	  DMA_Cmd(DMA1_Stream0, ENABLE);
}

//F*****************************************************************************
//* NAME:    SensorCount2_DMATX_IRQHandler
//* PURPOSE: DMA传输完成中断，SensorCount ModBus
//* PARAMS: 无
//* return:	 无
//******************************************************************************
void SensorCount2_DMATX_IRQHandler(void)
{  
  SensorCount2_TX_DMAStop();    //关闭DMA传输 (SensorCount发送)
	/**********************DR寄存器里面有数据开启DMA时会自动放入一个数据，长度减一。应对方法：接收中断重设长度，打开DMA，发送中断关闭DMA，重设长度，打开DMA********************/
	SensorCount2_RX_DMAStop();
	SensorCount2_RX_DMALenSet(SensorCount_DMA_RX_Len);//重置接受长度
	
	DMA_ClearFlag(DMA1_Stream7, DMA_IT_TCIF7); 
	while(! USART_GetFlagStatus(UART5, USART_FLAG_TC))NULL;
    Sensor2_RS485_RxEN;
	SensorCount2TXTask_Flag = 1;

	SensorCount2_RX_DMAStart();
	
}
//F*****************************************************************************
//* NAME:    SensorCount_DMARX_IRQHandler
//* PURPOSE: DMA传输完成中断，SensorCount ModBus
//* PARAMS: 无
//* return:	 无
//******************************************************************************
void SensorCount2_DMARX_IRQHandler(void)
{  
	u16 CRC_Temp;
	SensorCount2_RX_DMAStop();   //DMA接收
	DMA_ClearFlag(DMA1_Stream0, DMA_IT_TCIF0);
	
	SensorCount2_RX_DMAAddrSet((u32)YWYData.SensorMail.Buff);
	SensorCount2_RX_DMALenSet(SensorCount_DMA_RX_Len);//重置接受长度
	SensorCount2_RX_DMAStart();

	CRC_Temp = CRC_Check(YWYData.SensorMail.Buff,YWYData.SensorMail.Register.Len + 3);
	if(CRC_Temp == YWYData.SensorMail.Register.Crc)
	{
		YWYData.SensorMail_Flag = 1;            //使能LcdCount接收任务	 
		}     

		      //使能LcdCount接收任务	
}
//F*****************************************************************************
//* NAME:    SensorCount_Init
//* PURPOSE: SensorCount通讯初始化
//* PARAMS: 无
//* return:	 无
//******************************************************************************
void SensorCount_Init(void)
{  
	u8 i = 0;
  SensorCount_IO_Configuration(); 
	SensorCount_USART_Configuration(9600,None);                 
	SensorCount_DMA_Configuration();          
  SensorCount_NVIC_Configuration();           

	YWYData.Sensor_Ask_Timer.Init = 0;            //清除初始化标志
	YWYData.Sensor_Ask_Timer.Flag = 1;            //清除到时标志
	YWYData.Sensor_Ask_Timer.ON_OFF = 1;          //打开轮训开关
	YWYData.Sensor_Ask_Timer.Cycle = 1;           //设置为循环模式
	YWYData.Sensor_Ask_Timer.TimerCountMax = 100;  //周期100（1S）Sensor_ERR_Timer
	
	YWYData.Sensor_ERR_Timer[0].Init = 0;            //清除初始化标志
	YWYData.Sensor_ERR_Timer[0].Flag = 0;            //清除到时标志
	YWYData.Sensor_ERR_Timer[0].ON_OFF = 1;          //打开轮训开关
	YWYData.Sensor_ERR_Timer[0].Cycle = 1;           //设置为循环模式
	YWYData.Sensor_ERR_Timer[0].TimerCountMax = 200;  //周期100（1S）Sensor_ERR_Timer
	
	YWYData.Sensor_ERR_Timer[1].Init = 0;            //清除初始化标志
	YWYData.Sensor_ERR_Timer[1].Flag = 0;            //清除到时标志
	YWYData.Sensor_ERR_Timer[1].ON_OFF = 1;          //打开轮训开关
	YWYData.Sensor_ERR_Timer[1].Cycle  = 1;           //设置为循环模式
	YWYData.Sensor_ERR_Timer[1].TimerCountMax = 200;  //周期100（1S）
	
	YWYData.PowerOffDelay_Timer.Init = 0;            //清除初始化标志
	YWYData.PowerOffDelay_Timer.Flag = 0;            //清除到时标志
	YWYData.PowerOffDelay_Timer.ON_OFF = 0;          //打开轮训开关
	YWYData.PowerOffDelay_Timer.Cycle = 0;           //设置为循环模式
	YWYData.PowerOffDelay_Timer.TimerCountMax = 6000;  //周期6000（60S）
	
}

/***********RS485/探针命令发送************

*****************************************
*****************************************/
u8 Sensor_TX_Buff[10] = {0x00,0x03,0x00,0x02,0x00,0x01,0x00,0x00};
u8 Sensor_Ask_Data(u8 ID)//
{
  
  u16 i = 10;
  u16 CRC_Temp = 0;
	
	Sensor2_RS485_TxEN;
	SensorCount2TXTask_Flag = 0;
	Sensor_TX_Buff[0] = ID + 1;
	CRC_Temp = CRC_Check(Sensor_TX_Buff,6);
	Sensor_TX_Buff[7] = CRC_Temp >> 8;
	Sensor_TX_Buff[6] = CRC_Temp;

	SensorCount2_TX_DMAAddrSet((u32)Sensor_TX_Buff);
	SensorCount2_TX_DMALenSet(8);
	SensorCount2_TX_DMAStart();
	SensorCount_DMA_RX_Len = 7;
}
u8 Sensor_Ask_NumNow = 0;
void Sensor_Tast(void)
{
	if(YWYData.Sensor_Ask_Timer.Flag == 1)
	{
	  YWYData.Sensor_Ask_Timer.Flag = 0;
		Sensor_Ask_Data(Sensor_Ask_NumNow);
		Sensor_Ask_NumNow ++;
		if(Sensor_Ask_NumNow > 1)
			Sensor_Ask_NumNow = 0;
	  }
	
	if(YWYData.SensorMail_Flag == 1)
	{
	  YWYData.SensorMail_Flag = 0;
		if(YWYData.SensorMail.Register.SensorNum < 3)
		{
			if(YWYData.SensorMail.Register.Dat != 0x0200)
				YWYData.ErrState[YWYData.SensorMail.Register.SensorNum - 1].Flag.GasLeakage = 0;
			else
				YWYData.ErrState[YWYData.SensorMail.Register.SensorNum - 1].Flag.GasLeakage = 1;
			}  
	  }
  }