cancel
Showing results for 
Search instead for 
Did you mean: 

Can FreeRTOS software timer generate PWM?

HMSEdinburge
Associate II

Hello, i am using F446 and i am contemplating using FreeRTOS CMSIS V2 software timer to generate PWM to control motors via motor drivers (IC  chipset). Is this possible? Or must I use the hardware timer, TIM1~7 ?

19 REPLIES 19
Andrew Neil
Super User

@HMSEdinburge wrote:

must I use the hardware timer, TIM1~7 ?


Why would you not want to use them? This is exactly what they're for !

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

Ok, but can this FreeRTOS software timer produce PWM like the hardware timer? Can they driver a motor? 

Please share source code of software timer generating PWM, thx. 

Hardware timers all use up, therefore I got use a software timer. 

LCE
Principal II

Then check if the hardware timers are really needed for the stuff they are doing now.

If your PWM is really slow (like <= 1 kHz carrier or so), then maybe software can handle this well enough.

Otherwise - not at least a 10 kHz interrupt available or so?

But all this doesn't really make sense in a device that has hardware / timers built for that purpose.

 

Of course it can be done - just like bit-banging any other interface.

But you will never reach anywhere near the performance of a dedicated hardware timer.

Your code will need to manually make every single output transition and timer adjustment - and fit this around whatever else your code is doing.

 

But it's just software; you can easily try it out - but pay particular attention to the CPU usage ...

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

all my hardware timer TIM1~7 all used, therefore only left software timer, but can software timer able to drive an external motor? 

 


@HMSEdinburge wrote:

Hardware timers all use up


Really?

It looks like the F446 range has 14 timers:

AndrewNeil_0-1777473249532.png

https://www.st.com/en/microcontrollers-microprocessors/stm32f446/products.html

 

And there are STM32s available with up to 22 timers:

AndrewNeil_1-1777473645105.png

https://www.st.com/content/st_com/en/stm32-mcu-developer-zone/mcu-portfolio.html

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

@HMSEdinburge wrote:

 can software timer able to drive an external motor? 


A timer - whether software or hardware - just toggles an output pin.

It makes no difference to the timer (whether SW or HW) what is externally connected to that pin.

 

As @LCE said, the limitation on using SW timers is going to be the maximum speed achievable.

Also the CPU load.

 

But, again, you can easily just try it out: write some code to toggle a pin, watch it on your scope, check CPU utilisation...

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.
Gyessine
ST Employee

Hello @HMSEdinburge 
Well, it's doable but as @Andrew Neil and @LCE but do not expect high precision. Software timers can be used for motor control in low-precision or low-frequency applications. However, they are not a substitute for hardware timers when precise timing is required.

If the software timer you want to create have the same frequency and period as one of the hardware timers, you can create a variable that captures the hardware timer CNT register. Compare this variable with specified value (like a custom ARR) as a condition to perform the required task when the CNT register reaches the desired value for the software timer. This approach may improve precision slightly.

BR
Gyessine

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.