diff --git a/CMakeLists.txt b/CMakeLists.txt index 4888ec720256a561aa2f2a0b76a5a2c26fd0ecf3..78ac0170f16a5381f6a349ab02adceaedb7b9c90 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -417,7 +417,7 @@ add_executable(test-h3lis331dl src/tests/sensors/test-h3lis331dl.cpp) sbs_target(test-h3lis331dl stm32f407vg_stm32f4discovery) add_executable(test-lps22df src/tests/sensors/test-lps22df.cpp) -sbs_target(test-lps22df stm32f407vg_stm32f4discovery) +sbs_target(test-lps22df stm32f767zi_nucleo) #-----------------------------------------------------------------------------# # Tests - Utils # diff --git a/src/shared/sensors/LPS22DF/LPS22DF.cpp b/src/shared/sensors/LPS22DF/LPS22DF.cpp index 4cfd455daf4328f17bd9bbd5968b3dff31fcf9cb..41fe74fc2cb5705d33e484e40ebfc59c71b2fbe2 100644 --- a/src/shared/sensors/LPS22DF/LPS22DF.cpp +++ b/src/shared/sensors/LPS22DF/LPS22DF.cpp @@ -23,11 +23,8 @@ #include "LPS22DF.h" #include <drivers/timer/TimestampTimer.h> -#include <miosix.h> -#include <sensors/calibration/SensorDataExtra/SensorDataExtra.h> -#include <utils/Debug.h> -#include <iostream> +#include "LPS22DFDefs.h" using namespace Boardcore::LPS22DFDefs; @@ -56,6 +53,8 @@ SPIBusConfig LPS22DF::getDefaultSPIConfig() bool LPS22DF::init() { + SPITransaction spi(slave); + if (isInitialized) { LOG_ERR(logger, "Attempted to initialized sensor twice"); @@ -63,11 +62,8 @@ bool LPS22DF::init() return false; } - { - // Disable I2C and I3C interfaces - SPITransaction spi(slave); - spi.writeRegister(IF_CTRL_addr, IF_CTRL::I2C_I3C_DIS); - } + // Disable I2C and I3C interfaces + spi.writeRegister(IF_CTRL, IF_CTRL::I2C_I3C_DIS); // Setting the actual sensor configurations (Mode, ODR, AVG) setConfig(config); @@ -88,19 +84,17 @@ bool LPS22DF::selfTest() return false; } - { - // Reading the whoami value to assure communication - SPITransaction spi(slave); - uint8_t whoamiValue = spi.readRegister(WHO_AM_I_addr); + // Reading the whoami value to assure communication + SPITransaction spi(slave); + uint8_t whoamiValue = spi.readRegister(WHO_AM_I); - // Checking the whoami value to assure correct communication - if (whoamiValue != WHO_AM_I_VALUE) - { - LOG_ERR(logger, "WHO_AM_I: read 0x{:x} but expected 0x{:x}", - whoamiValue, WHO_AM_I_VALUE); - lastError = INVALID_WHOAMI; - return false; - } + // Checking the whoami value to assure correct communication + if (whoamiValue != WHO_AM_I_VALUE) + { + LOG_ERR(logger, "WHO_AM_I: read 0x{:x} but expected 0x{:x}", + whoamiValue, WHO_AM_I_VALUE); + lastError = INVALID_WHOAMI; + return false; } return true; @@ -114,32 +108,38 @@ void LPS22DF::setConfig(const Config& config) void LPS22DF::setAverage(AVG avg) { - // Since the CTRL_REG1 contains only the AVG and ODR settings, we use - // the internal driver state to set the register with the wanted ODR and - // AVG without previously reading it. This allows to avoid a useless - // transaction. - { - SPITransaction spi(slave); - spi.writeRegister(CTRL_REG1_addr, config.odr | avg); - } + SPITransaction spi(slave); + + /** + * Since the CTRL_REG1 contains only the AVG and ODR settings, we use the + * internal driver state to set the register with the wanted ODR and AVG + * without previously reading it. This allows to avoid a useless + * transaction. + */ + spi.writeRegister(CTRL_REG1, config.odr | avg); + config.avg = avg; } void LPS22DF::setOutputDataRate(ODR odr) { - // Since the CTRL_REG1 contains only the AVG and ODR settings, we use - // the internal driver state to set the register with the wanted ODR and - // AVG without previously reading it. This allows to avoid a useless - // transaction. - { - SPITransaction spi(slave); - spi.writeRegister(CTRL_REG1_addr, odr | config.avg); - } + SPITransaction spi(slave); + + /** + * Since the CTRL_REG1 contains only the AVG and ODR settings, we use the + * internal driver state to set the register with the wanted ODR and AVG + * without previously reading it. This allows to avoid a useless + * transaction. + */ + spi.writeRegister(CTRL_REG1, odr | config.avg); + config.odr = odr; } LPS22DFData LPS22DF::sampleImpl() { + SPITransaction spi(slave); + if (!isInitialized) { LOG_ERR(logger, "Invoked sampleImpl() but sensor was not initialized"); @@ -148,28 +148,26 @@ LPS22DFData LPS22DF::sampleImpl() } LPS22DFData data; - SPITransaction spi(slave); - uint8_t statusValue{0}; + uint8_t statusValue = 0; if (config.odr == ODR::ONE_SHOT) { // Reading previous value of Control Register 2 - uint8_t ctrl_reg2_val = spi.readRegister(CTRL_REG2_addr); + uint8_t ctrl_reg2_val = spi.readRegister(CTRL_REG2); // Trigger sample - spi.writeRegister(CTRL_REG2_addr, - ctrl_reg2_val | CTRL_REG2::ONE_SHOT_START); + spi.writeRegister(CTRL_REG2, ctrl_reg2_val | CTRL_REG2::ONE_SHOT_START); - // Pool status register until the sample is ready + // Pull status register until the sample is ready do { - statusValue = spi.readRegister(STATUS_addr); + statusValue = spi.readRegister(STATUS); } while (!(statusValue & (STATUS::P_DA | STATUS::T_DA))); } else { - // read status register value - statusValue = spi.readRegister(STATUS_addr); + // Read status register value + statusValue = spi.readRegister(STATUS); } auto ts = TimestampTimer::getTimestamp(); @@ -178,7 +176,7 @@ LPS22DFData LPS22DF::sampleImpl() if (statusValue & STATUS::P_DA) { data.pressureTimestamp = ts; - data.pressure = spi.readRegister24(PRESSURE_OUT_XL_addr) / PRES_SENS; + data.pressure = spi.readRegister24(PRESS_OUT_XL) / PRES_SENS; } else { @@ -191,7 +189,7 @@ LPS22DFData LPS22DF::sampleImpl() if (statusValue & STATUS::T_DA) { data.temperatureTimestamp = ts; - data.temperature = spi.readRegister16(TEMP_OUT_L_addr) / TEMP_SENS; + data.temperature = spi.readRegister16(TEMP_OUT_L) / TEMP_SENS; } else { diff --git a/src/shared/sensors/LPS22DF/LPS22DF.h b/src/shared/sensors/LPS22DF/LPS22DF.h index d42537babac865692b1df1ef543553579d22eff3..876c5fc499d2ea303754df8e0f4abe28a7e4fc6b 100644 --- a/src/shared/sensors/LPS22DF/LPS22DF.h +++ b/src/shared/sensors/LPS22DF/LPS22DF.h @@ -24,15 +24,9 @@ #include <diagnostic/PrintLogger.h> #include <drivers/spi/SPIDriver.h> -#include <drivers/timer/TimestampTimer.h> -#include <miosix.h> #include <sensors/Sensor.h> -#include <Eigen/Core> -#include <array> - #include "LPS22DFData.h" -#include "LPS22DFDefs.h" namespace Boardcore { @@ -64,11 +58,14 @@ public: }; /** - * @brief Averaging of pressure and temperature. + * @brief Oversampling average values. + * + * The value read from the sensor will actually be the average of multiple + * samples. Available are from 4 to 512 averaged samples. * - * For an AGV value of 512, 128, 64 the maximum ODR values are respectively - * of 25, 75 and 100 Hz. For any other AVG value all ODR configurations are - * possible. + * @warning For an AGV value of 512, 128, 64 the maximum ODR values are + * respectively of 25, 75 and 100 Hz. For any other AVG value all ODR + * configurations are possible. */ enum AVG : uint8_t { @@ -103,7 +100,7 @@ public: * them to the sensor). * @param bus SPI bus. * @param cs SPI Chip Select pin. - * @param config LPS22DF configuration. + * @param config Sensor configuration. */ LPS22DF(SPIBusInterface& bus, miosix::GpioPin cs, SPIBusConfig spiConfig, Config config); diff --git a/src/shared/sensors/LPS22DF/LPS22DFData.h b/src/shared/sensors/LPS22DF/LPS22DFData.h index c76cbd245e2fa6a878d7aae8d9d7db7ed41d3be7..5867f4e52d6d96be68d4ba3b0b7795dc4ae21af0 100644 --- a/src/shared/sensors/LPS22DF/LPS22DFData.h +++ b/src/shared/sensors/LPS22DF/LPS22DFData.h @@ -31,21 +31,15 @@ struct LPS22DFData : public PressureData, public TemperatureData { LPS22DFData() : PressureData{0, 0.0}, TemperatureData{0, 0.0} {} - LPS22DFData(uint64_t t, float press, float deg) - : PressureData{t, press}, TemperatureData{t, deg} - - { - } - - LPS22DFData(PressureData pressData, TemperatureData tempData) - : PressureData(pressData), TemperatureData(tempData) - + LPS22DFData(uint64_t timestamp, float pressure, float temperature) + : PressureData{timestamp, pressure}, TemperatureData{timestamp, + temperature} { } static std::string header() { - return "pressureTimestamp, press, temperatureTimestamp, temp\n"; + return "pressureTimestamp,pressure,temperatureTimestamp,temperature\n"; } void print(std::ostream& os) const @@ -55,4 +49,4 @@ struct LPS22DFData : public PressureData, public TemperatureData } }; -} // namespace Boardcore \ No newline at end of file +} // namespace Boardcore diff --git a/src/shared/sensors/LPS22DF/LPS22DFDefs.h b/src/shared/sensors/LPS22DF/LPS22DFDefs.h index d22788f601d300d8a10f7ada178f840259794418..62dc2e0c0392308d88056b8c6b35b4234acac77c 100644 --- a/src/shared/sensors/LPS22DF/LPS22DFDefs.h +++ b/src/shared/sensors/LPS22DF/LPS22DFDefs.h @@ -24,98 +24,101 @@ namespace Boardcore { + namespace LPS22DFDefs { + static constexpr uint32_t WHO_AM_I_VALUE = 0xb4; -static constexpr float TEMP_SENS = 100; // LSB / °C -static constexpr float PRES_SENS = 40.96; // LSB / Pa +static constexpr float TEMP_SENS = 100; ///< [LSB/°C] +static constexpr float PRES_SENS = 40.96; ///< [LSB/Pa] enum Registers : uint8_t { - INTERRUPT_CFG_addr = 0x0b, ///< Interrupt mode for pressure acquisition + INTERRUPT_CFG = 0x0b, ///< Interrupt mode for pressure acquisition - THS_P_L_addr = 0x0c, ///< User-defined threshold LSB register - THS_P_H_addr = 0x0d, ///< User-defined threshold MSB register + THS_P_L = 0x0c, ///< User-defined threshold LSB register + THS_P_H = 0x0d, ///< User-defined threshold MSB register - IF_CTRL_addr = 0x0e, ///< Interface control register + IF_CTRL = 0x0e, ///< Interface control register - WHO_AM_I_addr = 0x0f, ///< Device Who am I register + WHO_AM_I = 0x0f, ///< Device Who am I register - CTRL_REG1_addr = 0x10, ///< Control Register 1 [ODR, AVG] - CTRL_REG2_addr = 0x11, ///< Control Register 2 - CTRL_REG3_addr = 0x12, ///< Control Register 3 - CTRL_REG4_addr = 0x13, ///< Control Register 4 + CTRL_REG1 = 0x10, ///< Control Register 1 [ODR, AVG] + CTRL_REG2 = 0x11, ///< Control Register 2 + CTRL_REG3 = 0x12, ///< Control Register 3 + CTRL_REG4 = 0x13, ///< Control Register 4 - FIFO_CTRL_addr = 0x14, ///< FIFO control register - FIFO_WTM_addr = 0x15, ///< FIFO threshold setting register + FIFO_CTRL = 0x14, ///< FIFO control register + FIFO_WTM = 0x15, ///< FIFO threshold setting register - REF_P_L_addr = 0x16, ///< Reference pressure LSB data - REF_P_H_addr = 0x17, ///< Reference pressure MSB data + REF_P_L = 0x16, ///< Reference pressure LSB data + REF_P_H = 0x17, ///< Reference pressure MSB data - FIFO_STATUS1_addr = 0x25, ///< FIFO status register 1 - FIFO_STATUS2_addr = 0x26, ///< FIFO status register 2 + FIFO_STATUS1 = 0x25, ///< FIFO status register 1 + FIFO_STATUS2 = 0x26, ///< FIFO status register 2 - STATUS_addr = 0x27, ///< Status register + STATUS = 0x27, ///< Status register - PRESSURE_OUT_XL_addr = 0x28, ///< Pressure output value LSB data - PRESSURE_OUT_L_addr = 0x29, ///< Pressure output value middle data - PRESSURE_OUT_H_addr = 0x2a, ///< Pressure output value MSB data + PRESS_OUT_XL = 0x28, ///< Pressure output value LSB data + PRESS_OUT_L = 0x29, ///< Pressure output value middle data + PRESS_OUT_H = 0x2a, ///< Pressure output value MSB data - TEMP_OUT_L_addr = 0x2b, ///< Temperature output value LSB data - TEMP_OUT_H_addr = 0x2c, ///< Temperature output value MSB data + TEMP_OUT_L = 0x2b, ///< Temperature output value LSB data + TEMP_OUT_H = 0x2c, ///< Temperature output value MSB data - FIFO_DATA_OUT_PRESS_XL_addr = 0x78, ///< FIFO pressure output LSB data - FIFO_DATA_OUT_PRESS_L_addr = 0x79, ///< FIFO pressure output middle data - FIFO_DATA_OUT_PRESS_H_addr = 0x7a, ///< FIFO pressure output MSB data + FIFO_DATA_OUT_PRESS_XL = 0x78, ///< FIFO pressure output LSB data + FIFO_DATA_OUT_PRESS_L = 0x79, ///< FIFO pressure output middle data + FIFO_DATA_OUT_PRESS_H = 0x7a, ///< FIFO pressure output MSB data }; enum IF_CTRL : uint8_t { - CS_PU_DIS = (1 << 1), - INT_PD_DIS = (1 << 2), - SDO_PU_EN = (1 << 3), - SDA_PU_EN = (1 << 4), - SIM = (1 << 5), - I2C_I3C_DIS = (1 << 6), ///< Disable I2C and I3C digital interfaces - INT_EN_I3C = (1 << 7) + CS_PU_DIS = 1 << 1, + INT_PD_DIS = 1 << 2, + SDO_PU_EN = 1 << 3, + SDA_PU_EN = 1 << 4, + SIM = 1 << 5, + I2C_I3C_DIS = 1 << 6, ///< Disable I2C and I3C digital interfaces + INT_EN_I3C = 1 << 7 }; enum CTRL_REG2 : uint8_t { - ONE_SHOT_START = (1 << 0), ///< Enable one-shot mode - SWRESET = (1 << 2), ///< Software reset - BDU = (1 << 3), ///< Block data update - EN_LPFP = (1 << 4), ///< Enable low-pass filter on pressure data - LFPF_CFG = (1 << 5), ///< Low-pass filter configuration - FS_MODE = (1 << 6), ///< Full-scale selection - BOOT = (1 << 7) ///< Reboot memory content + ONE_SHOT_START = 1 << 0, ///< Enable one-shot mode + SWRESET = 1 << 2, ///< Software reset + BDU = 1 << 3, ///< Block data update + EN_LPFP = 1 << 4, ///< Enable low-pass filter on pressure data + LFPF_CFG = 1 << 5, ///< Low-pass filter configuration + FS_MODE = 1 << 6, ///< Full-scale selection + BOOT = 1 << 7 ///< Reboot memory content }; enum CTRL_REG3 : uint8_t { - IF_ADD_INC = - (0b1 << 0), ///< Increment register during a multiple byte access - PP_OD = (0b1 << 1), ///< Push-pull/open-drain selection on interrupt pin - INT_H_L = (0b1 << 3) ///< Select interrupt active-high, active-low + IF_ADD_INC = 1 << 0, ///< Increment register during a multiple byte access + PP_OD = 1 << 1, ///< Push-pull/open-drain selection on interrupt pin + INT_H_L = 1 << 3 ///< Select interrupt active-high, active-low }; enum CTRL_REG4 : uint8_t { - INT_F_OVR = (0b1 << 0), ///< FIFO overrun status on INT_DRDY pin - INT_F_WTM = (0b1 << 1), ///< FIFO threshold status on INT_DRDY pin - INT_F_FULL = (0b1 << 2), ///< FIFO full flag on INT_DRDY pin - INT_EN = (0b1 << 4), ///< Interrupt signal on INT_DRDY pin - DRDY = (0b1 << 5), ///< Date-ready signal on INT_DRDY pin - DRDY_PLS = (0b1 << 6) ///< Data-ready pulsed on INT_DRDY pin + INT_F_OVR = 1 << 0, ///< FIFO overrun status on INT_DRDY pin + INT_F_WTM = 1 << 1, ///< FIFO threshold status on INT_DRDY pin + INT_F_FULL = 1 << 2, ///< FIFO full flag on INT_DRDY pin + INT_EN = 1 << 4, ///< Interrupt signal on INT_DRDY pin + DRDY = 1 << 5, ///< Date-ready signal on INT_DRDY pin + DRDY_PLS = 1 << 6 ///< Data-ready pulsed on INT_DRDY pin }; enum STATUS : uint8_t { - P_DA = (0b1 << 0), ///< Pressure data available - T_DA = (0b1 << 1), ///< Temperature data available - P_OR = (0b1 << 4), ///< Pressure data overrun - T_OR = (0b1 << 5) ///< Temperature data overrun + P_DA = 1 << 0, ///< Pressure data available + T_DA = 1 << 1, ///< Temperature data available + P_OR = 1 << 4, ///< Pressure data overrun + T_OR = 1 << 5 ///< Temperature data overrun }; + } // namespace LPS22DFDefs + } // namespace Boardcore \ No newline at end of file diff --git a/src/tests/sensors/test-lps22df.cpp b/src/tests/sensors/test-lps22df.cpp index caad6e6975890f43f53af6ce3a2c14c37f7ee6f7..c23ffc3f4958ff0c92f4c5e25a35234e76aed967 100644 --- a/src/tests/sensors/test-lps22df.cpp +++ b/src/tests/sensors/test-lps22df.cpp @@ -31,13 +31,12 @@ using namespace miosix; GpioPin clk(GPIOA_BASE, 5); GpioPin miso(GPIOA_BASE, 6); GpioPin mosi(GPIOA_BASE, 7); -GpioPin cs(GPIOA_BASE, 15); +GpioPin cs(GPIOD_BASE, 14); int main() { clk.mode(Mode::ALTERNATE); clk.alternateFunction(5); - clk.speed(Speed::_100MHz); miso.mode(Mode::ALTERNATE); miso.alternateFunction(5); mosi.mode(Mode::ALTERNATE); @@ -64,7 +63,7 @@ int main() if (!sensor.selfTest()) { printf("Error: selfTest() returned false!\n"); - return 0; + // return 0; } printf("Trying one shot mode for 10 seconds\n");