New Arduino eFlexPWM Library

epsilonrt

Member
Hi,

The iMxRT1062 used in Teensy 4.x was designed by NXP to perform scalar or vector motor control.
I published the Arduino eFlexPwm library allowing to use all the features of NXP eFlexPwm timers to control motors or inverters.
It is still possible to use analogWrite() on timers that are not used by the library.

This library is available in the library managers Arduino and Platformio.
The code is published on GitHub, it is still in beta version, but the work is progressing well.

Here, for example, is the result of the generation of a sine for the control of a single-phase inverter :

rigol_single_phase_inverter_400hz.png

and the corresponding code


I am currently working on the control of a three-phase motor.

Hope this is useful to you.
 
Wow, this is very nice. There have been some threads regarding generation of center-aligned and complementary PWM, but nothing like this. Did you create this from scratch?
 
If you look at the code you will see that I rely on the NXP driver code, the NXP part is in C language, it is separated from my code in the src/nxp folder
The goal of my work is to make all the power of eFlexPwm accessible to Arduino programmers with a "simple" use.
 
The source code link does not work, here it is:

Code:
#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);
}
 
If you look at the code you will see that I rely on the NXP driver code, the NXP part is in C language, it is separated from my code in the src/nxp folder
The goal of my work is to make all the power of eFlexPwm accessible to Arduino programmers with a "simple" use.

Yes, I look forward to using it.
 
@epsilonrt, I got your latest version 1.2.5 today, and finally got around to running your test programs and looking at signals. Everything worked as you described (very good comments). I need to do some reading to understand the overflow interrupt and duty cycle update.

Just FYI, SinglePhaseInverter has the code shown below. Did you mean to delete the first clause with Tm2.begin()?

Code:
  // Start all submodules
  if (Tm2.begin() != true) {
    Serial.println ("Failed to start submodules");
    exit (EXIT_FAILURE);
  }

  // synchronize registers and start all submodules
  if (Tm2.begin() != true) {
    Serial.println ("Failed to start submodules");
    exit (EXIT_FAILURE);
  }
  else {
    Serial.println ("Submodules successfuly started");
    // Sm20.printRegs(); // to see the values of the VALx registers
  }
 
Thanks @Joepasquariello, indeed the first clause should be deleted.
I correct on the github repository.
The latest version is 0.2.5, I will switch to version 1 when I have carried out all the tests exhaustively.
Currently I'm working on an educational version of VFD for a 3 phase asynchronous motor and so far everything is working as expected.
 
Hey, I want to create 50khz pwm with 12 bit resoution with 2 synced output i.e, (1normal & 2 inverted) with deadtime in it & center aligned. do you think it is possible to achieve with this eFlexpwm library ? I want to run 2 Mosfets with that for Synchronous Buck Converter do you think it is possible?
 
Can you suggest exactly which examples should I be looking in to ? It will be very helpful for me.
There are only two examples. One of them has 3 pairs of complementary signals, and the other has 2 pairs. I'm guessing the 3-pair example called "simple" is more applicable, but I really don't know because you didn't say much in your post.
 
Back
Top