analogdevicesinc / TMC-API

TRINAMIC's IC API
MIT License
188 stars 83 forks source link

Make the SPI *_readInt and *_writeInt weak symbols #50

Open fleutot opened 7 months ago

fleutot commented 7 months ago

I am using the TMC4671, but I suppose that my issue is valid for most other target chips.

The current version of the API requires the user to implement tmc*_readwriteByte. It implements its own tmc*_readInt and tmc*_writeInt, which in turn calls to that user-defined function.

This implies that the firmware is involved and makes calls between each byte of the datagram.

MCUs often offer the possibility to send multiple bytes at once (one API call, maybe even one peripheral trigger), allowing to send a whole datagram with much fewer operations.

To do that, I would like to be able to override the _readInt and _writeInt functions with my own functions, written appropriately for the MCU I am using.

I understand that it is a good idea to let the current functions be there, for simplicity. However, making them weak would also let the user write their own, more efficient, version.

I have just tested that in my personal clone of TMC-API, with great success:

__attribute__((weak)) int32_t tmc4671_readInt(uint8_t motor, uint8_t address)
{
    // ...
}

Please consider making these functions weak, so that users can use your library without modifications.

Note: attributes to make weak symbols are not part of the C standard, so this might require some kind of pre-processor-fu. But I don't think that this is very difficult.