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

/*!
 *例程说明：
 *1、休眠唤醒例程,按下按键K1进入STOP模式，LED停止闪烁。
 *2、使用AMO监控电位器对应通道电压，调节电位器VR1，触发AMO中断退出休眠模式，LED恢复闪烁。
 */

/*!
 * @file main.c
 *
 * @brief This file is main.c.
 *
 */

/* ===========================================  Includes  =========================================== */
#include "gpio.h"
#include "adc_sample.h"
#include "system_ac7802x.h"
#include "ac780x_spm.h"

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

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

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

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

/* ======================================  Functions define  ======================================== */

/*!
 * @brief   main函数
 *
 * @param   none
 * @return  int
 */
int main(void)
{
    InitDelay(); /*! 初始化延时函数*/
    InitDebug(); /*! 初始化串口打印*/
    printf("\r\nAC7802x: ADC STOP Mode Sample\r\n");

    GPIO_LedInit();    /*! LED初始化*/
    GPIO_KeyInit();    /*! 按键初始化*/
    ADC_Sample_init(); /*! AD初始化*/

    SPM_EnableModuleWakeup(SPM_MODULE_ADC0, ENABLE); /*! 配置模块唤醒功能*/
    LED1_ON;                                         /*! 点亮LED*/
    mdelay(2000);                                    /*! delay 2S以防止误操作导致的一上电就进入Stop模式 */

    while (1)
    {
        mdelay(200); /*! tick延时200ms*/

        ADC_PrintValue(); /*! 触发ADC数据转换并打印 */
        LED1_TOGGLE;      /*! LED灯闪烁，指示程序运行*/

        if (Get_Key1Value()) /*! 判断KEY1是否按下 */
        {
            printf("Go To Stop Mode...\r\n"); /*! 休眠前Log打印 */
            DisableInterrupts;                /*! 休眠前关闭中断，防止休眠流程被打断。*/
            LED1_OFF;                         /*! 休眠前关闭LED1 */
            SysStop();                        /*! 进入Stop模式 */

            LED1_ON;                     /*! 调节电位器VR1，触发中断唤醒MCU,LED1_ON亮 */
            EnableInterrupts;            /*! 唤醒后先进行必要的处理，再打开中断。*/
            printf("Wakeup Now...\r\n"); /*! 唤醒后打印Log */
        }
    }
}

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