/**
  ******************************************************************************
  * @file     CMP_CFG.c
  * @author   SINOMCU-AE
  * @brief    CMP config
  *    
  *          This file provides functions to CMP config:
  *   CMP2 out PA12,INP OPAMP1OUT,INN CRV 2.0V;
  * @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 "Lib_ex_CMP2.h"
#include "CMP_CFG.h"

/**
  * @brief  CMP2 Pin config
  *    PA12   --> CMP2_OUT
  * @param  None
  * @retval None
  */
void cmp_pin_init(void) {
  MS32_GPIO_InitTypeDef  GpioInitStruct;
  
  // PA12   --> CMP2_OUT
  MS32_GPIO_StructInit(&GpioInitStruct);  
  GpioInitStruct.Pin        = MS32_GPIO_PIN_12;  
  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  CMP2 config
  *   INP OPAMP1OUT,INN CRV 2.0V; 
  * @param  None
  * @retval None
  */
void cmp2_init(void) {
  MS32_CMP_InitTypeDef CmpInitStruct;
  
  cmp_pin_init();    
    
  MS32_CMP_StructInit(&CmpInitStruct);
  CmpInitStruct.HysteresisSel = MS32_COMP_HYST_0MV;
  CmpInitStruct.PositionSel   = MS32_COMP2_POS_OP1OUT;     // OPAMP1 out --> CMP2 INP,MCU internal connect,CMP2 INP option different whit CMP1;
  CmpInitStruct.NegativeSel   = MS32_COMP_NEG_CRV;         // CMP2INN internal
  CmpInitStruct.CrvSel        = MS32_COMP_CRV_VREF;        // CRV user internal VREF 2.0V
  CmpInitStruct.CrvDivSel     = MS32_COMP_CRV_8_DIV_8;     // CMP2INN internal CRV 8/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_CMP2_Init(&CmpInitStruct);                          // init function had enable syscfg clock,use Lib_ex_CMP2 function when library not patch
}

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