35#include "tmc9660_version.h"
46template <
typename Port>
47concept DeclaresGpioSet =
51template <
typename Port>
53 requires { ExactHook<Port, bool(
CtrlPin,
GpioSignal)
noexcept>::Take(&Port::GpioSet); };
56template <
typename Port>
57concept DeclaresGpioGet =
58 requires(Port& port,
bool& level) { port.GpioGet(
CtrlPin::Faultn, level); };
61template <
typename Port>
63 requires { ExactHook<Port, bool(
CtrlPin,
bool&)
noexcept>::Take(&Port::GpioGet); };
66template <
typename Port>
67concept DeclaresDelayUs =
requires(Port& port) { port.DelayUs(
static_cast<uint32_t
>(1)); };
71template <
typename Port>
72concept HasDelayUs =
requires { ExactHook<Port, void(uint32_t)
noexcept>::Take(&Port::DelayUs); };
75template <
typename Port>
76concept DeclaresLog =
requires(Port& port) { port.Log(
LogLevel::Info,
""); };
79template <
typename Port>
81 requires { ExactHook<Port, void(
LogLevel,
const char*)
noexcept>::Take(&Port::Log); };
85template <
typename Port>
86void DelayUs(Port& port, uint32_t us)
noexcept {
87 if constexpr (HasDelayUs<Port>) {
90 static_assert(kAlwaysFalse<Port>,
91 "tmc9660: the port must implement `void DelayUs(uint32_t us) noexcept`");
96template <
typename Port>
97void Log(Port& port,
LogLevel level,
const char* message)
noexcept {
98 if constexpr (HasLog<Port>) {
99 port.Log(level, message);
102 static_cast<void>(port);
103 static_cast<void>(level);
104 static_cast<void>(message);
169template <
typename Port>
207template <AnyPort Port>
209 static_assert(!detail::DeclaresGpioSet<Port> || detail::HasGpioSet<Port>,
210 "tmc9660: the optional GpioSet hook must be `bool GpioSet(CtrlPin, GpioSignal) "
211 "noexcept`, so that a "
212 "failed pin write is reported");
214 !detail::DeclaresGpioGet<Port> || detail::HasGpioGet<Port>,
215 "tmc9660: the optional GpioGet hook must be `bool GpioGet(CtrlPin, bool& high) noexcept`");
217 !detail::DeclaresDelayUs<Port> || detail::HasDelayUs<Port>,
218 "tmc9660: the DelayUs hook must be `void DelayUs(uint32_t us) noexcept`, so that no wait is "
221 !detail::DeclaresLog<Port> || detail::HasLog<Port>,
222 "tmc9660: the optional Log hook must be `void Log(LogLevel, const char* message) noexcept`");
231 : port_(port), config_(config), mode_(config.mode), bl_app_mode_(config.mode) {}
282 return {.
status = last_status_tmcl_, .op = last_op_, .value = last_value_};
633 template <typename F>
645 template <
typename F>
647 if (value > F::kMax) {
650 return UpdateReg(addr, F::kMask, F::Insert(0U, value));
661 uint32_t value)
noexcept;
683 Error blTransport(
BlCmd cmd, uint32_t value,
bool write, detail::BlReply& reply)
noexcept;
684 Error tmclTransport(
const detail::TmclCommand& c,
bool probe, detail::TmclReply& reply)
noexcept;
685 Error blExchangeSpi(
BlCmd cmd, uint32_t value,
bool write, detail::BlReply& reply)
noexcept;
686 Error blExchangeUart(
BlCmd cmd, uint32_t value, detail::BlReply& reply)
noexcept;
687 Error tmclRawSpi(
const detail::TmclCommand& c,
bool probe, detail::TmclReply& reply)
noexcept;
688 Error tmclRawUart(
const detail::TmclCommand& c, detail::TmclReply& reply)
noexcept;
689 Error tmclSpiFetch(detail::TmclReply& reply)
noexcept;
690 Error spiFirstCommand() noexcept;
691 Result<uint32_t> regSpiExchange(uint8_t block, uint16_t offset,
bool write, uint32_t data,
692 bool probe) noexcept;
693 Result<uint32_t> regUartExchange(uint8_t command, uint8_t block, uint16_t offset, uint32_t data,
694 bool probe) noexcept;
695 Error regSpiDatagram(const std::array<uint8_t, detail::kRegSpiLen>& req,
696 detail::RegSpiReply& out) noexcept;
697 Result<detail::TmclReply> tmcl(uint8_t op, uint16_t type, uint8_t motor, uint32_t value,
698 bool write,
bool accept_loaded) noexcept;
699 Result<detail::TmclReply> tmclRaw(const detail::TmclCommand& c,
bool probe,
700 bool accept_loaded) noexcept;
701 Result<uint32_t> regExchange(
bool write,
RegAddr addr, uint32_t data) noexcept;
702 Result<uint32_t> regExchangeRaw(uint8_t command, uint8_t block, uint16_t offset,
bool write,
703 uint32_t data,
bool probe) noexcept;
704 Error checkRegSpiReply(const detail::RegSpiReply& r) noexcept;
705 Error spiTransfer(std::
size_t len) noexcept;
706 Error uartExchange(std::
size_t tx_len, std::
size_t rx_len) noexcept;
710 Result<
void> fail(
Error e, const
char* message) noexcept;
712 void markReset() noexcept;
713 Result<
void> verifyIdentity() noexcept;
714 Result<
void> waitForIdentity() noexcept;
715 Result<
void> regBaseline() noexcept;
717 Result<
void> blSetup(
Bank bank, uint32_t address) noexcept;
718 Result<uint32_t> blReadWord(uint32_t address) noexcept;
719 Result<
void> blWriteWord(uint32_t address, uint32_t value) noexcept;
720 Result<
void> disableDriveParameter() noexcept;
721 Result<
void> startParameterMode() noexcept;
722 Result<
void> verifyTmclLayout() noexcept;
724 Error uartResync() noexcept;
725 Error faultnGuard() noexcept;
727 Result<
void> blLiveness(uint32_t value) noexcept;
728 Result<
void> disableDriveRegister() noexcept;
729 Result<
void> idleSafe(const detail::TmclCommand& stop) noexcept;
730 Result<
void> startPreconditions() noexcept;
731 Result<
void> hardwareReset() noexcept;
732 Result<
void> blWaitPll(const
BootConfig& config, std::
size_t index) noexcept;
737 void blStopUnknownApplication() noexcept;
738 Result<
void> blWriteBootMode(const
BootConfig& config,
bool start) noexcept;
740 Result<
bool> gdrvLatched() noexcept;
744 Result<
void> armCanary() noexcept;
745 Result<int64_t> readParamRaw(
Param p, uint8_t motor) noexcept;
746 Error energisingGuard(
RegAddr addr, uint32_t value) const noexcept;
751 bool initialized_{
false};
752 bool canary_armed_{
false};
753 bool reg_started_{
false};
754 bool defer_resync_{
false};
755 bool uart_dirty_{
false};
756 bool watchdog_at_start_{
false};
762 uint8_t reg_counter_{0};
763 uint8_t last_status_{0};
764 uint8_t last_status_tmcl_{0};
766 uint32_t last_value_{0};
767 std::array<uint8_t, 9> tx_{};
768 std::array<uint8_t, 9> rx_{};
774#include "../src/tmc9660.ipp"
The 64-byte configuration memory of the bootloader, as 16 little-endian 32-bit words.
Definition tmc9660_bootconfig.hpp:175
Driver for one TMC9660 on a platform port.
Definition tmc9660.hpp:208
Result< void > ScriptStop() noexcept
ApplStop.
LastReply GetLastReply() const noexcept
The last parameter-mode reply, as the chip sent it (no bus traffic).
Definition tmc9660.hpp:281
Result< uint32_t > GetChipId() noexcept
GetInfo type 0: the chip ID.
Result< void > Deinitialize() noexcept
Switch the gate driver off and mark the driver uninitialised. The chip is left as it is.
Result< void > BlOtpLoad() noexcept
OTP_LOAD: load the OTP configuration. (OTP_BURN is not exposed: it is permanent.)
Mode GetMode() const noexcept
Definition tmc9660.hpp:270
Result< void > SetTargetTorque(int32_t value) noexcept
SetParam of TARGET_TORQUE.
Definition tmc9660.hpp:490
Result< void > GateDriverStart(const GateDriverRegConfig &config) noexcept
Energise the gate driver in register mode, in the order of the register manual, with the guards compl...
Result< void > PowerDown(PowerDownTime time) noexcept
Put the chip into the power-down state for a time (global parameter 11). The gate driver is switched ...
Result< void > DisableSpi1Slave() noexcept
Erratum 5 workaround: disable the SPI1 slave (op 146, type 4, bank 9, value 0). Only call it when the...
Result< void > SetParam(Param param, int64_t value, uint8_t motor=0) noexcept
SAP: write an axis parameter, after checking access, range and coded set against Table 57.
Result< void > Service() noexcept
Poll the things that need no request: FAULTN when the port reads it, and in parameter mode a read of ...
Result< void > StoreParams() noexcept
STAP: store all storable parameters to the external memory.
Result< void > ReturnToBootloader() noexcept
Return to the bootloader (Boot command). The gate driver is switched off and COMMUTATION_MODE is read...
Result< uint32_t > ReadReg(RegAddr addr) noexcept
Read a register of the register map.
Result< int64_t > GetParam(Param param, uint8_t motor=0) noexcept
GAP: read an axis parameter.
Result< uint32_t > BlGetInfo(BlInfo info) noexcept
GET_INFO.
Result< BootConfig > BlReadConfig() noexcept
Read the 64-byte configuration memory.
Result< void > SetIo(uint8_t pin, bool high) noexcept
SIO: set a digital output.
Result< void > ScriptStep() noexcept
ApplStep.
Result< void > EnsureInitialized() noexcept
Start the driver, once: bus up, optional hardware reset, identity check in Config::mode.
bool IsInitialized() const noexcept
Definition tmc9660.hpp:262
Result< uint32_t > ReadField(RegAddr addr) noexcept
The value of one field.
Definition tmc9660.hpp:634
Result< void > WriteField(RegAddr addr, uint32_t value) noexcept
Set one field.
Definition tmc9660.hpp:646
Result< uint32_t > ReadRawParam(uint16_t type, uint8_t motor=0) noexcept
Single-shot raw read: one GAP with a parameter number taken as given (not looked up in the table,...
Result< void > EnableDrive(uint8_t commutation_mode) noexcept
Energise the gate driver in parameter mode, after the start-up guards.
TMC9660(Port &port, const Config &config={}) noexcept
Bind the driver to a port and store the configuration. No I/O, cannot fail.
Definition tmc9660.hpp:230
Result< void > MotorStop() noexcept
Stop the motor (MST). Parameter mode. Sent before a UART resync, and once more (after the resync) whe...
Result< int64_t > GetGlobal(GlobalBank bank, uint16_t number) noexcept
GGP.
uint8_t LastStatus() const noexcept
Definition tmc9660.hpp:275
Result< void > BlEraseFlashSector(uint32_t address, bool allow_partition_table=false) noexcept
Erase one 4 KiB sector of the SPI flash (SECTOR_ERASE 0x20). Refused unless the flash is configured a...
Result< int32_t > GetIo(uint8_t pin, bool analog) noexcept
GIO.
Result< bool > BlMemoryStatus(Bank bank, MemQuery query) noexcept
Ask about an external memory.
Result< uint32_t > RegRamDebug(uint8_t subcommand, uint8_t index, bool write, uint32_t value) noexcept
RAMDebug access in register mode.
Result< void > ScriptReset() noexcept
ApplReset.
Result< void > DisableDrive() noexcept
Switch the gate driver off. Never refused: whatever the mode, state or earlier error,...
TMC9660(const TMC9660 &)=delete
One driver object per chip.
Result< int32_t > ScriptStatus() noexcept
GetStatus of the script.
Result< void > UpdateReg(RegAddr addr, uint32_t mask, uint32_t value) noexcept
Read-modify-write of the bits in mask; the mask must lie within the register's writable bits.
const Config & GetConfig() const noexcept
Definition tmc9660.hpp:285
Result< void > SetTargetPosition(int32_t value) noexcept
SetParam of TARGET_POSITION.
Definition tmc9660.hpp:506
Result< void > ScriptDownload(std::span< const TmclInstruction > script) noexcept
Download a script: DownloadStart, every instruction (each acknowledged with CMD_LOADED),...
Result< void > BlWriteConfig(const BootConfig &config, const BlWriteOptions &options={}) noexcept
Write a configuration and verify it.
Result< void > SetTargetVelocity(int32_t value) noexcept
SetParam of TARGET_VELOCITY.
Definition tmc9660.hpp:498
Result< int32_t > RamDebug(RamDebugType type, uint8_t motor_bank, int32_t value) noexcept
RAMDebug command.
Result< int32_t > ReferenceSearchCommand(ReferenceSearch action) noexcept
RFS.
Result< void > WriteReg(RegAddr addr, uint32_t value) noexcept
Write a register. Refused (nothing sent): read-only registers (InvalidArgument) and any write that se...
Result< uint32_t > ScriptReadMem(uint32_t address) noexcept
ReadMem: the script instruction word at address.
TMC9660 & operator=(const TMC9660 &)=delete
One driver object per chip.
Result< void > ScriptRun(bool from_address, uint32_t address) noexcept
ApplRun: run the script. The chip executes what is in its script memory, a script stored earlier or o...
Result< void > ClearFlags(Param flags, uint32_t mask) noexcept
SAP of a flag word: write 1 to the bits to clear.
Result< uint32_t > GetVersion() noexcept
GetInfo type 1: the version word.
Result< void > BlWrite32(Bank bank, uint32_t address, uint32_t value) noexcept
Write a 32-bit word of an external memory bank (SPI flash or I2C EEPROM).
Result< uint32_t > BlRead32(Bank bank, uint32_t address) noexcept
Read a 32-bit word of a bootloader memory bank.
Result< void > SetGlobal(GlobalBank bank, uint16_t number, int64_t value) noexcept
SGP, checked against Tables 61 to 63.
Result< void > BlStartApplication(BootMode target) noexcept
Read the configuration, set BOOT_MODE and START_MOTOR_CTRL and write it (BlWriteConfig).
Satisfied by a port that derives from exactly one of SpiInterface and UartInterface.
Definition tmc9660.hpp:170
Satisfied by a class that derives from SpiInterface<Port> and implements the required hooks with exac...
Definition tmc9660_spi_interface.hpp:105
Satisfied by a class that derives from UartInterface<Port> and implements the required hooks with exa...
Definition tmc9660_uart_interface.hpp:82
BlCmd
Bootloader command codes.
Definition tmc9660_types.hpp:269
RamDebugType
RAMDebug types (Table 16).
Definition tmc9660_types.hpp:375
CtrlPin
The control pins of the chip that the optional GpioSet() and GpioGet() hooks may drive.
Definition tmc9660_types.hpp:188
LogLevel
Severity passed to the optional Log() hook.
Definition tmc9660_types.hpp:172
Error
Why a call failed. Every call of the driver returns one of these in its Result.
Definition tmc9660_types.hpp:35
BootMode
BOOT_MODE of the boot configuration (offset 8, bits 0-1).
Definition tmc9660_types.hpp:218
TmclLayout
Where the type and the motor or bank bytes of a TMCL frame put their bits (ambiguity A2).
Definition tmc9660_types.hpp:428
ReferenceSearch
RFS sub-commands.
Definition tmc9660_types.hpp:393
BlInfo
GET_INFO selectors of the bootloader.
Definition tmc9660_types.hpp:235
Mode
The protocol the chip speaks on its host interface.
Definition tmc9660_types.hpp:202
BusStatus
What a UART port reports for one exchange.
Definition tmc9660_types.hpp:180
Tmcl
TMCL operation codes (Table 18 of the parameter mode manual).
Definition tmc9660_types.hpp:299
Bank
External memory banks of the bootloader (SET_BANK).
Definition tmc9660_types.hpp:227
GpioSignal
Logical level of a control pin; the port maps it to the physical polarity.
Definition tmc9660_types.hpp:196
@ Reset
RSTN, active low on the package pin; ACTIVE holds the chip in reset.
@ Faultn
FAULTN, an input; GpioGet reports the pin level (low: asserted).
@ Info
Lifecycle events: initialised, mode changes.
@ InvalidArgument
An argument or a Config field is out of range; nothing was sent.
@ MotorHigh
Byte 3 (SPI byte 2): motor << 4 | type[11:8]; the byte before holds type[7:0].
@ Bootloader
The bootloader: GET_INFO, memory access, configuration.
@ FactoryDefault
Restore the factory defaults.
@ Config
The 64-byte configuration memory at 0x00020000.
@ ACTIVE
The pin function is asserted.
The TMC9660 driver: everything it declares lives here; internals in detail.
Definition tmc9660.hpp:37
Param
The axis parameters, by name; the value is the number sent as the TMCL type.
Definition tmc9660_params.hpp:40
@ TARGET_VELOCITY
Target velocity value.
@ TARGET_POSITION
Target position value.
@ TARGET_TORQUE
Target torque value.
MemQuery
Queries about the external memory of the bootloader.
Definition tmc9660.hpp:117
@ Connected
MEM_IS_CONNECTED.
@ Configured
MEM_IS_CONFIGURED.
PowerDownTime
Power-down durations of TMC9660::PowerDown() (global parameter 11).
Definition tmc9660.hpp:124
GlobalBank
Banks of global parameters.
Definition tmc9660_globals.hpp:28
Options of TMC9660::BlWriteConfig().
Definition tmc9660.hpp:111
bool allow_ldo_change
Allow a change of the LDO word: it changes VEXT1 and VEXT2 at once.
Definition tmc9660.hpp:112
Configuration of the driver. The defaults are the chip's.
Definition tmc9660_types.hpp:461
Gate-driver start-up of register mode: the words of the manual's start sequence.
Definition tmc9660.hpp:144
uint32_t gdrv_hw
MCC_GDRV_HW: bias, charge pump, comparators and bridge enables.
Definition tmc9660.hpp:150
uint32_t prot_en
MCC_GDRV_PROT_EN after the start: protection handling (all on). 0 runs the stage unprotected: say so ...
Definition tmc9660.hpp:163
uint32_t gdrv_ocp_uvw
Definition tmc9660.hpp:159
uint32_t gdrv_prot
Definition tmc9660.hpp:155
uint32_t status_en
MCC_GDRV_STATUS_EN after the start: fault reporting (reset value: all on).
Definition tmc9660.hpp:161
uint32_t gdrv_ocp_y2
MCC_GDRV_OCP_Y2, as gdrv_ocp_uvw.
Definition tmc9660.hpp:160
uint32_t pwm_enable
MCC_PWM_CONFIG written first: ENABLE_UX1/VX2/WY1/Y2 and CHOP 1 are added.
Definition tmc9660.hpp:145
uint32_t gdrv_bbm
MCC_GDRV_BBM: break-before-make times.
Definition tmc9660.hpp:149
uint32_t gdrv_timing
MCC_GDRV_TIMING.
Definition tmc9660.hpp:148
uint32_t gdrv_cfg
MCC_GDRV_CFG: gate current and adaptive mode.
Definition tmc9660.hpp:147
uint8_t chop_run
Definition tmc9660.hpp:152
The last parameter-mode reply as received (see TMC9660::GetLastReply()).
Definition tmc9660.hpp:173
uint8_t op
Echoed command number.
Definition tmc9660.hpp:175
uint8_t status
TMCL status code.
Definition tmc9660.hpp:174
uint32_t value
Reply value.
Definition tmc9660.hpp:176
Address of a register: its block and its offset in the block.
Definition tmc9660_registers.hpp:28
The value of a call, or why there is none.
Definition tmc9660_types.hpp:112
constexpr bool ok() const noexcept
Definition tmc9660_types.hpp:117
static constexpr Result Success(T v) noexcept
A successful result.
Definition tmc9660_types.hpp:130
T value
The value; T{} when the call failed.
Definition tmc9660_types.hpp:113
static constexpr Result Failure(Error e) noexcept
A failed result.
Definition tmc9660_types.hpp:138
One instruction of a script download.
Definition tmc9660.hpp:136
uint8_t motor
Motor or bank (4 bits).
Definition tmc9660.hpp:139
int32_t value
Value.
Definition tmc9660.hpp:140
uint16_t type
Type or parameter number (12 bits).
Definition tmc9660.hpp:138
Tmcl op
Command number.
Definition tmc9660.hpp:137
The bootloader's configuration memory (64 bytes, datasheet "Bootloader Configuration") as a typed Boo...
Compile-time helpers of the TMC9660 driver: byte copy, exact hook matching, typed register fields and...
Pure frame encoders and decoders of the three TMC9660 protocols: bootloader, parameter mode (TMCL) an...
The global parameters of parameter mode (SGP, GGP): banks 0, 2 and 3 (Tables 61 to 63).
The axis parameters of parameter mode (SAP, GAP, STAP, AAP): number, range, default and access of eve...
The registers of register mode: block, offset, reset value and access masks of every register of the ...
The SPI port of the TMC9660 driver: the CRTP base, the SpiPort concept and the bus requirements.
Public types of the TMC9660 driver: errors, Result<T>, the protocol enumerations, Config and its mode...
The UART port of the TMC9660 driver: the CRTP base, the UartPort concept and the bus requirements.