NXP also provides an Eclipse based IDE, MCUxpresso 10.2.1 as well as an SDK, 2.4.1. The SDK provides an API for configuring and using peripherals, like the FTM, PIT and DMA. The 2.4.1 SDK will be used in code examples here. Downloading the SDK does require registering at the NXP website (free, as in beer) and requesting a specific Kinetis hardware configuration, either an evaluation board or a specific MCU. The blog uses the SDK API in code examples.
A stepper motor is an industrial standard motor that provides the ability to move a step with just an single digital pin. The physical motion is more complicated, but from the view of the MCU a stepper motor is moved by asserting and de-asserting an output pin.
The STEP signal rotates the stepper motion is fixed angle, typically 1/200th of a revolution. Attaching the shaft of the motor to something that moves in a linear fashion results in the linear or positional motion.
1 - Set ENABLE low.
2 - Set DIR CW or CCW
3 - Assert STEP
4 - Wait a specific time
5 - De-assert STEP
6 - Wait a specific time
7 - Repeat 4-7 until all STEPs have been asserted.
MCU Intensive Stepper Motion
Programmable Interrupt Timer
Enable the PIT interrupt.
If pluse_state is assert
Determine if motion is done.
If motion is not done
Flexible Timer Module
FTM Clock and Timer
The timer has several counting modes. The mode selected for STEP output is the count up timer.
QUADEN = 0 and CPWMS = 0 sets the count up mode.
The timer can have a tick interval the same as the Kinetis bus clock, or any power of 2 division up to 128. PS sets the prescaler value. [Note: Kinetis documentation uses the term system clock. It should say bus clock.]
The FTM timer is set to be a free running timer, never stopping when FTMEN=0 and MOD=0xFFFF.
In summary, to set a 16-bit count-up free running timer, CLKS=01, FTMEN=0, QUADEN=0, CPWMS=0, CNTIN=0, and MOD=0xFFFF.
FTM InterruptThe FTM has one interrupt for any event. Which FTM event occurred has to be determined by examining status registers. Each channel has an interrupt enable bit and an interrupt status bit. Other interrupts, such as faults can be configured.
It is not appropriate to set the TOF overflow interrupt bit with the free running clock as it will constantly overflow. It could be used as a low speed interrupt.
Output Compare Value
The output compare value is not the step time interval. It is the last output compare value plus the step time interval. The output compare event occurs when the output compare value and FTM COUNT are equal.
The FTM clock is a free running clock, not a countdown clock. The compare value is absolute FTM clock time to assert or deassert the STEP pin.
FTM and Assert and Deassert STEP Pin
FTM and Motion
Determine if motion is done
Set FTM channel CnV output compare to 1/2 the time interval.
[Link to next blog.]