#include "../include/hal_lksmcu_clk.h"
#include "lks32mc09x.h"
#include "lks32mc09x_user_manual.h"
#include "../include/hal_lksmcu_reg.h"
#include "../include/hal_lks32mc09x_reg.h"
static volatile uint32_t HAL_CLK_mclk = 96000000;
/**
 * @brief 设置主频
 * @param FREQ_x 主频
 */
void HAL_CLK_SetMcuFreq(HAL_CLK_FREQ_x FREQ_x)
{
    // 000:96MHz，32MHz
    // 010:72MHz，24MHz
    // 101:64MHz，32MHz
    HAL_REG_SYS_UNLOCK();
    switch (FREQ_x)
    {
        case HAL_CLK_FREQ_RC_8MHZ: ///< 主频4Mhz
            HAL_CLK_mclk = 8000000;
            HAL_REG_WRITE(SYS_CLK_CFG, 0);
            HAL_REG_WRITE_BIT_MASK_POS(SYS_AFE_REG7, SYS_AFE_REG7_PLL_FREQ, 0);
            break;
        case HAL_CLK_FREQ_RC_12MHZ: ///< 主频12Mhz
            HAL_CLK_mclk = 12000000;
            HAL_REG_WRITE(SYS_CLK_CFG, 0x101);
            HAL_REG_WRITE_BIT_MASK_POS(SYS_AFE_REG7, SYS_AFE_REG7_PLL_FREQ, 0);
            break;
        case HAL_CLK_FREQ_RC_24MHZ: ///< 主频24Mhz
            HAL_CLK_mclk = 24000000;
            HAL_REG_WRITE(SYS_CLK_CFG, 0x111);
            HAL_REG_WRITE_BIT_MASK_POS(SYS_AFE_REG7, SYS_AFE_REG7_PLL_FREQ, 0);
            break;
        case HAL_CLK_FREQ_RC_48MHZ: ///< 主频48Mhz
            HAL_CLK_mclk = 48000000;
            HAL_REG_WRITE(SYS_CLK_CFG, 0x155);
            HAL_REG_WRITE_BIT_MASK_POS(SYS_AFE_REG7, SYS_AFE_REG7_PLL_FREQ, 0);
            break;

        case HAL_CLK_FREQ_RC_9MHZ:
            HAL_CLK_mclk = 9000000;
            HAL_REG_WRITE(SYS_CLK_CFG, 0x101);
            HAL_REG_WRITE_BIT_MASK_POS(SYS_AFE_REG7, SYS_AFE_REG7_PLL_FREQ, 2);
            break; ///< 主频9Mhz
        case HAL_CLK_FREQ_RC_18MHZ:
            HAL_CLK_mclk = 18000000;
            HAL_REG_WRITE(SYS_CLK_CFG, 0x111);
            HAL_REG_WRITE_BIT_MASK_POS(SYS_AFE_REG7, SYS_AFE_REG7_PLL_FREQ, 2);
            break; ///< 主频18Mhz
        case HAL_CLK_FREQ_RC_36MHZ:
            HAL_CLK_mclk = 36000000;
            HAL_REG_WRITE(SYS_CLK_CFG, 0x155);
            HAL_REG_WRITE_BIT_MASK_POS(SYS_AFE_REG7, SYS_AFE_REG7_PLL_FREQ, 2);
            break; ///< 主频36Mhz
        case HAL_CLK_FREQ_RC_72MHZ:
            HAL_CLK_mclk = 72000000;
            HAL_REG_WRITE(SYS_CLK_CFG, 0x1ff);
            HAL_REG_WRITE_BIT_MASK_POS(SYS_AFE_REG7, SYS_AFE_REG7_PLL_FREQ, 2);
            break; ///< 主频72Mhz

        case HAL_CLK_FREQ_RC_16MHZ:
            HAL_CLK_mclk = 16000000;
            HAL_REG_WRITE(SYS_CLK_CFG, 0x111);
            HAL_REG_WRITE_BIT_MASK_POS(SYS_AFE_REG7, SYS_AFE_REG7_PLL_FREQ, 5);
            break; ///< 主频16Mhz
        case HAL_CLK_FREQ_RC_32MHZ:
            HAL_CLK_mclk = 32000000;
            HAL_REG_WRITE(SYS_CLK_CFG, 0x155);
            HAL_REG_WRITE_BIT_MASK_POS(SYS_AFE_REG7, SYS_AFE_REG7_PLL_FREQ, 5);
            break; ///< 主频32Mhz
        case HAL_CLK_FREQ_RC_64MHZ:
            HAL_CLK_mclk = 64000000;
            HAL_REG_WRITE(SYS_CLK_CFG, 0x1ff);
            HAL_REG_WRITE_BIT_MASK_POS(SYS_AFE_REG7, SYS_AFE_REG7_PLL_FREQ, 5);
            break; ///< 主频64Mhz

        default:
        case HAL_CLK_FREQ_RC_96MHZ: ///< 主频96Mhz
            HAL_CLK_mclk = 96000000;
            HAL_REG_WRITE(SYS_CLK_CFG, 0x1ff);
            break;
    }
    HAL_REG_SYS_LOCK();
}

/**
 * @brief 获取主频
 * @return 主频
 */
uint32_t HAL_CLK_GetMcuFreq(void)
{
    return 96000000;
}
