tmc2209_uart/
lib.rs

1//! # TMC2209 Driver
2//!
3//! A `no_std` Rust driver for the TMC2209 stepper motor driver.
4//!
5//! This crate provides:
6//! - Full register definitions for all 24 TMC2209 registers
7//! - Type-safe register access with bitfield getters/setters
8//! - High-level `Tmc2209` driver struct for UART communication
9//! - Blocking API (feature `blocking`)
10//! - Async API (feature `async`)
11//! - Utility functions for current/velocity calculations
12//!
13//! ## Features
14//!
15//! - `blocking` (default): Enable blocking UART API using `embedded_io` traits
16//! - `async`: Enable async UART API using `embedded_io_async` traits
17//! - `defmt`: Enable `defmt::Format` derives for debugging
18//!
19//! ## Example
20//!
21//! ```ignore
22//! use tmc2209::{Tmc2209, registers::{IholdIrun, Chopconf, MicrostepResolution}};
23//!
24//! // Create driver with UART and slave address 0
25//! let mut driver = Tmc2209::new(uart, 0);
26//!
27//! // Check connection
28//! if driver.is_connected() {
29//!     // Set motor current (run=16, hold=8)
30//!     driver.set_current(16, 8, 1)?;
31//!
32//!     // Set microstep resolution
33//!     driver.set_microsteps(MicrostepResolution::M16)?;
34//!
35//!     // Enable StealthChop for quiet operation
36//!     driver.enable_stealthchop()?;
37//!
38//!     // Start moving at velocity 1000
39//!     driver.set_velocity(1000)?;
40//! }
41//! ```
42//!
43//! ## Protocol Overview
44//!
45//! The TMC2209 uses a simple UART protocol at 115200 baud (configurable):
46//!
47//! - Read request: 4 bytes `[0x05, slave_addr, reg_addr, CRC]`
48//! - Write request: 8 bytes `[0x05, slave_addr, reg_addr|0x80, data[3:0], CRC]`
49//! - Read response: 8 bytes `[0x05, 0xFF, reg_addr, data[3:0], CRC]`
50//!
51//! The driver handles echo bytes automatically (TMC2209 echoes all sent data
52//! on its single-wire UART interface).
53
54#![no_std]
55#![warn(missing_docs)]
56
57pub mod crc;
58pub mod datagram;
59pub mod driver;
60pub mod error;
61pub mod registers;
62pub mod util;
63
64// Re-export main types at crate root
65pub use driver::Tmc2209;
66pub use error::Error;
67
68// Re-export commonly used register types
69pub use registers::{
70    Address, Chopconf, Coolconf, DrvStatus, FactoryConf, Gconf, Gstat, Ifcnt, IholdIrun, Ioin,
71    MicrostepResolution, Mscnt, Mscuract, OtpProg, OtpRead, Pwmconf, PwmAuto, PwmScale,
72    ReadableRegister, Register, SgResult, Sgthrs, Slaveconf, StandstillMode, Tcoolthrs, Tpowerdown,
73    Tpwmthrs, Tstep, Vactual, WritableRegister,
74};
75
76// Re-export utility functions
77pub use util::{
78    calculate_current_settings, cs_to_current, current_to_cs, optimal_vsense, tstep_to_velocity,
79    velocity_to_tpwmthrs, velocity_to_vactual, DEFAULT_FCLK, DEFAULT_RSENSE,
80};
81
82// Re-export datagram types for advanced usage
83pub use datagram::{ReadRequest, ReadResponse, ResponseReader, WriteRequest, MASTER_ADDR, SYNC};