/*******************************************************************************
*
*
* File:         demo_hal_pwr.c
* Description： pwr demo code
* Version：      V0.1
* Date：         2023-07-27
* Author：       jaya
 *******************************************************************************/

/*******************************************************************************
* 1.Included files
*******************************************************************************/
#include "stdarg.h"
#include "stdlib.h"
#include "stdio.h"
#include "test_cfg_global.h"
#include "hal_pwr.h"
#include "hal_gpio.h"
#include "tau_log.h"
#include "tau_delay.h"
#include "demo_hal_pwr.h"

#if _MODULE_DEMO_PWR_EN
/*******************************************************************************
* 2.Global constant and macro definitions using #define
*******************************************************************************/
#ifdef LOG_TAG
#undef LOG_TAG
#endif
#define LOG_TAG "demo_hal_pwr"

#define MAIN_POWER_SELECT              PWR_SEL_VCC           /* 主供电电源选择 */
#define SLEEP_MODE_POWER               PWR_SLEEP_IN_TP18     /* 息屏电源选择 */

/*******************************************************************************
* 3.Global function and Global variable declarations
*******************************************************************************/

/*******************************************************************************
* 4.Global variable extern declarations
*******************************************************************************/

/*******************************************************************************
* 5.Global function prototypes
*******************************************************************************/
static bool sg_system_resume = false;

static void ap_rstn_pull_high_cb(void *data)
{
    TAU_LOGD("AP RSTN !\n");
    /* system resume begin */
    sg_system_resume = true;
    /* 关闭AP reset检查     */
    hal_gpio_set_ap_reset_int(DISABLE, NULL, DETECT_HIGH_LVL);
}

/**
* @brief  normal sleep mode demo
* @param
* @retval bool true/false
*/
static bool demo_normal_sleep_mode()
{
    /* 主电源供电选择 */
    hal_pwr_set_main_power(MAIN_POWER_SELECT);

    if (MAIN_POWER_SELECT == PWR_SEL_VCC)
    {
        while (hal_pwr_get_vcc_power_ready() == false);
    }

    /* system 正常启动 */

    /* sustem suspend */
    /* 关闭外设/图像通路 */

    /* 息屏电源供电选择 */
    hal_pwr_set_sleep_mode_power(SLEEP_MODE_POWER);

    /* enter deep sleep mode */
    TAU_LOGD("enter stop mode now\n");

    /* normal sleep mode, MCU可以正常工作 */
    hal_gpio_set_ap_reset_int(ENABLE, ap_rstn_pull_high_cb, DETECT_RISING_EDGE);

    hal_pwr_enter_normal_sleep_mode();
    while (1)
    {
        /* normal sleep mode 下外设模块正常,打印正常,SWD通讯正常 */
        TAU_LOGD("wait ap rstn\n");
        if (sg_system_resume)
        {
            break;
        }
        delayMs(1000);
    }

    /* 退出sleep mode */
    hal_pwr_exit_sleep_mode();
    /* system resume */
    TAU_LOGD("system resum\n");
    return true;
}

/**
* @brief  stop sleep mode demo
* @param
* @retval bool true/false
*/
static bool demo_stop_sleep_mode()
{
    /* 主电源供电选择 */
    hal_pwr_set_main_power(MAIN_POWER_SELECT);

    if (MAIN_POWER_SELECT == PWR_SEL_VCC)
    {
        while (hal_pwr_get_vcc_power_ready() == false);
    }

    /* system 正常启动 */

    /* sustem suspend */
    /* 关闭外设/图像通路 */

    /* 息屏电源供电选择 */
    hal_pwr_set_sleep_mode_power(SLEEP_MODE_POWER);

    /* enter deep sleep mode */
    while (1)
    {

        TAU_LOGD("enter stop mode now\n");

        /* 等待打印完成 */
        delayMs(200); /* 实际使用不需要延时 */

        /* 配置唤醒AP RSTN 、SPIS CS、TD INT 唤醒*/
        hal_pwr_set_stop_sleep_wakeup_pin(IO_PAD_AP_RSTN, WUP_RISING_EDGE);
        hal_pwr_set_stop_sleep_wakeup_pin(IO_PAD_AP_SPIS_CSN, WUP_FALLING_EDGE);
        hal_pwr_set_stop_sleep_wakeup_pin(IO_PAD_TD_INT, WUP_FALLING_EDGE);
        delayMs(1000); /* 实际使用不需要延时 */
        /* 进入stop sleep mode, MCU停止运行,SWD无法通讯 */
        io_pad_e wakeup_io = hal_pwr_enter_stop_sleep_mode();

        TAU_LOGD("stop sleep mode wake up by io %d\n", wakeup_io);

        if (wakeup_io == IO_PAD_AP_RSTN)
        {
            TAU_LOGD("AP RSTN reset, system resume\n");
            break;
        }
        else
        {
            TAU_LOGD("Touch process\n");
            /* 处理touchu 事件 处理完成后继续进入sleep mode*/
        }
    }

    /* 退出sleep mode */
    hal_pwr_exit_sleep_mode();
    /* system resume */
    TAU_LOGD("system resum\n");
    return true;
}

/**
* @brief  deep sleep mode demo
* @param
* @retval bool true/false
*/
static bool demo_deep_sleep_mode()
{
    pwr_reset_flag_e reset_flag = hal_pwr_get_reset_flag();
    TAU_LOGD("wakeup flag %d\n", reset_flag);
    if (reset_flag == RF_TDINT_WAKEUP)
    {
        /* TD_INT reset, 处理触摸事件, 唤醒AP 等待AP RSTN*/
        TAU_LOGD("TD_INT reset\n");
    }
    /* 主电源供电选择 */
    hal_pwr_set_main_power(MAIN_POWER_SELECT);

    if (MAIN_POWER_SELECT == PWR_SEL_VCC)
    {
        while (hal_pwr_get_vcc_power_ready() == false);
    }

    /* system 正常启动 */

    /* enter deep sleep mode */
    TAU_LOGD("enter deep mode now\n");

    /* 息屏电源供电选择 */
    hal_pwr_set_sleep_mode_power(SLEEP_MODE_POWER);

    /* deep sleep mode,配置AP RSTN 上升沿,TD INT 下降沿唤醒 */
    hal_pwr_enter_deep_sleep_mode(WUP_RISING_EDGE, WUP_FALLING_EDGE);
    /* 数字完全掉电,SWD无法通讯,唤醒后芯片重启 */
    return true;
}

/**
* @brief  pwr demo case
* @param  none
* @retval none
*/
void demo_hal_pwr(void)
{
    TAU_LOGD("pwr dmeo \n");
    /* normal sleep mode demo */
    demo_normal_sleep_mode();
    /* stop sleep mode demo */
    demo_stop_sleep_mode();
    /* deep sleep mode demo */
    demo_deep_sleep_mode();
}
#endif
