/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2024. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */
/******************************************************************************
 * @version: V3.1.0  AC7840x  Demo
 ******************************************************************************/

#include "acmp_sample.h"
#include "acmp_drv.h"
#include "string.h"
#include "gpio.h"

/*!
* @brief ACMP中断回调函数
*
* @param[in] none
* @return none
*/
void ACMP_Callback(const uint32_t instance, uint32_t state)
{
    if ((state & ACMP_SR_F_Msk) != 0)
    {
        LED3_TOGGLE;
    }
}

/*!
 * @brief   ACMP模块配置
 *          ACMP模块配置为普通比较模式，使能DAC，设置DAC的电压为Vref/2,将ACMP0_IN5(PC2)的输入电压与内部DAC比较，使能ACMP_OUT输出。
 * @param   none
 * @return  none
 */
void ACMP_NoramlMode_Init(void)
{
    acmp_module_t config;

    memset(&config, 0U, sizeof(config));

    /*GPIO Configuration for ACMP */
    GPIO_DRV_SetMuxModeSel(PORTC, 2, PORT_PIN_DISABLED); /*PC2: ACMP0_IN5 */
    GPIO_DRV_SetMuxModeSel(PORTE, 3, PORT_MUX_ALT7);     /*PE3: ACMP0_OUT*/

    /*比较器配置*/
    config.comparator.interruptEn       = true;                /*中断使能位*/
    config.comparator.outputTrigger     = ACMP_BOTH_EDGES;     /*ACMP输出中断触发边沿选择 */
    config.comparator.outputSelect      = ACMP_COUTA;          /*输出信号选择 ACMP_COUTA:ACMP的输出为未经过数字滤波器，ACMP_COUT:ACMP的输出经过数字滤波器*/
    config.comparator.filterEnable      = true;                /*数字滤波器使能位 */
    config.comparator.filterSampleCount = 5;                   /*数字滤波器值，（0~255）对应（1~256）个ACMP功能时钟周期*/
    config.comparator.clockDivide       = ACMP_FLT_DIVIDE_1;   /*数字滤波器时钟分频选择 （0~7）对应（1~8）倍分频*/
    config.comparator.lpfBandwidth      = ACMP_LPF_2MHZ;       /*低通滤波器带宽配置 */
    config.comparator.inverterEnable    = false;               /*ACMP输出结果反转使能位 */
    config.comparator.pinState          = true;                /*ACMP比较结果输出到外部PIN*/
    config.comparator.hysteresisMode    = ACMP_HYS_BOTH_EDGE;  /*迟滞模式选择 ACMP_HYS_BOTH_EDGE：双边沿迟滞模式，ACMP_HYS_FALLING_EDGE：下降沿迟滞模式*/
    config.comparator.hysteresisLevel   = ACMP_LEVEL_HYS_20MV; /*迟滞等级配置 */
    config.comparator.usingLSIEnable    = false;               /*ACMP功能时钟选择配置，使能时ACMP功能时钟为LSI_CLK，不使能时为BUS_CLK */
    config.comparator.windowModeEnable  = false;               /*窗口控制模式使能位*/
    config.comparator.powerEnable       = true;                /*ACMP模块上电 */

    config.mux.positiveInputMux = ACMP_EXTERNAL_CH5;    /*正输入通道选择 */
    config.mux.negativeInputMux = ACMP_DAC_OUTPUT;      /*负输入通道选择*/

    /*内部DAC配置*/
    config.dac.voltageReferenceSource = ACMP_DAC_VDD;   /*DAC参考电压选择*/
    config.dac.voltage                = 127;            /*DAC输出电压code（0~255），DAC输出电压=Vref/256*dac_code */
    config.dac.state                  = true;           /*DAC使能配置 */

    /*轮询模式配置*/
    config.pollingMode.mode = ACMP_NONE_POLLING;        /*轮询模式配置,这里不使能轮询*/

    /*以下配置项，在非轮询模式下无需配置*/
    config.pollingMode.pollingClockDivide = ACMP_CLK_DIVIDE_256;    /*轮询模式分频设置*/
    config.pollingMode.pollingSequence    = 0;                      /*轮询通道序列设置，bit[8:0]，每个bit对应一个通道*/
    config.pollingMode.hallOutputEnable   = false;                  /*HALL输出使能位*/
    config.pollingMode.hallAOutputCh      = ACMP_EXTERNAL_CH0;      /*HALLA输出通道选择*/
    config.pollingMode.hallBOutputCh      = ACMP_EXTERNAL_CH0;      /*HALLB输出通道选择*/
    config.pollingMode.hallCOutputCh      = ACMP_EXTERNAL_CH0;      /*HALLC输出通道选择*/

    ACMP_DRV_Init(0, &config);
    ACMP_DRV_SetCallback(0, &ACMP_Callback);
}
