|
| 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