Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a440c1d

Browse files
committedNov 9, 2021
Updates library to use modern SPI practices when modifying SPI class settings
* clunky setting of SPI port speed, the entire lib might need a once over , but good enough for now.
1 parent 99f7af4 commit a440c1d

File tree

2 files changed

+14
-22
lines changed

2 files changed

+14
-22
lines changed
 

‎src/SparkFunLSM6DS3.cpp

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ LSM6DS3Core::LSM6DS3Core( uint8_t busType, uint8_t inputArg) : commInterface(I2C
6262
status_t LSM6DS3Core::beginCore(void)
6363
{
6464
status_t returnError = IMU_SUCCESS;
65+
uint32_t spiPortSpeed = 5000000;
6566

6667
switch (commInterface) {
6768

@@ -72,32 +73,20 @@ status_t LSM6DS3Core::beginCore(void)
7273
case SPI_MODE:
7374
// start the SPI library:
7475
SPI.begin();
75-
// Maximum SPI frequency is 10MHz, could divide by 2 here:
76-
SPI.setClockDivider(SPI_CLOCK_DIV4);
77-
// Data is read and written MSb first.
78-
#ifdef ESP32
79-
SPI.setBitOrder(SPI_MSBFIRST);
80-
#elif ESP8266
81-
SPI.setBitOrder(SPI_MSBFIRST);
82-
#else
83-
SPI.setBitOrder(MSBFIRST);
76+
77+
#ifdef __AVR__
78+
mySpiSettings = SPISettings(spiPortSpeed, MSB_FIRST, SPI_MODE1);
8479
#endif
85-
// Data is captured on rising edge of clock (CPHA = 0)
86-
// Base value of the clock is HIGH (CPOL = 1)
87-
88-
// MODE3 for 328p operation
89-
#ifdef __AVR__
90-
SPI.setDataMode(SPI_MODE3);
91-
#else
80+
#if defined(ESP32) || defined(ESP8266)
81+
mySpiSettings = SPISettings(spiPortSpeed, MSB_FIRST, SPI_MODE1);
9282
#endif
93-
94-
// MODE0 for Teensy 3.1 operation
9583
#ifdef __MK20DX256__
96-
SPI.setDataMode(SPI_MODE0);
97-
#else
84+
mySpiSettings = SPISettings(spiPortSpeed, MSB_FIRST, SPI_MODE0);
9885
#endif
99-
100-
// initalize the data ready and chip select pins:
86+
#ifdef ARDUINO_NANO33BLE
87+
mySpiSettings = SPISettings(spiPortSpeed, MSB_FIRST, SPI_MODE0);
88+
#endif
89+
10190
pinMode(chipSelectPin, OUTPUT);
10291
digitalWrite(chipSelectPin, HIGH);
10392
break;
@@ -304,6 +293,7 @@ status_t LSM6DS3Core::writeRegister(uint8_t offset, uint8_t dataToWrite) {
304293

305294
case SPI_MODE:
306295
// take the chip select low to select the device:
296+
SPI.beginTransaction(mySpiSettings);
307297
digitalWrite(chipSelectPin, LOW);
308298
// send the device the register you want to read:
309299
SPI.transfer(offset);

‎src/SparkFunLSM6DS3.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class LSM6DS3Core
8181

8282
//Change to base page
8383
status_t basePage( void );
84+
SPISettings mySpiSettings;
8485

8586
private:
8687

@@ -181,6 +182,7 @@ class LSM6DS3 : public LSM6DS3Core
181182

182183
float calcGyro( int16_t );
183184
float calcAccel( int16_t );
185+
184186

185187
private:
186188

0 commit comments

Comments
 (0)
Please sign in to comment.