tmc2130: Initial spi communication

pull/76/head
Alex Voinea 2021-07-17 14:09:31 +03:00 committed by DRracer
parent e2ba71fc03
commit 1022603f9d
3 changed files with 62 additions and 22 deletions

View File

@ -13,5 +13,32 @@ void TMC2130::Init(const MotorParams &params) {
// TODO
}
uint32_t TMC2130::ReadRegister(const MotorParams &params, 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 &params, 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 &params, 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

View File

@ -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 &params,
const MotorCurrents &currents,
@ -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 &params, 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 &params, 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 &params, uint8_t (&pData)[5]);
void _handle_spi_status(uint8_t status);
};
} // namespace tmc2130

View File

@ -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<P_speed_t>(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<S_speed_t>(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<I_speed_t>(config::idlerLimits.jerk),