eFlexPwm
Arduino eFlexPwm library for Teensy 4.x
eFlexPwmGlobal.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 <nxp/drivers/fsl_pwm.h>
10 
11 #define EFLEXPWM_DUMPREG_ENABLED 1
12 
14 namespace eFlex {
15 
19  enum Channel {
20  ChanA = 0,
21  ChanB = 1
22  };
23 
32  inline uint16_t u16TwoCompl (uint16_t value) {
33  return (~value + 1U);
34  }
35 
42  inline uint16_t reloadValue (uint8_t dutyCyclePercent) {
43  /* Rounding calculations to improve the accuracy of reloadValue */
44  return ( (65535U * dutyCyclePercent) + 50U) / 100U;
45  }
46 
47  #ifndef DOXYGEN
48  // Constants
49  // ----------------------------------------------------------------------------
50  const uint8_t NofTimers = 4;
51  const uint8_t NofSubmodules = FSL_FEATURE_PWM_SUBMODULE_COUNT;
52  const uint8_t NofPins = PWM_SUBMODULE_CHANNEL;
53 
54  enum TimerIndex {
55  Pwm1 = 0,
56  Pwm2,
57  Pwm3,
58  Pwm4
59  };
60 
61  enum TimerFlag {
62  Pwm1Flag = (1 << Pwm1),
63  Pwm2Flag = (1 << Pwm2),
64  Pwm3Flag = (1 << Pwm3),
65  Pwm4Flag = (1 << Pwm4),
66  AllTimersFlag = (Pwm1Flag | Pwm2Flag | Pwm3Flag | Pwm4Flag)
67  };
68 
69  enum SubmoduleIndex {
70  Submodule1 = 0,
71  Submodule2,
72  Submodule3,
73  Submodule4
74  };
75 
76  enum SubmoduleFlag {
77  Submodule1Flag = (1 << Submodule1),
78  Submodule2Flag = (1 << Submodule2),
79  Submodule3Flag = (1 << Submodule3),
80  Submodule4Flag = (1 << Submodule4),
81  AllSubmodulesFlag = (Submodule1Flag | Submodule2Flag | Submodule3Flag | Submodule4Flag)
82  };
83 
84  const pwm_submodule_t SM[NofSubmodules] = {
89  };
90 
91  // Variables
92  // ----------------------------------------------------------------------------
93  extern PWM_Type *PWM[NofTimers];
94  extern uint8_t SmMask[NofTimers];
95 
96  // Code
97  // ----------------------------------------------------------------------------
98 
99  // ----------------------------------------------------------------------------
100  inline pwm_channels_t kPwmChan (Channel c) {
101 
102  return (c == ChanA ? kPWM_PwmA : kPWM_PwmB);
103  }
104  #endif
105 }
enum _pwm_channels pwm_channels_t
List of PWM channels in each module.
enum _pwm_submodule pwm_submodule_t
List of PWM submodules.
@ kPWM_Module_2
@ kPWM_Module_3
@ kPWM_Module_1
@ kPWM_Module_0
Library namespace.
uint16_t reloadValue(uint8_t dutyCyclePercent)
Convert duty cycle to reload value.
Channel
PWM Channel.
uint16_t u16TwoCompl(uint16_t value)
Complement the variable of type uint16_t as needed.