mppt/Hardware/src/rs485.c

392 lines
12 KiB
C
Raw Normal View History

2024-07-11 06:58:55 +00:00
/*
* rs485.c
*
* Created on: 2024<EFBFBD><EFBFBD>6<EFBFBD><EFBFBD>20<EFBFBD><EFBFBD>
* Author: psx
*/
#include "rs485.h"
#include "uart_dev.h"
#include "ring_queue.h"
2024-10-26 01:07:30 +00:00
#include "pdebug.h"
2024-11-20 06:12:33 +00:00
#include "uart_send.h"
2024-07-11 06:58:55 +00:00
void USART3_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void USART4_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
//#define USART3_buffer_len 128
//#define USART4_buffer_len 128
//uint8_t USART3_Rbuffer_Num = 0;
//uint8_t USART4_Rbuffer_Num = 0;
/* <20><><EFBFBD>ջ<EFBFBD><D5BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
uint8_t USART3_Rbuffer[1] = {0x00};
uint8_t USART4_Rbuffer[1] = {0x00};
2024-11-13 08:44:13 +00:00
//uint8_t USART_Rbuffer[100] = {0x00};
//uint8_t USART_RbufferLen = 0;
2024-07-11 06:58:55 +00:00
///* <20><><EFBFBD><EFBFBD> */
//uint8_t USART3_Tbuffer_Num = 0;
//uint8_t USART3_Tbuffer_Len = 0;
//uint8_t *USART3_Tbuffer;
2024-11-20 06:12:33 +00:00
uart_send_info *GwSendInfo;
uart_send_info *BatSendInfo;
2024-07-11 06:58:55 +00:00
/*
* @brief <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>GW485
* @param
* @retval
*/
void GW_485_Init(int baud)
{
/* <20><>ʼ<EFBFBD><CABC>GW485<38><35><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = Pin_GW_485_RDE;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //<2F><><EFBFBD><EFBFBD>PB4Ϊ<34><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_Init(GPIO_GW_485_RDE, &GPIO_InitStructure);
GPIO_WriteBit(GPIO_GW_485_RDE, Pin_GW_485_RDE, read);
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* USART4 TX --> PB0 RX --> PB1 */
RCC_PB2PeriphClockCmd(RCC_PB2Periph_GPIOB, ENABLE);
RCC_PB1PeriphClockCmd(RCC_PB1Periph_USART4, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //<2F><><EFBFBD><EFBFBD>PB0Ϊ<30><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //<2F><><EFBFBD><EFBFBD>PB1Ϊ<31><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_Init(GPIOB, &GPIO_InitStructure);
2024-11-13 08:44:13 +00:00
USART_InitStructure.USART_BaudRate = baud;
// USART_InitStructure.USART_BaudRate = 9600;
// USART_InitStructure.USART_BaudRate = 115200;
2024-07-11 06:58:55 +00:00
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(USART4, &USART_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART4_IRQn;
2024-10-26 01:07:30 +00:00
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //<2F><>ռ<EFBFBD><D5BC><EFBFBD>ȼ<EFBFBD>Ϊ1
2024-07-11 06:58:55 +00:00
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //<2F><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>Ϊ2
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(USART4, USART_IT_RXNE, ENABLE);
2024-11-20 06:12:33 +00:00
USART_ITConfig(USART4, USART_IT_IDLE, ENABLE);
2024-07-11 06:58:55 +00:00
USART_Cmd(USART4,ENABLE);
}
/*
* @brief <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>BAT_485
* @param
* @retval
*/
void BAT_485_Init(int baud)
{
/* <20><>ʼ<EFBFBD><CABC>GW485<38><35><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = Pin_BAT_485_RDE;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //<2F><><EFBFBD><EFBFBD>PB5Ϊ<35><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_Init(GPIO_BAT_485_RDE, &GPIO_InitStructure);
GPIO_WriteBit(GPIO_BAT_485_RDE, Pin_BAT_485_RDE, read);
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* USART3 TX --> PB10 RX --> PB11 */
RCC_PB2PeriphClockCmd(RCC_PB2Periph_GPIOB, ENABLE);
RCC_PB1PeriphClockCmd(RCC_PB1Periph_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 = 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;
2024-10-26 01:07:30 +00:00
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //<2F><>ռ<EFBFBD><D5BC><EFBFBD>ȼ<EFBFBD>Ϊ1
2024-07-11 06:58:55 +00:00
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);
2024-11-20 06:12:33 +00:00
USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);
2024-07-11 06:58:55 +00:00
USART_Cmd(USART3,ENABLE);
}
/*
* @brief ͨ<EFBFBD><EFBFBD>485<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
* @param pUSARTx <EFBFBD><EFBFBD>Ӧ<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param data <EFBFBD>ַ<EFBFBD>
* @retval
*/
void USARTx_SendByte(USART_TypeDef* pUSARTx, uint8_t data)
{
// if (pUSARTx == GW_485) {
// GPIO_WriteBit(GPIO_GW_485_RDE, Pin_GW_485_RDE, write);
// }
// else if (pUSARTx == BAT_485) {
// GPIO_WriteBit(GPIO_BAT_485_RDE, Pin_BAT_485_RDE, write);
// }
// else {
// return;
// }
USART_SendData(pUSARTx, data);
while(USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
//
// if (pUSARTx == GW_485) {
// GPIO_WriteBit(GPIO_GW_485_RDE, Pin_GW_485_RDE, read);
// }
// else if (pUSARTx == BAT_485) {
// GPIO_WriteBit(GPIO_BAT_485_RDE, Pin_BAT_485_RDE, read);
// }
// else {
// return;
// }
}
///*
// * @brief ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD>ڷ<EFBFBD><DAB7><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֽ<EFBFBD>
// * @param pUSARTx <20><>Ӧ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD>
// * @param data <20>ַ<EFBFBD>
// * @retval
// */
//void USARTx_SendByte_str(USART_TypeDef* pUSARTx, uint8_t data)
//{
// USART_SendData(pUSARTx, data);
// while(USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
//}
//
///*
// * @brief ͨ<><CDA8>485<38><35><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
// * @param pUSARTx <20><>Ӧ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD>
// * @param data <20>ַ<EFBFBD><D6B7><EFBFBD>
// * @retval
// */
//void USARTx_SendStr(USART_TypeDef* pUSARTx, char *str)
//{
// if (pUSARTx == GW_485) {
// GPIO_WriteBit(GPIO_GW_485_RDE, Pin_GW_485_RDE, write);
// }
// else if (pUSARTx == BAT_485) {
// GPIO_WriteBit(GPIO_BAT_485_RDE, Pin_BAT_485_RDE, 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 == GW_485) {
// GPIO_WriteBit(GPIO_GW_485_RDE, Pin_GW_485_RDE, read);
// }
// else if (pUSARTx == BAT_485) {
// GPIO_WriteBit(GPIO_BAT_485_RDE, Pin_BAT_485_RDE, read);
// }
// else {
// return;
// }
//}
//
///*
// * @brief ͨ<><CDA8>485<38><35><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// * @param pUSARTx <20><>Ӧ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD>
// * @param data <20>ַ<EFBFBD><D6B7><EFBFBD>
// * @retval
// */
//void USARTx_SendStr_Len(USART_TypeDef* pUSARTx, char *str, int len)
//{
// if (pUSARTx == GW_485) {
// GPIO_WriteBit(GPIO_GW_485_RDE, Pin_GW_485_RDE, write);
// }
// else if (pUSARTx == BAT_485) {
// GPIO_WriteBit(GPIO_BAT_485_RDE, Pin_BAT_485_RDE, 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 == GW_485) {
// GPIO_WriteBit(GPIO_GW_485_RDE, Pin_GW_485_RDE, read);
// }
// else if (pUSARTx == BAT_485) {
// GPIO_WriteBit(GPIO_BAT_485_RDE, Pin_BAT_485_RDE, read);
// }
// else {
// return;
// }
//}
void USARTx_ITSendstr(USART_TypeDef* pUSARTx, char *str, int len)
{
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
}
/*
* @brief USART3<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param
* @retval
*/
void USART3_IRQHandler(void)
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //<2F>жϲ<D0B6><CFB2><EFBFBD>
{
// printf(" in usart3 \n");
// USART_ClearITPendingBit(USART3,USART_IT_RXNE); //<2F><><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־
2024-11-20 06:12:33 +00:00
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>߲<EFBFBD><DFB2><EFBFBD><EFBFBD><EFBFBD> */
uart_send.idleStateBat = 0;
uart_send.BatState = 0;
2024-07-11 06:58:55 +00:00
USART3_Rbuffer[0] = USART_ReceiveData(USART3); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
uart_device_info *dev = (uart_device_info *)g_bat485_uart3_handle;
if(!RingQueueFull(&dev->uart_ring_queue))
2024-11-20 06:12:33 +00:00
InRingQueue(&dev->uart_ring_queue, USART3_Rbuffer[0]);
2024-07-11 06:58:55 +00:00
}
2024-11-20 06:12:33 +00:00
if(USART_GetITStatus(USART3, USART_IT_IDLE) != RESET) //<2F>жϲ<D0B6><CFB2><EFBFBD>
{
uart_send.idleStateGw = 1;
USART_ReceiveData(USART3);
}
// if(USART_GetITStatus(USART3, USART_FLAG_TXE) != RESET) //<2F>жϷ<D0B6><CFB7><EFBFBD>
2024-07-11 06:58:55 +00:00
// {
2024-11-20 06:12:33 +00:00
// USART_SendData(USART3, uart_send.sendDataBat->data[uart_send.sendDataBat->Counter++]);
// if (uart_send.sendDataBat->dataLen == uart_send.sendDataBat->Counter) {
// USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
// uart_send.sendOverStateBat = 1;
// uart_send.sendStateBat = 0;
// uart_send.insertState++;
//// printf("\n\n bat uart_send.insertState : %d \n\n", uart_send.insertState);
// /* <20><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>ָ<EFBFBD><D6B8>Ϊ<EFBFBD><CEAA>ʱ */
// if (uart_send.insertData == NULL) {
// if (!uart_send.data1.dataState) {
// uart_send.insertData = &uart_send.data1;
// }
// if (!uart_send.data2.dataState) {
// uart_send.insertData = &uart_send.data2;
// }
// if (!uart_send.data3.dataState) {
// uart_send.insertData = &uart_send.data3;
// }
// }
// Delay_Us(100);
// bat485_tx_disenabla();
2024-07-11 06:58:55 +00:00
// }
// }
}
/*
* @brief USART4<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param
* @retval
*/
void USART4_IRQHandler(void)
{
if(USART_GetITStatus(USART4, USART_IT_RXNE) != RESET) //<2F>жϲ<D0B6><CFB2><EFBFBD>
{
2024-11-20 06:12:33 +00:00
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>߲<EFBFBD><DFB2><EFBFBD><EFBFBD><EFBFBD> */
uart_send.GwState = 0;
uart_send.idleStateGw = 0;
2024-11-13 08:44:13 +00:00
2024-07-11 06:58:55 +00:00
USART4_Rbuffer[0] = USART_ReceiveData(USART4); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
uart_device_info *dev = (uart_device_info *)g_gw485_uart4_handle;
if(!RingQueueFull(&dev->uart_ring_queue))
2024-10-26 01:07:30 +00:00
InRingQueue(&dev->uart_ring_queue, USART4_Rbuffer[0]);
2024-07-11 06:58:55 +00:00
}
2024-11-20 06:12:33 +00:00
if(USART_GetITStatus(USART4, USART_IT_IDLE) != RESET) //<2F>жϲ<D0B6><CFB2><EFBFBD>
{
uart_send.idleStateGw = 1;
USART_ReceiveData(USART4);
}
// if(USART_GetITStatus(USART4, USART_FLAG_TXE) != RESET) //<2F>жϷ<D0B6><CFB7><EFBFBD>
// {
//// USART_SendData(USART4, uart_send.sendDataGw->data[uart_send.sendDataGw->Counter++]);
//
//// USART_SendData(USART4, USART_Tbuffer[count++]);
//// if (len == count) {
//// USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
//////// }
////// if (uart_send.sendDataGw->dataLen <= uart_send.sendDataGw->Counter) {
////// USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
////// uart_send.sendOverStateGw = 1;
////// uart_send.sendStateGw = 0;
////// uart_send.insertState++;
////// printf("\n\n Gw uart_send.insertState : %d \n\n", uart_send.insertState);
////// /* <20><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>ָ<EFBFBD><D6B8>Ϊ<EFBFBD><CEAA>ʱ */
////// if (uart_send.insertData == NULL) {
////// if (!uart_send.data1.dataState) {
////// uart_send.insertData = &uart_send.data1;
////// }
////// if (!uart_send.data2.dataState) {
////// uart_send.insertData = &uart_send.data2;
////// }
////// if (!uart_send.data3.dataState) {
////// uart_send.insertData = &uart_send.data3;
////// }
////// }
//// Delay_Us(100);
//// gw485_tx_disenabla();
//// }
// }
2024-07-11 06:58:55 +00:00
// if(USART_GetFlagStatus(USART4,USART_FLAG_TC) == SET) //<2F>жϷ<D0B6><CFB7><EFBFBD>
// {
// USART_SendData(USART4, USART4_Tbuffer[USART3_Tbuffer_Num++]);
// if (USART4_Tbuffer_Len == USART4_Tbuffer_Num) {
// USART4_Tbuffer_Num = 0;
// USART_ITConfig(USART4, USART_IT_TC, DISABLE);
//// USART_ClearFlag(USART3,USART_FLAG_ORE); //<2F><><EFBFBD><EFBFBD>־
//// USART_ReceiveData(USART3); //<2F><>DR
// }
// }
}