-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Synchronize timers #5
Comments
I don't think the eFlexPwm library exposes this functionality, but you should be able to achieve it by fiddling with the registers directly and using xbar connect in addition to using the eFlexPwm library for everything else. Here are some interesting links related to your question: I was working on a solution to this very problem a while ago and some code snippets I had were:
The above code is untested and I got distracted from that particular bit of functionality. I'll come back to it at some point no doubt. Nevertheless you might find some useful bits in there to help you solve your problem. If you do find a solution then please paste it here. |
I considered incorporating this code ( https://www.nxp.com/docs/en/application-note/AN5142.pdf Chapter 3.14), but I'm having some trouble. .) |
You might find this post quite interesting. It is doing exactly what you want to do. Copied from the PJRC forum:
I happen to recently having been attempting the same thing, synchronizing different FlexPWM modules. It took me quite some hours figuring this out and I think I can explain what you saw back in 2020 (!)
Turns out the trigger outputs are one clock in duration (not the FlexPWM clock either - slowing this down doesn't help, I suspect Another gotcha I discovered was the inconsistent numbering of FlexPWM submodules - in most of the documentation each FlexPWM unit has submodules 0..3, whereas the trigger output lines to the XBAR switch are named
so for many hours I was bashing my head against this by assuming FLEXPWM4_PWM2_OUT_TRIG0 was for submodule 2
I've then routed the correct signal to EXT_SYNC on the other units and get them to sync up with only 20ns delay at 600MHz |
Hi, so I'm using the eFlexPwm library, and for now I can't find a way to synchronize two different modules. Is the library capable of doing that? Or only submodules?
So, I have this code, and when I check with an oscilloscope the signals from FlexPWM2 there's almost no deviation (100ns). But when I compare outputs from PWM2 and PWM 4 there's a delay of 3.0 us. Is there any way to synchronize the timers? So they start at the same time?
/*
eFlexPwm Simple Example
This example generates 3 pairs of PWM signals on a Teensy 4.1 board
Each signal pair corresponds to the PWMA output and the PWMB (A's complement)
output of an eFlexPWM submodule.
In this example, sub-modules 0, 2 and 3 of PWM2 are used. On the Teensy 4.1 board,
this corresponds to the following pins:
This example displays a message on the Serial link (USB CDC), and any error messages:
eFlexPwm Simple Example
Submodules successfuly started
*/
#include <Arduino.h>
#include <eFlexPwm.h>
//#include "TeensyTimerTool.h"
// Definitions
// ----------------------------------------------------------------------------
// avoid systematically prefixing objects with the namespace
using namespace eFlex;
//using namespace TeensyTimerTool;
// Variables
// ----------------------------------------------------------------------------
/* PWM frequence in hz. */
const uint32_t PwmFreq = 15000;
const uint32_t sinefreq = 50;
// My eFlexPWM submodules (Hardware > PWM2: SM[0], SM[2], SM[3])
SubModule Sm20(4,33);
SubModule Sm22(6,9);
SubModule Sm40(22);
SubModule Sm41(23);
SubModule Sm42(2,3);
// Initialize Timers for PWM2 and PWM4
Timer &Tm2 = Sm20.timer();
Timer &Tm4 = Sm40.timer();
// Duty Cycle in %
uint8_t dutyCyclePercent = 0;
// Code
void setup_FlexPWM2() {
Config myConfig;
myConfig.setReloadLogic(kPWM_ReloadPwmFullCycle); // Use full cycle reload
myConfig.setPwmFreqHz(PwmFreq); // Set PWM Frequency for PWM2.
// Initialize SubModule 0
Sm20.configure(myConfig);
// Initialize SubModule 2
myConfig.setClockSource(kPWM_Submodule0Clock); //Same clock as SubModule 0
myConfig.setPrescale(kPWM_Prescale_Divide_1);
myConfig.setInitializationControl(kPWM_Initialize_MasterSync);
Sm22.configure (myConfig);
}
void setup_FlexPWM4() {
Config myConfig;
myConfig.setReloadLogic(kPWM_ReloadPwmFullCycle); // Use full cycle reload
myConfig.setPwmFreqHz(PwmFreq); // Set PWM Frequency for PWM2.
// Initialize SubModule 0
Sm40.configure(myConfig);
// Initialize SubModule 1
myConfig.setClockSource(kPWM_Submodule0Clock); //Same clock as SubModule 0
myConfig.setPrescale(kPWM_Prescale_Divide_1);
myConfig.setInitializationControl(kPWM_Initialize_MasterSync);
Sm41.configure(myConfig);
Sm42.configure(myConfig);
}
void setup(){
setup_FlexPWM2();
setup_FlexPWM4();
Tm2.begin();
Tm4.begin();
}
void loop() {
int dutyCyclePercent_A = 50;
int dutyCyclePercent_B = 50;
Sm20.updateDutyCyclePercent (dutyCyclePercent_A, ChanA);
Sm20.updateDutyCyclePercent (dutyCyclePercent_B, ChanB);
Sm22.updateDutyCyclePercent (dutyCyclePercent_A, ChanA);
Sm22.updateDutyCyclePercent (dutyCyclePercent_B, ChanB);
Sm40.updateDutyCyclePercent (dutyCyclePercent_A); //Only has one channel
Sm41.updateDutyCyclePercent (dutyCyclePercent_B); //Only has one channel
Sm42.updateDutyCyclePercent (dutyCyclePercent_A, ChanA);
Sm42.updateDutyCyclePercent (dutyCyclePercent_B, ChanB);;
Tm2.setPwmLdok();
Tm4.setPwmLdok();
}
The text was updated successfully, but these errors were encountered: