/**
  ******************************************************************************
  * @file     CMP_CFG.c
  * @author   SINOMCU-AE
  * @brief    CMP config
  *    
  *          This file provides functions to CMP config:
  *   CMP1 INP2 PA1,out PA11,INN AVDD 4/8;Enable Interrupt.
  * @note    CMP filter not very well,It is recommended not to use CMP,The best choose is ADC     
  *         
  ******************************************************************************
  * @attention 
  *                           DISCLAIMER
  * The present firmware which is for guidance only aims at providing customers
  * with coding information regarding their products in order for them to save
  * time. As a result, SinoMCU shall not be held liable for any direct,indirect
  * or consequential damages with respect to any claims arising from the content 
  * of such firmware and/or the use made by customers of the coding information 
  * contained herein in connection with their products.
  *
  * <h2><center>&copy; Copyright (c) 2021 sinomcu All rights reserved.</center></h2>
  *
  ******************************************************************************
  */

#include "CMP_CFG.h"

/**
  * @brief  CMP1 Pin config
  *    PA1    <-- CMP1_INP2   
  *    PA11   --> CMP1_OUT
  * @param  None
  * @retval None
  */
void cmp_pin_init(void) {
  MS32_GPIO_InitTypeDef  GpioInitStruct;
  // PA1    <-- CMP1_INP2
  // PA11   --> CMP1_OUT
  MS32_GPIO_StructInit(&GpioInitStruct);  
  GpioInitStruct.Pin        = MS32_GPIO_PIN_1;  
  GpioInitStruct.Mode       = MS32_GPIO_MODE_ANALOG;
  // MS32_GPIO_StructInit():OUTPUT_PUSHPULL,LOW,PULL_NO,AF_0
  MS32_GPIO_Init(GPIOA,&GpioInitStruct); 

  GpioInitStruct.Pin        = MS32_GPIO_PIN_11;  
  GpioInitStruct.Mode       = MS32_GPIO_MODE_ALTERNATE;
  GpioInitStruct.OutputType = MS32_GPIO_OUTPUT_PUSHPULL;
  GpioInitStruct.Speed      = MS32_GPIO_SPEED_HIGH;
  GpioInitStruct.Pull       = MS32_GPIO_PULL_DOWN;
  GpioInitStruct.Alternate  = MS32_GPIO_AF_7;
  MS32_GPIO_Init(GPIOA,&GpioInitStruct);   
}

/**
  * @brief  CMP1 config
  *   PA1 INP2,INN AVDD 4/8; 
  * @param  None
  * @retval None
  */
void cmp1_init(void) {
  MS32_CMP_InitTypeDef CmpInitStruct;
  MS32_EXTI_InitTypeDef ExitInitStruct;
  
  cmp_pin_init();    
    
  MS32_CMP_StructInit(&CmpInitStruct);
  CmpInitStruct.HysteresisSel = MS32_COMP_HYST_0MV;        // MS32_COMP_HYST_30MV;
  CmpInitStruct.PositionSel   = MS32_COMP_POS_CPx2P_PIN;   // PA1 --> CMP1 INP2
  CmpInitStruct.NegativeSel   = MS32_COMP_NEG_CRV;         // CMP1 INN internal
  CmpInitStruct.CrvSel        = MS32_COMP_CRV_AVDD;        // CRV user AVDD
  CmpInitStruct.CrvDivSel     = MS32_COMP_CRV_4_DIV_8;     // CMP1 INN internal CRV 4/8
  CmpInitStruct.OutputFilter  =  MS32_COMP_OUT_FILTER_CLK128;   // do not use 256,512
  CmpInitStruct.OutputPolarity = MS32_COMP_OUT_POL_POS;
  CmpInitStruct.OutputSel     = MS32_COMP_OUT_TIM1_BKIN;
  MS32_CMP_Init(MS32_COMP1,&CmpInitStruct);                // init function had enable syscfg clock
  
  MS32_EXTI_StructInit(&ExitInitStruct);
  ExitInitStruct.Line_0_31   = MS32_EXTI_LINE_21;          // CMP1 interrupt
  ExitInitStruct.LineCommand = ENABLE;
  ExitInitStruct.Mode        = MS32_EXTI_MODE_IT;
  ExitInitStruct.Trigger     = MS32_EXTI_TRIGGER_RISING_FALLING;
  MS32_EXTI_Init(&ExitInitStruct);

  MS32_EXTI_ClearFlag_0_31(MS32_EXTI_LINE_21);  
  MS32_CMP_ITConfig(MS32_COMP_IT_ENABLE,0);                // Configure the CMP1 interrupt enable 
}

/******************************** END OF FILE *********************************/
