Wrong Data in STM32F4 + UART using LL (low layer) library
Hi,
I'm trying to run STM32F429 using LL library to send data periodically.
as a start, i'm sending 1 char every 1 sec, but i'm receive a wrong char (i send 'A', i receive something not clear), and when i call my custom systemClockConfig function, the UART_transmit function doesn't work.
i'm using keil as IDE, i have tryed stmcubeide and with the generated project, it works fine, but even when i copy and past the same exact function (UART config, system clock config, and also the main config) in keil it doesn't work.
#include "stm32f4xx_ll_rcc.h"
#include "stm32f4xx_ll_bus.h"
#include "stm32f4xx_ll_cortex.h"
#include "stm32f4xx_ll_utils.h"
#include "stm32f4xx_ll_pwr.h"
#include "stm32f4xx_ll_gpio.h"
#include "stm32f4xx_ll_spi.h"
#include "stm32f4xx_ll_usart.h"
#include "stm32f4xx_ll_system.h"
void vSystemClockConfig(void);
static void MX_USART1_UART_Init(void);
int main(void){
vSystemClockConfig();
MX_USART1_UART_Init();
while(1){
while(!LL_USART_IsActiveFlag_TXE(USART1));
LL_USART_TransmitData8(USART1, '1');
while (!LL_USART_IsActiveFlag_TC(USART1));
LL_mDelay(1000);
}
return 0;
}
static void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
LL_USART_InitTypeDef USART_InitStruct = {0};
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
/* Peripheral clock enable */
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
GPIO_InitStruct.Pin = LL_GPIO_PIN_10|LL_GPIO_PIN_9;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
GPIO_InitStruct.Alternate = LL_GPIO_AF_7;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
USART_InitStruct.BaudRate = 115200;
USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
USART_InitStruct.Parity = LL_USART_PARITY_NONE;
USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
LL_USART_Init(USART1, &USART_InitStruct);
LL_USART_ConfigAsyncMode(USART1);
LL_USART_Enable(USART1);
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
void vSystemClockConfig(void){
LL_FLASH_SetLatency(LL_FLASH_LATENCY_2);
// make sure latency is what we set previously
while(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_2);
LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE3);
LL_PWR_DisableOverDriveMode();
LL_RCC_HSE_Enable();
while(!LL_RCC_HSE_IsReady()); // wait until HSE it enabled and ready
LL_RCC_PLL_Disable(); // disable main PLL
//while(!LL_PWR_IsActiveFlag_VOS()); // make sure voltage scaling (VOS) is in low power mode
LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_HSE); // use HSE for system clock
// Wait till System clock is ready
while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_HSE);
LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1); // HSE is 8Mhz, div by 1 to keep sysclock at 8mhz also
LL_RCC_SetAPB1Prescaler(LL_RCC_SYSCLK_DIV_1); // also keep it at 8mhz
LL_RCC_SetAPB2Prescaler(LL_RCC_SYSCLK_DIV_1); // also keep it at 8mhz
LL_SetSystemCoreClock(8000000);
LL_Init1msTick(8000000); // we have 8MHz Hclk
// Enable SysTick interrupt
NVIC_SetPriority(SysTick_IRQn, 0); // Set SysTick priority
NVIC_EnableIRQ(SysTick_IRQn); // Enable SysTick interrupt
LL_RCC_SetTIMPrescaler(LL_RCC_TIM_PRESCALER_TWICE);
}
