Skip to main content
Visitor II
May 20, 2024
Solved

Wrong Data in STM32F4 + UART using LL (low layer) library

  • May 20, 2024
  • 3 replies
  • 3975 views

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);

}

 

 

    This topic has been closed for replies.
    Best answer by Skane

    Hi,
    since last time, after it worked randomly (Add HAL library without using it and removing system clock config), i had to move forward and work on something else, now i back again to this problem since it didn't make sense, i looked on the HW of the dev board and i think the configuration isn't on default, i'm using STM32F429 dev board, SB20 and SB19 should be close, R56 soldered and SB18 should be open to use external crytal X3, this is wasn't the case in my my board, so it wasn't using the HSE, i changed the config to HSI with 16MHz and removed HAL library and it's working.
    Thanks guys for help
    best regards

    3 replies

    Super User
    May 20, 2024

    > i'm sending 1 char every 1 sec, but i'm receive a wrong char (i send 'A', i receive something not clear)

    What is your receiver? Are levels OK (i.e. is there any leevl conversion e.g. to RS232 involved)? Are grounds connected properly?

    If this is not the cause, it's most likely incorrect baudrate. Start with default 16HMz HSI, without trying to set clocks in RCC, just set up the UART. Read out and check content of UART registers (mainly baudrate regsiter).

    JW

    SkaneAuthor
    Visitor II
    May 20, 2024


    @waclawek.jan  hi,
    The hardware side is fine, because when i use another project, the output of the UART is correct, for example, wheni i use HAL library, or even with LL library and i configure the clock using cubeMX and run in using cubeIDE, everything seems fine, the only problem i have is, when i use keil + LL library and i configure the clock, because when i remove the clock configuration, it's also works fine, so it's only happen when i call my systemClockConfig function, i already posted the function, so if i'm missing something, please tell me, i think my problem is not the UART config, but it's the clock configuration,

    Super User
    May 20, 2024

    @Skane wrote:

    i send 'A', i receive something not clear


    As @waclawek.jan suggested, the commonest cause of "junk characters received" in serial comms is incorrect baud rate:

    https://learn.sparkfun.com/tutorials/serial-communication/all#:~:text=If%20all%20the%20receiving%20device%20sees%20on%20its%20receive%20line%20is%20garbage%2C%20check%20to%20make%20sure%20the%20baud%20rates%20match%20up.

    Do you have an oscilloscope to check what is actually being sent on the wires?  (sending 'U' would give you an easier waveform to look at)

     

    Please use this button to properly post source code:

    AndrewNeil_0-1716200724840.png

     

     

    SkaneAuthor
    Visitor II
    May 20, 2024

    @Andrew Neilhi,
    the baud rite config is correct, but i think the problem in my system clock config, because when i use the default one (i don't config the clock) i get the correct char. but i don't know what is wrong with the clock config function, i just copied from a working project that also uses UART,

    Super User
    May 20, 2024

    @Skane wrote:

    the baud rite config is correct, ,


    How do you know that? 

    The fact that you're receiving "wrong" characters very much suggests that your baud rate is not correct.

    Again, have you used a scope or logic analyser to verify what's happening on the wire?

     


    @Skane wrote:

     i think the problem in my system clock config, 


    of course, if your clock is not running at the speed you think it is, then your baud rate calculations will be wrong!

    Route the clock to MCO to check what frequency it is.

    Check your register settings against a known-good project.



     

    SkaneAuthorAnswer
    Visitor II
    May 26, 2024

    Hi,
    since last time, after it worked randomly (Add HAL library without using it and removing system clock config), i had to move forward and work on something else, now i back again to this problem since it didn't make sense, i looked on the HW of the dev board and i think the configuration isn't on default, i'm using STM32F429 dev board, SB20 and SB19 should be close, R56 soldered and SB18 should be open to use external crytal X3, this is wasn't the case in my my board, so it wasn't using the HSE, i changed the config to HSI with 16MHz and removed HAL library and it's working.
    Thanks guys for help
    best regards