How to avoid speed reduction while using two SPI devices simultaneously?

DinoDart

New member
I am trying to use a Teensy 4.1 with two KX132 accelerometers in SPI and the SparkFun Arduino library to get acceleration data at the maximum possible ODR for the KX132, which is listed to be 26500 KHz.

I was able to get readings from one accelerometer at this speed, but I added a second and am running into issues, with the data output being about half of what I want when both accelerometers are hooked up.

Currently, I have the Teensy on a breadboard running off of USB power from my computer. The accelerometer breakout boards are connected to it with distinct CS pins but sharing SCK, MISO, and MOSI pins.

Would using one of the other SPI buses enable me to read the data at the maximum ODR with both accelerometers simultaneously? How would I go about doing this, or where can I find the documentation for it? I have tried looking and was left confused.

I've modified the default library SPI example for my purposes and attached it. Thanks in advance for any help you can offer!

C++:
/*
  example5-BasicReadings-SPI

  This example shows the basic settings and functions for retrieving accelerometer
  data using SPI.
  Please refer to the header file for more possible settings, found here:
  ..\SparkFun_KX13X_Arduino_Library\src\sfe_kx13x_defs.h

  Written by Elias Santistevan @ SparkFun Electronics, October 2022

  Product:

    https://www.sparkfun.com/products/17871

  Repository:

    https://github.com/sparkfun/SparkFun_KX13X_Arduino_Library

  SparkFun code, firmware, and software is released under the MIT
  License    (http://opensource.org/licenses/MIT).
*/

#include <SPI.h>
#include <SparkFun_KX13X.h> // Click here to get the library: http://librarymanager/All#SparkFun_KX13X
#include <Wire.h>

SparkFun_KX132_SPI kxAccel; //first KX132
SparkFun_KX132_SPI kxAccelTwo; //second KX132
//SparkFun_KX134_SPI kxAccel; // For the KX134, uncomment this and comment line above

outputData myData;   // Struct for the first accelerometer's data
outputData secondData; //Struct for the second accelerometer's data
const int chipSelect = 10; // Chip select for first accelerometer
const int chipSelect2 = 0;  //Chip select for second accelerometer
byte dataReadyPin = 3; //Pin for first accelerometer interrupt
byte dataReadySecond = 4; //Pin for second accelerometer interrupt

void setup()
{

  Serial.begin(115200);
  // Get the chip select pin ready.
  pinMode(chipSelect, OUTPUT);
  pinMode(chipSelect2, OUTPUT);

  digitalWrite(chipSelect, HIGH);
  digitalWrite(chipSelect2, HIGH);

  SPI.begin();

  // Wait for the Serial monitor to be opened.
  while (!Serial)
    delay(50);

  while (!kxAccel.begin(chipSelect))
  {
    Serial.print("Unique ID 1: 0x");
    Serial.println(kxAccel.getUniqueID(), HEX);
    Serial.println("Could not communicate with the first KX13X. Freezing.");
    delay(1000);
  }
 
  while (!kxAccelTwo.begin(chipSelect2)) {
    Serial.print("Unique ID 2: 0x");
    Serial.println(kxAccelTwo.getUniqueID(), HEX);
    Serial.println("Could not communicate with the second KX13X. Freezing.");
    delay(1000);
  }
 
  Serial.println("Ready.");
 
  // Reset the chip so that old settings don't apply to new setups.
  if (kxAccelTwo.softwareReset())
    Serial.println("Reset second accel.");
 
  delay(5);

 

  if (kxAccel.softwareReset())
    Serial.println("Reset.");

  // Give some time for the accelerometer to reset.
  // It needs two, but give it five for good measure.
  delay(5);

  // Many settings for KX13X can only be
  // applied when the accelerometer is powered down.
  // However there are many that can be changed "on-the-fly"
  // check datasheet for more info, or the comments in the
  // "...regs.h" file which specify which can be changed when.
  kxAccel.enableAccel(false);
  kxAccel.setRange(SFE_KX132_RANGE16G); // 16g Range
 
  // kxAccel.setRange(SFE_KX134_RANGE16G);         // 16g for the KX134

  kxAccel.enableDataEngine(); // Enables the bit that indicates data is ready.
  kxAccel.setOutputDataRate(15); // Default is 50Hz, this is 26500
  kxAccel.enablePhysInterrupt(); //enables interrupt pin 1
  kxAccel.routeHardwareInterrupt(0x10); //routes data ready bit to interrupt pin 1
  kxAccel.enableAccel();
 
  kxAccelTwo.enableAccel(false);
  kxAccelTwo.setRange(SFE_KX132_RANGE16G);
  kxAccelTwo.enableDataEngine(); // Enables the bit that indicates data is ready.
  kxAccelTwo.setOutputDataRate(15); // Default is 50Hz, this is 26500
  kxAccelTwo.enablePhysInterrupt(); //enables interrupt pin 1
  kxAccelTwo.routeHardwareInterrupt(0x10);
  kxAccelTwo.enableAccel();
 

}
int counter = 0;
void loop()
{
  // Check if data is ready.
  if (digitalRead(dataReadyPin) == HIGH || digitalRead(dataReadySecond) == HIGH)  //check for data ready
  {
    counter++;
    kxAccel.getAccelData(&myData);
    Serial.print("X1: ");
    Serial.print(myData.xData, 3);
    Serial.print(" Y1: ");
    Serial.print(myData.yData, 3);
    Serial.print(" Z1: ");
    Serial.print(myData.zData, 3);
    Serial.println();
 
    kxAccelTwo.getAccelData(&secondData);
   
    Serial.print("X2: ");
    Serial.print(secondData.xData, 3);
    Serial.print(" Y2: ");
    Serial.print(secondData.yData, 3);
    Serial.print(" Z2: ");
    Serial.print(secondData.zData, 3);
    Serial.println();
   
   
    Serial.println(counter);
   
  }
  //delay(1); // Delay should be 1/ODR (Output Data Rate), default is 1/50 ODR
}
 
For who? I can't speak for others, and I've not even looked in the library.

I have now, its 2K lines or more just to read an accelerometer, seems its a complex chip. There appears to be a way to set automatic reading at a configurable rate.

Probably first thing to try is figuring out if the library is using the fastest SPI speed the chip actually supports, and if not try increasing it?
 
For who? I can't speak for others, and I've not even looked in the library.

I have now, its 2K lines or more just to read an accelerometer, seems its a complex chip. There appears to be a way to set automatic reading at a configurable rate.

Probably first thing to try is figuring out if the library is using the fastest SPI speed the chip actually supports, and if not try increasing it?
Thank you Mark.

I will try looking through the library to see if I can figure that out. I'll post an update if I make progress.
 
Back
Top