#include "../include/hal_lksmcu_timer.h"
#include "lks32mc09x.h"

// 模块复位
void HAL_TIMER_Reset(HAL_TIMER_x timer_x)
{
    HAL_REG_SYS_UNLOCK();
    switch (timer_x)
    {
        case HAL_TIMER_0:
            HAL_REG_SET(SYS_SFT_RST, BIT4);
            HAL_REG_RESET(SYS_SFT_RST, BIT4);
            break;
        case HAL_TIMER_1:
            HAL_REG_SET(SYS_SFT_RST, BIT5);
            HAL_REG_RESET(SYS_SFT_RST, BIT5);
            break;
        case HAL_TIMER_2:
            HAL_REG_SET(SYS_SFT_RST, BIT6);
            HAL_REG_RESET(SYS_SFT_RST, BIT6);
            break;
        case HAL_TIMER_3:
            HAL_REG_SET(SYS_SFT_RST, BIT7);
            HAL_REG_RESET(SYS_SFT_RST, BIT7);
            break;
        default:
            break;
    }
    // 系统寄存器上锁
    HAL_REG_SYS_LOCK();
}
// 模块打开
void HAL_TIMER_Enable(HAL_TIMER_x timer_x)
{
    // 系统寄存器解锁
    HAL_REG_SYS_UNLOCK();
    switch (timer_x)
    {
        case HAL_TIMER_0:
            HAL_REG_SET(SYS_CLK_FEN, BIT4);
            break;
        case HAL_TIMER_1:
            HAL_REG_SET(SYS_CLK_FEN, BIT5);
            break;
        case HAL_TIMER_2:
            HAL_REG_SET(SYS_CLK_FEN, BIT6);
            break;
        case HAL_TIMER_3:
            HAL_REG_SET(SYS_CLK_FEN, BIT7);
            break;
        default:
            break;
    }
    // 系统寄存器上锁
    HAL_REG_SYS_LOCK();
}
// 模块关闭
void HAL_TIMER_Disable(HAL_TIMER_x timer_x)
{
    // 系统寄存器解锁
    HAL_REG_SYS_UNLOCK();
    switch (timer_x)
    {
        case HAL_TIMER_0:
            HAL_REG_RESET(SYS_CLK_FEN, BIT4);
            break;
        case HAL_TIMER_1:
            HAL_REG_RESET(SYS_CLK_FEN, BIT5);
            break;
        case HAL_TIMER_2:
            HAL_REG_RESET(SYS_CLK_FEN, BIT6);
            break;
        case HAL_TIMER_3:
            HAL_REG_RESET(SYS_CLK_FEN, BIT7);
            break;
        default:
            break;
    }
    // 系统寄存器上锁
    HAL_REG_SYS_LOCK();
}
// 设置pwm频率
void HAL_TIMER_SetFreq(HAL_TIMER_x timer_x, uint32_t freq)
{
    uint32_t mclk = HAL_CLK_GetMcuFreq();
    uint32_t th   = (mclk / freq);
    HAL_REG_WRITE(HAL_TIMER[timer_x]->TH, th);
}
// 获取当前的pwm频率
uint32_t HAL_TIMER_GetFreq(HAL_TIMER_x timer_x)
{
    uint32_t mclk = HAL_CLK_GetMcuFreq();
    uint32_t freq = (mclk / HAL_TIMER[timer_x]->TH);
    return freq;
}
// 获取计数器最大值
uint32_t HAL_TIMER_GetThCnt(HAL_TIMER_x timer_x)
{
    return HAL_TIMER[timer_x]->TH;
}
// 开始计数
void HAL_TIMER_StartCounting(HAL_TIMER_x timer_x)
{
    HAL_REG_SET(HAL_TIMER[timer_x]->CFG0, BIT31);
}
// 停止计数（同时计数器回到0）
void HAL_TIMER_StopCounting(HAL_TIMER_x timer_x)
{
    HAL_REG_RESET(HAL_TIMER[timer_x]->CFG0, BIT31);
    HAL_TIMER[timer_x]->CNT = 0;
}
// 设置pwm波形 仅PWM模式下有效
void HAL_TIMER_SetPwm(HAL_TIMER_x timer_x, int32_t t0, int32_t t1)
{
    HAL_REG_WRITE(HAL_TIMER[timer_x]->CHN0, t0);
    HAL_REG_WRITE(HAL_TIMER[timer_x]->CHN1, t1);
}
// 设置pwm波形q15格式输入 仅PWM模式下有效
void HAL_TIMER_SetPwmQ15(HAL_TIMER_x timer_x, int16_t t0, int16_t t1)
{
    uint32_t th = HAL_TIMER_GetThCnt(timer_x);
    t0          = t0 * th >> 15;
    t1          = t1 * th >> 15;
    HAL_REG_WRITE(HAL_TIMER[timer_x]->CHN0, t0);
    HAL_REG_WRITE(HAL_TIMER[timer_x]->CHN1, t1);
}
// 设置工作模式
void HAL_TIMER_SetWorkMode(HAL_TIMER_x timer_x, HAL_TIMER_MODE_x mode_x)
{
    switch (mode_x)
    {
        case HAL_TIMER_MODE_COUNT: ///< 计数模式
            HAL_REG_WRITE_BIT(HAL_TIMER[timer_x]->CFG0, 0xff, 0);
            break;
        case HAL_TIMER_MODE_PWMOUT: ///< PWM输出模式
            HAL_REG_WRITE_BIT(HAL_TIMER[timer_x]->CFG0, 0xff, BIT7 | BIT3);
            break;
        case HAL_TIMER_MODE_PWMIN: ///< PWM输入模式
            HAL_REG_WRITE_BIT(HAL_TIMER[timer_x]->CFG0, 0xff, BIT6 | BIT5 | BIT2 | BIT0);
            break;
        default:
            break;
    }
}

uint8_t HAL_TIMER_IrqOverflow(HAL_TIMER_x timer_x)
{
    uint32_t val1;

    val1 = HAL_TIMER[timer_x]->IE & HAL_TIMER[timer_x]->IF;
    if (val1 & BIT2)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
void HAL_TIMER_IrqOverflowEnable(HAL_TIMER_x timer_x)
{
    HAL_REG_SET(HAL_TIMER[timer_x]->IE, BIT2);
}
void HAL_TIMER_IrqOverflowDisable(HAL_TIMER_x timer_x)
{
    HAL_REG_RESET(HAL_TIMER[timer_x]->IE, BIT2);
}
void HAL_TIMER_IrqOverflowClear(HAL_TIMER_x timer_x)
{
    HAL_REG_WRITE(HAL_TIMER[timer_x]->IF, BIT2);
}

uint8_t HAL_TIMER_IrqCompare0(HAL_TIMER_x timer_x)
{
    uint32_t val1;

    val1 = HAL_TIMER[timer_x]->IE & HAL_TIMER[timer_x]->IF;
    if (val1 & BIT0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
void HAL_TIMER_IrqCompare0Enable(HAL_TIMER_x timer_x)
{
    HAL_REG_SET(HAL_TIMER[timer_x]->IE, BIT0);
}
void HAL_TIMER_IrqCompare0Disable(HAL_TIMER_x timer_x)
{
    HAL_REG_RESET(HAL_TIMER[timer_x]->IE, BIT0);
}
void HAL_TIMER_IrqCompare0Clear(HAL_TIMER_x timer_x)
{
    HAL_REG_WRITE(HAL_TIMER[timer_x]->IF, BIT0);
}

uint8_t HAL_TIMER_IrqCompare1(HAL_TIMER_x timer_x)
{
    uint32_t val1;

    val1 = HAL_TIMER[timer_x]->IE & HAL_TIMER[timer_x]->IF;
    if (val1 & BIT1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
void HAL_TIMER_IrqCompare1Enable(HAL_TIMER_x timer_x)
{
    HAL_REG_SET(HAL_TIMER[timer_x]->IE, BIT1);
}
void HAL_TIMER_IrqCompare1Disable(HAL_TIMER_x timer_x)
{
    HAL_REG_RESET(HAL_TIMER[timer_x]->IE, BIT1);
}
void HAL_TIMER_IrqCompare1Clear(HAL_TIMER_x timer_x)
{
    HAL_REG_WRITE(HAL_TIMER[timer_x]->IF, BIT1);
}
