/**
 *******************************************************************************
 * @file  hc32_ll_dbgc.c
 * @brief This file provides firmware functions to manage the DBGC.
 @verbatim
   Change Logs:
   Date             Author          Notes
   2022-10-31       CDT             First version
 @endverbatim
 *******************************************************************************
 * Copyright (C) 2022, Xiaohua Semiconductor Co., Ltd. All rights reserved.
 *
 * This software component is licensed by XHSC under BSD 3-Clause license
 * (the "License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                    opensource.org/licenses/BSD-3-Clause
 *
 *******************************************************************************
 */

/*******************************************************************************
 * Include files
 ******************************************************************************/
#include "hc32_ll_dbgc.h"
#include "hc32_ll_utility.h"

/**
 * @addtogroup LL_Driver
 * @{
 */

/**
 * @defgroup LL_DBGC DBGC
 * @brief DBGC Driver Library
 * @{
 */

#if (LL_DBGC_ENABLE == DDL_ON)

/*******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/

/*******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/

/*******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/

/*******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/

/*******************************************************************************
 * Local variable definitions ('static')
 ******************************************************************************/

/*******************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/
/**
 * @defgroup DBGC_Global_Functions DBGC Global Functions
 * @{
 */
/**
 * @brief  Get MCU status.
 * @param  None
 * @retval int32_t      @ref DBGC_MCU_Status
 */
uint32_t DBGC_GetMcuStatus(void)
{
    return READ_REG32_BIT(CM_DBGC->MCUSTAT, DBGC_MCUSTAT_SAFTYLOCK2 | DBGC_MCUSTAT_SAFTYLOCK1 | DBGC_MCUSTAT_AUTH);
}

/**
 * @brief  erase the chip.
 * @param  None
 * @retval int32_t:
 *         - LL_OK: erase successfully
 *         - LL_ERR: erase error
 */
int32_t DBGC_FlashErase(void)
{
    int32_t i32Ret = LL_OK;
    SET_REG32_BIT(CM_DBGC->FMCCTL, DBGC_FMCCTL_ERASEREQ);
    if (DBGC_FMCCTL_ERASEERR == READ_REG32_BIT(CM_DBGC->FMCCTL, DBGC_FMCCTL_ERASEERR)) {
        i32Ret = LL_ERR;
    }
    return i32Ret;
}

/**
 * @brief  Get authenticate ID.
 * @param  [out] pstcAuthID     Authenticate ID struct
 * @retval the value of the authenticate ID
 */
void DBGC_GetAuthID(stc_dbgc_auth_id_t *pstcAuthID)
{
    if (NULL != pstcAuthID) {
        pstcAuthID->u32AuthID0 = READ_REG32(CM_DBGC->AUTHID0);
        pstcAuthID->u32AuthID1 = READ_REG32(CM_DBGC->AUTHID1);
        pstcAuthID->u32AuthID2 = READ_REG32(CM_DBGC->AUTHID2);
    }
}

/**
 * @brief  Whether to stop the peripheral while mcu core stop.
 * @param  [in] u32Periph Specifies the peripheral. @ref DBGC_Periph_Sel
 * @param  [in] enNewState An @ref en_functional_state_t enumeration value.
 * @retval None
 */
void DBGC_PeriphCmd(uint32_t u32Periph, en_functional_state_t enNewState)
{
    if (ENABLE == enNewState) {
        CLR_REG32_BIT(CM_DBGC->MCUSTPCTL, u32Periph);
    } else {
        SET_REG32_BIT(CM_DBGC->MCUSTPCTL, u32Periph);
    }
}

/**
 * @brief  Whether to stop the peripheral2 while mcu core stop.
 * @param  [in] u32Periph Specifies the peripheral. @ref DBGC_Periph2_Sel
 * @param  [in] enNewState An @ref en_functional_state_t enumeration value.
 * @retval None
 */
void DBGC_Periph2Cmd(uint32_t u32Periph, en_functional_state_t enNewState)
{
    if (ENABLE == enNewState) {
        CLR_REG32_BIT(CM_DBGC->MCUSTPCTL2, u32Periph);
    } else {
        SET_REG32_BIT(CM_DBGC->MCUSTPCTL2, u32Periph);
    }
}

/**
 * @brief  Get chip ID.
 * @param  None
 * @retval uint32_t Chip ID
 */
uint32_t DBGC_GetChipID(void)
{
    return READ_REG32(CM_DBGC->CHIPID);
}

/**
 * @}
 */

#endif /* LL_DBGC_ENABLE */

/**
 * @}
 */

/**
 * @}
 */

/******************************************************************************
 * EOF (not truncated)
 *****************************************************************************/
