eFlexPwm
Arduino eFlexPwm library for Teensy 4.x
eFlexPwmTimer.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 
11 namespace eFlex {
12 
19  class Timer {
20 
21  public:
37  bool beginSubModules (uint8_t subModulesToBegin, bool doStart = true, bool doSync = true);
38 
52  inline bool begin (bool doStart = true, bool doSync = true) {
53  return beginSubModules (SmMask[m_tmidx], doStart, doSync);
54  }
55 
59  inline uint8_t index() const {
60  return m_tmidx;
61  }
62 
72  void enable (bool value = true);
73 
81  inline void disable () {
82  enable (false);
83  }
84 
88  inline bool isEnabled() const {
89  return m_isenabled;
90  }
91 
102  inline void start (uint8_t subModulesToStart, bool startit = true) {
103  if (startit) {
104  PWM_StartTimer (ptr(), subModulesToStart);
105  }
106  else {
107  PWM_StopTimer (ptr(), subModulesToStart);
108  }
109  }
110 
117  inline void start (bool startit = true) {
118  start (SmMask[m_tmidx], startit);
119  }
120 
130  inline void stop (uint8_t subModulesToStop) {
131  start (subModulesToStop, false);
132  }
133 
137  inline void stop () {
138  stop (false);
139  }
140 
147  void setupDutyCyclePercent (uint8_t dutyCyclePercent);
148 
156 
165  void setupDeadtime (uint16_t deadtimeValue, uint32_t unit = 1);
166 
173  void setupOutputEnable (bool activate = true);
174 
183 
184 
198  bool updateSetting (bool doSync = true);
199 
203  static inline uint32_t busClockHz() {
204  return F_BUS_ACTUAL;
205  }
206 
210  inline uint32_t srcClockHz() const {
211  return busClockHz();
212  }
213 
221  void printRegs (Stream &out = Serial) const;
222 
228  void printAllRegs (Stream &out = Serial) const;
229 
237  inline void setPwmLdok (bool value = true) {
238  setPwmLdok (SmMask[m_tmidx], value);
239  }
240 
241  //-----------------------------------------------------------------------
242  // NXP SDK WRAPPER
243  //-----------------------------------------------------------------------
244 
257  inline void setPwmLdok (uint8_t subModulesToUpdate, bool value) {
258  PWM_SetPwmLdok (ptr(), subModulesToUpdate, value);
259  }
260 
266  inline void setupFaultInputFilter (const pwm_fault_input_filter_param_t *faultInputFilterParams) {
267  PWM_SetupFaultInputFilter (ptr(), faultInputFilterParams);
268  }
269 
278  inline void setupFaults (pwm_fault_input_t faultNum, const pwm_fault_param_t *faultParams) {
279  PWM_SetupFaults (ptr(), faultNum, faultParams);
280  }
281 
288  static inline uint32_t prescalerToMinPwmFrequency (unsigned prescaler) {
289  return (busClockHz() / (static_cast<uint32_t> (prescaler & 0x7F) * 65535UL) + 1);
290  }
291 
295  static inline uint32_t prescalerToMinPwmFrequency (pwm_clock_prescale_t prescaler) {
296  return prescalerToMinPwmFrequency (1U << prescaler);
297  }
298 
299  protected:
305  Timer (uint8_t index);
306  inline PWM_Type *ptr() {
307  return m_ptr;
308  }
309  inline const PWM_Type *ptr() const {
310  return m_ptr;
311  }
312 
313  private:
314  uint8_t m_tmidx;
315  PWM_Type *m_ptr;
316  bool m_isenabled;
317  };
318  extern Timer *TM[NofTimers];
319 }
320 
enum _pwm_clock_prescale pwm_clock_prescale_t
PWM prescaler factor selection for clock source.
enum _pwm_fault_state pwm_fault_state_t
PWM output fault status.
enum _pwm_fault_input pwm_fault_input_t
List of PWM fault selections.
enum _pwm_level_select pwm_level_select_t
PWM output pulse mode, high-true or low-true.
Library namespace.
PWM Module.
Definition: eFlexPwmTimer.h:19
void disable()
Disable all instantiated submodules of the timer.
Definition: eFlexPwmTimer.h:81
uint8_t index() const
Returns the timer module index (0 for PWM1...)
Definition: eFlexPwmTimer.h:59
static uint32_t busClockHz()
Bus clock in Hz.
void start(bool startit=true)
Starts or stops the PWM counter for all instantiated submodules for this timer.
void printRegs(Stream &out=Serial) const
Print PWM module registers to the output stream.
void setupFaults(pwm_fault_input_t faultNum, const pwm_fault_param_t *faultParams)
Sets up the PWM fault protection.
bool updateSetting(bool doSync=true)
Update PWM signals for all submodules.
void stop()
Stops the PWM counter for all instantiated submodules for this timer.
static uint32_t prescalerToMinPwmFrequency(unsigned prescaler)
Calculates the minimum PWM frequency corresponding to a prescaler.
void stop(uint8_t subModulesToStop)
Stops the PWM counter for a single or multiple submodules.
void setupFaultInputFilter(const pwm_fault_input_filter_param_t *faultInputFilterParams)
Sets up the PWM fault input filter.
void enable(bool value=true)
Enable or disable all instantiated submodules of the timer.
uint32_t srcClockHz() const
PWM main counter clock in Hz.
void setupDutyCyclePercent(uint8_t dutyCyclePercent)
Setting the duty cycle for all submodules before calling begin.
bool begin(bool doStart=true, bool doSync=true)
Sets up the PWM signals for all instantiated submodules of the timer.
Definition: eFlexPwmTimer.h:52
void start(uint8_t subModulesToStart, bool startit=true)
Starts or stops the PWM counter for a single or multiple submodules.
bool beginSubModules(uint8_t subModulesToBegin, bool doStart=true, bool doSync=true)
Sets up the PWM signals for the sub-modules passed as parameters.
void setPwmLdok(uint8_t subModulesToUpdate, bool value)
Sets or clears the PWM LDOK bit on a single or multiple submodules.
void setupDeadtime(uint16_t deadtimeValue, uint32_t unit=1)
Setting the deadtime for all submodules before calling begin.
void printAllRegs(Stream &out=Serial) const
Print registers of PWM module and its submodules to the output stream.
bool isEnabled() const
Returns true if the timer is enabled.
Definition: eFlexPwmTimer.h:88
void setPwmLdok(bool value=true)
Sets or clears the PWM LDOK bit on all instantiated submodules for this timer.
void setupFaultState(pwm_fault_state_t faultState)
Setting the output fault status for all submodules before calling begin.
void setupOutputEnable(bool activate=true)
Setting output enable for all submodules before calling begin.
void setupLevel(pwm_level_select_t level)
Setting the output pulse mode for all submodules before calling begin.
Structure for the user to configure the fault input filter.
Structure is used to hold the parameters to configure a PWM fault.