#ifndef GPIO_CFG_H_
#define GPIO_CFG_H_
#include "base/define.h"

//具体实现思想,参考<<CM3权威指南>>第五章(87页~92页).
//IO口操作宏定义
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2)) 
#define MEM_ADDR_(addr)  *((volatile unsigned long  *)(addr)) 
#define BIT_ADDR_(addr, bitnum)   MEM_ADDR_(BITBAND(addr, bitnum)) 
//IO口地址映射
#define GPIOA_ODR_Addr    (GPIOA_BASE+12) //0x4001080C 
#define GPIOB_ODR_Addr    (GPIOB_BASE+12) //0x40010C0C 
#define GPIOC_ODR_Addr    (GPIOC_BASE+12) //0x4001100C 
#define GPIOD_ODR_Addr    (GPIOD_BASE+12) //0x4001140C 
#define GPIOE_ODR_Addr    (GPIOE_BASE+12) //0x4001180C 
#define GPIOF_ODR_Addr    (GPIOF_BASE+12) //0x40011A0C    
#define GPIOG_ODR_Addr    (GPIOG_BASE+12) //0x40011E0C    

#define GPIOA_IDR_Addr    (GPIOA_BASE+8) //0x40010808 
#define GPIOB_IDR_Addr    (GPIOB_BASE+8) //0x40010C08 
#define GPIOC_IDR_Addr    (GPIOC_BASE+8) //0x40011008 
#define GPIOD_IDR_Addr    (GPIOD_BASE+8) //0x40011408 
#define GPIOE_IDR_Addr    (GPIOE_BASE+8) //0x40011808 
#define GPIOF_IDR_Addr    (GPIOF_BASE+8) //0x40011A08 
#define GPIOG_IDR_Addr    (GPIOG_BASE+8) //0x40011E08 
 
//IO口操作,只对单一的IO口!
//确保n的值小于16!
#define PAout(n)   BIT_ADDR_(GPIOA_ODR_Addr,n)  //输出 
#define PAin(n)    BIT_ADDR_(GPIOA_IDR_Addr,n)  //输入 

#define PBout(n)   BIT_ADDR_(GPIOB_ODR_Addr,n)  //输出 
#define PBin(n)    BIT_ADDR_(GPIOB_IDR_Addr,n)  //输入 

#define PCout(n)   BIT_ADDR_(GPIOC_ODR_Addr,n)  //输出 
#define PCin(n)    BIT_ADDR_(GPIOC_IDR_Addr,n)  //输入 

#define PDout(n)   BIT_ADDR_(GPIOD_ODR_Addr,n)  //输出 
#define PDin(n)    BIT_ADDR_(GPIOD_IDR_Addr,n)  //输入 

#define PEout(n)   BIT_ADDR_(GPIOE_ODR_Addr,n)  //输出 
#define PEin(n)    BIT_ADDR_(GPIOE_IDR_Addr,n)  //输入

#define PFout(n)   BIT_ADDR_(GPIOF_ODR_Addr,n)  //输出 
#define PFin(n)    BIT_ADDR_(GPIOF_IDR_Addr,n)  //输入

#define PGout(n)   BIT_ADDR_(GPIOG_ODR_Addr,n)  //输出 
#define PGin(n)    BIT_ADDR_(GPIOG_IDR_Addr,n)  //输入

#define VCC_2V5_Pin							GPIO_Pin_1
#define VCC_2V5_Pin_Nu					1
#define VCC_2V5_GPIO_Port				GPIOA

#define R_AD_01_Pin							GPIO_Pin_3
#define R_AD_01_Nu							3
#define R_AD_01_Port						GPIOA

#define VCC_1V25_Pin						GPIO_Pin_3
#define VCC_1V25_Pin_Nu					3
#define VCC_1V25_GPIO_Port			GPIOA


#define C_H_DAC_Pin							GPIO_Pin_4
#define C_H_DAC_Pin_Nu					4
#define C_H_DAC_GPIO_Port				GPIOA

#define C_M_DAC_Pin							GPIO_Pin_5
#define C_M_DAC_Pin_Nu					5
#define C_M_DAC_GPIO_Port				GPIOA

#define V_LA_M_Pin							GPIO_Pin_6
#define V_LA_M_Pin_Nu						6
#define V_LA_M_GPIO_Port				GPIOA

#define V_LA_H_Pin							GPIO_Pin_7
#define V_LA_H_Pin_Nu						7
#define V_LA_H_GPIO_Port				GPIOA

#define LAVC_M_Pin							GPIO_Pin_8
#define LAVC_M_Pin_Nu						8
#define LAVC_M_GPIO_Port				GPIOA

#define USART1_TX_Pin						GPIO_Pin_9
#define USART1_TX_Pin_Nu				9
#define USART1_TX_GPIO_Port			GPIOA

#define USART1_RX_Pin						GPIO_Pin_10
#define USART1_RX_Pin_Nu				10
#define USART1_RX_GPIO_Port			GPIOA

#define CAN1_RX_Pin							GPIO_Pin_10
#define CAN1_RX_Pin_Nu					11
#define CAN1_RX_GPIO_Port				GPIOA

#define CAN1_TX_Pin							GPIO_Pin_11
#define CAN1_TX_Pin_Nu					12
#define CAN1_TX_GPIO_Port				GPIOA

#define SEG0_Pin								GPIO_Pin_15
#define SEG0_Pin_Nu							15
#define SEG0_GPIO_Port					GPIOA


#define R10_ON_Pin							  GPIO_Pin_0
#define R10_ON_Pin_Nu						0
#define R10_ON_GPIO_Port 				GPIOB


#define R510_ON_Pin							GPIO_Pin_1
#define R510_ON_Pin_Nu					1
#define R510_ON_GPIO_Port 			GPIOB

#define LED1_Pin								GPIO_Pin_2
#define LED1_Pin_Nu							2
#define LED1_GPIO_Port 					GPIOB

#define IO_CAP_AD_Pin						GPIO_Pin_5
#define IO_CAP_AD_Pin_Nu				5
#define IO_CAP_AD_Port					GPIOB

#define FIRE_TEST_Pin						GPIO_Pin_8
#define FIRE_TEST_Pin_Nu	  		8
#define FIRE_TEST_Port					GPIOB

#define IO_APD12_Pin						GPIO_Pin_9
#define IO_APD12_Pin_Nu					9
#define IO_APD12_GPIO_Port			GPIOB

#define I2IC_SCL_Pin						GPIO_Pin_10
#define I2IC_SCL_Pin_Nu					10
#define I2IC_SCL_GPIO_Port			GPIOB

#define I2IC_SDA_Pin						GPIO_Pin_11
#define I2IC_SDA_Pin_Nu					11
#define I2IC_SDA_GPIO_Port			GPIOB


#define LED2_Pin								GPIO_Pin_12
#define LED2_Pin_Nu							12
#define LED2_GPIO_Port 					GPIOB

#define OUTAL_Pin								GPIO_Pin_13
#define OUTAL_Pin_Nu						13
#define OUTAL_GPIO_Port 				GPIOB

#define OUTBL_Pin								GPIO_Pin_14
#define OUTBL_Pin_Nu						14
#define OUTBL_GPIO_Port 				GPIOB

#define OUTAH_Pin								GPIO_Pin_15
#define OUTAH_Pin_Nu						15
#define OUTAH_GPIO_Port 				GPIOB


#define AD_OUTA_Pin		  				GPIO_Pin_0
#define AD_OUTA_Pin_Nu					0
#define AD_OUTA_Port 						GPIOC

#define AD_OUTB_Pin		  				GPIO_Pin_1
#define AD_OUTB_Pin_Nu					1
#define AD_OUTB_Port 						GPIOC

#define AN_UA_Pin		  					GPIO_Pin_2
#define AN_UA_Pin_Nu						2
#define AN_UA_Port 							GPIOC

#define AN_MAL_Pin		  				GPIO_Pin_3
#define AN_MAL_Pin_Nu						3
#define AN_MAL_Port 						GPIOC

#define R100_ON_Pin							GPIO_Pin_4
#define R100_ON_Pin_Nu					4
#define R100_ON_GPIO_Port 			GPIOC

#define AN_CAP_AD_Pin		  			GPIO_Pin_5
#define AN_CAP_AD_Pin_Nu				5
#define AN_CAP_AD_Port 					GPIOC

#define OUTBH_Pin								GPIO_Pin_6
#define OUTBH_Pin_Nu						6
#define OUTBH_GPIO_Port 				GPIOC

#define POWER_ON_Pin						GPIO_Pin_7
#define POWER_ON_Pin_Nu					7
#define POWER_ON_GPIO_Port			GPIOC

#define SEG4_Pin								GPIO_Pin_8
#define SEG4_Pin_Nu							8
#define SEG4_GPIO_Port					GPIOC

#define SEG3_Pin								GPIO_Pin_9
#define SEG3_Pin_Nu							9
#define SEG3_GPIO_Port					GPIOC

#define SEG1_Pin								GPIO_Pin_10
#define SEG1_Pin_Nu							10
#define SEG1_GPIO_Port					GPIOC

#define SEG2_Pin								GPIO_Pin_11
#define SEG2_Pin_Nu							11
#define SEG2_GPIO_Port					GPIOC

#define U3_TXD_Pin							GPIO_Pin_10
#define U3_TXD_Pin_Nu						10
#define U3_TXD_GPIO_Port				GPIOC

#define U3_RXD_Pin							GPIO_Pin_11
#define U3_RXD_Pin_Nu						11
#define U3_RXD_GPIO_Port				GPIOC

#define IO_APD22_Pin						GPIO_Pin_13
#define IO_APD22_Pin_Nu					13
#define IO_APD22_GPIO_Port			GPIOC


#define IO_APD21_Pin						GPIO_Pin_14
#define IO_APD21_Pin_Nu					14
#define IO_APD21_GPIO_Port			GPIOC

#define IO_APD11_Pin						GPIO_Pin_15
#define IO_APD11_Pin_Nu					15
#define IO_APD11_GPIO_Port			GPIOC










#define LED2_Out     PBout(LED2_Pin_Nu)
#define LED1_Out     PBout(LED1_Pin_Nu)
#define LED1_Out_On     PBout(LED1_Pin_Nu)=0
#define LED1_Out_Off     PBout(LED1_Pin_Nu)=1

//电源开关

#define POWER_ON  {PCout(POWER_ON_Pin_Nu) = 1;}
#define POWER_OFF  {PCout(POWER_ON_Pin_Nu) = 0;}

//桥驱动
#define HMOS_SWITCH_W_1   {PAout(LAVC_M_Pin_Nu)=0; PCout(OUTBH_Pin_Nu)=0;PBout(OUTAL_Pin_Nu)=0; delay_nop(50); PBout(OUTBL_Pin_Nu)=1;PBout(OUTAH_Pin_Nu)=1;}
#define HMOS_SWITCH_W_0   {PAout(LAVC_M_Pin_Nu)=0; PBout(OUTBL_Pin_Nu)=0;PBout(OUTAH_Pin_Nu)=0; delay_nop(50); PBout(OUTAL_Pin_Nu)=1;PCout(OUTBH_Pin_Nu)=1;}

#define HMOS_SWITCH_RW_1  {PCout(OUTBH_Pin_Nu)=0;PBout(OUTAL_Pin_Nu)=0; delay_nop(50); PBout(OUTBL_Pin_Nu)=1;PBout(OUTAH_Pin_Nu)=1;PAout(LAVC_M_Pin_Nu)=1;}
#define HMOS_SWITCH_RW_0  {PAout(LAVC_M_Pin_Nu)=0; PBout(OUTBL_Pin_Nu)=0;PBout(OUTAH_Pin_Nu)=0; delay_nop(50); PBout(OUTAL_Pin_Nu)=1;PCout(OUTBH_Pin_Nu)=1;}

#define HMOS_SWITCH_GND   {PCout(OUTBH_Pin_Nu)=0; PAout(LAVC_M_Pin_Nu)=0;PBout(OUTAH_Pin_Nu)=0;delay_nop(50);PBout(OUTBL_Pin_Nu)=1;PBout(OUTAL_Pin_Nu)=1;}
#define HMOS_SWITCH_OFF   {PCout(OUTBH_Pin_Nu)=0; PAout(LAVC_M_Pin_Nu)=0;PBout(OUTAH_Pin_Nu)=0;delay_nop(50);PBout(OUTBL_Pin_Nu)=0;PBout(OUTAL_Pin_Nu)=0;}
#define HMOS_FAST_DISCHG  {PCout(POWER_ON_Pin_Nu) = 0; PCout(OUTBH_Pin_Nu)=0; PAout(LAVC_M_Pin_Nu)=0;PBout(OUTBL_Pin_Nu)=0;delay_nop(50);PBout(OUTAH_Pin_Nu)=1;PBout(OUTAL_Pin_Nu)=1;}
#define HMOS_SWITCH_ON    {PAout(LAVC_M_Pin_Nu)=0;PCout(OUTBH_Pin_Nu)=0; PBout(OUTAL_Pin_Nu)=0;delay_nop(50);PBout(OUTBL_Pin_Nu)=1;PBout(OUTAH_Pin_Nu)=1;}

//芯跳桥驱动
#define XTBus_POWER_Out   HMOS_SWITCH_W_1//总线模式开关
#define XTBUS_ON       		HMOS_SWITCH_ON
#define XTBUS_OFF					HMOS_SWITCH_OFF
#define XTBUS_W_1					HMOS_SWITCH_W_1
#define XTBUS_W_0					HMOS_SWITCH_W_0
#define XTBUS_WR_1				HMOS_SWITCH_W_1
#define XTBUS_WR_0				HMOS_SWITCH_W_0

//JQ 桥驱动

#define JQBus_POWER_Out      HMOS_SWITCH_W_1 //总线模式开关
#define JQBUS_ON       			 HMOS_SWITCH_ON
#define JQBUS_OFF						 HMOS_SWITCH_OFF
#define JQBUS_W_1						 HMOS_SWITCH_W_1
#define JQBUS_W_0						 HMOS_SWITCH_W_0

//EW 桥驱动
#define EW_SINGLE_MOD_M {PCout(OUTBH_Pin_Nu)=0;PBout(OUTAL_Pin_Nu)=0;PBout(OUTAH_Pin_Nu)=0; delay_nop(50); PBout(OUTBL_Pin_Nu)=1;PAout(LAVC_M_Pin_Nu)=1;}
#define EW_SINGLE_MOD_H HMOS_SWITCH_W_1

//#define EW_DIFF_MOD_M  {PCout(OUTBH_Pin_Nu)=0;PBout(OUTAL_Pin_Nu)=0; delay_nop(50); PBout(OUTBL_Pin_Nu)=1;PBout(OUTAH_Pin_Nu)=1;PAout(LAVC_M_Pin_Nu)=1;}
#define EW_DIFF_MOD_M  {PCout(OUTBH_Pin_Nu)=0;PBout(OUTAL_Pin_Nu)=0;PBout(OUTAH_Pin_Nu)=0; delay_nop(50); PBout(OUTBL_Pin_Nu)=1;PAout(LAVC_M_Pin_Nu)=1;}

#define EW_DIFF_MOD_H  HMOS_SWITCH_W_1
#define EW_DIFF_MOD_L  HMOS_SWITCH_W_0
#define EW_BUS_OFF     HMOS_SWITCH_OFF
#define EW_BUS_ON  		 HMOS_SWITCH_ON


void CtrlGpio_DefInit(void);
/*
@brief 获取设备地址
@param 无
@rtv 设备地址
*/
uint8_t Gpio_GetDeivceAddr(void);

/*
@brief 4限制电阻测量通道切换
@param 0 全关 
			1 通道1-4通，过桥丝
			2 通道2-3通，过桥丝
			3 通道1-3通，测阻抗
			4 通道2-4通，测阻抗
*/
void Gpio_ResistorSwitch(uint8_t uc_r);
/*
@brief 测量电容电压压降GPIO配置
*/
void Gpio_CAPSwitch(uint8_t uc_sw);
#endif

