0

Feather 32u4 compatible with Software SPI?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Feather 32u4 compatible with Software SPI?

by mckw on Mon Apr 15, 2019 12:10 pm

Let me preface by saying that I am not super experienced with electronics. I have a Adafruit Feather 32u4 that is using Hardware SPI to communicate over Bluetooth. Unfortunately, I'm also hoping to use the SD library, which also uses Hardware SPI. I am hoping to change the board to use Software SPI, instead, so that the two may work together (currently they won't). I cannot seem to change the SD library to use SW SPI instead.

The Bluefruit code by default has the following commented out :

Code: Select all | TOGGLE FULL SIZE
/* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO,
                             BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS,
                             BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);


Which leads me to believe I could use SW SPI? But simply uncommenting this and commenting out the HW SPI doesn't seem to work.

I am just not sure if it's not compatible with the board, or I need to do more than this. Thanks for the help.

mckw
 
Posts: 6
Joined: Mon Apr 15, 2019 12:07 pm

Re: Feather 32u4 compatible with Software SPI?

by adafruit_support_bill on Mon Apr 15, 2019 12:17 pm

You should be able to use both with hardware SPI. They can share the MISO, MOSI and SCK pins. You will need to use a separate CS pin for each board.

Software SPI is also possible. But to do that you would need separate pins for all 4 of the SPI signals: MISO, MOSI, SCK and CS.

adafruit_support_bill
 
Posts: 72282
Joined: Sat Feb 07, 2009 10:11 am

Re: Feather 32u4 compatible with Software SPI?

by mckw on Mon Apr 15, 2019 12:48 pm

adafruit_support_bill wrote:You should be able to use both with hardware SPI. They can share the MISO, MOSI and SCK pins. You will need to use a separate CS pin for each board.

Software SPI is also possible. But to do that you would need separate pins for all 4 of the SPI signals: MISO, MOSI, SCK and CS.


Okay.

Based on what I can tell with the board, is the board's CS set to 8. I have SD using pin 10 as its CS. It's not a separate board, technically, just part of the SD library using HW SPI. But with these two values set, the BLE won't work - just the SD.

Do I need to do some sort of slave select?

EDIT: I should mention that I can connect with the board, but it needs SPI to use UART.

EDIT 2: If I set all of the SW pins to pins not being used by SD, it similarly doesn't work. I can post a picture of the circuit / code, if needed.

mckw
 
Posts: 6
Joined: Mon Apr 15, 2019 12:07 pm

Re: Feather 32u4 compatible with Software SPI?

by Blueshark on Mon Apr 15, 2019 12:59 pm

mckw

What kind/brand of SD module are you using; is it from Adafruit or else where?
If its not from Adafruit, do you know if the SD card Module is 5V or 3V?

let the admin know...


Cheers...

Blueshark
 
Posts: 105
Joined: Thu Dec 21, 2017 7:19 pm

Re: Feather 32u4 compatible with Software SPI?

by adafruit_support_bill on Mon Apr 15, 2019 1:00 pm

A list of the components you are using would be helpful. A photo would be good too.

adafruit_support_bill
 
Posts: 72282
Joined: Sat Feb 07, 2009 10:11 am

Re: Feather 32u4 compatible with Software SPI?

by mckw on Mon Apr 15, 2019 1:14 pm

adafruit_support_bill wrote:A list of the components you are using would be helpful. A photo would be good too.


Board: Adafruit Feather 32u4 BLE
GPS Shield / SD Card: Sparkfun Mini GPS Shield (https://www.sparkfun.com/products/14030)

I have tried using both the Arduino built in SD.h library, as well as SdFat (https://github.com/greiman/SdFat).

I believe SD Module is 3.3V.

Board pins as follows:
#define BLUEFRUIT_SPI_CS 8
#define BLUEFRUIT_SPI_IRQ 7
#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused

GPS Shield / SD Pins:
Chip Select 10
Otherwise connected to MISO/MOSI/SCK

Orange wires are connecting SD Module to the main board.
Attachments
IMG_20190415_111230.jpg
IMG_20190415_111230.jpg (753.58 KiB) Viewed 61 times

mckw
 
Posts: 6
Joined: Mon Apr 15, 2019 12:07 pm

Re: Feather 32u4 compatible with Software SPI?

by adafruit_support_bill on Mon Apr 15, 2019 3:00 pm

OK. The wiring looks right. Can you post the code you have so far?

adafruit_support_bill
 
Posts: 72282
Joined: Sat Feb 07, 2009 10:11 am

Re: Feather 32u4 compatible with Software SPI?

by mckw on Mon Apr 15, 2019 3:10 pm

adafruit_support_bill wrote:OK. The wiring looks right. Can you post the code you have so far?


I tried to strip out some of the comments and stuff, because it's busy. Basically, the SD card initializes properly, and my phone seems to connect over BT, but then I can't actually do anything over BLE.

Code: Select all | TOGGLE FULL SIZE
/*********************************************************************
 This is an example for our nRF51822 based Bluefruit LE modules

 MIT license, check LICENSE for more information
 All text above, and the splash screen below must be included in
 any redistribution
*********************************************************************/

#include <Arduino.h>
#include <EEPROM.h>
#include <SPI.h>
#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_)
  #include <SoftwareSerial.h>
#endif

#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"
#include "SdFat.h"
#include "BluefruitConfig.h"
#include <TinyGPS++.h>

#define RX_PIN  9 // GPS TX
#define TX_PIN  5 // GPS RX
#define GPS_BAUD  9600  // GP-735 default baud rate
#define CHIP_SELECT 10  // SD card

#define FACTORYRESET_ENABLE         0
#define MINIMUM_FIRMWARE_VERSION    "0.6.6"
#define MODE_LED_BEHAVIOUR          "MODE"

TinyGPSPlus gps;
SoftwareSerial ss(RX_PIN,TX_PIN);


// Pin Configuration and Firmware Declarations

#define LED_PIN       13

const unsigned long
  BLINKTIME =         1000;
 
unsigned long
  t_blink =           0L;

int
  blinkState =        LOW;


/* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);


// A small helper
void error(const __FlashStringHelper*err) {
  Serial.println(err);
  while (1);
}


//// SD Card

// Test with reduced SPI speed for breadboards.  SD_SCK_MHZ(4) will select
// the highest speed supported by the board that is not over 4 MHz.
// Change SPI_SPEED to SD_SCK_MHZ(50) for best performance.
#define SPI_SPEED SD_SCK_MHZ(4)

SdFat sd;


/**************************************************************************/
/*!
    @brief  Sets up the HW an the BLE module (this function is called
            automatically on startup)
*/
/**************************************************************************/
void setup(void)
{
  pinMode(LED_PIN, OUTPUT);
  pinMode(RX_PIN, INPUT);
  pinMode(TX_PIN, OUTPUT);
 
  while (!Serial);  // required for Flora & Micro
  delay(500);
 
  Serial.begin(115200);

  // SD Card initializtion

  if (!sd.begin(CHIP_SELECT, SPI_SPEED)) {
    Serial.println(F("SD Error"));
  }

  Serial.println("SD Card init successful");

  // GPS

  ss.begin(GPS_BAUD); // Begin serial communication with GPS


  // Initializing BLE

  /* Initialise the module */
  Serial.print(F("Initialising the Bluefruit LE module: "));

  if ( !ble.begin() )
  {
    error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
  }
  Serial.println( F("OK!") );

  if ( FACTORYRESET_ENABLE )
  {
    /* Perform a factory reset to make sure everything is in a known state */
    Serial.println(F("Performing a factory reset: "));
    if ( ! ble.factoryReset() ){
      error(F("Couldn't factory reset"));
    }
  }

  /* Disable command echo from Bluefruit */
  ble.echo(false);

  Serial.println("Requesting Bluefruit info:");
  /* Print Bluefruit information */
  ble.info();

  Serial.println(F("Please use Adafruit Bluefruit LE app to connect in UART mode"));
  Serial.println(F("Then Enter characters to send to Bluefruit"));
  Serial.println();

  ble.verbose(false);  // debug info is a little annoying after this point!

  /* Wait for connection */
  while (! ble.isConnected()) {
      delay(500);
  }

  // LED Activity command is only supported from 0.6.6
  if ( ble.isVersionAtLeast(MINIMUM_FIRMWARE_VERSION) )
  {
    // Change Mode LED Activity
    Serial.println(F("******************************"));
    Serial.println(F("Change LED activity to " MODE_LED_BEHAVIOUR));
    ble.sendCommandCheckOK("AT+HWModeLED=" MODE_LED_BEHAVIOUR);
    Serial.println(F("******************************"));
  }

}

/**************************************************************************/
/*!
 *
    @brief  Constantly poll for new command or response data
*/
/**************************************************************************/
void loop(void)

  // Now Check for incoming characters from Bluefruit
  ble.println("AT+BLEUARTRX");
  ble.readline();
  ble.waitForOK();

  String BLEbuffer = ble.buffer;

  if (BLEbuffer.length() && BLEbuffer.indexOf("OK") == -1)
    Serial.print(F("[Recv] ")); Serial.println(BLEbuffer);
 
  if (BLEbuffer.indexOf("Status") >= 0) {
    Serial.println(F("Status Request Received"));
    ble.print("AT+BLEUARTTX=");
    if (t_blink) {
      ble.println("BLINK");
    }
    else {
      if (blinkState)
        ble.println("ON");
      else
        ble.println("OFF");
    }

    // check response stastus
    if (! ble.waitForOK() ) {
      Serial.println(F("Failed to get response"));
    }

    ble.println("AT+BLEUARTRX");
   
  }
 
  BLEbuffer = "";

  if(gps.location.isValid()){ // GPS has a fix
    digitalWrite(LED_PIN,HIGH); // Turn LED on
  }
  else  // GPS is looking for satellites, waiting on fix
  {
    digitalWrite(LED_PIN,LOW);  // Turn LED off
    Serial.print(F("No location"));
  }
  smartDelay(1000);
}

// Delay ms while still reading data packets from GPS
static void smartDelay(unsigned long ms)
{
  unsigned long start = millis();
  do
  {
    while(ss.available())
    {
      gps.encode(ss.read()); 
    }
  } while(millis() - start < ms);
}

mckw
 
Posts: 6
Joined: Mon Apr 15, 2019 12:07 pm

Re: Feather 32u4 compatible with Software SPI?

by adafruit_support_bill on Mon Apr 15, 2019 3:41 pm

Lets simplify things a bit and remove the GPS SoftwareSerial from the equation. The 32U4 actually has a spare hardware serial port you can use. But for now see if we can get just the BLE and SD to play together.

adafruit_support_bill
 
Posts: 72282
Joined: Sat Feb 07, 2009 10:11 am

Re: Feather 32u4 compatible with Software SPI?

by mckw on Mon Apr 15, 2019 3:59 pm

adafruit_support_bill wrote:Lets simplify things a bit and remove the GPS SoftwareSerial from the equation. The 32U4 actually has a spare hardware serial port you can use. But for now see if we can get just the BLE and SD to play together.


Oh jeez, okay. It works if I remove the GPS functionality. Apologies- I got the GPS working before I decided whether I wanted to use the SD module.

Do you have thoughts on why the GPS module would be causing problems? Perhaps the problem lies outside of SPI.

EDIT: Looks like my GPS and SD are sharing SPI, not BT and SD. So I guess that nulls this thread.

Thank you for the time, though.

mckw
 
Posts: 6
Joined: Mon Apr 15, 2019 12:07 pm

Re: Feather 32u4 compatible with Software SPI?

by adafruit_support_bill on Mon Apr 15, 2019 4:19 pm

Software serial is fairly processor intensive - especially with something like GPS which is continuously streaming data. It is possible that the interrupt overhead swamps the processor to the point where it can't handle the BT communication.

As I mentioned earlier, the 32U4 has a hardware serial port on pins 1 and 0 that you can use. That has much less overhead than software serial.

adafruit_support_bill
 
Posts: 72282
Joined: Sat Feb 07, 2009 10:11 am

Re: Feather 32u4 compatible with Software SPI?

by mckw on Mon Apr 15, 2019 4:47 pm

adafruit_support_bill wrote:Software serial is fairly processor intensive - especially with something like GPS which is continuously streaming data. It is possible that the interrupt overhead swamps the processor to the point where it can't handle the BT communication.

As I mentioned earlier, the 32U4 has a hardware serial port on pins 1 and 0 that you can use. That has much less overhead than software serial.


GPS seemed to be working just fine with BT before ever trying to integrate SD. But I can try to move to HW serial.

mckw
 
Posts: 6
Joined: Mon Apr 15, 2019 12:07 pm

Please be positive and constructive with your questions and comments.