Skip to content

Commit 6ac9c42

Browse files
Functional SPI example
SPI functionality has now been completed and works with the board. Made adjustments in Arduino Library and bus files.
1 parent 548a734 commit 6ac9c42

File tree

5 files changed

+51
-30
lines changed

5 files changed

+51
-30
lines changed

examples/Example4_SPIFunctionality/Example4_SPIFunctionality.ino

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "SparkFun_STHS34PF80_Arduino_Library.h"
2-
#include <Wire.h>
2+
#include <SPI.h>
3+
4+
SPIClass vspi(VSPI);
35

46
STHS34PF80_SPI mySensor;
57

@@ -8,34 +10,27 @@ int16_t presenceVal = 0;
810
int16_t motionVal = 0;
911

1012
// Set your chip select pin according to your setup.
11-
byte chipSelect = 12;
13+
uint8_t chipSelect = 12;
1214

1315
void setup()
1416
{
15-
SPI.begin();
17+
// SPI begin - initialize values with the pins according to your setup.
18+
// vspi.begin(CLOCK, MISO, MOSI, CS)
19+
vspi.begin(21, 27, 15, 12);
1620

1721
Serial.begin(115200);
1822

23+
// Configure the chip select pin
1924
pinMode(chipSelect, OUTPUT);
2025
digitalWrite(chipSelect, HIGH);
2126

2227
Serial.println("STHS34PF80 Example 4: SPI Functionality");
2328

24-
if( !mySensor.begin(chipSelect) ){
29+
if( !mySensor.begin(chipSelect, vspi) ){
2530
Serial.println("Did not begin.");
2631
while(1);
2732
}
2833

29-
30-
// Start communication with sensor
31-
if(mySensor.begin() == false)
32-
{
33-
Serial.println("Error"); // fix this print message
34-
while(1);
35-
}
36-
37-
mySensor.begin();
38-
3934
delay(1000);
4035
}
4136

src/SparkFun_STHS34PF80_Arduino_Library.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
bool STHS34PF80_I2C::begin(uint8_t devAddr)
44
{
55
bus.init(devAddr, Wire);
6-
sensor.read_reg = read;
7-
sensor.write_reg = write;
8-
sensor.mdelay = delayMS;
6+
sensor.read_reg = STHS34PF80_I2C::read;
7+
sensor.write_reg = STHS34PF80_I2C::write;
8+
sensor.mdelay = STHS34PF80_I2C::delayMS;
99
sensor.handle = &bus;
1010

1111
STHS34PF80::begin();
@@ -31,3 +31,36 @@ void STHS34PF80_I2C::delayMS(uint32_t millisec)
3131
{
3232
delay(millisec);
3333
}
34+
35+
bool STHS34PF80_SPI::begin(uint8_t chipSelect, SPIClass &spiPort)
36+
{
37+
SPISettings spiSettings = SPISettings(3000000, MSBFIRST, SPI_MODE3);
38+
bus.init(spiPort, spiSettings, chipSelect);
39+
sensor.read_reg = STHS34PF80_SPI::read;
40+
sensor.write_reg = STHS34PF80_SPI::write;
41+
sensor.mdelay = STHS34PF80_SPI::delayMS;
42+
sensor.handle = &bus;
43+
44+
STHS34PF80::begin();
45+
46+
return true; // returns error code here
47+
}
48+
49+
int32_t STHS34PF80_SPI::read(void* bus, uint8_t addr, uint8_t* data, uint16_t numData)
50+
{
51+
((SfeSPI*) bus)->readRegisterRegion(0, addr, data, numData);
52+
53+
return 0; // returns error code here
54+
}
55+
56+
int32_t STHS34PF80_SPI::write(void* bus, uint8_t addr, const uint8_t* data, uint16_t numData)
57+
{
58+
((SfeSPI*) bus)->writeRegisterRegion(0, addr, data, numData);
59+
60+
return 0; // returns error code here
61+
}
62+
63+
void STHS34PF80_SPI::delayMS(uint32_t millisec)
64+
{
65+
delay(millisec);
66+
}

src/SparkFun_STHS34PF80_Arduino_Library.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@ class STHS34PF80_I2C : public STHS34PF80
3838
class STHS34PF80_SPI : public STHS34PF80
3939
{
4040
public:
41-
bool begin();
41+
bool begin(uint8_t chipSelect, SPIClass &spiPort);
42+
static int32_t read(void *, uint8_t, uint8_t *, uint16_t);
43+
static int32_t write(void *, uint8_t, const uint8_t *, uint16_t);
44+
static void delayMS(uint32_t millisec);
4245

4346
private:
4447

45-
//Bus12C bus;
48+
SfeSPI bus;
4649

4750
};
4851

src/sfe_bus.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#include "sfe_bus.h"
22

3-
namespace sfe_STHS34PF80
4-
{
5-
63
/// @brief Constructor
74
/// @param addr
85
/// @param port
@@ -207,6 +204,4 @@ int SfeSPI::readRegisterRegion(uint8_t addr, uint8_t reg, uint8_t *data, uint16_
207204
_spiPort->endTransaction();
208205
return 0;
209206

210-
}
211-
212207
}

src/sfe_bus.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
#define SPI_READ 0x80
99

10-
namespace sfe_STHS34PF80 {
11-
1210
class SFE_BusI2C
1311
{
1412
public:
@@ -25,7 +23,7 @@ class SfeSPI //: public QwIDeviceBus
2523
{
2624
public:
2725

28-
SfeSPI(void);
26+
// SfeSPI(void);
2927

3028
bool init(uint8_t cs, bool bInit=false);
3129

@@ -45,7 +43,4 @@ class SfeSPI //: public QwIDeviceBus
4543
uint8_t _cs;
4644
};
4745

48-
};
49-
50-
5146
#endif

0 commit comments

Comments
 (0)