Grafito CANStepper

For anyone writing firmware for the board before production firmware ships. Everything here was verified on hardware rev V9 during bring-up. Images are linked for flash address 0x08000000 and loaded as described in Programming & flashing.

Rule 1: enable the instruction cache first

Required

Write 1 to ICACHE_CR (0x40030400) as the first instruction after reset, before any other code.

Without it, tight loops running from flash lock the chip up: the firmware freezes, LEDs stay stuck, and the core is in lockup with a double-bit ECC error reported on the flash line holding the loop. The error moved with the code, so it is not bad flash cells, and adding flash wait states did not help. Enabling the instruction cache cures it. The root cause is not fully understood yet, so treat this as mandatory.

Pin map

From the rev V9 schematic. Package pin numbers are for the LQFP-48.

FunctionMCU pinPkg pinNotes
VIN sensePA010Supply voltage through a resistor divider (not used yet)
TMC5160 CSNPA414Pull-up to 3.3 V
TMC5160 SCK / MISO / MOSIPA5 / PA6 / PA715 / 16 / 17SPI mode 3, 40-bit datagrams
TMC5160 DRV_ENNPB018Pull-up: driver stays off until firmware drives it low
Encoder CSNPB119MT6701 Z pin in SSI mode; pull-up to 3.3 V
CAN TXPB1021FDCAN1, AF9
CAN RXPB1225FDCAN1, AF9
Encoder CLKPB1326MT6701 B pin
Encoder DOPB1427MT6701 A pin, through a series resistor. Data valid after CLK rising edge.
ERR LEDPB1528Red LED, active high
USB D− / D+PA11 / PA1232 / 33USB-C, data only
SWDIO / SWCLKPA13 / PA1434 / 37Pads DIO, CLK, GND, NRST, 3V3
Limit 1 (LS3V3)PB44010 kΩ pull-up; reads high when open, low when active
Limit 2 (NPN)PB541Schottky-isolated, 10 kΩ pull-up; low when the NPN sensor switches on
TMC5160 DIAG0PB642Pull-up to 3.3 V; also on CN5 pin 4 (not used yet)
TMC5160 DIAG1PB743Pull-up to 3.3 V (not used yet)
BOOT0—44Pull-down; BOOT button pulls it to 3.3 V
NRST—7RESET button to GND, 100 nF
HSEPH0 / PH15 / 624 MHz crystal, 15 pF load capacitors

Not connected: PA1–PA3, PA8–PA10, PA15, PB2, PB3, PB8, PC13, PC15. VBAT is tied to 3.3 V.

TMC5160 wiring that firmware must respect

Driver pinWired toWhat it means
SPI_MODE3.3 VSPI is always on. The driver needs SPI setup in both modes.
SD_MODESDMODE switch (pull-down)Switch on = external STEP/DIR (Mode A); off = internal ramp generator (Mode B). Not wired to the MCU: read it from IOIN bit 6 over SPI.
DRV_ENNMCU PB0, pull-upDriver disabled at power-up and while the MCU is in reset
REFL_STEP / REFR_DIRCN5 pins 2 / 3, pull-downsSTEP/DIR in Mode A; left/right reference switches in Mode B. Not wired to the MCU.
DIAG0 / DIAG1MCU PB6 / PB7, pull-ups; DIAG0 also on CN5Open-drain by default (active low)
CLKGNDInternal clock, about 12 MHz
ENCA / ENCB / ENCNGNDThe driver's own encoder interface is unused; the MT6701 is read by the MCU
VCC_IO3.3 V3.3 V logic on SPI, STEP/DIR and DIAG
VSA, 12VOUTExternal 12 V rail (LM5164)Gate-drive supply comes from the 12 V buck, not the driver's internal regulator

So even in STEP/DIR mode, firmware must at least write the chopper and current registers (CHOPCONF with TOFF ≠ 0, IHOLD_IRUN, GLOBALSCALER if used) and then drive PB0 low before the motor will move.

The MT6701's MODE pin is tied to 3.3 V, which puts it in SSI mode (Z = CSN, B = CLK, A = DO). Its analog OUT and PUSH pins are not connected.

Key settings

SettingValueMeaning
ICACHE_CR (0x40030400)1Instruction cache on; required
FDCAN1 kernel clockHSE 24 MHzRCC_CCIPR5.FDCAN1SEL = 00
FDCAN1 NBTP0x02020C01500 kbit/s nominal (bring-up value)
TMC5160 CHOPCONF0x000100C3SpreadCycle, TOFF 3, 256 microsteps
TMC5160 clockInternal, about 12 MHzUsed in the VMAX formula below

TMC5160 velocity

With the internal clock (about 12 MHz) and 256 microsteps on a 200-step motor:

VMAX = rpm / 60 × 51200 × 2^24 / 12 000 000

For example 300 rpm → VMAX ≈ 357 914.

Encoder frame

The MT6701 returns a 24-bit SSI frame, clocked with CSN low:

BitsField
23:10Angle, 14 bits (16,384 counts/rev)
9:6Status (0000 = field OK; 1010 was seen with no magnet)
5:0CRC6, polynomial x⁶ + x + 1

In USB DFU mode the ROM bootloader sets PB13/PB14 to SPI2 (AF5), so the encoder reads all zeros until the chip is reset into user code (or halted at reset).

TMC5160 DRV_STATUS

Bit 24 of DRV_STATUS is the StallGuard flag, not s2ga (short to ground, phase A). Mixing them up prints a false "short to ground" fault.

Recommendations

  • Enable the instruction cache at reset, before any other code.
  • Stay in SpreadCycle at all speeds. Keep THIGH = 0 and do not use the high-speed full-step switch-over (vhighfs / vhighchm) without tuning: at about 300 rpm it upset an untuned motor, which then stalled on the way to 900–1500 rpm.
  • Use the TMC5160's internal ramp generator. The MCU only sets targets and speeds.
  • Use the encoder for stall detection and position checks. StallGuard is not tuned on this board yet.
  • Use a normal C toolchain (Arm GCC or STM32CubeIDE). Bring-up images were small hand-assembled Thumb-2 test programs and are not a base for product firmware.

On this page