#include "../include/hal_lksmcu_irq.h"
#include "hal_lks32mc09x.h"
/**
 * @brief 重置中断(等效于关闭中断)
 * @param irq 中断编号
 */
void HAL_IRQ_Reset(HAL_IRQ_x irq)
{
    HAL_IRQ_Disable(irq);
}

/**
 * @brief 使能全局中断
 * @param irq 中断编号
 */
void HAL_IRQ_Enable(HAL_IRQ_x irq)
{
    __enable_irq();
}

/**
 * @brief 关闭全局中断
 * @param irq 中断编号
 */
void HAL_IRQ_Disable(HAL_IRQ_x irq)
{
    __disable_irq();
}

/**
 * @brief 使能某个模块的中断
 * @param irq 中断编号
 * @param module 模块编号
 * @param number 中断号
 * @param priority 中断优先级
 */
void HAL_IRQ_EnableModule(HAL_IRQ_x irq, HAL_IRQ_MODULE_x module, uint8_t number, uint8_t priority)
{
    IRQn_Type irqn;
    switch (module)
    {
        case HAL_IRQ_MODULE_NonMaskableInt:
            irqn = NonMaskableInt_IRQn;
            break;
        case HAL_IRQ_MODULE_HardFault:
            irqn = HardFault_IRQn;
            break;
        case HAL_IRQ_MODULE_SVCall:
            irqn = SVCall_IRQn;
            break;
        case HAL_IRQ_MODULE_PendSV:
            irqn = PendSV_IRQn;
            break;
        case HAL_IRQ_MODULE_SysTick:
            irqn = SysTick_IRQn;
            break;
        case HAL_IRQ_MODULE_ADC:
            irqn = ADC_IRQn;
            break;
        case HAL_IRQ_MODULE_CMP:
            irqn = CMP_IRQn;
            break;
        case HAL_IRQ_MODULE_DMA:
            irqn = DMA_IRQn;
            break;
        case HAL_IRQ_MODULE_GPIO:
            irqn = GPIO_IRQn;
            break;
        case HAL_IRQ_MODULE_HALL:
            irqn = HALL_IRQn;
            break;
        case HAL_IRQ_MODULE_I2C:
            irqn = I2C_IRQn;
            break;
        case HAL_IRQ_MODULE_MCPWM:
            irqn = (number == 0) ? MCPWM0_IRQn : MCPWM1_IRQn;
            break;
        case HAL_IRQ_MODULE_SPI:
            irqn = SPI_IRQn;
            break;
        case HAL_IRQ_MODULE_TIMER:
            irqn = (number == 0) ? TIMER0_IRQn : TIMER1_IRQn;
            break;
        case HAL_IRQ_MODULE_UART:
            irqn = UART_IRQn;
            break;
        case HAL_IRQ_MODULE_WEAK:
            irqn = WAKE_IRQn;
            break;
        default:
            return;
    }
    NVIC_SetPriority(irqn, priority);
    NVIC_EnableIRQ(irqn);
}
/**
 * @brief 关闭某个模块的中断
 * @param irq 中断编号
 * @param module 模块编号
 * @param number 中断号
 */
void HAL_IRQ_DisableModule(HAL_IRQ_x irq, HAL_IRQ_MODULE_x module, uint8_t number)
{
    IRQn_Type irqn;
    switch (module)
    {
        case HAL_IRQ_MODULE_NonMaskableInt:
            irqn = NonMaskableInt_IRQn;
            break;
        case HAL_IRQ_MODULE_HardFault:
            irqn = HardFault_IRQn;
            break;
        case HAL_IRQ_MODULE_SVCall:
            irqn = SVCall_IRQn;
            break;
        case HAL_IRQ_MODULE_PendSV:
            irqn = PendSV_IRQn;
            break;
        case HAL_IRQ_MODULE_SysTick:
            irqn = SysTick_IRQn;
            break;
        case HAL_IRQ_MODULE_ADC:
            irqn = ADC_IRQn;
            break;
        case HAL_IRQ_MODULE_CMP:
            irqn = CMP_IRQn;
            break;
        case HAL_IRQ_MODULE_DMA:
            irqn = DMA_IRQn;
            break;
        case HAL_IRQ_MODULE_GPIO:
            irqn = GPIO_IRQn;
            break;
        case HAL_IRQ_MODULE_HALL:
            irqn = HALL_IRQn;
            break;
        case HAL_IRQ_MODULE_I2C:
            irqn = I2C_IRQn;
            break;
        case HAL_IRQ_MODULE_MCPWM:
            irqn = (number == 0) ? MCPWM0_IRQn : MCPWM1_IRQn;
            break;
        case HAL_IRQ_MODULE_SPI:
            irqn = SPI_IRQn;
            break;
        case HAL_IRQ_MODULE_TIMER:
            irqn = (number == 0) ? TIMER0_IRQn : TIMER1_IRQn;
            break;
        case HAL_IRQ_MODULE_UART:
            irqn = UART_IRQn;
            break;
        case HAL_IRQ_MODULE_WEAK:
            irqn = WAKE_IRQn;
            break;
        default:
            return;
    }
    NVIC_DisableIRQ(irqn);
}
/**
 * @brief 设置某个模块的中断优先级
 * @param irq 中断编号
 * @param module 模块编号
 * @param number 中断号
 * @param priority 中断优先级
 */
void HAL_IRQ_SetPriority(HAL_IRQ_x irq, HAL_IRQ_MODULE_x module, uint8_t number, uint8_t priority)
{
    IRQn_Type irqn;
    switch (module)
    {
        case HAL_IRQ_MODULE_NonMaskableInt:
            irqn = NonMaskableInt_IRQn;
            break;
        case HAL_IRQ_MODULE_HardFault:
            irqn = HardFault_IRQn;
            break;
        case HAL_IRQ_MODULE_SVCall:
            irqn = SVCall_IRQn;
            break;
        case HAL_IRQ_MODULE_PendSV:
            irqn = PendSV_IRQn;
            break;
        case HAL_IRQ_MODULE_SysTick:
            irqn = SysTick_IRQn;
            break;
        case HAL_IRQ_MODULE_ADC:
            irqn = ADC_IRQn;
            break;
        case HAL_IRQ_MODULE_CMP:
            irqn = CMP_IRQn;
            break;
        case HAL_IRQ_MODULE_DMA:
            irqn = DMA_IRQn;
            break;
        case HAL_IRQ_MODULE_GPIO:
            irqn = GPIO_IRQn;
            break;
        case HAL_IRQ_MODULE_HALL:
            irqn = HALL_IRQn;
            break;
        case HAL_IRQ_MODULE_I2C:
            irqn = I2C_IRQn;
            break;
        case HAL_IRQ_MODULE_MCPWM:
            irqn = (number == 0) ? MCPWM0_IRQn : MCPWM1_IRQn;
            break;
        case HAL_IRQ_MODULE_SPI:
            irqn = SPI_IRQn;
            break;
        case HAL_IRQ_MODULE_TIMER:
            irqn = (number == 0) ? TIMER0_IRQn : TIMER1_IRQn;
            break;
        case HAL_IRQ_MODULE_UART:
            irqn = UART_IRQn;
            break;
        case HAL_IRQ_MODULE_WEAK:
            irqn = WAKE_IRQn;
            break;
        default:
            return;
    }
    NVIC_SetPriority(irqn, priority);
}
