Skip to main content
Associate III
April 29, 2026
Question

Can FreeRTOS software timer generate PWM?

  • April 29, 2026
  • 6 replies
  • 679 views

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 ?

6 replies

Andrew Neil
Super User
April 29, 2026

@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.
Associate III
April 29, 2026

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. 

Andrew Neil
Super User
April 29, 2026

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.
LCE
Principal II
April 29, 2026

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.

 

Associate III
April 29, 2026

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

 

Andrew Neil
Super User
April 29, 2026

@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.
Technical Moderator
April 30, 2026

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.
mƎALLEm
Technical Moderator
April 30, 2026

As said by @Andrew Neil ,

There are 14 timers in STM32F446:

mALLEm_1-1777538987864.png

You are using half of timers resources in your case only 1 to 7 in your case.. So why bothering yourself with the software timer usage?

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
Associate III
April 30, 2026

Hi there, i got total 63 motors to control. F446 has 14 timer, TIM6 and TIM7 is a general timer, does not have CCR and AAR registers, so cant use for PWM. 

Anyone has the source code for software timer using FreeRTOS CMSIS V

mƎALLEm
Technical Moderator
April 30, 2026

@HMSEdinburge wrote:

Hi there, i got total 63 motors to control. 

Anyone has the source code for software timer using FreeRTOS CMSIS V


63 motors to be controlled with STM32F446 with FreeRTOS software timer to generate PWM?

I don't think this is feasible. Think about the CPU load and the accuracy of the PWM duty cycle!

This is something that needs to be done with hardware ..

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
AScha.3
Super User
May 1, 2026

Hi,

to get good info, you should also give some good info:

- how many motors need control at same time ?

- which kind of motor and driver ? (DC, stepper, 3-phase ...etc)

- needed speed of pwm or control signal ?

- and resolution of pwm needed (how many steps from 0...100% ) ?

 

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

These are the motors i m using, BLDC, stepper, servo actuators, stepper motor need 1/32 steps, BLDC needs PWM, and their various motor drivers, gate drivers etc. Total 63 motors. 

AScha.3
Super User
May 2, 2026

So to answer your question:

>Can FreeRTOS software timer generate PWM?

yes.

+

can you control 64 motors of different kind in real time just by using some RTOS:

no.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Lead III
May 4, 2026
  • Software PWM is limited in frequency/resolution. If you want 0-100 steps and use a 1 ms tick your frequency will be 10 Hz. 
  • You can share timers and handle things in software. But this results in dither as interrupt latency is not always predictable. Also can be a high load to CPU
  • Best is to use hardware timer
  • second best is to use external PWM IC. For example a SPI or I2C PWM IC. Such as the 16 channel PCA9685. Note that the PCA9685 is optimized for LEDs, but you can can connect it to a transistor switching a DC motor. Or to the enable pin of an H-bridge and use software to switch direction.

 


@HMSEdinburge wrote:

These are the motors i m using, BLDC, stepper, servo actuators, stepper motor need 1/32 steps, BLDC needs PWM, and their various motor drivers, gate drivers etc. Total 63 motors.


  • BLDC needs 3 phase control synchronized with back-emf detection. An MCU is not powerful enough to do several of these. Get a dedicated motor control IC/complete motor driver for each BLDC motor.
  • Get a dedicated motor control IC/complete motor driver for each stepper motor.
  • If they are RC servos you can use the PCA9685 for the 1-2ms pulse

 

63 motors seems like a lot. If the MCU has to do additional processing it may be underpowered. It's also rather ambitious for a beginner such as yourself. What is the application?

"Kudo posts if you have the same problem and kudo replies if the solution works.Click ""Accept as Solution"" if a reply solved your problem. If no solution was posted please answer with your own."