# Using Adafruit Bluefruit LE SPI Friend with Photon

**URL:** <https://community.particle.io/t/using-adafruit-bluefruit-le-spi-friend-with-photon/18189>\
**Category:** Hardware\
**Created:** [December 11, 2015, 8:44am UTC](https://community.particle.io/t/using-adafruit-bluefruit-le-spi-friend-with-photon/18189 "2015-12-11T08:44:05Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![itd24](https://avatars.discourse-cdn.com/v4/letter/i/c77e96/32.png) [@itd24](https://community.particle.io/u/itd24)\
**Post date:** [December 11, 2015, 8:44am UTC](https://community.particle.io/t/using-adafruit-bluefruit-le-spi-friend-with-photon/18189/1 "2015-12-11T08:44:05Z")

</div>

Hi,

I have compatibility problems with the above mentioned module and I hope any of you guys will be able to help 😄 .  
I recently got my hands on an [Adafruit Bluefruit LE SPI Friend](https://learn.adafruit.com/introducing-the-adafruit-bluefruit-spi-breakout/introduction) module. There is port available yet for the photon, but porting the arduino library was relatively simple (or so I thought).

I connected the module to the SPI1 pins, the IRQ pin is connected to D6 and the RST pin is connected to D7 and the CS pin of the module is connected to D5 (I am also defining D5 as the SS pin). While triggering the factory reset of my module works just fine (using the RST pin, sending the “AT+FACTORYRESET” command doesn’t work), there doesn’t seem to flow any information from the photon to the module or vice versa. I double checked the connections, all pins are connected correctly. I am using hardware SPI, however, I also tried to use software SPI, there was no difference.

I suspect I messed up the SPI settings. The library initially set the SPI settings to 4MHz, MSBFIRST and SPI\_MODE0:

```
SPISettings bluefruitSPI(4000000, MSBFIRST, SPI_MODE0);

```

While porting the library, I used the same settings:

```
SPIClass UsedSPI = SPI1;

void Adafruit_BluefruitLE_SPI::setSPI(){
    UsedSPI.setBitOrder(MSBFIRST);
    UsedSPI.setClockSpeed(4000000);
    UsedSPI.setDataMode(SPI_MODE0);
}

```

while looking for answers I came across the “SETCLOCKDIVIDERREFERENCE” function, but I don’t think I need to use it since I don’t call the SetClockDivider function anywhere.

I tried sending multiple commands. After sending a command, the photon expects an answer. The module should indicate, that there is data available, by setting the IRQ pin to hight, but the pin is never high (so no data available).

I am using the latest firmware on both, the photon and the module. The library, I’m trying to port, can be found here:  
[https://github.com/adafruit/Adafruit\_BluefruitLE\_nRF51](https://github.com/adafruit/Adafruit_BluefruitLE_nRF51)

I’d be happy to make the port available to the community, if by some miracle I’ll manage to make it work 😄

---

<div class="post-metadata">

**Author:** ![itd24](https://avatars.discourse-cdn.com/v4/letter/i/c77e96/32.png) [@itd24](https://community.particle.io/u/itd24)\
**Post date:** [December 11, 2015, 11:26am UTC](https://community.particle.io/t/using-adafruit-bluefruit-le-spi-friend-with-photon/18189/2 "2015-12-11T11:26:30Z")

</div>

Looks like I found the answer myself. If anyone has a similar problem porting an arduino library, here is what was wrong in my case:

1. it’s a little while since I developed in C++ and I forgot a few basics. I wanted to save the SPI object into a variable so I could easily switch between SPI and SPI1. C++ doesn’t work like C# and objects are copied by value rather than by reference. I forgot that little but important difference.

2. the library I wanted to port calls “SPI1.begin” rather generously. I don’t know how it is supposed to work on arduino, but on the Photon you only have to call SPI1.begin once (and every time you close it with SPI1.end). I solved this problem simply by wrapping the SPI calls in my own functions and adding a switch to the library:

That’s it. Noob problems solved 😛  
As promised, I’ll make the port available to the community as soon as I work out the kinks.

---

<div class="post-metadata">

**Author:** ![pietroGheo](https://avatars.discourse-cdn.com/v4/letter/p/b4bc9f/32.png) [@pietroGheo](https://community.particle.io/u/pietroGheo)\
**Post date:** [February 17, 2016, 11:18pm UTC](https://community.particle.io/t/using-adafruit-bluefruit-le-spi-friend-with-photon/18189/3 "2016-02-17T23:18:48Z")

</div>

Hello Itd24,

I am trying to configure my photon to make it able to work with the the Bluefruit LE SPI friend module. I have not so much experience with Photon and I’m having some trouble porting the library.

I have some questions for you:  
What are the main steps to do in order to port the Arduino library to make it photon compatible? Is generating the SPI signal the only issue I should take care of?  
Should I learn more about SPI protocol in order to make it?

Eventually could you share your ported library?

Thank you!

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [February 17, 2016, 11:39pm UTC](https://community.particle.io/t/using-adafruit-bluefruit-le-spi-friend-with-photon/18189/4 "2016-02-17T23:39:49Z")

</div>

SPI is already available on the Particle platform. You just need to fix the platform (avr) specific code to work if there’s any.

---

<div class="post-metadata">

**Author:** ![itd24](https://avatars.discourse-cdn.com/v4/letter/i/c77e96/32.png) [@itd24](https://community.particle.io/u/itd24)\
**Post date:** [February 23, 2016, 8:17am UTC](https://community.particle.io/t/using-adafruit-bluefruit-le-spi-friend-with-photon/18189/5 "2016-02-23T08:17:55Z")

</div>

Hello pietroGheo,

Once you get the hang of it, porting an Arduino library isn’t much of a problem. If you take the original Arduino library provided by Adafruit and you try to compile it with the Particle CLI, you get a few errors, mostly due to unsupported functions, like F(), and unknown libraries (“Arduino.h”).

If you replace the Arduino.h library with “application.h” and substitute the unknown functions, it should work.

the Photon isn’t much different from the Arduino, the code syntax is almost the same. SPI communication works the same way, so if you look into the SPI protocol a little, you should be able to make it work.

I have to go through the code and polish it a little before I share it, I just have to find the time 🙂

---

<div class="post-metadata">

**Author:** ![pietroGheo](https://avatars.discourse-cdn.com/v4/letter/p/b4bc9f/32.png) [@pietroGheo](https://community.particle.io/u/pietroGheo)\
**Post date:** [February 23, 2016, 9:30pm UTC](https://community.particle.io/t/using-adafruit-bluefruit-le-spi-friend-with-photon/18189/6 "2016-02-23T21:30:21Z")

</div>

Thank you guys for the answers.

I am working on porting the library right now and I am facing some problems about non existing types.  
I got the following error:

```
Adafruit_BluefruitLE_SPI.cpp:46:1: error: 'SPISettings' does not name a type
SPISettings bluefruitSPI(4000000, MSBFIRST, SPI_MODE0);
^
Adafruit_BluefruitLE_SPI.cpp: In member function 'bool
Adafruit_BluefruitLE_SPI::sendPacket(uint16_t, const uint8_t*, uint8_t, uint8_t)':
Adafruit_BluefruitLE_SPI.cpp:240:47: error: 'highByte' was not declared in this scope
msgCmd.header.cmd_id_high = highByte(command);
^
    Adafruit_BluefruitLE_SPI.cpp:241:46: error: 'lowByte' was not declared in this scope
msgCmd.header.cmd_id_low = lowByte(command);
^
Adafruit_BluefruitLE_SPI.cpp:250:9: error: 'class SPIClass' has no member named 'beginTransaction'
SPI.beginTransaction(bluefruitSPI);
^
Adafruit_BluefruitLE_SPI.cpp:250:26: error: 'bluefruitSPI' was not declared in this scope
SPI.beginTransaction(bluefruitSPI);
^
Adafruit_BluefruitLE_SPI.cpp:274:9: error: 'class SPIClass' has no member named 'endTransaction'
SPI.endTransaction();
^
Adafruit_BluefruitLE_SPI.cpp: In member function 'bool Adafruit_BluefruitLE_SPI::getPacket(sdepMsgResponse_t*)':
Adafruit_BluefruitLE_SPI.cpp:546:9: error: 'class SPIClass' has no member named 'beginTransaction'
SPI.beginTransaction(bluefruitSPI);
^
Adafruit_BluefruitLE_SPI.cpp:546:26: error: 'bluefruitSPI' was not declared in this scope
SPI.beginTransaction(bluefruitSPI);
^
Adafruit_BluefruitLE_SPI.cpp:603:71: error: 'word' was not declared in this scope

```

It seems that I didn’t import the library with the SPIsettings class definition or that the class and some functions’ names are different for Particle.

Do you have any suggestion how to solve this problem?

Thank you

---

<div class="post-metadata">

**Author:** ![itd24](https://avatars.discourse-cdn.com/v4/letter/i/c77e96/32.png) [@itd24](https://community.particle.io/u/itd24)\
**Post date:** [February 24, 2016, 7:19am UTC](https://community.particle.io/t/using-adafruit-bluefruit-le-spi-friend-with-photon/18189/7 "2016-02-24T07:19:45Z")

</div>

As far as I know SPISettings doesn’t exist in the Particle environment, you will have to change the SPI initialisation to be able to use the library with a Photon. Instead of using the SPISettings object, just use the functions defined in the Particle SDK to set the SPI settings.

The highByte and lowByte functions don’t exist either. Just define them in the Adafruit\_BLE.h file like so:

```
#define lowByte(w) ((uint8_t)((w) & 0xFF))
#define highByte(w) ((uint8_t)((w) >> 8))
```

---

<div class="post-metadata">

**Author:** ![pietroGheo](https://avatars.discourse-cdn.com/v4/letter/p/b4bc9f/32.png) [@pietroGheo](https://community.particle.io/u/pietroGheo)\
**Post date:** [February 24, 2016, 7:51pm UTC](https://community.particle.io/t/using-adafruit-bluefruit-le-spi-friend-with-photon/18189/8 "2016-02-24T19:51:26Z")

</div>

Yes,

you are right, SPISettings doesn’t exist for particle. I managed to set the SPI settings (or that is what I think). I initialized the settings using the Particle SPI object, as you said in your previous post. The compiler now makes its job, but when I try to run the test case (atcommand) I cannot communicate with the sensor. I am able to open the serial communication but I don’t receive any answer from the sensor. Now I am double checking the wiring, but everything seems fine.

To start the communication I’m just invoking the begin() method in the SPIClass class. Is that enough?  
Is there any difference between SPI1 and SPI?

Thank you

---

<div class="post-metadata">

**Author:** ![itd24](https://avatars.discourse-cdn.com/v4/letter/i/c77e96/32.png) [@itd24](https://community.particle.io/u/itd24)\
**Post date:** [February 24, 2016, 8:43pm UTC](https://community.particle.io/t/using-adafruit-bluefruit-le-spi-friend-with-photon/18189/9 "2016-02-24T20:43:08Z")

</div>

The typical procedure for using SPI is:

Initialisation:  
call SPI.begin()  
set the SPI settings

reading procedure:  
set slave select pin to high  
call SPI.transfer()  
set slave select pin back to low

SPI and SPI1 use different pins, so which one you use depends on your wiring.

---

<div class="post-metadata">

**Author:** ![pietroGheo](https://avatars.discourse-cdn.com/v4/letter/p/b4bc9f/32.png) [@pietroGheo](https://community.particle.io/u/pietroGheo)\
**Post date:** [February 25, 2016, 12:09am UTC](https://community.particle.io/t/using-adafruit-bluefruit-le-spi-friend-with-photon/18189/10 "2016-02-25T00:09:41Z")

</div>

Hi Itd24,

I managed to make the BLE communicating with the Photon. There were a couple of errors in the SPI methods that I defined.

Now I am gonna try to collect signals with some sensors and send them to my laptop via the BLE.

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![chillyjee](https://avatars.discourse-cdn.com/v4/letter/c/b4bc9f/32.png) [@chillyjee](https://community.particle.io/u/chillyjee)\
**Post date:** [April 12, 2016, 1:11pm UTC](https://community.particle.io/t/using-adafruit-bluefruit-le-spi-friend-with-photon/18189/11 "2016-04-12T13:11:37Z")

</div>

Hello, can we get some of your code here ? Or example code ?

I am having so much trouble to solve the connection and I thought it could be useful for further users to share the knowledge 🙂

---

<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 12, 2016, 7:05pm UTC](https://community.particle.io/t/using-adafruit-bluefruit-le-spi-friend-with-photon/18189/12 "2016-04-12T19:05:05Z")

</div>

I ported the Adafruit BlueFruitLE nRF51 library to the Particle and added it to the community libraries as “Adafruit\_BLE"

The Github repository is here: [https://github.com/rickkas7/Adafruit\_BLE](https://github.com/rickkas7/Adafruit_BLE)

---

<div class="post-metadata">

**Author:** ![chillyjee](https://avatars.discourse-cdn.com/v4/letter/c/b4bc9f/32.png) [@chillyjee](https://community.particle.io/u/chillyjee)\
**Post date:** [April 13, 2016, 6:21pm UTC](https://community.particle.io/t/using-adafruit-bluefruit-le-spi-friend-with-photon/18189/13 "2016-04-13T18:21:33Z")

</div>

Thank you very much rickkas7 !

---

<div class="post-metadata">

**Author:** ![swapn](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@swapn](https://community.particle.io/u/swapn)\
**Post date:** [June 8, 2016, 10:56pm UTC](https://community.particle.io/t/using-adafruit-bluefruit-le-spi-friend-with-photon/18189/14 "2016-06-08T22:56:27Z")

</div>

Hello rickkas7 thanks for your library. I am interfacing photon with adafruit flora BLE linked from here [https://learn.adafruit.com/adafruit-flora-bluefruit-le/overview](https://learn.adafruit.com/adafruit-flora-bluefruit-le/overview).  
But, I unable to get any data from BLE.

Thank you in advance !

---

<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 8, 2016, 11:03pm UTC](https://community.particle.io/t/using-adafruit-bluefruit-le-spi-friend-with-photon/18189/15 "2016-06-08T23:03:58Z")

</div>

I don’t have a Adafruit Bluefruit LE serial device, which is what the Flora is. I only tested the Bluefruit SPI device, so there’s a reasonable possibility there’s a bug in the serial code port, because I didn’t even look at it other than making sure it compiled. I’ll order one and test it, but it will take around a week.

---

<div class="post-metadata">

**Author:** ![swapn](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@swapn](https://community.particle.io/u/swapn)\
**Post date:** [June 9, 2016, 4:33pm UTC](https://community.particle.io/t/using-adafruit-bluefruit-le-spi-friend-with-photon/18189/16 "2016-06-09T16:33:04Z")

</div>

Thank you rickkas7. Please update when u get a chance

---

<div class="post-metadata">

**Author:** ![swapn](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@swapn](https://community.particle.io/u/swapn)\
**Post date:** [August 9, 2016, 6:29pm UTC](https://community.particle.io/t/using-adafruit-bluefruit-le-spi-friend-with-photon/18189/17 "2016-08-09T18:29:38Z")

</div>

rickkas7 have u verified th code for UART module?

---

<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:** [August 9, 2016, 6:58pm UTC](https://community.particle.io/t/using-adafruit-bluefruit-le-spi-friend-with-photon/18189/18 "2016-08-09T18:58:20Z")

</div>

There is a bug in the UART version of the code. It crashes and I was meaning to figure out why, and completely forgot about it. Sorry about that.

---

<div class="post-metadata">

**Author:** ![swapn](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@swapn](https://community.particle.io/u/swapn)\
**Post date:** [August 9, 2016, 6:59pm UTC](https://community.particle.io/t/using-adafruit-bluefruit-le-spi-friend-with-photon/18189/19 "2016-08-09T18:59:39Z")

</div>

no worries, thank you for reply let me know when u figured it out.
