Skip to content

Commit 6ac7e98

Browse files
committed
ex4 SPI
1 parent e3f12e7 commit 6ac7e98

File tree

3 files changed

+149
-4
lines changed

3 files changed

+149
-4
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Using the BMV080 Particulate Matter PM2.5 Sensor with SPI
3+
4+
This example shows how to use the sensor in "continuous mode" to get
5+
particulate matter readings once every second.
6+
7+
It uses polling of the device to check if new data is available.
8+
9+
By: Pete Lewis
10+
SparkFun Electronics
11+
Date: September, 2024
12+
SparkFun code, firmware, and software is released under the MIT License.
13+
Please see LICENSE.md for further details.
14+
15+
Hardware Connections:
16+
IoT RedBoard --> BMV080
17+
------------------------------
18+
GND --> GND
19+
3V3 --> 3V3
20+
PICO --> PICO (SDA)
21+
POCI --> POCI (AB0)
22+
SCK --> SCK (SCL)
23+
CS --> CS (AB1)
24+
25+
BMV080 "mode" jumper set to SPI (default)
26+
27+
Serial.print it out at 115200 baud to serial monitor.
28+
29+
Feel like supporting our work? Buy a board from SparkFun!
30+
https://www.sparkfun.com/products/?????
31+
*/
32+
33+
#include <Wire.h>
34+
#include "SparkFun_BMV080_Arduino_Library.h" // CTRL+Click here to get the library: http://librarymanager/All#SparkFun_BMV080
35+
36+
Bmv080 bmv080; // Create an instance of the BMV080 class
37+
38+
spi_device_t spi_device = {}; // SPI device struct instance for Bosch API
39+
40+
void setup()
41+
{
42+
Serial.begin(115200);
43+
44+
while(!Serial) delay(10); // Wait for Serial to become available.
45+
// Necessary for boards with native USB (like the SAMD51 Thing+).
46+
// For a final version of a project that does not need serial debug (or a USB cable plugged in),
47+
// Comment out this while loop, or it will prevent the remaining code from running.
48+
49+
Serial.println();
50+
Serial.println("BMV080 Example 4 - SPI");
51+
52+
/* Communication interface initialization */
53+
spi_init(&spi_device);
54+
55+
if (bmv080.initSPI(&spi_device) == false) {
56+
Serial.println("SPI init failure. Check your jumpers and the hookup guide. Freezing...");
57+
while (1)
58+
;
59+
}
60+
Serial.println("BMV080 SPI init successful");
61+
62+
/* Set the sensor mode to continuous mode */
63+
if(bmv080.setMode(SFE_BMV080_MODE_CONTINUOUS) == true)
64+
{
65+
Serial.println("BMV080 set to continuous mode");
66+
}
67+
else
68+
{
69+
Serial.println("Error setting BMV080 mode");
70+
}
71+
}
72+
73+
void loop()
74+
{
75+
if(bmv080.dataAvailable())
76+
{
77+
float pm25 = bmv080.getPM25();
78+
79+
Serial.print(pm25);
80+
81+
if(bmv080.getIsObstructed() == true)
82+
{
83+
Serial.print("\tObstructed");
84+
}
85+
86+
Serial.println();
87+
}
88+
delay(100);
89+
}

src/sfeBmv080.cpp

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,6 @@ bool sfeBmv080::init(i2c_device_t *i2c_device)
178178

179179
bool sfeBmv080::open(i2c_device_t *i2c_device)
180180
{
181-
//bmv080_handle_t bmv080_handle_temp = NULL;
182-
//setHandle(bmv080_handle_temp);
183181
bmv080_sercom_handle_t sercom_handle = (bmv080_sercom_handle_t)i2c_device;
184182
bmv080_callback_read_t read = (const bmv080_callback_read_t)combridge_i2c_read_16bit;
185183
bmv080_callback_write_t write = (const bmv080_callback_write_t)combridge_i2c_write_16bit;
@@ -199,6 +197,52 @@ bool sfeBmv080::open(i2c_device_t *i2c_device)
199197
}
200198
}
201199

200+
bool sfeBmv080::initSPI(spi_device_t *spi_device)
201+
{
202+
if(getDriverVersion() == false)
203+
{
204+
return false;
205+
}
206+
207+
if(openSPI(spi_device) == false)
208+
{
209+
return false;
210+
}
211+
212+
if(reset() == false)
213+
{
214+
return false;
215+
}
216+
217+
if(getID() == false)
218+
{
219+
return false;
220+
}
221+
222+
return true;
223+
}
224+
225+
bool sfeBmv080::openSPI(spi_device_t *spi_device)
226+
{
227+
bmv080_sercom_handle_t sercom_handle = (bmv080_sercom_handle_t)spi_device;
228+
bmv080_callback_read_t read = (const bmv080_callback_read_t)combridge_spi_read_16bit;
229+
bmv080_callback_write_t write = (const bmv080_callback_write_t)combridge_spi_write_16bit;
230+
bmv080_callback_delay_t delay_ms = (const bmv080_callback_delay_t)combridge_delay;
231+
232+
bmv080_status_code_t bmv080_current_status = bmv080_open(&bmv080_handle_class, sercom_handle, read, write, delay_ms);
233+
234+
if (bmv080_current_status != E_BMV080_OK)
235+
{
236+
Serial.println("BMV080 open failed");
237+
return false;
238+
}
239+
else
240+
{
241+
Serial.println("BMV080 open successfully");
242+
return true;
243+
}
244+
}
245+
202246
bool sfeBmv080::reset()
203247
{
204248
bmv080_status_code_t bmv080_current_status = bmv080_reset(bmv080_handle_class);

src/sfeBmv080.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,20 @@ class sfeBmv080
5353
/// @return 0 for succuss, negative for errors, positive for warnings
5454
sfeTkError_t isConnected();
5555

56-
/// @brief Initialize the sensor
56+
/// @brief Initialize the sensor i2c
5757
/// @details This function initializes the sensor and should be called
58-
/// before any other functions. It calls Open, Reset, getDriverVersion, and getID.
58+
/// @details before any other functions. It calls Open, Reset, getDriverVersion, and getID.
5959
/// @param i2c_device The I2C device to use
6060
/// @return True if successful, false otherwise
6161
bool init(i2c_device_t *i2c_device);
6262

63+
/// @brief Initialize the sensor SPI
64+
/// @details This function initializes the sensor and should be called
65+
/// @details before any other functions. It calls Open, Reset, getDriverVersion, and getID.
66+
/// @param spi_device The SPI device to use
67+
/// @return True if successful, false otherwise
68+
bool initSPI(spi_device_t *spi_device);
69+
6370
/// @brief Get the version information of this sensor driver.
6471
/// @return True if successful, false otherwise
6572
bool getDriverVersion();
@@ -69,6 +76,11 @@ class sfeBmv080
6976
/// @return True if successful, false otherwise
7077
bool open(i2c_device_t *i2c_device);
7178

79+
/// @brief Open a sensor unit by initializing a new handle.
80+
/// @param spi_device The SPI device to use
81+
/// @return True if successful, false otherwise
82+
bool openSPI(spi_device_t *spi_device);
83+
7284
/// @brief Reset the sensor
7385
/// @return True if successful, false otherwise
7486
bool reset();

0 commit comments

Comments
 (0)