cancel
Showing results for 
Search instead for 
Did you mean: 

UARTs Not Working On HSE Clock

JThom.15
Associate III

Hi ,

I am using an STM32F205 for my project. We were using HSI as the clock source before and found out that HSI will not work very well with high temperatures for UARTs, So we added an External crystal clock (16Mhz) but I am unable to get Comms out of UARTs now (There are some random values coming through uart sometimes). This is my clock config

RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  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 = 14;
  RCC_OscInitStruct.PLL.PLLN = 240;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  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_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  {
    Error_Handler();
  }

   /** Enables the Clock Security System
  */
  HAL_RCC_EnableCSS();

  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

I also updated the HSE value in stm32f2xx_hal_conf.h to 16Mhz.

I am using platformIO as the IDE for this project.
Am I missing any configuration?

1 ACCEPTED SOLUTION

Accepted Solutions

Finally I found the issue,

We Copied stm32f2xx_hal_conf.h from PlatformIO Library in to our project. And changed the HSE_VALUE in the copied file. But the code was still using the stm32f2xx_hal_conf.h from the PlatformIO library which has got HSE_VALUE 25Mhz.

Thanks all for the Help

 

View solution in original post

23 REPLIES 23
tjaekel
Lead

Is it a real XTAL or a CMOS OSC?
It differs on config, e.g. a CMOS OSC (just one signal on HSE pins used) needs a setting like "BYPASS".

When you use an external HSE OSC - you had to make sure the PLL config is right. The PLL setting wants to see the clocks in a certain frequency range:
Example: a 16 MHz external OSC (or XTAL) divided by 14 (PLLM) = 1.1142 MHz (maybe too low).
And then you multiply again with 240 (PLLN) = 274.285 MHz (maybe too fast?)

And all these resulting values look a "bit strange" (not a real integer clock ratio). Even the PLL would work, you might use a "strange" clock frequency for UART.
My suggestion (what I do pretty often): use the STRM32CubeMX tool and play with the clock config: you get a pretty good clue how to configure all the PLLs. (no need to generate code, just play with PLL settings and use the values to update in your code).

 

Thanks for the reply.

I am using a real XTAL. I updated the clock config according to this 

JThom15_0-1708663601289.png

I updated my PLLM to 13 and PLLN to 195 as per the above config, but still I am not getting proper comms out of the UARTS

OK, this looks good.

But you have to check also which CLK is used for the UART peripheral, and how to you set a baud rate divider for the UART. Your UART can be completely off on clock frequency (and therefore a wrong baud rate).

Add the UART as peripheral to this CubeMX, see the UART core clock and check the UART baud rate divider.

And make sure, the resulting baud rate is pretty correct (not so much off): you cannot set "any" baud rate: just UART clock plus divider: any value in between (or your UART clock is "wrong") results in a wrong baud rate or a baud rate which is a bit off (with an "error" offset, resulting in wrong UART bits).

Measure with a scope the UART bit frequency. Or go through the entire clock tree, including the UART clk selected, the divider set in order to see if it can result in a "reasonable" baud rate.
If you change the PLLs - you have to change potentially also the CLK config for the UART (at least the baud rate divider).

If I change the clock source to HSI with the above clock config, the UARTs works fine. 

Read out and check/post/compare to working, content of UART and relevant RCC registers.

JW

Hello @JThom.15 

A problem with the fundamental frequency of your oscillator?

Perhaps you should activate the HSE output on the MCO1 pin and you will be able to measure your real HSE frequency if = 16MHz ?
And follow @waclawek.jan recommendation from RCC and UART register configuration.

Best regards,

Romain,

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Check HSE_VALUE 

Scope 0x55 pattern for bit timing

Scope MCO for HSE timing

 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
LCE
Principal

Also check the quartz' datasheet for recommended load capacitance and check your capacitors.
You're not the first having trouble with that.

BTW, is the STM32 HSE circuit that sensitive, or was I just lucky over the last 20 years with my AVRs and PICs?

 

PS:

And why these odd dividers and multipliers?

That's for sure not the problem, but in my experience PLLs work better (stability, accuracy, jitter) with even values.

So there should be some smoother settings to get from 16 MHz to 120 MHz. 😉

JThom.15
Associate III

Hi,

I have measured the RCC_OSC_IN which is 16 MHz, 

JThom15_0-1708903221043.png

1.6055 MHz x 10  = 16MHz.

Currently in our board PA8 and PC9 are used for i2c comms, So I cant enable MCO1 and MCO2 for measuring the clock output.