Skip to content

Commit 2fcf226

Browse files
committed
increased I2C buffer to fix obstruction/reset issue
-causing an obstruction was causing the esp32 to reset. Upon further investigation, it seems that when you cause an obstruction or move your hand away quickly, there is a major dump of info on the I2C bus. Sometimes the payloads can get up to 400+ bytes. So this was overflowing and causing the issue. -removed the sfetoolkit stuff during troubleshooting, so may put that back in later.
1 parent d684e51 commit 2fcf226

File tree

5 files changed

+224
-159
lines changed

5 files changed

+224
-159
lines changed

examples/Example_01_BasicReadings/Example_01_BasicReadings.ino

Lines changed: 35 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -35,72 +35,59 @@ Bmv080 bmv080;
3535

3636
SET_LOOP_TASK_STACK_SIZE(60 * 1024); // 60KB
3737

38-
int hi2c;
38+
extern "C" bmv080_handle_t bmv080_handle = NULL;
3939

4040
/* A unique handle is used to address a BMV080 sensor unit */
41-
static bmv080_handle_t bmv080_handle = NULL;
41+
//static bmv080_handle_t bmv080_handle = NULL;
4242

4343
/* handle for print function to be used in interrupt service routine */
44-
static print_function_t print_handle = NULL;
44+
//static print_function_t print_handle = NULL;
4545

46-
volatile uint32_t data_ready_callback_count = 0;
46+
//volatile uint32_t data_ready_callback_count = 0;
4747

48-
void print_to_serial(const char *format, ...);
48+
//void print_to_serial(const char *format, ...);
4949

50-
/* Private variables ---------------------------------------------------------*/
51-
spi_device_t spi_device = {};
5250
i2c_device_t i2c_device = {};
5351

54-
bmv080_status_code_t bmv080_current_status = E_BMV080_OK;
55-
56-
volatile bmv080_output_t bmv080_output;
57-
5852
#define IRQ_Pin 14
5953

6054
bool int_flag = false;
6155

6256
void setup()
6357
{
64-
// Start serial
65-
Serial.begin(115200);
58+
// // Start serial
59+
// Serial.begin(115200);
6660

67-
while(!Serial) delay(10); // Wait for Serial to become available.
68-
// Necessary for boards with native USB (like the SAMD51 Thing+).
69-
// For a final version of a project that does not need serial debug (or a USB cable plugged in),
70-
// Comment out this while loop, or it will prevent the remaining code from running.
61+
// while(!Serial) delay(10); // Wait for Serial to become available.
62+
// // Necessary for boards with native USB (like the SAMD51 Thing+).
63+
// // For a final version of a project that does not need serial debug (or a USB cable plugged in),
64+
// // Comment out this while loop, or it will prevent the remaining code from running.
7165

72-
Serial.println();
73-
Serial.println("BMV080 Example 1 - Basic Readings");
66+
// Serial.println();
67+
// Serial.println("BMV080 Example 1 - Basic Readings");
7468

75-
Wire.begin();
69+
// Wire.begin();
7670

77-
if (bmv080.begin(BMV080_ADDR, Wire, BMV080_IRQ) == false) {
78-
Serial.println("BMV080 not detected at default I2C address. Check your jumpers and the hookup guide. Freezing...");
79-
while (1)
80-
;
81-
}
82-
Serial.println("BMV080 found!");
71+
// if (bmv080.begin(BMV080_ADDR, Wire, BMV080_IRQ) == false) {
72+
// Serial.println("BMV080 not detected at default I2C address. Check your jumpers and the hookup guide. Freezing...");
73+
// while (1)
74+
// ;
75+
// }
76+
// Serial.println("BMV080 found!");
8377

8478
// Wire.setClock(400000); //Increase I2C data rate to 400kHz
85-
86-
// TODO: Add other setup code if needed. Most setup should be done in begin()
87-
8879

8980
Serial.begin(115200);
9081
Serial.println("Starting BMV080 example...");
9182

92-
/* Communication interface initialization */
93-
spi_init(&spi_device);
83+
/* Communication interface initialization */
84+
9485
i2c_init(&i2c_device);
9586

9687
setup_sensor();
9788

98-
print_handle = (const print_function_t)print_to_serial;
99-
100-
enable_external_interrupt((bool)true); // Use of hardware interrupt of the BMV080 sensor unit can be used as trigger.
101-
10289
/* Start particle measurement in continuous mode */
103-
bmv080_current_status = bmv080_start_continuous_measurement(bmv080_handle);
90+
bmv080_status_code_t bmv080_current_status = bmv080_start_continuous_measurement(bmv080_handle);
10491

10592
if (bmv080_current_status != E_BMV080_OK)
10693
{
@@ -110,88 +97,25 @@ void setup()
11097
{
11198
printf("BMV080 continuous measurement started successfully\n");
11299
}
113-
114-
uint32_t sensor_measurement_duration_seconds = 60;
115-
data_ready_callback_count = 0;
116-
117-
printf("Particle measurement started in continuous mode for %d seconds \r\n", sensor_measurement_duration_seconds);
118100
}
119101

120102
void loop()
121103
{
122-
delay(100);
123-
if(int_flag == true)
104+
if(bmv080.dataAvailable())
124105
{
125-
int_flag = false;
126-
do{
127-
bmv080_service_routine();
128-
} while (digitalRead(IRQ_Pin) == 0);
129-
}
130-
}
131-
132-
/* Private functions ---------------------------------------------------------*/
133-
static void enable_external_interrupt(bool enable)
134-
{
135-
int checkPin = digitalPinToInterrupt(IRQ_Pin);
136-
137-
if (checkPin == -1) {
138-
Serial.println("Not a valid interrupt pin!");
139-
} else {
140-
Serial.println("Valid interrupt pin.");
141-
}
142-
143-
if(enable)
144-
{ /* Enabel external interrupt */
145-
attachInterrupt(digitalPinToInterrupt(IRQ_Pin), gpio_isr_handler, FALLING );
146-
}else
147-
{ /* Disable external interrupt */
148-
detachInterrupt(digitalPinToInterrupt(IRQ_Pin) );
149-
}
150-
}
106+
float pm25 = bmv080.getPM25();
151107

152-
void gpio_isr_handler(void)
153-
{
154-
int_flag = true;
155-
}
156-
157-
158-
/* Custom function for consuming sensor readings */
159-
void use_sensor_output(bmv080_output_t bmv080_output, void* callback_parameters)
160-
{
161-
data_ready_callback_count += 1;
162-
print_function_t print = (print_function_t)callback_parameters;
163-
164-
print("Runtime: %.2f s, PM2.5: %.0f ug/m^3, obstructed: %s, outside detection limits: %s\r\n",
165-
bmv080_output.runtime_in_sec, bmv080_output.pm2_5, (bmv080_output.is_obstructed ? "yes" : "no"), (bmv080_output.is_outside_detection_limits ? "yes" : "no"));
166-
}
167-
168-
void print_to_serial(const char *format, ...)
169-
{
170-
char print_buffer[1024];
171-
va_list args;
172-
va_start(args, format);
173-
vsnprintf(print_buffer, sizeof(print_buffer), format, args);
174-
va_end(args);
175-
Serial.print(print_buffer);
176-
}
108+
Serial.print(pm25);
177109

178-
void bmv080_service_routine(void)
179-
{
180-
if ( (bmv080_handle != NULL) && (print_handle != NULL))
110+
if(bmv080.getIsObstructed() == true)
181111
{
182-
//Serial.println("s");
183-
/* The interrupt is served by the BMV080 sensor driver */
184-
bmv080_status_code_t bmv080_current_status = bmv080_serve_interrupt(bmv080_handle, (bmv080_callback_data_ready_t)use_sensor_output, (void*)print_handle);
185-
if (bmv080_current_status != E_BMV080_OK)
186-
{
187-
printf("Fetching measurement data failed with BMV080 status %d\r\n", (int32_t)bmv080_current_status);
188-
}
112+
Serial.print("\tObstructed");
189113
}
190-
}
191-
192-
193-
194114

115+
Serial.println();
116+
}
117+
delay(100);
118+
}
195119

196120
void setup_sensor(void)
197121
{
@@ -202,7 +126,7 @@ void setup_sensor(void)
202126
char git_hash[12];
203127
int32_t commits_ahead = 0;
204128

205-
bmv080_current_status = bmv080_get_driver_version(&major, &minor, &patch, git_hash, &commits_ahead);
129+
bmv080_status_code_t bmv080_current_status = bmv080_get_driver_version(&major, &minor, &patch, git_hash, &commits_ahead);
206130

207131
if (bmv080_current_status != E_BMV080_OK)
208132
{
@@ -228,6 +152,8 @@ void setup_sensor(void)
228152
printf("BMV080 handle opened successfully\n");
229153
}
230154

155+
bmv080.setHandle(bmv080_handle);
156+
231157
/* Reset the BMV080 sensor unit */
232158
bmv080_current_status = bmv080_reset(bmv080_handle);
233159

src/SparkFun_BMV080_Arduino_Library.h

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,36 @@
2727

2828
// TODO: Add includes as needed (e.g. #include <Wire.h>, #include <SPI.h>)
2929
#include "sfeBmv080.h"
30-
#include <SparkFun_Toolkit.h>
30+
//#include <SparkFun_Toolkit.h>
3131

32-
#include "Arduino.h"
32+
// #include "Arduino.h"
3333
#include <bmv080.h>
3434
#include <bmv080_defs.h>
3535
#include "combridge.h"
3636

3737
class Bmv080 : public sfeBmv080
3838
{
3939
public:
40-
/// @brief Begins the Device
41-
/// @param address I2C device address to use for the sensor
42-
/// @param wirePort Wire port to use for I2C communication
43-
/// @return True if successful, false otherwise
44-
bool begin(const uint8_t address = SFE_BMV080_DEFAULT_ADDRESS, TwoWire &wirePort = Wire, const uint8_t irqPin = SFE_BMV080_DEFAULT_IRQ_PIN)
45-
{
46-
// Setup Arudino I2C bus
47-
_theI2CBus.init(wirePort, address);
48-
49-
// Begin the sensor
50-
return sfeBmv080::begin(&_theI2CBus) == kSTkErrOk;
51-
}
52-
53-
/// @brief Checks if the Device is connected
54-
/// @return True if the sensor is connected, false otherwise
55-
bool isConnected()
56-
{
57-
return sfeBmv080::isConnected() == kSTkErrOk;
58-
}
59-
60-
private:
61-
sfeTkArdI2C _theI2CBus;
40+
// /// @brief Begins the Device
41+
// /// @param address I2C device address to use for the sensor
42+
// /// @param wirePort Wire port to use for I2C communication
43+
// /// @return True if successful, false otherwise
44+
// bool begin(const uint8_t address = SFE_BMV080_DEFAULT_ADDRESS, TwoWire &wirePort = Wire, const uint8_t irqPin = SFE_BMV080_DEFAULT_IRQ_PIN)
45+
// {
46+
// // Setup Arudino I2C bus
47+
// _theI2CBus.init(wirePort, address);
48+
49+
// // Begin the sensor
50+
// return sfeBmv080::begin(&_theI2CBus) == kSTkErrOk;
51+
// }
52+
53+
// /// @brief Checks if the Device is connected
54+
// /// @return True if the sensor is connected, false otherwise
55+
// bool isConnected()
56+
// {
57+
// return sfeBmv080::isConnected() == kSTkErrOk;
58+
// }
59+
60+
// private:
61+
// sfeTkArdI2C _theI2CBus;
6262
};

src/combridge.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ void i2c_init(i2c_device_t *i2c_device)
9999
i2c_device->instance = &Wire;
100100
i2c_device->instance->begin();
101101
i2c_device->instance->setClock(I2C_CLK_FREQ);
102+
i2c_device->instance->setBufferSize(512);
102103
}
103104

104105

@@ -169,7 +170,10 @@ int8_t combridge_i2c_read_16bit(bmv080_sercom_handle_t handle, uint16_t header,
169170
while(i2c_device->instance->available() && (payload_index < (payload_length * 2) ))
170171
{
171172
payload_byte[payload_index++] = i2c_device->instance->read();
173+
//Serial.print(".");
172174
}
175+
//Serial.print("payload_length");
176+
//Serial.println(payload_length);
173177

174178
if(payload_index != (payload_length * 2))
175179
{

0 commit comments

Comments
 (0)