/* 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 adc_sample.c
 *
 * @brief This file contains the user ADC function.
 *
 */

/* ===========================================  Includes  =========================================== */
#include "adc_sample.h"
#include "ac780x_adc_reg.h"
#include "ac780x_gpio.h"
#include <string.h>

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

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

/* ==========================================  Variables  =========================================== */
uint16_t g_adc_InjectArray[3] = {0}; /*! 保存注入组通道转换结果*/

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

/* ======================================  Functions define  ======================================== */
/*!
 * @brief ADC触发数据转换测试.
 *
 * @param[in]  none
 * @return none
 */
void ADC_TriggerTest(void)
{
    ADC_SoftwareStartInjectConvert(ADC0); /*! 软件触发注入组采样*/

    while (!ADC_GetIEOCFlag(ADC0, 2)) /*!等待注入组最后一个通道转换完成*/
    {
        /* Wait */
    }

    g_adc_InjectArray[0] = ADC_GetInjectData(ADC0, 0); /*! 获取注入组0的转换值*/
    g_adc_InjectArray[1] = ADC_GetInjectData(ADC0, 1); /*! 获取注入组1的转换值*/
    g_adc_InjectArray[2] = ADC_GetInjectData(ADC0, 2); /*! 获取注入组2的转换值*/

    /*! 打印ADC各通道转换结果*/
    printf("ADC Inject value :  VR1 = 0x%-5x VR1 = 0x%-5x TSENSOR = 0x%-5x \r\n", g_adc_InjectArray[0], g_adc_InjectArray[1], g_adc_InjectArray[2]);
}

/*!
 * @brief 初始化ADC,配置ADC参数。
 *
 * @param[in] none
 * @return none
 */
void ADC_Sample_init(void)
{
    ADC_ConfigType adcConfig;                    /*! ADC初始化配置*/
    memset(&adcConfig, 0x00, sizeof(adcConfig)); /*! 清空结构体*/

    /*! ADC输入端口（电位器）配置*/
    GPIO_SetFunc(GPIOA, GPIO_PIN10, GPIO_FUN2); /*! PA10复用为FUN2，为ADC_IN1 ，对应电位器VR1*/

    /*! ADC配置
    ADC固定为12位分辨率，工作模式为MODE3（工作模式内容见参考手册8.4.2章节），规则组扫描+注入组扫描模式多通道单次转换，规则组长度为0,注入组长度为3
    每触发一次进行一次ADC转换，每次转换注入组中的3个通道。
    */
    adcConfig.clkPsc                     = ADC_CLK_PRESCALER_2;  /*! ADC时钟分频设置，Set ADC Clk = APB时钟频率/(clkPsc + 1) = 16M/2 = 8M */
    adcConfig.scanModeEn                 = ENABLE;               /*! 模式配置SCAN：扫描模式使能位 多通道需使能扫描模式，单通道则不需要使能*/
    adcConfig.continuousModeEn           = DISABLE;              /*! 模式配置CONT：连续模式使能位 0：触发一次后只转换一次，1：触发后可连续转换*/
    adcConfig.regularDiscontinuousModeEn = DISABLE;              /*! 模式配置DISCEN：打开规则组间断转换模式*/
    adcConfig.injectDiscontinuousModeEn  = DISABLE;              /*! 模式配置IDISEN：打开注入组间断转换模式*/
    adcConfig.injectAutoModeEn           = DISABLE;              /*! 模式配置IAUTO：自动注入模式*/
    adcConfig.intervalModeEn             = DISABLE;              /*! 模式配置INTERVAL：注入组为间隔转换模式*/
    adcConfig.regularDiscontinuousNum    = 0;                    /*! 规则组子组长度，用于Mode7和Mode8*/
    adcConfig.interruptEn                = DISABLE;              /*! 总中断使能*/
    adcConfig.voltageRef                 = ADC_VOLTAGEREF_VDDA;  /*! ADC参考源选择 */
    adcConfig.regularTriggerMode         = ADC_TRIGGER_INTERNAL; /*! 规则组触发源类型选择*/
    adcConfig.injectTriggerMode          = ADC_TRIGGER_INTERNAL; /*! 注入组触发源类型选择*/
    adcConfig.regularSequenceLength      = 0;                    /*! 规则组长度*/
    adcConfig.injectSequenceLength       = 3;                    /*! 注入组长度*/
    adcConfig.dataAlign                  = ADC_DATA_ALIGN_RIGHT; /*! 数据右对齐*/
    adcConfig.callBack                   = NULL;                 /*! ADC回调函数*/
    adcConfig.powerMode                  = ADC_POWER_ON;         /*! ADC上电*/
    ADC_Init(ADC0, &adcConfig);                                  /*! ADC works Mode Config*/

    /*!
        ADC转换率计算公式：
        总转换时间=(SPT+ 12)/ADC时钟频率+5个APB时钟周期
        备注：
        1.SPT为采样周期个数，具体见下面ADC_SPT_CLK_x。
        2.ADC时钟频率 = APB时钟频率 /(分频系数+1)
        3.7802x ADC支持的最高采样率为250Ksps(即转换时间不低于4us)，如果配置采样率高于该值，可能会导致采样结果精度显著减低。
    */
    /*! 注入组通道转换序列和转换速率配置*/
    ADC_SetInjectGroupChannel(ADC0, ADC_CH_1, ADC_SPT_CLK_33, 0);   /*! 采样&转换时间= (33+12)/8000000 + 5/16000000 ≈ 5.94us */
    ADC_SetInjectGroupChannel(ADC0, ADC_CH_1, ADC_SPT_CLK_33, 1);   /*! 采样&转换时间= (33+12)/8000000 + 5/16000000 ≈ 5.94us */
    ADC_SetInjectGroupChannel(ADC0, ADC_CH_VIN, ADC_SPT_CLK_33, 2); /*! 采样&转换时间= (33+12)/8000000 + 5/16000000 ≈ 5.94us */
    /*! Bandgap或T-sensor内部通道配置*/
    ADC_SetInternalChannel(ADC0, ADC_INTERNAL_TSENSOR); /*! 内部电压信号选择（仅在使用了ADC_CH_VIN 时配置） */
    ADC_SetVBufEnableFlag(ADC0, ENABLE);                /*! Bandgap和T-sensor内部通道使能（仅在使用了ADC_CH_VIN 时配置） */
}

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