This post shows a very simple C++ stepper motor design. It can control the direction and the number of steps to take.
It's a fixed speed design. I'm testing it on shabaz' EZ-MCXN dev board.
In this post I create the object oriented version of EZ-MCXN: NXP MCXN326 simple stepper motor controller . A preparation for a fully contained s-shaped up/down-ramp design.
Main code example (using a stepper motor object:
// ...
import mcx_pwm_stepper_driver;
// object to manage the stepper motor step and dir
using pwm_stepper_driver_t = mcx_pwm_stepper_driver::mcx_pwm_stepper_driver;
pwm_stepper_driver_t pwm_stepper_driver1(BOARD_INITPINS_DIR_PIN_GPIO_PIN, BOARD_INITPINS_DIR_PIN_GPIO, CTIMER3_PERIPHERAL, CTIMER3_PWM_PERIOD_CH, CTIMER3_PWM_0_CHANNEL);
int main(void) {
// ...
pwm_stepper_driver1.init();
pwm_stepper_driver1.set_control_mode(mcx_pwm_stepper_driver::control_mode::step);
pwm_stepper_driver1.set_speed(100);
while (1) {
pwm_stepper_driver1.set_dir(true);
SDK_DelayAtLeastUs(200000U, SystemCoreClock);
pwm_stepper_driver1.set_step(50);
pwm_stepper_driver1.start_motor();
while (pwm_stepper_driver1.is_motor_running()) {
__WFI(); // Enter low-power sleep mode; wake on timer interrupts
}
SDK_DelayAtLeastUs(5000U, SystemCoreClock);
}
}
extern "C" {
void CTIMER3_PwmCallback(uint32_t flags) {
if (flags & kCTIMER_Match3Flag) {
pwm_stepper_driver1.check_step();
}
}
} // extern "C"
Stepper motor class:
enum class control_mode {
notset, // avoid that an initialised but unset variable assumes the first valid value
step,
spin
};
class mcx_pwm_stepper_driver {
public:
mcx_pwm_stepper_driver(uint32_t dir, GPIO_Type *gpio, CTIMER_Type *timer, ctimer_match_t period_channel, ctimer_match_t pwm_channel) :
dir(dir), gpio(gpio), timer(timer), period_channel(period_channel), pwm_channel(pwm_channel) {}
void init() {
}
void set_dir(bool fwd) {
put(dir, fwd);
}
void set_control_mode(control_mode control_mode) {
this->control_mode_ = control_mode;
}
void set_speed(uint32_t tim_freq) {
set_freq = tim_freq;
CTIMER_SetupPwm(timer,
period_channel,
pwm_channel,
50U, // 50% duty cycle
tim_freq, // new pwm frequency in Hz
CLOCK_GetCTimerClkFreq(get_timer_instance(timer)), // CTimer functional clock in Hz
false); // Disable interrupt // TODO: check later if we may use the PWM interrupt
}
void check_step() {
if(control_mode_ == control_mode::step) {
step_count += 1;
if(step_count >= set_step_) { // If the step count exceeds the set steps, stop the motor
stop_motor();
step_count = 0;
}
}
}
void start_motor() {
CTIMER_Reset(timer);
CTIMER_StartTimer(timer);
}
void stop_motor() {
CTIMER_StopTimer(timer);
}
void set_step(uint32_t set_steps) {
set_step_ = set_steps;
}
bool is_motor_running() {
return ((timer->TCR & CTIMER_TCR_CEN_MASK) != 0U);
}
protected:
uint32_t dir;
GPIO_Type *gpio;
CTIMER_Type *timer;
control_mode control_mode_ = control_mode::step;
uint32_t set_freq = 0;
volatile uint32_t step_count = 0;
uint32_t set_step_ = 0;
ctimer_match_t period_channel;
ctimer_match_t pwm_channel;
void put(uint32_t pin, bool set) {
GPIO_PinWrite(gpio, pin, set ? pin : 0U);
}
private:
static constexpr uint32_t get_timer_instance(const CTIMER_Type* base) noexcept {
// Pure integer array — 100% legal in constexpr C++
constexpr uintptr_t addrs[] = CTIMER_BASE_ADDRS;
const uintptr_t target = MSDK_REG_NONSECURE_ADDR(base);
for (uint32_t i = 0; i < (sizeof(addrs) / sizeof(addrs[0])); ++i) {
if (MSDK_REG_NONSECURE_ADDR(addrs[i]) == target) {
return i;
}
}
return 0U;
}
};
The main code doesn't need to maintain status of the motor anymore.
Functionality added, that wasn't implemented in the C version: you can change the step speed.
If you don't set the speed, the design will use the one set in the configurator. Once you call it, the motor will step at the new frequency.
You can control multiple motors, each with their own object. As many as there are PWM able CTIMER peripherals.
The speed setting mechanism is essential for the smooth ramp support that I want to add. I'll have to switch to a mechanism that changes speed while the timer is running.
Thank you for reading