#include <stddef.h>

#include "Bsp.h"

IRQ_MAP_T IRQ_PERIPH[IRQ_NUM];

/**
 * @brief 中断回调函数初始化为空
 * @note 初始化系统中断
 * @param void
 * @retval void
 */
void IrqInit(void)
{
    //设定系统中断组
    NVIC_PriorityGroupConfig(NVIC_GROUP_LEVEL);
    for (uint8_t i = 0; i < IRQ_NUM; i++) {
        IRQ_PERIPH[i].IrqCallback = NULL;
    }
}
/**
 * @brief 中断回调函数注册
 * @note 将外部传入的函数地址关联到INTERRUPT_ST表中
 * @param Vector:中断号 void(*Func)(uint32_t):回调函数
 * @retval void
 */
void IrqRegister(unsigned char Irqn,void(*Func)(void *Param)) {
    IRQ_PERIPH[Irqn].Irqn = Irqn;
    IRQ_PERIPH[Irqn].IrqCallback = Func;
}

/**
 * @brief 中断NVIC配置
 * @note   设定一个中断的优先级
 * @param Vector:中断号 NvicPrePriority:主优先级 NvicSubPriority:抢占优先级
 * @retval void
 */
void IrqEnable(uint32_t Irq, uint8_t NvicPrePriority, uint8_t NvicSubPriority)
{
    NVIC_InitTypeDef NVIC_InitStructure;

    NVIC_InitStructure.NVIC_IRQChannel = Irq;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NvicPrePriority;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = NvicSubPriority;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

 /**
 * @brief 关闭中断
 * @note void
 * @param Vector:中断号
 * @retval void
 */
void IrqDisable(uint32_t Irq)
{
   NVIC_InitTypeDef NVIC_InitStructure;
   NVIC_InitStructure.NVIC_IRQChannel = Irq;
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
   NVIC_Init(&NVIC_InitStructure);
}

void USART1_IRQHandler(void)
{
    if (IRQ_PERIPH[COM0->Periph.Irqn].IrqCallback != NULL) {
        if (USART_GetFlagStatus(COM0->Periph.UartId, USART_FLAG_ORE) == SET)
        {
            USART_ClearFlag(COM0->Periph.UartId, USART_FLAG_ORE);
        }
        IRQ_PERIPH[COM0->Periph.Irqn].IrqCallback(COM0);
    }
}

void USART2_IRQHandler(void)
{
    if (USART_GetFlagStatus(COM1->Periph.UartId, USART_FLAG_ORE) == SET)
    {
        USART_ClearFlag(COM1->Periph.UartId, USART_FLAG_ORE);
    }
    if (IRQ_PERIPH[COM1->Periph.Irqn].IrqCallback != NULL) {
        IRQ_PERIPH[COM1->Periph.Irqn].IrqCallback(COM1);
    }
}

void USART3_IRQHandler(void)
{
    if (USART_GetFlagStatus(COM2->Periph.UartId, USART_FLAG_ORE) == SET)
    {
        USART_ClearFlag(COM2->Periph.UartId, USART_FLAG_ORE);
    }
    if (IRQ_PERIPH[COM2->Periph.Irqn].IrqCallback != NULL) {
        IRQ_PERIPH[COM2->Periph.Irqn].IrqCallback(COM2);
    }
}


void USART4_IRQHandler(void)
{
    if (USART_GetFlagStatus(COM3->Periph.UartId, USART_FLAG_ORE) == SET)
    {
        USART_ClearFlag(COM3->Periph.UartId, USART_FLAG_ORE);
    }
    if (IRQ_PERIPH[COM3->Periph.Irqn].IrqCallback != NULL) {
        IRQ_PERIPH[COM3->Periph.Irqn].IrqCallback(COM3);
    }
}

void USART5_IRQHandler(void)
{
    if (USART_GetFlagStatus(COM4->Periph.UartId, USART_FLAG_ORE) == SET)
    {
        USART_ClearFlag(COM4->Periph.UartId, USART_FLAG_ORE);
    }
    if (IRQ_PERIPH[COM4->Periph.Irqn].IrqCallback != NULL) {
        IRQ_PERIPH[COM4->Periph.Irqn].IrqCallback(COM4);
    }
}
