eFlexPwm
Arduino eFlexPwm library for Teensy 4.x
eFlexPwm

Arduino eFlexPwm library for Teensy 4.x (and SparkFun MicroMod Teensy)

eFlexPwm Logo

GitHub release (latest by date including pre-releases) PlatformIO Registry Arduino Registry

Framework Teensy4.0 Teensy4.1 TeensyMM


This library allows advanced use of NXP eFlexPWM timers. These timers are very powerful and can be used to manage power stages, motor controls, etc.

The Arduino API is easy to use, unfortunately, the analogWrite() function allows you to do very little when compared to the very advanced possibilities of the eFlexPwm timers of NXP microcontrollers such as the i.MXRT 1062 which equips the Teesny 4.x !

Here is an example of 6 synchronized signals with deadtime generated by the eFlexPwmSimple program:

DsLogic View

These signals are generated with the source code below :

#include <eFlexPwm.h>
using namespace eFlex;
// My eFlexPWM submodules (Hardware > PWM2: SM[0], SM[2], SM[3])
SubModule Sm20 (4, 33);
SubModule Sm22 (6, 9);
SubModule Sm23 (36, 37);
// Tm2 simplifies access to the functions that concern all the sub-modules
Timer &Tm2 = Sm20.timer();
uint8_t dutyCyclePercent = 0; // the duty cycle in %
const uint32_t PwmFreq = 18000; // FPwm 18kHz
void setup() {
Config myConfig;
myConfig.setReloadLogic (kPWM_ReloadPwmFullCycle);
myConfig.setPairOperation (kPWM_ComplementaryPwmA);
myConfig.setPwmFreqHz (PwmFreq);
// Initialize submodule
Sm20.configure (myConfig);
// Initialize submodule 2, make it use same counter clock as submodule 0.
myConfig.setClockSource (kPWM_Submodule0Clock);
myConfig.setPrescale (kPWM_Prescale_Divide_1);
myConfig.setInitializationControl (kPWM_Initialize_MasterSync);
Sm22.configure (myConfig);
Sm23.configure (myConfig);
Tm2.setupDeadtime (500); // deatime 500ns
// synchronize registers and start all submodules
Tm2.begin();
}
void loop() {
dutyCyclePercent += 5;
// Update duty cycles for all 3 PWM signals
Sm20.updateDutyCyclePercent (dutyCyclePercent, ChanA);
Sm22.updateDutyCyclePercent (dutyCyclePercent >> 1, ChanA);
Sm23.updateDutyCyclePercent (dutyCyclePercent >> 2, ChanA);
// Set the load okay bit for all submodules to load registers from their buffer
Tm2.setPwmLdok();
if (dutyCyclePercent >= 100) {
dutyCyclePercent = 5;
}
// Delay at least 100 PWM periods
delayMicroseconds ( (1000000U / PwmFreq) * 100);
}
@ kPWM_ComplementaryPwmA
@ kPWM_Initialize_MasterSync
@ kPWM_ReloadPwmFullCycle
@ kPWM_Prescale_Divide_1
@ kPWM_Submodule0Clock
Library namespace.

But this is only a small example of the possibilities offered by the eFlexPwm timers which are also designed to generate SPWM signals (Sine Pulse Width Modulation) :

Rigol View

This library is based on the code of the FSL_PWM driver from the NXP SDK, it is published under the BSD 3-Clause License. It is still in beta version.

Teesny eFlexPWM pins

The table below will allow you to identify the pins of the Teensy to use, by clicking on it, you can the Libre Office version of this table :

Teesny eFlexPWM pins