Page 1 of 1

spi_transaction_t struct question

Posted: Fri Aug 30, 2019 6:25 pm
by usinjin
Hello,

I am still somewhat new to SPI communication, so please forgive me if this is a silly question. I am looking at a few different C code examples of SPI sensor communication. When sending commands, some implementations seem to create a new spi_transaction_t and set the tx_data field with the command, but not set the addr field. Other codes (those not written for a specific sensor, but rather SPI communication in general) usually have some sort of writeBytes command, and take a register address. I am basically trying to reconcile these two implementations: when the addr field is not set explicitly by me, what does the SPI driver set it to? I tried printing addr out (using code that works correctly), and it seems to be set to 0. Is this correct? Looking at sensor datasheets, I don't see registers that seem to take "cmd input", only config registers. Is this because when a sensor receives the data in its shift register, commands aren't "stored" in some other register--so their address is effectively 0x00?

Basically I'd like to be able to call a function similar to this, even to write a command to the device (in my case and ADC, to say start/stop:

Code: Select all

esp_err_t SPI::writeBytes(spi_device_handle_t handle, uint8_t regAddr, size_t length, const uint8_t *data);
Any input is greatly appreciated.

Re: spi_transaction_t struct question

Posted: Sat Aug 31, 2019 3:09 pm
by ESP_Sprite
In the end, everything is bits sent out over the SPI bus. The ESP32 implements an abstraction that is used by a fair amount of devices (some of the first SPI bits meaning an address and/or command) so it is easier to send a chunk of data without needing to manually prefix it with that information, but in the end everything is bits sent out over the SPI bus. The distinction you see, is drivers either making use of this extra functionality by filling in the address field of the spi_transaction_t, while others prefer adding this information manually, by putting it before the data being received and sent. There's no real 'best way' here, pick whichever you prefer.