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

/*!
 * @file ac780x_ckgen.c
 *
 * @brief This file provides all clock generator config functions.
 *
 */

/* ===========================================  Includes  =========================================== */
#include "ac780x_ckgen.h"

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

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

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

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

/* ======================================  Functions define  ======================================== */
/*!
 * @brief Enable the module clock.
 *
 * @param[in] module: CKGEN_ClockType, value can be
 *                   - CLK_UART0
 *                   - CLK_UART1
 *                   - CLK_SPI0
 *                   - CLK_I2C0
 *                   - CLK_PWDT0
 *                   - CLK_PWM0
 *                   - CLK_PWM1
 *                   - CLK_PWM2
 *                   - CLK_TIMER
 *                   - CLK_RTC
 *                   - CLK_GPIO
 *                   - CLK_WDG
 *                   - CLK_CTU
 *                   - CLK_ADC0
 *                   - CLK_ACMP0
 * @param[in] enable: enable state
 *                   - ENABLE
 *                   - DISABLE
 * @return none
 */
void CKGEN_Enable(CKGEN_ClockType module, ACTION_Type enable)
{
    __IO uint32_t moduleValue = (uint32_t)module;

    DEVICE_ASSERT(IS_CKGEN_CLOCK_PARA(module));
    if (moduleValue < 32U)
    {
        if (enable == ENABLE)
        {
            SET_BIT32(CKGEN->PERI_CLK_EN_0, (uint32_t)(1U << moduleValue));
        }
        else
        {
            CLEAR_BIT32(CKGEN->PERI_CLK_EN_0, (uint32_t)(1U << moduleValue));
        }
    }
    else if (moduleValue < (uint32_t)CLK_MODULE_NUM)
    {
        moduleValue -= 32U;
        if (enable == ENABLE)
        {
            SET_BIT32(CKGEN->PERI_CLK_EN_1, (uint32_t)(1U << moduleValue));
        }
        else
        {
            CLEAR_BIT32(CKGEN->PERI_CLK_EN_1, (uint32_t)(1U << moduleValue));
        }
    }
    else
    {
        /* do nothing */
    }
}

/*!
 * @brief Do soft reset for the module.
 *
 * @param[in] module: the module to do soft reset, value can be
 *                   - SRST_UART0
 *                   - SRST_UART1
 *                   - SRST_SPI0
 *                   - SRST_I2C0
 *                   - SRST_PWDT0
 *                   - SRST_PWM0
 *                   - SRST_PWM1
 *                   - SRST_PWM2
 *                   - SRST_TIMER
 *                   - SRST_RTC
 *                   - SRST_GPIO
 *                   - SRST_WDG
 *                   - SRST_CTU
 *                   - SRST_ADC0
 *                   - SRST_ACMP0
 *                   - SRST_ANA_REG
 * @param[in] active: active state
 *                   - ENABLE
 *                   - DISABLE
 * @return none
 */
void CKGEN_SoftReset(CKGEN_SoftResetType module, ACTION_Type active)
{
    __IO uint32_t moduleValue = (uint32_t)module;

    DEVICE_ASSERT(IS_CKGEN_SOFT_RESET_PARA(module));
    if (moduleValue < 32U)
    {
        if (active == ENABLE)
        {
            SET_BIT32(CKGEN->PERI_SFT_RST0, (uint32_t)(1U << moduleValue));
        }
        else
        {
            CLEAR_BIT32(CKGEN->PERI_SFT_RST0, (uint32_t)(1U << moduleValue));
        }
    }
    else if (moduleValue < (uint32_t)SRST_MODULE_NUM)
    {
        moduleValue -= 32U;
        if (active == ENABLE)
        {
            SET_BIT32(CKGEN->PERI_SFT_RST1, (uint32_t)(1U << moduleValue));
        }
        else
        {
            CLEAR_BIT32(CKGEN->PERI_SFT_RST1, (uint32_t)(1U << moduleValue));
        }
    }
    else
    {
        /* do nothing */
    }
}

/*!
 * @brief Set the system clock source.
 *
 * @param[in] clockSource: system clock source, value can be
 *                        - SYSCLK_SRC_INTERNAL_OSC
 *                        - SYSCLK_SRC_EXTERNAL_OSC
 * @return none
 */
void CKGEN_SetSysclkSrc(SYSTEM_ClockSourceType clockSource)
{
    MODIFY_REG32(CKGEN->CTRL, CKGEN_CTRL_SYSCLK_SEL_Msk, CKGEN_CTRL_SYSCLK_SEL_Pos, (uint32_t)clockSource);
}

/*!
 * @brief Set the sysclck divider.
 *
 * @param[in] div: system clock divider set, value can be
 *                - SYSCLK_DIVIDER_1
 *                - SYSCLK_DIVIDER_2
 *                - SYSCLK_DIVIDER_4
 *                - SYSCLK_DIVIDER_16
 * @return none
 */
void CKGEN_SetSysclkDiv(SYSCLK_DividerType div)
{
    MODIFY_REG32(CKGEN->CTRL, CKGEN_CTRL_SYSCLK_DIV_Msk, CKGEN_CTRL_SYSCLK_DIV_Pos, (uint32_t)div);
}

/*!
 * @brief Set the APB clock divider.
 *
 * @param[in] div: apb clock divider set, value can be
 *                - APBCLK_DIVIDER_1
 *                - APBCLK_DIVIDER_2
 *                - APBCLK_DIVIDER_3
 *                - APBCLK_DIVIDER_4
 * @return none
 */
void CKGEN_SetAPBClockDivider(APBCLK_DividerType div)
{
    MODIFY_REG32(CKGEN->CTRL, CKGEN_CTRL_APBCLK_DIV_Msk, CKGEN_CTRL_APBCLK_DIV_Pos, (uint32_t)div);
}

/*!
 * @brief Enable/Disable XOSC monitor.
 *
 * @param[in] enable: enable state
 *                   - ENABLE
 *                   - DISABLE
 * @return none
 */
void CKGEN_EnableXOSCMonitor(ACTION_Type enable)
{
    MODIFY_REG32(CKGEN->CTRL, CKGEN_CTRL_XOSC_MON_EN_Msk, CKGEN_CTRL_XOSC_MON_EN_Pos, (uint32_t)enable);
}

/*!
 * @brief Override the NMI_Handler.
 *
 * @param[in] none
 * @return none
 */
void NMI_Handler(void)
{
    uint32_t wakeupStatus = SPM_GetModuleWakeupSourceFlag();

    /* check if nmi_b pad triggle nmi interrupt */
    if (wakeupStatus & SPM_WAKEUP_IRQ_STATUS_NMI_Msk)
    {
        /* write 1 clear NMI IRQ flag */
        SET_BIT32(SPM->WAKEUP_IRQ_STATUS, SPM_WAKEUP_IRQ_STATUS_NMI_Msk);
    }

    /* check if xosc loss triggle nmi interrupt */
    if (READ_BIT32(CKGEN->RESET_STATUS, CKGEN_RESET_STATUS_XOSC_LOSS_RST_STATUS_Msk))
    {
        /* clear all reset status to prevent always run in nmi_handler */
        SET_BIT32(CKGEN->RESET_STATUS, CKGEN_RESET_STATUS_CLR_RESET_STATUS_Msk);
        /* change clock source to internal clock */
        CKGEN_SetSysclkSrc(SYSCLK_SRC_INTERNAL_OSC);
    }
}

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