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

}

 

 

13 REPLIES 13

@Skane wrote:

 i already tried to use my oscilator but i see nothing in a working project,  .


Pardon? Did you mean "oscilloscope" there?

 


@Skane wrote:

 when i enabled the HAL library


Not sure what you mean by that?

The HAL is a higher level library, so hides more of the detail from you.

With LL, you need a far better understanding of the workings of the hardware - you have to take care of all the details by yourself.

@Andrew Neil, my mistake, i mean oscilloscope
I know HAL is higher than LL, and that's why i can't understand what HAL provides to the project to make it work, i didn't even call any HAL header, i just enabled it in the project (which mean it's visible for the compiler),
my question is, from the system config function i have provided, is there something missing?
beside that configuration, do i need to config something else, i see systick handler in many HAL project, i don't see how it's related, systick can be related to delays for example, but not to impact the uart clock,
i can't wrap my head toward something clear, i wasn't able to find any tutorial about STM32+LL library without using cubemx or cubeide, all of them are a pre-generated projects and they are working fine,


@Skane wrote:

 my mistake, i mean oscilloscope


OK, so what does this mean:

 


@Skane wrote:

 i already tried to use my oscilloscope but i see nothing in a working project,.


You need to look at your UART TX pin in your "non-working" project, and see what is actually being transmitted.

 

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