Initial commit
This commit is contained in:
492
Clean_TMC2209/lib/tmc/ic/TMC2209/TMC2209.c
Normal file
492
Clean_TMC2209/lib/tmc/ic/TMC2209/TMC2209.c
Normal file
@@ -0,0 +1,492 @@
|
||||
/*******************************************************************************
|
||||
* Copyright © 2019 TRINAMIC Motion Control GmbH & Co. KG
|
||||
* (now owned by Analog Devices Inc.),
|
||||
*
|
||||
* Copyright © 2023 Analog Devices Inc. All Rights Reserved. This software is
|
||||
* proprietary & confidential to Analog Devices, Inc. and its licensors.
|
||||
*******************************************************************************/
|
||||
|
||||
|
||||
#include "TMC2209.h"
|
||||
#include "tmc/hal/UART.h"
|
||||
#include "tmc/helpers/CRC.h"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint8_t table[256];
|
||||
uint8_t polynomial;
|
||||
bool isReflected;
|
||||
} CRCTypeDef;
|
||||
|
||||
CRCTypeDef CRCTables[CRC_TABLE_COUNT] = { 0 };
|
||||
|
||||
static uint8_t flipByte(uint8_t value);
|
||||
static uint32_t flipBitsInBytes(uint32_t value);
|
||||
|
||||
/* This function generates the Lookup table used for CRC calculations.
|
||||
*
|
||||
* Arguments:
|
||||
* uint8_t polynomial: The CRC polynomial for which the table will be generated.
|
||||
* bool isReflected: Indicator whether the CRC table will be reflected or not.
|
||||
* uint8_t index: The index of the table to be filled.
|
||||
*
|
||||
* How it works:
|
||||
* A CRC calculation of a byte can be done by taking the byte to be CRC'd,
|
||||
* shifting it left by one (appending a 0) and - if a 1 has been shifted out -
|
||||
* XOR-ing in the CRC polynomial. After 8 iterations the result will be the
|
||||
* CRC of the Byte.
|
||||
*
|
||||
* The function below does this in a compact way, by using all 4 bytes of a
|
||||
* uint32_t to do 4 separate CRC bytes at once.
|
||||
* For this to work without the Byte shifting interfering with adjacent bytes,
|
||||
* the polynomial has the 8th bit (0x100) set. That way, if the shifted-out bit
|
||||
* is 1, the following XOR-ing with the CRC polynomial will set that 1 to a 0,
|
||||
* resulting in the shifted-in 0 for the adjacent byte.
|
||||
* This process will go from the the lowest to the highest byte, resulting in
|
||||
* fully independent byte-wise CRC calculations. For the highest byte, the value
|
||||
* of the shifted-out byte needs to be stored before shifting the bytes (isMSBSet).
|
||||
*
|
||||
* The for-loop that iterates over all uint8_t values starts out with the
|
||||
* uint8_t values 3 to 0 stored in one uint32_t: 0x03020100
|
||||
* for each iteration each uint8_t value will increase by 4..
|
||||
* 0 -> 4 -> 8 -> C -> ...
|
||||
* 1 -> 5 -> 9 -> D -> ...
|
||||
* 2 -> 6 -> A -> E -> ...
|
||||
* 3 -> 7 -> B -> F -> ...
|
||||
* ..resulting in an increase of the uint32_t by 0x04040404:
|
||||
* 0x03020100 -> 0x07060504 -> 0x0B0A0908 -> 0x0F0E0D0C -> ...
|
||||
* The loop ends as soon as we have iterated over all uint8_t values.
|
||||
* We detect that by looking for the byte-wise overflow into the next byte:
|
||||
* 0xFFFEFDFC <- last uint32_t value to be calculated
|
||||
* 0xFF, 0xFE, 0xFD, 0xFC <- the corresponding uint8_t values
|
||||
* 0x103, 0x102, 0x101, 0x100 <- incremented uint8_t values (overflow into the next byte!)
|
||||
* 0x04030200 <- uint32_t value with the overflowed bytes
|
||||
*
|
||||
* We have the lower uint8_t values at the lower bytes of the uint32_t.
|
||||
* This allows us to simply store the lowest byte of the uint32_t,
|
||||
* right-shift the uint32_t by 8 and increment the table pointer.
|
||||
* After 4 iterations of that all 4 bytes of the uint32_t are stored in the table.
|
||||
*/
|
||||
uint8_t tmc_fillCRC8Table(uint8_t polynomial, bool isReflected, uint8_t index)
|
||||
{
|
||||
uint32_t CRCdata;
|
||||
// Helper pointer for traversing the result table
|
||||
uint8_t *table;
|
||||
|
||||
if(index >= CRC_TABLE_COUNT)
|
||||
return 0;
|
||||
|
||||
CRCTables[index].polynomial = polynomial;
|
||||
CRCTables[index].isReflected = isReflected;
|
||||
table = &CRCTables[index].table[0];
|
||||
|
||||
// Extend the polynomial to correct byte MSBs shifting into next bytes
|
||||
uint32_t poly = (uint32_t) polynomial | 0x0100;
|
||||
|
||||
// Iterate over all 256 possible uint8_t values, compressed into a uint32_t (see detailed explanation above)
|
||||
uint32_t i;
|
||||
for(i = 0x03020100; i != 0x04030200; i+=0x04040404)
|
||||
{
|
||||
// For reflected table: Flip the bits of each input byte
|
||||
CRCdata = (isReflected)? flipBitsInBytes(i) : i;
|
||||
|
||||
// Iterate over 8 Bits
|
||||
int j;
|
||||
for(j = 0; j < 8; j++)
|
||||
{
|
||||
// Store value of soon-to-be shifted out byte
|
||||
uint8_t isMSBSet = (CRCdata & 0x80000000)? 1:0;
|
||||
|
||||
// CRC Shift
|
||||
CRCdata <<= 1;
|
||||
|
||||
// XOR the bytes when required, lowest to highest
|
||||
CRCdata ^= (CRCdata & 0x00000100)? (poly ) : 0;
|
||||
CRCdata ^= (CRCdata & 0x00010000)? (poly << 8 ) : 0;
|
||||
CRCdata ^= (CRCdata & 0x01000000)? (poly << 16) : 0;
|
||||
CRCdata ^= (isMSBSet)? (poly << 24) : 0;
|
||||
}
|
||||
|
||||
// For reflected table: Flip the bits of each output byte
|
||||
CRCdata = (isReflected)? flipBitsInBytes(CRCdata) : CRCdata;
|
||||
// Store the CRC result bytes in the table array
|
||||
*table++ = (uint8_t) CRCdata;
|
||||
CRCdata >>= 8;
|
||||
*table++ = (uint8_t) CRCdata;
|
||||
CRCdata >>= 8;
|
||||
*table++ = (uint8_t) CRCdata;
|
||||
CRCdata >>= 8;
|
||||
*table++ = (uint8_t) CRCdata;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* This function calculates the CRC from a data buffer
|
||||
*
|
||||
* Arguments:
|
||||
* uint8_t *data: A pointer to the data that will be CRC'd.
|
||||
* uint32_t bytes: The length of the data buffer.
|
||||
* uint8_t index: The index of the CRC table to be used.
|
||||
*/
|
||||
uint8_t tmc_CRC8(uint8_t *data, uint32_t bytes, uint8_t index)
|
||||
{
|
||||
uint8_t result = 0;
|
||||
uint8_t *table;
|
||||
|
||||
if(index >= CRC_TABLE_COUNT)
|
||||
return 0;
|
||||
|
||||
table = &CRCTables[index].table[0];
|
||||
|
||||
while(bytes--)
|
||||
result = table[result ^ *data++];
|
||||
|
||||
return (CRCTables[index].isReflected)? flipByte(result) : result;
|
||||
}
|
||||
|
||||
uint8_t tmc_tableGetPolynomial(uint8_t index)
|
||||
{
|
||||
if(index >= CRC_TABLE_COUNT)
|
||||
return 0;
|
||||
|
||||
return CRCTables[index].polynomial;
|
||||
}
|
||||
|
||||
bool tmc_tableIsReflected(uint8_t index)
|
||||
{
|
||||
if(index >= CRC_TABLE_COUNT)
|
||||
return false;
|
||||
|
||||
return CRCTables[index].isReflected;
|
||||
}
|
||||
|
||||
// Helper functions
|
||||
static uint8_t flipByte(uint8_t value)
|
||||
{
|
||||
// swap odd and even bits
|
||||
value = ((value >> 1) & 0x55) | ((value & 0x55) << 1);
|
||||
// swap consecutive pairs
|
||||
value = ((value >> 2) & 0x33) | ((value & 0x33) << 2);
|
||||
// swap nibbles ...
|
||||
value = ((value >> 4) & 0x0F) | ((value & 0x0F) << 4);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/* This helper function switches all bits within each byte.
|
||||
* The byte order remains the same:
|
||||
* [b31 b30 b29 b28 b27 b26 b25 b24 .. b7 b6 b5 b4 b3 b2 b1 b0]
|
||||
* ||
|
||||
* \||/
|
||||
* \/
|
||||
* [b24 b25 b26 b27 b28 b29 b30 b31 .. b0 b1 b2 b3 b4 b5 b6 b7]
|
||||
*/
|
||||
static uint32_t flipBitsInBytes(uint32_t value)
|
||||
{
|
||||
// swap odd and even bits
|
||||
value = ((value >> 1) & 0x55555555) | ((value & 0x55555555) << 1);
|
||||
// swap consecutive pairs
|
||||
value = ((value >> 2) & 0x33333333) | ((value & 0x33333333) << 2);
|
||||
// swap nibbles ...
|
||||
value = ((value >> 4) & 0x0F0F0F0F) | ((value & 0x0F0F0F0F) << 4);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
typedef union {
|
||||
TMC2209TypeDef tmc2209;
|
||||
} DriverBoards;
|
||||
|
||||
DriverBoards driverBoards;
|
||||
#define TMC2209_CRC(data, length) tmc_CRC8(data, length, 1)
|
||||
|
||||
#define TMC2209 (driverBoards.tmc2209)
|
||||
#define BUFFER_SIZE 32
|
||||
#define INTR_PRI 6
|
||||
#define UART_TIMEOUT_VALUE 10
|
||||
#define WRITE_READ_DELAY 10
|
||||
// #include "tmc/boards/Board.h"
|
||||
// #include "tmc/tmc/StepDir.h"
|
||||
// // => UART wrapper
|
||||
// extern void tmc2209_readWriteArray(uint8_t channel, uint8_t *data, size_t writeLength, size_t readLength);
|
||||
// // <= UART wrapper
|
||||
|
||||
// // => CRC wrapper
|
||||
// extern uint8_t tmc2209_CRC8(uint8_t *data, size_t length);
|
||||
// // <= CRC wrapper
|
||||
|
||||
static UART_Config *TMC2209_UARTChannel;
|
||||
|
||||
static inline TMC2209TypeDef *motorToIC(uint8_t motor)
|
||||
{
|
||||
UNUSED(motor);
|
||||
|
||||
return &TMC2209;
|
||||
}
|
||||
|
||||
static inline UART_Config *channelToUART(uint8_t channel)
|
||||
{
|
||||
UNUSED(channel);
|
||||
|
||||
return TMC2209_UARTChannel;
|
||||
}
|
||||
|
||||
// => UART wrapper
|
||||
|
||||
int32_t UART_readWrite(UART_Config *uart, uint8_t *data, size_t writeLength, uint8_t readLength)
|
||||
{
|
||||
uart->rxtx.clearBuffers();
|
||||
uart->rxtx.txN(data, writeLength);
|
||||
/* Workaround: Give the UART time to send. Otherwise another write/readRegister can do clearBuffers()
|
||||
* before we're done. This currently is an issue with the IDE when using the Register browser and the
|
||||
* periodic refresh of values gets requested right after the write request.
|
||||
*/
|
||||
wait(WRITE_READ_DELAY);
|
||||
|
||||
// Abort early if no data needs to be read back
|
||||
if (readLength <= 0)
|
||||
return 0;
|
||||
|
||||
// Wait for reply with timeout limit
|
||||
uint32_t timestamp = 0;
|
||||
// while(uart->rxtx.bytesAvailable() < readLength)
|
||||
// {
|
||||
// if(timeSince(timestamp) > UART_TIMEOUT_VALUE)
|
||||
// {
|
||||
// Abort on timeout
|
||||
// return -1;
|
||||
// }
|
||||
// }
|
||||
|
||||
uart->rxtx.rxN(data, readLength);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void tmc2209_readWriteArray(uint8_t channel, uint8_t *data, size_t writeLength, size_t readLength)
|
||||
{
|
||||
UART_readWrite(channelToUART(channel), data, writeLength, readLength);
|
||||
}
|
||||
// <= UART wrapper
|
||||
|
||||
// => CRC wrapper
|
||||
// Return the CRC8 of [length] bytes of data stored in the [data] array.
|
||||
uint8_t tmc2209_CRC8(uint8_t *data, size_t length)
|
||||
{
|
||||
return TMC2209_CRC(data, length);
|
||||
}
|
||||
// <= CRC wrapper
|
||||
|
||||
void tmc2209_writeRegister(uint8_t motor, uint16_t address, int32_t value)
|
||||
{
|
||||
tmc2209_writeInt(motorToIC(motor), (uint8_t) address, value);
|
||||
|
||||
}
|
||||
|
||||
void tmc2209_readRegister(uint8_t motor, uint16_t address, int32_t *value)
|
||||
{
|
||||
*value = tmc2209_readInt(motorToIC(motor), (uint8_t) address);
|
||||
}
|
||||
|
||||
|
||||
void tmc2209_writeInt(TMC2209TypeDef *tmc2209, uint8_t address, int32_t value)
|
||||
{
|
||||
uint8_t data[8];
|
||||
|
||||
data[0] = 0x05;
|
||||
data[1] = tmc2209->slaveAddress;
|
||||
data[2] = address | TMC_WRITE_BIT;
|
||||
data[3] = (value >> 24) & 0xFF;
|
||||
data[4] = (value >> 16) & 0xFF;
|
||||
data[5] = (value >> 8 ) & 0xFF;
|
||||
data[6] = (value ) & 0xFF;
|
||||
data[7] = tmc2209_CRC8(data, 7);
|
||||
|
||||
tmc2209_readWriteArray(tmc2209->config->channel, &data[0], 8, 0);
|
||||
|
||||
// Write to the shadow register and mark the register dirty
|
||||
address = TMC_ADDRESS(address);
|
||||
tmc2209->config->shadowRegister[address] = value;
|
||||
tmc2209->registerAccess[address] |= TMC_ACCESS_DIRTY;
|
||||
}
|
||||
|
||||
int32_t tmc2209_readInt(TMC2209TypeDef *tmc2209, uint8_t address)
|
||||
{
|
||||
uint8_t data[8] = { 0 };
|
||||
|
||||
address = TMC_ADDRESS(address);
|
||||
|
||||
if (!TMC_IS_READABLE(tmc2209->registerAccess[address]))
|
||||
return tmc2209->config->shadowRegister[address];
|
||||
|
||||
data[0] = 0x05;
|
||||
data[1] = tmc2209->slaveAddress;
|
||||
data[2] = address;
|
||||
data[3] = tmc2209_CRC8(data, 3);
|
||||
|
||||
tmc2209_readWriteArray(tmc2209->config->channel, data, 4, 8);
|
||||
|
||||
// Byte 0: Sync nibble correct?
|
||||
if (data[0] != 0x05)
|
||||
return 0;
|
||||
|
||||
// Byte 1: Master address correct?
|
||||
if (data[1] != 0xFF)
|
||||
return 0;
|
||||
|
||||
// Byte 2: Address correct?
|
||||
if (data[2] != address)
|
||||
return 0;
|
||||
|
||||
// Byte 7: CRC correct?
|
||||
if (data[7] != tmc2209_CRC8(data, 7))
|
||||
return 0;
|
||||
|
||||
return ((uint32_t)data[3] << 24) | ((uint32_t)data[4] << 16) | (data[5] << 8) | data[6];
|
||||
}
|
||||
|
||||
void tmc2209_init(TMC2209TypeDef *tmc2209, uint8_t channel, uint8_t slaveAddress, ConfigurationTypeDef *tmc2209_config, const int32_t *registerResetState)
|
||||
{
|
||||
tmc2209->slaveAddress = slaveAddress;
|
||||
|
||||
tmc2209->config = tmc2209_config;
|
||||
tmc2209->config->callback = NULL;
|
||||
tmc2209->config->channel = channel;
|
||||
tmc2209->config->configIndex = 0;
|
||||
tmc2209->config->state = CONFIG_READY;
|
||||
|
||||
for(size_t i = 0; i < TMC2209_REGISTER_COUNT; i++)
|
||||
{
|
||||
tmc2209->registerAccess[i] = tmc2209_defaultRegisterAccess[i];
|
||||
tmc2209->registerResetState[i] = registerResetState[i];
|
||||
}
|
||||
}
|
||||
|
||||
static void writeConfiguration(TMC2209TypeDef *tmc2209)
|
||||
{
|
||||
uint8_t *ptr = &tmc2209->config->configIndex;
|
||||
const int32_t *settings;
|
||||
|
||||
if(tmc2209->config->state == CONFIG_RESTORE)
|
||||
{
|
||||
settings = tmc2209->config->shadowRegister;
|
||||
// Find the next restorable register
|
||||
while((*ptr < TMC2209_REGISTER_COUNT) && !TMC_IS_RESTORABLE(tmc2209->registerAccess[*ptr]))
|
||||
{
|
||||
(*ptr)++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
settings = tmc2209->registerResetState;
|
||||
// Find the next resettable register
|
||||
while((*ptr < TMC2209_REGISTER_COUNT) && !TMC_IS_RESETTABLE(tmc2209->registerAccess[*ptr]))
|
||||
{
|
||||
(*ptr)++;
|
||||
}
|
||||
}
|
||||
|
||||
if(*ptr < TMC2209_REGISTER_COUNT)
|
||||
{
|
||||
tmc2209_writeInt(tmc2209, *ptr, settings[*ptr]);
|
||||
(*ptr)++;
|
||||
}
|
||||
else // Finished configuration
|
||||
{
|
||||
if(tmc2209->config->callback)
|
||||
{
|
||||
((tmc2209_callback)tmc2209->config->callback)(tmc2209, tmc2209->config->state);
|
||||
}
|
||||
|
||||
tmc2209->config->state = CONFIG_READY;
|
||||
}
|
||||
}
|
||||
|
||||
void tmc2209_periodicJob(TMC2209TypeDef *tmc2209, uint32_t tick)
|
||||
{
|
||||
UNUSED(tick);
|
||||
|
||||
if(tmc2209->config->state != CONFIG_READY)
|
||||
{
|
||||
writeConfiguration(tmc2209);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void tmc2209_setRegisterResetState(TMC2209TypeDef *tmc2209, const int32_t *resetState)
|
||||
{
|
||||
for(size_t i = 0; i < TMC2209_REGISTER_COUNT; i++)
|
||||
tmc2209->registerResetState[i] = resetState[i];
|
||||
}
|
||||
|
||||
void tmc2209_setCallback(TMC2209TypeDef *tmc2209, tmc2209_callback callback)
|
||||
{
|
||||
tmc2209->config->callback = (tmc_callback_config) callback;
|
||||
}
|
||||
|
||||
uint8_t tmc2209_reset(TMC2209TypeDef *tmc2209)
|
||||
{
|
||||
if(tmc2209->config->state != CONFIG_READY)
|
||||
return false;
|
||||
|
||||
// Reset the dirty bits and wipe the shadow registers
|
||||
for(size_t i = 0; i < TMC2209_REGISTER_COUNT; i++)
|
||||
{
|
||||
tmc2209->registerAccess[i] &= ~TMC_ACCESS_DIRTY;
|
||||
tmc2209->config->shadowRegister[i] = 0;
|
||||
}
|
||||
|
||||
tmc2209->config->state = CONFIG_RESET;
|
||||
tmc2209->config->configIndex = 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t tmc2209_restore(TMC2209TypeDef *tmc2209)
|
||||
{
|
||||
if(tmc2209->config->state != CONFIG_READY)
|
||||
return false;
|
||||
|
||||
tmc2209->config->state = CONFIG_RESTORE;
|
||||
tmc2209->config->configIndex = 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t tmc2209_get_slave(TMC2209TypeDef *tmc2209)
|
||||
{
|
||||
return tmc2209->slaveAddress;
|
||||
}
|
||||
|
||||
void tmc2209_set_slave(TMC2209TypeDef *tmc2209, uint8_t slaveAddress)
|
||||
{
|
||||
tmc2209->slaveAddress = slaveAddress;
|
||||
}
|
||||
100
Clean_TMC2209/lib/tmc/ic/TMC2209/TMC2209.h
Normal file
100
Clean_TMC2209/lib/tmc/ic/TMC2209/TMC2209.h
Normal file
@@ -0,0 +1,100 @@
|
||||
/*******************************************************************************
|
||||
* Copyright © 2019 TRINAMIC Motion Control GmbH & Co. KG
|
||||
* (now owned by Analog Devices Inc.),
|
||||
*
|
||||
* Copyright © 2023 Analog Devices Inc. All Rights Reserved. This software is
|
||||
* proprietary & confidential to Analog Devices, Inc. and its licensors.
|
||||
*******************************************************************************/
|
||||
|
||||
|
||||
#ifndef TMC_IC_TMC2209_H_
|
||||
#define TMC_IC_TMC2209_H_
|
||||
|
||||
#include "tmc/helpers/Constants.h"
|
||||
#include "tmc/helpers/API_Header.h"
|
||||
#include "TMC2209_Register.h"
|
||||
#include "TMC2209_Constants.h"
|
||||
#include "TMC2209_Fields.h"
|
||||
|
||||
// Helper macros
|
||||
#define TMC2209_FIELD_READ(tdef, address, mask, shift) \
|
||||
FIELD_GET(tmc2209_readInt(tdef, address), mask, shift)
|
||||
#define TMC2209_FIELD_UPDATE(tdef, address, mask, shift, value) \
|
||||
(tmc2209_writeInt(tdef, address, FIELD_SET(tmc2209_readInt(tdef, address), mask, shift, value)))
|
||||
|
||||
// Usage note: use 1 TypeDef per IC
|
||||
typedef struct {
|
||||
ConfigurationTypeDef *config;
|
||||
|
||||
int32_t registerResetState[TMC2209_REGISTER_COUNT];
|
||||
uint8_t registerAccess[TMC2209_REGISTER_COUNT];
|
||||
|
||||
uint8_t slaveAddress;
|
||||
} TMC2209TypeDef;
|
||||
|
||||
typedef void (*tmc2209_callback)(TMC2209TypeDef*, ConfigState);
|
||||
|
||||
// Default Register values
|
||||
#define R00 0x00000040 // GCONF
|
||||
#define R10 0x00071703 // IHOLD_IRUN
|
||||
#define R11 0x00000014 // TPOWERDOWN
|
||||
#define R6C 0x10000053 // CHOPCONF
|
||||
#define R70 0xC10D0024 // PWMCONF
|
||||
|
||||
// Register access permissions:
|
||||
// 0x00: none (reserved)
|
||||
// 0x01: read
|
||||
// 0x02: write
|
||||
// 0x03: read/write
|
||||
// 0x13: read/write, separate functions/values for reading or writing
|
||||
// 0x23: read/write, flag register (write to clear)
|
||||
// 0x42: write, has hardware presets on reset
|
||||
static const uint8_t tmc2209_defaultRegisterAccess[TMC2209_REGISTER_COUNT] =
|
||||
{
|
||||
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
|
||||
0x03, 0x23, 0x01, 0x02, 0x02, 0x01, 0x01, 0x03, ____, ____, ____, ____, ____, ____, ____, ____, // 0x00 - 0x0F
|
||||
0x02, 0x02, 0x01, 0x02, 0x02, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, // 0x10 - 0x1F
|
||||
____, ____, 0x02, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, // 0x20 - 0x2F
|
||||
____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, // 0x30 - 0x3F
|
||||
0x02, 0x01, 0x02, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, // 0x40 - 0x4F
|
||||
____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, // 0x50 - 0x5F
|
||||
____, ____, ____, ____, ____, ____, ____, ____, ____, ____, 0x01, 0x01, 0x03, ____, ____, 0x01, // 0x60 - 0x6F
|
||||
0x03, 0x01, 0x01, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____ // 0x70 - 0x7F
|
||||
};
|
||||
|
||||
static const int32_t tmc2209_defaultRegisterResetState[TMC2209_REGISTER_COUNT] =
|
||||
{
|
||||
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
|
||||
R00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00 - 0x0F
|
||||
R10, R11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x10 - 0x1F
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x20 - 0x2F
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x30 - 0x3F
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x40 - 0x4F
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x50 - 0x5F
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, R6C, 0, 0, 0, // 0x60 - 0x6F
|
||||
R70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // 0x70 - 0x7F
|
||||
};
|
||||
|
||||
// Undefine the default register values.
|
||||
// This prevents warnings in case multiple TMC-API chip headers are included at once
|
||||
#undef R00
|
||||
#undef R10
|
||||
#undef R11
|
||||
#undef R6C
|
||||
#undef R70
|
||||
|
||||
// Communication
|
||||
void tmc2209_writeInt(TMC2209TypeDef *tmc2209, uint8_t address, int32_t value);
|
||||
int32_t tmc2209_readInt(TMC2209TypeDef *tmc2209, uint8_t address);
|
||||
|
||||
void tmc2209_init(TMC2209TypeDef *tmc2209, uint8_t channel, uint8_t slaveAddress, ConfigurationTypeDef *tmc2209_config, const int32_t *registerResetState);
|
||||
uint8_t tmc2209_reset(TMC2209TypeDef *tmc2209);
|
||||
uint8_t tmc2209_restore(TMC2209TypeDef *tmc2209);
|
||||
void tmc2209_setRegisterResetState(TMC2209TypeDef *tmc2209, const int32_t *resetState);
|
||||
void tmc2209_setCallback(TMC2209TypeDef *tmc2209, tmc2209_callback callback);
|
||||
void tmc2209_periodicJob(TMC2209TypeDef *tmc2209, uint32_t tick);
|
||||
|
||||
uint8_t tmc2209_get_slave(TMC2209TypeDef *tmc2209);
|
||||
void tmc2209_set_slave(TMC2209TypeDef *tmc2209, uint8_t slaveAddress);
|
||||
|
||||
#endif /* TMC_IC_TMC2209_H_ */
|
||||
20
Clean_TMC2209/lib/tmc/ic/TMC2209/TMC2209_Constants.h
Normal file
20
Clean_TMC2209/lib/tmc/ic/TMC2209/TMC2209_Constants.h
Normal file
@@ -0,0 +1,20 @@
|
||||
/*******************************************************************************
|
||||
* Copyright © 2019 TRINAMIC Motion Control GmbH & Co. KG
|
||||
* (now owned by Analog Devices Inc.),
|
||||
*
|
||||
* Copyright © 2023 Analog Devices Inc. All Rights Reserved. This software is
|
||||
* proprietary & confidential to Analog Devices, Inc. and its licensors.
|
||||
*******************************************************************************/
|
||||
|
||||
|
||||
#ifndef TMC_IC_TMC2209_TMC2209_CONSTANTS_H_
|
||||
#define TMC_IC_TMC2209_TMC2209_CONSTANTS_H_
|
||||
|
||||
#define TMC2209_MOTORS 1
|
||||
#define TMC2209_REGISTER_COUNT TMC_REGISTER_COUNT
|
||||
#define TMC2209_WRITE_BIT TMC_WRITE_BIT
|
||||
#define TMC2209_ADDRESS_MASK TMC_ADDRESS_MASK
|
||||
#define TMC2209_MAX_VELOCITY s32_MAX
|
||||
#define TMC2209_MAX_ACCELERATION u24_MAX
|
||||
|
||||
#endif /* TMC_IC_TMC2209_TMC2209_CONSTANTS_H_ */
|
||||
182
Clean_TMC2209/lib/tmc/ic/TMC2209/TMC2209_Fields.h
Normal file
182
Clean_TMC2209/lib/tmc/ic/TMC2209/TMC2209_Fields.h
Normal file
@@ -0,0 +1,182 @@
|
||||
/*******************************************************************************
|
||||
* Copyright © 2019 TRINAMIC Motion Control GmbH & Co. KG
|
||||
* (now owned by Analog Devices Inc.),
|
||||
*
|
||||
* Copyright © 2023 Analog Devices Inc. All Rights Reserved. This software is
|
||||
* proprietary & confidential to Analog Devices, Inc. and its licensors.
|
||||
*******************************************************************************/
|
||||
|
||||
|
||||
#ifndef TMC2209_FIELDS_H
|
||||
#define TMC2209_FIELDS_H
|
||||
|
||||
#define TMC2209_I_SCALE_ANALOG_MASK 0x01 // GCONF // I_scale_analog (Reset default=1)
|
||||
#define TMC2209_I_SCALE_ANALOG_SHIFT 0 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_INTERNAL_RSENSE_MASK 0x02 // GCONF // internal_Rsense (Reset default: OTP)
|
||||
#define TMC2209_INTERNAL_RSENSE_SHIFT 1 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_EN_SPREADCYCLE_MASK 0x04 // GCONF // en_spreadCycle (Reset default: OTP)
|
||||
#define TMC2209_EN_SPREADCYCLE_SHIFT 2 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_SHAFT_MASK 0x08 // GCONF // controls motor direction
|
||||
#define TMC2209_SHAFT_SHIFT 3 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_INDEX_OTPW_MASK 0x10 // GCONF // index_otpw
|
||||
#define TMC2209_INDEX_OTPW_SHIFT 4 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_INDEX_STEP_MASK 0x20 // GCONF // index_step
|
||||
#define TMC2209_INDEX_STEP_SHIFT 5 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_PDN_DISABLE_MASK 0x40 // GCONF // pdn_disable
|
||||
#define TMC2209_PDN_DISABLE_SHIFT 6 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_MSTEP_REG_SELECT_MASK 0x80 // GCONF // mstep_reg_select
|
||||
#define TMC2209_MSTEP_REG_SELECT_SHIFT 7 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_MULTISTEP_FILT_MASK 0x0100 // GCONF // multistep_filt (Reset default=1)
|
||||
#define TMC2209_MULTISTEP_FILT_SHIFT 8 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_TEST_MODE_MASK 0x0200 // GCONF // test_mode 0
|
||||
#define TMC2209_TEST_MODE_SHIFT 9 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_RESET_MASK 0x01 // GSTAT // reset
|
||||
#define TMC2209_RESET_SHIFT 0 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_DRV_ERR_MASK 0x02 // GSTAT // drv_err
|
||||
#define TMC2209_DRV_ERR_SHIFT 1 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_UV_CP_MASK 0x04 // GSTAT // uv_cp
|
||||
#define TMC2209_UV_CP_SHIFT 2 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_IFCNT_MASK 0xFF // IFCNT // Interface transmission counter. This register becomes incremented with each successful UART interface write access. Read out to check the serial transmission for lost data. Read accesses do not change the content. The counter wraps around from 255 to 0.
|
||||
#define TMC2209_IFCNT_SHIFT 0 // min.: 0, max.: 255, default: 0
|
||||
#define TMC2209_SLAVECONF_MASK 0x0F00 // SLAVECONF // SENDDELAY for read access (time until reply is sent): 0, 1: 8 bit times 2, 3: 3*8 bit times 4, 5: 5*8 bit times 6, 7: 7*8 bit times 8, 9: 9*8 bit times 10, 11: 11*8 bit times 12, 13: 13*8 bit times 14, 15: 15*8 bit times
|
||||
#define TMC2209_SLAVECONF_SHIFT 8 // min.: 0, max.: 15, default: 0
|
||||
#define TMC2209_OTPBIT_MASK 0x07 // OTP_PROG // Selection of OTP bit to be programmed to the selected byte location (n=0..7: programs bit n to a logic 1)
|
||||
#define TMC2209_OTPBIT_SHIFT 0 // min.: 0, max.: 7, default: 0
|
||||
#define TMC2209_OTPBYTE_MASK 0x30 // OTP_PROG // Selection of OTP programming location (0, 1 or 2)
|
||||
#define TMC2209_OTPBYTE_SHIFT 4 // min.: 0, max.: 3, default: 0
|
||||
#define TMC2209_OTPMAGIC_MASK 0xFF00 // OTP_PROG // Set to 0xBD to enable programming. A programming time of minimum 10ms per bit is recommended (check by reading OTP_READ).
|
||||
#define TMC2209_OTPMAGIC_SHIFT 8 // min.: 0, max.: 255, default: 0
|
||||
#define TMC2209_OTP0_BYTE_0_READ_DATA_MASK 0x01 // OTP_READ // to be detailed
|
||||
#define TMC2209_OTP0_BYTE_0_READ_DATA_SHIFT 0 // min.: 0, max.: 255, default: 0
|
||||
#define TMC2209_OTP1_BYTE_1_READ_DATA_MASK 0x02 // OTP_READ // to be detailed
|
||||
#define TMC2209_OTP1_BYTE_1_READ_DATA_SHIFT 8 // min.: 0, max.: 255, default: 0
|
||||
#define TMC2209_OTP2_BYTE_2_READ_DATA_MASK 0x04 // OTP_READ // to be detailed
|
||||
#define TMC2209_OTP2_BYTE_2_READ_DATA_SHIFT 16 // min.: 0, max.: 255, default: 0
|
||||
#define TMC2209_ENN_MASK 0x01 // IOIN //
|
||||
#define TMC2209_ENN_SHIFT 0 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_MS1_MASK 0x04 // IOIN //
|
||||
#define TMC2209_MS1_SHIFT 2 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_MS2_MASK 0x08 // IOIN //
|
||||
#define TMC2209_MS2_SHIFT 3 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_DIAG_MASK 0x10 // IOIN //
|
||||
#define TMC2209_DIAG_SHIFT 4 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_PDN_UART_MASK 0x40 // IOIN //
|
||||
#define TMC2209_PDN_UART_SHIFT 6 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_STEP_MASK 0x80 // IOIN //
|
||||
#define TMC2209_STEP_SHIFT 7 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_SEL_A_MASK 0x0100 // IOIN // Driver type
|
||||
#define TMC2209_SEL_A_SHIFT 8 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_DIR_MASK 0x0200 // IOIN //
|
||||
#define TMC2209_DIR_SHIFT 9 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_VERSION_MASK 0xFF000000 // IOIN // VERSION: 0x20=first version of the IC Identical numbers mean full digital compatibility.
|
||||
#define TMC2209_VERSION_SHIFT 24 // min.: 0, max.: 255, default: 0
|
||||
#define TMC2209_FCLKTRIM_MASK 0x1F // FACTORY_CONF // FCLKTRIM (Reset default: OTP) 0-31: Lowest to highest clock frequency. Check at charge pump output. The frequency span is not guaranteed, but it is tested, that tuning to 12MHz internal clock is possible. The devices come preset to 12MHz clock frequency by OTP programming.
|
||||
#define TMC2209_FCLKTRIM_SHIFT 0 // min.: 0, max.: 31, default: 0
|
||||
#define TMC2209_OTTRIM_MASK 0x30 // FACTORY_CONF // OTTRIM (Default: OTP) %00: OT=143°C, OTPW=120°C %01: OT=150°C, OTPW=120°C %10: OT=150°C, OTPW=143°C %11: OT=157°C, OTPW=143°C
|
||||
#define TMC2209_OTTRIM_SHIFT 8 // min.: 0, max.: 3, default: 0
|
||||
#define TMC2209_IHOLD_MASK 0x1F // IHOLD_IRUN // IHOLD (Reset default: OTP) Standstill current (0=1/32...31=32/32) In combination with stealthChop mode, setting IHOLD=0 allows to choose freewheeling or coil short circuit (passive braking) for motor stand still.
|
||||
#define TMC2209_IHOLD_SHIFT 0 // min.: 0, max.: 31, default: 0
|
||||
#define TMC2209_IRUN_MASK 0x1F00 // IHOLD_IRUN // IRUN (Reset default=31) Motor run current (0=1/32...31=32/32) Hint: Choose sense resistors in a way, that normal IRUN is 16 to 31 for best microstep performance.
|
||||
#define TMC2209_IRUN_SHIFT 8 // min.: 0, max.: 31, default: 0
|
||||
#define TMC2209_IHOLDDELAY_MASK 0x0F0000 // IHOLD_IRUN // IHOLDDELAY (Reset default: OTP) Controls the number of clock cycles for motor power down after standstill is detected (stst=1) and TPOWERDOWN has expired. The smooth transition avoids a motor jerk upon power down. 0: instant power down 1..15: Delay per current reduction step in multiple of 2^18 clocks
|
||||
#define TMC2209_IHOLDDELAY_SHIFT 16 // min.: 0, max.: 15, default: 0
|
||||
#define TMC2209_TPOWERDOWN_MASK 0xFF // TPOWERDOWN // (Reset default=20) Sets the delay time from stand still (stst) detection to motor current power down. Time range is about 0 to 5.6 seconds. 0...((2^8)-1) * 2^18 tclk Attention: A minimum setting of 2 is required to allow automatic tuning of stealthChop PWM_OFFS_AUTO.
|
||||
#define TMC2209_TPOWERDOWN_SHIFT 0 // min.: 0, max.: 255, default: 0
|
||||
#define TMC2209_TSTEP_MASK 0x0FFFFF // TSTEP // Actual measured time between two 1/256 microsteps derived from the step input frequency in units of 1/fCLK. Measured value is (2^20)-1 in case of overflow or stand still. The TSTEP related threshold uses a hysteresis of 1/16 of the compare value to compensate for jitter in the clock or the step frequency: (Txxx*15/16)-1 is the lower compare value for each TSTEP based comparison. This means, that the lower switching velocity equals the calculated setting, but the upper switching velocity is higher as defined by the hysteresis setting.
|
||||
#define TMC2209_TSTEP_SHIFT 0 // min.: 0, max.: 1048575, default: 0
|
||||
#define TMC2209_TPWMTHRS_MASK 0x0FFFFF // TPWMTHRS // Sets the upper velocity for stealthChop voltage PWM mode. For TSTEP = TPWMTHRS, stealthChop PWM mode is enabled, if configured. When the velocity exceeds the limit set by TPWMTHRS, the driver switches to spreadCycle. 0 = Disabled
|
||||
#define TMC2209_TPWMTHRS_SHIFT 0 // min.: 0, max.: 1048575, default: 0
|
||||
#define TMC2209_VACTUAL_MASK 0xFFFFFF // VACTUAL // VACTUAL allows moving the motor by UART control. It gives the motor velocity in +-(2^23)-1 [µsteps / t] 0: Normal operation. Driver reacts to STEP input. /=0: Motor moves with the velocity given by VACTUAL. Step pulses can be monitored via INDEX output. The motor direction is controlled by the sign of VACTUAL.
|
||||
#define TMC2209_VACTUAL_SHIFT 0 // min.: -8388608, max.: 8388607, default: 0
|
||||
#define TMC2209_SEMIN_MASK 0x0000000F
|
||||
#define TMC2209_SEMIN_SHIFT 0
|
||||
#define TMC2209_SEUP_MASK 0x00000060
|
||||
#define TMC2209_SEUP_SHIFT 5
|
||||
#define TMC2209_SEMAX_MASK 0x00000F00
|
||||
#define TMC2209_SEMAX_SHIFT 8
|
||||
#define TMC2209_SEDN_MASK 0x00006000
|
||||
#define TMC2209_SEDN_SHIFT 13
|
||||
#define TMC2209_SEIMIN_MASK 0x00008000
|
||||
#define TMC2209_SEIMIN_SHIFT 15
|
||||
#define TMC2209_MSCNT_MASK 0x03FF // MSCNT // Microstep counter. Indicates actual position in the microstep table for CUR_A. CUR_B uses an offset of 256 into the table. Reading out MSCNT allows determination of the motor position within the electrical wave.
|
||||
#define TMC2209_MSCNT_SHIFT 0 // min.: 0, max.: 1023, default: 0
|
||||
#define TMC2209_CUR_A_MASK 0x01FF // MSCURACT // (signed) Actual microstep current for motor phase A as read from the internal sine wave table (not scaled by current setting)
|
||||
#define TMC2209_CUR_A_SHIFT 0 // min.: -255, max.: 255, default: 0
|
||||
#define TMC2209_CUR_B_MASK 0x01FF0000 // MSCURACT // (signed) Actual microstep current for motor phase B as read from the internal sine wave table (not scaled by current setting)
|
||||
#define TMC2209_CUR_B_SHIFT 16 // min.: -255, max.: 255, default: 0
|
||||
#define TMC2209_TOFF_MASK 0x0F // CHOPCONF // chopper off time and driver enable, Off time setting controls duration of slow decay phase (Nclk = 12 + 32*Toff), %0000: Driver disable, all bridges off %0001: 1 - use only with TBL = 2 %0010 ... %1111: 2 - 15 (Default: OTP, resp. 3 in stealthChop mode)
|
||||
#define TMC2209_TOFF_SHIFT 0 // min.: 0, max.: 7, default: 0
|
||||
#define TMC2209_HSTRT_MASK 0x70 // CHOPCONF // hysteresis start value added to HEND, %000 - %111: Add 1, 2, ..., 8 to hysteresis low value HEND (1/512 of this setting adds to current setting) Attention: Effective HEND+HSTRT <= 16. Hint: Hysteresis decrement is done each 16 clocks. (Default: OTP, resp. 0 in stealthChop mode)
|
||||
#define TMC2209_HSTRT_SHIFT 4 // min.: 0, max.: 7, default: 0
|
||||
#define TMC2209_HEND_MASK 0x0780 // CHOPCONF // hysteresis low value OFFSET sine wave offset, %0000 - %1111: Hysteresis is -3, -2, -1, 0, 1, ..., 12 (1/512 of this setting adds to current setting) This is the hysteresis value which becomes used for the hysteresis chopper. (Default: OTP, resp. 5 in stealthChop mode)
|
||||
#define TMC2209_HEND_SHIFT 7 // min.: 0, max.: 255, default: 0
|
||||
#define TMC2209_TBL_MASK 0x018000 // CHOPCONF // blank time select, %00 - %11: Set comparator blank time to 16, 24, 32 or 40 clocks Hint: %00 or %01 is recommended for most applications (Default: OTP)
|
||||
#define TMC2209_TBL_SHIFT 15 // min.: 0, max.: 255, default: 0
|
||||
#define TMC2209_VSENSE_MASK 0x020000 // CHOPCONF // sense resistor voltage based current scaling
|
||||
#define TMC2209_VSENSE_SHIFT 17 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_MRES_MASK 0x0F000000 // CHOPCONF // MRES micro step resolution, %0000: Native 256 microstep setting. %0001 - %1000: 128, 64, 32, 16, 8, 4, 2, FULLSTEP: Reduced microstep resolution. The resolution gives the number of microstep entries per sine quarter wave. When choosing a lower microstep resolution, the driver automatically uses microstep positions which result in a symmetrical wave. Number of microsteps per step pulse = 2^MRES (Selection by pins unless disabled by GCONF. mstep_reg_select)
|
||||
#define TMC2209_MRES_SHIFT 24 // min.: 0, max.: 255, default: 0
|
||||
#define TMC2209_INTPOL_MASK 0x10000000 // CHOPCONF // interpolation to 256 microsteps
|
||||
#define TMC2209_INTPOL_SHIFT 28 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_DEDGE_MASK 0x20000000 // CHOPCONF // enable double edge step pulses
|
||||
#define TMC2209_DEDGE_SHIFT 29 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_DISS2G_MASK 0x40000000 // CHOPCONF // short to GND protection disable
|
||||
#define TMC2209_DISS2G_SHIFT 30 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_DISS2VS_MASK 0x80000000 // CHOPCONF // Low side short protection disable
|
||||
#define TMC2209_DISS2VS_SHIFT 31 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_OTPW_MASK 0x01 // DRV_STATUS // overtemperature prewarning flag
|
||||
#define TMC2209_OTPW_SHIFT 0 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_OT_MASK 0x02 // DRV_STATUS // overtemperature flag
|
||||
#define TMC2209_OT_SHIFT 1 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_S2GA_MASK 0x04 // DRV_STATUS // short to ground indicator phase A
|
||||
#define TMC2209_S2GA_SHIFT 2 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_S2GB_MASK 0x08 // DRV_STATUS // short to ground indicator phase B
|
||||
#define TMC2209_S2GB_SHIFT 3 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_S2VSA_MASK 0x10 // DRV_STATUS // low side short indicator phase A
|
||||
#define TMC2209_S2VSA_SHIFT 4 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_S2VSB_MASK 0x20 // DRV_STATUS // low side short indicator phase B
|
||||
#define TMC2209_S2VSB_SHIFT 5 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_OLA_MASK 0x40 // DRV_STATUS // open load indicator phase A
|
||||
#define TMC2209_OLA_SHIFT 6 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_OLB_MASK 0x80 // DRV_STATUS // open load indicator phase B
|
||||
#define TMC2209_OLB_SHIFT 7 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_T120_MASK 0x0100 // DRV_STATUS // 120°C comparator
|
||||
#define TMC2209_T120_SHIFT 8 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_T143_MASK 0x0200 // DRV_STATUS // 143°C comparator
|
||||
#define TMC2209_T143_SHIFT 9 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_T150_MASK 0x0400 // DRV_STATUS // 150°C comparator
|
||||
#define TMC2209_T150_SHIFT 10 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_T157_MASK 0x0800 // DRV_STATUS // 157°C comparator
|
||||
#define TMC2209_T157_SHIFT 11 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_CS_ACTUAL_MASK 0x1F0000 // DRV_STATUS // actual motor current
|
||||
#define TMC2209_CS_ACTUAL_SHIFT 16 // min.: 0, max.: 31, default: 0
|
||||
#define TMC2209_STEALTH_MASK 0x40000000 // DRV_STATUS // stealthChop indicator
|
||||
#define TMC2209_STEALTH_SHIFT 30 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_STST_MASK 0x80000000 // DRV_STATUS // standstill indicator
|
||||
#define TMC2209_STST_SHIFT 31 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_PWM_OFS_MASK 0xFF // PWMCONF // User defined PWM amplitude offset (0-255) related to full motor current (CS_ACTUAL=31) in stand still. (Reset default=36) When using automatic scaling (pwm_autoscale=1) the value is used for initialization, only. The autoscale function starts with PWM_SCALE_AUTO=PWM_OFS and finds the required offset to yield the target current automatically. PWM_OFS = 0 will disable scaling down motor current below a motor specific lower measurement threshold. This setting should only be used under certain conditions, i.e. when the power supply voltage can vary up and down by a factor of two or more. It prevents the motor going out of regulation, but it also prevents power down below the regulation limit. PWM_OFS > 0 allows automatic scaling to low PWM duty cycles even below the lower regulation threshold. This allows low (standstill) current settings based on the actual (hold) current scale (register IHOLD_IRUN).
|
||||
#define TMC2209_PWM_OFS_SHIFT 0 // min.: 0, max.: 255, default: 0
|
||||
#define TMC2209_PWM_GRAD_MASK 0xFF00 // PWMCONF // Velocity dependent gradient for PWM amplitude: PWM_GRAD * 256 / TSTEP This value is added to PWM_AMPL to compensate for the velocity-dependent motor back-EMF. With automatic scaling (pwm_autoscale=1) the value is used for first initialization, only. Set PWM_GRAD to the application specific value (it can be read out from PWM_GRAD_AUTO) to speed up the automatic tuning process. An approximate value can be stored to OTP by programming OTP_PWM_GRAD.
|
||||
#define TMC2209_PWM_GRAD_SHIFT 8 // min.: 0, max.: 255, default: 0
|
||||
#define TMC2209_PWM_FREQ_MASK 0x030000 // PWMCONF // %00: fPWM=2/1024 fCLK %01: fPWM=2/683 fCLK %10: fPWM=2/512 fCLK %11: fPWM=2/410 fCLK
|
||||
#define TMC2209_PWM_FREQ_SHIFT 16 // min.: 0, max.: 3, default: 0
|
||||
#define TMC2209_PWM_AUTOSCALE_MASK 0x040000 // PWMCONF //
|
||||
#define TMC2209_PWM_AUTOSCALE_SHIFT 18 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_PWM_AUTOGRAD_MASK 0x080000 // PWMCONF //
|
||||
#define TMC2209_PWM_AUTOGRAD_SHIFT 19 // min.: 0, max.: 1, default: 0
|
||||
#define TMC2209_FREEWHEEL_MASK 0x300000 // PWMCONF // Stand still option when motor current setting is zero (I_HOLD=0). %00: Normal operation %01: Freewheeling %10: Coil shorted using LS drivers %11: Coil shorted using HS drivers
|
||||
#define TMC2209_FREEWHEEL_SHIFT 20 // min.: 0, max.: 3, default: 0
|
||||
#define TMC2209_PWM_REG_MASK 0x0F000000 // PWMCONF // User defined maximum PWM amplitude change per half wave when using pwm_autoscale=1. (1...15): 1: 0.5 increments (slowest regulation) 2: 1 increment (default with OTP2.1=1) 3: 1.5 increments 4: 2 increments ... 8: 4 increments (default with OTP2.1=0) ... 15: 7.5 increments (fastest regulation)
|
||||
#define TMC2209_PWM_REG_SHIFT 24 // min.: 0, max.: 25, default: 0
|
||||
#define TMC2209_PWM_LIM_MASK 0xF0000000 // PWMCONF // Limit for PWM_SCALE_AUTO when switching back from spreadCycle to stealthChop. This value defines the upper limit for bits 7 to 4 of the automatic current control when switching back. It can be set to reduce the current jerk during mode change back to stealthChop. It does not limit PWM_GRAD or PWM_GRAD_AUTO offset. (Default = 12)
|
||||
#define TMC2209_PWM_LIM_SHIFT 28 // min.: 0, max.: 15, default: 0
|
||||
#define TMC2209_PWM_SCALE_SUM_MASK 0xFF // PWM_SCALE // Actual PWM duty cycle. This value is used for scaling the values CUR_A and CUR_B read from the sine wave table.
|
||||
#define TMC2209_PWM_SCALE_SUM_SHIFT 0 // min.: 0, max.: 255, default: 0
|
||||
#define TMC2209_PWM_SCALE_AUTO_MASK 0x01FF0000 // PWM_SCALE // 9 Bit signed offset added to the calculated PWM duty cycle. This is the result of the automatic amplitude regulation based on current measurement.
|
||||
#define TMC2209_PWM_SCALE_AUTO_SHIFT 16 // min.: -255, max.: 255, default: 0
|
||||
#define TMC2209_PWM_OFS_AUTO_MASK 0xFF // PWM_AUTO // Automatically determined offset value
|
||||
#define TMC2209_PWM_OFS_AUTO_SHIFT 0 // min.: 0, max.: 255, default: 0
|
||||
#define TMC2209_PWM_GRAD_AUTO_MASK 0xFF0000 // PWM_AUTO // Automatically determined gradient value
|
||||
#define TMC2209_PWM_GRAD_AUTO_SHIFT 16 // min.: 0, max.: 255, default: 0
|
||||
|
||||
#endif /* TMC2209_FIELDS_H */
|
||||
44
Clean_TMC2209/lib/tmc/ic/TMC2209/TMC2209_Register.h
Normal file
44
Clean_TMC2209/lib/tmc/ic/TMC2209/TMC2209_Register.h
Normal file
@@ -0,0 +1,44 @@
|
||||
/*******************************************************************************
|
||||
* Copyright © 2019 TRINAMIC Motion Control GmbH & Co. KG
|
||||
* (now owned by Analog Devices Inc.),
|
||||
*
|
||||
* Copyright © 2023 Analog Devices Inc. All Rights Reserved. This software is
|
||||
* proprietary & confidential to Analog Devices, Inc. and its licensors.
|
||||
*******************************************************************************/
|
||||
|
||||
|
||||
#ifndef TMC2209_REGISTER_H
|
||||
#define TMC2209_REGISTER_H
|
||||
|
||||
// ===== TMC2209 & 2202 & TMC2209 & 2220 & 2225 "Donkey Kong" family register set =====
|
||||
|
||||
#define TMC2209_GCONF 0x00
|
||||
#define TMC2209_GSTAT 0x01
|
||||
#define TMC2209_IFCNT 0x02
|
||||
#define TMC2209_SLAVECONF 0x03
|
||||
#define TMC2209_OTP_PROG 0x04
|
||||
#define TMC2209_OTP_READ 0x05
|
||||
#define TMC2209_IOIN 0x06
|
||||
#define TMC2209_FACTORY_CONF 0x07
|
||||
|
||||
#define TMC2209_IHOLD_IRUN 0x10
|
||||
#define TMC2209_TPOWERDOWN 0x11
|
||||
#define TMC2209_TSTEP 0x12
|
||||
#define TMC2209_TPWMTHRS 0x13
|
||||
#define TMC2209_TCOOLTHRS 0x14
|
||||
|
||||
#define TMC2209_VACTUAL 0x22
|
||||
|
||||
#define TMC2209_SGTHRS 0x40
|
||||
#define TMC2209_SG_RESULT 0x41
|
||||
#define TMC2209_COOLCONF 0x42
|
||||
|
||||
#define TMC2209_MSCNT 0x6A
|
||||
#define TMC2209_MSCURACT 0x6B
|
||||
#define TMC2209_CHOPCONF 0x6C
|
||||
#define TMC2209_DRVSTATUS 0x6F
|
||||
#define TMC2209_PWMCONF 0x70
|
||||
#define TMC2209_PWMSCALE 0x71
|
||||
#define TMC2209_PWM_AUTO 0x72
|
||||
|
||||
#endif /* TMC2209_Register */
|
||||
Reference in New Issue
Block a user