#include "stdio.h"
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdbool.h>
#include "hdl_usart.h"


//定义数据获取回调函数
static hdl_debug_uart_rx_cb_t debug_rx_callback_cbs;
static com_rx_callback com_rx_callback_cbs;

//接收中断
static void COM_Rx_IrqCallback(void);
static void COM_RxErr_IrqCallback(void);

static void INTC_IrqInstalHandler(const stc_irq_signin_config_t* pstcConfig, uint32_t u32Priority)
{
    if (NULL != pstcConfig)
    {
        (void)INTC_IrqSignIn(pstcConfig);
        NVIC_ClearPendingIRQ(pstcConfig->enIRQn);
        NVIC_SetPriority(pstcConfig->enIRQn, u32Priority);
        NVIC_EnableIRQ(pstcConfig->enIRQn);
    }
}

//调试串口
static void DEBUG_RxErr_IrqCallback(void)
{
    (void)USART_ReadData(DEBUG_UART);

    USART_ClearStatus(DEBUG_UART, (USART_FLAG_PARITY_ERR | USART_FLAG_FRAME_ERR | USART_FLAG_OVERRUN));
}

static void DEBUG_Rx_IrqCallback(void)
{
    uint8_t r_data = 0;

    r_data = USART_ReadData(DEBUG_UART);

    if (debug_rx_callback_cbs)
    {
        debug_rx_callback_cbs(r_data);
    }
}

void hdl_debug_uart_rx_reg(hdl_debug_uart_rx_cb_t pCBS)
{
    if (debug_rx_callback_cbs == 0)
    {
        debug_rx_callback_cbs = pCBS;
    }
}

void hdl_debug_uart_init(void)
{
    stc_usart_uart_init_t stcUartInit;

    debug_rx_callback_cbs = 0;

    LL_PERIPH_WE(
        LL_PERIPH_GPIO |
        LL_PERIPH_FCG |
        LL_PERIPH_PWC_CLK_RMU |
        LL_PERIPH_EFM |
        LL_PERIPH_SRAM
    );

    GPIO_SetDebugPort(GPIO_PIN_TRST, DISABLE);

    GPIO_SetFunc(DEBUG_UART_RX_PORT, DEBUG_UART_RX_PIN, DEBUG_UART_RX_GPIO_FUNC);
    GPIO_SetFunc(DEBUG_UART_TX_PORT, DEBUG_UART_TX_PIN, DEBUG_UART_TX_GPIO_FUNC);

    FCG_Fcg1PeriphClockCmd(DEBUG_UART_CLK, ENABLE);

    (void)USART_UART_StructInit(&stcUartInit);
    stcUartInit.u32ClockDiv = USART_CLK_DIV1;
    stcUartInit.u32Baudrate = 115200UL;
    stcUartInit.u32OverSampleBit = USART_OVER_SAMPLE_8BIT;
    USART_UART_Init(DEBUG_UART, &stcUartInit, NULL);

    //接收中断配置
    stc_irq_signin_config_t stcIrqSigninConfig;
    stcIrqSigninConfig.enIRQn = DEBUG_UART_INT_EI_IRQ;
    stcIrqSigninConfig.enIntSrc = DEBUG_UART_INT_EI;
    stcIrqSigninConfig.pfnCallback = &DEBUG_RxErr_IrqCallback;
    INTC_IrqInstalHandler(&stcIrqSigninConfig, DDL_IRQ_PRIO_DEFAULT);

    stcIrqSigninConfig.enIRQn = DEBUG_UART_INT_RI_IRQ;
    stcIrqSigninConfig.enIntSrc = DEBUG_UART_INT_RI;
    stcIrqSigninConfig.pfnCallback = &DEBUG_Rx_IrqCallback;
    INTC_IrqInstalHandler(&stcIrqSigninConfig, DDL_IRQ_PRIO_DEFAULT);

    LL_PERIPH_WP(
        LL_PERIPH_GPIO |
        LL_PERIPH_FCG |
        LL_PERIPH_PWC_CLK_RMU |
        LL_PERIPH_EFM |
        LL_PERIPH_SRAM
    );

   USART_FuncCmd(DEBUG_UART, (USART_TX | USART_RX | USART_INT_RX), ENABLE);
   //USART_FuncCmd(DEBUG_UART, (USART_TX | USART_RX ), ENABLE);
}

void hdl_debug_uart_putc(uint8_t ch)
{
    while (USART_GetStatus(DEBUG_UART, USART_FLAG_TX_EMPTY) == RESET);

    USART_WriteData(DEBUG_UART, ch);
    
}

int hdl_debug_uart_getc(int *ch)
{
    if(USART_GetStatus(DEBUG_UART, USART_FLAG_RX_FULL) ==SET){
        
        *ch = USART_ReadData(DEBUG_UART);
        
        return 0;
        
    }else{
    
        return -1;
    }

}

//通信模块串口初始化
void hdl_com_uart_init(void)
{
    stc_usart_uart_init_t stcUartInit={0};
    stc_irq_signin_config_t stcIrqSigninConfig;

    com_rx_callback_cbs = 0;

    LL_PERIPH_WE(LL_PERIPH_GPIO | LL_PERIPH_FCG | LL_PERIPH_PWC_CLK_RMU | \
                 LL_PERIPH_EFM | LL_PERIPH_SRAM);

    GPIO_SetFunc(COM_RX_PORT, COM_RX_PIN, COM_RX_GPIO_FUNC);
    GPIO_SetFunc(COM_TX_PORT, COM_TX_PIN, COM_TX_GPIO_FUNC);

    FCG_Fcg1PeriphClockCmd(COM_UART_CLK, ENABLE);

    (void)USART_UART_StructInit(&stcUartInit);
    stcUartInit.u32ClockDiv = USART_CLK_DIV1;
    stcUartInit.u32Baudrate = 500000UL;
    stcUartInit.u32OverSampleBit = USART_OVER_SAMPLE_8BIT;
    USART_UART_Init(COM_UART, &stcUartInit, NULL);

    //接收中断配置
    stcIrqSigninConfig.enIRQn = COM_INT_EI_IRQ;
    stcIrqSigninConfig.enIntSrc = COM_INT_EI;
    stcIrqSigninConfig.pfnCallback = &COM_RxErr_IrqCallback;
    INTC_IrqInstalHandler(&stcIrqSigninConfig, DDL_IRQ_PRIO_DEFAULT);

    stcIrqSigninConfig.enIRQn = COM_INT_RI_IRQ;
    stcIrqSigninConfig.enIntSrc = COM_INT_RI;
    stcIrqSigninConfig.pfnCallback = &COM_Rx_IrqCallback;
    INTC_IrqInstalHandler(&stcIrqSigninConfig, DDL_IRQ_PRIO_DEFAULT);

    USART_FuncCmd(COM_UART, (USART_TX | USART_RX | USART_INT_RX), ENABLE);
    
    LL_PERIPH_WP(LL_PERIPH_GPIO | LL_PERIPH_FCG | LL_PERIPH_PWC_CLK_RMU | \
                 LL_PERIPH_EFM | LL_PERIPH_SRAM);

}

//通信发送数据
void hdl_com_uart_send(uint8_t* w_buf, uint16_t len)
{
    uint8_t i;

    for (i = 0; i < len; i++)
    {
        USART_WriteData(COM_UART, w_buf[i]);
        
        while (USART_GetStatus(COM_UART, USART_FLAG_TX_CPLT) == RESET);
    }
}

//通信回调函数注册
void hdl_com_uart_rx_register(com_rx_callback pCBS)
{
    if (com_rx_callback_cbs == 0)
    {
        com_rx_callback_cbs = pCBS;
    }
}

//通信接收中断
static void COM_Rx_IrqCallback(void)
{
    uint8_t r_data = 0;

    r_data = USART_ReadData(COM_UART);

    if (com_rx_callback_cbs)
    {
        com_rx_callback_cbs(r_data);
    }
}

static void COM_RxErr_IrqCallback(void)
{
    (void)USART_ReadData(COM_UART);

    USART_ClearStatus(COM_UART, (USART_FLAG_PARITY_ERR | USART_FLAG_FRAME_ERR | USART_FLAG_OVERRUN));
}
