cancel
Showing results for 
Search instead for 
Did you mean: 

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

Skane
Associate III

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

}

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Skane
Associate III

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

View solution in original post

13 REPLIES 13

> 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

Andrew Neil
Evangelist III

@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

 

 


@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,

@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,


@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.



 

@Andrew Neilfirst of all, thank you for your help
i have tried many time to compare and check my project against a working project, i also removed all my code in main.c and copied the entire code from a working project that uses LL library and still didn't work.
it's always the same problem, when i use the system clock config, i get wrong char (it's the same char) and i'm sure the system clock config infects the baud rate i'm setting, maybe i'm missing something in the RCC config, or the systick config, i posted the whole project which is one main.c file.
this is how i created the project in keil.
+ enable CMSIS core
+ Device -> startup
+             -> STM32Cube Framework -> classic (i didn't use the cubemx)

+             -> STM32Cube LL -> Common, RCC, PWR, UART, GPIO, UTILS, TIM

since i don't want to use the HAL library i didn't enable any of it's components (i'm getting wormings about this, but the project compiles fine, no missing function).

I'm going to check the frequency now,

Skane
Associate III

Before i check the frequency, i enabled the HAL library, still not using any HAL function, i'm using the same code, and it's working. how is this possible?
is there weak function or any function that was redefined? is this something mandatory to do when to create a project, even if i don't want to use HAL, i need to enable it (enable common, cortex, pwr, rcc, uart, DMA) even dma must be enabled :\
don't anyone have an explanation for this?


@Skane wrote:

 i get wrong char (it's the same char) ,


And, again, the  most likely reason for that is wrong baud rate!

 


@Skane wrote:

 i'm sure the system clock config infects the baud rate,


Of course it does!

The baud rate is derived from the system clock - so, if the system clock rate is wrong, the baud rate is bound to be wrong.

Again, have you looked at what's happening on the wire with an oscilloscope?

@Andrew Neil 
I can't use MCO, both pin are already used for I2C (i'm using dev board stm32f429) i already tried to use my oscilator but i see nothing in a working project,  so i can't check the frequency, but as i said before, when i enabled the HAL library, the project works, and this is weird for me, do you have any idea what is the use of HAL library in a project based on LL library? (i have used the same code i posted, i just enabled the HAL cortex and common headers)
and about the baud rate, i was refering by correct config, to the 115200 setting, not to the actual baud rate, the actual baud rate is messed up due to a problem in the clock config which i don't know where. again, the clock config is so simple, HSE=8mhz, and every thing is running at 8mhz (i meal PLL_Div by 1), i wanted to make sure to use a very simple clock config.