Skip to content

Commit 87c205f

Browse files
authored
Added TMC2209 support (#727)
TMC2209: Added TMC2209 support
1 parent f57b5ec commit 87c205f

12 files changed

+1975
-0
lines changed

examples/tmc2209/main.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"machine"
5+
6+
"tinygo.org/x/drivers/tmc2209"
7+
)
8+
9+
func main() {
10+
uart := machine.UART0
11+
comm := tmc2209.NewUARTComm(*uart, 0)
12+
// Create an instance of the TMC2209 with UART communication
13+
tmc := tmc2209.NewTMC2209(comm, 0x00) // Replace 0x00 with the appropriate address
14+
15+
// Set up the TMC2209 driver
16+
err := tmc.Setup()
17+
if err != nil {
18+
println("Failed to set up TMC2209: ", err)
19+
}
20+
21+
// Write to a register (example: setting a register value)
22+
err = tmc.WriteRegister(0x10, 0x12345678) // Replace 0x10 with the register address and 0x12345678 with the value
23+
if err != nil {
24+
println("Failed to write register:", err)
25+
}
26+
27+
// Read from a register (example: reading a register value)
28+
value, err := tmc.ReadRegister(0x10)
29+
if err != nil {
30+
println("Failed to read register: ", err)
31+
}
32+
33+
// Output the read value
34+
println("Register value: ", value)
35+
}

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
module tinygo.org/x/drivers
22

3+
34
go 1.22.1
45

56
toolchain go1.23.1
67

8+
79
require (
810
github.com/eclipse/paho.mqtt.golang v1.2.0
911
github.com/frankban/quicktest v1.10.2

smoketest.sh

+1
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.uf2 -target=pico ./examples/tmc2209/main.go
139140
tinygo build -size short -o ./build/test.hex -target=pico ./examples/tmc5160/main.go
140141
tinygo build -size short -o ./build/test.uf2 -target=nicenano ./examples/sharpmem/main.go
141142
# network examples (espat)

tmc2209/README.md

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# TMC2209 Go Package
2+
3+
This package provides a lightweight interface to communicate with the TMC2209 stepper motor driver via UART. It is designed to be used with microcontrollers and supports both default UART communication and custom interface implementations for flexibility.
4+
Currently this package only implements the communications with TMC2209. Functions to handle specific operations such as EnableStealthChop() etc. will need to be implemented by the user.
5+
## Features
6+
- **UART Communication:** Standard communication through UART for controlling the TMC2209 driver.
7+
- **Custom Communication Interfaces:** Allows custom implementations of communication interfaces (e.g., USB, SPI, etc.) via the `RegisterComm` interface. The usecase for this is if you have host system that can "talk" to the microcontroller connected to the TMC2209
8+
- **Error Handling:** Lightweight error handling for TinyGo compatibility.
9+
- **Register Access:** Provides methods to read and write registers on the TMC2209.
10+
11+
## Setup
12+
13+
### Prerequisites
14+
- **TinyGo**: This package is optimized for TinyGo, which is suitable for running Go on microcontrollers and embedded systems.
15+
- **UART Communication**: For microcontrollers with UART support, such as ESP32, STM32, and Raspberry Pi Pico.
16+
17+
18+
## Usage
19+
### Using with Microcontrollers (Default UART)
20+
21+
To use the package with a microcontroller, you need to initialize the UART communication and configure the TMC2209 driver.
22+
Example:
23+
24+
```aiignore
25+
package main
26+
27+
import (
28+
"fmt"
29+
"log"
30+
"github.com/yourusername/tmc2209"
31+
"machine"
32+
)
33+
34+
func main() {
35+
uart := machine.UART0
36+
37+
// Create an instance of the TMC2209 with UART communication
38+
tmc := tmc2209.NewTMC2209(uart, 0x00) // Replace 0x00 with the appropriate address
39+
40+
// Set up the TMC2209 driver
41+
err := tmc.Setup()
42+
if err != nil {
43+
log.Fatalf("Failed to set up TMC2209: %v", err)
44+
}
45+
46+
// Write to a register (example: setting a register value)
47+
err = tmc.WriteRegister(0x10, 0x12345678) // Replace 0x10 with the register address and 0x12345678 with the value
48+
if err != nil {
49+
log.Fatalf("Failed to write register: %v", err)
50+
}
51+
52+
// Read from a register (example: reading a register value)
53+
value, err := tmc.ReadRegister(0x10)
54+
if err != nil {
55+
log.Fatalf("Failed to read register: %v", err)
56+
}
57+
58+
// Output the read value
59+
fmt.Printf("Register value: 0x%X\n", value)
60+
}
61+
62+
```
63+
## Microcontroller Notes
64+
65+
- **TinyGo Support:** This code is optimized for use with TinyGo on supported microcontrollers.
66+
- **UART Configuration:** Ensure the UART instance is configured correctly for your microcontroller.
67+
- The machine.UART0 in the example is for the default UART on TinyGo-compatible devices like the Raspberry Pi Pico. Check your microcontroller's documentation for the correct UART instance and pin configuration.
68+
69+
## 2. Custom Interface Implementation
70+
71+
If you want to use a custom communication interface (e.g., USB, SPI, etc.), you can implement the RegisterComm interface.
72+
Custom Interface Example (e.g., USB Communication):
73+
74+
```
75+
package main
76+
77+
import (
78+
"fmt"
79+
"log"
80+
"github.com/yourusername/tmc2209"
81+
"yourcustompackage" // Custom package for communication
82+
)
83+
84+
type CustomComm struct {
85+
// Implement your custom communication method here
86+
}
87+
88+
func (c *CustomComm) ReadRegister(register uint8, driverIndex uint8) (uint32, error) {
89+
// Implement the register read logic using your custom interface (USB, SPI, etc.)
90+
// For example, send the register read command over USB and read the response
91+
return 0, nil
92+
}
93+
94+
func (c *CustomComm) WriteRegister(register uint8, value uint32, driverIndex uint8) error {
95+
// Implement the register write logic using your custom interface (USB, SPI, etc.)
96+
// For example, send the register write command over USB
97+
return nil
98+
}
99+
100+
func main() {
101+
// Create an instance of the TMC2209 with your custom communication interface
102+
customComm := &CustomComm{}
103+
tmc := tmc2209.NewTMC2209(customComm, 0x00) // Replace 0x00 with the appropriate address
104+
105+
// Set up the TMC2209 driver
106+
err := tmc.Setup()
107+
if err != nil {
108+
log.Fatalf("Failed to set up TMC2209: %v", err)
109+
}
110+
111+
// Write to a register (example: setting a register value)
112+
err = tmc.WriteRegister(0x10, 0x12345678) // Replace 0x10 with the register address and 0x12345678 with the value
113+
if err != nil {
114+
log.Fatalf("Failed to write register: %v", err)
115+
}
116+
117+
// Read from a register (example: reading a register value)
118+
value, err := tmc.ReadRegister(0x10)
119+
if err != nil {
120+
log.Fatalf("Failed to read register: %v", err)
121+
}
122+
123+
// Output the read value
124+
fmt.Printf("Register value: 0x%X\n", value)
125+
}
126+
127+
```
128+
129+
### Custom Interface Notes:
130+
131+
- RegisterComm Interface: Your custom communication interface (e.g., USB, SPI) should implement the RegisterComm interface. This ensures that the TMC2209 driver can interact with your custom interface for reading and writing registers.
132+
- Error Handling: Implement proper error handling in your custom interface methods (ReadRegister and WriteRegister).
133+
134+
#### Functions
135+
```
136+
NewTMC2209(comm RegisterComm, address uint8) *TMC2209
137+
```
138+
Creates a new TMC2209 instance with the provided communication interface (comm) and driver address (address).
139+
140+
```Setup() error```
141+
142+
Initializes the communication interface. This is required before interacting with the TMC2209 driver. The method checks if the communication interface is UART and sets it up accordingly.
143+
144+
```WriteRegister(register uint8, value uint32) error```
145+
146+
Writes a value to a specific register on the TMC2209 driver.
147+
148+
```ReadRegister(register uint8) (uint32, error)```
149+
150+
Reads a value from a specific register on the TMC2209 driver.
151+
152+
### Error Handling
153+
154+
This package uses a lightweight custom error type (CustomError) to ensure compatibility with TinyGo and reduce external dependencies.
155+
Example Error:
156+
157+
```CustomError("communication interface not set")```
158+
159+
160+
Created by Amken3d
161+
162+
163+

0 commit comments

Comments
 (0)