zephyrproject-rtos / zephyr

Primary Git Repository for the Zephyr Project. Zephyr is a new generation, scalable, optimized, secure RTOS for multiple hardware architectures.
https://docs.zephyrproject.org
Apache License 2.0
10.8k stars 6.58k forks source link

STM32F030 clock control: software timers depend on special clock settings #55150

Closed danielSchaeble closed 1 year ago

danielSchaeble commented 1 year ago

A simple project with just one software timer and LED feedback was used. The used MCU is the STM32F030C8T6R. The timers depend to the rcc and/or pll frequencies, which actually should independent. The following configuration lead to working software timers.

image

By changing the frequency of the rcc or using the pll with a separate and valid configuration the timers do take more or less time than expected. The clock settings were checked with the clock tree of CubeMX.

Tested on: Ubuntu 20.04.5 LTS Zephyr 3.2

Related to discord post: https://discord.com/channels/720317445772017664/969266602925252638/1075368071713259531

erwango commented 1 year ago

@danielSchaeble An example of configuration leading to PWM issue ?

danielSchaeble commented 1 year ago

The pwm does also work with the clock settings above, but it is also mandatory to specifiy the st, prescaler. If left out the pwm does not work at all. The pwm is used for the RGB LEDS.

image

image

The actual problem is also the dependency to the rcc/pll configuration and the mandatory st,prescaler. (The st,prescaler was not mandatory with stm32wl MCU.)

erwango commented 1 year ago

it is also mandatory to specifiy the st, prescaler. If left out the pwm does not work at all. [...] The st,prescaler was not mandatory with stm32wl MCU.

When not specified, st,prescaler is set to default value (0). Depending on RCC configuration (which might differ on your WL target), st,prescaler might require tuning, this is expected.

The timers depend to the rcc and/or pll frequencies, which actually should independent.

Timer clock freq depend on APB domain frequency (equal if APB =1, 2xAPB Freq otherwise). This is handled internally in PWM driver. So to me this is normal, unless there is a valid configuration which isn't working

danielSchaeble commented 1 year ago

When not specified, st,prescaler is set to default value (0). Depending on RCC configuration (which might differ on your WL target), st,prescaler might require tuning, this is expected.

What does it mean to set the prescaler to 0? Isn't it a multiplicator/divider and setting it to 0 would set the frequency to 0? All clock prescalers, also in the WL target project, were set to default project settings of a CubeMX project, so it should be valid.

Timer clock freq depend on APB domain frequency (equal if APB =1, 2xAPB Freq otherwise). This is handled internally in PWM driver. So to me this is normal, unless there is a valid configuration which isn't working.

So does this mean it is normal that there is only one valid rcc/pll setting to make all zephyr timers and pwm work? Of course only considering valid clock settings in terms of it is valid for the target in general.

erwango commented 1 year ago

What does it mean to set the prescaler to 0? Isn't it a multiplicator/divider and setting it to 0 would set the frequency to 0? All clock prescalers, also in the WL target project, were set to default project settings of a CubeMX project, so it should be valid.

See bindings doc dts/bindings/timer/st,stm32-timers.yaml:

  st,prescaler:
    type: int
    required: true
    description: |
      Clock prescaler at the input of the timer
      Could be in range [0 .. 0xFFFF] for STM32 General Purpose Timers (CLK/(prescaler+1) )

it is normal that there is only one valid rcc/pll setting to make all zephyr timers and pwm work?

You should be able to get PWM working with every valid RCC/PLL configuration ... BUT it might require to tune st-prescaler

danielSchaeble commented 1 year ago

Okay then this seems to be normal. Does that also apply to zephyr timers? Is it correct that a timer can take less time than specified, when a prescaler is not set with a valid clock configuration?

erwango commented 1 year ago

Okay then this seems to be normal. Does that also apply to zephyr timers? Is it correct that a timer can take less time than specified, when a prescaler is not set with a valid clock configuration?

Yes, it will be the same if you're using counter_ll_stm32_timer (same IP, similar driver).

danielSchaeble commented 1 year ago

Okay thanks, then this is not a bug.