element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products & Partners
    Products & Partners
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Embedded and Microcontrollers
  • Technologies
  • More
Embedded and Microcontrollers
Blog EZ-MCXN: NXP MCXN326 C++ simple stepper motor controller
  • Blog
  • Forum
  • Documents
  • Quiz
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Embedded and Microcontrollers to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 19 Nov 2026 9:51 PM Date Created
  • Views 16 views
  • Likes 2 likes
  • Comments 0 comments
  • EZ-MCXN
  • MCXN
  • nxp
  • MCXN236
Related
Recommended

EZ-MCXN: NXP MCXN326 C++ simple stepper motor controller

Jan Cumps
Jan Cumps
19 Nov 2026

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.

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

Thank you for reading

Related posts.

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube