Using 2nd and third SPI Bus.

Status
Not open for further replies.

marinek9

Member
I am prototyping a piece of exercise equipment for research. This machine measures the tension in elastic bands attached to bar. The anchor point is attached to a cable that is controlled by a DC motor. This alows for the modulation of the load lifted in real time. Currently I am using a 3.5 with a load cell amp (HX711) and Adafruit BLE Breakout (using SPI). I need top add a draw wire linear position sensor. I have Super Droid ls7366r Encoder Buffer board (https://www.superdroidrobots.com/shop/item.aspx/dual-ls7366r-quadrature-encoder-buffer/1523/) to read the encoder count and pass counts. Both the BLE and the Encoder work independantly on SPI, but I cant get bot to work either by sharing the SPI (MOSI, MISO, & SCK) and assinging a seprate CS pin.

Is there a method i to initialize the SPI1 or 2? Ive changed the SPI to SPi1 in the Encoder H file, but the compiler sends an error.

Code:
#include <Adafruit_BLE_UART.h>
#include <SPI.h>
#include <Encoder_Buffer.h>
#define ADAFRUITBLE_REQ 10 // Buffer CS 20
#define ADAFRUITBLE_RDY 2
#define ADAFRUITBLE_RST 9


  
const int ENCODER_BUFFER_CS_PIN = 15;//do i need to initilize this pin to be a CS? 

//Encoder shres MOSI,MISO, and SCK opn SPI
//How can I use SPI1 for the Encoder

 
Adafruit_BLE_UART uart = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);
Encoder_Buffer Encoder1(ENCODER_BUFFER_CS_PIN);

void aciCallback(aci_evt_opcode_t event)
{
  switch (event)
    {
     case ACI_EVT_DEVICE_STARTED:
       Serial.println("Advertising started");
       break;
     case ACI_EVT_CONNECTED:
       Serial.println("Connected!");
       break;
     case ACI_EVT_DISCONNECTED:
       Serial.println("Disconnected or advertising timed out");
       break;
     default:
       break;
    }
}

void setup(){
   // set the slaveSelectPin as an output:
  pinMode (ENCODER_BUFFER_CS_PIN, OUTPUT);
  // initialize SPI:
  SPI.begin(); 
  Serial.begin(9600);
  while (!Serial);
  uart.setACIcallback(aciCallback);
  uart.setDeviceName("uart");
  uart.begin();
  uart.pollACI();
}

void loop() { 
 uart.pollACI();
 Serial.println(Encoder1.readEncoder());
 delay(10);
}
 
https://github.com/SuperDroidRobots/Encoder-Buffer-Library/blob/master/Encoder_Buffer.cpp

According to the CPP:
Code:
Encoder_Buffer::Encoder_Buffer(int _slaveSelectEnc){
  
 slaveSelectEnc = _slaveSelectEnc;

 
  pinMode(slaveSelectEnc, OUTPUT);
  SPI.begin();
   
}

The SPI bus is hard coded. To support multiple busses the author, or you, or anyone else (since it's open source), may modify the library to support multiple busses, or make it use the other bus by changing SPI to SPI1 or SPI2

Unfortunately, it doesn't allow SPI transactions either. The BLE and Encoder can work on the same bus but will take a bit of effort to include SPI transactions
 
Last edited:
I tried to modify the buffer header to SPI1, but the compiler wont accept it... gave a no member named SPI1 error.. Do I have to create a separate <SPI1.h> file??
 
I tried to modify the buffer header to SPI1, but the compiler wont accept it... gave a no member named SPI1 error.. Do I have to create a separate <SPI1.h> file??

SPI1 is only defined if you are compiling for Teensy LC, Teensy 3.5, or Teensy 3.6. So make sure you have the correct chipset targeted.

The message "no member named SPI1" means that you probably are trying to use it incorrectly. SPI1 is a global variable, much like SPI is. So instead of:

Code:
Encoder_Buffer::Encoder_Buffer(int _slaveSelectEnc){
   slaveSelectEnc = _slaveSelectEnc;
  pinMode(slaveSelectEnc, OUTPUT);
  SPI.begin();
}

You would write:

Code:
Encoder_Buffer::Encoder_Buffer(int _slaveSelectEnc){
  slaveSelectEnc = _slaveSelectEnc;
  pinMode(slaveSelectEnc, OUTPUT);
  SPI1.begin();
}

And similarly, change all "SPI." instances to "SPI1.".
 
Status
Not open for further replies.
Back
Top