Giter VIP home page Giter VIP logo

eflexpwm's Introduction

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);
}

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

eflexpwm's People

Contributors

epsilonrt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

lfarrand

eflexpwm's Issues

Synchronize timers

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:

PWM SubModule Pol Teensy Native
2 0 A 4 EMC_06
2 0 B 33 EMC_07
2 2 A 6 B0_10
2 2 B 9 B0_11
2 3 A 36 B1_02
2 3 B 37 B1_03
The frequency is fixed by the constant PwmFreq, here at 18 kHz
On sub-module 0, the duty cycle changes by 5 in 5 from 0 to 100% every 100 PWM periods.
The duty cycle of sub-module 2 is equal to that of sub-module 0 divided by 2.
The duty cycle of sub-module 3 is equal to that of sub-module 0 divided by 4.

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();
}

warning: list-initializer for non-class type must not be parenthesized

PACKAGES:

  • framework-arduinoteensy @ 1.158.0 (1.58)
  • tool-teensy @ 1.158.0 (1.58)
  • toolchain-gccarmnoneeabi-teensy @ 1.110301.0 (11.3.1)
.pio\libdeps\teensy41\eFlexPwm\src\source\eFlexPwmSubmodule.cpp: In constructor 'eFlex::SubModule::SubModule(int, int)':
.pio\libdeps\teensy41\eFlexPwm\src\source\eFlexPwmSubmodule.cpp:15:5: warning: list-initializer for non-class type must not be parenthesized
   15 |     m_pin ({Pin (pinA), Pin (pinB) }), m_tmidx (-1), m_smidx (-1), m_ptr (0), m_wasbegin (false) {

fsl_compat.h:15:0: warning: "assert" redefined

In file included from .pio\libdeps\teensy41\eFlexPwm\src/nxp/drivers/fsl_pwm.h:11:0,
                 from .pio\libdeps\teensy41\eFlexPwm\src/component/eFlexPwmGlobal.h:9,
                 from .pio\libdeps\teensy41\eFlexPwm\src/component/eFlexPwmPin.h:9,
                 from .pio\libdeps\teensy41\eFlexPwm\src/eFlexPwm.h:9,
                 from C:\Users\pasca\OneDrive\Documents\epsilonrt\src\ProjetElectro2023\lib\modules\FlexControl.h:1,
                 from C:\Users\pasca\OneDrive\Documents\epsilonrt\src\ProjetElectro2023\lib\modules\FaultInputs.cpp:3:
.pio\libdeps\teensy41\eFlexPwm\src/nxp/drivers/fsl_compat.h:15:0: warning: "assert" redefined
 #define assert(x)
 ^
In file included from .pio\libdeps\teensy41\Embedded Template Library\include/etl/error_handler.h:44:0,
                 from .pio\libdeps\teensy41\Embedded Template Library\include/etl/alignment.h:37,
                 from .pio\libdeps\teensy41\Embedded Template Library\include/etl/queue_spsc_isr.h:35,
                 from C:\Users\pasca\OneDrive\Documents\epsilonrt\src\ProjetElectro2023\lib\modules\InterruptControl.h:2,
                 from C:\Users\pasca\OneDrive\Documents\epsilonrt\src\ProjetElectro2023\lib\modules\FaultInputs.h:4,
                 from C:\Users\pasca\OneDrive\Documents\epsilonrt\src\ProjetElectro2023\lib\modules\FaultInputs.cpp:1:
c:\users\pasca\.platformio\packages\[email protected]\arm-none-eabi\include\assert.h:16:0: note: this is the location of the previous definition
 # define assert(__e) ((__e) ? (void)0 : __assert_func (__FILE__, __LINE__, \

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.