| @@ -0,0 +1,201 @@ | |||
| 1 | + | #pragma once | |
| 2 | + | ||
| 3 | + | #include "MCXN947_cm33_core0.h" | |
| 4 | + | #include <algorithm> | |
| 5 | + | #include <cstdint> | |
| 6 | + | ||
| 7 | + | #include "fsl_common.h" | |
| 8 | + | #include "fsl_port.h" | |
| 9 | + | #include "pin_mux.h" | |
| 10 | + | ||
| 11 | + | enum class PWM_SubModule : uint8_t { SM_0 = 0, SM_1 = 1, SM_2 = 2, SM_3 = 3 }; | |
| 12 | + | ||
| 13 | + | enum class PWM_Status : uint8_t { OK, Error }; | |
| 14 | + | void compile_error_min_must_be_less_than_max(); | |
| 15 | + | void compile_error_neutral_out_of_bounds(); | |
| 16 | + | void compile_error_bus_clk_cannot_be_zero(); | |
| 17 | + | // --- Compile-Time Configuration Structure --- | |
| 18 | + | struct FlexPWMConfig { | |
| 19 | + | PWM_SubModule submodule; | |
| 20 | + | uint32_t min_us; | |
| 21 | + | uint32_t neutral_us; | |
| 22 | + | uint32_t max_us; | |
| 23 | + | uint32_t bus_clk_hz; | |
| 24 | + | ||
| 25 | + | consteval FlexPWMConfig(PWM_SubModule sm, uint32_t min_u, uint32_t neutral_u, | |
| 26 | + | uint32_t max_u, uint32_t clk_hz = 150'000'000U) | |
| 27 | + | : submodule(sm), min_us(min_u), neutral_us(neutral_u), max_us(max_u), | |
| 28 | + | bus_clk_hz(clk_hz) { | |
| 29 | + | if (min_us >= max_us) { | |
| 30 | + | compile_error_min_must_be_less_than_max(); | |
| 31 | + | } | |
| 32 | + | if (neutral_us < min_us || neutral_us > max_us) { | |
| 33 | + | compile_error_neutral_out_of_bounds(); | |
| 34 | + | } | |
| 35 | + | if (bus_clk_hz == 0) { | |
| 36 | + | compile_error_bus_clk_cannot_be_zero(); | |
| 37 | + | } | |
| 38 | + | } | |
| 39 | + | // Helper to extract the associated PWM_A pin number for a submodule | |
| 40 | + | [[nodiscard]] consteval uint32_t get_pwm_a_pin() const { | |
| 41 | + | switch (submodule) { | |
| 42 | + | case PWM_SubModule::SM_0: | |
| 43 | + | return 6U; // PORT2_6 -> PWM1_A0 | |
| 44 | + | case PWM_SubModule::SM_1: | |
| 45 | + | return 4U; // PORT2_4 -> PWM1_A1 | |
| 46 | + | case PWM_SubModule::SM_2: | |
| 47 | + | return 2U; // PORT2_2 -> PWM1_A2 | |
| 48 | + | case PWM_SubModule::SM_3: | |
| 49 | + | return 0U; // PORT2_0 -> PWM1_A3 | |
| 50 | + | } | |
| 51 | + | return 0U; | |
| 52 | + | } | |
| 53 | + | ||
| 54 | + | // Pre-calculated constant tick conversion factor (Bus Clock / Prescaler 128 / | |
| 55 | + | // 1,000,000) | |
| 56 | + | [[nodiscard]] consteval uint64_t get_ticks_multiplier() const { | |
| 57 | + | return (bus_clk_hz / 128U); | |
| 58 | + | } | |
| 59 | + | ||
| 60 | + | [[nodiscard]] consteval uint16_t us_to_ticks(uint32_t us) const { | |
| 61 | + | uint64_t ticks = | |
| 62 | + | (static_cast<uint64_t>(us) * get_ticks_multiplier()) / 1'000'000U; | |
| 63 | + | return static_cast<uint16_t>(ticks); | |
| 64 | + | } | |
| 65 | + | ||
| 66 | + | [[nodiscard]] consteval uint16_t calculate_modulo_50hz() const { | |
| 67 | + | return us_to_ticks(20'000U); // 50 Hz period = 20,000 us | |
| 68 | + | } | |
| 69 | + | }; | |
| 70 | + | ||
| 71 | + | // --- Template-Optimized FlexPWM Driver Class --- | |
| 72 | + | template <const FlexPWMConfig &Cfg> class FlexPWM1Channel { | |
| 73 | + | public: | |
| 74 | + | constexpr FlexPWM1Channel() = default; | |
| 75 | + | ||
| 76 | + | // Hardware Initialization (Compile-time optimized register configuration) | |
| 77 | + | void init_hardware() noexcept { | |
| 78 | + | constexpr uint8_t sm_idx = static_cast<uint8_t>(Cfg.submodule); | |
| 79 | + | constexpr uint32_t pin_num = Cfg.get_pwm_a_pin(); | |
| 80 | + | ||
| 81 | + | // 1. Enable PORT2 and FlexPWM1 Clocks via NXP SDK | |
| 82 | + | CLOCK_EnableClock(kCLOCK_Port2); | |
| 83 | + | CLOCK_EnableClock(kCLOCK_Pwm1); | |
| 84 | + | ||
| 85 | + | // 2. Configure Pin Muxing using exact port_pin_config_t field layout | |
| 86 | + | port_pin_config_t pwm_pin_config = {}; | |
| 87 | + | ||
| 88 | + | pwm_pin_config.pullSelect = kPORT_PullDisable; | |
| 89 | + | pwm_pin_config.pullValueSelect = kPORT_LowPullResistor; | |
| 90 | + | pwm_pin_config.slewRate = kPORT_FastSlewRate; | |
| 91 | + | pwm_pin_config.passiveFilterEnable = kPORT_PassiveFilterDisable; | |
| 92 | + | pwm_pin_config.openDrainEnable = kPORT_OpenDrainDisable; | |
| 93 | + | pwm_pin_config.driveStrength = kPORT_LowDriveStrength; | |
| 94 | + | pwm_pin_config.mux = kPORT_MuxAlt5; // MuxAlt5 = FlexPWM1 | |
| 95 | + | pwm_pin_config.inputBuffer = kPORT_InputBufferEnable; | |
| 96 | + | pwm_pin_config.invertInput = kPORT_InputNormal; | |
| 97 | + | pwm_pin_config.lockRegister = kPORT_UnlockRegister; | |
| 98 | + | ||
| 99 | + | PORT_SetPinConfig(PORT2, pin_num, &pwm_pin_config); | |
| 100 | + | ||
| 101 | + | // 3. Submodule-specific clock gate enabling | |
| 102 | + | if constexpr (Cfg.submodule == PWM_SubModule::SM_0) { | |
| 103 | + | SYSCON->PWM1SUBCTL |= SYSCON_PWM1SUBCTL_CLK0_EN(0b1); | |
| 104 | + | } else if constexpr (Cfg.submodule == PWM_SubModule::SM_1) { | |
| 105 | + | SYSCON->PWM1SUBCTL |= SYSCON_PWM1SUBCTL_CLK1_EN(0b1); | |
| 106 | + | } else if constexpr (Cfg.submodule == PWM_SubModule::SM_2) { | |
| 107 | + | SYSCON->PWM1SUBCTL |= SYSCON_PWM1SUBCTL_CLK2_EN(0b1); | |
| 108 | + | } else if constexpr (Cfg.submodule == PWM_SubModule::SM_3) { | |
| 109 | + | SYSCON->PWM1SUBCTL |= SYSCON_PWM1SUBCTL_CLK3_EN(0b1); | |
| 110 | + | } | |
| 111 | + | ||
| 112 | + | // 4. Configure FlexPWM Submodule Hardware Registers | |
| 113 | + | PWM1->MCTRL &= ~PWM_MCTRL_RUN(1U << sm_idx); | |
| 114 | + | ||
| 115 | + | PWM1->SM[sm_idx].CTRL2 = PWM_CTRL2_INDEP(0b1) | PWM_CTRL2_DBGEN(0b1) | | |
| 116 | + | PWM_CTRL2_INIT_SEL(0b0) | | |
| 117 | + | PWM_CTRL2_RELOAD_SEL(0b0) | PWM_CTRL2_CLK_SEL(0); | |
| 118 | + | ||
| 119 | + | PWM1->SM[sm_idx].CTRL |= PWM_CTRL_FULL(0b1) | PWM_CTRL_PRSC(0b111); | |
| 120 | + | ||
| 121 | + | PWM1->SM[sm_idx].INIT = 0; | |
| 122 | + | PWM1->SM[sm_idx].VAL0 = 0; | |
| 123 | + | PWM1->SM[sm_idx].VAL1 = Cfg.calculate_modulo_50hz() - 1U; | |
| 124 | + | PWM1->SM[sm_idx].VAL2 = 0; | |
| 125 | + | PWM1->SM[sm_idx].VAL3 = Cfg.us_to_ticks(Cfg.neutral_us); | |
| 126 | + | PWM1->SM[sm_idx].VAL4 = 0; | |
| 127 | + | PWM1->SM[sm_idx].VAL5 = 0; | |
| 128 | + | ||
| 129 | + | PWM1->SM[sm_idx].DISMAP[0] = 0x0000; | |
| 130 | + | ||
| 131 | + | PWM1->MCTRL |= PWM_MCTRL_LDOK(1U << sm_idx); | |
| 132 | + | PWM1->OUTEN |= PWM_OUTEN_PWMA_EN(1U << sm_idx); | |
| 133 | + | PWM1->MCTRL |= PWM_MCTRL_RUN(1U << sm_idx); | |
| 134 | + | ||
| 135 | + | initialized_ = true; | |
| 136 | + | } | |
| 137 | + | ||
| 138 | + | void set_pulse_us(uint32_t us) noexcept { | |
| 139 | + | if (!initialized_) | |
| 140 | + | return; | |
| 141 | + | ||
| 142 | + | constexpr uint8_t sm_idx = static_cast<uint8_t>(Cfg.submodule); | |
| 143 | + | PWM1->SM[sm_idx].VAL3 = clamp_pulse_ticks(us); | |
| 144 | + | PWM1->MCTRL |= PWM_MCTRL_LDOK(1U << sm_idx); | |
| 145 | + | } | |
| 146 | + | ||
| 147 | + | void set_neutral() noexcept { | |
| 148 | + | if (!initialized_) | |
| 149 | + | return; | |
| 150 | + | ||
| 151 | + | constexpr uint8_t sm_idx = static_cast<uint8_t>(Cfg.submodule); | |
| 152 | + | PWM1->SM[sm_idx].VAL3 = Cfg.us_to_ticks(Cfg.neutral_us); | |
| 153 | + | PWM1->MCTRL |= PWM_MCTRL_LDOK(1U << sm_idx); | |
| 154 | + | } | |
| 155 | + | ||
| 156 | + | private: | |
| 157 | + | bool initialized_{false}; | |
| 158 | + | [[nodiscard]] static constexpr uint16_t | |
| 159 | + | clamp_pulse_ticks(uint32_t us) noexcept { | |
| 160 | + | // Limits clamped using immediate constants burned into instructions | |
| 161 | + | uint32_t clamped_us = std::clamp(us, Cfg.min_us, Cfg.max_us); | |
| 162 | + | ||
| 163 | + | // Single 32-bit hardware multiply (MUL) + bit-shift right (LSR #6) | |
| 164 | + | return static_cast<uint16_t>((clamped_us * 75U) >> 6); | |
| 165 | + | } | |
| 166 | + | }; | |
| 167 | + | ||
| 168 | + | ||
| 169 | + | ||
| 170 | + | //main.cpp | |
| 171 | + | #include "pwm.hpp" | |
| 172 | + | ||
| 173 | + | // --- Instantiate Configurations --- | |
| 174 | + | constexpr FlexPWMConfig SERVO_CFG(PWM_SubModule::SM_0, 1000U, 1500U, 2000U, | |
| 175 | + | 150'000'000U); | |
| 176 | + | constexpr FlexPWMConfig ESC_CFG(PWM_SubModule::SM_1, 1000U, 1000U, 2000U, | |
| 177 | + | 150'000'000U); | |
| 178 | + | ||
| 179 | + | // Driver Types linked directly to compile-time configuration references | |
| 180 | + | using ServoChannel = FlexPWM1Channel<SERVO_CFG>; | |
| 181 | + | using MotorChannel = FlexPWM1Channel<ESC_CFG>; | |
| 182 | + | ||
| 183 | + | // Instance allocation | |
| 184 | + | inline ServoChannel g_steering_servo; | |
| 185 | + | inline MotorChannel g_drive_motor; | |
| 186 | + | ||
| 187 | + | ||
| 188 | + | int main(void) { | |
| 189 | + | //... | |
| 190 | + | g_steering_servo.init_hardware(); | |
| 191 | + | g_drive_motor.init_hardware(); | |
| 192 | + | ||
| 193 | + | ||
| 194 | + | //... | |
| 195 | + | g_steering_servo.set_pulse_us(1000); | |
| 196 | + | g_drive_motor.set_pulse_us(2000); | |
| 197 | + | ||
| 198 | + | while (1U) { | |
| 199 | + | // Intentionally left blank | |
| 200 | + | } | |
| 201 | + | } | |
Sjoerd / PWM as cpp template stuff
Last active 1 month ago