#include "ys32md211.h"
#include "delay.h"
#include "ys32md211.h"
#include "COM.h"

/**
  * @brief  NVIC ???
  * @param  None
  * @retval None
  */
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  EXTI_InitTypeDef EXTI_InitStructure;

  /* ???? */
  NVIC_InitStructure.NVIC_IRQChannel = ADC_COMP_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;       //???????
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;       //????
  NVIC_Init(&NVIC_InitStructure);                       //?????

  EXTI_StructInit(&EXTI_InitStructure);
  /* EXTI??????? */
  EXTI_InitStructure.EXTI_Line = EXTI_Line17;              //???????17(COMP1)
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;      //???????
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;  //???????????/???
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;                //?????
  EXTI_Init(&EXTI_InitStructure);                          //???????
}

void COMP_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
  COMP_InitTypeDef COMP_InitStruct;

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_COMP1, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_COMP2, ENABLE);
	
	RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_GPIOA, ENABLE);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;                    
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;                     
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;           
  GPIO_Init(GPIOA, &GPIO_InitStructure); 
	
	COMP_DeInit(COMP2);

  COMP_StructInit(&COMP_InitStruct);

  /*COMP_InitStruct.COMP_PowerMode = COMP_PowerMode_High;
  COMP_InitStruct.COMP_Polarity = COMP_Polarity_NotReversed;
  COMP_InitStruct.COMP_FilterCount = 0;
  COMP_Init(COMP1, &COMP_InitStruct);
  COMP1_InputPlusConfig(COMP1, COMP1_InputPlus_PA3);
  COMP1_InputMinusConfig(COMP1, COMP1_InputMinus_PA0);

  COMP_DigitalFilterCountConfig(COMP1, 10);
  COMP_DigitalFilterCmd(COMP1, ENABLE);
  COMP12_InputHysteresisCmd(COMP1, ENABLE);
  COMP1_WindowModeConfig(COMP1, COMP1_WindowMode_INPSEL);
  COMP_Cmd(COMP1, ENABLE);
  COMP_LockCmd(COMP1, ENABLE);*/
	// COMP4->CSR=0;
  // COMP4->CSR=COM4_SET;//0x1|(1<<7)|(7<<10)|(1<<31);
	COMP_LockCmd(COMP2, DISABLE);//DISABLE
  COMP2_WindowModeConfig(COMP2_WindowMode_INPSEL);
	COMP_DigitalFilterCountConfig(COMP2, 50);
  COMP_AnologFilterSelect(COMP2,COMP_AnologFilter_Low);
  COMP_DigitalFilterCmd(COMP2, ENABLE);
  COMP12_InputHysteresisCmd(ENABLE);



}
