/* 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.
 */

#ifndef _AC780X_ADC_REG_H
#define _AC780X_ADC_REG_H
/*!
 * @file ac780x_adc_reg.h
 *
 * @brief Analog to digital converter module access register inline function definition.
 *
 */
#ifdef __cplusplus
extern "C" {
#endif

/* ===========================================  Includes  =========================================== */
#include "ac780x_adc.h"

/* ============================================  Define  ============================================ */
#define ADC_EACH_REG_SPT_NUM    (10UL)
#define ADC_EACH_REG_RSQ_NUM    (6UL)
#define ADC_RSQR0_RSQ_NUM       (16UL)
#define ADC_RSQR1_RSQ_NUM       (12UL)
#define ADC_RSQR2_RSQ_NUM       (6UL)
#define ADC_RSQR3_RSQ_NUM       (21UL)

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

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

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

/* ======================================  Functions define  ======================================== */
/*!
 * @brief Get adc power mode setting.
 *
 * @param[in] ADCx: adc module
 *              - ADC0
 * @return power mode
 *              - ADC_POWER_ON
 *              - ADC_POWER_DOWN
 */
__STATIC_INLINE ADC_PowerModeType ADC_GetPowerMode(const ADC_Type *ADCx)
{
    return (ADC_PowerModeType)(READ_BIT32(ADCx->CTRL1, ADC_CTRL1_ADON_Msk) >> ADC_CTRL1_ADON_Pos);
}

/*!
 * @brief Set adc power mode.
 *
 * @param[in] ADCx: adc module
 *               - ADC0
 * @param[in] mode: power mode
 *               - ADC_POWER_ON
 *               - ADC_POWER_DOWN
 * @return none
 */
__STATIC_INLINE void ADC_SetPowerMode(ADC_Type *ADCx, ADC_PowerModeType mode)
{
    MODIFY_REG32(ADCx->CTRL1, ADC_CTRL1_ADON_Msk, ADC_CTRL1_ADON_Pos, mode);
}

/*!
 * @brief Get adc clock prescaler setting.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @return prescaler value
                - ADC_CLK_PRESCALER_1
                - ADC_CLK_PRESCALER_2
                - ADC_CLK_PRESCALER_3
                - ADC_CLK_PRESCALER_4
                - ADC_CLK_PRESCALER_5
                - ADC_CLK_PRESCALER_6
                - ADC_CLK_PRESCALER_7
                - ADC_CLK_PRESCALER_8
                - ADC_CLK_PRESCALER_9
                - ADC_CLK_PRESCALER_10
                - ADC_CLK_PRESCALER_11
                - ADC_CLK_PRESCALER_12
                - ADC_CLK_PRESCALER_13
                - ADC_CLK_PRESCALER_14
                - ADC_CLK_PRESCALER_15
                - ADC_CLK_PRESCALER_16
 */
__STATIC_INLINE ADC_ClkPrescalerType ADC_GetClockPrescaler(const ADC_Type *ADCx)
{
    return (ADC_ClkPrescalerType)(READ_BIT32(ADCx->CTRL1, ADC_CTRL1_PSC_Msk) >> ADC_CTRL1_PSC_Pos);
}

/*!
* @brief Set adc clock prescaler.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] psc: prescaler value
                - ADC_CLK_PRESCALER_1
                - ADC_CLK_PRESCALER_2
                - ADC_CLK_PRESCALER_3
                - ADC_CLK_PRESCALER_4
                - ADC_CLK_PRESCALER_5
                - ADC_CLK_PRESCALER_6
                - ADC_CLK_PRESCALER_7
                - ADC_CLK_PRESCALER_8
                - ADC_CLK_PRESCALER_9
                - ADC_CLK_PRESCALER_10
                - ADC_CLK_PRESCALER_11
                - ADC_CLK_PRESCALER_12
                - ADC_CLK_PRESCALER_13
                - ADC_CLK_PRESCALER_14
                - ADC_CLK_PRESCALER_15
                - ADC_CLK_PRESCALER_16
* @return none
*/
__STATIC_INLINE void ADC_SetClockPrescaler(ADC_Type *ADCx, ADC_ClkPrescalerType psc)
{
    MODIFY_REG32(ADCx->CTRL1, ADC_CTRL1_PSC_Msk, ADC_CTRL1_PSC_Pos, psc);
}

/*!
 * @brief Get adc channel sample time setting.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @param[in] channel: adc channel
                - ADC_CH_0
                - ADC_CH_1
                - ADC_CH_2
                - ADC_CH_3
                - ADC_CH_4
                - ADC_CH_5
                - ADC_CH_6
                - ADC_CH_7
                - ADC_CH_8
                - ADC_CH_9
                - ADC_CH_10
                - ADC_CH_11
                - ADC_CH_12
                - ADC_CH_13
                - ADC_CH_14
                - ADC_CH_15
                - ADC_CH_16
                - ADC_CH_18
                - ADC_CH_VIN
 * @return sample time setting
                - ADC_SPT_CLK_9
                - ADC_SPT_CLK_7
                - ADC_SPT_CLK_17
                - ADC_SPT_CLK_33
                - ADC_SPT_CLK_64
                - ADC_SPT_CLK_140
                - ADC_SPT_CLK_215
                - ADC_SPT_CLK_5
 */
__STATIC_INLINE ADC_SampleTimeType ADC_GetChannelSampleTime(const ADC_Type *ADCx, ADC_ChannelType channel)
{
    const uint32_t spt1_num = ADC_EACH_REG_SPT_NUM;
    const uint32_t spt0_num = 2 * ADC_EACH_REG_SPT_NUM;
    ADC_SampleTimeType spt = ADC_SPT_CLK_9;
    uint32_t ch = (uint32_t)channel;

    if (ch < spt1_num)
    {
        spt = (ADC_SampleTimeType)(READ_BIT32(ADCx->SPT1, ADC_SPT_Msk << (ch * ADC_SPT_Width)) >> (ch * ADC_SPT_Width));
    }
    else if (ch < spt0_num)
    {
        ch -= spt1_num;
        spt = (ADC_SampleTimeType)(READ_BIT32(ADCx->SPT0, ADC_SPT_Msk << (ch * ADC_SPT_Width)) >> (ch * ADC_SPT_Width));
    }
    else
    {
        /*!< Nothing to do */
    }

    return spt;
}

/*!
* @brief Set channel sample time.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] channel: adc channel
                - ADC_CH_0
                - ADC_CH_1
                - ADC_CH_2
                - ADC_CH_3
                - ADC_CH_4
                - ADC_CH_5
                - ADC_CH_6
                - ADC_CH_7
                - ADC_CH_8
                - ADC_CH_9
                - ADC_CH_10
                - ADC_CH_11
                - ADC_CH_12
                - ADC_CH_13
                - ADC_CH_14
                - ADC_CH_15
                - ADC_CH_16
                - ADC_CH_18
                - ADC_CH_VIN
* @param[in] time: sample time
                - ADC_SPT_CLK_9
                - ADC_SPT_CLK_7
                - ADC_SPT_CLK_17
                - ADC_SPT_CLK_33
                - ADC_SPT_CLK_64
                - ADC_SPT_CLK_140
                - ADC_SPT_CLK_215
                - ADC_SPT_CLK_5
* @return none
*/
__STATIC_INLINE void ADC_SetChannelSampleTime(ADC_Type *ADCx, ADC_ChannelType channel, ADC_SampleTimeType time)
{
    const uint32_t spt1_num = ADC_EACH_REG_SPT_NUM;
    const uint32_t spt0_num = 2 * ADC_EACH_REG_SPT_NUM;
    uint32_t ch = (uint32_t)channel;

    if (ch < spt1_num)
    {
        MODIFY_REG32(ADCx->SPT1, (ADC_SPT_Msk << (ch * ADC_SPT_Width)), (ch * ADC_SPT_Width), time);
    }
    else if (ch < spt0_num)
    {
        ch -= spt1_num;
        MODIFY_REG32(ADCx->SPT0, (ADC_SPT_Msk << (ch * ADC_SPT_Width)), (ch * ADC_SPT_Width), time);
    }
    else
    {
        /*!< Nothing to do */
    }
}

/*!
 * @brief Get adc data alignment setting.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @return alignment format setting
                - ADC_DATA_ALIGN_RIGHT
                - ADC_DATA_ALIGN_LEFT
 */
__STATIC_INLINE ADC_DataAlignType ADC_GetDataAlign(const ADC_Type *ADCx)
{
    return (ADC_DataAlignType)(READ_BIT32(ADCx->CTRL0, ADC_CTRL0_ALIGN_Msk) >> ADC_CTRL0_ALIGN_Pos);
}

/*!
* @brief Set adc data alignment format.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] align: alignment format
                - ADC_DATA_ALIGN_RIGHT
                - ADC_DATA_ALIGN_LEFT
* @return none
*/
__STATIC_INLINE void ADC_SetDataAlign(ADC_Type *ADCx, ADC_DataAlignType align)
{
    MODIFY_REG32(ADCx->CTRL0, ADC_CTRL0_ALIGN_Msk, ADC_CTRL0_ALIGN_Pos, align);
}

/*!
 * @brief Get adc scan convert mode setting.
 *
 * @param[in] ADCx: DC0
 * @return scan convert mode enabling state
                - ENABLE
                - DISABLE
 */
__STATIC_INLINE ACTION_Type ADC_GetScanMode(const ADC_Type *ADCx)
{
    return (ACTION_Type)(READ_BIT32(ADCx->CTRL0, ADC_CTRL0_SCAN_Msk) >> ADC_CTRL0_SCAN_Pos);
}

/*!
* @brief Set scan convert mode.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] state: enabling state
                - ENABLE
                - DISABLE
* @return none
*/
__STATIC_INLINE void ADC_SetScanMode(ADC_Type *ADCx, ACTION_Type state)
{
    MODIFY_REG32(ADCx->CTRL0, ADC_CTRL0_SCAN_Msk, ADC_CTRL0_SCAN_Pos, state);
}

/*!
 * @brief Get adc continuous convert mode.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @return continuous convert mode enabling state
                - ENABLE
                - DISABLE
 */
__STATIC_INLINE ACTION_Type ADC_GetContinuousMode(const ADC_Type *ADCx)
{
    return (ACTION_Type)(READ_BIT32(ADCx->CTRL0, ADC_CTRL0_CONT_Msk) >> ADC_CTRL0_CONT_Pos);
}

/*!
* @brief Set continuous convert mode.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] state: enabling state
                - ENABLE
                - DISABLE
* @return none
*/
__STATIC_INLINE void ADC_SetContinuousMode(ADC_Type *ADCx, ACTION_Type state)
{
    MODIFY_REG32(ADCx->CTRL0, ADC_CTRL0_CONT_Msk, ADC_CTRL0_CONT_Pos, state);
}

/*!
 * @brief Get ADC mode 7 regular discontinuous number setting.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @return mode 7 regular discontinuous number setting.
 */
__STATIC_INLINE uint8_t ADC_GetRegularDiscontinuousNum(const ADC_Type* ADCx)
{
    return (uint8_t)(READ_BIT32(ADCx->CTRL0, ADC_CTRL0_DISCNUM_Msk) >> ADC_CTRL0_DISCNUM_Pos);
}

/*!
* @brief Set discontinuous mode number for regular group.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] num: sub group number
                - 0~7
* @return none
*/
__STATIC_INLINE void ADC_SetRegularDiscontinuousNum(ADC_Type* ADCx, uint8_t num)
{
    MODIFY_REG32(ADCx->CTRL0, ADC_CTRL0_DISCNUM_Msk, ADC_CTRL0_DISCNUM_Pos, num);
}

/*!
 * @brief Get adc discontinuous mode setting for regular group.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @return regular group discontinuous mode enabling state
                - ENABLE
                - DISABLE
 */
__STATIC_INLINE ACTION_Type ADC_GetRegularDiscontinuousMode(const ADC_Type *ADCx)
{
    return (ACTION_Type)(READ_BIT32(ADCx->CTRL0, ADC_CTRL0_DISCEN_Msk) >> ADC_CTRL0_DISCEN_Pos);
}

/*!
* @brief Set discontinuous mode for regular group.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] state: enabling state
                - ENABLE
                - DISABLE
* @return none
*/
__STATIC_INLINE void ADC_SetRegularDiscontinuousMode(ADC_Type *ADCx, ACTION_Type state)
{
    MODIFY_REG32(ADCx->CTRL0, ADC_CTRL0_DISCEN_Msk, ADC_CTRL0_DISCEN_Pos, state);
}

/*!
* @brief Start software trigger for regular group convert.
*
* @param[in] ADCx: adc module
                - ADC0
* @return none
*/
__STATIC_INLINE void ADC_SoftwareStartRegularConvert(ADC_Type *ADCx)
{
    SET_BIT32(ADCx->CTRL0, ADC_CTRL0_SWSTART_Msk);
}

/*!
 * @brief Get regular group trigger source.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @return regular group trigger source setting
                - ADC_TRIGGER_EXTERNAL
                - ADC_TRIGGER_INTERNAL
 */
__STATIC_INLINE ADC_TriggerConvertType ADC_GetRegularTriggerSource(const ADC_Type *ADCx)
{
    return (ADC_TriggerConvertType)(READ_BIT32(ADCx->CTRL0, ADC_CTRL0_EXTTRIG_Msk) >> ADC_CTRL0_EXTTRIG_Pos);
}

/*!
* @brief Set regular group trigger source.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] source: trigger source
                - ADC_TRIGGER_EXTERNAL
                - ADC_TRIGGER_INTERNAL
* @return none
*/
__STATIC_INLINE void ADC_SetRegularTriggerSource(ADC_Type *ADCx, ADC_TriggerConvertType source)
{
    MODIFY_REG32(ADCx->CTRL0, ADC_CTRL0_EXTTRIG_Msk, ADC_CTRL0_EXTTRIG_Pos, source);
}

/*!
 * @brief Get ADC EOC interrupt enable setting.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @param[in] seq: regular group sequence number
 * @return regular group EOC interrupt enabling state
                - ENABLE
                - DISABLE
 */
__STATIC_INLINE ACTION_Type ADC_GetEOCInterrupt(const ADC_Type *ADCx, uint8_t seq)
{
    return (ACTION_Type)(READ_BIT32(ADCx->REOCEN, 1U << seq) >> seq);
}

/*!
 * @brief Set ADC EOC interrupt enable setting.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @param[in] seq: regular group sequence number
 * @param[in] state: regular group EOC interrupt enabling state
                - ENABLE
                - DISABLE
 */
__STATIC_INLINE void ADC_SetEOCInterrupt(ADC_Type *ADCx, uint8_t seq, ACTION_Type state)
{
    if (state)
    {
        SET_BIT32(ADCx->REOCEN, 1U << seq);
    }
    else
    {
        CLEAR_BIT32(ADCx->REOCEN, 1U << seq);
    }
}

/*!
 * @brief Get ADC regualr gropu length setting.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @return regular group length setting.
 */
__STATIC_INLINE uint8_t ADC_GetRegularLength(const ADC_Type *ADCx)
{
    return (uint8_t)(READ_BIT32(ADCx->RSQR0, ADC_RSQR0_RSQL_Msk) >> ADC_RSQR0_RSQL_Pos);
}

/*!
* @brief Set regular group length.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] length: regular group convert length
                - 0 ~ ADC_REGULAR_SEQUENCE_LEGNTH-1
* @return none
*/
__STATIC_INLINE void ADC_SetRegularLength(ADC_Type *ADCx, uint8_t length)
{
    MODIFY_REG32(ADCx->RSQR0, ADC_RSQR0_RSQL_Msk, ADC_RSQR0_RSQL_Pos, length);
}

/*!
 * @brief Get ADC regular channel setting for the sequence number.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @param[in] seq: regular group sequence
 * @return convert channel setting for the specific regular group sequence
 */
__STATIC_INLINE ADC_ChannelType ADC_GetRegularSequence(const ADC_Type *ADCx, uint8_t seq)
{
    ADC_ChannelType channel = ADC_CH_MAX;

    if (seq < ADC_RSQR2_RSQ_NUM)
    {
        channel = (ADC_ChannelType)(READ_BIT32(ADCx->RSQR2, ADC_SQ_Msk << (seq * ADC_SQ_Width)) >> (seq * ADC_SQ_Width));
    } else if (seq < ADC_RSQR1_RSQ_NUM)
    {
        seq -= ADC_RSQR2_RSQ_NUM;
        channel = (ADC_ChannelType)(READ_BIT32(ADCx->RSQR1, ADC_SQ_Msk << (seq * ADC_SQ_Width)) >> (seq * ADC_SQ_Width));
    }
    else if (seq < ADC_RSQR0_RSQ_NUM)
    {
        seq -= ADC_RSQR1_RSQ_NUM;
        channel = (ADC_ChannelType)(READ_BIT32(ADCx->RSQR0, ADC_SQ_Msk << (seq * ADC_SQ_Width)) >> (seq * ADC_SQ_Width));
    }
    else if (seq < ADC_RSQR3_RSQ_NUM)
    {
        seq -= ADC_RSQR0_RSQ_NUM;
        channel = (ADC_ChannelType)(READ_BIT32(ADCx->RSQR3, ADC_SQ_Msk << (seq * ADC_SQ_Width)) >> (seq * ADC_SQ_Width));
    }
    else
    {
        /*!< Nothing to do */
    }

    return channel;
}

/*!
* @brief Set regular group sequence.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] channel: need to covert regular group channel
                - ADC_CH_0
                - ADC_CH_1
                ...
                - ADC_CH_TSENSOR
* @param[in] seq: regular group sequence
                - 0 ~ 11
* @return none
*/
__STATIC_INLINE void ADC_SetRegularSequence(ADC_Type *ADCx, ADC_ChannelType channel, uint8_t seq)
{
    if (seq < ADC_RSQR2_RSQ_NUM)
    {
        MODIFY_REG32(ADCx->RSQR2, (ADC_SQ_Msk << (seq * ADC_SQ_Width)), (seq * ADC_SQ_Width), channel);
    } else if (seq < ADC_RSQR1_RSQ_NUM)
    {
        seq -= ADC_RSQR2_RSQ_NUM;
        MODIFY_REG32(ADCx->RSQR1, (ADC_SQ_Msk << (seq * ADC_SQ_Width)), (seq * ADC_SQ_Width), channel);
    }
    else if (seq < ADC_RSQR0_RSQ_NUM)
    {
        seq -= ADC_RSQR1_RSQ_NUM;
        MODIFY_REG32(ADCx->RSQR0, (ADC_SQ_Msk << (seq * ADC_SQ_Width)), (seq * ADC_SQ_Width), channel);
    }
    else if (seq < ADC_RSQR3_RSQ_NUM)
    {
        seq -= ADC_RSQR0_RSQ_NUM;
        MODIFY_REG32(ADCx->RSQR3, (ADC_SQ_Msk << (seq * ADC_SQ_Width)), (seq * ADC_SQ_Width), channel);
    }
    else
    {
        /*!< Nothing to do */
    }
}

/*!
 * @brief Get regular group data.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @param[in] seq: regualr group sequence number.
 * @return regular group data
 */
__STATIC_INLINE uint16_t ADC_GetRegularData(const ADC_Type *ADCx, uint8_t seq)
{
    return ADCx->RDR[seq];
}

/*!
* @brief Start software trigger for injected group convert.
*
* @param[in] ADCx: adc module
                - ADC0
* @return none
*/
__STATIC_INLINE void ADC_SoftwareStartInjectConvert(ADC_Type *ADCx)
{
    SET_BIT32(ADCx->CTRL0, ADC_CTRL0_ISWSTART_Msk);
}

/*!
 * @brief Get injection trigger source setting.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @return injection group trigger souce type setting
                - ADC_TRIGGER_EXTERNAL
                - ADC_TRIGGER_INTERNAL
 */
__STATIC_INLINE ADC_TriggerConvertType ADC_GetInjectTriggerSource(const ADC_Type *ADCx)
{
    return (ADC_TriggerConvertType)(READ_BIT32(ADCx->CTRL0, ADC_CTRL0_IEXTTRIG_Msk) >> ADC_CTRL0_IEXTTRIG_Pos);
}

/*!
* @brief Set injected group trigger source.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] source: trigger source
                - ADC_TRIGGER_EXTERNAL
                - ADC_TRIGGER_INTERNAL
* @return none
*/
__STATIC_INLINE void ADC_SetInjectTriggerSource(ADC_Type *ADCx, ADC_TriggerConvertType source)
{
    MODIFY_REG32(ADCx->CTRL0, ADC_CTRL0_IEXTTRIG_Msk, ADC_CTRL0_IEXTTRIG_Pos, source);
}

/*!
 * @brief Get injection group IEOC interrupt enable flag setting.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @param[in] seq: injection group sequence number
 * @return injection group IEOC interrupt enabling state
                - ENABLE
                - DISABLE
 */
__STATIC_INLINE ACTION_Type ADC_GetIEOCInterrupt(const ADC_Type *ADCx, uint8_t seq)
{
    return (ACTION_Type)(READ_BIT32(ADCx->IEOCEN, 1U << seq) >> seq);
}

/*!
 * @brief Set injection group IEOC interrupt enable flag setting.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @param[in] seq: injection group sequence number
 * @param[in] state: injection group IEOC interrupt enabling state
                - ENABLE
                - DISABLE
 */
__STATIC_INLINE void ADC_SetIEOCInterrupt(ADC_Type *ADCx, uint8_t seq, ACTION_Type state)
{
    if (state)
    {
        SET_BIT32(ADCx->IEOCEN, 1U << seq);
    }
    else
    {
        CLEAR_BIT32(ADCx->IEOCEN, 1U << seq);
    }
}

/*!
 * @brief Get injected group length.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @return injected group convert length
                - 0 ~ ADC_INJECT_SEQUENCE_LEGNTH-1
 */
__STATIC_INLINE uint8_t ADC_GetInjectLength(const ADC_Type *ADCx)
{
    return (uint8_t)(READ_BIT32(ADCx->ISQR, ADC_ISQR_ISQL_Msk) >> ADC_ISQR_ISQL_Pos);
}

/*!
* @brief Set injected group length.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] length: injected group convert length
                - 0 ~ ADC_INJECT_SEQUENCE_LEGNTH-1
* @return none
*/
__STATIC_INLINE void ADC_SetInjectLength(ADC_Type *ADCx, uint8_t length)
{
    MODIFY_REG32(ADCx->ISQR, ADC_ISQR_ISQL_Msk, ADC_ISQR_ISQL_Pos, length);
}

/*!
 * @brief Get injection group sequence channel setting.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @param[in] seq: injection group sequence number
 * @return corresponding channel that injection group sequence will convert.
                - ADC_CH_0
                - ADC_CH_1
                ...
 */
__STATIC_INLINE ADC_ChannelType ADC_GetInjectSequence(const ADC_Type *ADCx, uint8_t seq)
{
    return (ADC_ChannelType)(READ_BIT32(ADCx->ISQR, ADC_SQ_Msk << (seq * ADC_SQ_Width)) >> (seq * ADC_SQ_Width));
}

/*!
* @brief Set injected group sequence.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] channel: need to covert regular group channel
                - ADC_CH_0
                - ADC_CH_1
                ...
* @param[in] seq: injected group sequence
                - 0 ~ 3
* @return none
*/
__STATIC_INLINE void ADC_SetInjectSequence(ADC_Type *ADCx, ADC_ChannelType channel, uint8_t seq)
{
    MODIFY_REG32(ADCx->ISQR, (ADC_SQ_Msk << (seq * ADC_SQ_Width)), (seq * ADC_SQ_Width), channel);
}

/*!
 * @brief Get injected group offset value.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @param[in] num: injected group channel number
                - 0 ~ 3
 * @return Offset Value
                - 0 ~ 0xfff
 */
__STATIC_INLINE uint32_t ADC_GetInjectOffset(const ADC_Type *ADCx, uint8_t num)
{
    return READ_BIT32(ADCx->IOFR[num], ADC_IOFR_IOFR_Msk) >> ADC_IOFR_IOFR_Pos;
}

/*!
* @brief Set injected group offset value.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] num: injected group channel number
                - 0 ~ 3
* @param[in] offset: Offset Value
                - 0 ~ 0xfff
* @return none
*/
__STATIC_INLINE void ADC_SetInjectOffset(ADC_Type *ADCx, uint8_t num, uint32_t offset)
{
    MODIFY_REG32(ADCx->IOFR[num], ADC_IOFR_IOFR_Msk, ADC_IOFR_IOFR_Pos, offset);
}

/*!
* @brief Get injected group data.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] num: injected group channel number
                - 0 ~ 3
* @return injected data
*/
__STATIC_INLINE uint16_t ADC_GetInjectData(const ADC_Type *ADCx, uint8_t num)
{
    return (uint16_t)(READ_BIT32(ADCx->IDR[num], ADC_IDR_IDR_Msk) >> ADC_IDR_IDR_Pos);
}

/*!
 * @brief Get ADC inject auto mode enable flag.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @return inject auto mode setting
 */
__STATIC_INLINE ACTION_Type ADC_GetInjectAutoMode(const ADC_Type *ADCx)
{
    return (ACTION_Type)(READ_BIT32(ADCx->CTRL0, ADC_CTRL0_IAUTO_Msk) >> ADC_CTRL0_IAUTO_Pos);
}

/*!
* @brief Set ADC automatic injected group convert after regular complete.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] state: enabling state
                - ENABLE
                - DISABLE
* @return none
*/
__STATIC_INLINE void ADC_SetInjectAutoMode(ADC_Type *ADCx, ACTION_Type state)
{
    MODIFY_REG32(ADCx->CTRL0, ADC_CTRL0_IAUTO_Msk, ADC_CTRL0_IAUTO_Pos, state);
}

/*!
 * @brief Get ADC interval mode setting.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @return injection group interval mode setting.
 */
__STATIC_INLINE ACTION_Type ADC_GetIntervalMode(const ADC_Type *ADCx)
{
    return (ACTION_Type)(READ_BIT32(ADCx->CTRL0, ADC_CTRL0_INTERVAL_Msk) >> ADC_CTRL0_INTERVAL_Pos);
}

/*!
* @brief Set ADC interval mode.
*
* Can only be configured in mode3/5, other modes do not work
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] state: enabling state
                - ENABLE
                - DISABLE
* @return none
*/
__STATIC_INLINE void ADC_SetIntervalMode(ADC_Type *ADCx, ACTION_Type state)
{
    MODIFY_REG32(ADCx->CTRL0, ADC_CTRL0_INTERVAL_Msk, ADC_CTRL0_INTERVAL_Pos, state);
}
/*!
 * @brief Get ADC injection group discontinuous mode setting.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @return injection group discontinuous mode setting.
 */
__STATIC_INLINE ACTION_Type ADC_GetInjectDiscontinuousMode(const ADC_Type *ADCx)
{
    return (ACTION_Type)(READ_BIT32(ADCx->CTRL0, ADC_CTRL0_IDISCEN_Msk) >> ADC_CTRL0_IDISCEN_Pos);
}

/*!
* @brief Set discontinuous mode for injected group.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] state: enabling state
                - ENABLE
                - DISABLE
* @return none
*/
__STATIC_INLINE void ADC_SetInjectDiscontinuousMode(ADC_Type *ADCx, ACTION_Type state)
{
    MODIFY_REG32(ADCx->CTRL0, ADC_CTRL0_IDISCEN_Msk, ADC_CTRL0_IDISCEN_Pos, state);
}

/*!
 * @brief Get AMO Interrupt.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @return AMO interrupt enabling state
                - ENABLE
                - DISABLE
 */
__STATIC_INLINE ACTION_Type ADC_GetAMOInterrupt(const ADC_Type *ADCx)
{
    return (ACTION_Type)(READ_BIT32(ADCx->CTRL0, ADC_CTRL0_AMOIE_Msk) >> ADC_CTRL0_AMOIE_Pos);
}

/*!
* @brief Set AMO Interrupt.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] state: enabling state
                - ENABLE
                - DISABLE
* @return none
*/
__STATIC_INLINE void ADC_SetAMOInterrupt(ADC_Type *ADCx, ACTION_Type state)
{
    MODIFY_REG32(ADCx->CTRL0, ADC_CTRL0_AMOIE_Msk, ADC_CTRL0_AMOIE_Pos, state);
}

/*!
 * @brief Get analog monitor single channel.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @return analog monitor channel
                - ADC_CH_0
                - ADC_CH_1
                ...
 */
__STATIC_INLINE ADC_ChannelType ADC_GetAMOSingleChannel(const ADC_Type *ADCx)
{
    return (ADC_ChannelType)(READ_BIT32(ADCx->CTRL0, ADC_CTRL0_AMOCH_Msk) >> ADC_CTRL0_AMOCH_Pos);
}

/*!
* @brief Set analog monitor single channel.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] channel: analog monitor channel
                - ADC_CH_0
                - ADC_CH_1
                ...
* @return none
*/
__STATIC_INLINE void ADC_SetAMOSingleChannel(ADC_Type *ADCx, ADC_ChannelType channel)
{
    MODIFY_REG32(ADCx->CTRL0, ADC_CTRL0_AMOCH_Msk, ADC_CTRL0_AMOCH_Pos, channel);
}

/*!
 * @brief Get analog monitor single channel mode.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @return AMO single channel monitor enabling state
                - ENABLE
                - DISABLE
 */
__STATIC_INLINE ACTION_Type ADC_GetAMOSingleChannelMode(const ADC_Type *ADCx)
{
    return (ACTION_Type)(READ_BIT32(ADCx->CTRL0, ADC_CTRL0_AMOSGL_Msk) >> ADC_CTRL0_AMOSGL_Pos);
}

/*!
* @brief Set analog monitor single channel mode.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] state: enabling state
                - ENABLE
                - DISABLE
* @return none
*/
__STATIC_INLINE void ADC_SetAMOSingleChannelMode(ADC_Type *ADCx, ACTION_Type state)
{
    MODIFY_REG32(ADCx->CTRL0, ADC_CTRL0_AMOSGL_Msk, ADC_CTRL0_AMOSGL_Pos, state);
}

/*!
 * @brief Get regular group analog monitor mode.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @return AMO regular group monitor enabling state
                - ENABLE
                - DISABLE
 */
__STATIC_INLINE ACTION_Type ADC_GetAMORegularMode(const ADC_Type *ADCx)
{
    return (ACTION_Type)(READ_BIT32(ADCx->CTRL0, ADC_CTRL0_AMOEN_Msk) >> ADC_CTRL0_AMOEN_Pos);
}

/*!
* @brief Set regular group analog monitor mode.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] state: enabling state
                - ENABLE
                - DISABLE
* @return none
*/
__STATIC_INLINE void ADC_SetAMORegularMode(ADC_Type *ADCx, ACTION_Type state)
{
    MODIFY_REG32(ADCx->CTRL0, ADC_CTRL0_AMOEN_Msk, ADC_CTRL0_AMOEN_Pos, state);
}
/*!
 * @brief Get injected group analog monitor mode.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @return AMO injection group monitor enabling state
                - ENABLE
                - DISABLE
 */
__STATIC_INLINE ACTION_Type ADC_GetAMOInjectMode(const ADC_Type *ADCx)
{
    return (ACTION_Type)(READ_BIT32(ADCx->CTRL0, ADC_CTRL0_IAMOEN_Msk) >> ADC_CTRL0_IAMOEN_Pos);
}

/*!
* @brief Set injected group analog monitor mode.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] state: enabling state
                - ENABLE
                - DISABLE
* @return none
*/
__STATIC_INLINE void ADC_SetAMOInjectMode(ADC_Type *ADCx, ACTION_Type state)
{
    MODIFY_REG32(ADCx->CTRL0, ADC_CTRL0_IAMOEN_Msk, ADC_CTRL0_IAMOEN_Pos, state);
}

/*!
 * @brief Get ADC Analog monitor trigger mode setting.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @param[in] mode: AMO trigger mode.
 */
__STATIC_INLINE ADC_AMOTriggerModeType ADC_GetAMOTriggerMode(const ADC_Type *ADCx)
{
    return (ADC_AMOTriggerModeType)(READ_BIT32(ADCx->CTRL0, ADC_CTRL0_AMOMODE_Msk) >> ADC_CTRL0_AMOMODE_Pos);
}

/*!
* @brief Set analog monitor trigger type.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] mode: amo trigger mode
                - ADC_AMO_TRIGGER_LEVEL
                - ADC_AMO_TRIGGER_EDGE
* @return none
*/
__STATIC_INLINE void ADC_SetAMOTriggerMode(ADC_Type *ADCx, ADC_AMOTriggerModeType mode)
{
    MODIFY_REG32(ADCx->CTRL0, ADC_CTRL0_AMOMODE_Msk, ADC_CTRL0_AMOMODE_Pos, mode);
}

/*!
 * @brief Get analog monitor threshold.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @param[out] highValue: returned high threshold value
                - 0~0xfff
 * @param[out] lowValue: returned low threshold value
                - 0~0xfff
 */
__STATIC_INLINE void ADC_GetAMOThreshold(const ADC_Type *ADCx, uint16_t *highValue, uint16_t *lowValue)
{
    *highValue = (uint16_t)(READ_BIT32(ADCx->AMOHR, ADC_AMOHR_AMOHT_Msk) >> ADC_AMOHR_AMOHT_Pos);
    *lowValue = (uint16_t)(READ_BIT32(ADCx->AMOLR, ADC_AMOLR_AMOLT_Msk) >> ADC_AMOLR_AMOLT_Pos);
}

/*!
* @brief Set analog monitor threshold.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] highValue: high threshold value
                - 0~0xfff
* @param[in] lowValue: low threshold value
                - 0~0xfff
* @return none
*/
__STATIC_INLINE void ADC_SetAMOThreshold(ADC_Type *ADCx, uint16_t highValue, uint16_t lowValue)
{
    MODIFY_REG32(ADCx->AMOHR, ADC_AMOHR_AMOHT_Msk, ADC_AMOHR_AMOHT_Pos, highValue);
    MODIFY_REG32(ADCx->AMOLR, ADC_AMOLR_AMOLT_Msk, ADC_AMOLR_AMOLT_Pos, lowValue);
}

/*!
 * @brief Get analog monitor offset.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @param[out] highOffset: returned high offset value
                - 0~0xfff
 * @param[out] lowOffset: returned low offset value
                - 0~0xfff
 */
__STATIC_INLINE void ADC_GetAMOOffset(const ADC_Type *ADCx, uint16_t *highOffset, uint16_t *lowOffset)
{
    *highOffset = (uint16_t)(READ_BIT32(ADCx->AMOHR, ADC_AMOHR_AMOHO_Msk) >> ADC_AMOHR_AMOHO_Pos);
    *lowOffset = (uint16_t)(READ_BIT32(ADCx->AMOLR, ADC_AMOLR_AMOLO_Msk) >> ADC_AMOLR_AMOLO_Pos);
}

/*!
 * @brief Set analog monitor offset.
 *
 * @param[in] ADCx: adc module
                 - ADC0
 * @param[in] highOffset: high offset value
                 - 0~0xfff
 * @param[in] lowOffset: low offset value
                 - 0~0xfff
 * @return none
 */
__STATIC_INLINE void ADC_SetAMOOffset(ADC_Type *ADCx, uint16_t highOffset, uint16_t lowOffset)
{
    MODIFY_REG32(ADCx->AMOHR, ADC_AMOHR_AMOHO_Msk, ADC_AMOHR_AMOHO_Pos, highOffset);
    MODIFY_REG32(ADCx->AMOLR, ADC_AMOLR_AMOLO_Msk, ADC_AMOLR_AMOLO_Pos, lowOffset);
}

/*!
 * @brief Get AMO flag.
 *
 * @param[in] ADCx: adc module
                 - ADC0
 * @return AMO flag
 */
__STATIC_INLINE uint32_t ADC_GetAMOFlag(const ADC_Type *ADCx)
{
    return READ_BIT32(ADCx->STR, ADC_STR_AMO_Msk);
}

/*!
 * @brief Clear AMO flag.
 *
 * @param[in] ADCx: adc module
                 - ADC0
 * @return none
 */
__STATIC_INLINE void ADC_ClearAMOFlag(ADC_Type * const ADCx)
{
    CLEAR_BIT32(ADCx->STR, ADC_STR_AMO_Msk);
}

/*!
 * @brief Get EOC flag.
 *
 * @param[in] ADCx: adc module
                 - ADC0
 * @param[in] seq: regular group sequence number
 * @return EOC flag
 */
__STATIC_INLINE uint32_t ADC_GetEOCFlag(const ADC_Type *ADCx, uint8_t seq)
{
    return READ_BIT32(ADCx->REOC, 1U << seq);
}

/*!
 * @brief Clear EOC flag.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @param[in] seq: regualr group sequence number
 * @return none
 */
__STATIC_INLINE void ADC_ClearEOCFlag(ADC_Type *ADCx, uint8_t seq)
{
    ADCx->REOC = 1U << seq;
}

/*!
 * @brief Get IEOC flag.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @param[in] seq: injection group sequence number
 * @return IEOC flag
 */
__STATIC_INLINE uint32_t ADC_GetIEOCFlag(const ADC_Type *ADCx, uint8_t seq)
{
    return READ_BIT32(ADCx->IEOC, 1U << seq);
}

/*!
 * @brief Clear IEOC flag.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @param[in] seq: injection group sequence number
 * @return none
 */
__STATIC_INLINE void ADC_ClearIEOCFlag(ADC_Type *ADCx, uint8_t seq)
{
    ADCx->IEOC = 1U << seq;
}

/*!
* @brief Get idle flag.
*
* @param[in] ADCx: adc module
                - ADC0
* @return idle flag
*/
__STATIC_INLINE uint32_t ADC_GetIdleFlag(const ADC_Type *ADCx)
{
    return READ_BIT32(ADCx->STR, ADC_STR_IDLE_Msk);
}
/*!
* @brief Get normal analog monitor flag.
*
* @param[in] ADCx: adc module
                - ADC0
* @return NAMO flag
*/
__STATIC_INLINE uint32_t ADC_GetNAMOFlag(const ADC_Type *ADCx)
{
    return READ_BIT32(ADCx->STR, ADC_STR_NAMO_Msk);
}

/*!
* @brief Clear normal analog monitor flag.
*
* @param[in] ADCx: adc module
                - ADC0
* @return none
*/
__STATIC_INLINE void ADC_ClearNAMOFlag(ADC_Type * const ADCx)
{
    CLEAR_BIT32(ADCx->STR, ADC_STR_NAMO_Msk);
}

/*!
* @brief Get abnormal analog monitor flag.
*
* @param[in] ADCx: adc module
                - ADC0
* @return AAMO flag
*/
__STATIC_INLINE uint32_t ADC_GetAAMOFlag(const ADC_Type * ADCx)
{
    return READ_BIT32(ADCx->STR, ADC_STR_AAMO_Msk);
}

/*!
* @brief Clear abnormal analog monitor flag.
*
* @param[in] ADCx: adc module
                - ADC0
* @return none
*/
__STATIC_INLINE void ADC_ClearAAMOFlag(ADC_Type * const ADCx)
{
    CLEAR_BIT32(ADCx->STR, ADC_STR_AAMO_Msk);
}

/*!
 * @brief Get ADC calibration calculation enable flag setting.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @return whether calibration calculation funciton is enabled.
 */
__STATIC_INLINE ACTION_Type ADC_GetCalibration(const ADC_Type *ADCx)
{
    return (ACTION_Type)(READ_BIT32(ADCx->CTRL1, ADC_CTRL1_CALEN_Msk) >> ADC_CTRL1_CALEN_Pos);
}

/*!
* @brief Set Calibration function.
*
* @param[in] ADCx: adc module
                - ADC0
* @param[in] state: enabling state
                - ENABLE
                - DISABLE
* @return none
*/
__STATIC_INLINE void ADC_SetCalibration(ADC_Type *ADCx, ACTION_Type state)
{
    MODIFY_REG32(ADCx->CTRL1, ADC_CTRL1_CALEN_Msk, ADC_CTRL1_CALEN_Pos, state);
}

/*!
 * @brief Get ADC external channel gain error & offset error setting.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @param[out] gain: current gain error value setting in LSB
 * @param[out] offset: current offsest error value setting in LSB
 */
__STATIC_INLINE void ADC_GetExternalGainOffset(const ADC_Type *ADCx, int16_t *gain, int16_t *offset)
{
    *gain = (int16_t)((uint16_t)(READ_BIT32(ADCx->CGV, ADC_CGV_GE0_Msk) >> ADC_CGV_GE0_Pos) << 4U) >> 4U;
    *offset = (int16_t)((uint16_t)(READ_BIT32(ADCx->COV, ADC_COV_OE0_Msk) >> ADC_COV_OE0_Pos) << 5U) >> 5U;
}

/*!
 * @brief Set ADC external channel gain error & offset error.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @param[in] gain: gain error value setting in LSB
 * @param[in] offset: offsest error value setting in LSB
 */
__STATIC_INLINE void ADC_SetExternalGainOffset(ADC_Type *ADCx, int16_t gain, int16_t offset)
{
    uint16_t gainValue = gain, offsetValue = offset;
    MODIFY_REG32(ADCx->CGV, ADC_CGV_GE0_Msk, ADC_CGV_GE0_Pos, gainValue);
    MODIFY_REG32(ADCx->COV, ADC_COV_OE0_Msk, ADC_COV_OE0_Pos, offsetValue);
}

/*!
 * @brief Get ADC internal channel gain error & offset error setting.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @param[in] gain: current gain error value setting in LSB
 * @param[in] offset: current offsest error value setting in LSB
 */
__STATIC_INLINE void ADC_GetInternalGainOffset(const ADC_Type *ADCx, int16_t *gain, int16_t *offset)
{
    *gain = (int16_t)((uint16_t)(READ_BIT32(ADCx->CGV, ADC_CGV_GE1_Msk) >> ADC_CGV_GE1_Pos) << 4U) >> 4U;
    *offset = (int16_t)((uint16_t)(READ_BIT32(ADCx->COV, ADC_COV_OE1_Msk) >> ADC_COV_OE1_Pos) << 5U) >> 5U;
}

/*!
 * @brief Set ADC internal channel gain error & offset error.
 *
 * @param[in] ADCx: adc module
                - ADC0
 * @param[in] gain: gain error value setting in LSB
 * @param[in] offset: offsest error value setting in LSB
 */
__STATIC_INLINE void ADC_SetInternalGainOffset(ADC_Type *ADCx, int16_t gain, int16_t offset)
{
    uint16_t gainValue = gain, offsetValue = offset;
    MODIFY_REG32(ADCx->CGV, ADC_CGV_GE1_Msk, ADC_CGV_GE1_Pos, gainValue);
    MODIFY_REG32(ADCx->COV, ADC_COV_OE1_Msk, ADC_COV_OE1_Pos, offsetValue);
}

/*!
 * @brief Set adc voltage reference source.
 *
 * @param[in] base: adc module
 *                  - ADC0
 * @param[in] voltageRef: voltage reference source
 *                  - ADC_VOLTAGEREF_VREF
 *                  - ADC_VOLTAGEREF_VDDA
 * @return none
 */
__STATIC_INLINE void ADC_SetVoltageReference(ADC_Type * const base, const adc_voltage_reference_t voltageRef)
{
    MODIFY_REG32(ANA_RG->ADC_CFG0, ANA_RG_ADC_CFG0_VREF_SEL_Msk, ANA_RG_ADC_CFG0_VREF_SEL_Pos, voltageRef);
}

/*!
 * @brief Get adc voltage reference source setting.
 *
 * @param[in] base: adc module
 *                  - ADC0
 * @return  voltage reference source setting
 *                  - ADC_VOLTAGEREF_VREF
 *                  - ADC_VOLTAGEREF_VDDA
 */
__STATIC_INLINE adc_voltage_reference_t ADC_GetVoltageReference(const ADC_Type * const base)
{
    return (adc_voltage_reference_t)(READ_BIT32(ANA_RG->ADC_CFG0, ANA_RG_ADC_CFG0_VREF_SEL_Msk) >> ANA_RG_ADC_CFG0_VREF_SEL_Pos);
}

/*!
 * @brief Get internal BG & T-Sensor test buffer enable flag.
 *
 * @param[in] ADCx: adc module
 *                  - ADC0
 * @return whether internal BG & T-Sensor test buffer is enabled
 */
__STATIC_INLINE ACTION_Type ADC_GetVBufEnableFlag(const ADC_Type *ADCx)
{
    return (ACTION_Type)(READ_BIT32(ANA_RG->ADC_CFG0, ANA_RG_ADC_CFG0_VBUF_EN_Msk) >> ANA_RG_ADC_CFG0_VBUF_EN_Pos);
}

/*!
 * @brief Set internal BG & T-Sensor test buffer enable flag.
 *
 * @param[in] ADCx: adc module
 *                  - ADC0
 * @param[in] state: enable state
                    - ENABLE
                    - DISABLE
 */
__STATIC_INLINE void ADC_SetVBufEnableFlag(ADC_Type *ADCx, ACTION_Type state)
{
    MODIFY_REG32(ANA_RG->ADC_CFG0, ANA_RG_ADC_CFG0_VBUF_EN_Msk, ANA_RG_ADC_CFG0_VBUF_EN_Pos, state);
}

/*!
 * @brief Get internal BG & T-Sensor test channel enable flag
 *
 * @param[in] ADCx: adc module
 *                  - ADC0
 * @return whether internal BG & T-Sensor test channel is enabled
                    - ENABLE
                    - DISABLE
 */
__STATIC_INLINE ACTION_Type ADC_GetTestChannelEnableFlag(const ADC_Type *ADCx)
{
    return (ACTION_Type)(READ_BIT32(ANA_RG->ADC_CFG1, ANA_RG_ADC_CFG1_TEST_EN_Msk) >> ANA_RG_ADC_CFG1_TEST_EN_Pos);
}

/*!
 * @brief Set internal BG & T-Sensor test channel enable flag
 *
 * @param[in] ADCx: adc module
 *                  - ADC0
 * @param[in] state: enable state
                    - ENABLE
                    - DISABLE
 */
__STATIC_INLINE void ADC_SetTestChannelEnableFlag(ADC_Type *ADCx, ACTION_Type state)
{
    MODIFY_REG32(ANA_RG->ADC_CFG1, ANA_RG_ADC_CFG1_TEST_EN_Msk, ANA_RG_ADC_CFG1_TEST_EN_Pos, state);
}

/*!
 * @brief Get ADC internal monitor channel setting.
 *
 * @param[in] base: adc module
 *                  - ADC0
 * @return current internal monitor channel selection setting
                    - ADC_INTERNAL_BG
                    - ADC_INTERNAL_TSENSOR
 */
__STATIC_INLINE adc_internal_channel_t ADC_GetInternalChannel(const ADC_Type * const base)
{
    return (adc_internal_channel_t)(READ_BIT32(ANA_RG->ADC_CFG1, ANA_RG_ADC_CFG1_MON_SEL_Msk) >> ANA_RG_ADC_CFG1_MON_SEL_Pos);
}

/*!
 * @brief Set ADC internal channel.
 *
 * @param[in] base: adc module
 *                  - ADC0
 * @param[in] signal: internal signal setting.
                    - ADC_INTERNAL_BG
                    - ADC_INTERNAL_TSENSOR
 */
__STATIC_INLINE void ADC_SetInternalChannel(ADC_Type * const base, const adc_internal_channel_t signal)
{
    MODIFY_REG32(ANA_RG->ADC_CFG1, ANA_RG_ADC_CFG1_MON_SEL_Msk, ANA_RG_ADC_CFG1_MON_SEL_Pos, signal);
}

#ifdef __cplusplus
}
#endif

#endif /* _AC780X_ADC_REG_H */

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