stm32h750/driver/board/uart_log.c

70 lines
2.3 KiB
C

#include "uart_log.h"
UART_HandleTypeDef console;
static void UART_LOG_Msp_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;
/*##-1- Enable peripherals and GPIO Clocks #################################*/
/* Enable GPIO TX/RX clock */
USART_LOG_TX_GPIO_CLK_ENABLE();
USART_LOG_RX_GPIO_CLK_ENABLE();
/* Select HSI as source of USARTx clocks */
RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART_LOG;
RCC_PeriphClkInit.Usart16ClockSelection = RCC_USART_LOG_CLKSOURCE_HSI;
HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit);
/* Enable USARTx clock */
USART_LOG_CLK_ENABLE();
/*##-2- Configure peripheral GPIO ##########################################*/
/* UART TX GPIO pin configuration */
GPIO_InitStruct.Pin = USART_LOG_TX_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = USART_LOG_TX_AF;
HAL_GPIO_Init(USART_LOG_TX_GPIO_PORT, &GPIO_InitStruct);
/* UART RX GPIO pin configuration */
GPIO_InitStruct.Pin = USART_LOG_RX_PIN;
GPIO_InitStruct.Alternate = USART_LOG_RX_AF;
HAL_GPIO_Init(USART_LOG_RX_GPIO_PORT, &GPIO_InitStruct);
/* NVIC for USART */
HAL_NVIC_SetPriority(USART_LOG_IRQn, 0, 1);
HAL_NVIC_EnableIRQ(USART_LOG_IRQn);
}
void uart_log_init(void)
{
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(&console) != HAL_OK) {
while (1) {
}
}
/* Set the RXFIFO threshold */
HAL_UARTEx_SetRxFifoThreshold(&console, UART_RXFIFO_THRESHOLD_1_2);
/* Enable the FIFO mode */
HAL_UARTEx_EnableFifoMode(&console);
}