Skip to content

Commit

Permalink
Merge pull request #2 from adafruit/LPS22
Browse files Browse the repository at this point in the history
LPS22 support (and some LPS25 changes)
  • Loading branch information
ladyada authored May 25, 2020
2 parents 292931e + ed5ef95 commit 6d21da7
Show file tree
Hide file tree
Showing 8 changed files with 345 additions and 127 deletions.
90 changes: 90 additions & 0 deletions Adafruit_LPS22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include <Adafruit_LPS2X.h>

/*! @brief Initializer for post i2c/spi init
* @param sensor_id Optional unique ID for the sensor set
* @returns True if chip identified and initialized
*/
bool Adafruit_LPS22::_init(int32_t sensor_id) {

Adafruit_BusIO_Register chip_id = Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LPS2X_WHOAMI, 1);

// make sure we're talking to the right chip
uint8_t id = chip_id.read();

if (id != LPS22HB_CHIP_ID) {
return false;
}
_sensorid_pressure = sensor_id;
_sensorid_temp = sensor_id + 1;

temp_scaling = 100;

ctrl1_reg = new Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LPS22_CTRL_REG1, 1);
ctrl2_reg = new Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LPS22_CTRL_REG2, 1);
ctrl3_reg = new Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LPS22_CTRL_REG3, 1);
threshp_reg = new Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LPS22_THS_P_L_REG, 1);

reset();
// do any software reset or other initial setup
setDataRate(LPS22_RATE_25_HZ);
// interrupt on data ready
configureInterrupt(true, false, true);

pressure_sensor = new Adafruit_LPS2X_Pressure(this);
temp_sensor = new Adafruit_LPS2X_Temp(this);

delay(10); // delay for first reading
return true;
}

/**
* @brief Sets the rate at which pressure and temperature measurements
*
* @param new_data_rate The data rate to set. Must be a `lps22_rate_t`
*/
void Adafruit_LPS22::setDataRate(lps22_rate_t new_data_rate) {
Adafruit_BusIO_RegisterBits data_rate =
Adafruit_BusIO_RegisterBits(ctrl1_reg, 3, 4);

data_rate.write((uint8_t)new_data_rate);
}

/**
* @brief Gets the current rate at which pressure and temperature measurements
* are taken
*
* @return lps22_rate_t The current data rate
*/
lps22_rate_t Adafruit_LPS22::getDataRate(void) {
Adafruit_BusIO_RegisterBits data_rate =
Adafruit_BusIO_RegisterBits(ctrl1_reg, 3, 4);

return (lps22_rate_t)data_rate.read();
}

/**
* @brief Configures the INT pin
* @param activelow Pass true to make the INT pin drop low on interrupt
* @param opendrain Pass true to make the INT pin an open drain output
* @param data_ready If true, interrupt fires on new data ready
* @param pres_high If true, interrupt fires on high threshold pass
* @param pres_low If true, interrupt fires on low threshold pass
* @param fifo_full If true, interrupt fires on fifo full
* @param fifo_watermark If true, interrupt fires on fifo watermark pass
* @param fifo_overflow If true, interrupt fires on fifo overflow
*/
void Adafruit_LPS22::configureInterrupt(bool activelow, bool opendrain,
bool data_ready, bool pres_high,
bool pres_low, bool fifo_full,
bool fifo_watermark,
bool fifo_overflow) {
uint8_t reg = (activelow << 7) | (opendrain << 6) | (fifo_full << 5) |
(fifo_watermark << 4) | (fifo_overflow << 3) |
(data_ready << 2);
ctrl3_reg->write(reg);
}
95 changes: 95 additions & 0 deletions Adafruit_LPS25.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <Adafruit_LPS2X.h>

/*! @brief Initializer for post i2c/spi init
* @param sensor_id Optional unique ID for the sensor set
* @returns True if chip identified and initialized
*/
bool Adafruit_LPS25::_init(int32_t sensor_id) {

Adafruit_BusIO_Register chip_id = Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LPS2X_WHOAMI, 1);

// make sure we're talking to the right chip
uint8_t id = chip_id.read();

if (id != LPS25HB_CHIP_ID) {
return false;
}
_sensorid_pressure = sensor_id;
_sensorid_temp = sensor_id + 1;

temp_scaling = 480;
inc_spi_flag = 0x40;

ctrl1_reg = new Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LPS25_CTRL_REG1, 1);
ctrl2_reg = new Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LPS25_CTRL_REG2, 1);
ctrl3_reg = new Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LPS25_CTRL_REG3, 1);
threshp_reg = new Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LPS25_THS_P_L_REG, 1);

reset();
// do any software reset or other initial setup
powerDown(false);
setDataRate(LPS25_RATE_25_HZ);

pressure_sensor = new Adafruit_LPS2X_Pressure(this);
temp_sensor = new Adafruit_LPS2X_Temp(this);
delay(10); // delay for first reading
return true;
}

/**
* @brief Sets the rate at which pressure and temperature measurements
*
* @param new_data_rate The data rate to set. Must be a `lps25_rate_t`
*/
void Adafruit_LPS25::setDataRate(lps25_rate_t new_data_rate) {
Adafruit_BusIO_Register ctrl1 = Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LPS25_CTRL_REG1, 1);
Adafruit_BusIO_RegisterBits data_rate =
Adafruit_BusIO_RegisterBits(&ctrl1, 3, 4);

data_rate.write((uint8_t)new_data_rate);
}

/**
* @brief Gets the current rate at which pressure and temperature measurements
* are taken
*
* @return lps25_rate_t The current data rate
*/
lps25_rate_t Adafruit_LPS25::getDataRate(void) {
Adafruit_BusIO_Register ctrl1 = Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LPS25_CTRL_REG1, 1);
Adafruit_BusIO_RegisterBits data_rate =
Adafruit_BusIO_RegisterBits(&ctrl1, 3, 4);

return (lps25_rate_t)data_rate.read();
}

/**
* @brief Puts the sensor into power down mode, shutting the sensor down
*
* @param power_down
*/
void Adafruit_LPS25::powerDown(bool power_down) {
Adafruit_BusIO_RegisterBits pd = Adafruit_BusIO_RegisterBits(ctrl1_reg, 1, 7);
pd.write(!power_down); // pd bit->0 == power down
}

/**
* @brief Configures the INT pin, by default it will output DRDY signal
* @param activelow Pass true to make the INT pin drop low on interrupt
* @param opendrain Pass true to make the INT pin an open drain output
* @param pres_high If true, interrupt fires on high threshold pass
* @param pres_low If true, interrupt fires on low threshold pass
*/
void Adafruit_LPS25::configureInterrupt(bool activelow, bool opendrain,
bool pres_high, bool pres_low) {
uint8_t reg =
(activelow << 7) | (opendrain << 6) | (pres_low << 1) | pres_high;
ctrl3_reg->write(reg);
}
105 changes: 15 additions & 90 deletions Adafruit_LPS2X.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,45 +135,13 @@ bool Adafruit_LPS2X::begin_SPI(int8_t cs_pin, int8_t sck_pin, int8_t miso_pin,
return _init(sensor_id);
}

/*! @brief Initializer for post i2c/spi init
* @param sensor_id Optional unique ID for the sensor set
* @returns True if chip identified and initialized
*/
bool Adafruit_LPS2X::_init(int32_t sensor_id) {

Adafruit_BusIO_Register chip_id = Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LPS2X_WHOAMI, 1);

// make sure we're talking to the right chip
uint8_t id = chip_id.read();

if ((id != LPS2X_CHIP_ID) && (id != LPS25HB_CHIP_ID)) {
return false;
}
_sensorid_pressure = sensor_id;
_sensorid_temp = sensor_id + 1;

reset();
// do any software reset or other initial setup
powerDown(false);
setDataRate(LPS2X_RATE_25_HZ);

pressure_sensor = new Adafruit_LPS2X_Pressure(this);
temp_sensor = new Adafruit_LPS2X_Temp(this);
delay(10); // delay for first reading
return true;
}

/**
* @brief Performs a software reset initializing registers to their power on
* state
*
*/
void Adafruit_LPS2X::reset(void) {
Adafruit_BusIO_Register ctrl_2 = Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LPS2X_CTRL_REG2, 1);
Adafruit_BusIO_RegisterBits sw_reset =
Adafruit_BusIO_RegisterBits(&ctrl_2, 1, 2);
Adafruit_BusIO_RegisterBits(ctrl2_reg, 1, 2);

sw_reset.write(1);
while (sw_reset.read()) {
Expand All @@ -182,43 +150,12 @@ void Adafruit_LPS2X::reset(void) {
}

/**
* @brief Puts the sensor into power down mode, shutting the sensor down
*
* @param power_down
* @brief Set the pressure threshold register for interrupt levels
* @param hPa_delta The u16 that will be written to the register, check the
* datasheet for more info on the format of this value!
*/
void Adafruit_LPS2X::powerDown(bool power_down) {
Adafruit_BusIO_Register ctrl_1 = Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LPS2X_CTRL_REG1, 1);
Adafruit_BusIO_RegisterBits pd = Adafruit_BusIO_RegisterBits(&ctrl_1, 1, 7);
pd.write(!power_down); // pd bit->0 == power down
}

/**
* @brief Gets the current rate at which pressure and temperature measurements
* are taken
*
* @return lps2x_rate_t The current data rate
*/
lps2x_rate_t Adafruit_LPS2X::getDataRate(void) {
Adafruit_BusIO_Register ctrl1 = Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LPS2X_CTRL_REG1, 1);
Adafruit_BusIO_RegisterBits data_rate =
Adafruit_BusIO_RegisterBits(&ctrl1, 3, 4);

return (lps2x_rate_t)data_rate.read();
}
/**
* @brief Sets the rate at which pressure and temperature measurements
*
* @param new_data_rate The data rate to set. Must be a `lps2x_rate_t`
*/
void Adafruit_LPS2X::setDataRate(lps2x_rate_t new_data_rate) {
Adafruit_BusIO_Register ctrl1 = Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LPS2X_CTRL_REG1, 1);
Adafruit_BusIO_RegisterBits data_rate =
Adafruit_BusIO_RegisterBits(&ctrl1, 3, 4);

data_rate.write((uint8_t)new_data_rate);
void Adafruit_LPS2X::setPresThreshold(uint16_t hPa_delta) {
threshp_reg->write(hPa_delta);
}

/******************* Adafruit_Sensor functions *****************/
Expand All @@ -231,9 +168,9 @@ void Adafruit_LPS2X::_read(void) {
uint8_t pressure_addr = LPS2X_PRESS_OUT_XL;
uint8_t temp_addr = LPS2X_TEMP_OUT_L;
if (spi_dev) {
// for LPS2X SPI, addr[7] is r/w, addr[6] is auto increment
pressure_addr |= 0x40;
temp_addr |= 0x40;
// for LPS25 SPI, addr[7] is r/w, addr[6] is auto increment
pressure_addr |= inc_spi_flag;
temp_addr |= inc_spi_flag;
}

Adafruit_BusIO_Register pressure_data = Adafruit_BusIO_Register(
Expand Down Expand Up @@ -263,12 +200,12 @@ void Adafruit_LPS2X::_read(void) {
if (raw_temp & 0x8000) {
raw_temp = raw_temp - 0xFFFF;
}
unscaled_temp = raw_temp;
_temp = raw_temp / temp_scaling;

if (raw_pressure & 0x800000) {
raw_pressure = raw_pressure - 0xFFFFFF;
}
unscaled_pressure = raw_pressure;
_pressure = raw_pressure / 4096.0;
}

/*!
Expand Down Expand Up @@ -315,7 +252,7 @@ void Adafruit_LPS2X::fillPressureEvent(sensors_event_t *pressure,
pressure->sensor_id = _sensorid_pressure;
pressure->type = SENSOR_TYPE_PRESSURE;
pressure->timestamp = timestamp;
pressure->pressure = (unscaled_pressure / 4096.0);
pressure->pressure = _pressure;
}

void Adafruit_LPS2X::fillTempEvent(sensors_event_t *temp, uint32_t timestamp) {
Expand All @@ -324,12 +261,13 @@ void Adafruit_LPS2X::fillTempEvent(sensors_event_t *temp, uint32_t timestamp) {
temp->sensor_id = _sensorid_temp;
temp->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
temp->timestamp = timestamp;
temp->temperature = (unscaled_temp / 480) + 42.5;
temp->temperature = _temp;
}

/**************************************************************************/
/*!
@brief Gets the sensor_t data for the LPS2X's tenperature
@param sensor The allocated sensor_t that we will fill!
*/
/**************************************************************************/
void Adafruit_LPS2X_Pressure::getSensor(sensor_t *sensor) {
Expand Down Expand Up @@ -366,6 +304,7 @@ bool Adafruit_LPS2X_Pressure::getEvent(sensors_event_t *event) {
/**************************************************************************/
/*!
@brief Gets the sensor_t data for the LPS2X's tenperature
@param sensor The allocated sensor_t that we will fill!
*/
/**************************************************************************/
void Adafruit_LPS2X_Temp::getSensor(sensor_t *sensor) {
Expand All @@ -381,7 +320,6 @@ void Adafruit_LPS2X_Temp::getSensor(sensor_t *sensor) {
sensor->min_delay = 0;
sensor->min_value = -30;
sensor->max_value = 105;
// 480 LSB = 1°C >> 1 LSB = 1/480°C >> 1 LSB = 0.00208 °C
sensor->resolution = 0.00208;
}

Expand All @@ -398,16 +336,3 @@ bool Adafruit_LPS2X_Temp::getEvent(sensors_event_t *event) {

return true;
}
/**
* @brief Sets the polarity of the INT pin.
*
* @param active_low Set to true to make the pin active low
*/
void Adafruit_LPS2X::interruptsActiveLow(bool active_low) {
Adafruit_BusIO_Register ctrl3 = Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LPS2X_CTRL_REG3, 1);

Adafruit_BusIO_RegisterBits active_low_bit =
Adafruit_BusIO_RegisterBits(&ctrl3, 1, 7);
active_low_bit.write(active_low);
}
Loading

0 comments on commit 6d21da7

Please sign in to comment.