ESP-IDF 32bit register SPI read/write
Posted: Mon May 20, 2024 3:33 pm
Hello,
I am trying to read/write 32 bit registers from stepper driver TMC5240 using a ESP32S3 CPU.
According to stepper DS, the write operation should be performed writing the register address + 0x80 followed by 32 bit register values.
For example, for a write access to the register (VACTUAL), the address byte has to be set to 0x80 + 0x22 = 0xA2, so to write AABBCCDD to this register I should send 0xA2 0xAA 0xBB 0xCC.
The read operation consists in writing the register address followed by 4 bytes of zeros.
Does the 32 bits limit of SPI_TRANS_USE_TXDATA, SPI_TRANS_USE_RXDATA includes also the address?
the following functions does not work, in particular I always read the same value (0x39) from all the registers.
I am trying to read/write 32 bit registers from stepper driver TMC5240 using a ESP32S3 CPU.
According to stepper DS, the write operation should be performed writing the register address + 0x80 followed by 32 bit register values.
For example, for a write access to the register (VACTUAL), the address byte has to be set to 0x80 + 0x22 = 0xA2, so to write AABBCCDD to this register I should send 0xA2 0xAA 0xBB 0xCC.
The read operation consists in writing the register address followed by 4 bytes of zeros.
Does the 32 bits limit of SPI_TRANS_USE_TXDATA, SPI_TRANS_USE_RXDATA includes also the address?
the following functions does not work, in particular I always read the same value (0x39) from all the registers.
Code: Select all
int32_t tmc5240_readInt(TMC5240TypeDef *tmc5240, uint8_t address)
{
uint8_t channel = tmc5240->config->channel;
if (channel < stepperINVALID)
{
esp_err_t err;
uint32_t RxDataBuff;
uint32_t value = 0;
// uint8_t pBuffer2[2];
spi_transaction_t spitr = {
.cmd = address,
.length = 4 * 8,
.flags = SPI_TRANS_USE_TXDATA,
.tx_data = {value},
.rx_buffer = &RxDataBuff,
.rxlength = 32
//.user = (void*) &sensor_ctx,
};
err = spi_device_acquire_bus(m_stepper_cfg[channel].hspi, portMAX_DELAY);
if (err != ESP_OK ) printf("SPI ACQ BUS ERROR\n");
err = spi_device_transmit(m_stepper_cfg[channel].hspi, &spitr);
ESP_ERROR_CHECK(err);
if (err != ESP_OK ) printf("SPI READ ERROR address %d\n", address);
spi_device_release_bus(m_stepper_cfg[channel].hspi);
//if (err != ESP_OK ) printf("SPI ACQ BUS ERROR\n");
//uint32_t res = SPI_SWAP_DATA_RX(RxDataBuff, 32);
return(RxDataBuff);
}
return -1;
}
Code: Select all
void tmc5240_writeInt(TMC5240TypeDef *tmc5240, uint8_t address, int32_t value)
{
uint8_t channel = tmc5240->config->channel;
esp_err_t err;
spi_transaction_t t = {
.cmd = address + SPI_WRITE_CMD,
.length = 4 * 8,
.flags = SPI_TRANS_USE_TXDATA,
.tx_data = {value},
};
err = spi_device_acquire_bus(m_stepper_cfg[channel].hspi, portMAX_DELAY);
if (err != ESP_OK ) printf("SPI ACQ BUS ERROR\n");
assert(err==ESP_OK);
err = spi_device_polling_transmit(m_stepper_cfg[channel].hspi, &t);
if (err != ESP_OK ) printf("SPI WRITE ERROR (%d)\n", address);
spi_device_release_bus(m_stepper_cfg[channel].hspi);
}