Nano 33 BLE SPI Speed not changing

Hi all,

Pretty new to the arduino nano, even more new with respect to the SPI library and how it should function. It is my understanding that the first argument of the SPISettings() function should be the maximum data transfer rate, but for whatever reason when I change the value nothing seems to change on the scope.

My setup:

SPI 4-wire connected to a BMA456 accelerometer (datasheet: https://www.mouser.com/datasheet/2/783/BST-BMA456-DS000-1509567.pdf)

Here is my code:

#include <SPI.h>

// Define the digital pin for the SPI Interface.
const int slaveSelectPin1 = 10;

byte readByte = 0x80;
byte writeByte = 0x00;

byte chipAddress = 0x00;

void setup() {
  // put your setup code here, to run once:
  pinMode(LED_BUILTIN, OUTPUT);

// Initialize Serial Communication for debugging
  Serial.begin(9600);
  while (!Serial);
// Pause so I can open the serial monitor
  delay(1000);

// Cycle the slave select pin so the accelerometer switches to SPI mode
  pinMode(slaveSelectPin1, OUTPUT);
  digitalWrite(slaveSelectPin1, LOW);
  digitalWrite(slaveSelectPin1, HIGH);
//

}

void loop() {

  double pauseTime = 1000;

  SPI.begin();  // init SPI

  SPI.beginTransaction(SPISettings(1000000, LSBFIRST, SPI_MODE0)); //set parameters
  
  digitalWrite(slaveSelectPin1, LOW);
  delay(1000);

  // Send the Chip ID address as command
  Serial.print("Transmit: 0x");
  Serial.println(readByte | chipAddress, HEX);
  byte test = SPI.transfer(readByte | chipAddress); // Send command
  Serial.print("Received: 0x");
  Serial.println(test, HEX);
  
  delay(pauseTime);

  // Transmit one dummy command
  Serial.println("Dummy");
  test = SPI.transfer(0x00); // Wait one dummy
  Serial.println(test, HEX);
  delay(pauseTime);

  // Now receive the correct value
  Serial.println("Read Actual");
  test = SPI.transfer(0x00); // Read byte // Should be correct value
  Serial.println(test, HEX);
  delay(pauseTime);

  // Set the CSB pin to high
  digitalWrite(slaveSelectPin1, HIGH);

  // End transaction
  SPI.endTransaction();
  SPI.end();

  // Wait a bit before the next loop
  delay(10000);
  
}

The attached 1_MHz.jpg is the "screenshot" of my oscilloscope showing the output for this setup.

If I change the argument of the SPISettings() function to be 100,000 (i.e. 100 kHz) and re-upload, the resulting output does not seem to change. The 100kHz.jpg is the oscilloscope reading of that change.

I would really appreciate a second pair of eyes on this in the hopes that I'm just doing something really dumb.

In the mean time I will give it a try on an Uno (R3) to see if I get different clock speeds and report back.

Thanks in advance!

I see the same thing on my oscilloscope. I had a lock into the SPI.cpp and SPI.h files and it looks like they pass the settings to the mbedOS SPI. I could not find the source files for that only the spi_api.h. I suspect the library simply ignores the frequency settings. The API file even has a comment that says you might not get what you want.

/** Set the SPI baud rate
 *
 * Actual frequency may differ from the desired frequency due to available dividers and bus clock
 * Configures the SPI peripheral's baud rate
 * @param[in,out] obj The SPI object to configure
 * @param[in]     hz  The baud rate in Hz
 */
void spi_frequency(spi_t *obj, int hz);

Because SPI is synchronous with a separate clock signal and the frequency is set to 1MHz it probably works in most cases. So, the developer who ported this to the chip probably wanted to save some time and used a fixed frequency. :frowning:

Well, that certainly explains it! :roll_eyes:

I wonder if passing frequencies that are correct clock divisions (ie clock/32, etc) would yield different rates?

The main reason I was hoping to change the rate is that I am trying to communicate with this accelerometer (bma456) over a foot or so of wired connections. At this point, all of the lines from the nano (SCK,MOSI,CSB) are producing what I can tell to be the correct signals, but the MISO line from the accelerometer is behaving very strangely.

If I disconnect the arduino end of the MISO line, the accelerometer will generate the correct output byte. But, if the arduino is connected, the output of the accelerometer becomes only a single binary value (for example: 0x10, 0x04, 0x20). I suspect that it is the "high" frequency of the transmission rate that is affecting this, but I don't know enough about these things to be sure. Are there any other potential causes that people know of for things like this? I'd be happy to provide additional documentation/start a new thread if that is better.

Either way, thanks for the reply Klaus_K, I appreciate it.