/*
 * J5-0_USART3.c
 *
 *  Created on: 2024年5月18日
 *      Author: 34509
 */
#include "J5-0_USART3.h"



void USART3_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));



uint8_t USART_Rbuffer_Num = 0;
uint8_t USART_Tbuffer_Num = 0;
/* 接收缓冲区数组 */
uint8_t USART_Rbuffer[USART3_buffer_len];

/*
 * Function Name  : J5_0_485_Init
 * Description    : 初始话串口3用于控制J5-0共6个485接口,设置控制的485为J0.
 * Input          : None
 * Return         : None
 **/
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++;
    }

//    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);
}
