cancel
Showing results for 
Search instead for 
Did you mean: 

USART prints junk after initializing the HSE clock (HAL_RCC_ClockConfig)

Kaveh
Associate III

Hello,

I am getting junk printed on the USART after initializing HSE clock. I had USART output at every single line and figured out that up to "HAL_RCC_ClockConfig" everything is ok and junk is printed right after executing this line. Also made sure that code reaches end of program, so it doesn't get stuck from any of the Error_Handler situations (HAL_OK).

USART output:

[2020-10-29_20:28:41:842]SYSCLK : 64000000Hz

[2020-10-29_20:28:41:842]HCLK  : 64000000Hz

[2020-10-29_20:28:41:842]PCLK1 : 64000000Hz

[2020-10-29_20:28:41:842]PCLK2 : 64000000Hz

[2020-10-29_20:28:41:875]   à à üü àüà àü ààà    àà à à àüà  àü ààà    àà à  àüà àü ààà    àà à  àüàà àü ààà    àà à 

code:

  memset(msg,0,sizeof(msg));

  sprintf(msg,"PCLK2 : %ldHz\r\n",HAL_RCC_GetPCLK2Freq());

  HAL_UART_Transmit(&huart2,(uint8_t*)msg,strlen(msg),HAL_MAX_DELAY);

RCC_OscInitTypeDef osc_init;

RCC_ClkInitTypeDef clk_init;

RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;

memset(&osc_init,0,sizeof(osc_init));

osc_init.OscillatorType = RCC_OSCILLATORTYPE_HSE;

  osc_init.HSEState = RCC_HSE_ON;

  osc_init.PLL.PLLState = RCC_PLL_NONE;

  while ( HAL_RCC_OscConfig(&osc_init) != HAL_OK)

  {

   Error_Handler();

  }

  clk_init.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | \

   RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 | \

RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;

  clk_init.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;

  clk_init.SYSCLKDivider = RCC_SYSCLK_DIV1;

  clk_init.AHBCLKDivider = RCC_HCLK_DIV1;

  clk_init.APB3CLKDivider = RCC_APB3_DIV1;

  clk_init.APB1CLKDivider = RCC_APB1_DIV1;

  clk_init.APB2CLKDivider = RCC_APB2_DIV1;

  clk_init.APB4CLKDivider = RCC_APB4_DIV1;

  if( HAL_RCC_ClockConfig(&clk_init, FLASH_LATENCY_0) != HAL_OK)

  {

   Error_Handler();

  }

  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART2;

  PeriphClkInitStruct.Usart234578ClockSelection = RCC_USART234578CLKSOURCE_D2PCLK1;

  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)

  {

   Error_Handler();

  }

 /*---------------------------- AFTER THIS LINE SYSCLK is SOURCED BY HSE------------------*/

  //__HAL_RCC_HSI_DISABLE(); //Saves some current

  /* LETS REDO THE SYSTICK CONFIGURATION */

   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  MX_USART2_UART_Init();

user_data = " AFTER.\r\n";

  if (HAL_UART_Transmit(&huart2,(uint8_t*)user_data,strlen(user_data), HAL_MAX_DELAY) != HAL_OK)

  {

  // Error occurred

  Error_Handler();

  }

5 REPLIES 5
TDK
Guru

Check that HSE_VALUE is set to whatever your crystal speed is. Typically 8000000 or 25000000.

If you feel a post has answered your question, please click "Accept as Solution".
Kaveh
Associate III

Thank you for the quick response TDK. The HSE_VALUE was indeed the wrong frequency, but I am still facing the issue after changing it to correct frequency of 8000000.

TDK
Guru

Definitely looks like a timing issue. Check USARTx->BRR to see if the baud rate gets set correctly.

I don't think calling MX_USART2_UART_Init twice causes issues, but maybe it's expecting the peripheral to be reset prior to this call.

If you feel a post has answered your question, please click "Accept as Solution".
Kaveh
Associate III

Sorry I am very new to HAL programming. Where should I look for USARTx->BRR and what should be the correct value? I used the CubMX to generate the code and USART2 is used.

Also, I removed the first "MX_USART2_UART_Init " and all the USART calls to it. Still junk is printed after initializing the clock.

[2020-10-31_20:00:27:617]   à à üü àüà àü ààà    àà à à àüà  àü ààà    àà à  àüà àü ààà    àà à  àüàà àü ààà    àà à 

gregstm
Senior III

Send the single character "U" repeatedly to the UART and observe with an oscilloscope (the character "U" will give a square wave signal). Observe the difference after the call to HAL_RCC_ClockConfig.