diff --git a/src/hal/avr/tmc2130.cpp b/src/hal/avr/tmc2130.cpp index f8a042c..be9ac1f 100644 --- a/src/hal/avr/tmc2130.cpp +++ b/src/hal/avr/tmc2130.cpp @@ -13,5 +13,32 @@ void TMC2130::Init(const MotorParams ¶ms) { // TODO } +uint32_t TMC2130::ReadRegister(const MotorParams ¶ms, Registers reg) { + uint8_t pData[5] = {(uint8_t)reg}; + _spi_tx_rx(params, pData); + // _handle_spi_status(pData[0]); /// could be outdated. Safer not to use it. + pData[0] = 0; + _spi_tx_rx(params, pData); + _handle_spi_status(pData[0]); + return ((uint32_t)pData[1] << 24 | (uint32_t)pData[2] << 16 | (uint32_t)pData[3] << 8 | (uint32_t)pData[4]); +} + +void TMC2130::WriteRegister(const MotorParams ¶ms, Registers reg, uint32_t data) { + uint8_t pData[5] = {(uint8_t)((uint8_t)(reg) | 0x80), (uint8_t)(data >> 24), (uint8_t)(data >> 16), (uint8_t)(data >> 8), (uint8_t)data}; + _spi_tx_rx(params, pData); + _handle_spi_status(pData[0]); +} + +void TMC2130::_spi_tx_rx(const MotorParams ¶ms, uint8_t (&pData)[5]) { + hal::gpio::WritePin(params.csPin, hal::gpio::Level::low); + for (uint8_t i = 0; i < sizeof(pData); i++) + pData[i] = hal::spi::TxRx(params.spi, pData[i]); + hal::gpio::WritePin(params.csPin, hal::gpio::Level::high); +} + +void TMC2130::_handle_spi_status(uint8_t status) { + spi_status |= status & 0x03; /// update reset_flag and driver_error +} + } // namespace tmc2130 } // namespace hal diff --git a/src/hal/tmc2130.h b/src/hal/tmc2130.h index f01c2f3..610e1d1 100644 --- a/src/hal/tmc2130.h +++ b/src/hal/tmc2130.h @@ -1,6 +1,7 @@ #pragma once #include "../hal/gpio.h" #include "../hal/shr16.h" +#include "../hal/spi.h" namespace hal { @@ -16,6 +17,7 @@ enum MotorMode : uint8_t { }; struct MotorParams { + hal::spi::SPI_TypeDef *spi; uint8_t idx; ///< SHR16 index bool dirOn; ///< forward direction gpio::GPIO_pin csPin; ///< CS pin @@ -33,8 +35,31 @@ struct MotorCurrents { class TMC2130 { MotorMode mode; MotorCurrents currents; + uint8_t spi_status = 0; public: + enum class Registers : uint8_t { + /// General Configuration Registers + GCONF = 0x00, + GSTAT = 0x01, + IOIN = 0x04, + + /// Velocity Dependent Driver Feature Control Register Set + IHOLD_IRUN = 0x10, + TPOWERDOWN = 0x11, + TSTEP = 0x12, + TPWMTHRS = 0x13, + TCOOLTHRS = 0x14, + THIGH = 0x15, + + /// Motor Driver Registers + MSCNT = 0x6A, + CHOPCONF = 0x6C, + COOLCONF = 0x6D, + DRV_STATUS = 0x6F, + PWMCONF = 0x70, + }; + /// Constructor TMC2130(const MotorParams ¶ms, const MotorCurrents ¤ts, @@ -82,27 +107,15 @@ public: return gpio::ReadPin(params.sgPin) == gpio::Level::high; } - enum class Registers : uint8_t { - /// General Configuration Registers - GCONF = 0x00, - GSTAT = 0x01, - IOIN = 0x04, + /// Reads a driver register and updates the status flags + uint32_t ReadRegister(const MotorParams ¶ms, Registers reg); - /// Velocity Dependent Driver Feature Control Register Set - IHOLD_IRUN = 0x10, - TPOWERDOWN = 0x11, - TSTEP = 0x12, - TPWMTHRS = 0x13, - TCOOLTHRS = 0x14, - THIGH = 0x15, + /// Writes a driver register and updates the status flags + void WriteRegister(const MotorParams ¶ms, Registers reg, uint32_t data); - /// Motor Driver Registers - MSCNT = 0x6A, - CHOPCONF = 0x6C, - COOLCONF = 0x6D, - DRV_STATUS = 0x6F, - PWMCONF = 0x70, - }; +private: + void _spi_tx_rx(const MotorParams ¶ms, uint8_t (&pData)[5]); + void _handle_spi_status(uint8_t status); }; } // namespace tmc2130 diff --git a/src/modules/motion.h b/src/modules/motion.h index baaa00f..30b7c8f 100644 --- a/src/modules/motion.h +++ b/src/modules/motion.h @@ -42,7 +42,7 @@ static constexpr AxisParams axisParams[NUM_AXIS] = { // Pulley { .name = 'P', - .params = { .idx = Pulley, .dirOn = config::pulley.dirOn, .csPin = PULLEY_CS_PIN, .stepPin = PULLEY_STEP_PIN, .sgPin = PULLEY_SG_PIN, .uSteps = config::pulley.uSteps }, + .params = { .spi = config::TmcSpiBus, .idx = Pulley, .dirOn = config::pulley.dirOn, .csPin = PULLEY_CS_PIN, .stepPin = PULLEY_STEP_PIN, .sgPin = PULLEY_SG_PIN, .uSteps = config::pulley.uSteps }, .currents = { .vSense = config::pulley.vSense, .iRun = config::pulley.iRun, .iHold = config::pulley.iHold }, .mode = DefaultMotorMode(config::pulley), .jerk = unitToSteps(config::pulleyLimits.jerk), @@ -51,7 +51,7 @@ static constexpr AxisParams axisParams[NUM_AXIS] = { // Selector { .name = 'S', - .params = { .idx = Selector, .dirOn = config::selector.dirOn, .csPin = SELECTOR_CS_PIN, .stepPin = SELECTOR_STEP_PIN, .sgPin = SELECTOR_SG_PIN, .uSteps = config::selector.uSteps }, + .params = { .spi = config::TmcSpiBus, .idx = Selector, .dirOn = config::selector.dirOn, .csPin = SELECTOR_CS_PIN, .stepPin = SELECTOR_STEP_PIN, .sgPin = SELECTOR_SG_PIN, .uSteps = config::selector.uSteps }, .currents = { .vSense = config::selector.vSense, .iRun = config::selector.iRun, .iHold = config::selector.iHold }, .mode = DefaultMotorMode(config::selector), .jerk = unitToSteps(config::selectorLimits.jerk), @@ -60,7 +60,7 @@ static constexpr AxisParams axisParams[NUM_AXIS] = { // Idler { .name = 'I', - .params = { .idx = Idler, .dirOn = config::idler.dirOn, .csPin = IDLER_CS_PIN, .stepPin = IDLER_STEP_PIN, .sgPin = IDLER_SG_PIN, .uSteps = config::idler.uSteps }, + .params = { .spi = config::TmcSpiBus, .idx = Idler, .dirOn = config::idler.dirOn, .csPin = IDLER_CS_PIN, .stepPin = IDLER_STEP_PIN, .sgPin = IDLER_SG_PIN, .uSteps = config::idler.uSteps }, .currents = { .vSense = config::idler.vSense, .iRun = config::idler.iRun, .iHold = config::idler.iHold }, .mode = DefaultMotorMode(config::idler), .jerk = unitToSteps(config::idlerLimits.jerk),