2024-07-05 03:52:43 +00:00
|
|
|
|
#include "uart_dev.h"
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include "timer.h"
|
|
|
|
|
#include "gpio.h"
|
2024-07-09 02:48:29 +00:00
|
|
|
|
#include "inflash.h"
|
2024-07-05 03:52:43 +00:00
|
|
|
|
|
|
|
|
|
extern UART_HandleTypeDef huart1;
|
|
|
|
|
extern UART_HandleTypeDef huart3;
|
|
|
|
|
|
|
|
|
|
static void uart_init(uartIndex_e uart_index, int baud);
|
|
|
|
|
static u_int8_t uart_putchar(device_handle device, char ch);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
device_handle g_term_uart_handle;
|
|
|
|
|
device_handle g_rs485_uart_handle;
|
2024-08-06 07:33:33 +00:00
|
|
|
|
device_handle g_ec801_uart_handle;
|
2024-07-05 03:52:43 +00:00
|
|
|
|
|
|
|
|
|
device_handle uart_dev_init(uartIndex_e uart_index, uint8_t *buff, int buff_size);
|
|
|
|
|
static u_int8_t term_in_buff[200];
|
2024-08-06 07:33:33 +00:00
|
|
|
|
static u_int8_t ec801_in_buff[200];
|
2024-07-05 03:52:43 +00:00
|
|
|
|
static u_int8_t rs485_in_buff[300];
|
|
|
|
|
|
|
|
|
|
u_int8_t rs485_out_buff[100];
|
2024-08-06 07:33:33 +00:00
|
|
|
|
u_int8_t ec801_out_buff[100];
|
2024-07-05 03:52:43 +00:00
|
|
|
|
static u_int8_t term_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 = TERM_UART_INDEX,
|
|
|
|
|
.uart_baudrate = 9600,
|
|
|
|
|
},
|
|
|
|
|
[1] = {
|
|
|
|
|
.init = 0,
|
|
|
|
|
.uart_index = RS485_UART_INDEX,
|
|
|
|
|
.uart_baudrate = 9600,
|
|
|
|
|
},
|
2024-08-06 07:33:33 +00:00
|
|
|
|
[2] = {
|
|
|
|
|
.init = 0,
|
|
|
|
|
.uart_index = EC801_UART_INDEX,
|
|
|
|
|
.uart_baudrate = 115200,
|
|
|
|
|
},
|
2024-07-05 03:52:43 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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>
|
|
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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
|
|
|
|
|
*/
|
|
|
|
|
static void uart_init(uartIndex_e uart_index, int baud)
|
|
|
|
|
{
|
|
|
|
|
if(uart_index == TERM_UART_INDEX){
|
2024-07-08 02:09:40 +00:00
|
|
|
|
// MX_USART1_UART_Init();
|
|
|
|
|
MX_USART1_UART_Init(baud);
|
2024-07-05 03:52:43 +00:00
|
|
|
|
}else if(uart_index == RS485_UART_INDEX){
|
2024-07-08 02:09:40 +00:00
|
|
|
|
MX_USART3_UART_Init(baud);
|
2024-07-08 02:55:37 +00:00
|
|
|
|
// MX_USART3_UART_Init();
|
2024-07-05 03:52:43 +00:00
|
|
|
|
}
|
2024-08-06 07:33:33 +00:00
|
|
|
|
else if(uart_index == EC801_UART_INDEX){
|
|
|
|
|
MX_UART5_Init(baud);
|
|
|
|
|
// MX_USART5_UART_Init();
|
|
|
|
|
}
|
2024-07-05 03:52:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD>رմ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @param uart_index <EFBFBD><EFBFBD><EFBFBD>ں<EFBFBD>
|
|
|
|
|
* @retval None
|
|
|
|
|
*/
|
|
|
|
|
void uart_close(uartIndex_e uart_index)
|
|
|
|
|
{
|
|
|
|
|
if(uart_index == TERM_UART_INDEX){
|
|
|
|
|
HAL_UART_MspDeInit(&huart1);
|
|
|
|
|
}else if(uart_index == RS485_UART_INDEX){
|
|
|
|
|
HAL_UART_MspDeInit(&huart3);
|
2024-08-06 07:33:33 +00:00
|
|
|
|
}else if(uart_index == EC801_UART_INDEX){
|
|
|
|
|
HAL_UART_MspDeInit(&huart5);
|
|
|
|
|
}
|
2024-07-05 03:52:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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>
|
|
|
|
|
*/
|
|
|
|
|
static u_int8_t uart_putchar(device_handle device, char ch)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
int ret=HAL_ERROR;
|
|
|
|
|
|
|
|
|
|
uart_device_info *device_info = (uart_device_info *)device;
|
|
|
|
|
if((!device) || (!device_info->init))
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if(device_info->uart_index == TERM_UART_INDEX){
|
|
|
|
|
ret= HAL_UART_Transmit(&huart1,(uint8_t*)&ch,1,10);
|
|
|
|
|
}else if(device_info->uart_index == RS485_UART_INDEX){
|
|
|
|
|
ret= HAL_UART_Transmit(&huart3,(uint8_t*)&ch,1,10);
|
2024-08-06 07:33:33 +00:00
|
|
|
|
}else if(device_info->uart_index == EC801_UART_INDEX){
|
|
|
|
|
ret= HAL_UART_Transmit(&huart5,(uint8_t*)&ch,1,10);
|
2024-07-05 03:52:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(ret == HAL_OK){
|
|
|
|
|
return 1;
|
|
|
|
|
}else{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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)
|
|
|
|
|
{
|
|
|
|
|
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++);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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 0
|
|
|
|
|
if(device == g_rs485_uart_handle){
|
|
|
|
|
rs485_tx_enable();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
for (int i = 0; i < len; i++){
|
|
|
|
|
uart_putchar(device, ((u_int8_t *)data)[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
if(device == g_rs485_uart_handle){
|
|
|
|
|
rs485_rx_enable();
|
|
|
|
|
}
|
|
|
|
|
#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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**999
|
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD> .
|
|
|
|
|
* @param <EFBFBD>ɱ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
* @retval Null
|
|
|
|
|
*/
|
|
|
|
|
void term_printf(char *format, ...)
|
|
|
|
|
{
|
|
|
|
|
memset(term_out_buff, 0, sizeof(term_out_buff));
|
|
|
|
|
va_list va_ptr;
|
|
|
|
|
va_start(va_ptr, format);
|
|
|
|
|
vsprintf((char*)term_out_buff, (char const *)format, va_ptr);
|
|
|
|
|
va_end(va_ptr);
|
|
|
|
|
|
|
|
|
|
uart_sendstr(g_term_uart_handle, (char*)term_out_buff);
|
|
|
|
|
}
|
2024-08-06 07:33:33 +00:00
|
|
|
|
|
2024-07-05 03:52:43 +00:00
|
|
|
|
#if 0
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ն˶<EFBFBD>ȡһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> .
|
|
|
|
|
* @param
|
|
|
|
|
* @param
|
|
|
|
|
* @param
|
|
|
|
|
* @retval <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
int term_uart_readln(u_int8_t *buff, int buff_size, u_int32_t timeout_ms)
|
|
|
|
|
{
|
|
|
|
|
int offset = 0;
|
|
|
|
|
char c = 0;
|
|
|
|
|
u_int32_t wait_timeout_flag = 0;
|
|
|
|
|
|
|
|
|
|
buff_size--;
|
|
|
|
|
|
|
|
|
|
if(timeout_ms){
|
|
|
|
|
set_flag_timeout(timeout_ms, &wait_timeout_flag);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(offset = 0; offset<buff_size; offset++){
|
|
|
|
|
while((!uart_dev_char_present(g_term_uart_handle)) && (!wait_timeout_flag));
|
|
|
|
|
|
|
|
|
|
if(wait_timeout_flag){
|
|
|
|
|
break;
|
|
|
|
|
}else{
|
|
|
|
|
c = uart_dev_in_char(g_term_uart_handle);
|
|
|
|
|
|
|
|
|
|
buff[offset] = c;
|
|
|
|
|
// if(g_term_echo_on)
|
|
|
|
|
// out_char(c); //<2F><><EFBFBD><EFBFBD>
|
|
|
|
|
if((c == '\n') || (c == '\r')){
|
|
|
|
|
offset++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(timeout_ms)
|
|
|
|
|
stop_timout();
|
|
|
|
|
|
|
|
|
|
buff[offset] = '\0';
|
|
|
|
|
return offset;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD>.
|
|
|
|
|
* @retval None
|
|
|
|
|
*/
|
|
|
|
|
void init_term_uart()
|
|
|
|
|
{
|
|
|
|
|
g_term_uart_handle = uart_dev_init(TERM_UART_INDEX, term_in_buff, sizeof(term_in_buff));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>RS485<EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
|
|
|
|
* @retval None
|
|
|
|
|
*/
|
|
|
|
|
void init_rs485_uart()
|
|
|
|
|
{
|
|
|
|
|
g_rs485_uart_handle = uart_dev_init(RS485_UART_INDEX, rs485_in_buff, sizeof(rs485_in_buff));
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-06 07:33:33 +00:00
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>4Gģ<EFBFBD>鴮<EFBFBD><EFBFBD>.
|
|
|
|
|
* @retval None
|
|
|
|
|
*/
|
|
|
|
|
void init_ec801_uart()
|
|
|
|
|
{
|
|
|
|
|
g_ec801_uart_handle = uart_dev_init(EC801_UART_INDEX, ec801_in_buff, sizeof(ec801_in_buff));
|
|
|
|
|
}
|
|
|
|
|
|