#include "AnalogChannelCtrl_2d5g_base.h"
#include "delay.h"
#include "sys.h"
#include "AnalogChannelCtrl.h"
#include "AnalogChannelCtrl_Dictionary.h"
//////////////////////////////////////////////////////////////////////////////////
// 			2203/7/X GHz_ljw 合并版
//////////////////////////////////////////////////////////////////////////////

u8 CH1_PROTECT_EN=0;
u8 CH2_PROTECT_EN=0;
u8 CH3_PROTECT_EN=0;
u8 CH4_PROTECT_EN=0;
unsigned char TRIG_ShiftReg_CMD_temp;
unsigned long CH_ShiftReg_CMD_temp;
 

void AnalogChProtect_GPIO_Init(void)
{
    NVIC_InitTypeDef   NVIC_InitStructure;
    EXTI_InitTypeDef   EXTI_InitStructure;

    //KEY_Init(); //CH1,CH2,CH3,CH4对应保护引脚IO口初始化
    GPIO_InitTypeDef  GPIO_InitStructure;

// RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOE, ENABLE);//使能GPIOA.GPIOC.GPIOD.GPIOE时钟

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //CH3对应保护引脚
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通输入模式
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100M
//  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN ;//下拉
    GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOD0,1,2,3

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //CH1对应保护引脚
    GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化GPIOD0,1,2,3

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //CH2对应保护引脚
    GPIO_Init(GPIOD, &GPIO_InitStructure);//初始化GPIOD0,1,2,3

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //CH4对应保护引脚
    GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化GPIOD0,1,2,3
 
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//使能SYSCFG时钟
 
    SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource11);//PD0 连接到中断线0
    SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOD, EXTI_PinSource8);//PD1 连接到中断线1
    SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource3);//PD2 连接到中断线2
    SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource5);//PD3 连接到中断线3

    /* 配置EXTI_Line0,1,2,3 */
    EXTI_InitStructure.EXTI_Line = EXTI_Line3|EXTI_Line5 | EXTI_Line8 | EXTI_Line11;//LINE0/LINE1/LINE2/LINE3
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//中断事件
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //上升沿触发
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;//使能LINE0
    EXTI_Init(&EXTI_InitStructure);//配置

//	/* 配置EXTI_Line2,3,4 */
//	EXTI_InitStructure.EXTI_Line = EXTI_Line2 | EXTI_Line3 | EXTI_Line4;
//  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//中断事件
//  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿触发
//  EXTI_InitStructure.EXTI_LineCmd = ENABLE;//中断线使能
//  EXTI_Init(&EXTI_InitStructure);//配置

    NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;//外部中断0
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//抢占优先级1
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;//子优先级2
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道
    NVIC_Init(&NVIC_InitStructure);//配置

    NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;//外部中断1
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//抢占优先级1
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;//子优先级2
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道
    NVIC_Init(&NVIC_InitStructure);//配置


    NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;//外部中断2
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//抢占优先级1
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03;//子优先级2
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道
    NVIC_Init(&NVIC_InitStructure);//配置


//	NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;//外部中断3
//  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//抢占优先级1
//  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x04;//子优先级2
//  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道
//  NVIC_Init(&NVIC_InitStructure);//配置

}

//过压保护触发，通道切换到高阻衰减档
void AnalogChCtrl_ProtectProcess(void)
{
		u8 Protected=0;
    if(CH1_PROTECT_EN==1)
    {
        CH_ShiftReg_CMD_temp&=0XFFFFFF00;
				//CH_ShiftReg_CMD_temp|=bwCtrlWords[2]; //500Mhz
				CH_ShiftReg_CMD_temp|=bwCtrlWords[1]; //1Ghz
        CD_4094(TRIG_ShiftReg_CMD_temp,1);
        CD_4094(CH_ShiftReg_CMD_temp,4);
        CH1_PROTECT_EN=0;
				Protected=1;
        //printf("CH1_PROTECT_EN\r\n");
    }
    if(CH2_PROTECT_EN==1)
    {
        CH_ShiftReg_CMD_temp&=0XFFFF00FF;
				//CH_ShiftReg_CMD_temp|=bwCtrlWords[2]<<8; //500Mhz
				CH_ShiftReg_CMD_temp|=bwCtrlWords[1]<<8; //1Ghz
        CD_4094(TRIG_ShiftReg_CMD_temp,1);
        CD_4094(CH_ShiftReg_CMD_temp,4);
        CH2_PROTECT_EN=0;
        //printf("CH2_PROTECT_EN\r\n");
				Protected=1;
    }
    if(CH3_PROTECT_EN==1)
    {
        CH_ShiftReg_CMD_temp&=0XFF00FFFF;
				//CH_ShiftReg_CMD_temp|=bwCtrlWords[2]<<16; //500Mhz
				CH_ShiftReg_CMD_temp|=bwCtrlWords[1]<<16;//1Ghz
        CD_4094(TRIG_ShiftReg_CMD_temp,1);
        CD_4094(CH_ShiftReg_CMD_temp,4);
        CH3_PROTECT_EN=0;
        //printf("CH3_PROTECT_EN\r\n");
				Protected=1;
    }
    if(CH4_PROTECT_EN==1)
    {
        CH_ShiftReg_CMD_temp&=0X00FFFFFF;
				//CH_ShiftReg_CMD_temp|=bwCtrlWords[2]<<24; //500Mhz
				CH_ShiftReg_CMD_temp|=bwCtrlWords[1]<<24; ///1Ghz
        CD_4094(TRIG_ShiftReg_CMD_temp,1);
        CD_4094(CH_ShiftReg_CMD_temp,4);
        CH4_PROTECT_EN=0;
        //printf("CH4_PROTECT_EN\r\n");
				Protected=1;
    }
		if (Protected==1)
        delay_us(100);
}
