spi_transaction_t struct question

usinjin
Posts: 6
Joined: Wed Aug 30, 2017 6:13 am

spi_transaction_t struct question

Postby usinjin » Fri Aug 30, 2019 6:25 pm

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.

ESP_Sprite
Posts: 9724
Joined: Thu Nov 26, 2015 4:08 am

Re: spi_transaction_t struct question

Postby ESP_Sprite » Sat Aug 31, 2019 3:09 pm

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.

Who is online

Users browsing this forum: Google [Bot] and 173 guests