/*
 * spv40xx_pmu.c
 *
 *  Created on: 2023年5月10日
 *      Author: mglsq
 */
#include "platform.h"
#include "hal_common.h"
#include "hal_pmu.h"


#define enable   1
#define disable  0

/**
* @brief        PMU电源检测配置
* @details      使能/关闭电源检测
* @param[in]    power_detect_s：pmu_power_detect_bmp_enum 中的项目（多项逻辑或）
* @param[in]    en：ENABLE/DISABLE
* @return       none
* @note         none
*/
void pmu_power_detect_config(pmu_power_detect_en_t pwr_det, uint32_t en)
{
    hal_assert(0 == (pwr_det & (~(
                                    PMU_POWER_DETECT_CPP    |
                                    PMU_POWER_DETECT_AVCC   |
                                    PMU_POWER_DETECT_DVDD   |
                                    PMU_POWER_DETECT_IOVCC))));
    hal_assert((en == enable) || (en == disable));

    if (en)
    {
        AON_PMU->PWR_CTL |= pwr_det;
    }
    else
    {
        AON_PMU->PWR_CTL &= ~pwr_det;
    }
}

/**
* @brief        获取PMU电源检测配置
* @details      获取电源检测的使能/关闭状态
* @param[in]    none
* @return       pmu_power_detect_bmp_enum 中的项目（多项逻辑或）
* @note         none
*/
uint32_t pmu_get_power_detect_config(void)
{
    return (AON_PMU->PWR_CTL & (
                PMU_POWER_DETECT_CPP    |
                PMU_POWER_DETECT_AVCC   |
                PMU_POWER_DETECT_DVDD   |
                PMU_POWER_DETECT_IOVCC));
}

/**
* @brief        PMU电源强制开启功能配置
* @details      使能/关闭电源强制开启功能
* @param[in]    power_force_on_s：pmu_power_force_on_bmp_enum 中的项目（多项逻辑或）
* @param[in]    en：ENABLE/DISABLE
* @return       none
* @note         电源强制开启功能决定PMU在STANDBY状态下的电源使能情况
*/
void pmu_power_force_on_config(pmu_power_force_en_t pwr_force, uint32_t en)
{
    hal_assert(0 == (pwr_force & (~(
                                      PMU_POWER_FORCE_ON_CPP  |
                                      PMU_POWER_FORCE_ON_AVCC |
                                      PMU_POWER_FORCE_ON_VDDQ |
                                      PMU_POWER_FORCE_ON_DVDD |
                                      PMU_POWER_FORCE_ON_IOVCC    |
                                      PMU_POWER_FORCE_ON_BIAS))));
    hal_assert((en == enable) || (en == disable));

    if (en)
    {
        AON_PMU->PWR_CTL |= pwr_force;
    }
    else
    {
        AON_PMU->PWR_CTL &= ~pwr_force;
    }
}

/**
* @brief        获取PMU电源强制开启功能配置
* @details      获取电源强制开启功能的使能/关闭状态
* @param[in]    none
* @return       pmu_power_force_on_bmp_enum 中的项目（多项逻辑或）
* @note         none
*/
uint32_t pmu_get_power_force_on_config(void)
{
    return (AON_PMU->PWR_CTL & (
                PMU_POWER_FORCE_ON_CPP  |
                PMU_POWER_FORCE_ON_AVCC |
                PMU_POWER_FORCE_ON_VDDQ |
                PMU_POWER_FORCE_ON_DVDD |
                PMU_POWER_FORCE_ON_IOVCC    |
                PMU_POWER_FORCE_ON_BIAS));
}

/**
* @brief        PMU电源平常开启功能配置
* @details      使能/关闭电源平常开启功能
* @param[in]    power_normal_on_s：pmu_power_normal_on_bmp_enum 中的项目（多项逻辑或）
* @param[in]    en：ENABLE/DISABLE
* @return       none
* @note         电源平常开启功能和电源强制开启功能共同决定PMU在ACTIVE状态下的电源使能情况
*/
void pmu_power_normal_on_config(pmu_power_normal_en_t pwr_normal, uint32_t en)
{
    hal_assert(0 == (pwr_normal & (~(
                                       PMU_POWER_NORMAL_ON_CPP |
                                       PMU_POWER_NORMAL_ON_AVCC    |
                                       PMU_POWER_NORMAL_ON_VDDQ    |
                                       PMU_POWER_NORMAL_ON_DVDD    |
                                       PMU_POWER_NORMAL_ON_IOVCC   |
                                       PMU_POWER_NORMAL_ON_BIAS))));
    hal_assert((en == enable) || (en == disable));

    if (en)
    {
        AON_PMU->PWR_CTL |= pwr_normal;
    }
    else
    {
        AON_PMU->PWR_CTL &= ~pwr_normal;
    }
}

/**
* @brief        获取PMU电源平常开启功能配置
* @details      获取电源平常开启功能的使能/关闭状态
* @param[in]    none
* @return       pmu_power_normal_on_bmp_enum 中的项目（多项逻辑或）
* @note         none
*/
uint32_t pmu_get_power_normal_on_config(void)
{
    return (AON_PMU->PWR_CTL & (
                PMU_POWER_NORMAL_ON_CPP |
                PMU_POWER_NORMAL_ON_AVCC    |
                PMU_POWER_NORMAL_ON_VDDQ    |
                PMU_POWER_NORMAL_ON_DVDD    |
                PMU_POWER_NORMAL_ON_IOVCC   |
                PMU_POWER_NORMAL_ON_BIAS));
}

/**
* @brief        PMU状态配置
* @details      配置PMU状态
* @param[in]    pmu_state：pmu_state_enum 中的项目之一
* @param[in]    pmu_restart_time：pmu_restart_time_enum 中的项目之一
* @return       无
* @note         只有当pmu_state为PMU_STATE_RESTART时，pmu_restart_time才有效
*/
void pmu_state_config(pmu_state_set_t pwr_state)
{
    hal_assert(
        (pwr_state == PMU_STATE_ACTIVE) ||
        (pwr_state == PMU_STATE_RESTART) ||
        (pwr_state == PMU_STATE_STANDBY)    ||
        (pwr_state == PMU_STATE_HIBERNATE));
    uint32_t pmu_pwr_ctl = AON_PMU->PWR_CTL;
    pmu_pwr_ctl &= ~(AON_PMU_PWR_CTL_STATE_MASK);
    pmu_pwr_ctl |= pwr_state;
    AON_PMU->PWR_CTL = pmu_pwr_ctl;
}

/**
* @brief        PMU重启
* @details      配置PMU状态
* @param[in]    pmu_restart_time：pmu_restart_time_enum 中的项目之一
* @return       无
* @note         只有当pmu_state为PMU_STATE_RESTART时，pmu_restart_time才有效
*/
void pmu_state_restart(pmu_restart_time_t pwr_restart_time)
{
    hal_assert(
        (pwr_restart_time == PMU_RESTART_TIME_100MS)    ||
        (pwr_restart_time == PMU_RESTART_TIME_250MS)    ||
        (pwr_restart_time == PMU_RESTART_TIME_500MS)    ||
        (pwr_restart_time == PMU_RESTART_TIME_1S));
    uint32_t pmu_pwr_ctl = AON_PMU->PWR_CTL;
    pmu_pwr_ctl &= ~(AON_PMU_PWR_CTL_STATE_MASK | AON_PMU_PWR_CTL_RESTART_TIME_MASK);
    pmu_pwr_ctl |= PMU_STATE_RESTART | pwr_restart_time;
    AON_PMU->PWR_CTL = pmu_pwr_ctl;
}

/**
* @brief        PMU AVDD电压配置
* @details      PMU AVDD电压配置
* @param[in]    pmu_avdd_vol：pmu_avdd_vol_enum 中的项目之一
* @return       无
* @note         无
*/
void pmu_avdd_voltage_config(pmu_avdd_vol_t pwr_avdd_vol)
{
    hal_assert(
        (pwr_avdd_vol == PMU_AVDD_VOL_1_05V)    ||
        (pwr_avdd_vol == PMU_AVDD_VOL_1_10V)    ||
        (pwr_avdd_vol == PMU_AVDD_VOL_1_15V)    ||
        (pwr_avdd_vol == PMU_AVDD_VOL_1_20V));
    uint32_t pmu_vol_ctl = AON_PMU->VOL_CTL;
    pmu_vol_ctl &= ~AON_PMU_VOL_CTL_AVDD_VOL_MASK;
    pmu_vol_ctl |= pwr_avdd_vol;
    AON_PMU->VOL_CTL = pmu_vol_ctl;
}

/**
* @brief        获取PMU AVDD电压配置
* @details      获取PMU AVDD电压配置
* @param[in]    none
* @return       pmu_avdd_vol_enum 中的项目之一
* @note         none
*/
pmu_avdd_vol_t pmu_get_avdd_voltage_config(void)
{
    return (AON_PMU->VOL_CTL & AON_PMU_VOL_CTL_AVDD_VOL_MASK);
}

/**
* @brief        PMU CPP电压配置
* @details      PMU CPP电压配置
* @param[in]    pmu_cpp_vol：pmu_cpp_vol_enum 中的项目之一
* @return       无
* @note         无
*/
void pmu_cpp_voltage_config(pmu_cpp_vol_t pwr_cpp_vol)
{
    hal_assert(
        (pwr_cpp_vol == PMU_CPP_VOL_1_70V)  ||
        (pwr_cpp_vol == PMU_CPP_VOL_1_75V)  ||
        (pwr_cpp_vol == PMU_CPP_VOL_1_80V)  ||
        (pwr_cpp_vol == PMU_CPP_VOL_1_85V));
    uint32_t pmu_vol_ctl = AON_PMU->VOL_CTL;
    pmu_vol_ctl &= ~AON_PMU_VOL_CTL_CPP_VOL_MASK;
    pmu_vol_ctl |= pwr_cpp_vol;
    AON_PMU->VOL_CTL = pmu_vol_ctl;
}

/**
* @brief        获取PMU CPP电压配置
* @details      获取PMU CPP电压配置
* @param[in]    none
* @return       pmu_cpp_vol_enum 中的项目之一
* @note         none
*/
pmu_cpp_vol_t pmu_get_cpp_voltage_config(void)
{
    return (AON_PMU->VOL_CTL & AON_PMU_VOL_CTL_CPP_VOL_MASK);
}

/**
* @brief        PMU IOVCC电压配置
* @details      PMU IOVCC电压配置
* @param[in]    pmu_iovcc_vol：pmu_iovcc_vol_enum 中的项目之一
* @return       无
* @note         无
*/
void pmu_iovcc_voltage_config(pmu_iovcc_vol_t pwr_iovcc_vol)
{
    hal_assert(
        (pwr_iovcc_vol == PMU_IOVCC_VOL_1_8V)   ||
        (pwr_iovcc_vol == PMU_IOVCC_VOL_1_9V)   ||
        (pwr_iovcc_vol == PMU_IOVCC_VOL_2_0V)   ||
        (pwr_iovcc_vol == PMU_IOVCC_VOL_2_1V)   ||
        (pwr_iovcc_vol == PMU_IOVCC_VOL_2_2V)   ||
        (pwr_iovcc_vol == PMU_IOVCC_VOL_2_3V)   ||
        (pwr_iovcc_vol == PMU_IOVCC_VOL_2_4V)   ||
        (pwr_iovcc_vol == PMU_IOVCC_VOL_2_5V)   ||
        (pwr_iovcc_vol == PMU_IOVCC_VOL_2_6V)   ||
        (pwr_iovcc_vol == PMU_IOVCC_VOL_2_7V)   ||
        (pwr_iovcc_vol == PMU_IOVCC_VOL_2_8V)   ||
        (pwr_iovcc_vol == PMU_IOVCC_VOL_2_9V)   ||
        (pwr_iovcc_vol == PMU_IOVCC_VOL_3_0V)   ||
        (pwr_iovcc_vol == PMU_IOVCC_VOL_3_1V)   ||
        (pwr_iovcc_vol == PMU_IOVCC_VOL_3_2V)   ||
        (pwr_iovcc_vol == PMU_IOVCC_VOL_3_3V));
    uint32_t pmu_vol_ctl = AON_PMU->VOL_CTL;
    pmu_vol_ctl &= ~AON_PMU_VOL_CTL_IOVCC_VOL_MASK;
    pmu_vol_ctl |= pwr_iovcc_vol;
    AON_PMU->VOL_CTL = pmu_vol_ctl;
}

/**
* @brief        获取PMU IOVCC电压配置
* @details      获取PMU IOVCC电压配置
* @param[in]    none
* @return       pmu_iovcc_vol_enum 中的项目之一
* @note         none
*/
pmu_iovcc_vol_t pmu_get_iovcc_voltage_config(void)
{
    return (AON_PMU->VOL_CTL & AON_PMU_VOL_CTL_IOVCC_VOL_MASK);
}

/**
* @brief        PMU AVCC电压配置
* @details      PMU AVCC电压配置
* @param[in]    pmu_avcc_vol：pmu_avcc_vol_enum 中的项目之一
* @return       无
* @note         无
*/
void pmu_avcc_voltage_config(pmu_avcc_vol_t pwr_avcc_vol)
{
    hal_assert(
        (pwr_avcc_vol == PMU_AVCC_VOL_2_7V) ||
        (pwr_avcc_vol == PMU_AVCC_VOL_2_8V) ||
        (pwr_avcc_vol == PMU_AVCC_VOL_2_9V) ||
        (pwr_avcc_vol == PMU_AVCC_VOL_3_0V) ||
        (pwr_avcc_vol == PMU_AVCC_VOL_3_1V) ||
        (pwr_avcc_vol == PMU_AVCC_VOL_3_2V) ||
        (pwr_avcc_vol == PMU_AVCC_VOL_3_3V) ||
        (pwr_avcc_vol == PMU_AVCC_VOL_3_4V));
    uint32_t pmu_vol_ctl = AON_PMU->VOL_CTL;
    pmu_vol_ctl &= ~AON_PMU_VOL_CTL_AVCC_VOL_MASK;
    pmu_vol_ctl |= pwr_avcc_vol;
    AON_PMU->VOL_CTL = pmu_vol_ctl;
}

/**
* @brief        获取PMU AVCC电压配置
* @details      获取PMU AVCC电压配置
* @param[in]    none
* @return       pmu_avcc_vol_enum 中的项目之一
* @note         none
*/
pmu_avcc_vol_t pmu_get_avcc_voltage_config(void)
{
    return (AON_PMU->VOL_CTL & AON_PMU_VOL_CTL_AVCC_VOL_MASK);
}

/**
* @brief        PMU DVDD电压配置
* @details      PMU DVDD电压配置
* @param[in]    pmu_dvdd_vol：pmu_dvdd_vol_enum 中的项目之一
* @return       无
* @note         无
*/
void pmu_dvdd_voltage_config(pmu_dvdd_vol_t pwr_dvdd_vol)
{
    hal_assert(
        (pwr_dvdd_vol == PMU_DVDD_VOL_0_7V) ||
        (pwr_dvdd_vol == PMU_DVDD_VOL_0_8V) ||
        (pwr_dvdd_vol == PMU_DVDD_VOL_0_9V) ||
        (pwr_dvdd_vol == PMU_DVDD_VOL_1_0V) ||
        (pwr_dvdd_vol == PMU_DVDD_VOL_1_1V) ||
        (pwr_dvdd_vol == PMU_DVDD_VOL_1_2V) ||
        (pwr_dvdd_vol == PMU_DVDD_VOL_1_3V) ||
        (pwr_dvdd_vol == PMU_DVDD_VOL_1_4V));
    uint32_t pmu_vol_ctl = AON_PMU->VOL_CTL;
    pmu_vol_ctl &= ~AON_PMU_VOL_CTL_DVDD_VOL_MASK;
    pmu_vol_ctl |= pwr_dvdd_vol;
    AON_PMU->VOL_CTL = pmu_vol_ctl;
}

/**
* @brief        获取PMU DVDD电压配置
* @details      获取PMU DVDD电压配置
* @param[in]    none
* @return       pmu_dvdd_vol_enum 中的项目之一
* @note         none
*/
pmu_dvdd_vol_t pmu_get_dvdd_voltage_config(void)
{
    return (AON_PMU->VOL_CTL & AON_PMU_VOL_CTL_DVDD_VOL_MASK);
}

/**
* @brief        PMU SVDD电压配置
* @details      PMU SVDD电压配置
* @param[in]    pmu_svdd_vol：pmu_svdd_vol_enum 中的项目之一
* @return       无
* @note         无
*/
void pmu_svdd_voltage_config(pmu_svdd_vol_t pwr_svdd_vol)
{
    hal_assert(
        (pwr_svdd_vol == PMU_SVDD_VOL_1_05V)    ||
        (pwr_svdd_vol == PMU_SVDD_VOL_1_10V)    ||
        (pwr_svdd_vol == PMU_SVDD_VOL_1_15V)    ||
        (pwr_svdd_vol == PMU_SVDD_VOL_1_20V));
    uint32_t pmu_vol_ctl = AON_PMU->VOL_CTL;
    pmu_vol_ctl &= ~AON_PMU_VOL_CTL_SVDD_VOL_MASK;
    pmu_vol_ctl |= pwr_svdd_vol;
    AON_PMU->VOL_CTL = pmu_vol_ctl;
}

/**
* @brief        获取PMU SVDD电压配置
* @details      获取PMU SVDD电压配置
* @param[in]    none
* @return       pmu_svdd_vol_enum 中的项目之一
* @note         none
*/
pmu_svdd_vol_t pmu_get_svdd_voltage_config(void)
{
    return (AON_PMU->VOL_CTL & AON_PMU_VOL_CTL_SVDD_VOL_MASK);
}

/**
* @brief        PMU SVCC电压配置
* @details      PMU SVCC电压配置
* @param[in]    pmu_svcc_vol：pmu_svcc_vol_enum 中的项目之一
* @return       无
* @note         无
*/
void pmu_svcc_voltage_config(pmu_svcc_vol_t pwr_svcc_vol)
{
    hal_assert(
        (pwr_svcc_vol == PMU_SVCC_VOL_2_7V) ||
        (pwr_svcc_vol == PMU_SVCC_VOL_2_8V) ||
        (pwr_svcc_vol == PMU_SVCC_VOL_2_9V) ||
        (pwr_svcc_vol == PMU_SVCC_VOL_3_0V) ||
        (pwr_svcc_vol == PMU_SVCC_VOL_3_1V) ||
        (pwr_svcc_vol == PMU_SVCC_VOL_3_2V) ||
        (pwr_svcc_vol == PMU_SVCC_VOL_3_3V) ||
        (pwr_svcc_vol == PMU_SVCC_VOL_3_4V));
    uint32_t pmu_vol_ctl = AON_PMU->VOL_CTL;
    pmu_vol_ctl &= ~AON_PMU_VOL_CTL_SVCC_VOL_MASK;
    pmu_vol_ctl |= pwr_svcc_vol;
    AON_PMU->VOL_CTL = pmu_vol_ctl;
}

/**
* @brief        获取PMU SVCC电压配置
* @details      获取PMU SVCC电压配置
* @param[in]    none
* @return       pmu_svcc_vol_enum 中的项目之一
* @note         none
*/
pmu_svcc_vol_t pmu_get_svcc_voltage_config(void)
{
    return (AON_PMU->VOL_CTL & AON_PMU_VOL_CTL_SVCC_VOL_MASK);
}

/**
* @brief        PMU时钟保持配置
* @details      使能/关闭时钟保持
* @param[in]    pmu_clk_keep_s：pmu_clk_keep_enum 中的项目（多项逻辑或）
* @param[in]    en：ENABLE/DISABLE
* @return       无
* @note         无
*/
void pmu_clk_keep_config(uint32_t pwr_clk_keep_s, uint32_t en)
{
    hal_assert(0 == (pwr_clk_keep_s & (~(
                                           PMU_CLK_KEEP_PLL_AUDIO  |
                                           PMU_CLK_KEEP_PLL_CORE   |
                                           PMU_CLK_KEEP_OSC_XTAL   |
                                           PMU_CLK_KEEP_OSC_DEV))));
    hal_assert((en == enable) || (en == disable));

    if (en)
    {
        AON_PMU->CLK_CTL |= pwr_clk_keep_s;
    }
    else
    {
        AON_PMU->CLK_CTL &= ~pwr_clk_keep_s;
    }
}

/**
* @brief        获取PMU时钟保持配置
* @details      获取时钟保持的使能/关闭状态
* @param[in]    none
* @return       pmu_clk_keep_enum 中的项目（多项逻辑或）
* @note         none
*/
uint32_t pmu_get_ckl_keep_config(void)
{
    return AON_PMU->CLK_CTL;
}

/**
* @brief        PMU ONOFF边沿配置
* @details      PMU ONOFF边沿配置
* @param[in]    pmu_onoff_edge：pmu_onoff_edge_enum 中的项目之一
* @return       none
* @note         none
*/
void pmu_onoff_edge_config(pmu_onoff_edge_t pwr_onoff_edge)
{
    hal_assert(
        (pwr_onoff_edge == PMU_ONOFF_EDGE_NONE) ||
        (pwr_onoff_edge == PMU_ONOFF_EDGE_RISE) ||
        (pwr_onoff_edge == PMU_ONOFF_EDGE_FALL) ||
        (pwr_onoff_edge == PMU_ONOFF_EDGE_BOTH) ||
        (pwr_onoff_edge == PMU_ONOFF_EDGE_HIGH) ||
        (pwr_onoff_edge == PMU_ONOFF_EDGE_LOW));
    uint32_t pmu_onoff = AON_PMU->ONOFF;
    pmu_onoff &= ~AON_PMU_ONOFF_ONOFF_EDGE_MASK;
    pmu_onoff |= pwr_onoff_edge;
    AON_PMU->ONOFF = pmu_onoff;
}

/**
* @brief        获取PMU ONOFF边沿配置
* @details      获取PMU ONOFF边沿配置
* @param[in]    none
* @return       pmu_onoff_edge_enum 中的项目之一
* @note         none
*/
pmu_onoff_edge_t pmu_get_onoff_edge_config(void)
{
    return (AON_PMU->ONOFF & AON_PMU_ONOFF_ONOFF_EDGE_MASK);
}

/**
* @brief        PMU GPIO作为ONOFF功能配置
* @details      使能/关闭GPIO作为ONOFF功能
* @param[in]    en：ENABLE/DISABLE
* @return       none
* @note         none
*/
void pmu_gpio_to_onoff_enable(uint32_t en)
{
    hal_assert((en == enable) || (en == disable));
    AON_PMU->ONOFF_f.GPIO_SEL = en;
}

/**
* @brief        获取PMU GPIO作为ONOFF功能的配置
* @details      获取GPIO作为ONOFF功能的使能/关闭状态
* @param[in]    none
* @return       ENABLE/DISABLE
* @note         none
*/
uint32_t pmu_is_gpio_onoff_en(void)
{
    return AON_PMU->ONOFF_f.GPIO_SEL;
}

/**
* @brief        PMU ONOFF引脚的ONOFF功能配置
* @details      使能/关闭ONOFF引脚的ONOFF功能
* @param[in]    en：ENABLE/DISABLE
* @return       none
* @note         none
*/
void pmu_onoff_pin_enable(uint32_t en)
{
    hal_assert((en == enable) || (en == disable));
    AON_PMU->ONOFF_f.ONOFF_SEL = en;
}

/**
* @brief        获取PMU ONOFF引脚的ONOFF功能的配置
* @details      获取ONOFF引脚的ONOFF功能的使能/关闭状态
* @param[in]    none
* @return       ENABLE/DISABLE
* @note         none
*/
uint32_t pmu_is_onoff_pin_en(void)
{
    return AON_PMU->ONOFF_f.ONOFF_SEL;
}


/**
* @brief        PMU ONOFF长按时间配置
* @details      PMU ONOFF长按时间配置
* @param[in]    pmu_onoff_long_time：pmu_onoff_long_time_enum 中的项目之一
* @return       none
* @note         none
*/
void pmu_onoff_long_time_config(pmu_onoff_long_time_t pwr_onoff_long_time)
{
    hal_assert(
        (pwr_onoff_long_time == PMU_ONOFF_LONG_TIME_4S) ||
        (pwr_onoff_long_time == PMU_ONOFF_LONG_TIME_8S) ||
        (pwr_onoff_long_time == PMU_ONOFF_LONG_TIME_12S)    ||
        (pwr_onoff_long_time == PMU_ONOFF_LONG_TIME_16S));
    uint32_t pmu_onoff = AON_PMU->ONOFF;
    pmu_onoff &= ~AON_PMU_ONOFF_ONOFF_LONG_TIME_MASK;
    pmu_onoff |= pwr_onoff_long_time;
    AON_PMU->ONOFF = pmu_onoff;
}

/**
* @brief        获取PMU ONOFF长按时间配置
* @details      获取PMU ONOFF长按时间配置
* @param[in]    none
* @return       pmu_onoff_long_time_enum 中的项目之一
* @note         none
*/
pmu_onoff_long_time_t pmu_get_onoff_long_time_config(void)
{
    return (AON_PMU->ONOFF & AON_PMU_ONOFF_ONOFF_LONG_TIME_MASK);
}

/**
* @brief        PMU ONOFF超长按时间配置
* @details      PMU ONOFF超长按时间配置
* @param[in]    pmu_onoff_ulong_time：pmu_onoff_ulong_time_enum 中的项目之一
* @return       none
* @note         none
*/
void pmu_onoff_ulong_time_config(pmu_onoff_ulong_time_t pwr_onoff_ulong_time)
{
    hal_assert(
        (pwr_onoff_ulong_time == PMU_ONOFF_ULONG_TIME_12S)  ||
        (pwr_onoff_ulong_time == PMU_ONOFF_ULONG_TIME_24S));
    uint32_t pmu_onoff = AON_PMU->ONOFF;
    pmu_onoff &= ~AON_PMU_ONOFF_ONOFF_ULONG_TIME_MASK;
    pmu_onoff |= pwr_onoff_ulong_time;
    AON_PMU->ONOFF = pmu_onoff;
}

/**
* @brief        获取PMU ONOFF超长按时间配置
* @details      获取PMU ONOFF超长按时间配置
* @param[in]    none
* @return       pmu_onoff_ulong_time_enum 中的项目之一
* @note         none
*/
pmu_onoff_ulong_time_t pmu_get_onoff_ulong_time_config(void)
{
    return (AON_PMU->ONOFF & AON_PMU_ONOFF_ONOFF_ULONG_TIME_MASK);
}

/**
* @brief        PMU ONOFF短按时间配置
* @details      PMU ONOFF短按时间配置
* @param[in]    pmu_onoff_short_time：pmu_onoff_short_time_enum 中的项目之一
* @return       none
* @note         none
*/
void pmu_onoff_short_time_config(pmu_onoff_short_time_t pwr_onoff_short_time)
{
    hal_assert(
        (pwr_onoff_short_time == PMU_ONOFF_SHORT_TIME_125MS)    ||
        (pwr_onoff_short_time == PMU_ONOFF_SHORT_TIME_250MS)    ||
        (pwr_onoff_short_time == PMU_ONOFF_SHORT_TIME_500MS)    ||
        (pwr_onoff_short_time == PMU_ONOFF_SHORT_TIME_1S)   ||
        (pwr_onoff_short_time == PMU_ONOFF_SHORT_TIME_1_5S) ||
        (pwr_onoff_short_time == PMU_ONOFF_SHORT_TIME_2S)   ||
        (pwr_onoff_short_time == PMU_ONOFF_SHORT_TIME_3S)   ||
        (pwr_onoff_short_time == PMU_ONOFF_SHORT_TIME_4S));
    uint32_t pmu_onoff = AON_PMU->ONOFF;
    pmu_onoff &= ~AON_PMU_ONOFF_ONOFF_SHORT_TIME_MASK;
    pmu_onoff |= pwr_onoff_short_time;
    AON_PMU->ONOFF = pmu_onoff;
}

/**
* @brief        获取PMU ONOFF短按时间配置
* @details      获取PMU ONOFF短按时间配置
* @param[in]    none
* @return       pmu_onoff_short_time_enum 中的项目之一
* @note         none
*/
pmu_onoff_short_time_t pmu_get_onoff_short_time_config(void)
{
    return (AON_PMU->ONOFF & AON_PMU_ONOFF_ONOFF_SHORT_TIME_MASK);
}

/**
* @brief        PMU ONOFF防抖时间配置
* @details      PMU ONOFF防抖时间配置
* @param[in]    pmu_onoff_deb_time：pmu_onoff_deb_time_enum 中的项目之一
* @return       none
* @note         none
*/
void pmu_onoff_debounce_time_config(pmu_onoff_deb_time_t pwr_onoff_deb_time)
{
    hal_assert(
        (pwr_onoff_deb_time == PMU_ONOFF_DEB_TIME_25MS) ||
        (pwr_onoff_deb_time == PMU_ONOFF_DEB_TIME_50MS) ||
        (pwr_onoff_deb_time == PMU_ONOFF_DEB_TIME_75MS) ||
        (pwr_onoff_deb_time == PMU_ONOFF_DEB_TIME_100MS));
    uint32_t pmu_onoff = AON_PMU->ONOFF;
    pmu_onoff &= ~AON_PMU_ONOFF_ONOFF_DEB_MASK;
    pmu_onoff |= pwr_onoff_deb_time;
    AON_PMU->ONOFF = pmu_onoff;
}

/**
* @brief        获取PMU ONOFF防抖时间配置
* @details      获取PMU ONOFF防抖时间配置
* @param[in]    none
* @return       pmu_onoff_deb_time_enum 中的项目之一
* @note         none
*/
pmu_onoff_short_time_t pmu_get_onoff_debounce_time_config(void)
{
    return (AON_PMU->ONOFF & AON_PMU_ONOFF_ONOFF_DEB_MASK);
}

/**
* @brief        PMU ONOFF的BOOT功能配置
* @details      使能/关闭ONOFF的BOOT功能
* @param[in]    en：ENABLE/DISABLE
* @return       none
* @note         使能后，ONOFF的超长按将触发切换引导flash
*/
void pmu_onoff_boot_enable(uint32_t en)
{
    hal_assert((en == enable) || (en == disable));
    AON_PMU->ONOFF_f.ONOFF_BOOT = en;
}

/**
* @brief        获取PMU ONOFF的BOOT功能配置
* @details      获取ONOFF的BOOT功能的使能/关闭状态
* @param[in]    none
* @return       ENABLE/DISABLE
* @note         none
*/
uint32_t pmu_is_onoff_boot_en(void)
{
    return AON_PMU->ONOFF_f.ONOFF_BOOT;
}

/**
* @brief        获取PMU ONOFF防抖后的按下状态
* @details      获取PMU ONOFF防抖后的按下状态
* @param[in]    none
* @return       pmu_onoff_state_enum 中的项目之一
* @note         none
*/
pmu_onoff_in_state_t pmu_get_onoff_debounce_status(void)
{
    return AON_PMU->ONOFF_f.ONOFF_DEB;
}

/**
* @brief        获取PMU 电源状态
* @details      获取PMU 电源状态
* @param[in]    none
* @return       pmu_power_state_enum 中的项目之一
* @note         用于区分系统是上电启动还是ONOFF唤醒
*/
pmu_power_on_status_t pmu_get_power_status(void)
{
    hal_log_info("onoff reg: 0x%08x\n", AON_PMU->ONOFF);
    return AON_PMU->ONOFF_f.PWR_STA;
}


/**
* @brief        获取PMU 各路电源的状态
* @details      获取PMU 各路电源的状态
* @param[in]    none
* @return       pmu_power_ok_bmp_enum 中的项目（多项逻辑或）
* @note         none
*/
uint32_t pmu_get_power_ok_status(void)
{
    return AON_PMU->PWR_STA;
}

/**
* @brief        PMU 中断使能配置
* @details      PMU 中断使能配置
* @param[in]    pmu_it_s：pmu_it_bmp_enum 中的项目（多项逻辑或）
* @param[in]    en：ENABLE/DISABLE
* @return       none
* @note         none
*/
void pmu_interrupt_enable_config(pmu_interrupt_en_t pwr_irq)
{
    hal_assert(0 == (pwr_irq & (~(
                                    PMU_IT_ONOFF_EDGE   |
                                    PMU_IT_ONOFF_XLONG  |
                                    PMU_IT_ONOFF_ULONG  |
                                    PMU_IT_ONOFF_LONG   |
                                    PMU_IT_ONOFF_SHORT))));
    AON_PMU->IE |= pwr_irq;
}

//PMU 中断使能禁止配置
void pmu_interrupt_disable_config(pmu_interrupt_en_t pwr_irq)
{
    hal_assert(0 == (pwr_irq & (~(
                                    PMU_IT_ONOFF_EDGE   |
                                    PMU_IT_ONOFF_XLONG  |
                                    PMU_IT_ONOFF_ULONG  |
                                    PMU_IT_ONOFF_LONG   |
                                    PMU_IT_ONOFF_SHORT))));
    AON_PMU->IE &= ~pwr_irq;
}
/**
* @brief        获取PMU 中断使能配置
* @details      获取PMU 中断使能配置
* @param[in]    none
* @return       pmu_it_bmp_enum 中的项目（多项逻辑或）
* @note         none
*/
uint32_t pmu_get_interrupt_enable_config(void)
{
    return AON_PMU->IE;
}

/**
* @brief        获取PMU 中断Pending寄存器数据
* @details      获取PMU 中断Pending寄存器数据
* @param[in]    none
* @return       pmu_it_bmp_enum 中的项目（多项逻辑或）
* @note         none
*/
uint32_t pmu_get_interrupt_pending(void)
{
    return AON_PMU->PD;
}

/**
* @brief        获取PMU 特定中断Pending位的状态
* @details      获取PMU 特定中断Pending位的状态
* @param[in]    pmu_it：pmu_it_bmp_enum 中的项目之一
* @return       SET/RESET
* @note         none
*/
uint32_t pmu_is_interrupt_pending(pmu_pending_flag_t pwr_irq)
{
    hal_assert(
        (pwr_irq == PMU_ONOFF_EDGE_PENDING) ||
        (pwr_irq == PMU_ONOFF_XLONG_PENDING) ||
        (pwr_irq == PMU_ONOFF_ULONG_PENDING) ||
        (pwr_irq == PMU_ONOFF_LONG_PENDING) ||
        (pwr_irq == PMU_ONOFF_SHORT_PENDING));
    return (AON_PMU->PD & pwr_irq ? SET : RESET);
}

/**
* @brief        获取PMU 特定中断是否有效
* @details      获取PMU 特定中断是否有效
* @param[in]    pmu_it：pmu_it_bmp_enum 中的项目之一
* @return       SET/RESET
* @note         当中断使能位有效且Pending位有效时，认为中断有效
*/
uint32_t pmu_is_interrupt_valid(pmu_pending_flag_t pwr_irq)
{
    hal_assert(
        (pwr_irq == PMU_SPIO_PENDING) ||
        (pwr_irq == PMU_ONOFF_EDGE_PENDING) ||
        (pwr_irq == PMU_ONOFF_XLONG_PENDING) ||
        (pwr_irq == PMU_ONOFF_ULONG_PENDING) ||
        (pwr_irq == PMU_ONOFF_LONG_PENDING) ||
        (pwr_irq == PMU_ONOFF_SHORT_PENDING));
    return (AON_PMU->IE & AON_PMU->PD & pwr_irq ? SET : RESET);
}

/**
* @brief        清除PMU 中断Pending位
* @details      清除PMU 中断Pending位
* @param[in]    pmu_it_s：pmu_it_bmp_enum 中的项目（多项逻辑或）
* @return       none
* @note         none
*/
void pmu_clear_interrupt_pending(uint32_t pwr_irq)
{
    hal_assert(
        (pwr_irq == PMU_SPIO_PENDING) ||
        (pwr_irq == PMU_ONOFF_EDGE_PENDING) ||
        (pwr_irq == PMU_ONOFF_XLONG_PENDING) ||
        (pwr_irq == PMU_ONOFF_ULONG_PENDING) ||
        (pwr_irq == PMU_ONOFF_LONG_PENDING) ||
        (pwr_irq == PMU_ONOFF_SHORT_PENDING));
    AON_PMU->PD = pwr_irq;
}

/**
* @brief        PMU 唤醒功能配置
* @details      PMU 唤醒功能配置
* @param[in]    pmu_wakeup_s：pmu_wakeup_bmp_enum 中的项目（多项逻辑或）
* @param[in]    en：ENABLE/DISABLE
* @return       none
* @note         none
*/
void pmu_wakeup_config(pmu_wakeup_en_t pwr_wakeup, uint32_t en)
{
    hal_assert(0 == (pwr_wakeup & (~(
                                       PMU_WAKEUP_SPIO_INT  |
                                       PMU_WAKEUP_ONOFF_EDGE   |
                                       PMU_WAKEUP_ONOFF_XLONG  |
                                       PMU_WAKEUP_ONOFF_ULONG  |
                                       PMU_WAKEUP_ONOFF_LONG   |
                                       PMU_WAKEUP_ONOFF_SHORT))));
    hal_assert((en == enable) || (en == disable));

    if (en)
    {
        AON_PMU->WKEN |= pwr_wakeup;
    }
    else
    {
        AON_PMU->WKEN &= ~pwr_wakeup;
    }
}

/**
* @brief        获取PMU 唤醒功能配置
* @details      获取PMU 唤醒功能配置
* @param[in]    none
* @return       pmu_wakeup_bmp_enum 中的项目（多项逻辑或）
* @note         none
*/
uint32_t pmu_get_wakeup_config(void)
{
    return AON_PMU->WKEN;
}

/**
* @brief        PMU RESTART功能配置
* @details      PMU RESTART功能配置
* @param[in]    pmu_restart_s：pmu_restart_bmp_enum 中的项目（多项逻辑或）
* @param[in]    en：ENABLE/DISABLE
* @return       none
* @note         none
*/
void pmu_restart_config(pmu_restart_en_t pwr_restart_s, uint32_t en)
{
    hal_assert(0 == (pwr_restart_s & (~(
                                          PMU_RESTART_SVDD    |
                                          PMU_RESTART_XLONG   |
                                          PMU_RESTART_ULONG   |
                                          PMU_RESTART_LONG    |
                                          PMU_RESTART_SHORT))));
    hal_assert((en == enable) || (en == disable));

    if (en)
    {
        AON_PMU->RSTEN |= pwr_restart_s;
    }
    else
    {
        AON_PMU->RSTEN &= ~pwr_restart_s;
    }
}

/**
* @brief        获取PMU RESTART功能配置
* @details      获取PMU RESTART功能配置
* @param[in]    none
* @return       pmu_restart_bmp_enum 中的项目（多项逻辑或）
* @note         none
*/
uint32_t pmu_get_restart_config(void)
{
    return (AON_PMU->RSTEN & (
                PMU_RESTART_SVDD    |
                PMU_RESTART_XLONG   |
                PMU_RESTART_ULONG   |
                PMU_RESTART_LONG    |
                PMU_RESTART_SHORT));
}


/**
* @brief        PMU RESET功能配置
* @details      PMU RESET功能配置
* @param[in]    pmu_reset_s：pmu_reset_bmp_enum 中的项目（多项逻辑或）
* @param[in]    en：ENABLE/DISABLE
* @return       none
* @note         none
*/
void pmu_reset_config(pmu_reset_en_t pwr_reset_s, uint32_t en)
{
    hal_assert(0 == (pwr_reset_s & (~(
                                        PMU_RESET_SVDD  |
                                        PMU_RESET_XLONG |
                                        PMU_RESET_ULONG |
                                        PMU_RESET_LONG  |
                                        PMU_RESET_SHORT))));
    hal_assert((en == enable) || (en == disable));

    if (en)
    {
        AON_PMU->RSTEN |= pwr_reset_s;
    }
    else
    {
        AON_PMU->RSTEN &= ~pwr_reset_s;
    }
}

/**
* @brief        获取PMU RESET功能配置
* @details      获取PMU RESET功能配置
* @param[in]    none
* @return       pmu_reset_bmp_enum 中的项目（多项逻辑或）
* @note         none
*/
uint32_t pmu_get_reset_config(void)
{
    return (AON_PMU->RSTEN & (
                PMU_RESET_SVDD  |
                PMU_RESET_XLONG |
                PMU_RESET_ULONG |
                PMU_RESET_LONG  |
                PMU_RESET_SHORT));
}

