diff --git a/component/libc/printf.c b/component/libc/printf.c index d662c98..d44cc2f 100644 --- a/component/libc/printf.c +++ b/component/libc/printf.c @@ -2,16 +2,11 @@ #include "stdarg.h" #include "string.h" -UART_HandleTypeDef *console = NULL; -extern UART_HandleTypeDef UartHandle; +extern UART_HandleTypeDef console; int putchar(int c) { - if (console == NULL) { - return 0; - } - - HAL_UART_Transmit(console, (uint8_t*)&c, 0, HAL_TIMEOUT_VALUE); + HAL_UART_Transmit(&console, (uint8_t*)&c, 0, HAL_TIMEOUT_VALUE); return c; } @@ -22,11 +17,7 @@ int puts(const char *c) len = strlen(c); - if (console == NULL) { - return 0; - } - - HAL_UART_Transmit(console, (uint8_t*)c, len, HAL_TIMEOUT_VALUE); + HAL_UART_Transmit(&console, (uint8_t*)c, len, HAL_TIMEOUT_VALUE); return len; } @@ -41,11 +32,7 @@ int putstring(const char *c) len = strlen(c); - if (console == NULL) { - return 0; - } - - HAL_UART_Transmit(console, (uint8_t*)c, len, HAL_TIMEOUT_VALUE); + HAL_UART_Transmit(&console, (uint8_t*)c, len, HAL_TIMEOUT_VALUE); return len; } @@ -63,7 +50,7 @@ int printf(const char *fmt, ...) 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; } @@ -81,8 +68,3 @@ int printf(const char *fmt, ...) return len; } #endif - -void uart_log_set_console(void) -{ - console = &UartHandle; -} diff --git a/component/libc/vsnprintf.c b/component/libc/vsnprintf.c index 23ed71c..37da9f2 100644 --- a/component/libc/vsnprintf.c +++ b/component/libc/vsnprintf.c @@ -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) { - while (!(__HAL_UART_GET_FLAG(console, UART_FLAG_TXFNF))); - console->Instance->TDR = (uint8_t)character; + while (!(__HAL_UART_GET_FLAG(&console, UART_FLAG_TXFNF))); + console.Instance->TDR = (uint8_t)character; } diff --git a/driver/board/uart_log.c b/driver/board/uart_log.c index f08c6cf..49fe608 100644 --- a/driver/board/uart_log.c +++ b/driver/board/uart_log.c @@ -1,8 +1,6 @@ #include "uart_log.h" -UART_HandleTypeDef UartHandle; - -extern void uart_log_set_console(void); +UART_HandleTypeDef console; static void UART_LOG_Msp_Init(void) { @@ -46,28 +44,26 @@ static void UART_LOG_Msp_Init(void) void uart_log_init(void) { - UartHandle.Instance = USART_LOG; - UartHandle.Init.BaudRate = UART_LOG_BAUDRATE; - UartHandle.Init.WordLength = UART_WORDLENGTH_8B; - UartHandle.Init.StopBits = UART_STOPBITS_1; - UartHandle.Init.Parity = UART_PARITY_NONE; - UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE; - UartHandle.Init.Mode = UART_MODE_TX_RX; - UartHandle.Init.ClockPrescaler = UART_PRESCALER_DIV1; - UartHandle.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; - UartHandle.Init.OverSampling = UART_OVERSAMPLING_16; + console.Instance = USART_LOG; + console.Init.BaudRate = UART_LOG_BAUDRATE; + console.Init.WordLength = UART_WORDLENGTH_8B; + console.Init.StopBits = UART_STOPBITS_1; + console.Init.Parity = UART_PARITY_NONE; + console.Init.HwFlowCtl = UART_HWCONTROL_NONE; + console.Init.Mode = UART_MODE_TX_RX; + console.Init.ClockPrescaler = UART_PRESCALER_DIV1; + console.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; + console.Init.OverSampling = UART_OVERSAMPLING_16; UART_LOG_Msp_Init(); - if(HAL_UART_Init(&UartHandle) != HAL_OK) - { + if(HAL_UART_Init(&console) != HAL_OK) { while (1) { } } /* 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 */ - HAL_UARTEx_EnableFifoMode(&UartHandle); - uart_log_set_console(); + HAL_UARTEx_EnableFifoMode(&console); }