PWM Timer synchronization

Hi there

To drive a TI BLDC Motor Driver DRV8305, i need 3 synchronized PWM signals. But in case that on the Photon per timer just 2 pwm channels are available, i need to synchronize another timer and its PWM channels.
For this i like to take timer 3 as master and timer 4 as slave.
I tried after hours of searching this with the STM32 peripheral library:

// define Master-Timer 3 (pwmUH+pwmVH)
TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Enable);
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
// define Slave-Timer 4 (pwmWH); start with Timer3
TIM_SelectInputTrigger(TIM4, TIM_TS_ITR2);
TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Trigger);

But it doesn’t work. Does anyone know if i made a fault, or if it is possible at all to do that with the peripheral library or if the Firmware of the Photon overrides this commands? Or is there another possibility?

Thanks a lot for your help.
Kind regards
Silvan

Yes, you can use the STM32 standard peripheral library from user firmware, but it’s a little tricky.

If you can provide the exact requirements for your PWM outputs and I can probably come up with the right sample code.

There are certain things that are really difficult to do, like complementary outputs, because not all of the STM32 pins are exposed on the Photon, but many things are possible.

Hy rickkas

Thanks a lot for the fast answer.
As I sad, I like to drive an DRV8305. This one needs 3 PWM signals which are synchronised (rising flank of high times starts at the same time and not displaced). For PWM channels in the same timer Group this is automaticly correct. But because I Need a second timer for the third PWM, I have to synchronize two timers.

Exactly, I like to set timer 3 with channel D2 and D3 as Master and Timer 4 channel D1 as Slave PWM.

Davidcane did something simmilar with PWM complementary outputs
And his solution was also the peripherial library.

Hy rickkas
Do you (or someone els) already see a solution for my problem?
Kind regards
Silvan

I was unable to get it to work either. Even for a simple single PWM example. I didn’t have time to dig into it, I presume that, like interrupt handling, some of these things are taken over by system firmware and hard to override. Timers themselves work fine, like from the SparkIntervalTimer library, so maybe it’s a timer to output pin thing that’s getting reset by system firmware? Or maybe I just did it incorrectly? Not really sure.

Out of curiosity, what is your target PWM frequency?
Can you feed the digital output of the 2nd PWM into an interrupt input pin causing a one shot timer to occur with digital I/O.

Or perhaps you already tried that?

Dear rickkas
Thanks for trying. Bad news :frowning:
If this will not work, i canot use the photon which i like for everything else…

Dear coveguy: the pwm frequency is 24khz to be unhearable and the resolution is 11bit so the shortest hightime is 20ns. No chance to do this with IOs.

Perhaps an administrator can help? Otherwise i also think its a problem of firmware, i will place this topic there…

It’s not necessary (nor desirable) to post again. We read all of the topics. I’m not saying it’s impossible, but it’s certainly not one of the things the Photon was designed to do, so there will be a lot of do-it-yourself required in figuring out how to do it.

Personally, at a certain level I’d lean toward a lower-level device to take that over (maybe a PIC) and leave the high-level stuff to to the Photon.

3 Likes

Dear rickkas
Thanks. I didn’t know thatyou are a admin.

A last chance i see: perhaps it is possible not to use the firmware commands to generate the pwm as for example with “analogwrite()” and consequently only to use the standard peripheral library from st? Or did you allready try this?

Kind regards
Silvan

I did have better success setting the pinMode(pin, OUTPUT) and then using tone() to set it in PWM mode. analogWrite() should have the same effect. I did have better luck using the STM32 standard library functions changing the timer behavior after doing that, but I wasn’t 100% sure it was working properly, But, yes, that’s what I’d recommend starting with and I think it has a good chance of working.

I GOT IT WORKING!!! I AM THE KING!! :smiley:

First I got it working only with the Standard peripheral library and the example in it documentation and an example of the cubex.
This get me a better understanding. So I tried again to do it with generating the pwm by particle Firmware.
The only changes to the beginning was, that I have to define master and slave AFTER I started the pwm already and to update the Synchronisation not only at timer starts, but at every Update of the master. The code Looks realy easy like this:

   //  analogWrite(pwmUH, 200, 28000);
   //  analogWrite(pwmVH, 200, 28000);
   //  analogWrite(pwmWH, 200, 28000);

   //  define Master-Timer 3 (pwmUH+pwmVH)
   TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);
   TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
   // define Slave-Timer 4 (pwmWH); synchrnoized with Timer3
   TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);
   TIM_SelectInputTrigger(TIM4, TIM_TS_ITR2);         

So easy, but hours and hours of thinking. Or in other words: thats programming :wink:

3 Likes