mirror of
https://gitea.ecohim.ru:3000/RS485_Relay/RS485_Relay2_fw.git
synced 2025-08-07 16:20:30 +03:00
103 lines
2.3 KiB
C
103 lines
2.3 KiB
C
#include "uart.h"
|
|
#include "main.h"
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
|
|
uint8_t * uart_tx_buf;
|
|
uint8_t uart_tx_busy=0;
|
|
uint16_t uart_tx_counter=0;
|
|
uint16_t uart_tx_len=0;
|
|
|
|
uint8_t * uart_rx_buf;
|
|
uint8_t uart_rx_busy=0;
|
|
uint16_t uart_rx_counter=0;
|
|
uint16_t uart_rx_len=0;
|
|
extern uint16_t last_rx_time;
|
|
|
|
extern UART_HandleTypeDef huart1;
|
|
|
|
#define TXEN_ON HAL_GPIO_WritePin(TXEN_GPIO_Port, TXEN_Pin, GPIO_PIN_SET)
|
|
#define TXEN_OFF HAL_GPIO_WritePin(TXEN_GPIO_Port, TXEN_Pin, GPIO_PIN_RESET)
|
|
|
|
void USER_UART1_IRQHandler(UART_HandleTypeDef *huart)
|
|
{
|
|
uint8_t data;
|
|
|
|
if((huart1.Instance->ISR & USART_ISR_RXNE) != RESET)
|
|
{
|
|
last_rx_time = 0;
|
|
data = (uint8_t)(huart1.Instance->RDR & (uint8_t)0x00FF); // читает байт из регистра
|
|
if(uart_rx_busy)
|
|
{
|
|
uart_rx_buf[uart_rx_counter]=data;
|
|
if(++uart_rx_counter == uart_rx_len)
|
|
{
|
|
uart_rx_busy = 0;
|
|
UART_RxCpltCallback();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
if((uart_tx_busy)&&((huart1.Instance->ISR & USART_ISR_TC) != RESET))
|
|
{
|
|
if(++uart_tx_counter == uart_tx_len)
|
|
{
|
|
TXEN_OFF;
|
|
uart_tx_busy = 0;
|
|
UART_TxCpltCallback();
|
|
__HAL_UART_DISABLE_IT(&huart1, UART_IT_TC);
|
|
}
|
|
else
|
|
//putchar(uart_tx_buf[uart_tx_counter]);
|
|
huart->Instance->TDR=uart_tx_buf[uart_tx_counter];
|
|
}
|
|
huart->Instance->ICR=0xFFFFFFFF;
|
|
}
|
|
|
|
uint8_t UART_Receive_IT(uint8_t * data, uint16_t len, uint16_t timeout)
|
|
{
|
|
uint16_t i;
|
|
|
|
//if(len>UART_RX_LEN)return RET_OVERFLOW;
|
|
for(i=0;i<timeout;i++)
|
|
{
|
|
if(uart_rx_busy!=0)
|
|
HAL_Delay(1);
|
|
else
|
|
break;
|
|
}
|
|
if(i==timeout) return RET_TIMEOUT;
|
|
uart_rx_counter = 0;
|
|
uart_rx_len = len;
|
|
uart_rx_buf = data;
|
|
uart_rx_busy = 1;
|
|
return RET_OK;
|
|
}
|
|
|
|
|
|
uint8_t UART_Transmit_IT(uint8_t * data, uint16_t len, uint16_t timeout)
|
|
{
|
|
uint16_t i;
|
|
|
|
for(i=0;i<timeout;i++)
|
|
{
|
|
if(uart_tx_busy!=0)
|
|
HAL_Delay(1);
|
|
else
|
|
break;
|
|
}
|
|
if(i==timeout) return RET_TIMEOUT;
|
|
uart_tx_busy = 1;
|
|
TXEN_ON;
|
|
HAL_Delay(1);
|
|
uart_tx_buf = data;
|
|
uart_tx_counter = 0;
|
|
uart_tx_len = len;
|
|
huart1.Instance->TDR=uart_tx_buf[0];
|
|
//putchar(uart_tx_buf[0]);
|
|
__HAL_UART_ENABLE_IT(&huart1, UART_IT_TC);
|
|
return RET_OK;
|
|
}
|