Skip to content

Commit c1f514b

Browse files
committed
ESP_ERR_INVALID_STATE if not initialized
1 parent 1160b23 commit c1f514b

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

sdpsensor.cpp

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ uint8_t computeCRC(uint8_t* data)
5555
SDPSensor::SDPSensor(uint8_t i2c_addr, i2c_port_t i2c_port) {
5656
this->i2c_addr = i2c_addr;
5757
this->i2c_port = i2c_port;
58-
this->pressureScale = 0; // will be set in the init()
58+
this->pressureScale = 0; // will be read & set in the begin()
5959
this->maxSuccessiveFailsCount = 2;
6060
this->failsCount = 0;
61+
this->initialized = false;
6162
}
6263

6364

@@ -87,6 +88,13 @@ void SDPSensor::initI2C(int pinSDA, int pinSCL) {
8788
}
8889

8990

91+
void logInitFailed(esp_err_t err) {
92+
ESP_LOGE(TAG_SDPSENSOR,
93+
"Could not initialize and setup the SDP sensor: %s",
94+
esp_err_to_name(err));
95+
}
96+
97+
9098
esp_err_t SDPSensor::begin() {
9199
SDPSensor::reset(); // stop continuous mode
92100

@@ -102,9 +110,15 @@ esp_err_t SDPSensor::begin() {
102110
const TickType_t ticks_to_wait_long = pdMS_TO_TICKS(100);
103111
esp_err_t err;
104112
err = i2c_master_write_to_device(i2c_port, i2c_addr, cmd0, SDPSENSOR_I2C_CMD_LEN, ticks_to_wait_long);
105-
if (err != ESP_OK) return err;
113+
if (err != ESP_OK) {
114+
logInitFailed(err);
115+
return err;
116+
}
106117
err = i2c_master_write_to_device(i2c_port, i2c_addr, cmd1, SDPSENSOR_I2C_CMD_LEN, ticks_to_wait_long);
107-
if (err != ESP_OK) return err;
118+
if (err != ESP_OK) {
119+
logInitFailed(err);
120+
return err;
121+
}
108122

109123
/*
110124
Read product id and serial number.
@@ -113,7 +127,10 @@ esp_err_t SDPSensor::begin() {
113127
| Value | pid1 |CRC| pid2 |CRC| serial |
114128
*/
115129
err = i2c_master_read_from_device(i2c_port, i2c_addr, read_buffer, 18, ticks_to_wait_long);
116-
if (err != ESP_OK) return err;
130+
if (err != ESP_OK) {
131+
logInitFailed(err);
132+
return err;
133+
}
117134

118135
const uint32_t pid = (read_buffer[0] << 24) | (read_buffer[1] << 16)
119136
| (read_buffer[3] << 8) | (read_buffer[4] << 0);
@@ -157,25 +174,37 @@ esp_err_t SDPSensor::begin() {
157174
ESP_LOGI(TAG_SDPSENSOR, "Initialized SDP%d %dPa sensor (PID=0x%08X)", model_number, range_pa, pid);
158175

159176
err = i2c_master_write_to_device(i2c_port, i2c_addr, cmd_measure, SDPSENSOR_I2C_CMD_LEN, ticks_to_wait_long);
160-
if (err != ESP_OK) return err;
177+
if (err != ESP_OK) {
178+
logInitFailed(err);
179+
return err;
180+
}
161181

162182
vTaskDelay(pdMS_TO_TICKS(90)); // theoretically 45 ms
163183

164184
err = i2c_master_read_from_device(i2c_port, i2c_addr, read_buffer, 9, ticks_to_wait_long);
165-
if (err != ESP_OK) return err;
185+
if (err != ESP_OK) {
186+
logInitFailed(err);
187+
return err;
188+
}
166189

167190
this->pressureScale = ((int16_t) read_buffer[6]) << 8 | read_buffer[7];
168191

169192
ESP_LOGI(TAG_SDPSENSOR, "SDP%d pressure scale: %d", model_number, this->pressureScale);
170193

194+
this->initialized = true;
195+
171196
return err;
172197
}
173198

199+
174200
uint16_t SDPSensor::getPressureScale() {
175201
return pressureScale;
176202
}
177203

204+
178205
esp_err_t SDPSensor::startContinuous() {
206+
if (!initialized) return ESP_ERR_INVALID_STATE;
207+
179208
uint8_t cmd[SDPSENSOR_I2C_CMD_LEN] = { 0x36, 0x1E };
180209
const TickType_t ticks_to_wait_long = pdMS_TO_TICKS(100);
181210
esp_err_t err = i2c_master_write_to_device(i2c_port, i2c_addr, cmd,
@@ -187,8 +216,11 @@ esp_err_t SDPSensor::startContinuous() {
187216
return err;
188217
}
189218

219+
190220
esp_err_t SDPSensor::stopContinuous() {
191-
uint8_t cmd[SDPSENSOR_I2C_CMD_LEN] = { 0x3F, 0xF9 };
221+
if (!initialized) return ESP_ERR_INVALID_STATE;
222+
223+
uint8_t cmd[SDPSENSOR_I2C_CMD_LEN] = { 0x3F, 0xF9 };
192224
const TickType_t ticks_to_wait_long = pdMS_TO_TICKS(100);
193225
esp_err_t err = i2c_master_write_to_device(i2c_port, i2c_addr, cmd,
194226
SDPSENSOR_I2C_CMD_LEN, ticks_to_wait_long);
@@ -198,6 +230,7 @@ esp_err_t SDPSensor::stopContinuous() {
198230
return err;
199231
}
200232

233+
201234
esp_err_t SDPSensor::reset() {
202235
uint8_t cmd[1] = { 0x06 };
203236
const TickType_t ticks_to_wait_long = pdMS_TO_TICKS(100);
@@ -208,13 +241,16 @@ esp_err_t SDPSensor::reset() {
208241
return err;
209242
}
210243

244+
211245
void SDPSensor::watchdogSetParams(uint32_t maxSuccessiveFailsCount) {
212246
this->maxSuccessiveFailsCount = maxSuccessiveFailsCount;
213247
}
214248

215249

216250
void SDPSensor::watchdogCheck(esp_err_t status) {
217-
if (status == ESP_OK || status == ESP_ERR_INVALID_CRC) {
251+
if (!initialized) return;
252+
253+
if (status == ESP_OK || status == ESP_ERR_INVALID_CRC) {
218254
// invalid CRC does not mean that the sensor is not functioning
219255
failsCount = 0;
220256
} else {
@@ -239,7 +275,9 @@ void SDPSensor::watchdogCheck(esp_err_t status) {
239275

240276

241277
esp_err_t SDPSensor::readDiffPressure(int16_t *diffPressureRaw) {
242-
uint8_t data[3] = { 0 };
278+
if (!initialized) return ESP_ERR_INVALID_STATE;
279+
280+
uint8_t data[3] = { 0 };
243281
esp_err_t err = i2c_master_read_from_device(i2c_port, i2c_addr, data, 3,
244282
I2C_NO_TIMEOUT);
245283
if (err == ESP_OK) {
@@ -253,7 +291,9 @@ esp_err_t SDPSensor::readDiffPressure(int16_t *diffPressureRaw) {
253291

254292

255293
esp_err_t SDPSensor::readDiffPressureTemperature(int16_t *diffPressureRaw, float *temperature) {
256-
uint8_t data[6] = { 0 };
294+
if (!initialized) return ESP_ERR_INVALID_STATE;
295+
296+
uint8_t data[6] = { 0 };
257297

258298
/*
259299
Data Format:

sdpsensor.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class SDPSensor {
1818
uint16_t pressureScale; /* Diff pressure scale */
1919
uint32_t failsCount; /* [watchdog] SDP successive failed reads count */
2020
uint32_t maxSuccessiveFailsCount; /* [watchdog] SDP max successive failed reads before SW reset */
21+
bool initialized;
2122
public:
2223

2324
/**
@@ -120,10 +121,11 @@ class SDPSensor {
120121
*
121122
* @param diffPressureRaw - a pointer to save the result
122123
* @returns the error code (defined in esp_err.h):
123-
* ESP_OK - success
124-
* ESP_FAIL - failure
125-
* ESP_ERR_TIMEOUT - timed out
126-
* ESP_ERR_INVALID_CRC - CRC mismatch
124+
* ESP_OK - success
125+
* ESP_FAIL - failure
126+
* ESP_ERR_TIMEOUT - timed out
127+
* ESP_ERR_INVALID_CRC - CRC mismatch
128+
* ESP_ERR_INVALID_STATE - not initialized
127129
*/
128130
esp_err_t readDiffPressure(int16_t *diffPressureRaw);
129131

0 commit comments

Comments
 (0)