eFlexPwm
Arduino eFlexPwm library for Teensy 4.x
eFlexPwmSubmodule.h
1 /*
2  Copyright (c) 2023, epsilonrt
3  All rights reserved.
4 
5  SPDX-License-Identifier: BSD-3-Clause
6 */
7 #pragma once
8 
9 #include "eFlexPwmPin.h"
10 #include "eFlexPwmConfig.h"
11 #include "eFlexPwmTimer.h"
12 
13 namespace eFlex {
14 
20  class SubModule {
21  public:
35  SubModule (int pinChanA, int pinChanB = -1);
36 
49  bool isValid() const;
50 
59  bool configure (const Config &config);
60 
64  inline const Config &config() const {
65  return m_config;
66  }
67 
81  bool begin (bool doStart = true, bool doSync = true);
82 
92  void enable (bool value = true);
93 
101  inline void disable () {
102  enable (false);
103  }
104 
108  bool isEnabled() const;
109 
115  inline void start (bool value = true) {
116  timer().start (1 << index(), value);
117  }
118 
119 
123  inline void stop () {
124  start (false);
125  }
126 
134  inline void setPwmLdok (bool value = true) {
135 
136  timer().setPwmLdok (1 << index(), value);
137  }
138 
142  inline Timer &timer() {
143  return *TM[m_tmidx];
144  }
145 
149  inline uint8_t index() const {
150  return m_smidx;
151  }
152 
163  bool setPwmFrequency (uint32_t freq, bool doSync = true, bool adjustPrescaler = false);
164 
170  inline uint32_t pwmFrequency() const {
171  return config().pwmFreqHz();
172  }
173 
183  // TODO: Test setPwmMode()
184  inline bool setPwmMode (pwm_mode_t mode, bool doSync = true) {
185  m_config.setMode (mode);
186  return updateSetting (doSync);
187  }
188 
194  inline pwm_mode_t pwmMode() const {
195  return config().mode();
196  }
197 
205  inline void setupOutputEnable (Channel channel, bool activate = true) {
206 
207  m_signal[channel].pwmchannelenable = activate;
208  }
209 
216  inline void setupOutputEnable (bool activate = true) {
217  for (uint8_t i = ChanA; i <= ChanB ; i++) {
218  setupOutputEnable (static_cast<Channel> (i), activate);
219  }
220  }
221 
227  bool outputEnableSetting (Channel channel) {
228 
229  return m_signal[channel].pwmchannelenable;
230  }
231 
239  inline void setupDutyCyclePercent (Channel channel, uint8_t dutyCyclePercent) {
240  m_signal[channel].dutyCyclePercent = dutyCyclePercent;
241  }
242 
249  inline void setupDutyCyclePercent (uint8_t dutyCyclePercent) {
250  for (uint8_t i = ChanA; i <= ChanB ; i++) {
251  setupDutyCyclePercent (static_cast<Channel> (i), dutyCyclePercent);
252  }
253  }
254 
260  uint8_t dutyCyclePercentSetting (Channel channel) {
261  return m_signal[channel].dutyCyclePercent;
262  }
263 
271  inline void setupLevel (Channel channel, pwm_level_select_t level) {
272  m_signal[channel].level = level;
273  }
274 
281  inline void setupLevel (pwm_level_select_t level) {
282  for (uint8_t i = ChanA; i <= ChanB ; i++) {
283  setupLevel (static_cast<Channel> (i), level);
284  }
285  }
286 
293  return m_signal[channel].level;
294  }
295 
305  inline void setupDeadtime (Channel channel, uint16_t deadtimeValue, uint32_t unit = 1) {
306  if (unit == 1) {
307  m_signal[channel].deadtimeValue = deadtimeValue;
308  }
309  else {
310  m_signal[channel].deadtimeValue = ( (uint64_t) timer().srcClockHz() * deadtimeValue) / unit;
311  }
312  }
313 
322  inline void setupDeadtime (uint16_t deadtimeValue, uint32_t unit = 1) {
323  for (uint8_t i = ChanA; i <= ChanB ; i++) {
324  setupDeadtime (static_cast<Channel> (i), deadtimeValue, unit);
325  }
326  }
327 
333  uint16_t deadtimeSetting (Channel channel) {
334  return m_signal[channel].deadtimeValue;
335  }
336 
345  inline void setupFaultState (Channel channel, pwm_fault_state_t faultState) {
346  m_signal[channel].faultState = faultState;
347  }
348 
356  inline void setupFaultState (pwm_fault_state_t faultState) {
357  for (uint8_t i = ChanA; i <= ChanB ; i++) {
358  setupFaultState (static_cast<Channel> (i), faultState);
359  }
360  }
361 
368  return m_signal[channel].faultState;
369  }
370 
384  bool updateSetting (bool doSync = true);
385 
396  inline void setInitValue (uint16_t value) {
397  m_ptr->SM[m_smidx].INIT = value;
398  }
399 
410  inline void setVal0Value (uint16_t value) {
411  m_ptr->SM[m_smidx].VAL0 = value;
412  }
413 
424  inline void setVal1Value (uint16_t value) {
425  m_ptr->SM[m_smidx].VAL1 = value;
426  }
427 
438  inline void setVal2Value (uint16_t value) {
439  m_ptr->SM[m_smidx].VAL2 = value;
440  }
441 
452  inline void setVal3Value (uint16_t value) {
453  m_ptr->SM[m_smidx].VAL3 = value;
454  }
455 
466  inline void setVal4Value (uint16_t value) {
467  m_ptr->SM[m_smidx].VAL4 = value;
468  }
469 
480  inline void setVal5Value (uint16_t value) {
481  m_ptr->SM[m_smidx].VAL5 = value;
482  }
483 
489  inline uint16_t initValue () {
490  return m_ptr->SM[m_smidx].INIT;
491  }
492 
498  inline uint16_t val0Value () {
499  return m_ptr->SM[m_smidx].VAL0;
500  }
501 
507  inline uint16_t val1Value () {
508  return m_ptr->SM[m_smidx].VAL1;
509  }
510 
516  inline uint16_t val2Value () {
517  return m_ptr->SM[m_smidx].VAL2;
518  }
519 
525  inline uint16_t val3Value () {
526  return m_ptr->SM[m_smidx].VAL3;
527  }
528 
534  inline uint16_t val4Value () {
535  return m_ptr->SM[m_smidx].VAL4;
536  }
537 
543  inline uint16_t val5Value () {
544  return m_ptr->SM[m_smidx].VAL5;
545  }
546 
552  void printRegs (Stream &out = Serial);
553 
554  //-----------------------------------------------------------------------
555  // NXP SDK WRAPPER
556  //-----------------------------------------------------------------------
557 
571  inline void updateDutyCyclePercent (uint8_t dutyCyclePercent, Channel channel = ChanA) {
572 
573  m_duty[static_cast<uint8_t> (channel)] = reloadValue(dutyCyclePercent); // [0, 65535]
574  m_signal[static_cast<uint8_t> (channel)].dutyCyclePercent = dutyCyclePercent; // [0, 100]
575  PWM_UpdatePwmDutycycleHighAccuracy (ptr(), SM[m_smidx], kPwmChan (channel), m_config.m_mode, m_duty[static_cast<uint8_t> (channel)]);
576  }
577 
591  inline void updateDutyCycle (uint16_t dutyCycle, Channel channel = ChanA) {
592 
593  m_duty[static_cast<uint8_t> (channel)] = dutyCycle; // [0, 65535]
594  PWM_UpdatePwmDutycycleHighAccuracy (ptr(), SM[m_smidx], kPwmChan (channel), m_config.m_mode, dutyCycle);
595  }
596 
607  inline bool setupPwmPhaseShift (Channel channel, uint8_t shiftvalue, bool doSync = true) {
608 
609  return (PWM_SetupPwmPhaseShift (ptr(), SM[m_smidx], kPwmChan (channel), m_config.pwmFreqHz(), timer().srcClockHz(), shiftvalue, doSync) == kStatus_Success);
610  }
611 
621  inline void setupInputCapture (Channel channel, const pwm_input_capture_param_t *inputCaptureParams) {
622  PWM_SetupInputCapture (ptr(), SM[m_smidx], kPwmChan (channel), inputCaptureParams);
623  }
624 
634  inline void setupForceSignal (Channel channel, pwm_force_signal_t mode) {
635  PWM_SetupForceSignal (ptr(), SM[m_smidx], kPwmChan (channel), mode);
636  }
637 
644  inline uint32_t statusFlags () {
645  return PWM_GetStatusFlags (ptr(), SM[m_smidx]);
646  }
647 
654  inline void clearStatusFlags (uint32_t mask) {
655  PWM_ClearStatusFlags (ptr(), SM[m_smidx], mask);
656  }
657 
664  inline void enableInterrupts (uint32_t mask) {
665  PWM_EnableInterrupts (ptr(), SM[m_smidx], mask);
666  }
667 
674  inline void disableInterrupts (uint32_t mask) {
675  PWM_DisableInterrupts (ptr(), SM[m_smidx], mask);
676  }
677 
684  inline uint32_t enabledInterrupts () {
685  return PWM_GetEnabledInterrupts (ptr(), SM[m_smidx]);
686  }
687 
693  inline void setDMAWatermarkControl (pwm_watermark_control_t pwm_watermark_control) {
694  PWM_DMAFIFOWatermarkControl (ptr(), SM[m_smidx], pwm_watermark_control);
695  }
696 
702  inline void setDMACaptureSourceSelect (pwm_dma_source_select_t pwm_dma_source_select) {
703  PWM_DMACaptureSourceSelect (ptr(), SM[m_smidx], pwm_dma_source_select);
704  }
705 
713  inline void enableDMACapture (uint16_t mask, bool activate = true) {
714  PWM_EnableDMACapture (ptr(), SM[m_smidx], mask, activate);
715  }
716 
722  inline void enableDMAWrite (bool activate) {
723  PWM_EnableDMAWrite (ptr(), SM[m_smidx], activate);
724  }
725 
737  inline void setVALxValue (pwm_value_register_t valueRegister, uint16_t value) {
738  PWM_SetVALxValue (ptr(), SM[m_smidx], valueRegister, value);
739  }
740 
747  inline uint16_t VALxValue (pwm_value_register_t valueRegister) {
748  return PWM_GetVALxValue (ptr(), SM[m_smidx], valueRegister);
749  }
750 
761  inline void enableOutputTrigger (pwm_value_register_t valueRegister, bool activate = true) {
762  PWM_OutputTriggerEnable (ptr(), SM[m_smidx], valueRegister, activate);
763  }
764 
773  inline void enableOutputTrigger (uint16_t valueRegisterMask) {
774  PWM_ActivateOutputTrigger (ptr(), SM[m_smidx], valueRegisterMask);
775  }
776 
785  inline void disableOutputTrigger (uint16_t valueRegisterMask) {
786  PWM_DeactivateOutputTrigger (ptr(), SM[m_smidx], valueRegisterMask);
787  }
788 
798  inline void setupSwCtrlOut (Channel channel, bool value) {
799  PWM_SetupSwCtrlOut (ptr(), SM[m_smidx], kPwmChan (channel), value);
800  }
801 
813  inline void setPwmFaultState (Channel channel, pwm_fault_state_t faultState) {
814  PWM_SetPwmFaultState (ptr(), SM[m_smidx], kPwmChan (channel), faultState);
815  }
816 
830  inline void setupFaultDisableMap (Channel channel, pwm_fault_channels_t pwm_fault_channels, uint16_t value) {
831  PWM_SetupFaultDisableMap (ptr(), SM[m_smidx], kPwmChan (channel), pwm_fault_channels, value);
832  }
833 
841  inline void enableOutput (Channel channel) {
842  PWM_OutputEnable (ptr(), kPwmChan (channel), SM[m_smidx]);
843  }
844 
852  inline void disableOutput (Channel channel) {
853  PWM_OutputDisable (ptr(), kPwmChan (channel), SM[m_smidx]);
854  }
855 
863  inline uint8_t dutyCyclePercent (Channel channel) {
864  return PWM_GetPwmChannelState (ptr(), SM[m_smidx], kPwmChan (channel));
865  }
866 
877  inline bool setOutputToIdle (Channel channel, bool idleStatus) {
878  return (PWM_SetOutputToIdle (ptr(), kPwmChan (channel), SM[m_smidx], idleStatus) == kStatus_Success);
879  }
880 
887  m_config.setPrescale (prescaler);
889  PWM_SetClockMode (ptr(), SM[m_smidx], prescaler);
890  }
891 
901  }
902 
907  return config().prescale();
908  }
909 
917  inline void setPwmForceOutputToZero (Channel channel, bool forcetozero) {
918  PWM_SetPwmForceOutputToZero (ptr(), SM[m_smidx], kPwmChan (channel), forcetozero);
919  }
920 
927  inline void setChannelOutput (Channel channel, pwm_output_state_t outputstate) {
928  PWM_SetChannelOutput (ptr(), SM[m_smidx], kPwmChan (channel), outputstate);
929  }
930 
936  inline uint32_t minPwmFrequency() const {
937  return m_fpmin;
938  }
939 
949  bool adjustPrescaler (uint32_t pwmFreq);
950 
951  protected:
952  inline PWM_Type *ptr() {
953  return m_ptr;
954  }
955 
956  inline const PWM_Type *ptr() const {
957  return m_ptr;
958  }
959 
960  inline pwm_signal_param_t *kPwmSignals() {
961  return m_signal;
962  }
963 
964  inline const pwm_signal_param_t *kPwmSignals() const {
965  return m_signal;
966  }
967 
968  static inline void setSignalToDefault (pwm_signal_param_t &signal) {
969  signal.pwmchannelenable = true;
970  signal.level = kPWM_HighTrue;
971  signal.dutyCyclePercent = 50;
972  signal.deadtimeValue = 0;
974  }
975 
976  private:
977  Pin m_pin[NofPins];
978  uint8_t m_tmidx;
979  uint8_t m_smidx;
980  PWM_Type *m_ptr;
981  pwm_signal_param_t m_signal[NofPins];
982  Config m_config;
983  bool m_wasbegin;
984  uint32_t m_fpmin;
985  uint16_t m_duty[NofPins];
986  };
987 
988  extern SubModule *SmList[NofTimers][NofSubmodules];
989 }
enum _pwm_dma_source_select pwm_dma_source_select_t
List of PWM capture DMA enable source select.
enum _pwm_force_signal pwm_force_signal_t
PWM output options when a FORCE_OUT signal is asserted.
enum _pwm_value_register pwm_value_register_t
List of PWM value registers.
enum _pwm_output_state pwm_output_state_t
PWM channel output status.
enum _pwm_clock_prescale pwm_clock_prescale_t
PWM prescaler factor selection for clock source.
enum _pwm_fault_channels pwm_fault_channels_t
List of PWM fault channels.
enum _pwm_fault_state pwm_fault_state_t
PWM output fault status.
enum _pwm_mode pwm_mode_t
PWM operation mode.
enum _pwm_watermark_control pwm_watermark_control_t
PWM FIFO Watermark AND Control.
enum _pwm_level_select pwm_level_select_t
PWM output pulse mode, high-true or low-true.
@ kPWM_HighTrue
@ kPWM_PwmFaultState0
Library namespace.
uint16_t reloadValue(uint8_t dutyCyclePercent)
Convert duty cycle to reload value.
Channel
PWM Channel.
Configuration.
uint32_t pwmFreqHz() const
PWM signal frequency in Hz, default 5000.
void setPrescale(pwm_clock_prescale_t prescale)
Set the Pre-scaler to divide down the clock.
void setMode(pwm_mode_t mode)
Set the PWM operation mode.
pwm_clock_prescale_t prescale() const
Pre-scaler to divide down the clock, default kPWM_Prescale_Divide_1.
pwm_mode_t mode() const
PWM operation mode, default CenterAligned.
void setChannelOutput(Channel channel, pwm_output_state_t outputstate)
This function set the output state of the PWM pin as requested for the current cycle.
uint8_t dutyCyclePercentSetting(Channel channel)
value of the parameter set by setupDutyCyclePercent (Channel, uint8_t)
pwm_clock_prescale_t prescaler() const
the pwm submodule prescaler
void setupSwCtrlOut(Channel channel, bool value)
Sets the software control output for a pin to high or low.
void setupForceSignal(Channel channel, pwm_force_signal_t mode)
Selects the signal to output on a PWM pin when a FORCE_OUT signal is asserted.
void setPwmForceOutputToZero(Channel channel, bool forcetozero)
This function enables-disables the forcing of the output of a given eFlexPwm channel to logic 0.
uint16_t val4Value()
Get the PWM VAL4 register.
void disableOutputTrigger(uint16_t valueRegisterMask)
Disables the PWM output trigger.
void enableDMAWrite(bool activate)
Enables or disables the PWM DMA write request.
void stop()
Stops the PWM counter for this submodule.
bool setupPwmPhaseShift(Channel channel, uint8_t shiftvalue, bool doSync=true)
Set PWM phase shift for PWM channel running on channel PWM_A, PWM_B which with 50% duty cycle....
uint8_t dutyCyclePercent(Channel channel)
Get the dutycycle value.
void setVal4Value(uint16_t value)
Set the PWM VAL4 register.
bool isValid() const
Checks the validity of the parameters passed to the constructor.
void setPwmLdok(bool value=true)
Sets or clears the PWM LDOK bit on all instantiated submodules for this timer.
uint16_t val1Value()
Get the PWM VAL1 register.
uint32_t pwmFrequency() const
Current PWM signal frequency in Hz.
void disableOutput(Channel channel)
Set PWM output disable.
bool setPwmFrequency(uint32_t freq, bool doSync=true, bool adjustPrescaler=false)
Set the Pwm Frequency.
void setupDutyCyclePercent(Channel channel, uint8_t dutyCyclePercent)
Setting the duty cycle before calling begin.
bool isEnabled() const
Returns true if the outputsof submodule were enabled.
void setupFaultState(pwm_fault_state_t faultState)
Setting the output fault status for all channels before calling begin.
uint32_t enabledInterrupts()
Gets the enabled PWM interrupts.
void setClockMode(pwm_clock_prescale_t prescaler)
Set the pwm submodule prescaler.
void setInitValue(uint16_t value)
Set the PWM INIT register.
void setupFaultState(Channel channel, pwm_fault_state_t faultState)
Setting the output fault status before calling begin.
void enableOutputTrigger(pwm_value_register_t valueRegister, bool activate=true)
Enables or disables the PWM output trigger.
void setDMAWatermarkControl(pwm_watermark_control_t pwm_watermark_control)
Capture DMA Enable Source Select.
void printRegs(Stream &out=Serial)
Print submodule registers to stream out.
uint32_t minPwmFrequency() const
Current minimal PWM frequency in Hz.
void setupDeadtime(Channel channel, uint16_t deadtimeValue, uint32_t unit=1)
Setting the deadtime before calling begin.
void enableDMACapture(uint16_t mask, bool activate=true)
Enables or disables the selected PWM DMA Capture read request.
void setupLevel(pwm_level_select_t level)
Setting the output pulse mode for all channels before calling begin.
bool adjustPrescaler(uint32_t pwmFreq)
Adjust the prescaler if necessary.
uint16_t deadtimeSetting(Channel channel)
value of the parameter set by setupDeadtime (Channel, uint16_t)
bool configure(const Config &config)
Configures the peripheral for basic operation.
void setupLevel(Channel channel, pwm_level_select_t level)
Setting the output pulse mode before calling begin.
bool outputEnableSetting(Channel channel)
value of the parameter set by setupOutputEnable (Channel, bool)
SubModule(int pinChanA, int pinChanB=-1)
Construct a new SubModule object.
void updateDutyCyclePercent(uint8_t dutyCyclePercent, Channel channel=ChanA)
Updates the PWM signal's dutycycle.
void setupOutputEnable(bool activate=true)
Setting output enable for all channels before calling begin.
uint16_t initValue()
Get the PWM INIT register.
void setVal3Value(uint16_t value)
Set the PWM VAL3 register.
bool begin(bool doStart=true, bool doSync=true)
Sets up the PWM signals for a PWM submodule.
void setVALxValue(pwm_value_register_t valueRegister, uint16_t value)
Set the PWM VALx registers.
void enableInterrupts(uint32_t mask)
Enables the selected PWM interrupts.
uint16_t val2Value()
Get the PWM VAL2 register.
uint16_t val0Value()
Get the PWM VAL1 register.
void setDMACaptureSourceSelect(pwm_dma_source_select_t pwm_dma_source_select)
Capture DMA Enable Source Select.
void disableInterrupts(uint32_t mask)
Disables the selected PWM interrupts.
void updateDutyCycle(uint16_t dutyCycle, Channel channel=ChanA)
Updates the PWM signal's dutycycle with 16-bit accuracy.
void setupInputCapture(Channel channel, const pwm_input_capture_param_t *inputCaptureParams)
Sets up the PWM input capture.
Timer & timer()
Returns the parent PWM module.
bool updateSetting(bool doSync=true)
Update PWM signals for a PWM submodule.
void setPrescaler(pwm_clock_prescale_t prescaler)
Set the pwm submodule prescaler.
void setupDutyCyclePercent(uint8_t dutyCyclePercent)
Setting the duty cycle for all channels before calling begin.
uint16_t val3Value()
Get the PWM VAL3 register.
void setVal0Value(uint16_t value)
Set the PWM VAL0 register.
void enableOutput(Channel channel)
Set PWM output enable.
void setupDeadtime(uint16_t deadtimeValue, uint32_t unit=1)
Setting the deadtime for all channels before calling begin.
void setVal5Value(uint16_t value)
Set the PWM VAL5 register.
pwm_fault_state_t faultStateSetting(Channel channel)
value of the parameter set by setupFaultState (Channel, pwm_fault_state_t)
bool setOutputToIdle(Channel channel, bool idleStatus)
Set PWM output in idle status (high or low).
const Config & config() const
Current configuration.
void setupFaultDisableMap(Channel channel, pwm_fault_channels_t pwm_fault_channels, uint16_t value)
Set PWM fault disable mapping.
pwm_level_select_t levelSetting(Channel channel)
value of the parameter set by setupLevel (Channel, pwm_level_select_t)
void setupOutputEnable(Channel channel, bool activate=true)
Setting output enable before calling begin.
uint8_t index() const
Returns the Submodule index (0 for Submodule0...)
void start(bool value=true)
Starts the PWM counter for this submodule.
uint16_t val5Value()
Get the PWM VAL5 register.
uint16_t VALxValue(pwm_value_register_t valueRegister)
Get the PWM VALx registers.
void setVal1Value(uint16_t value)
Set the PWM VAL1 register.
uint32_t statusFlags()
Gets the PWM status flags.
void enableOutputTrigger(uint16_t valueRegisterMask)
Enables the PWM output trigger.
pwm_mode_t pwmMode() const
Current PWM signal frequency in Hz.
void setPwmFaultState(Channel channel, pwm_fault_state_t faultState)
Set PWM output fault status.
bool setPwmMode(pwm_mode_t mode, bool doSync=true)
Set the Pwm operation mode.
void enable(bool value=true)
Enable or disable the submodule.
void disable()
Disable the submodule.
void setVal2Value(uint16_t value)
Set the PWM VAL2 register.
void clearStatusFlags(uint32_t mask)
Clears the PWM status flags.
PWM Module.
Definition: eFlexPwmTimer.h:19
static uint32_t prescalerToMinPwmFrequency(unsigned prescaler)
Calculates the minimum PWM frequency corresponding to a prescaler.
uint32_t srcClockHz() const
PWM main counter clock in Hz.
void start(uint8_t subModulesToStart, bool startit=true)
Starts or stops the PWM counter for a single or multiple submodules.
void setPwmLdok(bool value=true)
Sets or clears the PWM LDOK bit on all instantiated submodules for this timer.
Structure for the user to define the PWM signal characteristics.
pwm_level_select_t level
pwm_fault_state_t faultState
Structure is used to hold parameters to configure the capture capability of a signal pin.