SPI Master Driver with multiple tasks and one physical device; clarification on thread-safety.

Tulitomaatti
Posts: 1
Joined: Wed Sep 30, 2020 5:15 pm

SPI Master Driver with multiple tasks and one physical device; clarification on thread-safety.

Postby Tulitomaatti » Wed Sep 30, 2020 5:37 pm

Hello,

I am slightly confused by the SPI Master Driver documentation. It is warned that "if multiple tasks try to access the same SPI Device, the driver is not thread-safe." and the recommended workaround are mutexes or refactoring the application as to let the SPI peripheral be only accessed by one task at a time. The terminology section hints that "Device" is understood as a physical device (due signals and CS-lines being mentioned).

Does this apply strictly to physical devices on the SPI bus, or just to logical devices represented by their respective spi_device_handle_t -handles added on the bus with spi_bus_add_device()? I.e. could I have multiple tasks accessing a single physical SPI device in a thread safe manner, given that each task uses it's own logical spi_device_handle_t -handle (and acquiring/releasing the bus for the logical device as needed with spi_device_acquire/release_bus() )?

I am mainly interested in knowing this in order to avoid perhaps totally unnecessary semaphores or mutexes to protect bus access, in case the already existing spi_device_acquire_bus() and multiple handles representing the same physical device would already serve as a way for a task to prevent other tasks from meddling with the bus while it performs its function.

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

Re: SPI Master Driver with multiple tasks and one physical device; clarification on thread-safety.

Postby ESP_Sprite » Fri Oct 02, 2020 2:20 pm

It strictly applies to the logical device abstraction in the SPI driver, however, each of these uses its own separate CS line and as such would normally map to a physically device as well. You could theoretically do your own CS handling using a standard GPIO and use the logical devices to do thread separation.

tosemusername
Posts: 18
Joined: Tue Feb 05, 2019 5:28 pm
Location: Brazil

Re: SPI Master Driver with multiple tasks and one physical device; clarification on thread-safety.

Postby tosemusername » Tue Dec 22, 2020 8:10 pm

Funny that I found this post by coincidence, with basically the same question I asked a few months ago, though it was on github.
https://github.com/espressif/esp-idf/issues/5517
It's also a bit ironic, given that people prefer to post there than here since this forum seems mostly dead.
Like OP and I pointed out on the github issue, the docs are more confusing than helpful in this case, and should be changed.

Who is online

Users browsing this forum: No registered users and 280 guests