

/**
 ******************************************************************************
 *
 * @file        main.C
 * @brief       MG32 demo main c Code. 
 *
 * @par         Project
 *              MG32
 * @version     V1.02
 * @date        2024/10/16
 * @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"
#include "MG32_IEC60730_Common.h"

/* Wizard menu ---------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#if defined(MG32_2ND)
    #define MG_EB_LED1SET  PIND(8)
    #define MG_EB_LED2SET  PIND(9)
    #define MG_EB_LED3SET  PIND(10)

    #define MG_EB_LED1     PD8
    #define MG_EB_LED2     PD9
    #define MG_EB_LED3     PD10
#endif
#if defined(MG32_1ST) || defined(MG32_3RD)
    #define MG_EB_LED1     PE13
    #define MG_EB_LED2     PE14
    #define MG_EB_LED3     PE15
    
    #define MG_EB_LED1SET  PINE(13)
    #define MG_EB_LED2SET  PINE(14)
    #define MG_EB_LED3SET  PINE(15)
#endif

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void Sample_Code(void);
void Sample_SysTick_Init(void);

/* Exported variables --------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
/* External vairables --------------------------------------------------------*/


int main(void)
{    
    PIN_InitTypeDef        PINX_InitStruct;
    
    // ------------------------------------------------------------------------
    // chip initial (User can enable CSC, GPIO, TM, ADC, EXIC ... wizard)
    // ------------------------------------------------------------------------
    ChipInit();
    
    Sample_SysTick_Init();
        
    //============================================
    //System Tick inital
    InitTick(12000000UL, 0); // 1ms Tick
    
    //============================================
    PINX_InitStruct.PINX_Mode				= PINX_Mode_PushPull_O;
    PINX_InitStruct.PINX_PUResistant        = PINX_PUResistant_Enable;
    PINX_InitStruct.PINX_Speed              = PINX_Speed_Low;
    PINX_InitStruct.PINX_OUTDrive           = PINX_OUTDrive_Level0;
    PINX_InitStruct.PINX_FilterDivider      = PINX_FilterDivider_Bypass;
    PINX_InitStruct.PINX_Inverse            = PINX_Inverse_Disable;
    PINX_InitStruct.PINX_Alternate_Function = 0;

    GPIO_PinMode_Config(MG_EB_LED1SET,&PINX_InitStruct);
    GPIO_PinMode_Config(MG_EB_LED2SET,&PINX_InitStruct);
    GPIO_PinMode_Config(MG_EB_LED3SET,&PINX_InitStruct);
    
    while(1)
    {            
        // Toggle LEDs        
		PE13 = 0; PE14 = 1; PE15 = 1;
		Delay(250);
		PE13 = 1; PE14 = 0; PE15 = 1;
		Delay(250);
		PE13 = 1; PE14 = 1; PE15 = 0;
		Delay(250);
        
        //To do......
        
        
        // --------------------------------------------------------------------
        // 1. 'IEC60730_Flash_RunTime_AP' function call is only available in 'ON Mode'.
        // 2. User call this function 'IEC60730_Flash_RunTime_AP' to check AP area using CRC code.
        // 3. If 'IEC60730_Flash_RunTime_AP' returns 'IEC60730_TEST_FAILURE' code. 
        //    User must pay attention to this issue.
        // --------------------------------------------------------------------
        if (IEC60730_Flash_RunTime_AP() == IEC60730_TEST_FAILURE)
        {
            // to do ... (There is an unknow problem in the AP area. User needs to care about it.)
            
            while(1);
        }
        
        // --------------------------------------------------------------------
        // 1. 'IEC60730_Flash_RunTime_IAP' function call is only available in 'ON Mode'.
        // 2. User call this function 'IEC60730_Flash_RunTime_IAP' to check IAP area using CRC code.
        // 3. If 'IEC60730_Flash_RunTime_IAP' returns 'IEC60730_TEST_FAILURE' code. User must pay attention to this issue.
        // 4. The IAP check code is stored in the last 4 bytes of the last page of the IAP area.
        // 5. Very import !!! After updating the IAP area, user must update IAP check code by 'xxxxxx' function call.
        // --------------------------------------------------------------------
//        if (IEC60730_Flash_RunTime_IAP() == IEC60730_TEST_FAILURE)
//        {
//            // to do ... (There is an unknow problem in the IAP area. User needs to care about it.)
//            
//            while(1);
//        }
        
    }
}




















