/* 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) 2023. 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 module interrupt callback function
 * @param   wpara:Interrupt flag bit parameter
 *           lpara：not used
 * @return  none
 */
void ACMP_Callback(void *device, uint32_t wpara, uint32_t lpara)
{
    if (0 != (wpara & ACMP_SR_F5_Msk))      /* ACMP for polling channels_ IN5 interrupt */
    {
        /* user code */
    }
    else if (0 != (wpara & ACMP_SR_F6_Msk)) /* ACMP for polling channels_ IN6 interrupt occurred */
    {
        /* user code */
    }
    else
    {

    }
}

/*!
 * @brief   ACMP_PollingMode_Init
 *          The ACMP module is configured in polling comparison mode to enable DAC and set the DAC voltage to Vref/2,
 *          Polling channel set to ACMP_ IN6 (PA4), ACMP_ IN5 (PA5)

Enable ACMP output.
 * @param   none
 * @return  none
 */
void ACMP_PollingMode_Init(void)
{
    ACMP_ConfigType config;
    memset(&config, 0U, sizeof(config));

    GPIO_SetFunc(GPIOA, GPIO_PIN4, GPIO_FUN2);  /* PA4--ACMP_IN6.*/
    GPIO_SetFunc(GPIOA, GPIO_PIN5, GPIO_FUN2);  /* PA5--ACMP_IN5.*/
    GPIO_SetFunc(GPIOA, GPIO_PIN3, GPIO_FUN5);  /* PA3--DAC_OUT*/
    GPIO_SetFunc(GPIOC, GPIO_PIN12, GPIO_FUN5); /* PC12--ACMP_OUT*/

    /* Comparator configuration */
    config.positivePin = ACMP_EXTERNAL_CH6;             /* Positive input channel */
    config.negativePin = ACMP_DAC_OUTPUT;               /* Negative input channel */
    config.hysterisisVol = ACMP_HYSTERISIS_40MV;        /* Hysteresis voltage configuration */
    config.edgeType = ACMP_EDGE_TRIGGER_FALLING_RISING; /* Interrupt triggered edge selection */
    config.acmpOutEn = ENABLE;                          /* ACMP result output to external pin function enable bit */
    config.interruptEn = ENABLE;                        /* Interrupt enable bit */
    config.callback = ACMP_Callback;                    /* Interrupt callback function */
    config.acmpEn = ENABLE;                             /* ACMP module enable bit */

    /* DAC configuration */
    config.dacRef = ACMP_DAC_VDD;                       /* DAC reference source selection*/
    config.dacValue = 128;                              /* DAC output voltage value selection:0~255 */
    config.dacEn = ENABLE;                              /* DAC enable bit */
    config.dacOutEn = ENABLE;
    config.dacOutMode = DAC_OUTPUT_THROUGH_BUFFER;

    /* Polling mode configuration */
    config.pollingModeType = ACMP_POSITIVE_POLLING;     /* Polling mode configuration*/
    /* The following configuration items do not need to be configured in non polling mode */
    config.pollingSequence = 0x60;                      /* Polling channel sequence bits [32:0], each bit corresponds to a channel */
    config.pollingDiv = ACMP_CLK_DIVIDE_256;            /* Polling mode frequency division coefficient */

    ACMP_Init(ACMP0, &config);
}



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