#include "../include/hal_lksmcu_opa.h"
#include "lks32mc09x.h"

// 模块复位
void HAL_OPA_Reset(HAL_OPA_x opa_x)
{
}
// 模块打开
void HAL_OPA_Enable(HAL_OPA_x opa_x)
{
    // 系统寄存器解锁
    HAL_REG_SYS_UNLOCK();
    switch (opa_x)
    {
        case HAL_OPA_0: ///< ADC模块0
            HAL_REG_SET(SYS_AFE_REG5, BIT2);
            break;
        case HAL_OPA_1: ///< ADC模块1
            HAL_REG_SET(SYS_AFE_REG5, BIT3);
            break;
        case HAL_OPA_2: ///< ADC模块2
            HAL_REG_SET(SYS_AFE_REG5, BIT4);
            break;
        case HAL_OPA_3: ///< ADC模块3
            HAL_REG_SET(SYS_AFE_REG5, BIT5);
            break;
        default:
            break;
    }
    // 系统寄存器上锁
    HAL_REG_SYS_LOCK();
}
// 模块关闭
void HAL_OPA_Disable(HAL_OPA_x opa_x)
{
    // 系统寄存器解锁
    HAL_REG_SYS_UNLOCK();
    switch (opa_x)
    {
        case HAL_OPA_0: ///< ADC模块0
            HAL_REG_RESET(SYS_AFE_REG5, BIT2);
            break;
        case HAL_OPA_1: ///< ADC模块1
            HAL_REG_RESET(SYS_AFE_REG5, BIT3);
            break;
        case HAL_OPA_2: ///< ADC模块2
            HAL_REG_RESET(SYS_AFE_REG5, BIT4);
            break;
        case HAL_OPA_3: ///< ADC模块3
            HAL_REG_RESET(SYS_AFE_REG5, BIT5);
            break;
        default:
            break;
    }
    // 系统寄存器上锁
    HAL_REG_SYS_LOCK();
}
// 设置增益
void HAL_OPA_SetGain(HAL_OPA_x opa_x, HAL_OPA_GAIN_x gain_x)
{
    uint32_t gain;

    switch (gain_x)
    {
        case HAL_OPA_GAIN_HIGH: ///< 高增益
            gain = 1;
            break;
        case HAL_OPA_GAIN_MEDIUM: ///< 中等增益
            gain = 2;
            break;
        case HAL_OPA_GAIN_LOW: ///< 低增益
            gain = 3;
            break;
        default:
            gain = 0;
            break;
    }
    // 系统寄存器解锁
    HAL_REG_SYS_UNLOCK();
    switch (opa_x)
    {
        case HAL_OPA_0: ///< ADC模块0
            HAL_REG_WRITE_BIT(SYS_AFE_REG0, BIT0 | BIT1, gain);
            break;
        case HAL_OPA_1: ///< ADC模块1
            HAL_REG_WRITE_BIT(SYS_AFE_REG0, BIT2 | BIT3, gain << 2);
            break;
        case HAL_OPA_2: ///< ADC模块2
            HAL_REG_WRITE_BIT(SYS_AFE_REG0, BIT4 | BIT5, gain << 4);
            break;
        case HAL_OPA_3: ///< ADC模块3
            HAL_REG_WRITE_BIT(SYS_AFE_REG0, BIT6 | BIT7, gain << 6);
            break;
        default:
            break;
    }
    // 系统寄存器上锁
    HAL_REG_SYS_LOCK();
}
