Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: New Arduino eFlexPWM Library

  1. #1
    Junior Member
    Join Date
    Feb 2023
    Posts
    6

    New Arduino eFlexPWM Library

    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 :

    Click image for larger version. 

Name:	rigol_single_phase_inverter_400hz.png 
Views:	21 
Size:	69.7 KB 
ID:	30702

    and the corresponding code


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

    Hope this is useful to you.

  2. #2
    Senior Member
    Join Date
    Oct 2016
    Posts
    1,048
    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?

  3. #3
    Junior Member
    Join Date
    Feb 2023
    Posts
    6
    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.

  4. #4
    Junior Member
    Join Date
    Feb 2023
    Posts
    6
    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);
    }

  5. #5
    Senior Member
    Join Date
    Oct 2016
    Posts
    1,048
    Quote Originally Posted by epsilonrt View Post
    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.

  6. #6
    Senior Member
    Join Date
    Oct 2016
    Posts
    1,048
    @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
      }

  7. #7
    Junior Member
    Join Date
    Feb 2023
    Posts
    6
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •