/* 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) 2022. 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: V1.0.0 demo
 ******************************************************************************/

 
 /*!
 * @file acmp.c
 *
 * @brief This file contains the user acmp function..
 *
 */

/* ===========================================  Includes  =========================================== */
#include "acmp.h"
#include "ac780x_gpio.h"
#include "string.h"
#include "ac780x_timer.h"

/* ============================================  Define  ============================================ */

/* ===========================================  Typedef  ============================================ */


/* ==========================================  Variables  =========================================== */


/* ====================================  Functions declaration  ===================================== */


/* ======================================  Functions define  ======================================== */
/*!
 * @brief   ACMP_Callback
 *          ACMP模块中断回调函数
 * @param   wpara:中断标志位参数
 *          lpara：未使用
 * @return  none
 */
void ACMP_Callback(void *device, uint32_t wpara, uint32_t lpara)
{
    if(0 != (wpara & ACMP_SR_F0_Msk)) /*轮询通道的ACMP_INO发生中断*/
    {
        if(GPIO_LEVEL_LOW == GPIO_GetPinLevel(GPIOA, GPIO_PIN2))
        {
            GPIO_SetPinLevel(GPIOA, GPIO_PIN2, GPIO_LEVEL_HIGH);
        }
        else
        {
            GPIO_SetPinLevel(GPIOA, GPIO_PIN2, GPIO_LEVEL_LOW);
        }
    } 
    else if(0 != (wpara & ACMP_SR_F1_Msk)) /*轮询通道的ACMP_IN1发生中断*/
    {
        if(GPIO_LEVEL_LOW == GPIO_GetPinLevel(GPIOA, GPIO_PIN3))
        {
            GPIO_SetPinLevel(GPIOA, GPIO_PIN3, GPIO_LEVEL_HIGH);
        }
        else
        {
            GPIO_SetPinLevel(GPIOA, GPIO_PIN3, GPIO_LEVEL_LOW);
        }
    }
    else
    {}
}

/*!
 * @brief   ACMP_PollingMode_Init
 *          ACMP模块配置为轮询比较模式，使能DAC，设置DAC电压为Vref/2，
 *          轮询通道设置为ACMP_IN1 (PA10)，ACMP_IN0(PA11)
 *          使能ACMP输出。
 * @param   none
 * @return  none
 */
void ACMP_PollingMode_Init(void)
{
    ACMP_ConfigType config;
    memset(&config, 0U, sizeof(config));
    
    GPIO_SetFunc(GPIOA, GPIO_PIN10, GPIO_FUN2); /* PA10--ACMP_IN1.*/
    GPIO_SetFunc(GPIOA, GPIO_PIN11, GPIO_FUN2); /* PA11--ACMP_IN0.*/
    GPIO_SetFunc(GPIOB, GPIO_PIN1, GPIO_FUN3);  /* PB1--DAC_OUT,该引脚只在32pin封装上有.*/
    GPIO_SetFunc(GPIOA, GPIO_PIN14, GPIO_FUN2); /* PA14--ACMP_OUT,该引脚默认功能为SWD_DIO，因此使用时必须在上电后预留时间，给仿真器连接.*/
    
    /* 比较器配置 */
    config.positivePin = ACMP_EXTERNAL_CH1;             /* 正输入通道 */
    config.negativePin = ACMP_DAC_OUTPUT;               /* 负输入通道 */
    config.hysterisisVol = ACMP_HYSTERISIS_40MV;        /* 迟滞电压配置 */
    config.edgeType = ACMP_EDGE_TRIGGER_FALLING_RISING; /* 中断触发边沿选择 */
    config.acmpOutEn = ENABLE;                          /* ACMP结果输出至外部引脚功能使能位 */
    config.interruptEn = ENABLE;                        /* 中断使能位 */
    config.callBack = ACMP_Callback;                    /* 中断回调函数 */
    config.acmpEn = ENABLE;                             /* ACMP模块使能位 */
    
    /* DAC 配置 */
    config.dacRef = ACMP_DAC_VDD;                       /* DAC参考源选择*/
    config.dacValue = 32;                               /* DAC输出电压值选择，0~63 */
    config.dacEn = ENABLE;                              /* DAC使能位 */
    
    /* 轮询模式配置 */
    config.pollingModeType = ACMP_POSITIVE_POLLING;     /* 轮询模式配置*/
    /*以下配置项，在非轮询模式下无需配置*/
    config.pollingSequence = 0x03;                      /* 轮询通道序列 bit[8:0],每个bit对应一个通道 */
    config.pollingDiv = ACMP_CLK_DIVIDE_34;             /* 轮询模式分频系数 */
    config.hallAOutputCh = ACMP_EXTERNAL_CH0;           /* HALLA输出通道选择 */
    config.hallBOutputCh = ACMP_EXTERNAL_CH0;           /* HallB 输出通道选择 */
    config.hallCOutputCh = ACMP_EXTERNAL_CH0;           /* HallC 输出通道选择 */
    config.hallOutEn = DISABLE;                         /* HALL输出功能使能位 */
    
    ACMP_Init(ACMP0,&config);
}



/* =============================================  EOF  ============================================== */
