mppt/App/src/uart_dev.c

318 lines
6.9 KiB
C
Raw Normal View History

2024-07-11 06:58:55 +00:00
/*
* uart_dev.c
*
* Created on: 2024<EFBFBD><EFBFBD>6<EFBFBD><EFBFBD>22<EFBFBD><EFBFBD>
* Author: psx
*/
#include "uart_dev.h"
#include "inflash.h"
/* ʹ<><CAB9>485<38><35><EFBFBD><EFBFBD> */
#define rs485_send_enable 1
static void uart_init(uartIndex_e uart_index, int baud);
static uint8_t uart_putchar(device_handle device, char ch);
device_handle g_bat485_uart3_handle;
device_handle g_gw485_uart4_handle;
static uint8_t bat485_in_buff[200];
static uint8_t gw485_in_buff[300];
uint8_t rs485_out_buff[100];
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ںż<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
* @param uart_index <EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ں<EFBFBD>
* @param uart_baudrate <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
uart_device_info uart_devices[]={
[0] = {
.init = 0,
.uart_index = BAT485_UART_INDEX,
.uart_baudrate = 9600,
},
[1] = {
.init = 0,
.uart_index = GW485_UART_INDEX,
.uart_baudrate = 9600,
},
};
/**
* @brief <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
* @param uart_index <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ں<EFBFBD>
* @param buff <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><EFBFBD>buff<EFBFBD><EFBFBD>ַ
* @param buff_size <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><EFBFBD>buff<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>С
* @retval <EFBFBD><EFBFBD><EFBFBD>ھ<EFBFBD><EFBFBD><EFBFBD>
*/
//#define ELEMENT_OF(x) (sizeof(x) / sizeof((x)[0]))
//device_handle uart_dev_init(uartIndex_e uart_index, uint8_t *buff, int buff_size)
//{
// int i = 0;
// for(; i < ELEMENT_OF(uart_devices); i++){
// if(uart_devices[i].uart_index == uart_index){
// if(!uart_devices[i].init){
// InitRingQueue(&uart_devices[i].uart_ring_queue, buff, buff_size);
// uart_init(uart_index, uart_devices[i].uart_baudrate);
//
// uart_devices[i].init = 1;
// }
// return (device_handle)(&uart_devices[i]);
// }
// }
// return 0;
//}
device_handle uart_dev_init(void)
{
// int i = 0;
// for(; i < ELEMENT_OF(uart_devices); i++){
// if(uart_devices[i].uart_index == uart_index){
// if(!uart_devices[i].init){
// InitRingQueue(&uart_devices[i].uart_ring_queue, buff, buff_size);
// uart_init(uart_index, uart_devices[i].uart_baudrate);
//
// uart_devices[i].init = 1;
// }
// return (device_handle)(&uart_devices[i]);
// }
// }
InitRingQueue(&uart_devices[0].uart_ring_queue, bat485_in_buff, sizeof(bat485_in_buff));
2024-08-29 02:50:22 +00:00
uart_init(BAT485_UART_INDEX, g_slConfigInfo.baud_485);
2024-07-11 06:58:55 +00:00
uart_devices[0].init = 1;
g_bat485_uart3_handle = (device_handle)(&uart_devices[0]);
InitRingQueue(&uart_devices[1].uart_ring_queue, gw485_in_buff, sizeof(gw485_in_buff));
2024-08-29 02:50:22 +00:00
uart_init(GW485_UART_INDEX, g_slConfigInfo.baud_485);
2024-07-11 06:58:55 +00:00
uart_devices[1].init = 1;
g_gw485_uart4_handle = (device_handle)(&uart_devices[1]);
return 0;
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>.
* @param uart_index <EFBFBD><EFBFBD><EFBFBD>ں<EFBFBD>
* @param baud <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @retval None
*/
void uart_init(uartIndex_e uart_index, int baud)
{
if (uart_index == BAT485_UART_INDEX) {
2024-09-18 01:47:44 +00:00
// BAT_485_Init(uart_devices[0].uart_baudrate);
BAT_485_Init(115200);
2024-07-11 06:58:55 +00:00
} else if (uart_index == GW485_UART_INDEX) {
GW_485_Init(uart_devices[1].uart_baudrate);
}
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>.
* @param uart_index <EFBFBD><EFBFBD><EFBFBD>ں<EFBFBD>
* @param ch <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>
* @retval 1 <EFBFBD>ɹ<EFBFBD> 0ʧ<EFBFBD><EFBFBD>
*/
uint8_t uart_putchar(device_handle device, char ch)
{
uart_device_info *device_info = (uart_device_info *)device;
if((!device) || (!device_info->init))
return 0;
if (device_info->uart_index == BAT485_UART_INDEX) {
USARTx_SendByte(BAT_485, ch);
} else if(device_info->uart_index == GW485_UART_INDEX) {
USARTx_SendByte(GW_485, ch);
}
return 1;
}
/**
* @brief bat485<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>.
* @param
* @retval
*/
void bat485_tx_enabla(void)
{
GPIO_WriteBit(GPIO_BAT_485_RDE, Pin_BAT_485_RDE, write);
}
/**
* @brief bat485<EFBFBD><EFBFBD><EFBFBD>͹ر<EFBFBD>.
* @param
* @retval
*/
void bat485_tx_disenabla(void)
{
GPIO_WriteBit(GPIO_BAT_485_RDE, Pin_BAT_485_RDE, read);
}
/**
* @brief gw485<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>.
* @param
* @retval
*/
void gw485_tx_enabla(void)
{
GPIO_WriteBit(GPIO_GW_485_RDE, Pin_GW_485_RDE, write);
}
/**
* @brief gw485<EFBFBD><EFBFBD><EFBFBD>͹ر<EFBFBD>.
* @param
* @retval
*/
void gw485_tx_disenabla(void)
{
GPIO_WriteBit(GPIO_GW_485_RDE, Pin_GW_485_RDE, read);
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>.
* @param uart_index <EFBFBD><EFBFBD><EFBFBD>ں<EFBFBD>
* @param str <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
* @retval None
*/
void uart_sendstr(device_handle device,char *str)
{
#if rs485_send_enable
if(device == g_bat485_uart3_handle){
bat485_tx_enabla();
}
else if (device == g_gw485_uart4_handle) {
gw485_tx_enabla();
}
#endif
while ((*str) != (char )0u) {
if (*str == ASCII_CHAR_LINE_FEED){
uart_putchar(device, (ASCII_CHAR_CARRIAGE_RETURN));
uart_putchar(device, ASCII_CHAR_LINE_FEED);
str++;
}else{
uart_putchar(device, *str++);
}
}
#if rs485_send_enable
if(device == g_bat485_uart3_handle){
bat485_tx_disenabla();
}
else if (device == g_gw485_uart4_handle) {
gw485_tx_disenabla();
}
#endif
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><EFBFBD>ֽڷ<EFBFBD><EFBFBD><EFBFBD>.
* @param device <EFBFBD><EFBFBD><EFBFBD>ھ<EFBFBD><EFBFBD><EFBFBD>
* @param data <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param len <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>
* @retval None
*/
void uart_dev_write(device_handle device, void *data, int len)
{
#if rs485_send_enable
if(device == g_bat485_uart3_handle){
bat485_tx_enabla();
}
else if (device == g_gw485_uart4_handle) {
gw485_tx_enabla();
}
#endif
for (int i = 0; i < len; i++) {
2024-09-18 01:47:44 +00:00
// uart_putchar(device, ((u_int8_t *)data)[i]);
uart_putchar(device, ((char *)data)[i]);
2024-07-11 06:58:55 +00:00
}
2024-09-18 01:47:44 +00:00
Delay_Ms(1);
2024-07-11 06:58:55 +00:00
#if rs485_send_enable
if(device == g_bat485_uart3_handle){
bat485_tx_disenabla();
}
else if (device == g_gw485_uart4_handle) {
gw485_tx_disenabla();
}
#endif
}
/**
* @brief <EFBFBD>жϴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><EFBFBD>buff<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
* @param device <EFBFBD><EFBFBD><EFBFBD>ھ<EFBFBD><EFBFBD><EFBFBD>
* @retval 0 <EFBFBD><EFBFBD> 1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
int uart_dev_char_present(device_handle device)
{
uart_device_info *device_info = (uart_device_info *)device;
if((!device) || (!device_info->init))
return 0;
return !RingQueueEmpty(&device_info->uart_ring_queue);
}
/**
* @brief <EFBFBD>Ӵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><EFBFBD>buff<EFBFBD><EFBFBD>ȡһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
* @param device <EFBFBD><EFBFBD><EFBFBD>ھ<EFBFBD><EFBFBD><EFBFBD>
* @retval <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>
*/
char uart_dev_in_char(device_handle device)
{
uart_device_info *device_info = (uart_device_info *)device;
char c = 0;
if (uart_dev_char_present(device))
OutRingQueue(&device_info->uart_ring_queue, (u_int8_t*)&c);
return c;
}
/**
* @brief <EFBFBD>ж<EFBFBD>ѭ<EFBFBD><EFBFBD>buff<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
* @param ring_queue ѭ<EFBFBD><EFBFBD>buff<EFBFBD><EFBFBD>ַ
* @retval 0 <EFBFBD><EFBFBD> 1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
int ring_queue_dev_char_present(RingQueue *ring_queue)
{
return !RingQueueEmpty(ring_queue);
}
/**
* @brief <EFBFBD><EFBFBD>ѭ<EFBFBD><EFBFBD>buff<EFBFBD><EFBFBD>ȡһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
* @param ring_queue ѭ<EFBFBD><EFBFBD>buff<EFBFBD><EFBFBD>ַ
* @retval <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>
*/
char ring_queue_dev_in_char(RingQueue *ring_queue)
{
char c = 0;
if (ring_queue_dev_char_present(ring_queue))
OutRingQueue(ring_queue, (u_int8_t*)&c);
return c;
}
/**
* @brief <EFBFBD>ѭ<EFBFBD><EFBFBD>buff<EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݸ<EFBFBD><EFBFBD><EFBFBD>.
* @param ring_queue ѭ<EFBFBD><EFBFBD>buff<EFBFBD><EFBFBD>ַ
* @retval ѭ<EFBFBD><EFBFBD>buff<EFBFBD>еĸ<EFBFBD><EFBFBD><EFBFBD>
*/
int ring_queue_length(device_handle device)
{
uart_device_info *device_info = (uart_device_info *)device;
return RingQueueLength(&device_info->uart_ring_queue);
}