Skip to content

Commit 4869496

Browse files
authored
Added TMC5160 support (#725)
TMC5160: Added TMC5160 support * Added example code for tmc5160 and smoke test * Update go.mod * Updated mod file * Cleaned up commented out code and updated readme. * ran go fmt * Fixed setrampspeed func * Removed spi1 pin setup in example.go. Renamed SPIComm to spicomm.go * Removed unused test file * Removed commented line
1 parent 00578a3 commit 4869496

File tree

13 files changed

+3050
-5
lines changed

13 files changed

+3050
-5
lines changed

examples/tmc5160/main.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Connects to SPI1 on a RP2040 (Pico)
2+
package main
3+
4+
import (
5+
"machine"
6+
7+
"tinygo.org/x/drivers/tmc5160"
8+
)
9+
10+
func main() {
11+
// Step 1. Setup your protocol. SPI setup shown below
12+
spi := machine.SPI1
13+
spi.Configure(machine.SPIConfig{
14+
Frequency: 12000000, // Upto 12 MHZ is pretty stable. Reduce to 5 or 6 Mhz if you are experiencing issues
15+
Mode: 3,
16+
LSBFirst: false,
17+
})
18+
// Step 2. Set up all associated Pins
19+
csPin0 := machine.GPIO13
20+
csPin0.Configure(machine.PinConfig{Mode: machine.PinOutput})
21+
enn0 := machine.GPIO18
22+
enn0.Configure(machine.PinConfig{Mode: machine.PinOutput})
23+
24+
// csPins is a map of all chip select pins in a multi driver setup.
25+
//Only one pin csPin0 mapped to "0"is shown in this example, but add more mappings as required
26+
csPins := map[uint8]machine.Pin{0: csPin0}
27+
//bind csPin to driverAdddress
28+
driverAddress := uint8(0) // Let's assume we are working with driver at address 0x01
29+
// Step 3. Bind the communication interface to the protocol
30+
comm := tmc5160.NewSPIComm(*spi, csPins)
31+
// Step 4. Define your stepper like this below
32+
//stepper := tmc5160.NewStepper(angle , gearRatio vSupply rCoil , lCoil , iPeak , rSense , mSteps, fclk )
33+
stepper := tmc5160.NewDefaultStepper() // Default Stepper should be used only for testing.
34+
// Step 5. Instantiate your driver
35+
driver := tmc5160.NewDriver(
36+
comm,
37+
driverAddress,
38+
enn0,
39+
stepper)
40+
41+
// Setting and getting mode
42+
rampMode := tmc5160.NewRAMPMODE(comm, driverAddress)
43+
err := rampMode.SetMode(tmc5160.PositioningMode)
44+
if err != nil {
45+
return
46+
}
47+
mode, err := rampMode.GetMode()
48+
if err != nil {
49+
println("Error getting mode:", err)
50+
} else {
51+
println("Current Mode:", mode)
52+
}
53+
54+
// Read GCONF register
55+
GCONF := tmc5160.NewGCONF()
56+
gconfVal, err := driver.ReadRegister(tmc5160.GCONF)
57+
// Uppack the register to get all the bits and bytes of the register
58+
GCONF.Unpack(gconfVal)
59+
//E.g. MultiStepFlit is retrieved from the GCONF register
60+
println("GCONF:MultiStepFlit:", GCONF.MultistepFilt)
61+
}

go.mod

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
module tinygo.org/x/drivers
22

3-
go 1.18
3+
go 1.22.1
4+
5+
toolchain go1.23.1
46

57
require (
68
github.com/eclipse/paho.mqtt.golang v1.2.0
79
github.com/frankban/quicktest v1.10.2
810
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
11+
github.com/orsinium-labs/tinymath v1.1.0
912
github.com/soypat/natiu-mqtt v0.5.1
13+
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d
1014
golang.org/x/net v0.7.0
1115
tinygo.org/x/tinyfont v0.3.0
1216
tinygo.org/x/tinyterm v0.1.0
1317
)
1418

1519
require (
16-
github.com/google/go-cmp v0.5.2 // indirect
20+
github.com/google/go-cmp v0.6.0 // indirect
1721
github.com/kr/pretty v0.2.1 // indirect
1822
github.com/kr/text v0.1.0 // indirect
19-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
2023
)

go.sum

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@ github.com/eclipse/paho.mqtt.golang v1.2.0 h1:1F8mhG9+aO5/xpdtFkW4SxOJB67ukuDC3t
33
github.com/eclipse/paho.mqtt.golang v1.2.0/go.mod h1:H9keYFcgq3Qr5OUJm/JZI/i6U7joQ8SYLhZwfeOo6Ts=
44
github.com/frankban/quicktest v1.10.2 h1:19ARM85nVi4xH7xPXuc5eM/udya5ieh7b/Sv+d844Tk=
55
github.com/frankban/quicktest v1.10.2/go.mod h1:K+q6oSqb0W0Ininfk863uOk1lMy69l/P6txr3mVT54s=
6-
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
76
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
7+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
8+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
89
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
910
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
1011
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
1112
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
1213
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
1314
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
1415
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
16+
github.com/orsinium-labs/tinymath v1.1.0 h1:KomdsyLHB7vE3f1nRAJF2dyf1m/gnM2HxfTeV1vS5UA=
17+
github.com/orsinium-labs/tinymath v1.1.0/go.mod h1:WPXX6ei3KSXG7JfA03a+ekCYaY9SWN4I+JRl2p6ck+A=
1518
github.com/soypat/natiu-mqtt v0.5.1 h1:rwaDmlvjzD2+3MCOjMZc4QEkDkNwDzbct2TJbpz+TPc=
1619
github.com/soypat/natiu-mqtt v0.5.1/go.mod h1:xEta+cwop9izVCW7xOx2W+ct9PRMqr0gNVkvBPnQTc4=
1720
github.com/valyala/fastjson v1.6.3/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
21+
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d h1:0olWaB5pg3+oychR51GUVCEsGkeCU/2JxjBgIo4f3M0=
22+
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d/go.mod h1:qj5a5QZpwLU2NLQudwIN5koi3beDhSAlJwa67PuM98c=
1823
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
1924
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
20-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
2125
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
2226
tinygo.org/x/drivers v0.14.0/go.mod h1:uT2svMq3EpBZpKkGO+NQHjxjGf1f42ra4OnMMwQL2aI=
2327
tinygo.org/x/drivers v0.15.1/go.mod h1:uT2svMq3EpBZpKkGO+NQHjxjGf1f42ra4OnMMwQL2aI=

smoketest.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/mpu9150/mai
136136
tinygo build -size short -o ./build/test.hex -target=macropad-rp2040 ./examples/sh1106/macropad_spi
137137
tinygo build -size short -o ./build/test.hex -target=macropad-rp2040 ./examples/encoders/quadrature-interrupt
138138
tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/mcp9808/main.go
139+
tinygo build -size short -o ./build/test.hex -target=pico ./examples/tmc5160/main.go
139140
# network examples (espat)
140141
tinygo build -size short -o ./build/test.hex -target=challenger-rp2040 ./examples/net/ntpclient/
141142
# network examples (wifinina)

tmc5160/README.MD

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
# TMC5160 Driver for Go (TinyGo)
2+
3+
This repository provides a Go-based driver for the **TMC5160** stepper motor driver, implemented for both **SPI** and **UART** communication modes. The driver allows you to easily interface with the TMC5160 to configure and control stepper motors.
4+
5+
## Table of Contents
6+
7+
- [Installation](#installation)
8+
- [Communication Modes](#communication-modes)
9+
- [SPI Mode](#spi-mode)
10+
- [UART Mode](#uart-mode)
11+
- [Usage Example](#usage-example)
12+
- [Setting and Getting Modes](#setting-and-getting-modes)
13+
- [Reading and Writing Registers](#reading-and-writing-registers)
14+
- [API Reference](#api-reference)
15+
- [License](#license)
16+
17+
## Installation
18+
19+
To use the TMC5160 driver, you'll need to have **TinyGo** installed. You can install TinyGo by following the [official installation guide](https://tinygo.org/getting-started/).
20+
21+
### Dependencies
22+
23+
- **machine**: To interface with hardware on platforms like Raspberry Pi, STM32, etc.
24+
- **TinyGo**: A Go compiler for embedded systems.
25+
26+
Add the module
27+
28+
```bash
29+
import "tinygo.org/x/drivers/tmc5160"
30+
```
31+
32+
### Communication Modes
33+
34+
The TMC5160 supports two communication modes for controlling the motor:
35+
36+
**SPI Mode**
37+
38+
To communicate with the TMC5160 in SPI mode, you'll need to configure the SPI bus and the chip-select (CS) pin. This allows full-speed communication between your microcontroller and the TMC5160.
39+
SPI Setup
40+
41+
In SPI mode, you must configure the SPI interface on your microcontroller. Here's how to set up SPI communication for the TMC5160.
42+
43+
```go
44+
spi := machine.SPI1
45+
csPin := machine.GPIO13
46+
spi.Configure(machine.SPIConfig{
47+
SCK: machine.GPIO10,
48+
SDI: machine.GPIO11,
49+
SDO: machine.GPIO12,
50+
Frequency: 5000000,
51+
Mode: 3,
52+
LSBFirst: false,
53+
})
54+
55+
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
56+
```
57+
**Sending Commands via SPI**
58+
59+
The driver supports reading and writing registers using the SPIComm interface, which is initialized with the configured SPI bus and CS pins
60+
61+
```go
62+
comm := tmc5160.NewSPIComm(*spi, csPins)
63+
driver := tmc5160.NewTMC5160(comm, driverIndex)
64+
driver.WriteRegister(tmc5160.GCONF, value)
65+
66+
```
67+
68+
**UART Mode**
69+
70+
Alternatively, you can use UART mode to communicate with the TMC5160. UART mode is useful for cases where SPI is not available or when the TMC5160 is used in multi-driver configurations with limited SPI pins.
71+
UART Setup
72+
73+
In UART mode, you will need to configure the UART interface with the appropriate baud rate and settings:
74+
75+
```go
76+
uart := machine.UART0
77+
uart.Configure(machine.UARTConfig{
78+
BaudRate: 115200,
79+
})
80+
```
81+
#### Sending Commands via UART
82+
83+
The UART communication is handled through the UARTComm struct, which wraps the UART interface.
84+
85+
```go
86+
comm := tmc5160.NewUARTComm(uart, 0x01)
87+
driver := tmc5160.NewTMC5160(comm, 0)
88+
driver.WriteRegister(tmc5160.GCONF, 0x01)
89+
```
90+
91+
## Usage Example
92+
93+
Here’s a simple example of how to use the TMC5160 driver with SPI and UART modes:
94+
95+
```aiignore
96+
// Connects to SPI1 on a RP2040 (Pico)
97+
package main
98+
99+
import (
100+
"machine"
101+
102+
"tinygo.org/x/drivers/tmc5160"
103+
)
104+
105+
func main() {
106+
// Step 1. Setup your protocol. SPI setup shown below
107+
spi := machine.SPI1
108+
spi.Configure(machine.SPIConfig{
109+
SCK: machine.GPIO10,
110+
SDI: machine.GPIO11,
111+
SDO: machine.GPIO12,
112+
Frequency: 12000000, // Upto 12 MHZ is pretty stable. Reduce to 5 or 6 Mhz if you are experiencing issues
113+
Mode: 3,
114+
LSBFirst: false,
115+
})
116+
// Step 2. Set up all associated Pins
117+
csPin0 := machine.GPIO13
118+
csPin0.Configure(machine.PinConfig{Mode: machine.PinOutput})
119+
enn0 := machine.GPIO18
120+
enn0.Configure(machine.PinConfig{Mode: machine.PinOutput})
121+
122+
// csPins is a map of all chip select pins in a multi driver setup.
123+
//Only one pin csPin0 mapped to "0"is shown in this example, but add more mappings as required
124+
csPins := map[uint8]machine.Pin{0: csPin0}
125+
//bind csPin to driverAdddress
126+
driverAddress := uint8(0) // Let's assume we are working with driver at address 0x01
127+
// Step 3. Bind the communication interface to the protocol
128+
comm := tmc5160.NewSPIComm(*spi, csPins)
129+
// Step 4. Define your stepper like this below
130+
//stepper := tmc5160.NewStepper(angle , gearRatio vSupply rCoil , lCoil , iPeak , rSense , mSteps, fclk )
131+
stepper := tmc5160.NewDefaultStepper() // Default Stepper should be used only for testing.
132+
// Step 5. Instantiate your driver
133+
driver := tmc5160.NewDriver(
134+
comm,
135+
driverAddress,
136+
enn0,
137+
stepper)
138+
139+
// Setting and getting mode
140+
rampMode := tmc5160.NewRAMPMODE(comm, driverAddress)
141+
err := rampMode.SetMode(tmc5160.PositioningMode)
142+
if err != nil {
143+
return
144+
}
145+
mode, err := rampMode.GetMode()
146+
if err != nil {
147+
println("Error getting mode:", err)
148+
} else {
149+
println("Current Mode:", mode)
150+
}
151+
152+
// Read GCONF register
153+
GCONF := tmc5160.NewGCONF()
154+
gconfVal, err := driver.ReadRegister(tmc5160.GCONF)
155+
// Uppack the register to get all the bits and bytes of the register
156+
GCONF.Unpack(gconfVal)
157+
//E.g. MultiStepFlit is retrieved from the GCONF register
158+
println("GCONF:MultiStepFlit:", GCONF.MultistepFilt)
159+
}
160+
161+
162+
```
163+
## Reading and Writing Registers
164+
165+
You can easily read and write registers using the WriteRegister and ReadRegister methods:
166+
167+
```aiignore
168+
// Write a value to a register
169+
err := driver.WriteRegister(tmc5160.GCONF, 0x01)
170+
if err != nil {
171+
fmt.Println("Error writing register:", err)
172+
}
173+
174+
// Read a register
175+
value, err := driver.ReadRegister(tmc5160.GCONF)
176+
if err != nil {
177+
fmt.Println("Error reading register:", err)
178+
} else {
179+
fmt.Println("Read value from GCONF:", value)
180+
}
181+
182+
```
183+
184+
## API Reference
185+
186+
NewSPIComm(spi machine.SPI, csPins map[uint8]machine.Pin) *SPIComm
187+
188+
Creates a new SPI communication interface for the TMC5160.
189+
190+
NewUARTComm(uart machine.UART, address uint8) *UARTComm
191+
192+
Creates a new UART communication interface for the TMC5160.
193+
194+
NewTMC5160(comm RegisterComm, address uint8) *TMC5160
195+
196+
Creates a new instance of the TMC5160 driver.
197+
198+
WriteRegister(register uint8, value uint32) error
199+
200+
Writes a value to the specified register.
201+
202+
ReadRegister(register uint8) (uint32, error)
203+
204+
Reads a value from the specified register.
205+
206+
## License
207+
208+
This project is licensed under the MIT License
209+
210+
211+

0 commit comments

Comments
 (0)