pile_com_stm32/main/stm32/comm.c
2024-01-20 17:44:50 +08:00

105 lines
2.1 KiB
C

#include "stdlib.h"
#include "utils.h"
#include "fram.h"
#include "ModbusS.h"
#include "ads1220.h"
// #include "SEGGER_RTT.h"
#include "ModbusM.h"
extern void modbus_master_poll(int n);
extern int modbus_master_on_revice(int ch, uint8_t *rxbuf, int length);
extern uint8_t uart1_txbuf[];
extern uint8_t uart1_rxbuf[];
extern uint8_t uart2_txbuf[];
extern uint8_t uart2_rxbuf[];
extern int uart1_rx_start(void);
extern int uart1_rx_check(void);
extern int uart1_tx_start(uint8_t *data, int length);
extern int uart2_rx_start(void);
extern int uart2_rx_check(void);
extern int uart2_tx_start(uint8_t *data, int length);
void RS485_init();
void print_init();
extern uint8_t led_toggle_count;
void comm_poll(void)
{
static uint32_t modbus_master_tick = 0;
static uint32_t tick_100ms = 0;
static int uart1_timeout = 0;
static int uart2_timeout = 0;
int rxlen,txlen;
// if((rxlen = uart1_rx_check()) > 0){
// txlen = ModbusSlaveProcess(uart1_txbuf,uart1_rxbuf,rxlen,1);
// if(txlen > 0)
// {
// HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
// uart1_tx_start(uart1_txbuf,txlen);
// }
// else{
// uart1_rx_start();
// }
// }
if((rxlen = uart2_rx_check()) > 0){
txlen = ModbusSlaveProcess(uart2_txbuf,uart2_rxbuf,rxlen,1);
if(txlen > 0)
{
uart2_tx_start(uart2_txbuf,txlen);
}
else{
uart2_rx_start();
}
led_toggle_count = 0;
uart1_timeout = 0;
}
if(TickDiff(tick_100ms) > 100)
{
tick_100ms = xTaskGetTickCount();
if(++uart1_timeout > 100){
uart1_timeout = 0;
// HAL_UART_MspDeInit(&huart2);
// MX_USART2_UART_Init();
uart2_rx_start();
}
if(++uart2_timeout > 1000){
uart2_timeout = 0;
// HAL_UART_MspDeInit(&huart1);
// MX_USART1_UART_Init();
}
}
if(TickDiff(modbus_master_tick) > 10)
{
modbus_master_tick = xTaskGetTickCount();
modbus_master_poll(0);
}
if((rxlen = uart1_rx_check()) > 0){ // 做一个uart1_rx_check 读串口
modbus_master_on_revice(0,uart1_rxbuf,rxlen);
uart2_timeout = 0;
}
}
void comm_init(void)
{
// MX_USART1_UART_Init();
// MX_USART2_UART_Init();
RS485_init();
print_init();
ModbusM_init();
uart2_rx_start();
}