Skip to content
Snippets Groups Projects
Commit aa52d67c authored by EmilioCorigliano's avatar EmilioCorigliano Committed by Alberto Nidasio
Browse files

[LPS28DFW] Removed FIFO option, improved setting of config, simplified...

[LPS28DFW] Removed FIFO option, improved setting of config, simplified conversion of pressure and improved test
parent 87d0d713
Branches
No related tags found
No related merge requests found
...@@ -176,21 +176,21 @@ bool LPS28DFW::init() ...@@ -176,21 +176,21 @@ bool LPS28DFW::init()
return true; return true;
} }
bool LPS28DFW::setConfig(const SensorConfig& sensorConfig) bool LPS28DFW::setConfig(const SensorConfig& newSensorConfig)
{ {
{ {
uint8_t fifo_ctrl{0}; uint8_t fifo_ctrl{0};
switch (sensorConfig.mode) switch (newSensorConfig.mode)
{ {
case Mode::ONE_SHOT_MODE: case Mode::ONE_SHOT_MODE:
fifo_ctrl |= ONE_SHOT; sensorConfig = {newSensorConfig.fsr, newSensorConfig.avg,
Mode::ONE_SHOT_MODE, ODR::ONE_SHOT, false};
fifo_ctrl |= FIFO_CTRL::BYPASS;
break; break;
case Mode::CONTINUOUS_MODE: case Mode::CONTINUOUS_MODE:
sensorConfig = newSensorConfig;
fifo_ctrl |= CONTINUOUS; fifo_ctrl |= CONTINUOUS;
break; break;
case Mode::FIFO_MODE:
fifo_ctrl |= FIFO;
break;
default: default:
LOG_ERR(logger, "Mode not supported"); LOG_ERR(logger, "Mode not supported");
break; break;
...@@ -203,29 +203,19 @@ bool LPS28DFW::setConfig(const SensorConfig& sensorConfig) ...@@ -203,29 +203,19 @@ bool LPS28DFW::setConfig(const SensorConfig& sensorConfig)
} }
} }
if (!(setFullScaleRange(sensorConfig.fsr) && if (!(setFullScaleRange(sensorConfig.fsr) && setAverage(sensorConfig.avg) &&
setOutputDataRate(sensorConfig.odr) && setAverage(sensorConfig.avg))) setOutputDataRate(sensorConfig.odr)))
{ {
LOG_ERR(logger, "Sensor not configured"); LOG_ERR(logger, "Sensor not configured");
return false; return false;
} }
if (sensorConfig.drdy) if (!i2c.writeRegister(i2cConfig, CTRL_REG4_addr,
{ (sensorConfig.drdy ? (INT_EN | DRDY) : 0)))
if (!i2c.writeRegister(i2cConfig, CTRL_REG4_addr, INT_EN | DRDY))
{
lastError = BUS_FAULT;
return false;
}
}
else
{
if (!i2c.writeRegister(i2cConfig, CTRL_REG4_addr, 0))
{ {
lastError = BUS_FAULT; lastError = BUS_FAULT;
return false; return false;
} }
}
return true; return true;
} }
...@@ -337,9 +327,12 @@ LPS28DFWData LPS28DFW::sampleImpl() ...@@ -337,9 +327,12 @@ LPS28DFWData LPS28DFW::sampleImpl()
return lastSample; return lastSample;
} }
// sign extending the 27-bit value: shifting to the right a signed type
// extends its sign. So positioning the bytes shifted to the left of 8
// bits, casting the result in a signed int8_t and then shifting the
// result to the right of 8 bits will make the work.
int32_t press_temp = int32_t press_temp =
(((val[2] & (1 << 7)) ? 0xff : 0x00) | // sign extension ((int32_t)((val[2] << 24) | (val[1] << 16) | (val[0] << 8))) >> 8;
(val[2] << 16) | (val[1] << 8) | (val[0] << 0));
data.pressure = ((float)press_temp / pressureSensitivity); data.pressure = ((float)press_temp / pressureSensitivity);
......
...@@ -91,8 +91,7 @@ public: ...@@ -91,8 +91,7 @@ public:
enum Mode enum Mode
{ {
ONE_SHOT_MODE, // BYPASS, ODR = ONE_SHOT ONE_SHOT_MODE, // BYPASS, ODR = ONE_SHOT
CONTINUOUS_MODE, // BYPASS, ODR = odr CONTINUOUS_MODE // BYPASS, ODR = odr
FIFO_MODE // FIFO, ODR = odr
}; };
typedef struct typedef struct
...@@ -102,7 +101,6 @@ public: ...@@ -102,7 +101,6 @@ public:
Mode mode; ///< Mode of operation Mode mode; ///< Mode of operation
ODR odr; ///< Output data rate ODR odr; ///< Output data rate
bool drdy; ///< Enable Interrupt for Data Ready bool drdy; ///< Enable Interrupt for Data Ready
size_t fifoSize; ///< If fifo, length of fifo
} SensorConfig; } SensorConfig;
/** /**
...@@ -118,7 +116,7 @@ public: ...@@ -118,7 +116,7 @@ public:
LPS28DFW(I2C& i2c, bool sa0, LPS28DFW(I2C& i2c, bool sa0,
SensorConfig sensorConfig = {FullScaleRange::FS_1260, AVG::AVG_32, SensorConfig sensorConfig = {FullScaleRange::FS_1260, AVG::AVG_32,
Mode::CONTINUOUS_MODE, ODR::ODR_100, Mode::CONTINUOUS_MODE, ODR::ODR_100,
false, 0}); false});
/** /**
* @brief Initializes the sensor with the current settings. * @brief Initializes the sensor with the current settings.
......
...@@ -40,13 +40,8 @@ typedef Gpio<GPIOB_BASE, 8> i1scl; ...@@ -40,13 +40,8 @@ typedef Gpio<GPIOB_BASE, 8> i1scl;
typedef Gpio<GPIOB_BASE, 9> i1sda; typedef Gpio<GPIOB_BASE, 9> i1sda;
I2C i2c(I2C1, i1scl::getPin(), i1sda::getPin()); I2C i2c(I2C1, i1scl::getPin(), i1sda::getPin());
LPS28DFW::SensorConfig lps28dfwConfig{LPS28DFW::FullScaleRange::FS_1260, LPS28DFW lps28dfw(i2c, false);
LPS28DFW::AVG::AVG_4, uint8_t nSamples = 10;
LPS28DFW::Mode::CONTINUOUS_MODE,
LPS28DFW::ODR::ODR_10,
true,
0};
LPS28DFW lps28dfw(i2c, false, lps28dfwConfig);
/** /**
* ONE_SHOT | AVG4 : 161.6 us/samp (W1+R1+W2+W1+R5) @1Hz: 1.7 uA [FROM DS] * ONE_SHOT | AVG4 : 161.6 us/samp (W1+R1+W2+W1+R5) @1Hz: 1.7 uA [FROM DS]
* ONE_SHOT | AVG512 : 162 us/samp (W1+R1+W2+W1+R5) @1Hz: 32.2 uA [FROM DS] * ONE_SHOT | AVG512 : 162 us/samp (W1+R1+W2+W1+R5) @1Hz: 32.2 uA [FROM DS]
...@@ -54,26 +49,51 @@ LPS28DFW lps28dfw(i2c, false, lps28dfwConfig); ...@@ -54,26 +49,51 @@ LPS28DFW lps28dfw(i2c, false, lps28dfwConfig);
* ODR1 | AVG512 : 127.8 us/samp (W1+R1+W1+R5) @1Hz: 32.8 uA [FROM DS] * ODR1 | AVG512 : 127.8 us/samp (W1+R1+W1+R5) @1Hz: 32.8 uA [FROM DS]
*/ */
miosix::Thread *waiting = 0; miosix::Thread *waiting = 0;
bool sampleAvailable = false;
void __attribute__((used)) EXTI5_IRQHandlerImpl() void __attribute__((used)) EXTI5_IRQHandlerImpl()
{ {
if (waiting) if (waiting)
{ {
waiting->wakeup(); sampleAvailable = true;
waiting->IRQwakeup();
} }
} }
int main() void sampleOneShotMode(LPS28DFW &lps28dfw)
{ {
waiting = Thread::getCurrentThread(); printf("Start One-Shot\n");
enableExternalInterrupt(GPIOE_BASE, 5, InterruptTrigger::RISING_EDGE); LPS28DFW::SensorConfig lps28dfwConfig{
LPS28DFW::FullScaleRange::FS_1260, LPS28DFW::AVG::AVG_64,
LPS28DFW::Mode::ONE_SHOT_MODE, LPS28DFW::ODR::ONE_SHOT, false};
lps28dfw.setConfig(lps28dfwConfig);
if (!lps28dfw.init()) for (uint8_t i = 0; i < nSamples; i++)
{ {
printf("Error initialization of sensor\n"); lps28dfw.sample();
return 0;
if (lps28dfw.getLastError() == SensorErrors::NO_ERRORS)
{
lps28dfw.getLastSample().print(std::cout);
}
else
{
printf("Error: %d\n", lps28dfw.getLastError());
}
Thread::sleep(100);
} }
for (;;) printf("End One-Shot\n\n");
}
void sampleContinuousMode(LPS28DFW &lps28dfw)
{
printf("Start Continuous\n");
LPS28DFW::SensorConfig lps28dfwConfig{
LPS28DFW::FullScaleRange::FS_1260, LPS28DFW::AVG::AVG_64,
LPS28DFW::Mode::CONTINUOUS_MODE, LPS28DFW::ODR::ODR_10, false};
lps28dfw.setConfig(lps28dfwConfig);
for (uint8_t i = 0; i < nSamples; i++)
{ {
lps28dfw.sample(); lps28dfw.sample();
...@@ -85,8 +105,63 @@ int main() ...@@ -85,8 +105,63 @@ int main()
{ {
printf("Error: %d\n", lps28dfw.getLastError()); printf("Error: %d\n", lps28dfw.getLastError());
} }
Thread::sleep(100);
}
printf("End Continuous\n\n");
}
void sampleInterruptMode(LPS28DFW &lps28dfw)
{
printf("Start Interrupt\n");
LPS28DFW::SensorConfig lps28dfwConfig{
LPS28DFW::FullScaleRange::FS_1260, LPS28DFW::AVG::AVG_64,
LPS28DFW::Mode::CONTINUOUS_MODE, LPS28DFW::ODR::ODR_10, true};
lps28dfw.setConfig(lps28dfwConfig);
for (uint8_t i = 0; i < nSamples; i++)
{
while (!sampleAvailable)
{
waiting->wait(); waiting->wait();
} }
sampleAvailable = false;
lps28dfw.sample();
if (lps28dfw.getLastError() == SensorErrors::NO_ERRORS)
{
lps28dfw.getLastSample().print(std::cout);
}
else
{
printf("Error: %d\n", lps28dfw.getLastError());
}
}
printf("End Interrupt\n\n");
}
int main()
{
waiting = Thread::getCurrentThread();
enableExternalInterrupt(GPIOE_BASE, 5, InterruptTrigger::RISING_EDGE);
if (!lps28dfw.init())
{
printf("Error initialization of sensor\n");
return 0;
}
for (;;)
{
printf("\nNew set of samples\n");
sampleOneShotMode(lps28dfw);
sampleContinuousMode(lps28dfw);
sampleInterruptMode(lps28dfw);
}
return 0; return 0;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment