So using the MANTA M5P Board and using STM32CubeIDE to code the custom firmware as the manta as the stm32 mcu. And the following is a summary and main snippets of my code that I added onto the ide after generating the main.c from CubeMX:
- Initialization
- HAL & clocks: Calls
HAL_Init() and SystemClock_Config() to get the MCU up and running.
- Peripherals:
- GPIO (
MX_GPIO_Init):
- EN (PA15) as push-pull output → drives the TMC5160’s enable pin low to turn the driver on.
- DIR (PC9) and STEP (PC8) as outputs for step/dir signaling.
- LED (PD2) as output for visual feedback.
- DIAG1 (PD3) as a pull-up input → reads the driver’s open-drain stall signal.
- SPI2 (
MX_SPI2_Init): Configured as a 4-wire master to talk to the TMC5160’s SPI interface.
- TIM3 (
MX_TIM3_Init): Used as a micro-delay timer for generating precise STEP pulse widths.
- Driver Configuration via SPI2
In main(), right after HAL_TIM_Base_Start(), I send a handful of 5-byte datagrams down SPI2 to the TMC5160:
Then I write the following datagrams to the following tmc driver registers:
TMC5160_WriteReg(0x00, (1U<<8)); // GCONF: bit-8 → DIAG1 goes low on StallGuard2 stall
TMC5160_WriteReg(0x10, 0x08140A); // IHOLD_IRUN: IHOLD=8, IRUN=20, IHOLDDELAY=10
TMC5160_WriteReg(0x11, 0x0000000A); // TPOWERDOWN
TMC5160_WriteReg(0x14, 0x00000000); // TCOOLTHRS=0 → StallGuard2 always active
TMC5160_WriteReg(0x6C, 0x04814153); // CHOPCONF: SpreadCycle, 16 µstep, TOFF=3, TBL=24
TMC5160_WriteReg(0x6D, 0x00440000); // COOLCONF: SGT=–60, SFILT=0
SPI2 is used only to write to these registers (no reads or other data transfers).
These register settings together turn on sensorless StallGuard2, tune its sensitivity, and set my motor currents & microstep mode.
- Stepping Loop & Stall Detection
while (1) {
// 1) Poll DIAG1 (PD3). If it’s pulled LOW by the driver, we’ve hit a stall:
if (HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_3) == GPIO_PIN_RESET) {
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET); // turn off LED
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET); // disable driver
break;
}
// 2) Otherwise, generate one STEP pulse:
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_SET);
microDelay(100);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_RESET);
microDelay(100);
// 3) LED ON while spinning (no stall)
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
}
EN (PA15) = LOW → driver enabled; when a stall is detected, I drive it HIGH to cut power.
DIR (PC9) is set once at startup and never changes, so the motor runs in one direction.
STEP (PC8) pulses at 5 kHz (100 µs high, 100 µs low) for continuous rotation.
PD3 is sampled each pass; when the TMC5160’s StallGuard2 logic pulls DIAG1 low, I stop the loop.
The pin out configuration for my mcu is as follows:
It would be greatly appreciated if you could see where I may be going wrong. After so much troubleshooting and changing the parameters, I have yet to stall my motor. I’ve just been holding down a plier on the shaft as it rotates no matter how much I press, it just doesn’t turn off. I do also see the current slightly increase but nothing too crazy but the stepper warms up as I hold it for quite a while.