Skip to content
This repository has been archived by the owner on Jan 16, 2020. It is now read-only.

Commit

Permalink
ms5611: fix sleep in work process will block WorkQueue schedule. (#116)
Browse files Browse the repository at this point in the history
It will seriously reduce other sensors's collect rate.
  • Loading branch information
iZhangHui authored and LorenzMeier committed Jul 4, 2016
1 parent d1ab158 commit 29b18fb
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 39 deletions.
90 changes: 54 additions & 36 deletions drivers/ms5611/MS5611.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ int MS5611::ms5611_init()
return -EIO;
}

// Request to convert the temperature
if (_request(ADDR_CMD_CONVERT_D2) < 0) {
DF_LOG_ERR("error: temp measure failed");
return -EIO;
}

return 0;
}

Expand Down Expand Up @@ -313,7 +319,7 @@ int MS5611::_request(uint8_t cmd)
return ret;
}

int MS5611::_collect(uint32_t *raw)
int MS5611::_collect(uint32_t &raw)
{
int ret;

Expand All @@ -329,66 +335,78 @@ int MS5611::_collect(uint32_t *raw)
ret = _readReg(cmd, &buf[0], 3);

if (ret < 0) {
*raw = 0;
raw = 0;
return -1;
}

cvt.b[0] = buf[2];
cvt.b[1] = buf[1];
cvt.b[2] = buf[0];
cvt.b[3] = 0;
*raw = cvt.w;
raw = cvt.w;

return 0;
}

void MS5611::_measure(void)
{
if (_measure_phase == 0) {
if (_collect(temperature_from_sensor) < 0) {
DF_LOG_ERR("error: temp collect failed");
reset();

/* collect fails, re-initiate a temperature read command
* or we are stuck.
*/
if (_request(ADDR_CMD_CONVERT_D2) < 0) {
DF_LOG_ERR("error: temp measure failed");
}

// Request to convert the temperature
if (_request(ADDR_CMD_CONVERT_D2) < 0) {
DF_LOG_ERR("error: temp measure failed");
return;
}
return;
}

usleep(MS5611_CONVERSION_INTERVAL_US);
// Request to convert the pressure
if (_request(ADDR_CMD_CONVERT_D1) < 0) {
DF_LOG_ERR("error: pressure measure failed");
}

// read the result
uint32_t temperature_from_sensor;
_measure_phase++;

if (_collect(&temperature_from_sensor) < 0) {
DF_LOG_ERR("error: temp collect failed");
reset();
return;
}
} else {
if (_collect(pressure_from_sensor) < 0) {
DF_LOG_ERR("error: pressure collect failed");
reset();

// Request to convert the pressure
if (_request(ADDR_CMD_CONVERT_D1) < 0) {
DF_LOG_ERR("error: pressure measure failed");
return;
}
/* collect fails, re-initiate a pressure read command
* or we are stuck.
*/
if (_request(ADDR_CMD_CONVERT_D1) < 0) {
DF_LOG_ERR("error: pressure measure failed");
return;
}
}

usleep(MS5611_CONVERSION_INTERVAL_US);
// Request to convert the temperature
if (_request(ADDR_CMD_CONVERT_D2) < 0) {
DF_LOG_ERR("error: temp measure failed");
}

// read the result
uint32_t pressure_from_sensor;
_measure_phase = 0;

if (_collect(&pressure_from_sensor) < 0) {
DF_LOG_ERR("error: pressure collect failed");
return;
}

m_synchronize.lock();
m_synchronize.lock();

m_sensor_data.temperature_c = convertTemperature(temperature_from_sensor) / 100.0;
m_sensor_data.pressure_pa = convertPressure(pressure_from_sensor);
m_sensor_data.last_read_time_usec = DriverFramework::offsetTime();
m_sensor_data.read_counter++;
m_sensor_data.temperature_c = convertTemperature(temperature_from_sensor) / 100.0;
m_sensor_data.pressure_pa = convertPressure(pressure_from_sensor);
m_sensor_data.last_read_time_usec = DriverFramework::offsetTime();
m_sensor_data.read_counter++;
_publish(m_sensor_data);

_publish(m_sensor_data);
m_synchronize.signal();
m_synchronize.unlock();

}

m_synchronize.signal();
m_synchronize.unlock();
}

int MS5611::_publish(struct baro_sensor_data &data)
Expand Down
15 changes: 12 additions & 3 deletions drivers/ms5611/MS5611.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,15 @@ struct ms5611_sensor_measurement {

#define MS5611_SLAVE_ADDRESS 0x77 /* 7-bit slave address */

#if MS5611_MEASURE_INTERVAL_US < (MS5611_CONVERSION_INTERVAL_US * 2)
#error "MS5611_MEASURE_INTERVAL_US Must >= MS5611_CONVERSION_INTERVAL_US * 2"
#endif

class MS5611 : public BaroSensor
{
public:
MS5611(const char *device_path) :
BaroSensor(device_path, MS5611_MEASURE_INTERVAL_US)
BaroSensor(device_path, MS5611_MEASURE_INTERVAL_US / 2)
{
m_id.dev_id_s.devtype = DRV_DF_DEVTYPE_MS5611;
m_id.dev_id_s.address = MS5611_SLAVE_ADDRESS;
Expand Down Expand Up @@ -108,7 +112,7 @@ class MS5611 : public BaroSensor
// Request to convert pressure or temperature data
int _request(uint8_t phase);
// Read out the requested sensor data
int _collect(uint32_t *raw);
int _collect(uint32_t &raw);

bool crc4(uint16_t *n_prom);

Expand All @@ -119,7 +123,12 @@ class MS5611 : public BaroSensor
int reset();

struct ms5611_sensor_calibration m_sensor_calibration;
struct ms5611_sensor_measurement m_raw_sensor_convertion;;
struct ms5611_sensor_measurement m_raw_sensor_convertion;

uint32_t temperature_from_sensor;
uint32_t pressure_from_sensor;

int _measure_phase;
};

}; // namespace DriverFramework

0 comments on commit 29b18fb

Please sign in to comment.