cancel
Showing results for 
Search instead for 
Did you mean: 

Corrupted terminal output using USART2 on STM32F4 Discovery

Sherif Armanyous
Associate II
Posted on January 16, 2018 at 13:03

Hi ,

I am generating usart2 in STM43F407 Discovery using stmcubemx and keil where it does not work and the configuration in stmcube is as follows : > rcc - crystal oscillator - HSEPLL Source > USART2 - asynchronous -9600baud- 8bit data

Data should be 'Hello World\n' it appears on the terminal corrupted data

/* Includes ------------------------------------------------------------------*/

#include 'main.h'

#include 'stm32f4xx_hal.h'

#include <string.h>

UART_HandleTypeDef huart2;

char test[]='Hello World\n';

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_USART2_UART_Init(void);

int main(void)

{

HAL_Init();

SystemClock_Config();

MX_GPIO_Init();

MX_USART2_UART_Init();

while (1)

{

HAL_UART_Transmit(&huart2,(uint8_t *)test,strlen(test),100);

HAL_Delay(1000);

}

}

void SystemClock_Config(void)

{

RCC_OscInitTypeDef RCC_OscInitStruct;

RCC_ClkInitTypeDef RCC_ClkInitStruct;

/**Configure the main internal regulator output voltage

*/

__HAL_RCC_PWR_CLK_ENABLE();

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

/**Initializes the CPU, AHB and APB busses clocks

*/

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;

RCC_OscInitStruct.HSEState = RCC_HSE_ON;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

RCC_OscInitStruct.PLL.PLLM = 25;

RCC_OscInitStruct.PLL.PLLN = 168;

RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

RCC_OscInitStruct.PLL.PLLQ = 4;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

/**Initializes the CPU, AHB and APB busses clocks

*/

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

/**Configure the Systick interrupt time

*/

HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

/**Configure the Systick

*/

HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

/* SysTick_IRQn interrupt configuration */

HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

}

/* USART2 init function */

static void MX_USART2_UART_Init(void)

{

huart2.Instance = USART2;

huart2.Init.BaudRate = 9600;

huart2.Init.WordLength = UART_WORDLENGTH_8B;

huart2.Init.StopBits = UART_STOPBITS_1;

huart2.Init.Parity = UART_PARITY_NONE;

huart2.Init.Mode = UART_MODE_TX_RX;

huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;

huart2.Init.OverSampling = UART_OVERSAMPLING_16;

if (HAL_UART_Init(&huart2) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

}

/** Configure pins as

* Analog

* Input

* Output

* EVENT_OUT

* EXTI

*/

static void MX_GPIO_Init(void)

{

/* GPIO Ports Clock Enable */

__HAL_RCC_GPIOH_CLK_ENABLE();

__HAL_RCC_GPIOA_CLK_ENABLE();

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**

* @brief This function is executed in case of error occurrence.

* @param None

* @retval None

*/

void _Error_Handler(char * file, int line)

{

/* USER CODE BEGIN Error_Handler_Debug */

/* User can add his own implementation to report the HAL error return state */

while(1)

{

}

/* USER CODE END Error_Handler_Debug */

}

#ifdef USE_FULL_ASSERT

/**

* @brief Reports the name of the source file and the source line number

* where the assert_param error has occurred.

* @param file: pointer to the source file name

* @param line: assert_param error line source number

* @retval None

*/

void assert_failed(uint8_t* file, uint32_t line)

{

/* USER CODE BEGIN 6 */

/* User can add his own implementation to report the file name and line number,

ex: printf('Wrong parameters value: file %s on line %d\r\n', file, line) */

/* USER CODE END 6 */

}

#endif

/**

* @}

*/

/**

* @}

*/

Note: this post was migrated and contained many threaded conversations, some content may be missing.
23 REPLIES 23
Andrew Neil
Chief II
Posted on January 16, 2018 at 13:04

please don't put the whole question in the title!

Posted on January 16, 2018 at 13:18

This works fine for me on STM32F103C8 / UART1. I would check the other side (PC, serial setup 8N1?).

Andrew Neil
Chief II
Posted on January 16, 2018 at 13:38

Sherif Armanyous wrote:

it appears on the terminal corrupted data

That almost invariably means that you have the baud rate wrong:

https://cdn.sparkfun.com/r/700-700/assets/c/e/2/d/a/50d247c5ce395fdc6b000000.png

See:

https://learn.sparkfun.com/tutorials/serial-communication

- about 2/3 down the page

Sherif Armanyous
Associate II
Posted on January 16, 2018 at 13:45

> I configured Baud rate as 9600 bps at STMCubeMX and code is generated at keil MDK V5. 

> Clock is choosen HSE= 25MHZ,  HSE PLL Source SYSCLK 84 MHZ,  HCLK = 84 MHZ and PClk1 which supplies APB1Bus = 42MHZ

Posted on January 16, 2018 at 13:37

Ok,  Sorry for that 

Posted on January 16, 2018 at 13:57

So what baud rate did you set your terminal to?

If the terminal is  correctly set to the baud rate you think you've configured,  you're just going to have to get your scope out, and take a look to see what baud rate you are actually getting.

Posted on January 16, 2018 at 13:58

The STM32 board is connected to PC, isn't it? The PC needs to have 9600, 8N1 settings to match the STM32 side.

Posted on January 16, 2018 at 14:16

And how is it connected?

Posted on January 16, 2018 at 14:18

9600