Using Adafruit Bluefruit LE SPI Friend with Photon

Hi,

I have compatibility problems with the above mentioned module and I hope any of you guys will be able to help :smile: .
I recently got my hands on an Adafruit Bluefruit LE SPI Friend 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

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

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:

    bool SPIBegan = false;

    void Adafruit_BluefruitLE_SPI::beginSPI(){
    if(!SPIBegan){
    SPI1.begin(m_cs_pin); //D5
    setSPI();
    SPIBegan = true;
    }
    }

    void Adafruit_BluefruitLE_SPI::endSPI(){
    if(SPIBegan){
    SPI1.end();
    SPIBegan = false;
    }
    }

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

1 Like

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!

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

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 :slight_smile:

1 Like

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

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))

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

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.

1 Like

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!

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 :slight_smile:

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

4 Likes

Thank you very much rickkas7 !

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.
But, I unable to get any data from BLE.

Thank you in advance !

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.

1 Like

Thank you rickkas7. Please update when u get a chance

rickkas7 have u verified th code for UART module?

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.

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