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
Posted on January 16, 2018 at 14:24

>baud =9600, data bits=8 , stop bits =1.

> STM32F407VG connected to UART TTL to USB CONVERTER and I tested this TTL TO USB converter with different modules while it is working fine.

Posted on January 16, 2018 at 14:31

When you say, 'TTL' - it is for 3V, isn't it?

And you do have a good ground connection, don't you?

Posted on January 16, 2018 at 14:41

> TTL connected for 3.3 v not 5v and powered from pc usb

> STM32F4 Discovery also powered from pc usb

connection from it is tested using GSM Sim808 and the same terminal sending AT Commands and it worked successfuly 

Posted on January 16, 2018 at 14:45

Do you have any logic analyser to sniff the UART? This way we can eliminate wiring errors (right pins), etc

Posted on January 16, 2018 at 14:51

No

Posted on January 16, 2018 at 14:56

Before I bought the logic analyser I usually sampled the output through the ADC (poor man scope) to see the timing.

Posted on January 16, 2018 at 17:09

I generated the project for STM32F401-Disco (do not have F407) using HSI/PLL and UART2/115200 worked fine.

Posted on January 16, 2018 at 16:47

The STM32F407-DISCO has an 8 MHz HSE clock input, this must be selected via the PLL and also the HSE_VALUE define

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 16, 2018 at 18:04

I tested it and worked using your advice so , I need to thank you for your great help .

Turvey.Clive.002

Posted on January 16, 2018 at 18:06

Thanks Mr

Golab.Bogdan

I solved it , where it was my mistake as I set HSE (High speed external clock) as either 25 or 16 MHZ as it should be 8MHZ , So your advices regarding uart helped me and gave me great ideas and for sure I got great knowledge from our conversation.