I am using STM32CUBEMX
to generate codes. In the example (Examples\UART\UART_Printf) of STM32WB55
, I did not find __HAL_RCC_USART1_CLK_ENABLE()
. I am very confused why it is not necessary to enable USART1 clock, but we need to enable GPIOB clock?
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_ODD;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOB_CLK_ENABLE();
}
Both calls are necessary.
__HAL_RCC_USART1_CLK_ENABLE
is invoked from HAL_UART_MspInit
(see stm32wbxx_hal_msp.c), which in turn is invoked from HAL_UART_Init
This is the way, how library and Cube samples are organized. Higher level code fills main descriptor (huart1
in your case) with pointer to the periphery instance, base initialization parameters and calls the Init
function with this descriptor. The Init
function performs register initialization, but first calls the MspInit
function, that must be implemented in the application code. The MspInit
function performs additional initialization like enabling clocks, GPIO configuration, etc. All what doesn't fit a library code
MspInit
is also implemented as empty function with weak
attribute in the library by default, which means any other implementation will override the default one.