#include "Bsp.h"

SYSTEM_INT_REG SystemIntReg;

/**
 * @brief 禁止中断
 * @param
 * @retval void
 * @note   void
 * @example void
 */
void DisAllowInt(void)
{
    __set_PRIMASK(1);
}

 /**
 * @brief 允许中断
 * @param
 * @retval void
 * @note   void
 * @example void
 */
void AllowInt(void)
{
    __set_PRIMASK(0);
}

/**
 * @brief 中断回调函数初始化为空
 * @param
 * @retval void
 * @note   void
 * @example void
 */
void IntCbInit(void)
{
    NVIC_PriorityGroupConfig(NVIC_GROUP_LEVEL);
    //memset(&SystemIntReg,NULL,sizeof(SYSTEM_INT_REG));
}
/**
 * @brief 中断回调函数注册
 * @param
 * @retval void
 * @note   void
 * @example void
 */
void IntCbReg(uint32_t Vector, void(*Func)(uint32_t))
{
    switch (Vector)
    {
        #ifdef USE_USART1
        case USART1_IRQn:
            SystemIntReg.Usart1.CallBack = Func;
            SystemIntReg.Usart1.IntId = USART1_BASE;
            break;
        #endif
        #ifdef USE_USART2
        case USART2_IRQn:
            SystemIntReg.Usart1.CallBack = Func;
            SystemIntReg.Usart1.IntId = USART2_BASE;
            break;
        #endif
        #ifdef USE_USART3
        case USART3_IRQn:
            SystemIntReg.Usart1.CallBack = Func;
            SystemIntReg.Usart1.IntId = USART3_BASE;
            break;
        #endif
        #ifdef USE_USART4
        case UART4_IRQn:
            SystemIntReg.Usart1.CallBack = Func;
            SystemIntReg.Usart1.IntId = UART4_BASE;
            break;
        #endif
        #ifdef USE_USART5
        case UART5_IRQn:
            SystemIntReg.Usart1.CallBack = Func;
            SystemIntReg.Usart1.IntId = UART5_BASE;
            break;
        #endif
        default:
            break;
    }
}

/**
 * @brief 中断NVIC配置
 * @param
 * @retval void
 * @note   void
 * @example void
 */
void IntSetLevel(uint32_t Vector, uint8_t NvicPrePriority, uint8_t NvicSubPriority)
 {
    NVIC_InitTypeDef NVIC_InitStructure;

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

 /**
 * @brief 关闭中断
 * @param
 * @retval void
 * @note   void
 * @example void
 */
 void SetIntDisable(uint32_t Vector)
 {
    NVIC_InitTypeDef NVIC_InitStructure;

    NVIC_InitStructure.NVIC_IRQChannel = Vector;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
 }

#ifdef USE_USART1
void USART1_IRQHandler(void)
{
    if (USART_GetFlagStatus(USART1, USART_FLAG_ORE) == SET)
    {
        USART_ClearFlag(USART1, USART_FLAG_ORE);
    }
    (*SystemIntReg.Usart1.CallBack)(SystemIntReg.Usart1.IntId);
}
#endif

#ifdef USE_USART2
void USART2_IRQHandler(void)
{
    if (USART_GetFlagStatus(USART2, USART_FLAG_ORE) == SET)
    {
        USART_ClearFlag(USART2, USART_FLAG_ORE);
    }
    (*SystemIntReg.Usart2.CallBack)((void*)SystemIntReg.Usart2.IntId);
}
#endif

#ifdef USE_USART3
void USART3_IRQHandler(void)
{
    if (USART_GetFlagStatus(USART3, USART_FLAG_ORE) == SET)
    {
        USART_ClearFlag(USART3, USART_FLAG_ORE);
    }
    (*SystemIntReg.Usart3.CallBack)((void*)SystemIntReg.Usart3.IntId);
}
#endif

#ifdef USE_USART4
void USART4_IRQHandler(void)
{
    if (USART_GetFlagStatus(UART4, USART_FLAG_ORE) == SET)
    {
        USART_ClearFlag(UART4, USART_FLAG_ORE);
    }
    (*SystemIntReg.Usart4.CallBack)((void*)SystemIntReg.Usart4.IntId);
}
#endif

#ifdef USE_USART5
void USART5_IRQHandler(void)
{
    if (USART_GetFlagStatus(UART5, USART_FLAG_ORE) == SET)
    {
        USART_ClearFlag(UART5, USART_FLAG_ORE);
    }
    (*SystemIntReg.Usart5.CallBack)((void*)SystemIntReg.Usart5.IntId);
}
#endif
