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

/*!
 * @RoutineDescription
 *
 * 1.ADC low-power wake-up routine: Press button K3 to enter STOP mode, LED1 blinking stops.
 * 2.Utilize AMO to monitor the voltage of the potentiometer on the corresponding channel. Adjust potentiometer VR1, trigger AMO interrupt to exit low-power mode, LED1 resumes blinking.
 */

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

/* ===========================================  Includes  =========================================== */
#include <string.h>
#include "gpio.h"
#include "adc.h"
#include "ac780x_spm.h"
#include "ac780x_uart.h"

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

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

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

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

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

/*!
 * @brief main function
 *
 * @param none
 *
 * @return int
 */
int main(void)
{
    InitDelay();
    InitDebug();
    printf("\r\nAC7803x: ADC STOP Mode Sample\r\n");

    GPIO_LedInit();
    GPIO_KeyInit();
    ADC_Sample_init();

    SPM_EnableModuleWakeup(SPM_MODULE_ADC0, ENABLE); /* Configure the ADC0 module for wake-up functionality. */
    LED1_ON;
    mdelay(1000);

    while (1)
    {
        mdelay(400);

        printf("ADC_value:  VR1 = %d, Bandgap = %d \r\n", g_adc_array[0], g_adc_array[1]);
        LED1_TOGGLE;

        if (Get_Key3Value())
        {
            printf("Go To Stop Mode...\r\n");

            SysTick->CTRL &= (~SysTick_CTRL_ENABLE_Msk);
            SCB->ICSR |= SCB_ICSR_PENDSTCLR_Msk;                /* Clear SysTick exception pending bit. */
            DisableInterrupts;                /* Before entering low-power mode, disable global interrupts to prevent interruption of the low-power process. */
            memset((uint32_t *)NVIC->ICER, 0xFF, sizeof(NVIC->ICER));   /* Disable all enabled interrupts in NVIC. */
            memset((uint32_t *)NVIC->ICPR, 0xFF, sizeof(NVIC->ICPR));   /* Clear all pending interrupt requests in NVIC. */
            LED1_OFF;
            LED2_OFF;
            UART_DeInit(UART1);
            SPM_EnableModuleSleepACK(SLEEP_ACK_DMA0, DISABLE);  /* Before entering low-power mode, disable DMA acknowledgment to prevent low-power failure. */

            SysStop();                        /* Enter the Stop mode. */

            LED1_ON;                          /* Adjust potentiometer VR1 to trigger an interrupt and wake up the MCU. LED1 lights up. */
            InitDebug();
            EnableInterrupts;                 /* After waking up, perform necessary processing before enabling interrupts. */
            // SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;  /* Turn on the SysTick only when needed, otherwise, there's no need to activate it. */

            printf("Wakeup Now...\r\n");
        }
    }
}

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