arduino / arduino/ArduinoCore-mbed

Is SPI thread safe?

Open
#388 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
411
Forks
225
PR merge metrics
No merged PRs in 30d

Description

I need to use same SPI port (MOSI, MISO, SCK) with different Chip Select (CS). However [SPI class](https://github.com/arduino/ArduinoCore-mbed/blob/bbc628439eb4dd64b70348e91ecda8e0c45e318c/libraries/SPI/SPI.h#L31) is not supporting CS. Therefore I need to code like:
```
#include "SPI.h"
#include "mbed.h"

rtos::Thread thread;

auto CS0 = D7;
auto CS1 = D6;

void thread_func() {
digitalWrite(CS0, LOW);
SPI.begin();
digitalWrite(CS0, HIGH);
}

void setup() {
pinMode(CS0, OUTPUT);
digitalWrite(CS0, HIGH);
pinMode(CS1, OUTPUT);
digitalWrite(CS1, HIGH);

thread.start(mbed::callback(thread_func));

digitalWrite(CS1, LOW);
SPI.begin();
digitalWrite(CS1, HIGH);
}

void loop() {}
```
Is this thread safe? Is there any chance CS0 and CS1 to go LOW at the same time?

On the other hand, mbed's SPI supports CS pin.

If Arduino's SPI is not thread safe, how about using mbed's SPI CS pin?

Contributor guide

No contributing guide indexed for this repository

Research direction

The relevant entry points are libraries/SPI/SPI.h and cores/arduino/mbed/drivers/include/drivers/SPIMaster.h; compare their chip-select and synchronization behavior and reproduce the two-thread example on a supported board. Done means documenting whether Arduino SPI and mbed SPI with CS are safe for this use, including any limitations.

Written by the indexing model from the issue text.

Assessment

Tech stack
arduino, cpp
Domain
embedded-iot
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.