/*--------------------------------------------------------------------------------------
*  @file     HostSlaveHandle.c
*  @author   ZhangJing
*  @version  base on stm32f0x   
*  @date     2015.09.11
*  @brief    主从芯片数据处理
---------------------------------------------------------------------------------------*/
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "TypeDefine.h"
#include "USARTSetup.h"
#include "SystemAlarm.h"
#include "HostSlaveHandle.h"

/***************************************************************************************
*  Function:	HostToSlaveHandle						                                                           
*  Object:		主芯片向从芯片发送数据处理
*  输入:			无
*  输出:			无	                                     
*  备注:			1、在非待机状态下每100ms发送一次数据给从机
*							2、进入待机状态时不给从机发命令，保持休眠，节省功耗
****************************************************************************************/
/*void HostToSlaveHandle( void )
{
	uint8_t tempIndex = 0;
	
	usart3XmitBuffer[tempIndex++] = 0xaa; //AA 55 为数据同步字头
	usart3XmitBuffer[tempIndex++] = 0x55;
	usart3XmitBuffer[tempIndex++] = 1;
	if( realTimeData.ctlSlaveRun == StandBy )
	{
		usart3XmitBuffer[tempIndex++] = 4; //泵头断电进入休眠状态
		USART3Send( usart3XmitBuffer, tempIndex );
		realTimeData.ctlSlaveRun = WaitState;
	}
	else if( realTimeData.ctlSlaveRun != WaitState )
	{
 		if( ( realTimeData.ctlSlaveRun == Poweroff ) && ( realTimeData.stateRun == Poweroff ) )
		{
			usart3XmitBuffer[tempIndex++] = 3; //关机命令
		}
		else if( ( sysAlarmFlag.MechanicalFault == ALARM_ON ) || ( sysAlarmFlag.MechanicalFault == ALARM_CONTINUE ) )
		{
			usart3XmitBuffer[tempIndex++] = 2; //机械报警，关闭电机电源
		}		
		else
		{
			usart3XmitBuffer[tempIndex++] = 1;  //电机开始工作
		}
		if( TaskSchedulerFlag.hostXmitFlag == TASK_FLAG_SET )
		{
			
			USART3Send( usart3XmitBuffer, tempIndex );
			
	 		TaskSchedulerFlag.hostXmitFlag = TASK_FLAG_CLEAR;
		}
	}
}
*/

void Power_off(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure;
  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
	
	/* Configure the GPIO_MainPOWER pin */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOD, &GPIO_InitStructure);	
	/* 打开总电源（MCU 电源）	*/
	GPIO_ResetBits(GPIOD,GPIO_Pin_5);
}
	
void HostToSlaveHandle( void )
{
//	uint8_t tempIndex = 0;
	
/*  if( realTimeData.ctlSlaveRun == StandBy )
	{
		
		realTimeData.ctlSlaveRun = WaitState;
	}
	
	else if( realTimeData.ctlSlaveRun != WaitState )*/
	{
 		if( ( realTimeData.ctlSlaveRun == Poweroff ) && ( realTimeData.stateRun == Poweroff ) )
		{
			//GPIO_SetBits(GPIOD,GPIO_Pin_5); //关机命令
			Power_off();
			
		}
		else if( ( sysAlarmFlag.MechanicalFault == ALARM_ON ) || ( sysAlarmFlag.MechanicalFault == ALARM_CONTINUE ) )
		{
			GPIO_ResetBits(GPIOA,GPIO_Pin_8);	 //机械报警，关闭电机电源
		}		
		else
		{
			GPIO_SetBits(GPIOA,GPIO_Pin_8);  //电机开始工作
		}
	}
}

