gateway/Hardware/src/J5-0_USART3.c

282 lines
6.6 KiB
C
Raw Normal View History

2024-07-13 02:22:47 +00:00
/*
* J5-0_USART3.c
*
* Created on: 2024<EFBFBD><EFBFBD>5<EFBFBD><EFBFBD>18<EFBFBD><EFBFBD>
* 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;
/* <20><><EFBFBD>ջ<EFBFBD><D5BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
uint8_t USART_Rbuffer[USART3_buffer_len];
/*
* Function Name : J5_0_485_Init
* Description : <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>3<EFBFBD><EFBFBD><EFBFBD>ڿ<EFBFBD><EFBFBD><EFBFBD>J5-0<EFBFBD><EFBFBD>6<EFBFBD><EFBFBD>485<EFBFBD>ӿ<EFBFBD>,<EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD>Ƶ<EFBFBD>485ΪJ0.
* Input : None
* Return : None
**/
void J5_0_485_Init(void)
{
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);
/* <20><><EFBFBD><EFBFBD>485״̬Ϊ<CCAC><CEAA> */
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);
/* <20><><EFBFBD><EFBFBD>USART3<54><33><EFBFBD><EFBFBD>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; //<2F><><EFBFBD><EFBFBD>PB10Ϊ<30><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //<2F><><EFBFBD><EFBFBD>PB11Ϊ<31><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_Init(GPIOB, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
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; //<2F><>ռ<EFBFBD><D5BC><EFBFBD>ȼ<EFBFBD>Ϊ1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //<2F><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>Ϊ1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨ<51><CDA8>ʹ<EFBFBD><CAB9>
NVIC_Init(&NVIC_InitStructure); //<2F>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD>ʼ<EFBFBD><CABC>
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) //<2F>жϲ<D0B6><CFB2><EFBFBD>
{
// USART_ClearITPendingBit(USART3,USART_IT_RXNE); //<2F><><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־
USART_Rbuffer[USART_Rbuffer_Num] = USART_ReceiveData(USART3); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
USART_Rbuffer_Num++;
}
if(USART_GetITStatus(USART3, USART_IT_IDLE) != RESET) //<2F>жϲ<D0B6><CFB2><EFBFBD>
{
// for(USART_Tbuffer_Num=0;USART_Tbuffer_Num < USART_Rbuffer_Num;USART_Tbuffer_Num++)
// {
// USARTx_SendByte(USART3, USART_Rbuffer[USART_Tbuffer_Num]); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// }
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; //<2F><>ʼ<EFBFBD><CABC>
USART_ReceiveData(USART3); //<2F><>DR
}
if(USART_GetFlagStatus(USART3,USART_FLAG_ORE) == SET) //<2F><><EFBFBD><EFBFBD>
{
USART_ClearFlag(USART3,USART_FLAG_ORE); //<2F><><EFBFBD><EFBFBD>־
USART_ReceiveData(USART3); //<2F><>DR
}
}
/*
* Function Name : USART_CONNET_J0
* Description : <EFBFBD><EFBFBD><EFBFBD>ƴ<EFBFBD><EFBFBD><EFBFBD>3<EFBFBD><EFBFBD><EFBFBD><EFBFBD>J0<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>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 : <EFBFBD><EFBFBD><EFBFBD>ƴ<EFBFBD><EFBFBD><EFBFBD>3<EFBFBD><EFBFBD><EFBFBD><EFBFBD>J5<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>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 : <EFBFBD><EFBFBD><EFBFBD>ƴ<EFBFBD><EFBFBD><EFBFBD>3<EFBFBD><EFBFBD><EFBFBD><EFBFBD>J6<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>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 : <EFBFBD><EFBFBD><EFBFBD>ƴ<EFBFBD><EFBFBD><EFBFBD>3<EFBFBD><EFBFBD><EFBFBD><EFBFBD>J7<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>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 : <EFBFBD><EFBFBD><EFBFBD>ƴ<EFBFBD><EFBFBD><EFBFBD>3<EFBFBD><EFBFBD><EFBFBD><EFBFBD>J8<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>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 : <EFBFBD><EFBFBD><EFBFBD>ƴ<EFBFBD><EFBFBD><EFBFBD>3<EFBFBD><EFBFBD><EFBFBD><EFBFBD>J9<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>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);
}