Skip to content

Commit a7469f0

Browse files
committed
Improve SPI communication using SPISettings
Using SPISettings together with begin/endTransaction allows using multiple systems communicating over the same SPI bus. Thus far, the initial SPI configuration was overridden by other systems that configured SPI. Additionally, due to compiler and linker optimizations, adjusting the SPI settings via BME280_SPI_CLOCK, a much smaller memory footprint is possible. Finally, allow modification of the SPI mode by defining BME280_SPI_MODE.
1 parent 8c89f95 commit a7469f0

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

src/SparkFunBME280.cpp

+5-26
Original file line numberDiff line numberDiff line change
@@ -86,24 +86,6 @@ uint8_t BME280::begin()
8686
case SPI_MODE:
8787
// start the SPI library:
8888
SPI.begin();
89-
#ifdef ARDUINO_ARCH_ESP32
90-
SPI.setFrequency(1000000);
91-
// Data is read and written MSb first.
92-
SPI.setBitOrder(SPI_MSBFIRST);
93-
// Like the standard arduino/teensy comment below, mode0 seems wrong according to standards
94-
// but conforms to the timing diagrams when used for the ESP32
95-
SPI.setDataMode(SPI_MODE0);
96-
#else
97-
// Maximum SPI frequency is 10MHz, could divide by 2 here:
98-
SPI.setClockDivider(SPI_CLOCK_DIV32);
99-
// Data is read and written MSb first.
100-
SPI.setBitOrder(MSBFIRST);
101-
// Data is captured on rising edge of clock (CPHA = 0)
102-
// Base value of the clock is HIGH (CPOL = 1)
103-
// This was SPI_MODE3 for RedBoard, but I had to change to
104-
// MODE0 for Teensy 3.1 operation
105-
SPI.setDataMode(SPI_MODE3);
106-
#endif
10789
// initialize the data ready and chip select pins:
10890
pinMode(settings.chipSelectPin, OUTPUT);
10991
digitalWrite(settings.chipSelectPin, HIGH);
@@ -580,6 +562,7 @@ void BME280::readRegisterRegion(uint8_t *outputPointer , uint8_t offset, uint8_t
580562
break;
581563

582564
case SPI_MODE:
565+
SPI.beginTransaction(settings.spiSettings);
583566
// take the chip select low to select the device:
584567
digitalWrite(settings.chipSelectPin, LOW);
585568
// send the device the register you want to read:
@@ -593,6 +576,7 @@ void BME280::readRegisterRegion(uint8_t *outputPointer , uint8_t offset, uint8_t
593576
}
594577
// take the chip select high to de-select:
595578
digitalWrite(settings.chipSelectPin, HIGH);
579+
SPI.endTransaction();
596580
break;
597581

598582
default:
@@ -641,14 +625,7 @@ uint8_t BME280::readRegister(uint8_t offset)
641625
break;
642626

643627
case SPI_MODE:
644-
// take the chip select low to select the device:
645-
digitalWrite(settings.chipSelectPin, LOW);
646-
// send the device the register you want to read:
647-
SPI.transfer(offset | 0x80); //Ored with "read request" bit
648-
// send a value of 0 to read the first byte returned:
649-
result = SPI.transfer(0x00);
650-
// take the chip select high to de-select:
651-
digitalWrite(settings.chipSelectPin, HIGH);
628+
readRegisterRegion(&result, offset, 1);
652629
break;
653630

654631
default:
@@ -693,6 +670,7 @@ void BME280::writeRegister(uint8_t offset, uint8_t dataToWrite)
693670
break;
694671

695672
case SPI_MODE:
673+
SPI.beginTransaction(settings.spiSettings);
696674
// take the chip select low to select the device:
697675
digitalWrite(settings.chipSelectPin, LOW);
698676
// send the device the register you want to read:
@@ -702,6 +680,7 @@ void BME280::writeRegister(uint8_t offset, uint8_t dataToWrite)
702680
// decrement the number of bytes left to read:
703681
// take the chip select high to de-select:
704682
digitalWrite(settings.chipSelectPin, HIGH);
683+
SPI.endTransaction();
705684
break;
706685

707686
default:

src/SparkFunBME280.h

+14-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ Distributed as-is; no warranty is given.
4646
#define I2C_MODE 0
4747
#define SPI_MODE 1
4848

49+
#ifndef BME280_SPI_CLOCK
50+
#ifdef ARDUINO_ARCH_ESP32
51+
#define BME280_SPI_CLOCK 1000000
52+
#else
53+
#define BME280_SPI_CLOCK 500000
54+
#endif
55+
#endif
56+
57+
#ifndef BME280_SPI_MODE
58+
#define BME280_SPI_MODE SPI_MODE0
59+
#endif
60+
4961
#define NO_WIRE 0
5062
#define HARD_WIRE 1
5163
#define SOFT_WIRE 2
@@ -120,7 +132,8 @@ struct BME280_SensorSettings
120132
uint8_t commInterface;
121133
uint8_t I2CAddress;
122134
uint8_t chipSelectPin;
123-
135+
SPISettings spiSettings{BME280_SPI_CLOCK, MSBFIRST, BME280_SPI_MODE};
136+
124137
//Deprecated settings
125138
uint8_t runMode;
126139
uint8_t tStandby;

0 commit comments

Comments
 (0)