/**
 * @file exo_ahw_al_psm_common.c
 *
 * @brief This file contains abstracted driver function for power sense monitor
 *
 * @copyright Copyright 2023 Antaris, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include "exo_ahw_al_psm_common.h"
#include "exo_ahw_al_psm_wrapper.h"
#include "exo_ah_iob_csn.h"
#include "exo_ahdlsd.h"
#include <stdlib.h>
#include "logger.h"
#include "err_handler.h"

/**
 * @brief  API to initialize the power sense monitor.
 */
hal_ret_sts ahw_al_psm_init(ahw_al_psm_hdle* hpsm_arg, ahw_inst_id_t ahw_id )
{
#ifdef LINUX_TEMP_PORT
    DEBUG_CPRINT(("\n EXO AH-AL POWER SENSE MONITOR Initialize"));
#endif
    hal_ret_sts sts;
    hpsm_arg->ahw_gen_info.ahw_inst_id =ahw_id;
    if(HAL_SCS ==ahw_al_psm_common_init(hpsm_arg))
    {
        ahal_set_hdle(ahw_id, hpsm_arg);
        sts = HAL_SCS;
        log_id(LOG_INFO_L1,LOG_AHW,AHW_AL_PSM_INIT_SCS);
        if(HAL_SCS != ahdlsd_updt_ah_state(ahw_id, AH_ACTIVATED))
        {
            sts = HAL_AHDLSD_FW_ERR;
        }
        if(HAL_SCS != ahiobcsn_updt_ahste(&hpsm_arg->ahw_gen_info, AH_FREE_STATE))
        {
            sts = HAL_AHIOBCSN_FW_ERR;
        }
    }
    else
    {
        sts = HAL_AH_INIT_ERR;
        log_id(LOG_ERROR,LOG_AHW,AHW_AL_PSM_INIT_ERR);
        if(HAL_SCS != ahdlsd_updt_ah_state(ahw_id, AH_ERROR))
        {
            sts = HAL_AHDLSD_FW_ERR;
        }
    }
#ifdef LINUX_TEMP_PORT
    DEBUG_CPRINT(("\n EXO AH-AL POWER SENSE MONITOR  Initialization completed successfully"));
#endif
    return sts;
}

/**
 * @brief  API to de-Initialize the power sense monitor.
 */
hal_ret_sts ahw_al_psm_deinit(ahw_al_psm_hdle* hpsm_arg, ahw_inst_id_t ahw_id)
{
#ifdef LINUX_TEMP_PORT
    DEBUG_CPRINT(("\n EXO AH-AL POWER SENSE MONITOR De-Initialize"));
#endif
    hal_ret_sts sts;
    ahal_get_hdle(ahw_id);
    if(HAL_SCS == ahw_al_psm_common_deinit(hpsm_arg))
    {
        sts = HAL_SCS;
    	log_id(LOG_INFO_L1,LOG_AHW,AHW_AL_PSM_DEINIT_SCS);
        if(HAL_SCS != ahdlsd_updt_ah_state(ahw_id, AH_DEACTIVATED))
        {
            sts = HAL_AHDLSD_FW_ERR;
        }
    }
    else
    {
        sts = HAL_AH_INIT_ERR;
    	log_id(LOG_ERROR,LOG_AHW,AHW_AL_PSM_DEINIT_ERR);
        if(HAL_SCS != ahdlsd_updt_ah_state(ahw_id, AH_ERROR))
        {
            sts = HAL_AHDLSD_FW_ERR;
        }
    }
#ifdef LINUX_TEMP_PORT
    DEBUG_CPRINT(("\n EXO AH-AL POWER SENSE MONITOR De-Initialization completed successfully"));
#endif
    return sts;
}
/**
 * @brief  reads the data and writes data
 * returns  status .
 */
hal_ret_sts  ahw_al_psm_self_test(uint32_t ahw_id)
{
    hal_ret_sts sts;
    ahw_al_psm_hdle *hpsm = ahal_get_hdle(ahw_id);
        ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS == ahw_al_psm_common_self_test(hpsm))
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            //sts = HAL_AH_DRIVER_ERR;
            sts = 1;
        }
    return sts;
}
/**
 * @brief API to read the power sense monitor die identifier
 */
hal_ret_sts  ahw_al_psm_read_id(uint32_t ahw_id)
{
    hal_ret_sts sts;
    ahw_al_psm_hdle *hpsm = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&hpsm->ahw_gen_info);
    if(HAL_SCS == sts)
    {

        ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS == ahw_al_psm_common_read_id(hpsm))
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_PSM_READ_ID_ERR_INST_ID__eq__u32_,ahw_id);
        }
    }
    else
    {
    }
    return sts;
}

/**
 * @brief API to generates a system reset.
 */
hal_ret_sts ahw_al_psm_reset(uint32_t ahw_id)
{
    hal_ret_sts sts;
    ahw_al_psm_hdle *hpsm = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&hpsm->ahw_gen_info);
    if(HAL_SCS == sts)
    {
        ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS ==ahw_al_psm_common_reset(hpsm))
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_PSM_READ_ID_ERR_INST_ID__eq__u32_,ahw_id);
        }
    }
    else
    {
    }
    return sts;
}

/**
 * @brief  API to program the Power Sense Monitor calibration register
 */
hal_ret_sts ahw_al_psm_set_calibration(uint32_t ahw_id, uint16_t calibration)
{
    hal_ret_sts sts;
    ahw_al_psm_hdle *hpsm = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&hpsm->ahw_gen_info);
    if(HAL_SCS == sts)
    {
    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_AH_DRIVER_ERR;
		}

        ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS == ahw_al_psm_common_set_calibration(hpsm,calibration))
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_PSM_SET_CALIBRATION_ERR_INST_ID__eq__u32_,ahw_id);
        }
    }
    else
    {
    }
    return sts;
}

/**
 * @brief  API to read programmed calibration value
 */
uint16_t ahw_al_psm_get_calibration(uint32_t ahw_id)
{
    uint16_t res=0;
    ahw_al_psm_hdle *hpsm = ahal_get_hdle(ahw_id);
    hal_ret_sts sts = ahiobcsn_check_ste(&hpsm->ahw_gen_info);
    if(HAL_SCS == sts)
    {
        ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_BUSY_STATE);
        res = ahw_al_psm_common_get_calibration(hpsm);
        ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
    }
    else
    {
    }
    return res;
}

/**
 *  @brief  API to configure the alert pin behavior
 */
hal_ret_sts  ahw_al_psm_alert_pinconfig(uint32_t ahw_id,ahw_al_psm_alert_pin_cfg_t *alert_pin_cfg)
{
    hal_ret_sts sts;
    ahw_al_psm_hdle *hpsm = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&hpsm->ahw_gen_info);
    if(HAL_SCS == sts)
    {

        ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS == ahw_al_psm_common_alert_pinconfig(hpsm,alert_pin_cfg))
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_PSM_ALERT_PINCONFIG_ERR_INST_ID__eq__u32_,ahw_id);
        }
    }
    else
    {
    }
    return sts;
}

/**
 * @brief  API to set the alert function
 */
hal_ret_sts ahw_al_psm_set_alertfunction(uint32_t ahw_id,psm_alert_function_t alert_function)
{
    hal_ret_sts sts;
    ahw_al_psm_hdle *hpsm = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&hpsm->ahw_gen_info);
    if(HAL_SCS == sts)
    {
    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_AH_DRIVER_ERR;
		}

        ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS == ahw_al_psm_common_set_alertfunction(hpsm,alert_function))
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_PSM_SET_ALERTFUNCTION_ERR_INST_ID__eq__u32_,ahw_id);
        }
    }
    else
    {
    }
    return sts;
}

/**
 * @brief API to get the programmed alert function
 */
hal_ret_sts ahw_al_psm_get_alertfunction(uint32_t ahw_id,psm_alert_function_t alert_function)
{
    hal_ret_sts sts;
    ahw_al_psm_hdle *hpsm = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&hpsm->ahw_gen_info);
    if(HAL_SCS == sts)
    {
    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_AH_DRIVER_ERR;
		}

        ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS ==ahw_al_psm_common_get_alertfunction(hpsm,alert_function))
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_PSM_GET_ALERTFUNCTION_ERR_INST_ID__eq__u32_,ahw_id);
        }
    }
    else
    {
    }
    return sts;
}

/**
 * @brief  API to program the VBus threshold
 */
hal_ret_sts ahw_al_psm_setvbusthreshold(uint32_t ahw_id, float vbusthreshold)
{
    hal_ret_sts sts;
    ahw_al_psm_hdle *hpsm = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&hpsm->ahw_gen_info);
    if(HAL_SCS == sts)
    {
    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_AH_DRIVER_ERR;
		}

        ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS == ahw_al_psm_common_setvbusthreshold(hpsm,vbusthreshold))
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_PSM_SETVBUSTHRESHOLD_ERR_INST_ID__eq__u32_,ahw_id);
        }
    }
    else
    {
    }
    return sts;
}

/**
 * @brief  API to get the VBUS threshold
 */
hal_ret_sts  ahw_al_psm_getvbusthreshold(uint32_t ahw_id)
{
    hal_ret_sts sts;
    ahw_al_psm_hdle *hpsm = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&hpsm->ahw_gen_info);
    if(HAL_SCS == sts)
    {
    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_AH_DRIVER_ERR;
		}

        ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS == ahw_al_psm_common_getvbusthreshold(hpsm))
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_PSM_GETVBUSTHRESHOLD_ERR_INST_ID__eq__u32_,ahw_id);
        }
    }
    else
    {
    }
    return sts;
}

/**
 * @brief  API to program the VShunt threshold
 */
hal_ret_sts  ahw_al_psm_setvshuntthreshold(uint32_t ahw_id, float vshuntthreshold)
{
    hal_ret_sts sts;
    ahw_al_psm_hdle *hpsm = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&hpsm->ahw_gen_info);
    if(HAL_SCS == sts)
    {
    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_AH_DRIVER_ERR;
		}

        ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS == ahw_al_psm_common_setvbusthreshold(hpsm,vshuntthreshold))
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_PSM_SETVHUNTTHRESHOLD_ERR_INST_ID__eq__u32_,ahw_id);
        }
    }
    else
    {
    }
    return sts;
}

/**
 * @brief API to get the VShunt threshold
 */
hal_ret_sts ahw_al_psm_getshuntthreshold(uint32_t ahw_id)
{
    hal_ret_sts sts;
    ahw_al_psm_hdle *hpsm = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&hpsm->ahw_gen_info);
    if(HAL_SCS == sts)
    {
    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_AH_DRIVER_ERR;
		}

        ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS == ahw_al_psm_common_getvbusthreshold(hpsm))
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);
            sts = HAL_AH_DRIVER_ERR;
        }
    }
    else
    {
    }
    return sts;
}

/**
 * @brief  API to program the current threshold
 */
hal_ret_sts ahw_al_psm_set_current_threshold(uint32_t ahw_id, float current_threshold)
{
    hal_ret_sts sts;
    ahw_al_psm_hdle *hpsm = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&hpsm->ahw_gen_info);
    if(HAL_SCS == sts)
    {
    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_AH_DRIVER_ERR;
		}

        ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS == ahw_al_psm_common_set_current_threshold(hpsm,current_threshold))
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_PSM_SET_CURRENT_THRESHOLD_ERR_INST_ID__eq__u32_,ahw_id);
        }
    }
    else
    {
    }
    return sts;
}

/**
 * @brief API to program the Power threshold
 */
hal_ret_sts ahw_al_psm_setpowerthreshold(uint32_t ahw_id, uint32_t powerthreshold)
{
    hal_ret_sts sts;
    ahw_al_psm_hdle *hpsm = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&hpsm->ahw_gen_info);
    if(HAL_SCS == sts)
    {
    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_AH_DRIVER_ERR;
		}

        ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS == ahw_al_psm_common_setpowerthreshold(hpsm,powerthreshold))
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_PSM_SETPOWERTHRESHOLD_ERR_INST_ID__eq__u32_,ahw_id);
        }
    }
    else
    {
    }
    return sts;
}

/**
 * @brief  API to get the Power threshold
 */
hal_ret_sts ahw_al_psm_getpowerthreshold(uint32_t ahw_id)
{
    hal_ret_sts sts;
    ahw_al_psm_hdle *hpsm = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&hpsm->ahw_gen_info);
    if(HAL_SCS == sts)
    {
    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_AH_DRIVER_ERR;
		}

        ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_BUSY_STATE);
        if(HAL_SCS == ahw_al_psm_common_getpowerthreshold(hpsm))
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            sts = HAL_SCS;
        }
        else
        {
            ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
            err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);
            sts = HAL_AH_DRIVER_ERR;
            log_id_with_one_arg(LOG_ERROR,LOG_AHW,AHW_AL_PSM_GETPOWERTHRESHOLD_ERR_INST_ID__eq__u32_,ahw_id);
        }
    }
    else
    {
    }
    return sts;
}

/**
 * @brief API to read the bus voltage
 */
float   ahw_al_psm_get_vbus(uint32_t ahw_id)
{
    float res=0;
    hal_ret_sts sts;
    ahw_al_psm_hdle *hpsm = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&hpsm->ahw_gen_info);
    if(HAL_SCS == sts)
    {

    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_AH_DRIVER_ERR;
		}

        ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_BUSY_STATE);
        res = ahw_al_psm_common_get_vbus(hpsm);
        ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
    }
    else
    {
        err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);

    }
    return res;
}

/**
 * @brief  API to read the shunt voltage
 */
float ahw_al_psm_get_vshunt(uint32_t ahw_id)
{
    float res=0;
    hal_ret_sts sts;
    ahw_al_psm_hdle *hpsm = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&hpsm->ahw_gen_info);
    if(HAL_SCS == sts)
    {

    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_AH_DRIVER_ERR;
		}

    	ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_BUSY_STATE);
        res = ahw_al_psm_common_get_vshunt(hpsm);
        ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);
    }
    else
    {
        err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);

    }
    return res;
}

/**
 * @brief API to read the power value
 */
uint32_t  ahw_al_psm_get_power(uint32_t ahw_id)
{
    uint32_t res=0;
    hal_ret_sts sts;
    ahw_al_psm_hdle *hpsm = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&hpsm->ahw_gen_info);
    if(HAL_SCS == sts)
    {
    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_AH_DRIVER_ERR;
		}

        ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_BUSY_STATE);
        res = ahw_al_psm_common_get_power(hpsm);
        ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);

    }
    else
    {
        err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);

    }
    return res;
}

/**
 * @brief  API to read the current value
 */
float ahw_al_psm_get_current(uint32_t ahw_id)
{
    float res=0;
    hal_ret_sts sts;
    ahw_al_psm_hdle *hpsm = ahal_get_hdle(ahw_id);
    sts = ahiobcsn_check_ste(&hpsm->ahw_gen_info);
    if(HAL_SCS == sts)
    {
    	sts = err_get_fault_status(ERR_GROUP_AHW_SET_1, ahw_id-2);
		if(sts != 0)
		{
			return HAL_AH_DRIVER_ERR;
		}




    	ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_BUSY_STATE);
        res = ahw_al_psm_common_get_current(hpsm);
        ahiobcsn_updt_ahste(&hpsm->ahw_gen_info, AH_FREE_STATE);

    }
    else
    {
        err_detected(ERR_GROUP_AHW_SET_1, ahw_id-2);

    }
    return res;
}
