Revise the TMC2130 interface for stepping

Change the TMC2130 class after starting to review the Motion API.
Move motor mode handling and stepping to this module.

Create a new MotorParams class for compile-time settings (currently
pins, direction, microstepping).

Keep other settings separate, so that we can pass constexpr expressions
while stepping and still get 1 instruction without overhead.
pull/41/head
Yuri D'Elia 2021-07-03 09:33:07 +02:00 committed by DRracer
parent ea8b335f8d
commit c78ed77ccc
1 changed files with 74 additions and 10 deletions

View File

@ -1,18 +1,82 @@
#pragma once #pragma once
#include "../hal/gpio.h"
/// TMC2130 interface #include "../hal/shr16.h"
/// There are multiple TMC2130 on our board, so there will be multiple instances of this class
/// @@TODO @leptun - design some lightweight TMC2130 interface
namespace hal { namespace hal {
class TMC2130 { /// TMC2130 interface
/// There are multiple TMC2130 on our board, so there will be multiple
/// instances of this class
/// @@TODO @leptun - design some lightweight TMC2130 interface
namespace tmc2130 {
public: enum MotorMode : uint8_t {
/// constructor - do some one-time init stuff here Stealth,
TMC2130(uint8_t id); Normal
/// (re)initialization of the chip
void Init();
}; };
struct MotorParams {
uint8_t idx; ///< SHR16 index
bool dirOn; ///< forward direction
gpio::GPIO_pin csPin; ///< CS pin
gpio::GPIO_pin stepPin; ///< step pin
gpio::GPIO_pin sgPin; ///< stallguard pin
uint8_t uSteps; ///< microstep resolution
};
struct MotorCurrents {
uint8_t iRun; ///< Running current
uint8_t iHold; ///< Holding current
};
class TMC2130 {
MotorMode mode;
MotorCurrents currents;
public:
/// constructor
TMC2130(const MotorParams& params,
const MotorCurrents& currents,
MotorMode mode);
/// (re)initialization of the chip
void Init(const MotorParams& params);
/// Get/Change the current motor mode
MotorMode Mode() { return mode; } const
void SetMode(MotorMode mode);
/// Get/Change the current motor currents
const MotorCurrents& Currents() { return currents; } const
void SetCurrents(const MotorCurrents& currents);
/// Return enabled state
bool Enabled() const;
/// Enable/Disable the motor
void SetEnabled(const MotorParams& params, bool enabled)
{
hal::shr16::shr16.SetTMCDir(params.idx, enabled);
}
/// Set direction
void SetDir(const MotorParams& params, bool dir)
{
hal::shr16::shr16.SetTMCDir(params.idx, dir ^ params.dirOn);
}
/// Step the motor
inline void Step(const MotorParams& params)
{
gpio::TogglePin(params.stepPin); // assumes DEDGE
}
/// Return SG state
inline bool Stall(const MotorParams& params)
{
return gpio::ReadPin(params.sgPin) == gpio::Level::high;
}
};
} // namespace tmc2130
} // namespace hal } // namespace hal