

/**
 ******************************************************************************
 *
 * @file        ADC_x4_DataMode_main.c
 * @brief       MG32 demo main c Code. 
 *
 * @par         Project
 *              MG32
 * @version     V1.00
 * @date        2026/07/03
 * @author      Megawin Software Center
 * @copyright   Copyright (c) 2017 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.
 *******************************************************************************
 *******************************************************************************
 */



/* Includes ------------------------------------------------------------------*/
#include "MG32_DRV.h"
#include "MG32_ChipInit.h"

/* Wizard menu ---------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
/* External vairables --------------------------------------------------------*/

int main(void)
{    
    ADC_InitTypeDef ADC_Base;
    __IO uint16_t ADC_PA0_Data, ADC_PA1_Data, ADC_PA2_Data, ADC_PA3_Data;
    
    #if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) /* ARM Compiler V6 */
        (void) ADC_PA0_Data;
        (void) ADC_PA1_Data;
        (void) ADC_PA2_Data;
        (void) ADC_PA3_Data;
    #endif
    
    // ------------------------------------------------------------------------
    // chip initial (User can enable CSC, GPIO, TM, ADC, EXIC ... wizard)
    // ------------------------------------------------------------------------
    ChipInit();    
        

    // ------------------------------------------------------------------------
    // 1.Config ADC base parameter    
    ADC_BaseStructure_Init(&ADC_Base);
    {   // modify parameter
        ADC_Base.ADCMainClockSelect = ADC_CKADC;
        ADC_Base.ADC_IntCK_Div = ADC_IntDIV4;       // 250KSPS=24MHz/DIV4/24clks
    
        // ADC data alignment mode (Right or Left)
        ADC_Base.ADC_DataAlign = ADC_RightJustified;
        
        // ADC conversion resolution 8, 10 or 12 bit
        ADC_Base.ADC_ResolutionSel = ADC_12BitData;
        
        // ADC overwritten data or keep data
        ADC_Base.ADC_DataOverrunEvent = ADC_DataOverWritten;
        
    }
    ADC_Base_Init(ADC0, &ADC_Base);
    
    // ------------------------------------------------------------------------
	// 2.Enable ADC 
    ADC_Cmd(ADC0, ENABLE);
    
    // ------------------------------------------------------------------------
    // 3.Config ADC conversion mode and enable all ADC conversion channels
    ADC_ConversionMode_Select(ADC0, ScanContinueMode);  // ScanContinueMode
    ADC_ScanLoopChannel_Enable(ADC0, (ADC_MskAIN0 | ADC_MskAIN1 | ADC_MskAIN2 | ADC_MskAIN3), ENABLE);
    
    // ------------------------------------------------------------------------
    // 4.Clear all flag
    ADC_ClearFlag(ADC0, 0xFFFFFFFF);

    // ------------------------------------------------------------------------
    // 5.Start Calibration 
    ADC_StartCalibration(ADC0, ENABLE);
        
    // ------------------------------------------------------------------------
	// 6.Enable ADC four data mode
    ADC_FourDataMode_Cmd(ADC0, ENABLE);

    // ------------------------------------------------------------------------
	// 7.Trigger Source select and Start conversion
    ADC_TriggerSource_Select(ADC0, ADC_START);
    
    // === ADC sample PA0~PA3 channel in ScanContinueMode ===
    ADC_ChannelMUX_Select(ADC0, ADC_ExternalChannel);
    
    ADC_SoftwareConversion_Cmd(ADC0, ENABLE);
    while(ADC_GetSingleFlagStatus(ADC0, ADC_ESCNVF) == DRV_UnHappened);
    ADC_ClearFlag(ADC0, ADC_ESCNVF);
    
    ADC_PA0_Data = (uint16_t) ADC_GetDAT0Data(ADC0);    // Get PA0 channel ADC conversion data.
    ADC_PA1_Data = (uint16_t) ADC_GetDAT1Data(ADC0);    // Get PA1 channel ADC conversion data.
    ADC_PA2_Data = (uint16_t) ADC_GetDAT2Data(ADC0);    // Get PA2 channel ADC conversion data.
    ADC_PA3_Data = (uint16_t) ADC_GetDAT3Data(ADC0);    // Get PA3 channel ADC conversion data.
    
    // to do ... 


    while(1)
    {
        //To do......
    }
}




















