# SPI bus access control between threads

**URL:** <https://community.particle.io/t/spi-bus-access-control-between-threads/59872>\
**Category:** Libraries\
**Tags:** photon\
**Created:** [March 29, 2021, 9:04pm UTC](https://community.particle.io/t/spi-bus-access-control-between-threads/59872 "2021-03-29T21:04:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![armor](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/armor/32/18398_2.png) [@armor](https://community.particle.io/u/armor)\
**Post date:** [March 29, 2021, 9:04pm UTC](https://community.particle.io/t/spi-bus-access-control-between-threads/59872/1 "2021-03-29T21:04:00Z")

</div>

I have a photon which uses SPI bus to connect to a TFT screen and to an SD card. In addition, there is a SPI FRAM which holds an event queue. Each peripheral has a unique CS pin. The event queue is accessed using the PublishQueueAsyncRK library which publishes from a separate thread from the application. I have noticed that during periods where I might queue a number of events from the application thread and immediately after that I might read from SD and write to TFT, that I might see either corruption of the event data, or corruption of images drawn from SD to screen or primitives drawn directly to the TFT. Aside from using delay() in the application thread immediately after the queuing of an event are there any simple ways to avoid clashes between threads competing to use the SPI bus?  
I am thinking checking for all CS pins high - but this would not be atomic? PublishQueueAsync is already using a mutex.  
@rickkas7 @ScruffR

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [March 29, 2021, 9:39pm UTC](https://community.particle.io/t/spi-bus-access-control-between-threads/59872/2 "2021-03-29T21:39:28Z")

</div>

You absolutely must use SPI transactions (SPI.beginTransction and SPI.endTransaction). You need to put them around any group of operations that can’t be interrupted by another SPI transaction on the same bus. The SpiFlashRK library uses them, so one of the other libraries probably doesn’t.

---

<div class="post-metadata">

**Author:** ![armor](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/armor/32/18398_2.png) [@armor](https://community.particle.io/u/armor)\
**Post date:** [March 29, 2021, 10:07pm UTC](https://community.particle.io/t/spi-bus-access-control-between-threads/59872/3 "2021-03-29T22:07:07Z")

</div>

@rickkas7 Thanks for the quick reply. I have used SPI.beginTransaction and SPI.endTransaction - I have gone through all the source (libraries) looking for "SPI." and nothing except for SdFat - which I had not gone through. I hadn't understood that beginTransaction exclusively claimed the SPI bus since it is separate from the CS pin reset and set.

Should have re-read this:

> In addition to reconfiguring the SPI peripheral, `beginTransaction()` also acquires the SPI peripheral lock, blocking other threads from using the selected SPI peripheral until [`endTransaction()`](https://docs.particle.io/reference/device-os/firmware/photon/#endtransaction-) is called. See [Synchronizing Access to Shared System Resources](https://docs.particle.io/reference/device-os/firmware/photon/#synchronizing-access-to-shared-system-resources) section for additional information on shared resource locks.
> 
> It is required that you use `beginTransaction()` and `endTransaction()` if:
> 
> - You have more than one SPI device and they have different settings (speed, bit order, or mode)
> - You have more than one thread or use SPI from a Software Timer

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [March 29, 2021, 10:12pm UTC](https://community.particle.io/t/spi-bus-access-control-between-threads/59872/4 "2021-03-29T22:12:30Z")

</div>

I haven’t checked recently, but in the past there was an issue that the define that SdFat checked to see if SPI transactions were available was not set on Particle. I’d definitely look there first. It may be fixed (it’s been a while, and I might have misremembered) but it’s as good of a place to start as any.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/6/4/64635adccc35238caa19e9b553ec3e98b163d026.png) [@system](https://community.particle.io/u/system)\
**Post date:** [May 28, 2021, 10:13pm UTC](https://community.particle.io/t/spi-bus-access-control-between-threads/59872/5 "2021-05-28T22:13:12Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
