gateway/Hardware/src/UART.c

897 lines
25 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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