This post shows a very simple stepper motor design, using NXP's Configurator and driver APIs. 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.
This can be helpful to work with the stepper(s) of the Rise Of The Robots - Robotics Design Challenge.

What can this code do:
- control a STEP/DIR motor driver (e.g. A4988 )
- generate a number of steps
- fixed speeds, that you can set at config time, or in code before launching a step command
- set the motor direction
How does it do that:
- DIR is a simple GPIO pin
- STEP is controlled by a timer, in PWM mode.
- an interrupt counts the number of steps and stops the timer when all steps are taken.
Resources:
- PWM timer CTIMER3
- STEP pin 39 set by CTIMER3 PWM output
- DIR pin 38 controlled by GPIO port 4 pin 16.

Configuration
Configure CLOCK

Configure CTIMER

Configure PINs

Code
This example will take 100 steps, change motor direction, and take 100 steps again. Repeat.
#include "fsl_common.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "peripherals.h"
#include "fsl_gpio.h"
#include "fsl_ctimer.h"
#include <stdbool.h>
#include <stdint.h>
volatile uint32_t steps_remaining = 0;
void CTIMER3_PwmCallback(uint32_t flags) {
if (flags & kCTIMER_Match3Flag) {
if (steps_remaining > 0) {
steps_remaining--;
if (steps_remaining == 0) {
CTIMER_StopTimer(CTIMER3_PERIPHERAL);
}
}
}
}
void Stepper_MoveSteps(uint32_t steps) {
if (steps == 0) return;
steps_remaining = steps;
CTIMER_Reset(CTIMER3_PERIPHERAL);
CTIMER_StartTimer(CTIMER3_PERIPHERAL);
}
int main(void) {
BOARD_InitPins();
BOARD_BootClockRUN();
BOARD_InitBootPeripherals();
while (1) {
GPIO_PortToggle(BOARD_INITPINS_DIR_PIN_GPIO, 1U << BOARD_INITPINS_DIR_PIN_GPIO_PIN);
Stepper_MoveSteps(100);
while (steps_remaining > 0) {
__NOP();
}
for (uint32_t i = 0; i < 1000000U; i++) {
__NOP();
}
}
}
If you use an NXP dev board - like in the Rise Of The Robots Design Challenge - you may not have to call the individual init functions. If you enable initialisation in the configurator, the individual setup functions will be called in your board init code.
Thank you for reading