[fix] update uart_log console

This commit is contained in:
zhji 2024-11-20 22:19:36 +08:00
parent adef945b30
commit 3de75b2326
3 changed files with 22 additions and 44 deletions

View File

@ -2,16 +2,11 @@
#include "stdarg.h" #include "stdarg.h"
#include "string.h" #include "string.h"
UART_HandleTypeDef *console = NULL; extern UART_HandleTypeDef console;
extern UART_HandleTypeDef UartHandle;
int putchar(int c) int putchar(int c)
{ {
if (console == NULL) { HAL_UART_Transmit(&console, (uint8_t*)&c, 0, HAL_TIMEOUT_VALUE);
return 0;
}
HAL_UART_Transmit(console, (uint8_t*)&c, 0, HAL_TIMEOUT_VALUE);
return c; return c;
} }
@ -22,11 +17,7 @@ int puts(const char *c)
len = strlen(c); len = strlen(c);
if (console == NULL) { HAL_UART_Transmit(&console, (uint8_t*)c, len, HAL_TIMEOUT_VALUE);
return 0;
}
HAL_UART_Transmit(console, (uint8_t*)c, len, HAL_TIMEOUT_VALUE);
return len; return len;
} }
@ -41,11 +32,7 @@ int putstring(const char *c)
len = strlen(c); len = strlen(c);
if (console == NULL) { HAL_UART_Transmit(&console, (uint8_t*)c, len, HAL_TIMEOUT_VALUE);
return 0;
}
HAL_UART_Transmit(console, (uint8_t*)c, len, HAL_TIMEOUT_VALUE);
return len; return len;
} }
@ -63,7 +50,7 @@ int printf(const char *fmt, ...)
len = (len > sizeof(print_buf)) ? sizeof(print_buf) : len; len = (len > sizeof(print_buf)) ? sizeof(print_buf) : len;
HAL_UART_Transmit(console, (uint8_t*)print_buf, len, HAL_TIMEOUT_VALUE); HAL_UART_Transmit(&console, (uint8_t*)print_buf, len, HAL_TIMEOUT_VALUE);
return len; return len;
} }
@ -81,8 +68,3 @@ int printf(const char *fmt, ...)
return len; return len;
} }
#endif #endif
void uart_log_set_console(void)
{
console = &UartHandle;
}

View File

@ -219,11 +219,11 @@ static inline void out_buffer(char character, void* buffer, size_t idx, size_t m
} }
} }
extern UART_HandleTypeDef *console; extern UART_HandleTypeDef console;
static inline void out_console(char character, void* buffer, size_t idx, size_t maxlen) static inline void out_console(char character, void* buffer, size_t idx, size_t maxlen)
{ {
while (!(__HAL_UART_GET_FLAG(console, UART_FLAG_TXFNF))); while (!(__HAL_UART_GET_FLAG(&console, UART_FLAG_TXFNF)));
console->Instance->TDR = (uint8_t)character; console.Instance->TDR = (uint8_t)character;
} }

View File

@ -1,8 +1,6 @@
#include "uart_log.h" #include "uart_log.h"
UART_HandleTypeDef UartHandle; UART_HandleTypeDef console;
extern void uart_log_set_console(void);
static void UART_LOG_Msp_Init(void) static void UART_LOG_Msp_Init(void)
{ {
@ -46,28 +44,26 @@ static void UART_LOG_Msp_Init(void)
void uart_log_init(void) void uart_log_init(void)
{ {
UartHandle.Instance = USART_LOG; console.Instance = USART_LOG;
UartHandle.Init.BaudRate = UART_LOG_BAUDRATE; console.Init.BaudRate = UART_LOG_BAUDRATE;
UartHandle.Init.WordLength = UART_WORDLENGTH_8B; console.Init.WordLength = UART_WORDLENGTH_8B;
UartHandle.Init.StopBits = UART_STOPBITS_1; console.Init.StopBits = UART_STOPBITS_1;
UartHandle.Init.Parity = UART_PARITY_NONE; console.Init.Parity = UART_PARITY_NONE;
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE; console.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UartHandle.Init.Mode = UART_MODE_TX_RX; console.Init.Mode = UART_MODE_TX_RX;
UartHandle.Init.ClockPrescaler = UART_PRESCALER_DIV1; console.Init.ClockPrescaler = UART_PRESCALER_DIV1;
UartHandle.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; console.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
UartHandle.Init.OverSampling = UART_OVERSAMPLING_16; console.Init.OverSampling = UART_OVERSAMPLING_16;
UART_LOG_Msp_Init(); UART_LOG_Msp_Init();
if(HAL_UART_Init(&UartHandle) != HAL_OK) if(HAL_UART_Init(&console) != HAL_OK) {
{
while (1) { while (1) {
} }
} }
/* Set the RXFIFO threshold */ /* Set the RXFIFO threshold */
HAL_UARTEx_SetRxFifoThreshold(&UartHandle, UART_RXFIFO_THRESHOLD_1_2); HAL_UARTEx_SetRxFifoThreshold(&console, UART_RXFIFO_THRESHOLD_1_2);
/* Enable the FIFO mode */ /* Enable the FIFO mode */
HAL_UARTEx_EnableFifoMode(&UartHandle); HAL_UARTEx_EnableFifoMode(&console);
uart_log_set_console();
} }