Hello,
I am using a Trinamic TMC2130 stepper driver, which is configured via SPI.
To transmit the commads, there is already this function available:
uint8_t tmc_write(int axis, uint8_t cmd, uint32_t data)
{
uint8_t s;
...
...
...
s = SPI.transfer(cmd);
SPI.transfer((data >> 24UL) & 0xFF) & 0xFF;
SPI.transfer((data >> 16UL) & 0xFF) & 0xFF;
SPI.transfer((data >> 8UL) & 0xFF) & 0xFF;
SPI.transfer((data >> 0UL) & 0xFF) & 0xFF;
return s;
}
It is called for example like this:
tmc_write(5, WRITE | REG_IHOLD_IRUN, 0x00051D1AUL);
Note: I have deleted some parts, which are for the chip select.
The code is working this way. But now I want to change the settings within the Arduino code.
Until now, I have done it manually with a Excel-tool.
To get an better understanding, I need to explain the syntax(see attachment):
Several parameters (in this case 3), are within a binary number.
Parameter 1: Bit 0 to 4
Parameter 2: Bit 8 to 12
Parameter 3: Bit 16 to 19
Parameter 1 is set to DEC 26
Parameter 2 is set to DEC 29
Parameter 3 is set to DEC 5
So at first all decimal numbers must be converted to binary numbers and be placed at the correct bit position. Then there must be a conversion from binary to HEX.
Then I would get 51D1A as HEX-number. But I need it in the syntax with 0x as prefix and the "UL" at the end. So I must get 0x00051D1AUL, which can transfered to the SPI-function.
What is the best way to do this?
I have already created a function, which will generate the right code, but as String and not as uint32_t, like it is neccessery for the SPI-function. Is there an easy solution to convert this String to a uint32_t or is this solution totally wrong?
//--------IHOLDELAY-------------
String strIHOLDELAY;
byte zerosIHOLDELAY = 4 - String(IHOLDELAY, BIN).length();
for (byte i = 0; i < zerosIHOLDELAY; i++) {
strIHOLDELAY = strIHOLDELAY + "0";
}
strIHOLDELAY = strIHOLDELAY + String(IHOLDELAY, BIN);
String hex0IHOLDELAY;
String hex1IHOLDELAY;
hex0IHOLDELAY = strIHOLDELAY.substring(0, 4);
hex1IHOLDELAY = strIHOLDELAY.substring(4, 8);
convHEX(hex0IHOLDELAY);
IHOLDIRUN[0] = charvalue;
//--------String-Contruction-------------
String zeros = IHOLDIRUN;
byte zerosAmount = 8 - String(zeros).length();
zeros="";
for (byte i = 0; i < zerosAmount; i++) {
zeros = zeros + "0";
}
zeros = "0x" + zeros + IHOLDIRUN+ "UL";
IHOLDIRUNstr = zeros;