# Photon: Can the Photon be used as a SPI slave with the existing firmware primitives?

**URL:** <https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535>\
**Category:** Firmware\
**Created:** [July 20, 2015, 11:33pm UTC](https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535 "2015-07-20T23:33:01Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwest](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jwest](https://community.particle.io/u/jwest)\
**Post date:** [July 20, 2015, 11:33pm UTC](https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535/1 "2015-07-20T23:33:01Z")

</div>

Hello,

Can the Photon be used as a SPI slave with the existing firmware primitives?

On this page:  
[http://docs.particle.io/photon/firmware/#communication-spi](http://docs.particle.io/photon/firmware/#communication-spi)

It says:  
SPI  
This library allows you to communicate with SPI devices, with the Core/Photon as the master device.

I have taken a look at the code in the repository branch - it appears the code is in a Broadcom (actually ST but Broadcom lib does have some SPI source as well) library (I haven’t used a lib tool yet to find out for sure) - but can the code be modified to convert the Photon to a SPI slave? (Assuming this isn’t already done or posted someplace I haven’t found yet. - But if that was the case you’d think there would be mention of that on the above referenced page.)

Can we get the source to all of the .a lib (archive) files? (If anything is different from the WICED or libs such as STM32F4xx\_DSP\_StdPeriph\_Lib\_V1.5.1?)

Thanks,  
jwest

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [July 21, 2015, 12:15am UTC](https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535/2 "2015-07-21T00:15:37Z")

</div>

@mdma, @peekay123, any thoughts?

---

<div class="post-metadata">

**Author:** ![jwest](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jwest](https://community.particle.io/u/jwest)\
**Post date:** [July 29, 2015, 7:13am UTC](https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535/3 "2015-07-29T07:13:49Z")

</div>

Hello Again,

Since the source is there and the hardware is capable; we can roll our own driver - but, will Particle officially support this option in the foreseeable future?

Thanks,  
jwest

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [July 29, 2015, 8:56am UTC](https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535/4 "2015-07-29T08:56:22Z")

</div>

This could be a useful PR (pull request) 👍

---

<div class="post-metadata">

**Author:** ![jwest](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jwest](https://community.particle.io/u/jwest)\
**Post date:** [July 29, 2015, 9:38pm UTC](https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535/5 "2015-07-29T21:38:06Z")

</div>

Thanks - I agree (I think).

---

<div class="post-metadata">

**Author:** ![PatrikHe](https://avatars.discourse-cdn.com/v4/letter/p/7feea3/32.png) [@PatrikHe](https://community.particle.io/u/PatrikHe)\
**Post date:** [January 15, 2016, 12:47pm UTC](https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535/6 "2016-01-15T12:47:36Z")

</div>

Any follow up on this?  
I’m currently trying to use my photon as SPI slave and could really benefit from any advances you might have made.

---

<div class="post-metadata">

**Author:** ![jwest](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jwest](https://community.particle.io/u/jwest)\
**Post date:** [February 18, 2016, 11:41pm UTC](https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535/7 "2016-02-18T23:41:59Z")

</div>

Hello,

Yes - what is the current status on this request?

Thanks,  
John W.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [February 19, 2016, 6:20am UTC](https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535/8 "2016-02-19T06:20:56Z")

</div>

Had you filed a pull request back then?

---

<div class="post-metadata">

**Author:** ![mdma](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/mdma/32/1924_2.png) [@mdma](https://community.particle.io/u/mdma)\
**Post date:** [February 27, 2016, 12:52am UTC](https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535/9 "2016-02-27T00:52:59Z")

</div>

There’s a PR here for SPI slave support. For those building locally, we’d love you to test and give feedback! [https://github.com/spark/firmware/pull/882](https://github.com/spark/firmware/pull/882)

---

<div class="post-metadata">

**Author:** ![mohammedmidhat](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@mohammedmidhat](https://community.particle.io/u/mohammedmidhat)\
**Post date:** [April 9, 2016, 11:09am UTC](https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535/10 "2016-04-09T11:09:41Z")

</div>

Is this officially part of the latest firmware? What was the feedback on it? Does it actually work?

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [April 9, 2016, 12:43pm UTC](https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535/11 "2016-04-09T12:43:24Z")

</div>

Just have a look here and the respective links inside it  
[Particle Device OS Updates Thread - #20 by BDub](https://community.particle.io/t/particle-firmware-update-thread/14378/20)

> [@](#):
>
> [Photon/Electron] [SPI Slave support](https://docs.particle.io/reference/firmware/photon/#begin-spi_mode-uint16_t-) #882

---

<div class="post-metadata">

**Author:** ![BDub](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bdub/32/5137_2.png) [@BDub](https://community.particle.io/u/BDub)\
**Post date:** [April 9, 2016, 11:52pm UTC](https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535/12 "2016-04-09T23:52:33Z")

</div>

Yes it works 🙂 Tested at 1MHz and 15MHz. There are on-device integration tests as well:

> **[particle-iot/firmware](https://github.com/particle-iot/firmware/tree/develop/user/tests/wiring/spi_master_slave)**
>
> firmware - Device OS (Firmware) for Particle Devices

Be sure to get your wiring correct before you power up!

---

<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:** [April 9, 2016, 11:59pm UTC](https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535/13 "2016-04-09T23:59:48Z")

</div>

It almost works at 60 MHz even! I’ve been transmitting 1000 64-byte blocks per second and I’m getting some errors, flipped bits and bit shift; trying to decide whether it makes sense to error detect or slow it down. Also, I’m using two prototype boards and 4” wires between them, which surely doesn’t help.  
successCount=4361134 failureCount=1512 tooFastCount=0

---

<div class="post-metadata">

**Author:** ![mohammedmidhat](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@mohammedmidhat](https://community.particle.io/u/mohammedmidhat)\
**Post date:** [April 10, 2016, 7:49am UTC](https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535/14 "2016-04-10T07:49:39Z")

</div>

Thank you very much. This is cool 🙂

---

<div class="post-metadata">

**Author:** ![jwest](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jwest](https://community.particle.io/u/jwest)\
**Post date:** [June 24, 2016, 6:07am UTC](https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535/15 "2016-06-24T06:07:42Z")

</div>

I am curious about this in the SPI example code:

```cpp
/* executes once at startup */
void setup() {
    Serial.begin(9600);
    for (int i = 0; i < sizeof(tx_buffer); i++)
      tx_buffer[i] = (uint8_t)i;
    SPI.onSelect(onSelect);
    SPI.begin(SPI_MODE_SLAVE, A2);
}

```

Why is Serial.begin(9600) there?  
And, in the section regarding the SPI clock - in slave mode the master drives the clock - in the explanation on the SPI page that isn’t clear; in fact I would say it is confusing. Since SPI is a serially clocked bus; and if the master is clocking - the SPI slave just needs to be able to detect the SPI clk - and has nothing to do with driving the SPI clk.

Regards,  
jwest

---

<div class="post-metadata">

**Author:** ![jwest](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jwest](https://community.particle.io/u/jwest)\
**Post date:** [June 24, 2016, 10:01am UTC](https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535/16 "2016-06-24T10:01:00Z")

</div>

Also - is there any photon example code showing the spi slave setup; remaining connected to WiFi, and posting data to the cloud (all in the same app)?

I upgraded to 0.5.1 and managed to almost brick my photons. Nice.

---

<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:** [June 24, 2016, 10:13am UTC](https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535/17 "2016-06-24T10:13:21Z")

</div>

The Serial.begin(9600) call has nothing to do with the SPI itself. It’s just there because the part of the sample code in loop() prints something to the serial debug console, and you need to call Serial.begin to initialize it first.

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [June 24, 2016, 12:02pm UTC](https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535/18 "2016-06-24T12:02:43Z")

</div>

> [@jwest](#):
>
> And, in the section regarding the SPI clock - in slave mode the master drives the clock - in the explanation on the SPI page that isn't clear;

I looked at the documentation and I see what you mean. There is no clear description of the SPI pin interactions described for master and slave modes. In slave mode, the photon CLK and CS pins are an inputs whereas in master mode, they are outputs.

I also noticed that the slave example will starve the system firmware since neither `SYSTEM_MODE(MANUAL)` or `SYSTEM_THREAD(ENABLED)` is indicated nor is `Particle.process()` called in the `while(1)` loop of the example. This should be fixed IMO.

---

<div class="post-metadata">

**Author:** ![jwest](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jwest](https://community.particle.io/u/jwest)\
**Post date:** [June 25, 2016, 10:06am UTC](https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535/19 "2016-06-25T10:06:23Z")

</div>

Agreed. For those that could be novices on this subject; the vagueness with which those instructions were written would and can cause a lot of confusion.

As it is - I hooked up my serial bus analyzer to verify which SPI Mode I was communicating in - as well as making sure SPI CS was behaving properly. The instructions do not give you the confidence one would need to write those routines without the use of an analyzer.

Things such as default SPI Modes should be clearly called out - that isn’t clear to me either.

---

<div class="post-metadata">

**Author:** ![jwest](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jwest](https://community.particle.io/u/jwest)\
**Post date:** [June 27, 2016, 7:23am UTC](https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535/20 "2016-06-27T07:23:36Z")

</div>

Particle,

In slave mode - I can only seem to get this to work:

SPI.setBitOrder(LSBFIRST);

Have you guys fully tested MSBFIRST? Just wondering.

If you have - can you please post an example with your COMPLETE set-up in slave mode? Including setting (default) mode, etc?

For my master microcontroller; I have stepped through all modes, and MSBFIRST and LSBFIRST - I was only able to get _anything_ to work with LSBFIRST.

Thanks,  
John W.

[Next page](https://community.particle.io/t/photon-can-the-photon-be-used-as-a-spi-slave-with-the-existing-firmware-primitives/13535.md?page=2)
