Skip to content

Commit

Permalink
Added TMC2209 support (#727)
Browse files Browse the repository at this point in the history
TMC2209: Added TMC2209 support
  • Loading branch information
amken3d authored Jan 28, 2025
1 parent f57b5ec commit 87c205f
Show file tree
Hide file tree
Showing 12 changed files with 1,975 additions and 0 deletions.
35 changes: 35 additions & 0 deletions examples/tmc2209/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"machine"

"tinygo.org/x/drivers/tmc2209"
)

func main() {
uart := machine.UART0
comm := tmc2209.NewUARTComm(*uart, 0)
// Create an instance of the TMC2209 with UART communication
tmc := tmc2209.NewTMC2209(comm, 0x00) // Replace 0x00 with the appropriate address

// Set up the TMC2209 driver
err := tmc.Setup()
if err != nil {
println("Failed to set up TMC2209: ", err)
}

// Write to a register (example: setting a register value)
err = tmc.WriteRegister(0x10, 0x12345678) // Replace 0x10 with the register address and 0x12345678 with the value
if err != nil {
println("Failed to write register:", err)
}

// Read from a register (example: reading a register value)
value, err := tmc.ReadRegister(0x10)
if err != nil {
println("Failed to read register: ", err)
}

// Output the read value
println("Register value: ", value)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module tinygo.org/x/drivers


go 1.22.1

toolchain go1.23.1


require (
github.com/eclipse/paho.mqtt.golang v1.2.0
github.com/frankban/quicktest v1.10.2
Expand Down
1 change: 1 addition & 0 deletions smoketest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/mpu9150/mai
tinygo build -size short -o ./build/test.hex -target=macropad-rp2040 ./examples/sh1106/macropad_spi
tinygo build -size short -o ./build/test.hex -target=macropad-rp2040 ./examples/encoders/quadrature-interrupt
tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/mcp9808/main.go
tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/tmc2209/main.go
tinygo build -size short -o ./build/test.hex -target=pico ./examples/tmc5160/main.go
tinygo build -size short -o ./build/test.uf2 -target=nicenano ./examples/sharpmem/main.go
# network examples (espat)
Expand Down
163 changes: 163 additions & 0 deletions tmc2209/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# TMC2209 Go Package

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.
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.
## Features
- **UART Communication:** Standard communication through UART for controlling the TMC2209 driver.
- **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
- **Error Handling:** Lightweight error handling for TinyGo compatibility.
- **Register Access:** Provides methods to read and write registers on the TMC2209.

## Setup

### Prerequisites
- **TinyGo**: This package is optimized for TinyGo, which is suitable for running Go on microcontrollers and embedded systems.
- **UART Communication**: For microcontrollers with UART support, such as ESP32, STM32, and Raspberry Pi Pico.


## Usage
### Using with Microcontrollers (Default UART)

To use the package with a microcontroller, you need to initialize the UART communication and configure the TMC2209 driver.
Example:

```aiignore
package main
import (
"fmt"
"log"
"github.com/yourusername/tmc2209"
"machine"
)
func main() {
uart := machine.UART0
// Create an instance of the TMC2209 with UART communication
tmc := tmc2209.NewTMC2209(uart, 0x00) // Replace 0x00 with the appropriate address
// Set up the TMC2209 driver
err := tmc.Setup()
if err != nil {
log.Fatalf("Failed to set up TMC2209: %v", err)
}
// Write to a register (example: setting a register value)
err = tmc.WriteRegister(0x10, 0x12345678) // Replace 0x10 with the register address and 0x12345678 with the value
if err != nil {
log.Fatalf("Failed to write register: %v", err)
}
// Read from a register (example: reading a register value)
value, err := tmc.ReadRegister(0x10)
if err != nil {
log.Fatalf("Failed to read register: %v", err)
}
// Output the read value
fmt.Printf("Register value: 0x%X\n", value)
}
```
## Microcontroller Notes

- **TinyGo Support:** This code is optimized for use with TinyGo on supported microcontrollers.
- **UART Configuration:** Ensure the UART instance is configured correctly for your microcontroller.
- 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.

## 2. Custom Interface Implementation

If you want to use a custom communication interface (e.g., USB, SPI, etc.), you can implement the RegisterComm interface.
Custom Interface Example (e.g., USB Communication):

```
package main
import (
"fmt"
"log"
"github.com/yourusername/tmc2209"
"yourcustompackage" // Custom package for communication
)
type CustomComm struct {
// Implement your custom communication method here
}
func (c *CustomComm) ReadRegister(register uint8, driverIndex uint8) (uint32, error) {
// Implement the register read logic using your custom interface (USB, SPI, etc.)
// For example, send the register read command over USB and read the response
return 0, nil
}
func (c *CustomComm) WriteRegister(register uint8, value uint32, driverIndex uint8) error {
// Implement the register write logic using your custom interface (USB, SPI, etc.)
// For example, send the register write command over USB
return nil
}
func main() {
// Create an instance of the TMC2209 with your custom communication interface
customComm := &CustomComm{}
tmc := tmc2209.NewTMC2209(customComm, 0x00) // Replace 0x00 with the appropriate address
// Set up the TMC2209 driver
err := tmc.Setup()
if err != nil {
log.Fatalf("Failed to set up TMC2209: %v", err)
}
// Write to a register (example: setting a register value)
err = tmc.WriteRegister(0x10, 0x12345678) // Replace 0x10 with the register address and 0x12345678 with the value
if err != nil {
log.Fatalf("Failed to write register: %v", err)
}
// Read from a register (example: reading a register value)
value, err := tmc.ReadRegister(0x10)
if err != nil {
log.Fatalf("Failed to read register: %v", err)
}
// Output the read value
fmt.Printf("Register value: 0x%X\n", value)
}
```

### Custom Interface Notes:

- 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.
- Error Handling: Implement proper error handling in your custom interface methods (ReadRegister and WriteRegister).

#### Functions
```
NewTMC2209(comm RegisterComm, address uint8) *TMC2209
```
Creates a new TMC2209 instance with the provided communication interface (comm) and driver address (address).

```Setup() error```

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.

```WriteRegister(register uint8, value uint32) error```

Writes a value to a specific register on the TMC2209 driver.

```ReadRegister(register uint8) (uint32, error)```

Reads a value from a specific register on the TMC2209 driver.

### Error Handling

This package uses a lightweight custom error type (CustomError) to ensure compatibility with TinyGo and reduce external dependencies.
Example Error:

```CustomError("communication interface not set")```


Created by Amken3d
info@amken3d.us


Loading

0 comments on commit 87c205f

Please # to comment.