Initial commit

This commit is contained in:
2026-03-31 13:10:37 +02:00
commit 03325b9502
566 changed files with 351758 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
/*******************************************************************************
* Copyright © 2016 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_API_HEADER_H_
#define TMC_API_HEADER_H_
#include "Config.h"
#include "Macros.h"
#include "Constants.h"
#include "Bits.h"
#include "CRC.h"
#include "RegisterAccess.h"
#include <stdlib.h>
#include "Types.h"
// TODO: Restructure these.
/*
* Goal: Just give these values here as status back to the IDE when used with EvalSystem.
* Currently, this is obtained by just leaving out implementation specific error bits here.
*/
typedef enum {
TMC_ERROR_NONE = 0x00,
TMC_ERROR_GENERIC = 0x01,
TMC_ERROR_FUNCTION = 0x02,
TMC_ERROR_MOTOR = 0x08,
TMC_ERROR_VALUE = 0x10,
TMC_ERROR_CHIP = 0x40
} TMCError;
typedef enum {
TMC_COMM_DEFAULT,
TMC_COMM_SPI,
TMC_COMM_UART
} TMC_Comm_Mode;
#endif /* TMC_API_HEADER_H_ */

View File

@@ -0,0 +1,92 @@
/*******************************************************************************
* 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.
*******************************************************************************/
// BIT DEFINITION
#ifndef TMC_BITS_H_
#define TMC_BITS_H_
#define BIT0 0x00000001
#define BIT1 0x00000002
#define BIT2 0x00000004
#define BIT3 0x00000008
#define BIT4 0x00000010
#define BIT5 0x00000020
#define BIT6 0x00000040
#define BIT7 0x00000080
#define BIT8 0x00000100
#define BIT9 0x00000200
#define BIT10 0x00000400
#define BIT11 0x00000800
#define BIT12 0x00001000
#define BIT13 0x00002000
#define BIT14 0x00004000
#define BIT15 0x00008000
#define BIT16 0x00010000
#define BIT17 0x00020000
#define BIT18 0x00040000
#define BIT19 0x00080000
#define BIT20 0x00100000
#define BIT21 0x00200000
#define BIT22 0x00400000
#define BIT23 0x00800000
#define BIT24 0x01000000
#define BIT25 0x02000000
#define BIT26 0x04000000
#define BIT27 0x08000000
#define BIT28 0x10000000
#define BIT29 0x20000000
#define BIT30 0x40000000
#define BIT31 0x80000000
#define BYTE0_MASK 0x00000000000000FF
#define BYTE0_SHIFT 0
#define BYTE1_MASK 0x000000000000FF00
#define BYTE1_SHIFT 8
#define BYTE2_MASK 0x0000000000FF0000
#define BYTE2_SHIFT 16
#define BYTE3_MASK 0x00000000FF000000
#define BYTE3_SHIFT 24
#define BYTE4_MASK 0x000000FF00000000
#define BYTE4_SHIFT 32
#define BYTE5_MASK 0x0000FF0000000000
#define BYTE5_SHIFT 40
#define BYTE6_MASK 0x00FF000000000000
#define BYTE6_SHIFT 48
#define BYTE7_MASK 0xFF00000000000000
#define BYTE7_SHIFT 56
#define SHORT0_MASK (BYTE0_MASK|BYTE1_MASK)
#define SHORT0_SHIFT BYTE0_SHIFT
#define SHORT1_MASK (BYTE2_MASK|BYTE3_MASK)
#define SHORT1_SHIFT BYTE2_SHIFT
#define SHORT2_MASK (BYTE4_MASK|BYTE5_MASK)
#define SHORT2_SHIFT BYTE4_SHIFT
#define SHORT3_MASK (BYTE6_MASK|BYTE7_MASK)
#define SHORT3_SHIFT BYTE6_SHIFT
#define WORD0_MASK (SHORT0_MASK|SHORT1_MASK)
#define WORD0_SHIFT SHORT0_SHIFT
#define WORD1_MASK (SHORT2_MASK|SHORT3_MASK)
#define WORD1_SHIFT SHORT2_SHIFT
#define NIBBLE(value, n) (((value) >> ((n) << 2)) & 0x0F)
#define BYTE(value, n) (((value) >> ((n) << 3)) & 0xFF)
#define SHORT(value, n) (((value) >> ((n) << 4)) & 0xFFFF)
#define WORD(value, n) (((value) >> ((n) << 5)) & 0xFFFFFFFF)
#define _8_16(__1, __0) (((__1) << BYTE1_SHIFT) | ((__0) << BYTE0_SHIFT))
#define _8_32(__3, __2, __1, __0) (((__3) << BYTE3_SHIFT) | ((__2) << BYTE2_SHIFT) | ((__1) << BYTE1_SHIFT) | ((__0) << BYTE0_SHIFT))
#define _16_32(__1, __0) (((__1) << SHORT1_SHIFT) | ((__0) << SHORT0_SHIFT))
#define _8_64(__7, __6, __5, __4, __3, __2, __1, __0) (((__7) << BYTE7_SHIFT) | ((__6) << BYTE6_SHIFT) | ((__5) << BYTE5_SHIFT) | ((__4) << BYTE4_SHIFT) | ((__3) << BYTE3_SHIFT) | ((__2) << BYTE2_SHIFT) | ((__1) << BYTE1_SHIFT) | ((__0) << BYTE0_SHIFT))
#define _16_64(__3, __2, __1, __0) (((__3) << SHORT3_SHIFT) | ((__2) << SHORT2_SHIFT) | ((__1) << SHORT1_SHIFT) | ((__0) << SHORT0_SHIFT))
#define _32_64(__1, __0) (((__1) << WORD1_SHIFT) | ((__0) << WORD0_SHIFT))
#endif /* TMC_BITS_H_ */

View File

@@ -0,0 +1,214 @@
/*******************************************************************************
* Copyright © 2017 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.
*******************************************************************************/
/*
* This is a generic implementation for a CRC8 generator supporting
* both compile-time (1) and run-time initialized Lookup tables for efficient CRC8 calculation.
* You can store multiple tables for different polynomials and (non-)reflected CRCs.
* The different tables are referenced by an index, with an upper limit set at compile time (CRC_TABLE_COUNT).
*
* To generate CRCs you must first generate the Lookup-table by calling fillCRCTable()
* with any index. CRCs can then be generated from any data buffer by calling CRC()
* with the same index previously given to fillCRCTable().
*
* The table generation has been optimized for speed so that the runtime
* table generation can even be done during normal operation if required.
* However, as long as the required polynomials are known on initialization,
* the table generation should be done at that time.
* On the Landungsbruecke the initialization of a CRC table takes ~250µs. (2)
* Should your application still have problems with the table calculation time,
* this algorithm could probably be speed up by preparing a 2- or 4-bit lookup table
* to speed up the actual table generation.
*
* (1): For compile-time CRC tables, just fill the table(s) by initializing CRCTables[] to the proper values.
* (2): Tested by toggling a GPIO pin, generating a table in-between and measuring the GPIO pulse width.
*/
#include "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;
}

View File

@@ -0,0 +1,25 @@
/*******************************************************************************
* Copyright © 2017 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_HELPERS_CRC_H_
#define TMC_HELPERS_CRC_H_
#include "Types.h"
// Amount of CRC tables available
// Each table takes ~260 bytes (257 bytes, one bool and structure padding)
#define CRC_TABLE_COUNT 2
uint8_t tmc_fillCRC8Table(uint8_t polynomial, bool isReflected, uint8_t index);
uint8_t tmc_CRC8(uint8_t *data, uint32_t bytes, uint8_t index);
uint8_t tmc_tableGetPolynomial(uint8_t index);
bool tmc_tableIsReflected(uint8_t index);
#endif /* TMC_HELPERS_CRC_H_ */

View File

@@ -0,0 +1,41 @@
/*******************************************************************************
* Copyright © 2018 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_HELPERS_CONFIG_H_
#define TMC_HELPERS_CONFIG_H_
#include "Constants.h"
#include "Types.h"
// Callback functions have IC-dependent parameters
// To store the function pointers we use this dummy type, which is never
// called without casting it to the IC-specific type first.
// (Casting between function pointers is allowed by the C standard)
typedef void (*tmc_callback_config)(void);
// States of a configuration
typedef enum {
CONFIG_READY,
CONFIG_RESET,
CONFIG_RESTORE
} ConfigState;
// structure for configuration mechanism
typedef struct
{
ConfigState state;
uint8_t configIndex;
int32_t shadowRegister[TMC_REGISTER_COUNT];
uint8_t (*reset) (void);
uint8_t (*restore) (void);
tmc_callback_config callback;
uint8_t channel;
} ConfigurationTypeDef;
#endif /* TMC_HELPERS_CONFIG_H_ */

View File

@@ -0,0 +1,24 @@
/*******************************************************************************
* Copyright © 2018 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_HELPERS_CONSTANTS_H_
#define TMC_HELPERS_CONSTANTS_H_
#define TMC_WRITE_BIT 0x80
#define TMC_ADDRESS_MASK 0x7F
#define TMC_DEFAULT_MOTOR 0
//#define TMC_DIRECTION_RIGHT TRUE
//#define TMC_DIRECTION_LEFT FALSE
#define TMC_REGISTER_COUNT 128 // Default register count
#endif /* TMC_HELPERS_CONSTANTS_H_ */

View File

@@ -0,0 +1,173 @@
/*******************************************************************************
* Copyright © 2018 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 "Functions.h"
int32_t tmc_limitInt(int32_t value, int32_t min, int32_t max)
{
if (value > max)
return max;
else if (value < min)
return min;
else
return value;
}
int64_t tmc_limitS64(int64_t value, int64_t min, int64_t max)
{
if (value > max)
return max;
else if (value < min)
return min;
else
return value;
}
/* lookup table for square root function */
static const unsigned char sqrttable[256] =
{
0, 16, 22, 27, 32, 35, 39, 42, 45, 48, 50, 53, 55, 57, 59, 61,
64, 65, 67, 69, 71, 73, 75, 76, 78, 80, 81, 83, 84, 86, 87, 89,
90, 91, 93, 94, 96, 97, 98, 99, 101, 102, 103, 104, 106, 107, 108, 109,
110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126,
128, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,
143, 144, 144, 145, 146, 147, 148, 149, 150, 150, 151, 152, 153, 154, 155, 155,
156, 157, 158, 159, 160, 160, 161, 162, 163, 163, 164, 165, 166, 167, 167, 168,
169, 170, 170, 171, 172, 173, 173, 174, 175, 176, 176, 177, 178, 178, 179, 180,
181, 181, 182, 183, 183, 184, 185, 185, 186, 187, 187, 188, 189, 189, 190, 191,
192, 192, 193, 193, 194, 195, 195, 196, 197, 197, 198, 199, 199, 200, 201, 201,
202, 203, 203, 204, 204, 205, 206, 206, 207, 208, 208, 209, 209, 210, 211, 211,
212, 212, 213, 214, 214, 215, 215, 216, 217, 217, 218, 218, 219, 219, 220, 221,
221, 222, 222, 223, 224, 224, 225, 225, 226, 226, 227, 227, 228, 229, 229, 230,
230, 231, 231, 232, 232, 233, 234, 234, 235, 235, 236, 236, 237, 237, 238, 238,
239, 240, 240, 241, 241, 242, 242, 243, 243, 244, 244, 245, 245, 246, 246, 247,
247, 248, 248, 249, 249, 250, 250, 251, 251, 252, 252, 253, 253, 254, 254, 255
};
int32_t tmc_sqrti(int32_t x)
{
int32_t xn;
// Negative parameter?
if (x < 0)
return -1;
if (x < 0x0100)
return (int32_t) sqrttable[x] >> 4;
if (x >= 0x00010000)
{
if (x >= 0x01000000)
{
if (x >= 0x10000000)
{
if (x >= 0x40000000)
{
// 0x40000000 <= x < 0x7FFFFFFF
xn = (int32_t) sqrttable[x >> 24] << 8;
}
else
{
// 0x10000000 <= x < 0x40000000
xn = (int32_t) sqrttable[x >> 22] << 7;
}
}
else
{
if (x >= 0x04000000)
{
// 0x04000000 <= x < 0x10000000
xn = (int32_t) sqrttable[x >> 20] << 6;
}
else
{
// 0x01000000 <= x < 0x04000000
xn = (int32_t) sqrttable[x >> 18] << 5;
}
}
// Two steps of the babylonian method
xn = (xn + 1 + (x / xn)) >> 1;
xn = (xn + 1 + (x / xn)) >> 1;
}
else
{
if (x >= 0x00100000)
{
if (x >= 0x00400000)
{
// 0x00400000 <= x < 0x01000000
xn = (int32_t) sqrttable[x >> 16] << 4;
}
else
{
// 0x00100000 <= x < 0x00400000
xn = (int32_t) sqrttable[x >> 14] << 3;
}
}
else
{
if (x >= 0x00040000)
{
// 0x00040000 <= x < 0x00100000
xn = (int32_t) sqrttable[x >> 12] << 2;
}
else
{
// 0x00010000 <= x < 0x00040000
xn = (int32_t) sqrttable[x >> 10] << 1;
}
}
// One step of the babylonian method
xn = (xn + 1 + (x / xn)) >> 1;
}
}
else
{
if (x >= 0x1000)
{
if (x >= 0x4000)
{
// 0x4000 <= x < 0x00010000
xn = (int32_t) (sqrttable[x >> 8] ) + 1;
}
else
{
// 0x1000 <= x < 0x4000
xn = (int32_t) (sqrttable[x >> 6] >> 1) + 1;
}
}
else
{
if (x >= 0x0400)
{
// 0x0400 <= x < 0x1000
xn = (int32_t) (sqrttable[x >> 4] >> 2) + 1;
}
else
{
// 0x0100 <= x < 0x0400
xn = (int32_t) (sqrttable[x >> 2] >> 3) + 1;
}
}
}
// Make sure that our result is floored
if ((xn * xn) > x)
xn--;
return xn;
}
int32_t tmc_filterPT1(int64_t *akku, int32_t newValue, int32_t lastValue, uint8_t actualFilter, uint8_t maxFilter)
{
*akku += (newValue-lastValue) << (maxFilter-actualFilter);
return *akku >> maxFilter;
}

View File

@@ -0,0 +1,20 @@
/*******************************************************************************
* Copyright © 2018 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_FUNCTIONS_H_
#define TMC_FUNCTIONS_H_
#include "API_Header.h"
int32_t tmc_limitInt(int32_t value, int32_t min, int32_t max);
int64_t tmc_limitS64(int64_t value, int64_t min, int64_t max);
int32_t tmc_sqrti(int32_t x);
int32_t tmc_filterPT1(int64_t *akku, int32_t newValue, int32_t lastValue, uint8_t actualFilter, uint8_t maxFilter);
#endif /* TMC_FUNCTIONS_H_ */

View File

@@ -0,0 +1,59 @@
/*******************************************************************************
* Copyright © 2018 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_MACROS_H_
#define TMC_MACROS_H_
/* Cast a n bit signed int to a 32 bit signed int
* This is done by checking the MSB of the signed int (Bit n).
* If it is 1, the value is negative and the Bits 32 to n+1 are set to 1
* If it is 0, the value remains unchanged
*/
#define CAST_Sn_TO_S32(value, n) ((value) | (((value) & ((uint32_t)1<<((n)-1)))? ~(((uint32_t)1<<(n))-1) : 0 ))
// Min/Max macros
#ifndef MIN
#define MIN(a,b) (((a)<(b)) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a,b) (((a)>(b)) ? (a) : (b))
#endif
// Static Array length
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
#endif
// Generic mask/shift macros
#define FIELD_GET(data, mask, shift) \
(((data) & (mask)) >> (shift))
#define FIELD_SET(data, mask, shift, value) \
(((data) & (~(mask))) | (((value) << (shift)) & (mask)))
// Register read/write/update macros using Mask/Shift:
#define FIELD_READ(read, motor, address, mask, shift) \
FIELD_GET(read(motor, address), mask, shift)
#define FIELD_WRITE(write, motor, address, mask, shift, value) \
(write(motor, address, ((value)<<(shift)) & (mask)))
#define FIELD_UPDATE(read, write, motor, address, mask, shift, value) \
(write(motor, address, FIELD_SET(read(motor, address), mask, shift, value)))
// Macro to surpress unused parameter warnings
#ifndef UNUSED
#define UNUSED(x) (void)(x)
#endif
// Memory access helpers
// Force the compiler to access a location exactly once
#define ACCESS_ONCE(x) *((volatile typeof(x) *) (&x))
// Macro to remove write bit for shadow register array access
#define TMC_ADDRESS(x) ((x) & (TMC_ADDRESS_MASK))
#endif /* TMC_MACROS_H_ */

View File

@@ -0,0 +1,86 @@
/*******************************************************************************
* Copyright © 2017 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.
*******************************************************************************/
/*
* The permission system aims to allow a general-purpose implementation for
* all common hardware register usages. This includes:
* - Trivial Cases: Read, Write, Read & Write
*
* - Read & Write accesses that route to different values/functions of a chip.
* (e.g. serial communication, where read/write corresponds to RX/TX)
* - Read to clear, write to clear. This does not directly affect the access,
* but can be used to implement a software shadow register for flags
* (ORing the read value into a shadow register instead of overwriting).
* - Registers with default values that are not known (e.g. Factory configuration
* values that should not be overwritten by default).
*/
#ifndef TMC_HELPERS_REGISTERACCESS_H
#define TMC_HELPERS_REGISTERACCESS_H
// Register access bits
/* Lower nibble is used for read/write, higher nibble is used for
* special case registers. This makes it easy to identify the read/write
* part of the permissions in a hexadecimal permission number.
* The dirty bit will only ever be set at runtime, so we keep the easily
* readable lower nibble.
*/
#define TMC_ACCESS_NONE 0x00
#define TMC_ACCESS_READ 0x01
#define TMC_ACCESS_WRITE 0x02
// 0x04 is currently unused
#define TMC_ACCESS_DIRTY 0x08 // Register has been written since reset -> shadow register is valid for restore
// Special Register bits
#define TMC_ACCESS_RW_SPECIAL 0x10 // Read and write are independent - different values and/or different functions
#define TMC_ACCESS_FLAGS 0x20 // Register has read or write to clear flags.
#define TMC_ACCESS_HW_PRESET 0x40 // Register has hardware presets (e.g. Factory calibrations) - do not write a default value
// 0x80 is currently unused
// Permission combinations
#define TMC_ACCESS_RW (TMC_ACCESS_READ | TMC_ACCESS_WRITE) // 0x03 - Read and write
#define TMC_ACCESS_RW_SEPARATE (TMC_ACCESS_RW | TMC_ACCESS_RW_SPECIAL) // 0x13 - Read and write, with separate values/functions
#define TMC_ACCESS_R_FLAGS (TMC_ACCESS_READ | TMC_ACCESS_FLAGS) // 0x21 - Read, has flags (read to clear)
#define TMC_ACCESS_RW_FLAGS (TMC_ACCESS_RW | TMC_ACCESS_FLAGS) // 0x23 - Read and write, has flags (read or write to clear)
#define TMC_ACCESS_W_PRESET (TMC_ACCESS_WRITE | TMC_ACCESS_HW_PRESET) // 0x42 - Write, has hardware preset - skipped in reset routine
#define TMC_ACCESS_RW_PRESET (TMC_ACCESS_RW | TMC_ACCESS_HW_PRESET) // 0x43 - Read and write, has hardware presets - skipped in reset routine
// Helper macros
#define TMC_IS_READABLE(x) ((x) & TMC_ACCESS_READ)
#define TMC_IS_WRITABLE(x) ((x) & TMC_ACCESS_WRITE)
#define TMC_IS_DIRTY(x) ((x) & TMC_ACCESS_DIRTY)
#define TMC_IS_PRESET(x) ((x) & TMC_ACCESS_HW_PRESET)
#define TMC_IS_RESETTABLE(x) (((x) & (TMC_ACCESS_W_PRESET)) == TMC_ACCESS_WRITE) // Write bit set, Hardware preset bit not set
#define TMC_IS_RESTORABLE(x) (((x) & TMC_ACCESS_WRITE) && (!(x & TMC_ACCESS_HW_PRESET) || (x & TMC_ACCESS_DIRTY))) // Write bit set, if it's a hardware preset register, it needs to be dirty
// Struct for listing registers that have constant contents which we cannot
// obtain by reading them due to the register not being read-back.
typedef struct
{
uint8_t address;
uint32_t value;
} TMCRegisterConstant;
// Helper define:
// Most register permission arrays are initialized with 128 values.
// In those fields its quite hard to have an easy overview of available
// registers. For that, ____ is defined to 0, since 4 underscores are
// very easy to distinguish from the 2-digit hexadecimal values.
// This way, the used registers (permission != ACCESS_NONE) are easily spotted
// amongst unused (permission == ACCESS_NONE) registers.
#define ____ 0x00
// Helper define:
// Default reset values are not used if the corresponding register has a
// hardware preset. Since this is not directly visible in the default
// register reset values array, N_A is used as an indicator for a preset
// value, where any value will be ignored anyways (N_A: not available).
#define N_A 0
#endif /* TMC_HELPERS_REGISTERACCESS_H */

View File

@@ -0,0 +1,107 @@
/*******************************************************************************
* Copyright © 2016 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.
*******************************************************************************/
// Turn off the integer typedefs by uncommenting the following two defines.
// If your IDE (e.g. Arduino IDE) already defines these types, deactivating
// these typedefs will fix errors and/or warnings.
//#define TMC_SKIP_UINT_TYPEDEFS // Disable u8, u16, u32, uint8, uint16 and uint32 typedefs
//#define TMC_SKIP_INT_TYPEDEFS // Disable s8, s16, s32, int8, int16 and int32 typedefs
#ifndef TMC_TYPES_H_
#define TMC_TYPES_H_
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#ifndef TMC_TYPES_INTEGERS
#define TMC_TYPES_INTEGERS
// todo: change to standard ISO C99 types (ED)
// www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf
// ISO C99: 7.18 Integer types 8, 16, 32, or 64 bits
// intN_t = twos complement signed integer type with width N, no padding bits.
// uintN_t = an unsigned integer type with width N.
// floatN_t = N bit IEE 754 float.
// INT8_MIN, INT8_MAX, INT16_MIN, INT16_MAX, INT32_MIN, INT32_MAX, .... UINT32_MAX
typedef float float32_t;
typedef double float64_t;
#ifndef TMC_TYPES_INTEGERS_UNSIGNED
#define TMC_TYPES_INTEGERS_UNSIGNED
#ifndef TMC_SKIP_UINT_TYPEDEFS
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
#endif /* TMC_SKIP_UINT_TYPEDEFS */
#define u8_MAX (uint8_t) 255
#define u10_MAX (uint16_t) 1023
#define u12_MAX (uint16_t) 4095
#define u15_MAX (uint16_t) 32767
#define u16_MAX (uint16_t) 65535
#define u18_MAX (uint32_t) 262143uL
#define u20_MAX (uint32_t) 1048575uL
#define u22_MAX (uint32_t) 4194303uL
#define u24_MAX (uint32_t) 16777215uL
#define u32_MAX (uint32_t) 4294967295uL
#endif /* TMC_TYPES_INTEGERS_UNSIGNED */
#ifndef TMC_TYPES_INTEGERS_SIGNED
#define TMC_TYPES_INTEGERS_SIGNED
#ifndef TMC_SKIP_INT_TYPEDEFS
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int8_t int8;
typedef int16_t int16;
typedef int32_t int32;
#endif /* TMC_SKIP_INT_TYPEDEFS */
#define s8_MAX (int8_t) 127
#define s8_MIN (int8_t) -128
#define s16_MAX (int16_t) 32767
#define s16_MIN (int16_t) -32768
#define s24_MAX (int32_t) 8388607
#define s24_MIN (int32_t) -8388608
#define s32_MAX (int32_t) 2147483647
#define s32_MIN (int32_t) -2147483648
#endif /* TMC_TYPES_INTEGERS_SIGNED */
#endif /* TMC_TYPES_INTEGERS */
#ifndef TMC_TYPES_NULL
#define TMC_TYPES_NULL
#ifndef NULL
#define NULL ((void *) 0)
#endif /* NULL */
#ifndef FALSE
#define FALSE false
#endif
#ifndef TRUE
#define TRUE true
#endif
#endif /* TMC_TYPES_NULL */
#endif /* TMC_TYPES_H_ */