/**
 ******************************************************************************
 *
 * @file        MG32_ADC_Init.H
 *
 * @brief       This file is to configure ADC baudrate setting.
 *              Device : MG32F02D032
 * @par         Project
 *              MG32 
 * @version     V1.03
 * @date        2026/04/15
 * @author      MG32CoGen 
 * @copyright   Copyright (c) 2022 MegaWin Technology Co., Ltd.
 *              All rights reserved.
 *
 ******************************************************************************
 * @par         Disclaimer 
 *      The Demo software is provided "AS IS"  without any warranty, either 
 *      expressed or implied, including, but not limited to, the implied warranties 
 *      of merchantability and fitness for a particular purpose.  The author will 
 *      not be liable for any special, incidental, consequential or indirect 
 *      damages due to loss of data or any other reason. 
 *      These statements agree with the world wide and local dictated laws about 
 *      authorship and violence against these laws. 
 *******************************************************************************
 *******************************************************************************
 */

#ifndef _MG32_ADC_INIT_H
 
#define _MG32_ADC_INIT_H

#include "MG32_ADC_DRV.h"
#if defined(IRQHandler_Middleware_Level_)       
    #include "MG32_ADC_MID.h"
#endif

// <<< Use Configuration Wizard in Context Menu >>>

//<e0.0> Module - Mode Active
    #define CONF_ADC0_MODE                                  0x00000000
//  <h> ADC base parameters
//      <o0.16..23> ADC sampling time select <0-255>
//              <i> 0T ~ 255T clocks.T= CK_ADC0_INT
            #define CONF_ADC0_CR0_SMP_SEL                   0x00000000
//      <o0.6..7> ADC data resolution select <0=> 12-bit <1=> 10-bit <2=> 8-bit
            #define CONF_ADC0_CR0_RES_SEL                   0x00000000
//      <o0.3> ADC data alignment select <0=> Right (Right alignment) <1=> Left (Left alignment)
            #define CONF_ADC0_CR1_ALIGN_SEL                 0x00000000
//      <o0.24..28> Digital Offset adjuster <0x10=> -16 <0x11=> -15 <0x12=> -14 <0x13=> -13 <0x14=> -12 <0x15=> -11
//                                          <0x16=> -10 <0x17=> -9 <0x18=> -8 <0x19=> -7 <0x1A=> -6 <0x1B=> -5
//                                          <0x1C=> -4 <0x1D=> -3 <0x1E=> -2 <0x1F=> -1 <0x00=> 0 <0x01=> +1
//                                          <0x02=> +2 <0x03=> +3 <0x04=> +4 <0x05=> +5 <0x06=> +6 <0x07=> +7
//                                          <0x08=> +8 <0x09=> +9 <0x0A=> +10 <0x0B=> +11 <0x0C=> +12 <0x0D=> +13 <0x0E=> +14 <0x0F=> +15
            #define CONF_ADC0_CR1_DOS_VAL                   0x00000000
//      <o0.2> Stop to convert the next channel (WAIT function)
//          <i> Wait conversion mode enable for low CPU frequency
//          <i> Release condition : 'Read ADC Data' or 'Clear flag'
            #define CONF_ADC0_CR0_WAIT_EN                   0x00000000
//      <o0.8> Overrun mode <0=> Overwritten (Overwritten by new data) <1=> Keep (Preserved old date)
            #define CONF_ADC0_CR1_OVR_MDS                   0x00000000
//      <o0.19..25> ADC conversion mode <0x00=> ADC One shot mode <0x01=> ADC Continue mode <0x20=> Scan mode <0x21=> Scan continue mode <0x40=> Loop mode
//          <i> ADC One Shot mode : Convert one ADC data from single channel after one start trigger, then stop conversion.
//          <i> ADC Continue mode : Convert one ADC data from single channel after one start trigger, then hardware automatically start next ADC conversion continuously and does not stop conversion.
//          <i> Scan mode : Convert one ADC data from one selected channel after one start trigger, then stop conversion. To follow and convert next ADC data from next selected channel after next start trigger. Run the same steps for all selected channels.
//          <i> Scan continue mode : Convert multiple ADC data continuously from multiple selected channels after one start trigger, then stop conversion when scan finished for all selected channels.
//          <i> Loop mode : When ADC conversion start by one start trigger, hardware does the channel scan ADC conversion one by one until finish the conversion for all selected channels. And hardware automatically start next channel scan ADC conversion continuously and does not stop.
            #define CONF_ADC0_START                         0x00000000
//      <o0.16..18> ADC start control source select <0x0=> SW : ADC0_START register setting <0x1=> TM00 : TM00_TRGO <0x2=> PIN : ADC0_TRG : ADC external trigger pin
//                                                  <0x5=> TM01 : TM01_TRGO <7=> TM36 : TM36_TRGO
            #define CONF_ADC0_START_SEL                     0x00000000
//          <o0.20..21> ADC0 start trigger selection <0x0=> Disable <0x1=> Rising edge <0x2=> Falling edge <0x4=> Dual-edge
//              <i> When selects Disable, the edge trigger detection is disabled and no start trigger signal output. When ADC0_START_SEL = SW (ADC0_START register setting), this register is no effect.
                #define CONF_ADC0_START_TRG_SEL             0x00000000
//      <o0.8..12> Conversion one channel select
//          <0=> PA0
//          <1=> PA1
//          <2=> PA2
//          <3=> PA3
//          <16=> Internal VSSA
//          <17=> DAC_P1
//          <18=> DAC_P0
//          <19=> INT_VBG (VBUF)
            #define CONF_ADC0_START_CH_MUX                  0x00000000
//      <h> Scan/Loop channel select - sequence channel scan
//          <q0.0> PA0
                #define CONF_ADC0_MSK_B0                    0x00000000
//          <q0.1> PA1
                #define CONF_ADC0_MSK_B1                    0x00000000
//          <q0.2> PA2      
                #define CONF_ADC0_MSK_B2                    0x00000000
//          <q0.3> PA3          
                #define CONF_ADC0_MSK_B3                    0x00000000
//      </h>
//    </h>
//    <h> Clock configuration
//      <o0.8..9> CK_ADC_INT source select <0=> CK_ADC_PR + Divider <3=> TM00_TRGO/2 <2=> TM01_TRGO/2
            #define CONF_ADC0_CLK_CK_SEL2                   0x00000000
//          <i> CK_ADC_PR from CSC (AHB or APB).
//          <i> TM00_TRGO : TM00 periodic output overflow event (User must enable TM00).
//          <i> TM01_TRGO : TM01 periodic output overflow event (User must enable TM01).
//      <o0.4..5> CK_ADC_PR input divider <0=> DIV1 <1=> DIV2 <3=> DIV4 <3=> DIV16
            #define CONF_ADC0_CLK_CK_DIV                    0x00000000
//    </h>
//      <h> Auto-off function
//          <o0.1> Auto-off function Enable
//          <i> The ADC needs time to wake up. So please refer to the power-on configuration of CLK. 
            #define CONF_ADC0_CR0_AUTOFF_EN                 0x00000000
//      <o0.24..25> ADC power-on start up counter clock divider.<0=> DIV1 <1=> DIV4 <2=> DIV16 <3=> DIV32
//          <i> This divider is used to divide the input APB clock to output as the start up counter clock.
            #define CONF_ADC0_CLK_CK_SDIV                   0x00000000
//      <o0.0..3> ADC power-on start up counter. <0-15>
//          <i> This register is only valid when auto power-off mode is enabled (ADC0_AUTOFF_EN=1).
//          <i> The range 0~15 is indicated counter inital value 0~15. 
            #define CONF_ADC0_CLK_SCNT                      0x00000000
//      </h> 
//  </e>

//<e0.0> ADC interrupt all enable (IEA)
            #define CONF_ADC0_INT_IEA                       0x00000000
//      <q0.2> ADC sampling end interrupt (ESMP_IE)
            #define CONF_ADC0_INT_ESMP                      0x00000000
//      <q0.3> ADC one-time conversion end interrupt (E1CNV_IE)
            #define CONF_ADC0_INT_E1CNV                     0x00000000
//      <q0.5> ADC channel scan conversion end interrupt (ESCNV_IE)
            #define CONF_ADC0_INT_ESCNV                     0x00000000
//      <q0.7> ADC conversion overrun event interrupt (OVR_IE)
            #define CONF_ADC0_INT_OVR                       0x00000000
//</e>
// <<< end of Configuration section >>>

    // ----------------------------------------------
    // Combined with ADC_INT register bit function
    #define CONF_ADC_INT    (CONF_ADC0_INT_IEA      |\
                             CONF_ADC0_INT_ESMP     |\
                             CONF_ADC0_INT_E1CNV    |\
                             CONF_ADC0_INT_ESCNV    |\
                             CONF_ADC0_INT_OVR		)
            
    // ----------------------------------------------
    // Combined with ADC_CLK register bit function
    #if defined(CONF_ADC0_CLK_SCNT)
    #define CONF_ADC_CLK    (CONF_ADC0_CLK_CK_SEL2  |\
                             CONF_ADC0_CLK_CK_DIV   |\
                             0x00002000             |\
                             CONF_ADC0_CLK_CK_SDIV  |\
                             CONF_ADC0_CLK_SCNT     )
    #else                                                
    #define CONF_ADC_CLK    (CONF_ADC0_CLK_CK_SEL2  |\
                             CONF_ADC0_CLK_CK_DIV   )
    #endif
                                                     
    // ----------------------------------------------
    // Combined with ADC_CR0 register bit function
    #if defined(CONF_ADC0_CR0_MDS)
    #define CONF_ADC_CR0    (CONF_ADC0_CR0_SMP_SEL  |\
                             CONF_ADC0_CR0_RES_SEL  |\
                             CONF_ADC0_CR0_WAIT_EN  |\
                             CONF_ADC0_CR0_MDS      )   
    #elif defined(CONF_ADC0_CR0_AUTOFF_EN)
    #define CONF_ADC_CR0    (CONF_ADC0_CR0_SMP_SEL  |\
                             CONF_ADC0_CR0_RES_SEL  |\
                             CONF_ADC0_CR0_WAIT_EN  |\
                             CONF_ADC0_CR0_AUTOFF_EN)   
    #else                                                
    #define CONF_ADC_CR0    (CONF_ADC0_CR0_SMP_SEL  |\
                             CONF_ADC0_CR0_RES_SEL  |\
                             CONF_ADC0_CR0_WAIT_EN  )   
    #endif                                               
                                                     
    // ----------------------------------------------
    // Combined with ADC_CR1 register bit function
    #define CONF_ADC_CR1    (CONF_ADC0_CR1_ALIGN_SEL|\
                             CONF_ADC0_CR1_DOS_VAL  |\
                             CONF_ADC0_CR1_OVR_MDS  )

    // ----------------------------------------------
    // Combined with ADC_MSK register bit function
    #if defined(MG32_1ST) || defined(MG32_3RD) || defined(MG32_5TH)
    #define CONF_ADC_MSK    (CONF_ADC0_MSK_SUM0_MUX |\
                             CONF_ADC0_MSK_SUM1_MUX |\
                             CONF_ADC0_MSK_SUM2_MUX |\
                             CONF_ADC0_MSK_B0       |\
                             CONF_ADC0_MSK_B1       |\
                             CONF_ADC0_MSK_B2       |\
                             CONF_ADC0_MSK_B3       |\
                             CONF_ADC0_MSK_B4       |\
                             CONF_ADC0_MSK_B5       |\
                             CONF_ADC0_MSK_B6       |\
                             CONF_ADC0_MSK_B7       |\
                             CONF_ADC0_MSK_B8       |\
                             CONF_ADC0_MSK_B9       |\
                             CONF_ADC0_MSK_B10      |\
                             CONF_ADC0_MSK_B11      |\
                             CONF_ADC0_MSK_B12      |\
                             CONF_ADC0_MSK_B13      |\
                             CONF_ADC0_MSK_B14      |\
                             CONF_ADC0_MSK_B15      )
    #define CONF_ADC_MSKx   (CONF_ADC0_MSK_B0       |\
                             CONF_ADC0_MSK_B1       |\
                             CONF_ADC0_MSK_B2       |\
                             CONF_ADC0_MSK_B3       |\
                             CONF_ADC0_MSK_B4       |\
                             CONF_ADC0_MSK_B5       |\
                             CONF_ADC0_MSK_B6       |\
                             CONF_ADC0_MSK_B7       |\
                             CONF_ADC0_MSK_B8       |\
                             CONF_ADC0_MSK_B9       |\
                             CONF_ADC0_MSK_B10      |\
                             CONF_ADC0_MSK_B11      |\
                             CONF_ADC0_MSK_B12      |\
                             CONF_ADC0_MSK_B13      |\
                             CONF_ADC0_MSK_B14      |\
                             CONF_ADC0_MSK_B15      )
    #elif defined(MG32_2ND)
    #define CONF_ADC_MSK    (CONF_ADC0_MSK_SUM0_MUX |\
                             CONF_ADC0_MSK_SUM1_MUX |\
                             CONF_ADC0_MSK_SUM2_MUX |\
                             CONF_ADC0_MSK_B0       |\
                             CONF_ADC0_MSK_B1       |\
                             CONF_ADC0_MSK_B2       |\
                             CONF_ADC0_MSK_B3       |\
                             CONF_ADC0_MSK_B8       |\
                             CONF_ADC0_MSK_B9       |\
                             CONF_ADC0_MSK_B10      |\
                             CONF_ADC0_MSK_B11      |\
                             CONF_ADC0_MSK_B12      |\
                             CONF_ADC0_MSK_B13      |\
                             CONF_ADC0_MSK_B14      |\
                             CONF_ADC0_MSK_B15      )
    #define CONF_ADC_MSKx   (CONF_ADC0_MSK_B0       |\
                             CONF_ADC0_MSK_B1       |\
                             CONF_ADC0_MSK_B2       |\
                             CONF_ADC0_MSK_B3       |\
                             CONF_ADC0_MSK_B8       |\
                             CONF_ADC0_MSK_B9       |\
                             CONF_ADC0_MSK_B10      |\
                             CONF_ADC0_MSK_B11      |\
                             CONF_ADC0_MSK_B12      |\
                             CONF_ADC0_MSK_B13      |\
                             CONF_ADC0_MSK_B14      |\
                             CONF_ADC0_MSK_B15      )
    #elif defined(MG32_4TH)
    #define CONF_ADC_MSK    (CONF_ADC0_MSK_SUM0_MUX |\
                             CONF_ADC0_MSK_SUM1_MUX |\
                             CONF_ADC0_MSK_SUM2_MUX |\
                             CONF_ADC0_MSK_B0       |\
                             CONF_ADC0_MSK_B1       |\
                             CONF_ADC0_MSK_B2       |\
                             CONF_ADC0_MSK_B3       |\
                             CONF_ADC0_MSK_B8       |\
                             CONF_ADC0_MSK_B9       |\
                             CONF_ADC0_MSK_B10      |\
                             CONF_ADC0_MSK_B11      )
    #define CONF_ADC_MSKx   (CONF_ADC0_MSK_B0       |\
                             CONF_ADC0_MSK_B1       |\
                             CONF_ADC0_MSK_B2       |\
                             CONF_ADC0_MSK_B3       |\
                             CONF_ADC0_MSK_B8       |\
                             CONF_ADC0_MSK_B9       |\
                             CONF_ADC0_MSK_B10      |\
                             CONF_ADC0_MSK_B11      )
    #elif defined(MG32_6TH)
    #define CONF_ADC_MSK    (CONF_ADC0_MSK_B0       |\
                             CONF_ADC0_MSK_B1       |\
                             CONF_ADC0_MSK_B2       |\
                             CONF_ADC0_MSK_B3       )
    #define CONF_ADC_MSKx   (CONF_ADC0_MSK_B0       |\
                             CONF_ADC0_MSK_B1       |\
                             CONF_ADC0_MSK_B2       |\
                             CONF_ADC0_MSK_B3       )
    #endif                                               

    // ----------------------------------------------
    // ADC_MSK check rule
    #if CONF_ADC0_START>=0x01000000
        #if CONF_ADC_MSKx==0
            #error "Error: ADC0 needs to enable sequence channel"
        #endif
    #endif

    // ----------------------------------------------
    // Combined with ADC_START register bit function
    #define CONF_ADC_START  (CONF_ADC0_START        |\
                             CONF_ADC0_START_SEL    |\
                             CONF_ADC0_START_TRG_SEL|\
                             CONF_ADC0_START_CH_MUX )

    // ----------------------------------------------
    // Combined with ADC_ANA register bit function
    #if defined(CONF_ADC0_ANA_PGA_EN)
        #if defined(MG32_1ST)
        #define CONF_ADC_ANA    (CONF_ADC0_ANA_PGA_EN   |\
                                 0x00000000             )
        #else
        #define CONF_ADC_ANA    (CONF_ADC0_ANA_PGA_EN   |\
                                 0x00000300             )
        #endif
    #else
    #define CONF_ADC_ANA    (0x00000300)
    #endif  

    // ----------------------------------------------
    // Combined with ADC_GAIN register bit function
    #define CONF_ADC_GAIN   (0)
       
        
    // ------------------------------------------------------------------------
    // Assemble mADC structure within Middleware level
    // ------------------------------------------------------------------------

    // ------------------------------------------------------------------------
    // Single-End or Different mode selection
    #if defined(CONF_ADC0_CR0_MDS)
        #define mADC_Conversion_Type            CONF_ADC0_CR0_MDS
    #else
        #define mADC_Conversion_Type            ADC_CONV_SINGLE
    #endif
        
    // ------------------------------------------------------------------------
    // Clock + Divider selection
    #if CONF_ADC0_CLK_CK_SEL2 == (0 << 8)  

        // Clock divider selection
        #if CONF_ADC0_CLK_CK_DIV == (0 << 4)
            #define mADC_Clock_Select           ADC_CLOCKPROC_DIV1
        #elif CONF_ADC0_CLK_CK_DIV == (1 << 4)
            #define mADC_Clock_Select           ADC_CLOCKPROC_DIV2
        #elif CONF_ADC0_CLK_CK_DIV == (2 << 4)
            #define mADC_Clock_Select           ADC_CLOCKPROC_DIV4
        #elif CONF_ADC0_CLK_CK_DIV == (3 << 4)
            #define mADC_Clock_Select           ADC_CLOCKPROC_DIV16
        #endif    

    #elif CONF_ADC0_CLK_CK_SEL2 == (2 << 8)  
        // select TM00_TRGO / 2
        #define mADC_Clock_Select               ADC_TM00UPDATE_DIV2
    #elif CONF_ADC0_CLK_CK_SEL2 == (3 << 8)
        // select TM01_TRGO / 2
        #define mADC_Clock_Select               ADC_TM00UPDATE_DIV2
    #endif

    // ------------------------------------------------------------------------
    // ADC conversion resolution selection
    #define mADC_Resolution_Select              CONF_ADC0_CR0_RES_SEL

    // ------------------------------------------------------------------------
    // Right / Left justified
    #define mADC_Justified_Select               CONF_ADC0_CR1_ALIGN_SEL

    // ------------------------------------------------------------------------
    // ADC Conversion Mode select
    #define mADC_Conversion_Mode                CONF_ADC0_START

    // ------------------------------------------------------------------------
    // ADC Wait_EN Function select
    #define mADC_WaitFunction_Select            CONF_ADC0_CR0_WAIT_EN

    // ------------------------------------------------------------------------
    // Auto Power with  select  
    #if defined(CONF_ADC0_CR0_AUTOFF_EN)
        #define mADC_AutoPowerOff_Select        CONF_ADC0_CR0_AUTOFF_EN
    #else
        #define mADC_AutoPowerOff_Select        0
    #endif

    // ------------------------------------------------------------------------
    // Clock divider select for wake up state   
    #if defined(CONF_ADC0_CLK_CK_SDIV)
        #define mADC_SDIV_Select                CONF_ADC0_CLK_CK_SDIV
        #define mADC_SCNT_Select                CONF_ADC0_CLK_SCNT >> 28        // Clock counter for wake up state  
    #else
        #define mADC_SDIV_Select                0
        #define mADC_SCNT_Select                0        
    #endif

    // ------------------------------------------------------------------------
    // ADC Trigger Converion Source Select 
    #define mADC_TriggerConversion_Select       CONF_ADC0_START_SEL

    // ------------------------------------------------------------------------
    // ADC Trigger Edge Select 
    #define mADC_TriggerEdge_Select             CONF_ADC0_START_TRG_SEL

    // ------------------------------------------------------------------------
    // ADC Over Written Function Select 
    #define mADC_OverWritten_Select             CONF_ADC0_CR1_OVR_MDS

    // ------------------------------------------------------------------------
    // ADC Extend Sampling Time
    #define mADC_ExtendSampleTime_Select        (CONF_ADC0_CR0_SMP_SEL >> 16)

    // ------------------------------------------------------------------------
    // ADC PGA Command + ADC PGA Gain Select
    #if defined(CONF_ADC0_ANA_PGA_EN)
        #define mADC_PGACmd_Select              CONF_ADC0_ANA_PGA_EN
        #define mADC_PGAGain_Select             CONF_ADC0_GAIN_PGA
    #else
        #define mADC_PGACmd_Select              0
        #define mADC_PGAGain_Select             0
    #endif


/**
 * @name    Function announce
 *          
 */
///@{ 
void ADC_Init(void);

///@}
#endif  // _MG32_ADC_INIT_H

