Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT]: Adding in the TMC429 driver #72

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added tmc/ic/TMC429/README.md
Empty file.
62 changes: 62 additions & 0 deletions tmc/ic/TMC429/TMC429.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*******************************************************************************
* Copyright © 2019 TRINAMIC Motion Control GmbH & Co. KG
* (now owned by Analog Devices Inc.),
*
* Copyright © 2024 Analog Devices Inc. All Rights Reserved.
* This software is proprietary to Analog Devices, Inc. and its licensors.
*******************************************************************************/

#include "TMC429.h"
#include "TMC429_HW_Abstraction.h"

void tmc429_rotateMotor(uint16_t icID, uint8_t motor, int32_t velocity)
{
if(motor >= TMC429_MOTORS)
return;

writeRegisterSPI(icID, TMC429_V_TARGET(motor), (velocity >= 0)? velocity : -velocity);
}

int32_t readRegisterSPI(uint16_t icID, uint8_t address)
{
uint8_t data[4] = { 0 };

// Set the address for reading
// Set bit 0 to 1 to indicate a read operation
data[0] = (address & TMC429_ADDRESS_MASK) | TMC429_WRITE_READ_BIT;

// Send the read request
tmc429_readWriteSPI(icID, &data[0], sizeof(data));

// Rewrite the address and set the read bit
data[0] = (address & TMC429_ADDRESS_MASK) | TMC429_WRITE_READ_BIT;

// Send another request to receive the read reply
tmc429_readWriteSPI(icID, &data[0], sizeof(data));

return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
}

void writeRegisterSPI(uint16_t icID, uint8_t address, int32_t value)
{
uint8_t data[4] = { 0 };

// Set bit 0 to 0 to indicate a write operation
data[0] = address & TMC429_ADDRESS_MASK;
data[1] = 0xFF & (value >> 16);
data[2] = 0xFF & (value >> 8);
data[3] = 0xFF & value;

// Send the write request
tmc429_readWriteSPI(icID, &data[0], sizeof(data));
}

int32_t tmc429_readRegister(uint16_t icID, uint8_t address)
{
return readRegisterSPI(icID, address);
}

void tmc429_writeRegister(uint16_t icID, uint8_t address, int32_t value)
{
writeRegisterSPI(icID, address, value);
}
76 changes: 76 additions & 0 deletions tmc/ic/TMC429/TMC429.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*******************************************************************************
* Copyright © 2019 TRINAMIC Motion Control GmbH & Co. KG
* (now owned by Analog Devices Inc.),
*
* Copyright © 2024 Analog Devices Inc. All Rights Reserved.
* This software is proprietary to Analog Devices, Inc. and its licensors.
*******************************************************************************/

#ifndef TMC_IC_TMC429_H_
#define TMC_IC_TMC429_H_

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include "TMC429_HW_Abstraction.h"

/*******************************************************************************
* API Configuration Defines
* These control optional features of the TMC-API implementation.
* These can be commented in/out here or defined from the build system.
*******************************************************************************/

// => TMC-API wrapper
extern void tmc429_readWriteSPI(uint16_t icID, uint8_t *data, size_t dataLength);
// => TMC-API wrapper

int32_t tmc429_readRegister(uint16_t icID, uint8_t address);
void tmc429_writeRegister(uint16_t icID, uint8_t address, int32_t value);
void tmc429_rotateMotor(uint16_t icID, uint8_t motor, int32_t velocity);

typedef struct
{
uint32_t mask;
uint8_t shift;
uint8_t address;
bool isSigned;
} RegisterField;

static inline uint32_t tmc429_fieldExtract(uint32_t data, RegisterField field)
{
uint32_t value = (data & field.mask) >> field.shift;

if (field.isSigned)
{
// Apply signedness conversion
uint32_t baseMask = field.mask >> field.shift;
uint32_t signMask = baseMask & (~baseMask >> 1);
value = (value ^ signMask) - signMask;
}

return value;
}

static inline uint32_t tmc429_fieldRead(uint16_t icID, RegisterField field)
{
uint32_t value = tmc429_readRegister(icID, field.address);

return tmc429_fieldExtract(value, field);
}

static inline uint32_t tmc429_fieldUpdate(uint32_t data, RegisterField field, uint32_t value)
{
return (data & (~field.mask)) | ((value << field.shift) & field.mask);
}

static inline void tmc429_fieldWrite(uint16_t icID, RegisterField field, uint32_t value)
{
uint32_t regValue = tmc429_readRegister(icID, field.address);

regValue = tmc429_fieldUpdate(regValue, field, value);

tmc429_writeRegister(icID, field.address, regValue);
}


#endif /* TMC_IC_TMC429_H_ */
Loading