-
Notifications
You must be signed in to change notification settings - Fork 218
Added TMC5160 support #725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c8071ba
Added TMC5160 support
amken3d 9d77cdd
Added example code for tmc5160 and smoke test
amken3d f8507b5
Update go.mod
amken3d 5ef87d5
Updated mod file
amken3d 09ac2c4
Cleaned up commented out code and updated readme.
amken3d 55290a1
ran go fmt
amken3d 5cf8a2e
Fixed setrampspeed func
amken3d 70d09e8
Removed spi1 pin setup in example.go. Renamed SPIComm to spicomm.go
amken3d e957752
Removed unused test file
amken3d 1c92aec
Removed commented line
amken3d File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Connects to SPI1 on a RP2040 (Pico) | ||
package main | ||
|
||
import ( | ||
"machine" | ||
|
||
"tinygo.org/x/drivers/tmc5160" | ||
) | ||
|
||
func main() { | ||
// Step 1. Setup your protocol. SPI setup shown below | ||
spi := machine.SPI1 | ||
spi.Configure(machine.SPIConfig{ | ||
Frequency: 12000000, // Upto 12 MHZ is pretty stable. Reduce to 5 or 6 Mhz if you are experiencing issues | ||
Mode: 3, | ||
LSBFirst: false, | ||
}) | ||
// Step 2. Set up all associated Pins | ||
csPin0 := machine.GPIO13 | ||
csPin0.Configure(machine.PinConfig{Mode: machine.PinOutput}) | ||
enn0 := machine.GPIO18 | ||
enn0.Configure(machine.PinConfig{Mode: machine.PinOutput}) | ||
|
||
// csPins is a map of all chip select pins in a multi driver setup. | ||
//Only one pin csPin0 mapped to "0"is shown in this example, but add more mappings as required | ||
csPins := map[uint8]machine.Pin{0: csPin0} | ||
//bind csPin to driverAdddress | ||
driverAddress := uint8(0) // Let's assume we are working with driver at address 0x01 | ||
// Step 3. Bind the communication interface to the protocol | ||
comm := tmc5160.NewSPIComm(*spi, csPins) | ||
// Step 4. Define your stepper like this below | ||
//stepper := tmc5160.NewStepper(angle , gearRatio vSupply rCoil , lCoil , iPeak , rSense , mSteps, fclk ) | ||
stepper := tmc5160.NewDefaultStepper() // Default Stepper should be used only for testing. | ||
// Step 5. Instantiate your driver | ||
driver := tmc5160.NewDriver( | ||
comm, | ||
driverAddress, | ||
enn0, | ||
stepper) | ||
|
||
// Setting and getting mode | ||
rampMode := tmc5160.NewRAMPMODE(comm, driverAddress) | ||
err := rampMode.SetMode(tmc5160.PositioningMode) | ||
if err != nil { | ||
return | ||
} | ||
mode, err := rampMode.GetMode() | ||
if err != nil { | ||
println("Error getting mode:", err) | ||
} else { | ||
println("Current Mode:", mode) | ||
} | ||
|
||
// Read GCONF register | ||
GCONF := tmc5160.NewGCONF() | ||
gconfVal, err := driver.ReadRegister(tmc5160.GCONF) | ||
// Uppack the register to get all the bits and bytes of the register | ||
GCONF.Unpack(gconfVal) | ||
//E.g. MultiStepFlit is retrieved from the GCONF register | ||
println("GCONF:MultiStepFlit:", GCONF.MultistepFilt) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,23 @@ | ||
module tinygo.org/x/drivers | ||
|
||
go 1.18 | ||
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 | ||
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 | ||
github.com/orsinium-labs/tinymath v1.1.0 | ||
github.com/soypat/natiu-mqtt v0.5.1 | ||
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d | ||
golang.org/x/net v0.7.0 | ||
tinygo.org/x/tinyfont v0.3.0 | ||
tinygo.org/x/tinyterm v0.1.0 | ||
) | ||
|
||
require ( | ||
github.com/google/go-cmp v0.5.2 // indirect | ||
github.com/google/go-cmp v0.6.0 // indirect | ||
github.com/kr/pretty v0.2.1 // indirect | ||
github.com/kr/text v0.1.0 // indirect | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
# TMC5160 Driver for Go (TinyGo) | ||
|
||
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. | ||
|
||
## Table of Contents | ||
|
||
- [Installation](#installation) | ||
- [Communication Modes](#communication-modes) | ||
- [SPI Mode](#spi-mode) | ||
- [UART Mode](#uart-mode) | ||
- [Usage Example](#usage-example) | ||
- [Setting and Getting Modes](#setting-and-getting-modes) | ||
- [Reading and Writing Registers](#reading-and-writing-registers) | ||
- [API Reference](#api-reference) | ||
- [License](#license) | ||
|
||
## Installation | ||
|
||
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/). | ||
|
||
### Dependencies | ||
|
||
- **machine**: To interface with hardware on platforms like Raspberry Pi, STM32, etc. | ||
- **TinyGo**: A Go compiler for embedded systems. | ||
|
||
Add the module | ||
|
||
```bash | ||
import "tinygo.org/x/drivers/tmc5160" | ||
``` | ||
|
||
### Communication Modes | ||
|
||
The TMC5160 supports two communication modes for controlling the motor: | ||
|
||
**SPI Mode** | ||
|
||
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. | ||
SPI Setup | ||
|
||
In SPI mode, you must configure the SPI interface on your microcontroller. Here's how to set up SPI communication for the TMC5160. | ||
|
||
```go | ||
spi := machine.SPI1 | ||
csPin := machine.GPIO13 | ||
spi.Configure(machine.SPIConfig{ | ||
SCK: machine.GPIO10, | ||
SDI: machine.GPIO11, | ||
SDO: machine.GPIO12, | ||
Frequency: 5000000, | ||
Mode: 3, | ||
LSBFirst: false, | ||
}) | ||
|
||
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) | ||
``` | ||
**Sending Commands via SPI** | ||
|
||
The driver supports reading and writing registers using the SPIComm interface, which is initialized with the configured SPI bus and CS pins | ||
|
||
```go | ||
comm := tmc5160.NewSPIComm(*spi, csPins) | ||
driver := tmc5160.NewTMC5160(comm, driverIndex) | ||
driver.WriteRegister(tmc5160.GCONF, value) | ||
|
||
``` | ||
|
||
**UART Mode** | ||
|
||
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. | ||
UART Setup | ||
|
||
In UART mode, you will need to configure the UART interface with the appropriate baud rate and settings: | ||
|
||
```go | ||
uart := machine.UART0 | ||
uart.Configure(machine.UARTConfig{ | ||
BaudRate: 115200, | ||
}) | ||
``` | ||
#### Sending Commands via UART | ||
|
||
The UART communication is handled through the UARTComm struct, which wraps the UART interface. | ||
|
||
```go | ||
comm := tmc5160.NewUARTComm(uart, 0x01) | ||
driver := tmc5160.NewTMC5160(comm, 0) | ||
driver.WriteRegister(tmc5160.GCONF, 0x01) | ||
``` | ||
|
||
## Usage Example | ||
|
||
Here’s a simple example of how to use the TMC5160 driver with SPI and UART modes: | ||
|
||
```aiignore | ||
// Connects to SPI1 on a RP2040 (Pico) | ||
package main | ||
|
||
import ( | ||
"machine" | ||
|
||
"tinygo.org/x/drivers/tmc5160" | ||
) | ||
|
||
func main() { | ||
// Step 1. Setup your protocol. SPI setup shown below | ||
spi := machine.SPI1 | ||
spi.Configure(machine.SPIConfig{ | ||
SCK: machine.GPIO10, | ||
SDI: machine.GPIO11, | ||
SDO: machine.GPIO12, | ||
Frequency: 12000000, // Upto 12 MHZ is pretty stable. Reduce to 5 or 6 Mhz if you are experiencing issues | ||
Mode: 3, | ||
LSBFirst: false, | ||
}) | ||
// Step 2. Set up all associated Pins | ||
csPin0 := machine.GPIO13 | ||
csPin0.Configure(machine.PinConfig{Mode: machine.PinOutput}) | ||
enn0 := machine.GPIO18 | ||
enn0.Configure(machine.PinConfig{Mode: machine.PinOutput}) | ||
|
||
// csPins is a map of all chip select pins in a multi driver setup. | ||
//Only one pin csPin0 mapped to "0"is shown in this example, but add more mappings as required | ||
csPins := map[uint8]machine.Pin{0: csPin0} | ||
//bind csPin to driverAdddress | ||
driverAddress := uint8(0) // Let's assume we are working with driver at address 0x01 | ||
// Step 3. Bind the communication interface to the protocol | ||
comm := tmc5160.NewSPIComm(*spi, csPins) | ||
// Step 4. Define your stepper like this below | ||
//stepper := tmc5160.NewStepper(angle , gearRatio vSupply rCoil , lCoil , iPeak , rSense , mSteps, fclk ) | ||
stepper := tmc5160.NewDefaultStepper() // Default Stepper should be used only for testing. | ||
// Step 5. Instantiate your driver | ||
driver := tmc5160.NewDriver( | ||
comm, | ||
driverAddress, | ||
enn0, | ||
stepper) | ||
|
||
// Setting and getting mode | ||
rampMode := tmc5160.NewRAMPMODE(comm, driverAddress) | ||
err := rampMode.SetMode(tmc5160.PositioningMode) | ||
if err != nil { | ||
return | ||
} | ||
mode, err := rampMode.GetMode() | ||
if err != nil { | ||
println("Error getting mode:", err) | ||
} else { | ||
println("Current Mode:", mode) | ||
} | ||
|
||
// Read GCONF register | ||
GCONF := tmc5160.NewGCONF() | ||
gconfVal, err := driver.ReadRegister(tmc5160.GCONF) | ||
// Uppack the register to get all the bits and bytes of the register | ||
GCONF.Unpack(gconfVal) | ||
//E.g. MultiStepFlit is retrieved from the GCONF register | ||
println("GCONF:MultiStepFlit:", GCONF.MultistepFilt) | ||
} | ||
|
||
|
||
``` | ||
## Reading and Writing Registers | ||
|
||
You can easily read and write registers using the WriteRegister and ReadRegister methods: | ||
|
||
```aiignore | ||
// Write a value to a register | ||
err := driver.WriteRegister(tmc5160.GCONF, 0x01) | ||
if err != nil { | ||
fmt.Println("Error writing register:", err) | ||
} | ||
|
||
// Read a register | ||
value, err := driver.ReadRegister(tmc5160.GCONF) | ||
if err != nil { | ||
fmt.Println("Error reading register:", err) | ||
} else { | ||
fmt.Println("Read value from GCONF:", value) | ||
} | ||
|
||
``` | ||
|
||
## API Reference | ||
|
||
NewSPIComm(spi machine.SPI, csPins map[uint8]machine.Pin) *SPIComm | ||
|
||
Creates a new SPI communication interface for the TMC5160. | ||
|
||
NewUARTComm(uart machine.UART, address uint8) *UARTComm | ||
|
||
Creates a new UART communication interface for the TMC5160. | ||
|
||
NewTMC5160(comm RegisterComm, address uint8) *TMC5160 | ||
|
||
Creates a new instance of the TMC5160 driver. | ||
|
||
WriteRegister(register uint8, value uint32) error | ||
|
||
Writes a value to the specified register. | ||
|
||
ReadRegister(register uint8) (uint32, error) | ||
|
||
Reads a value from the specified register. | ||
|
||
## License | ||
|
||
This project is licensed under the MIT License | ||
|
||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.