/*
 * UART.c
 *
 *  Created on: 2024年5月18日
 *      Author: 34509
 */
#include "UART.h"
#include "uart_dev.h"


void USART2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void USART3_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void UART4_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void UART5_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void UART6_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void UART7_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void UART8_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));



/* 接收缓冲区数组 */
uint8_t USART2_Rbuffer[1] = {0x00};
uint8_t USART3_Rbuffer[1] = {0x00};
uint8_t UART4_Rbuffer[1] = {0x00};
uint8_t UART5_Rbuffer[1] = {0x00};
uint8_t UART6_Rbuffer[1] = {0x00};
uint8_t UART7_Rbuffer[1] = {0x00};
uint8_t UART8_Rbuffer[1] = {0x00};



void USARTx_SendByte(USART_TypeDef* pUSARTx, uint8_t data)
{
    if (pUSARTx == J5_0_USART) {
        rt_pin_write(USART3_DE, write);
    }
    else if (pUSARTx == J1_USART) {
        rt_pin_write(J1_DE, write);
    }
    else if (pUSARTx == J2_USART) {
        rt_pin_write(J2_DE, write);
    }
    else if (pUSARTx == J3_USART) {
        rt_pin_write(J3_DE, write);
    }
    else if (pUSARTx == J4_USART) {
        rt_pin_write(J4_DE, write);
    }
    else {
        return;
    }

    USART_SendData(pUSARTx, data);
    while(USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);

    if (pUSARTx == J5_0_USART) {
        rt_pin_write(USART3_DE, read);
    }
    else if (pUSARTx == J1_USART) {
        rt_pin_write(J1_DE, read);
    }
    else if (pUSARTx == J2_USART) {
        rt_pin_write(J2_DE, read);
    }
    else if (pUSARTx == J3_USART) {
        rt_pin_write(J3_DE, read);
    }
    else if (pUSARTx == J4_USART) {
        rt_pin_write(J4_DE, read);
    }
    else {
        return;
    }
}


void USARTx_SendByte_str(USART_TypeDef* pUSARTx, uint8_t data)
{
    USART_SendData(pUSARTx, data);
    while(USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
}

void USARTx_SendStr(USART_TypeDef* pUSARTx, char *str)
{
    if (pUSARTx == J5_0_USART) {
        rt_pin_write(USART3_DE, write);
    }
    else if (pUSARTx == J1_USART) {
        rt_pin_write(J1_DE, write);
    }
    else if (pUSARTx == J2_USART) {
        rt_pin_write(J2_DE, write);
    }
    else if (pUSARTx == J3_USART) {
        rt_pin_write(J3_DE, write);
    }
    else if (pUSARTx == J4_USART) {
        rt_pin_write(J4_DE, write);
    }
    else {
        return;
    }

    uint8_t i = 0;
    do
    {
        USARTx_SendByte_str(pUSARTx, *(str+i));
       i++;
    }while(*(str+i) != '\0');
    while(USART_GetFlagStatus(pUSARTx, USART_FLAG_TC) == RESET);

    if (pUSARTx == J5_0_USART) {
        rt_pin_write(USART3_DE, read);
    }
    else if (pUSARTx == J1_USART) {
        rt_pin_write(J1_DE, read);
    }
    else if (pUSARTx == J2_USART) {
        rt_pin_write(J2_DE, read);
    }
    else if (pUSARTx == J3_USART) {
        rt_pin_write(J3_DE, read);
    }
    else if (pUSARTx == J4_USART) {
        rt_pin_write(J4_DE, read);
    }
    else {
        return;
    }
}

void USARTx_SendStr_Len(USART_TypeDef* pUSARTx, char *str, int len)
{
    if (pUSARTx == J5_0_USART) {
        rt_pin_write(USART3_DE, write);
    }
    else if (pUSARTx == J1_USART) {
        rt_pin_write(J1_DE, write);
    }
    else if (pUSARTx == J2_USART) {
        rt_pin_write(J2_DE, write);
    }
    else if (pUSARTx == J3_USART) {
        rt_pin_write(J3_DE, write);
    }
    else if (pUSARTx == J4_USART) {
        rt_pin_write(J4_DE, write);
    }
    else {
        return;
    }

    uint8_t i = 0;
    do
    {
        USARTx_SendByte_str(pUSARTx, *(str+i));
       i++;
    }while(--len);
    while(USART_GetFlagStatus(pUSARTx, USART_FLAG_TC) == RESET);

    if (pUSARTx == J5_0_USART) {
        rt_pin_write(USART3_DE, read);
    }
    else if (pUSARTx == J1_USART) {
        rt_pin_write(J1_DE, read);
    }
    else if (pUSARTx == J2_USART) {
        rt_pin_write(J2_DE, read);
    }
    else if (pUSARTx == J3_USART) {
        rt_pin_write(J3_DE, read);
    }
    else if (pUSARTx == J4_USART) {
        rt_pin_write(J4_DE, read);
    }
    else {
        return;
    }
}


/*
 * @brief   初始化UART6对应的J1_485,默认为接收，电源关闭状态
 * @param   baud    波特率
 * @retval
 *
 */
void J1_485_Init(uint32_t baud)
{
    rt_pin_mode(J1_DE, PIN_MODE_OUTPUT);
    /* 设置485状态为读 */
    rt_pin_write(J1_DE, read);

    rt_pin_mode(J1_PWR_Ctrl, PIN_MODE_OUTPUT);
    J1_PWR_Close();

    GPIO_InitTypeDef   GPIO_InitStructure;
    USART_InitTypeDef  USART_InitStructure;
    NVIC_InitTypeDef   NVIC_InitStructure;

    /* UART6  TX --> PC0   RX --> PC1  */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART6, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;              //设置PC0为复用推挽输出
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;        //设置PC1为浮空输入
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    USART_InitStructure.USART_BaudRate = baud;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    USART_Init(UART6, &USART_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = UART6_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;      //抢占优先级为3
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;           //子优先级为3
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;              //IRQ通道使能
    NVIC_Init(&NVIC_InitStructure);                              //中断优先级初始化

    USART_ITConfig(UART6, USART_IT_RXNE, ENABLE);

    USART_Cmd(UART6,ENABLE);
}

/* 打开J1485的电源 */
void J1_PWR_Open(void)
{
    rt_pin_write(J1_PWR_Ctrl, open);
}
/* 关闭J1485的电源 */
void J1_PWR_Close(void)
{
    rt_pin_write(J1_PWR_Ctrl, close);
}


/*
 * @brief   初始化UART7对应的J2_485,默认为接收，电源关闭状态
 * @param   baud    波特率
 * @retval
 *
 */
void J2_485_Init(uint32_t baud)
{
    rt_pin_mode(J2_DE, PIN_MODE_OUTPUT);
    /* 设置485状态为读 */
    rt_pin_write(J2_DE, read);

    rt_pin_mode(J2_PWR_Ctrl, PIN_MODE_OUTPUT);
    J2_PWR_Close();

    GPIO_InitTypeDef   GPIO_InitStructure;
    USART_InitTypeDef  USART_InitStructure;
    NVIC_InitTypeDef   NVIC_InitStructure;

    /* UART7  TX --> PC2   RX --> PC3  */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART7, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;              //设置PC2为复用推挽输出
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;        //设置PC3为浮空输入
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    USART_InitStructure.USART_BaudRate = baud;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    USART_Init(UART7, &USART_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = UART7_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;      //抢占优先级为3
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;           //子优先级为3
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;              //IRQ通道使能
    NVIC_Init(&NVIC_InitStructure);                              //中断优先级初始化

    USART_ITConfig(UART7, USART_IT_RXNE, ENABLE);

    USART_Cmd(UART7,ENABLE);
}

/* 打开J2 485的电源 */
void J2_PWR_Open(void)
{
    rt_pin_write(J2_PWR_Ctrl, open);
}

/* 关闭J2 485的电源 */
void J2_PWR_Close(void)
{
    rt_pin_write(J2_PWR_Ctrl, close);
}


/*
 * @brief   初始化USART2对应的J3_485,默认为接收，电源关闭状态
 * @param   baud    波特率
 * @retval
 *
 */
void J3_485_Init(uint32_t baud)
{
    rt_pin_mode(J3_DE, PIN_MODE_OUTPUT);
    /* 设置485状态为读 */
    rt_pin_write(J3_DE, read);

    rt_pin_mode(J3_PWR_Ctrl, PIN_MODE_OUTPUT);
    J3_PWR_Close();

    GPIO_InitTypeDef   GPIO_InitStructure;
    USART_InitTypeDef  USART_InitStructure;
    NVIC_InitTypeDef   NVIC_InitStructure;

    /* USART2  TX --> PA2   RX --> PA3  */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;              //设置PA2为复用推挽输出
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;        //设置PA3为浮空输入
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    USART_InitStructure.USART_BaudRate = baud;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    USART_Init(USART2, &USART_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;      //抢占优先级为1
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;           //子优先级为1
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;              //IRQ通道使能
    NVIC_Init(&NVIC_InitStructure);                              //中断优先级初始化

    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

    USART_Cmd(USART2,ENABLE);
}

/* 打开J3 485的电源 */
void J3_PWR_Open(void)
{
    rt_pin_write(J3_PWR_Ctrl, open);
}

/* 关闭J3 485的电源 */
void J3_PWR_Close(void)
{
    rt_pin_write(J3_PWR_Ctrl, close);
}


/*
 * @brief   初始化UART8对应的J4_485,默认为接收，电源关闭状态
 * @param   baud    波特率
 * @retval
 *
 */
void J4_485_Init(uint32_t baud)
{
    rt_pin_mode(J4_DE, PIN_MODE_OUTPUT);
    /* 设置485状态为读 */
    rt_pin_write(J4_DE, read);

    rt_pin_mode(J4_PWR_Ctrl, PIN_MODE_OUTPUT);
    J4_PWR_Open();

    GPIO_InitTypeDef   GPIO_InitStructure;
    USART_InitTypeDef  USART_InitStructure;
    NVIC_InitTypeDef   NVIC_InitStructure;

    /* UART8  TX --> PC4   RX --> PC5  */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART8, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;              //设置PC4为复用推挽输出
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;        //设置PC5为浮空输入
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    USART_InitStructure.USART_BaudRate = baud;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    USART_Init(UART8, &USART_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = UART8_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;      //抢占优先级为3
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;           //子优先级为3
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;              //IRQ通道使能
    NVIC_Init(&NVIC_InitStructure);                              //中断优先级初始化

    USART_ITConfig(UART8, USART_IT_RXNE, ENABLE);

    USART_Cmd(UART8,ENABLE);
}

/* 打开J4 485的电源 */
void J4_PWR_Open(void)
{
    rt_pin_write(J4_PWR_Ctrl, open);
}

/* 关闭J4 485的电源 */
void J4_PWR_Close(void)
{
    rt_pin_write(J4_PWR_Ctrl, close);
}


/*
 * @brief   初始话USART3用于控制J5-0共6个485接口,设置控制的485为J0，电源全部关闭
 * @param   baud    波特率
 * @retval
 *
 */
void J5_0_485_Init(uint32_t baud)
{
    rt_pin_mode(J5_PWR_Ctrl, PIN_MODE_OUTPUT);
    rt_pin_mode(J6_PWR_Ctrl, PIN_MODE_OUTPUT);
    rt_pin_mode(J7_PWR_Ctrl, PIN_MODE_OUTPUT);
    rt_pin_mode(J8_PWR_Ctrl, PIN_MODE_OUTPUT);
    rt_pin_mode(J9_PWR_Ctrl, PIN_MODE_OUTPUT);

    rt_pin_mode(USART3_DE, PIN_MODE_OUTPUT);
    /* 设置485状态为读 */
    rt_pin_write(USART3_DE, read);

    rt_pin_mode(TD_USART_A, PIN_MODE_OUTPUT);
    rt_pin_mode(TD_USART_B, PIN_MODE_OUTPUT);
    rt_pin_mode(TD_USART_C, PIN_MODE_OUTPUT);
    /* 设置USART3连接J0 485 */
    USART_CONNET_J0();


    GPIO_InitTypeDef   GPIO_InitStructure;
    USART_InitTypeDef  USART_InitStructure;
    NVIC_InitTypeDef   NVIC_InitStructure;

    /* USART3  TX --> PB10   RX --> PB11  */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;              //设置PB10为复用推挽输出
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;        //设置PB11为浮空输入
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    USART_InitStructure.USART_BaudRate = baud;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    USART_Init(USART3, &USART_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;      //抢占优先级为1
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;           //子优先级为1
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;              //IRQ通道使能
    NVIC_Init(&NVIC_InitStructure);                              //中断优先级初始化

    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
//    USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);

    USART_Cmd(USART3,ENABLE);
}

//void USART3_IRQHandler(void)
//{
//    if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //中断产生
//    {
////        USART_ClearITPendingBit(USART3,USART_IT_RXNE);                //清除中断标志
////        USART_Rbuffer[USART_Rbuffer_Num] = USART_ReceiveData(USART3); //接收数据
////        USART_Rbuffer_Num++;
//        USART3_Rbuffer[0] = USART_ReceiveData(USART3); //接收数据
//        uint8_t c = 0;
//        uart_device_info *dev = (uart_device_info *)g_J50RS485_USART3_handle;
//        c = USART3_Rbuffer[0];
//        if(!RingQueueFull(&dev->uart_ring_queue))
//           InRingQueue(&dev->uart_ring_queue, c);
//
//    }
//
////    if(USART_GetITStatus(USART3, USART_IT_IDLE) != RESET) //中断产生
////    {
//////        for(USART_Tbuffer_Num=0;USART_Tbuffer_Num < USART_Rbuffer_Num;USART_Tbuffer_Num++)
//////        {
//////            USARTx_SendByte(USART3, USART_Rbuffer[USART_Tbuffer_Num]); //发送数据
//////        }
////        USARTx_SendStr_Len(USART3, USART_Rbuffer, USART_Rbuffer_Num);
////        rt_kprintf("USART_Rbuffer_Num = %d \n", USART_Rbuffer_Num);
//////        USART3->STATR;
//////        USART3->DATAR;
////        USART_Rbuffer_Num = 0; //初始化
////        USART_ReceiveData(USART3);               //读DR
////    }
////
////    if(USART_GetFlagStatus(USART3,USART_FLAG_ORE) == SET) //溢出
////    {
////        USART_ClearFlag(USART3,USART_FLAG_ORE);  //清标志
////        USART_ReceiveData(USART3);               //读DR
////    }
//}

/*
 * Function Name  : USART_CONNET_J0
 * Description    : 控制串口3连接J0对应的485.
 * Input          : None
 * Return         : None
 **/
void USART_CONNET_J0(void)
{
//    J5_PWR_Close();
//    J6_PWR_Close();
//    J7_PWR_Close();
//    J8_PWR_Close();
//    J9_PWR_Close();
    rt_pin_write(TD_USART_A, PIN_LOW);
    rt_pin_write(TD_USART_B, PIN_LOW);
    rt_pin_write(TD_USART_C, PIN_LOW);
}

/*
 * Function Name  : USART_CONNET_J5
 * Description    : 控制串口3连接J5对应的485.
 * Input          : None
 * Return         : None
 **/
void USART_CONNET_J5(void)
{
//    J5_PWR_Open();
//    J6_PWR_Close();
//    J7_PWR_Close();
//    J8_PWR_Close();
//    J9_PWR_Close();
    rt_pin_write(TD_USART_A, PIN_LOW);
    rt_pin_write(TD_USART_B, PIN_LOW);
    rt_pin_write(TD_USART_C, PIN_LOW);
}

/*
 * Function Name  : USART_CONNET_J6
 * Description    : 控制串口3连接J6对应的485.
 * Input          : None
 * Return         : None
 **/
void USART_CONNET_J6(void)
{
//    J5_PWR_Close();
//    J6_PWR_Open();
//    J7_PWR_Close();
//    J8_PWR_Close();
//    J9_PWR_Close();
    rt_pin_write(TD_USART_A, PIN_HIGH);
    rt_pin_write(TD_USART_B, PIN_LOW);
    rt_pin_write(TD_USART_C, PIN_LOW);
}

/*
 * Function Name  : USART_CONNET_J7
 * Description    : 控制串口3连接J7对应的485.
 * Input          : None
 * Return         : None
 **/
void USART_CONNET_J7(void)
{
//    J5_PWR_Close();
//    J6_PWR_Close();
//    J7_PWR_Open();
//    J8_PWR_Close();
//    J9_PWR_Close();
    rt_pin_write(TD_USART_A, PIN_HIGH);
    rt_pin_write(TD_USART_B, PIN_HIGH);
    rt_pin_write(TD_USART_C, PIN_LOW);
}

/*
 * Function Name  : USART_CONNET_J8
 * Description    : 控制串口3连接J8对应的485.
 * Input          : None
 * Return         : None
 **/
void USART_CONNET_J8(void)
{
//    J5_PWR_Close();
//    J6_PWR_Close();
//    J7_PWR_Close();
//    J8_PWR_Open();
//    J9_PWR_Close();
    rt_pin_write(TD_USART_A, PIN_LOW);
    rt_pin_write(TD_USART_B, PIN_LOW);
    rt_pin_write(TD_USART_C, PIN_HIGH);
}

/*
 * Function Name  : USART_CONNET_J9
 * Description    : 控制串口3连接J9对应的485.
 * Input          : None
 * Return         : None
 **/
void USART_CONNET_J9(void)
{
//    J5_PWR_Close();
//    J6_PWR_Close();
//    J7_PWR_Close();
//    J8_PWR_Close();
//    J9_PWR_Open();
    rt_pin_write(TD_USART_A, PIN_HIGH);
    rt_pin_write(TD_USART_B, PIN_LOW);
    rt_pin_write(TD_USART_C, PIN_HIGH);
}

void J5_PWR_Open(void)
{
    rt_pin_write(J5_PWR_Ctrl, open);
}

void J5_PWR_Close(void)
{
    rt_pin_write(J5_PWR_Ctrl, close);
}

void J6_PWR_Open(void)
{
    rt_pin_write(J6_PWR_Ctrl, open);
}

void J6_PWR_Close(void)
{
    rt_pin_write(J6_PWR_Ctrl, close);
}

void J7_PWR_Open(void)
{
    rt_pin_write(J7_PWR_Ctrl, open);
}

void J7_PWR_Close(void)
{
    rt_pin_write(J7_PWR_Ctrl, close);
}

void J8_PWR_Open(void)
{
    rt_pin_write(J8_PWR_Ctrl, open);
}

void J8_PWR_Close(void)
{
    rt_pin_write(J8_PWR_Ctrl, close);
}

void J9_PWR_Open(void)
{
    rt_pin_write(J9_PWR_Ctrl, open);
}

void J9_PWR_Close(void)
{
    rt_pin_write(J9_PWR_Ctrl, close);
}


/*
 * @brief   初始化UART4对应的LORA串口
 * @param   baud    波特率
 * @retval
 *
 */
void LORA_UART4_Init(uint32_t baud)
{
    GPIO_InitTypeDef   GPIO_InitStructure;
    USART_InitTypeDef  USART_InitStructure;
    NVIC_InitTypeDef   NVIC_InitStructure;

    /* UART4  TX --> PC10   RX --> PC11  */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;              //设置PC10为复用推挽输出
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;        //设置PC11为浮空输入
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    USART_InitStructure.USART_BaudRate = baud;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    USART_Init(UART4, &USART_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;      //抢占优先级为3
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;           //子优先级为3
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;              //IRQ通道使能
    NVIC_Init(&NVIC_InitStructure);                              //中断优先级初始化

    USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);

    USART_Cmd(UART4,ENABLE);
}


/*
 * @brief   初始化UART4对应的LORA串口
 * @param   baud    波特率
 * @retval
 *
 */
void Upward_UART5_Init(uint32_t baud)
{
    GPIO_InitTypeDef   GPIO_InitStructure;
    USART_InitTypeDef  USART_InitStructure;
    NVIC_InitTypeDef   NVIC_InitStructure;

    /* UART5  TX --> PC12   RX --> PD2  */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;              //设置PC12为复用推挽输出
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;        //设置PD2为浮空输入
    GPIO_Init(GPIOD, &GPIO_InitStructure);

    USART_InitStructure.USART_BaudRate = baud;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    USART_Init(UART5, &USART_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;      //抢占优先级为3
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;           //子优先级为3
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;              //IRQ通道使能
    NVIC_Init(&NVIC_InitStructure);                              //中断优先级初始化

    USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);

    USART_Cmd(UART5,ENABLE);
}



void USART2_IRQHandler(void)
{
    if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //中断产生
    {
        USART2_Rbuffer[0] = USART_ReceiveData(USART2); //接收数据
        uint8_t c = 0;
        uart_device_info *dev = (uart_device_info *)g_J3RS485_USART2_handle;
        c = USART2_Rbuffer[0];
        if(!RingQueueFull(&dev->uart_ring_queue))
           InRingQueue(&dev->uart_ring_queue, c);
    }
}

void USART3_IRQHandler(void)
{
    if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //中断产生
    {
        USART3_Rbuffer[0] = USART_ReceiveData(USART3); //接收数据
        uint8_t c = 0;
        uart_device_info *dev = (uart_device_info *)g_J50RS485_USART3_handle;
        c = USART3_Rbuffer[0];
        if(!RingQueueFull(&dev->uart_ring_queue))
           InRingQueue(&dev->uart_ring_queue, c);

    }
}

//void UART4_IRQHandler(void)
//{
//    if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET) //中断产生
//    {
//        UART4_Rbuffer[0] = USART_ReceiveData(UART4); //接收数据
//        uint8_t c = 0;
//        uart_device_info *dev = (uart_device_info *)g_LORA_UART4_handle;
//        c = UART4_Rbuffer[0];
//        if(!RingQueueFull(&dev->uart_ring_queue))
//           InRingQueue(&dev->uart_ring_queue, c);
//    }
//}

void UART5_IRQHandler(void)
{
    if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET) //中断产生
    {
        UART5_Rbuffer[0] = USART_ReceiveData(UART5); //接收数据
        uint8_t c = 0;
        uart_device_info *dev = (uart_device_info *)g_Upward_UART5_handle;
        c = UART5_Rbuffer[0];
        if(!RingQueueFull(&dev->uart_ring_queue))
           InRingQueue(&dev->uart_ring_queue, c);
    }
}

void UART6_IRQHandler(void)
{
    if(USART_GetITStatus(UART6, USART_IT_RXNE) != RESET) //中断产生
    {
        UART6_Rbuffer[0] = USART_ReceiveData(UART6); //接收数据
        uint8_t c = 0;
        uart_device_info *dev = (uart_device_info *)g_J1RS485_UART6_handle;
        c = UART6_Rbuffer[0];
        if(!RingQueueFull(&dev->uart_ring_queue))
           InRingQueue(&dev->uart_ring_queue, c);
    }
}

void UART7_IRQHandler(void)
{
    if(USART_GetITStatus(UART7, USART_IT_RXNE) != RESET) //中断产生
    {
        UART7_Rbuffer[0] = USART_ReceiveData(UART7); //接收数据
        uint8_t c = 0;
        uart_device_info *dev = (uart_device_info *)g_J2RS485_UART7_handle;
        c = UART7_Rbuffer[0];
        if(!RingQueueFull(&dev->uart_ring_queue))
           InRingQueue(&dev->uart_ring_queue, c);
    }
}

void UART8_IRQHandler(void)
{
    if(USART_GetITStatus(UART8, USART_IT_RXNE) != RESET) //中断产生
    {
        UART8_Rbuffer[0] = USART_ReceiveData(UART8); //接收数据
        uint8_t c = 0;
        uart_device_info *dev = (uart_device_info *)g_J4RS485_UART8_handle;
        c = UART8_Rbuffer[0];
        if(!RingQueueFull(&dev->uart_ring_queue))
           InRingQueue(&dev->uart_ring_queue, c);
    }
}

















