From fb032f703de80e5a6a0f1e6cba14d32aec576677 Mon Sep 17 00:00:00 2001 From: Aymane Bahssain Date: Fri, 6 Mar 2026 16:27:55 +0100 Subject: [PATCH 1/7] feat(I3C): adding support for I3C Signed-off-by: Aymane Bahssain --- CMakeLists.txt | 31 ++++++++++++ src/LPS22DFSensor.cpp | 107 +++++++++++++++++++++++++++++++++++++++++- src/LPS22DFSensor.h | 35 ++++++++++++++ 3 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..942a9fa --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,31 @@ +# v3.21 implemented semantic changes regarding $ +# See https://cmake.org/cmake/help/v3.21/command/target_link_libraries.html#linking-object-libraries-via-target-objects +cmake_minimum_required(VERSION 3.21) + +add_library(LPS22DF INTERFACE) +add_library(LPS22DF_usage INTERFACE) + +target_include_directories(LPS22DF_usage INTERFACE + src +) + + +target_link_libraries(LPS22DF_usage INTERFACE + base_config +) + +target_link_libraries(LPS22DF INTERFACE LPS22DF_usage) + + + +add_library(LPS22DF_bin OBJECT EXCLUDE_FROM_ALL + src/lps22df_reg.c + src/LPS22DFSensor.cpp +) +target_link_libraries(LPS22DF_bin PUBLIC LPS22DF_usage) + +target_link_libraries(LPS22DF INTERFACE + LPS22DF_bin + $ +) + diff --git a/src/LPS22DFSensor.cpp b/src/LPS22DFSensor.cpp index 70d11ee..3190ccd 100644 --- a/src/LPS22DFSensor.cpp +++ b/src/LPS22DFSensor.cpp @@ -71,6 +71,32 @@ LPS22DFSensor::LPS22DFSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : de enabled = 0L; } +LPS22DFSensor::LPS22DFSensor(I3CBus *i3c, uint8_t staticAddr7, uint8_t dynAddr7) +{ + reg_ctx.write_reg = LPS22DF_io_write; + reg_ctx.read_reg = LPS22DF_io_read; + reg_ctx.handle = (void *)this; + + dev_i2c = NULL; + dev_spi = NULL; + dev_i3c = i3c; + + address = (dynAddr7 != 0) ? dynAddr7 : staticAddr7; + i3c_static7 = staticAddr7; + i3c_dyn7 = dynAddr7; + + enabled = 0U; + initialized = 0U; + + bus_type = LPS22DF_I3C_BUS; +} + +void LPS22DFSensor::set_address(uint8_t dynAddr7) +{ + + address = dynAddr7; +} + /** * @brief Configure the sensor in order to be used * @retval 0 in case of success, an error code otherwise @@ -86,21 +112,62 @@ LPS22DFStatusTypeDef LPS22DFSensor::begin() digitalWrite(cs_pin, HIGH); } + if (dev_i3c != nullptr) { + Serial.println("I3C"); + + bool doAutoSetdasa = (i3c_static7 != 0) && (i3c_dyn7 != 0); + + if (doAutoSetdasa) { + dev_i3c->setClock(1000000); // 1 MHz mixte I3C+I2C + + if (!dev_i3c->assignDynamicAddress(i3c_static7, i3c_dyn7)) { + Serial.println("I3C: SETDASA failed"); + return LPS22DF_ERROR; + } + + address = i3c_dyn7; + + uint8_t id = 0; + if (ReadID(&id) != LPS22DF_OK || id != LPS22DF_ID) { + Serial.print("I3C: probe failed after SETDASA, id=0x"); + Serial.println(id, HEX); + return LPS22DF_ERROR; + } + + Serial.println("I3C: dynamic address and WHO_AM_I OK"); + } else { + + uint8_t id = 0; + if (ReadID(&id) != LPS22DF_OK || id != LPS22DF_ID) { + Serial.print("I3C: manual mode: WHO_AM_I failed, id=0x"); + Serial.println(id, HEX); + return LPS22DF_ERROR; + } + Serial.println("I3C: manual mode, WHO_AM_I OK at address 0x"); + Serial.println(address, HEX); + } + dev_i3c->setClock(12500000); + } + /* Set bdu and if_inc recommended for driver usage */ if (lps22df_init_set(®_ctx, LPS22DF_DRV_RDY) != LPS22DF_OK) { return LPS22DF_ERROR; } /* Select bus interface */ - if (bus_type == LPS22DF_SPI_3WIRES_BUS) { /* SPI 3-Wires */ + if (bus_type == LPS22DF_SPI_3WIRES_BUS) { bus_mode.interface = lps22df_bus_mode_t::LPS22DF_SPI_3W; - } else if (bus_type == LPS22DF_SPI_4WIRES_BUS) { /* SPI 3-Wires */ + } else if (bus_type == LPS22DF_SPI_4WIRES_BUS) { bus_mode.interface = lps22df_bus_mode_t::LPS22DF_SPI_4W; + } else if (bus_type == LPS22DF_I3C_BUS) { + bus_mode.interface = lps22df_bus_mode_t::LPS22DF_INT_PIN_ON_I3C; } else { bus_mode.interface = lps22df_bus_mode_t::LPS22DF_SEL_BY_HW; } bus_mode.filter = lps22df_bus_mode_t::LPS22DF_AUTO; + bus_mode.i3c_ibi_time = lps22df_bus_mode_t::LPS22DF_IBI_1ms; + if (lps22df_bus_mode_set(®_ctx, &bus_mode) != LPS22DF_OK) { return LPS22DF_ERROR; } @@ -501,3 +568,39 @@ int32_t LPS22DF_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16 { return ((LPS22DFSensor *)handle)->IO_Read(pBuffer, ReadAddr, nBytesToRead); } + +LPS22DFStatusTypeDef LPS22DFSensor::ConfigureDataReadyOnI3cIbi() +{ + if (dev_i3c == nullptr) { + return LPS22DF_ERROR; + } + + if (Write_Reg(LPS22DF_IF_CTRL, 0x80) != LPS22DF_OK) { + return LPS22DF_ERROR; + } + + if (Write_Reg(LPS22DF_CTRL_REG4, 0x30) != LPS22DF_OK) { + return LPS22DF_ERROR; + } + + return LPS22DF_OK; +} + +LPS22DFStatusTypeDef LPS22DFSensor::EnableIbiOnBus(uint8_t targetIndex, + uint32_t timeoutMs, + bool withPayload) +{ + if (dev_i3c == nullptr) { + return LPS22DF_ERROR; + } + + if (dev_i3c->enableIbi(targetIndex, address, withPayload, false, timeoutMs) != 0) { + return LPS22DF_ERROR; + } + + if (dev_i3c->enableControllerEvents() != 0) { + return LPS22DF_ERROR; + } + + return LPS22DF_OK; +} \ No newline at end of file diff --git a/src/LPS22DFSensor.h b/src/LPS22DFSensor.h index 42cd083..46f6766 100644 --- a/src/LPS22DFSensor.h +++ b/src/LPS22DFSensor.h @@ -53,6 +53,7 @@ #include "Wire.h" #include "SPI.h" #include "lps22df_reg.h" +#include "I3C.h" /* Defines -------------------------------------------------------------------*/ @@ -60,6 +61,15 @@ #define LPS22DF_I2C_BUS 0U #define LPS22DF_SPI_4WIRES_BUS 1U #define LPS22DF_SPI_3WIRES_BUS 2U +#define LPS22DF_I3C_BUS 3U + +/** I3C/I2C Device Static Address 7 bit format if SA0=0 -> 0x5C if SA0=1 -> 0x5D **/ +#define LPS22DF_I3C_ADD_L 0x5CU +#define LPS22DF_I3C_ADD_H 0x5DU + +/** I3C/I2C Device PID**/ +static const uint64_t LPS22DF_I3C_PID_L = 0x020800B4100BULL; +static const uint64_t LPS22DF_I3C_PID_H = 0x020800B4900BULL; /* Typedefs ------------------------------------------------------------------*/ @@ -79,6 +89,7 @@ class LPS22DFSensor { public: LPS22DFSensor(TwoWire *i2c, uint8_t address = LPS22DF_I2C_ADD_H); LPS22DFSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed = 2000000); + LPS22DFSensor(I3CBus *i3c, uint8_t staticAddr7 = 0, uint8_t dynAddr7 = 0); LPS22DFStatusTypeDef begin(); LPS22DFStatusTypeDef end(); @@ -100,6 +111,13 @@ class LPS22DFSensor { LPS22DFStatusTypeDef Set_One_Shot(); LPS22DFStatusTypeDef Get_One_Shot_Status(uint8_t *Status); + LPS22DFStatusTypeDef ConfigureDataReadyOnI3cIbi(); + LPS22DFStatusTypeDef EnableIbiOnBus(uint8_t targetIndex = 1, + uint32_t timeoutMs = 1000, + bool withPayload = true); + + void set_address(uint8_t dynAddr7); + /** * @brief Utility function to read data. * @param pBuffer: pointer to data to be read. @@ -144,6 +162,13 @@ class LPS22DFSensor { return 0; } + if (dev_i3c) { + if (dev_i3c->readRegBuffer(address, RegisterAddr, pBuffer, NumByteToRead) == 0) { + return 0; + } + return 1; + } + return 1; } @@ -187,6 +212,12 @@ class LPS22DFSensor { return 0; } + if (dev_i3c) { + if (dev_i3c->writeRegBuffer(address, RegisterAddr, pBuffer, NumByteToWrite) == 0) { + return 0; + } + return 1; + } return 1; } @@ -198,6 +229,7 @@ class LPS22DFSensor { /* Helper classes. */ TwoWire *dev_i2c; SPIClass *dev_spi; + I3CBus *dev_i3c; uint32_t bus_type; /*0 means I2C, 1 means SPI 4-Wires, 2 means SPI-3-Wires */ uint8_t initialized; @@ -209,6 +241,9 @@ class LPS22DFSensor { int cs_pin; uint32_t spi_speed; + uint8_t i3c_static7; + uint8_t i3c_dyn7; + lps22df_ctx_t reg_ctx; }; From c383e5fd985a0c6a50486c75b73798f4c1400e62 Mon Sep 17 00:00:00 2001 From: Aymane Bahssain Date: Mon, 27 Apr 2026 11:29:08 +0200 Subject: [PATCH 2/7] chore: ensure stm32 core version is higher than 2.12.0 Signed-off-by: Aymane Bahssain --- src/LPS22DFSensor.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/LPS22DFSensor.h b/src/LPS22DFSensor.h index 46f6766..7d8e1e0 100644 --- a/src/LPS22DFSensor.h +++ b/src/LPS22DFSensor.h @@ -41,6 +41,10 @@ #ifndef __LPS22DFSensor_H__ #define __LPS22DFSensor_H__ +/* Core compatibility --------------------------------------------------------*/ +#if defined(STM32_CORE_VERSION) && (STM32_CORE_VERSION <= 0x020C0000) + #error "This library is not compatible with core version used. Please update the core." +#endif /* Includes ------------------------------------------------------------------*/ /* For compatibility with ESP32 platforms */ From 5465fd7a452b9bc43c8b0a0ed9c6957305f6a97a Mon Sep 17 00:00:00 2001 From: Aymane Bahssain Date: Mon, 18 May 2026 14:22:44 +0200 Subject: [PATCH 3/7] feat(I3C): add I3C examples Signed-off-by: Aymane Bahssain --- .../LPS22DF_I3C_Basic/LPS22DF_I3C_Basic.ino | 53 ++++++++++++++ .../LPS22DF_I3C_DynAddrAssign.ino | 71 +++++++++++++++++++ examples/LPS22DF_I3C_IBI/LPS22DF_I3C_IBI.ino | 46 ++++++++++++ extras/codespell-ignore-words-list.txt | 1 + 4 files changed, 171 insertions(+) create mode 100644 examples/LPS22DF_I3C_Basic/LPS22DF_I3C_Basic.ino create mode 100644 examples/LPS22DF_I3C_DynAddrAssign/LPS22DF_I3C_DynAddrAssign.ino create mode 100644 examples/LPS22DF_I3C_IBI/LPS22DF_I3C_IBI.ino diff --git a/examples/LPS22DF_I3C_Basic/LPS22DF_I3C_Basic.ino b/examples/LPS22DF_I3C_Basic/LPS22DF_I3C_Basic.ino new file mode 100644 index 0000000..628c670 --- /dev/null +++ b/examples/LPS22DF_I3C_Basic/LPS22DF_I3C_Basic.ino @@ -0,0 +1,53 @@ +#include "I3C.h" +#include "LPS22DFSensor.h" + +static const uint8_t LPS22DF_DYN_ADDR = 0x30; + +LPS22DFSensor sensor(&I3C, LPS22DF_I3C_ADD_H, LPS22DF_DYN_ADDR); + +void setup() { + Serial.begin(115200); + while (!Serial) {} + delay(1000); + + Serial.println("=== LPS22DF SETDASA ==="); + + if (!I3C.begin(I3C_SDA, I3C_SCL, 1000000U)) { + Serial.println("begin() failed"); + while (1) {} + } + + if (!I3C.resetDynamicAddresses()) { + Serial.println("resetDynamicAddresses() failed"); + while (1) {} + } + + if (sensor.begin() != LPS22DF_OK) { + Serial.println("sensor.begin() failed"); + while (1) {} + } + + if (sensor.Enable() != LPS22DF_OK) { + Serial.println("sensor.Enable() failed"); + while (1) {} + } + + Serial.println("LPS22DF ready"); +} + +void loop() { + float p = 0.0f; + float t = 0.0f; + + if (sensor.GetPressure(&p) == LPS22DF_OK && sensor.GetTemperature(&t) == LPS22DF_OK) { + Serial.print("P = "); + Serial.print(p, 2); + Serial.print(" hPa T = "); + Serial.print(t, 1); + Serial.println(" C"); + } else { + Serial.println("Read failed"); + } + + delay(500); +} diff --git a/examples/LPS22DF_I3C_DynAddrAssign/LPS22DF_I3C_DynAddrAssign.ino b/examples/LPS22DF_I3C_DynAddrAssign/LPS22DF_I3C_DynAddrAssign.ino new file mode 100644 index 0000000..b707966 --- /dev/null +++ b/examples/LPS22DF_I3C_DynAddrAssign/LPS22DF_I3C_DynAddrAssign.ino @@ -0,0 +1,71 @@ +#include "I3C.h" +#include "LPS22DFSensor.h" + +LPS22DFSensor sensor(&I3C, 0x00); + +void setup() { + Serial.begin(115200); + while (!Serial) {} + delay(1000); + + Serial.println("=== LPS22DF DAA ==="); + + if (!I3C.begin(I3C_SDA, I3C_SCL, 1000000U)) { + Serial.println("begin() failed"); + while (1) {} + } + + I3CDiscoveredDevice devices[8]{}; + size_t found = 0; + + if (I3C.discover(devices, 8, &found)) { + Serial.println("discover() failed"); + while (1) {} + } + + uint8_t lpsDynAddr = 0U; + + for (size_t i = 0; i < found; ++i) { + Serial.println(devices[i].pid,HEX); + if (devices[i].pid == LPS22DF_I3C_PID_H) { + lpsDynAddr = devices[i].dynAddr; + break; + } + } + + sensor.set_address(lpsDynAddr); + + if (lpsDynAddr == 0U) { + Serial.println("Sensor not found failed"); + while (1) {} + } + + if (sensor.begin() != LPS22DF_OK) { + Serial.println("sensor.begin() failed"); + while (1) {} + } + + if (sensor.Enable() != LPS22DF_OK) { + Serial.println("sensor.Enable() failed"); + while (1) {} + } + + Serial.println("LPS22DF ready"); +} + +void loop() { + float p = 0.0f; + float t = 0.0f; + + if (sensor.GetPressure(&p) == LPS22DF_OK && sensor.GetTemperature(&t) == LPS22DF_OK) { + Serial.print("P = "); + Serial.print(p, 2); + Serial.print(" hPa T = "); + Serial.print(t, 1); + Serial.println(" C"); + } else { + Serial.println("Read failed"); + } + + delay(500); +} diff --git a/examples/LPS22DF_I3C_IBI/LPS22DF_I3C_IBI.ino b/examples/LPS22DF_I3C_IBI/LPS22DF_I3C_IBI.ino new file mode 100644 index 0000000..6003483 --- /dev/null +++ b/examples/LPS22DF_I3C_IBI/LPS22DF_I3C_IBI.ino @@ -0,0 +1,46 @@ +#include "I3C.h" +#include "LPS22DFSensor.h" + +LPS22DFSensor sensor(&I3C, LPS22DF_I3C_ADD_H, 0x30); +static volatile bool ibiPending = false; + +void setup() { + Serial.begin(115200); + while (!Serial) {} + + I3C.begin(I3C_SDA, I3C_SCL, 1000000U); + I3C.onIbi([](const I3CControllerIbiInfo&, void*) { + ibiPending = true; + }, + nullptr); + + sensor.begin(); + sensor.SetOutputDataRate(10.0f); + sensor.ConfigureDataReadyOnI3cIbi(); + sensor.EnableIbiOnBus(); + sensor.Enable(); + + Serial.println("Waiting for IBI ..."); +} + +void loop() { + if (!ibiPending && !I3C.hasIbi()) { + return; + } + + ibiPending = false; + + I3CControllerIbiInfo ibi{}; + if (!I3C.readIbi(ibi)) { + return; + } + + float p, t; + if (sensor.GetPressure(&p) == LPS22DF_OK && sensor.GetTemperature(&t) == LPS22DF_OK) { + Serial.print(" P = "); + Serial.print(p, 2); + Serial.print(" hPa, T = "); + Serial.print(t, 1); + Serial.println(" C"); + } +} diff --git a/extras/codespell-ignore-words-list.txt b/extras/codespell-ignore-words-list.txt index 6f65d22..8df130b 100644 --- a/extras/codespell-ignore-words-list.txt +++ b/extras/codespell-ignore-words-list.txt @@ -1 +1,2 @@ +daa ths \ No newline at end of file From f171c5a9bc769b0caec64baf56aa66c1622810db Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Mon, 13 Jul 2026 10:28:31 +0200 Subject: [PATCH 4/7] ci: enhance compilation step Allows to - use new compile-example feature like /user-core-repo - compile I3C examples only for boards with I3C support Signed-off-by: Frederic Pillon --- .github/workflows/Continuous-integration.yml | 6 +++-- extras/build_config.json | 25 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 extras/build_config.json diff --git a/.github/workflows/Continuous-integration.yml b/.github/workflows/Continuous-integration.yml index 7d9340a..20d3a6e 100644 --- a/.github/workflows/Continuous-integration.yml +++ b/.github/workflows/Continuous-integration.yml @@ -59,8 +59,10 @@ jobs: id: compile uses: stm32duino/actions/compile-examples@main with: - board-pattern: "NUCLEO_L476RG" - + custom-config: "./extras/build_config.json" + board-pattern: "NUCLEO_L476RG|NUCLEO_H503RB" + env: + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} # Use the output from the `Compilation` step - name: Compilation Errors if: failure() diff --git a/extras/build_config.json b/extras/build_config.json new file mode 100644 index 0000000..9253f07 --- /dev/null +++ b/extras/build_config.json @@ -0,0 +1,25 @@ +{ + "cores": [ + { + "maintainer": "STMicroelectronics", + "architecture": "stm32", + "boards": [ + ], + "sketches": [ + { + "pattern": "[^I][^3][^C]", + "applicable": true, + "boards": [ + "NUCLEO_L476RG", + "NUCLEO_H503RB" + ] + }, + { + "pattern": "I3C", + "applicable": true, + "boards": [ "NUCLEO_H503RB"] + } + ] + } + ] +} From ed4a838e298d55dc4bcd9f3ea9d9b3b0eefc3d90 Mon Sep 17 00:00:00 2001 From: Aymane Bahssain Date: Wed, 15 Jul 2026 10:48:10 +0200 Subject: [PATCH 5/7] feat(LPS22DF): guard I3C code with I3C_SUPPORTED Signed-off-by: Aymane Bahssain --- src/LPS22DFSensor.cpp | 19 +++++++++++++++---- src/LPS22DFSensor.h | 34 ++++++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/LPS22DFSensor.cpp b/src/LPS22DFSensor.cpp index 3190ccd..d2ff1e7 100644 --- a/src/LPS22DFSensor.cpp +++ b/src/LPS22DFSensor.cpp @@ -71,6 +71,7 @@ LPS22DFSensor::LPS22DFSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : de enabled = 0L; } +#if defined(I3C_SUPPORTED) LPS22DFSensor::LPS22DFSensor(I3CBus *i3c, uint8_t staticAddr7, uint8_t dynAddr7) { reg_ctx.write_reg = LPS22DF_io_write; @@ -93,9 +94,9 @@ LPS22DFSensor::LPS22DFSensor(I3CBus *i3c, uint8_t staticAddr7, uint8_t dynAddr7) void LPS22DFSensor::set_address(uint8_t dynAddr7) { - address = dynAddr7; } +#endif /** * @brief Configure the sensor in order to be used @@ -112,6 +113,7 @@ LPS22DFStatusTypeDef LPS22DFSensor::begin() digitalWrite(cs_pin, HIGH); } +#if defined(I3C_SUPPORTED) if (dev_i3c != nullptr) { Serial.println("I3C"); @@ -148,6 +150,7 @@ LPS22DFStatusTypeDef LPS22DFSensor::begin() } dev_i3c->setClock(12500000); } +#endif /* Set bdu and if_inc recommended for driver usage */ if (lps22df_init_set(®_ctx, LPS22DF_DRV_RDY) != LPS22DF_OK) { @@ -159,14 +162,20 @@ LPS22DFStatusTypeDef LPS22DFSensor::begin() bus_mode.interface = lps22df_bus_mode_t::LPS22DF_SPI_3W; } else if (bus_type == LPS22DF_SPI_4WIRES_BUS) { bus_mode.interface = lps22df_bus_mode_t::LPS22DF_SPI_4W; - } else if (bus_type == LPS22DF_I3C_BUS) { + } +#if defined(I3C_SUPPORTED) + else if (bus_type == LPS22DF_I3C_BUS) { bus_mode.interface = lps22df_bus_mode_t::LPS22DF_INT_PIN_ON_I3C; - } else { + } +#endif + else { bus_mode.interface = lps22df_bus_mode_t::LPS22DF_SEL_BY_HW; } bus_mode.filter = lps22df_bus_mode_t::LPS22DF_AUTO; +#if defined(I3C_SUPPORTED) bus_mode.i3c_ibi_time = lps22df_bus_mode_t::LPS22DF_IBI_1ms; +#endif if (lps22df_bus_mode_set(®_ctx, &bus_mode) != LPS22DF_OK) { return LPS22DF_ERROR; @@ -569,6 +578,7 @@ int32_t LPS22DF_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16 return ((LPS22DFSensor *)handle)->IO_Read(pBuffer, ReadAddr, nBytesToRead); } +#if defined(I3C_SUPPORTED) LPS22DFStatusTypeDef LPS22DFSensor::ConfigureDataReadyOnI3cIbi() { if (dev_i3c == nullptr) { @@ -603,4 +613,5 @@ LPS22DFStatusTypeDef LPS22DFSensor::EnableIbiOnBus(uint8_t targetIndex, } return LPS22DF_OK; -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/LPS22DFSensor.h b/src/LPS22DFSensor.h index 7d8e1e0..f4a650d 100644 --- a/src/LPS22DFSensor.h +++ b/src/LPS22DFSensor.h @@ -57,24 +57,28 @@ #include "Wire.h" #include "SPI.h" #include "lps22df_reg.h" -#include "I3C.h" +#if (defined(I3C1_BASE) || defined(I3C2_BASE)) && !defined(I3C_SUPPORTED) + #define I3C_SUPPORTED + #include "I3C.h" +#endif /* Defines -------------------------------------------------------------------*/ #define LPS22DF_I2C_BUS 0U #define LPS22DF_SPI_4WIRES_BUS 1U #define LPS22DF_SPI_3WIRES_BUS 2U -#define LPS22DF_I3C_BUS 3U - -/** I3C/I2C Device Static Address 7 bit format if SA0=0 -> 0x5C if SA0=1 -> 0x5D **/ -#define LPS22DF_I3C_ADD_L 0x5CU -#define LPS22DF_I3C_ADD_H 0x5DU +#if defined(I3C_SUPPORTED) + #define LPS22DF_I3C_BUS 3U -/** I3C/I2C Device PID**/ -static const uint64_t LPS22DF_I3C_PID_L = 0x020800B4100BULL; -static const uint64_t LPS22DF_I3C_PID_H = 0x020800B4900BULL; + /** I3C/I2C Device Static Address 7 bit format if SA0=0 -> 0x5C if SA0=1 -> 0x5D **/ + #define LPS22DF_I3C_ADD_L 0x5CU + #define LPS22DF_I3C_ADD_H 0x5DU + /** I3C/I2C Device PID**/ + static const uint64_t LPS22DF_I3C_PID_L = 0x020800B4100BULL; + static const uint64_t LPS22DF_I3C_PID_H = 0x020800B4900BULL; +#endif /* Typedefs ------------------------------------------------------------------*/ @@ -93,7 +97,9 @@ class LPS22DFSensor { public: LPS22DFSensor(TwoWire *i2c, uint8_t address = LPS22DF_I2C_ADD_H); LPS22DFSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed = 2000000); +#if defined(I3C_SUPPORTED) LPS22DFSensor(I3CBus *i3c, uint8_t staticAddr7 = 0, uint8_t dynAddr7 = 0); +#endif LPS22DFStatusTypeDef begin(); LPS22DFStatusTypeDef end(); @@ -166,12 +172,13 @@ class LPS22DFSensor { return 0; } +#if defined(I3C_SUPPORTED) if (dev_i3c) { if (dev_i3c->readRegBuffer(address, RegisterAddr, pBuffer, NumByteToRead) == 0) { return 0; } - return 1; } +#endif return 1; } @@ -216,12 +223,15 @@ class LPS22DFSensor { return 0; } + +#if defined(I3C_SUPPORTED) if (dev_i3c) { if (dev_i3c->writeRegBuffer(address, RegisterAddr, pBuffer, NumByteToWrite) == 0) { return 0; } return 1; } +#endif return 1; } @@ -233,7 +243,9 @@ class LPS22DFSensor { /* Helper classes. */ TwoWire *dev_i2c; SPIClass *dev_spi; +#if defined(I3C_SUPPORTED) I3CBus *dev_i3c; +#endif uint32_t bus_type; /*0 means I2C, 1 means SPI 4-Wires, 2 means SPI-3-Wires */ uint8_t initialized; @@ -245,8 +257,10 @@ class LPS22DFSensor { int cs_pin; uint32_t spi_speed; +#if defined(I3C_SUPPORTED) uint8_t i3c_static7; uint8_t i3c_dyn7; +#endif lps22df_ctx_t reg_ctx; }; From fe49cc7b2d60338a1765a71812601068d9785ad2 Mon Sep 17 00:00:00 2001 From: Davide QUARANTIELLO Date: Thu, 6 Aug 2026 14:40:42 +0200 Subject: [PATCH 6/7] adjust the begin for i3c,remove ibi functionalities and example, update library.properties and readme --- README.md | 23 ++++- .../LPS22DF_I3C_Basic/LPS22DF_I3C_Basic.ino | 30 +++++- .../LPS22DF_I3C_DynAddrAssign.ino | 33 ++++++- examples/LPS22DF_I3C_IBI/LPS22DF_I3C_IBI.ino | 46 ---------- keywords.txt | 11 +++ library.properties | 2 +- src/LPS22DFSensor.cpp | 91 +++---------------- src/LPS22DFSensor.h | 10 +- 8 files changed, 108 insertions(+), 138 deletions(-) delete mode 100644 examples/LPS22DF_I3C_IBI/LPS22DF_I3C_IBI.ino diff --git a/README.md b/README.md index e79e23a..37aad7a 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Arduino library to support the LPS22DF 260-1260 hPa absolute digital output baro ## API -This sensor uses I2C or SPI to communicate. +This sensor uses I2C, SPI or I3C to communicate. For I2C it is then required to create a TwoWire interface before accessing to the sensors: TwoWire dev_i2c(I2C_SDA, I2C_SCL); @@ -27,6 +27,27 @@ An instance can be created and enabled when the SPI bus is used following the pr PressTemp.begin(); PressTemp.Enable(); +An instance can be created and enabled when the I3C bus is used with SETDASA (static-to-dynamic address assignment): + + LPS22DFSensor PressTemp(&I3C, LPS22DF_I3C_ADD_H, 0x30); + I3C.begin(I3C_SDA, I3C_SCL, 1000000U); + I3C.resetDynamicAddresses(); + I3C.assignDynamicAddress(PressTemp.getStaticAddress(), PressTemp.getDynAddress()); + PressTemp.begin(); + I3C.setClock(12500000); + PressTemp.Enable(); + +An instance can be created and enabled when the I3C bus is used with ENTDAA (dynamic address discovery): + + LPS22DFSensor PressTemp(&I3C); + I3C.begin(I3C_SDA, I3C_SCL, 1000000U); + I3C.discover(devices, 8, &found); + // find dynAddr by matching LPS22DF_I3C_PID_H in discovered devices + PressTemp.set_address(dynAddr); + PressTemp.begin(); + I3C.setClock(12500000); + PressTemp.Enable(); + The access to the sensor values is done as explained below: Read pressure and temperature. diff --git a/examples/LPS22DF_I3C_Basic/LPS22DF_I3C_Basic.ino b/examples/LPS22DF_I3C_Basic/LPS22DF_I3C_Basic.ino index 628c670..206bed6 100644 --- a/examples/LPS22DF_I3C_Basic/LPS22DF_I3C_Basic.ino +++ b/examples/LPS22DF_I3C_Basic/LPS22DF_I3C_Basic.ino @@ -1,9 +1,21 @@ -#include "I3C.h" -#include "LPS22DFSensor.h" +/* + @file LPS22DF_I3C_Basic.ino + @author STMicroelectronics + @brief Example to use the LPS22DF pressure sensor with I3C and SETDASA command + ******************************************************************************* + Copyright (c) 2026, STMicroelectronics + All rights reserved. + + This software component is licensed by ST under BSD 3-Clause license, + the "License"; You may not use this file except in compliance with the + License. You may obtain a copy of the License at: + opensource.org/licenses/BSD-3-Clause -static const uint8_t LPS22DF_DYN_ADDR = 0x30; + ******************************************************************************* +*/ +#include "LPS22DFSensor.h" -LPS22DFSensor sensor(&I3C, LPS22DF_I3C_ADD_H, LPS22DF_DYN_ADDR); +LPS22DFSensor sensor(&I3C, LPS22DF_I3C_ADD_H, 0x30); void setup() { Serial.begin(115200); @@ -22,11 +34,21 @@ void setup() { while (1) {} } + if (!I3C.assignDynamicAddress(sensor.getStaticAddress(), sensor.getDynAddress())) { + Serial.println("assignDynamicAddress() failed"); + while (1) {} + } + if (sensor.begin() != LPS22DF_OK) { Serial.println("sensor.begin() failed"); while (1) {} } + if (!I3C.setClock(12500000)) { + Serial.println("setClock() failed"); + while (1) {} + } + if (sensor.Enable() != LPS22DF_OK) { Serial.println("sensor.Enable() failed"); while (1) {} diff --git a/examples/LPS22DF_I3C_DynAddrAssign/LPS22DF_I3C_DynAddrAssign.ino b/examples/LPS22DF_I3C_DynAddrAssign/LPS22DF_I3C_DynAddrAssign.ino index b707966..2d7beda 100644 --- a/examples/LPS22DF_I3C_DynAddrAssign/LPS22DF_I3C_DynAddrAssign.ino +++ b/examples/LPS22DF_I3C_DynAddrAssign/LPS22DF_I3C_DynAddrAssign.ino @@ -1,7 +1,22 @@ -#include "I3C.h" +/* + @file LPS22DF_I3C_DynAddrAssign.ino + @author STMicroelectronics + @brief Example to use the LPS22DF pressure sensor with I3C dynamic address assignment + ******************************************************************************* + Copyright (c) 2026, STMicroelectronics + All rights reserved. + + This software component is licensed by ST under BSD 3-Clause license, + the "License"; You may not use this file except in compliance with the + License. You may obtain a copy of the License at: + opensource.org/licenses/BSD-3-Clause + + ******************************************************************************* +*/ + #include "LPS22DFSensor.h" -LPS22DFSensor sensor(&I3C, 0x00); +LPS22DFSensor sensor(&I3C); void setup() { Serial.begin(115200); @@ -33,10 +48,13 @@ void setup() { } } - sensor.set_address(lpsDynAddr); - if (lpsDynAddr == 0U) { - Serial.println("Sensor not found failed"); + Serial.println("Sensor not found"); + while (1) {} + } + + if (sensor.set_address(lpsDynAddr) != LPS22DF_OK) { + Serial.println("set_address() failed"); while (1) {} } @@ -45,6 +63,11 @@ void setup() { while (1) {} } + if (!I3C.setClock(12500000)) { + Serial.println("setClock() failed"); + while (1) {} + } + if (sensor.Enable() != LPS22DF_OK) { Serial.println("sensor.Enable() failed"); while (1) {} diff --git a/examples/LPS22DF_I3C_IBI/LPS22DF_I3C_IBI.ino b/examples/LPS22DF_I3C_IBI/LPS22DF_I3C_IBI.ino deleted file mode 100644 index 6003483..0000000 --- a/examples/LPS22DF_I3C_IBI/LPS22DF_I3C_IBI.ino +++ /dev/null @@ -1,46 +0,0 @@ -#include "I3C.h" -#include "LPS22DFSensor.h" - -LPS22DFSensor sensor(&I3C, LPS22DF_I3C_ADD_H, 0x30); -static volatile bool ibiPending = false; - -void setup() { - Serial.begin(115200); - while (!Serial) {} - - I3C.begin(I3C_SDA, I3C_SCL, 1000000U); - I3C.onIbi([](const I3CControllerIbiInfo&, void*) { - ibiPending = true; - }, - nullptr); - - sensor.begin(); - sensor.SetOutputDataRate(10.0f); - sensor.ConfigureDataReadyOnI3cIbi(); - sensor.EnableIbiOnBus(); - sensor.Enable(); - - Serial.println("Waiting for IBI ..."); -} - -void loop() { - if (!ibiPending && !I3C.hasIbi()) { - return; - } - - ibiPending = false; - - I3CControllerIbiInfo ibi{}; - if (!I3C.readIbi(ibi)) { - return; - } - - float p, t; - if (sensor.GetPressure(&p) == LPS22DF_OK && sensor.GetTemperature(&t) == LPS22DF_OK) { - Serial.print(" P = "); - Serial.print(p, 2); - Serial.print(" hPa, T = "); - Serial.print(t, 1); - Serial.println(" C"); - } -} diff --git a/keywords.txt b/keywords.txt index adbc4b8..54b49b0 100644 --- a/keywords.txt +++ b/keywords.txt @@ -27,6 +27,9 @@ Read_Reg KEYWORD2 Write_Reg KEYWORD2 Set_One_Shot KEYWORD2 Get_One_Shot_Status KEYWORD2 +set_address KEYWORD2 +getStaticAddress KEYWORD2 +getDynAddress KEYWORD2 ####################################### # Constants (LITERAL1) @@ -34,3 +37,11 @@ Get_One_Shot_Status KEYWORD2 LPS22DF_OK LITERAL1 LPS22DF_ERROR LITERAL1 +LPS22DF_I2C_BUS LITERAL1 +LPS22DF_SPI_4WIRES_BUS LITERAL1 +LPS22DF_SPI_3WIRES_BUS LITERAL1 +LPS22DF_I3C_BUS LITERAL1 +LPS22DF_I3C_ADD_L LITERAL1 +LPS22DF_I3C_ADD_H LITERAL1 +LPS22DF_I3C_PID_L LITERAL1 +LPS22DF_I3C_PID_H LITERAL1 diff --git a/library.properties b/library.properties index 0e085ff..68d19d2 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=STM32duino LPS22DF -version=1.0.4 +version=1.1.0 author=SRA maintainer=stm32duino sentence=Nano pressure sensor. diff --git a/src/LPS22DFSensor.cpp b/src/LPS22DFSensor.cpp index d2ff1e7..a3f01ac 100644 --- a/src/LPS22DFSensor.cpp +++ b/src/LPS22DFSensor.cpp @@ -2,8 +2,8 @@ ****************************************************************************** * @file LPS22DFSensor.cpp * @author SRA - * @version V1.0.1 - * @date July 2022 + * @version V1.1.0 + * @date July 2026 * @brief Implementation of a LPS22DF pressure sensor. ****************************************************************************** * @attention @@ -92,10 +92,16 @@ LPS22DFSensor::LPS22DFSensor(I3CBus *i3c, uint8_t staticAddr7, uint8_t dynAddr7) bus_type = LPS22DF_I3C_BUS; } -void LPS22DFSensor::set_address(uint8_t dynAddr7) +LPS22DFStatusTypeDef LPS22DFSensor::set_address(uint8_t dynAddr7) { + if (initialized) { return LPS22DF_ERROR; } address = dynAddr7; + i3c_dyn7 = dynAddr7; + return LPS22DF_OK; } + +uint8_t LPS22DFSensor::getStaticAddress() const { return i3c_static7; } +uint8_t LPS22DFSensor::getDynAddress() const { return i3c_dyn7; } #endif /** @@ -115,40 +121,13 @@ LPS22DFStatusTypeDef LPS22DFSensor::begin() #if defined(I3C_SUPPORTED) if (dev_i3c != nullptr) { - Serial.println("I3C"); - - bool doAutoSetdasa = (i3c_static7 != 0) && (i3c_dyn7 != 0); - - if (doAutoSetdasa) { - dev_i3c->setClock(1000000); // 1 MHz mixte I3C+I2C - - if (!dev_i3c->assignDynamicAddress(i3c_static7, i3c_dyn7)) { - Serial.println("I3C: SETDASA failed"); - return LPS22DF_ERROR; - } - - address = i3c_dyn7; - - uint8_t id = 0; - if (ReadID(&id) != LPS22DF_OK || id != LPS22DF_ID) { - Serial.print("I3C: probe failed after SETDASA, id=0x"); - Serial.println(id, HEX); - return LPS22DF_ERROR; - } - - Serial.println("I3C: dynamic address and WHO_AM_I OK"); - } else { - - uint8_t id = 0; - if (ReadID(&id) != LPS22DF_OK || id != LPS22DF_ID) { - Serial.print("I3C: manual mode: WHO_AM_I failed, id=0x"); - Serial.println(id, HEX); - return LPS22DF_ERROR; - } - Serial.println("I3C: manual mode, WHO_AM_I OK at address 0x"); - Serial.println(address, HEX); + if (address < 0x08 || address > 0x77) { + return LPS22DF_ERROR; + } + uint8_t id = 0; + if (ReadID(&id) != LPS22DF_OK || id != LPS22DF_ID) { + return LPS22DF_ERROR; } - dev_i3c->setClock(12500000); } #endif @@ -173,9 +152,6 @@ LPS22DFStatusTypeDef LPS22DFSensor::begin() } bus_mode.filter = lps22df_bus_mode_t::LPS22DF_AUTO; -#if defined(I3C_SUPPORTED) - bus_mode.i3c_ibi_time = lps22df_bus_mode_t::LPS22DF_IBI_1ms; -#endif if (lps22df_bus_mode_set(®_ctx, &bus_mode) != LPS22DF_OK) { return LPS22DF_ERROR; @@ -578,40 +554,3 @@ int32_t LPS22DF_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16 return ((LPS22DFSensor *)handle)->IO_Read(pBuffer, ReadAddr, nBytesToRead); } -#if defined(I3C_SUPPORTED) -LPS22DFStatusTypeDef LPS22DFSensor::ConfigureDataReadyOnI3cIbi() -{ - if (dev_i3c == nullptr) { - return LPS22DF_ERROR; - } - - if (Write_Reg(LPS22DF_IF_CTRL, 0x80) != LPS22DF_OK) { - return LPS22DF_ERROR; - } - - if (Write_Reg(LPS22DF_CTRL_REG4, 0x30) != LPS22DF_OK) { - return LPS22DF_ERROR; - } - - return LPS22DF_OK; -} - -LPS22DFStatusTypeDef LPS22DFSensor::EnableIbiOnBus(uint8_t targetIndex, - uint32_t timeoutMs, - bool withPayload) -{ - if (dev_i3c == nullptr) { - return LPS22DF_ERROR; - } - - if (dev_i3c->enableIbi(targetIndex, address, withPayload, false, timeoutMs) != 0) { - return LPS22DF_ERROR; - } - - if (dev_i3c->enableControllerEvents() != 0) { - return LPS22DF_ERROR; - } - - return LPS22DF_OK; -} -#endif \ No newline at end of file diff --git a/src/LPS22DFSensor.h b/src/LPS22DFSensor.h index f4a650d..3a9b0ed 100644 --- a/src/LPS22DFSensor.h +++ b/src/LPS22DFSensor.h @@ -121,12 +121,12 @@ class LPS22DFSensor { LPS22DFStatusTypeDef Set_One_Shot(); LPS22DFStatusTypeDef Get_One_Shot_Status(uint8_t *Status); - LPS22DFStatusTypeDef ConfigureDataReadyOnI3cIbi(); - LPS22DFStatusTypeDef EnableIbiOnBus(uint8_t targetIndex = 1, - uint32_t timeoutMs = 1000, - bool withPayload = true); + LPS22DFStatusTypeDef set_address(uint8_t dynAddr7); - void set_address(uint8_t dynAddr7); +#if defined(I3C_SUPPORTED) + uint8_t getStaticAddress() const; + uint8_t getDynAddress() const; +#endif /** * @brief Utility function to read data. From d9ae5add7e1d620b44678935b5b36b5698c7f539 Mon Sep 17 00:00:00 2001 From: Davide QUARANTIELLO Date: Mon, 31 Aug 2026 11:24:33 +0200 Subject: [PATCH 7/7] astyle and begin changed --- README.md | 9 ++-- .../LPS22DF_DataLog_Terminal_I2C.ino} | 16 +++++--- .../LPS22DF_DataLog_Terminal_I3C.ino} | 31 +++++++------- .../LPS22DF_DataLog_Terminal_I3C_ENTDAA.ino} | 41 ++++++++++--------- keywords.txt | 1 - src/LPS22DFSensor.cpp | 35 +++++++++------- src/LPS22DFSensor.h | 6 +-- 7 files changed, 76 insertions(+), 63 deletions(-) rename examples/{LPS22DF_DataLog_Terminal/LPS22DF_DataLog_Terminal.ino => LPS22DF_DataLog_Terminal_I2C/LPS22DF_DataLog_Terminal_I2C.ino} (84%) rename examples/{LPS22DF_I3C_Basic/LPS22DF_I3C_Basic.ino => LPS22DF_DataLog_Terminal_I3C/LPS22DF_DataLog_Terminal_I3C.ino} (70%) rename examples/{LPS22DF_I3C_DynAddrAssign/LPS22DF_I3C_DynAddrAssign.ino => LPS22DF_DataLog_Terminal_I3C_ENTDAA/LPS22DF_DataLog_Terminal_I3C_ENTDAA.ino} (71%) diff --git a/README.md b/README.md index 37aad7a..4aaa705 100644 --- a/README.md +++ b/README.md @@ -29,11 +29,11 @@ An instance can be created and enabled when the SPI bus is used following the pr An instance can be created and enabled when the I3C bus is used with SETDASA (static-to-dynamic address assignment): - LPS22DFSensor PressTemp(&I3C, LPS22DF_I3C_ADD_H, 0x30); + LPS22DFSensor PressTemp(&I3C, LPS22DF_I3C_ADD_H); I3C.begin(I3C_SDA, I3C_SCL, 1000000U); I3C.resetDynamicAddresses(); - I3C.assignDynamicAddress(PressTemp.getStaticAddress(), PressTemp.getDynAddress()); - PressTemp.begin(); + I3C.assignDynamicAddress(PressTemp.getStaticAddress(), LPS22DF_DYNAMIC_ADDRESS); + PressTemp.begin(LPS22DF_DYNAMIC_ADDRESS); I3C.setClock(12500000); PressTemp.Enable(); @@ -43,8 +43,7 @@ An instance can be created and enabled when the I3C bus is used with ENTDAA (dyn I3C.begin(I3C_SDA, I3C_SCL, 1000000U); I3C.discover(devices, 8, &found); // find dynAddr by matching LPS22DF_I3C_PID_H in discovered devices - PressTemp.set_address(dynAddr); - PressTemp.begin(); + PressTemp.begin(dynAddr); I3C.setClock(12500000); PressTemp.Enable(); diff --git a/examples/LPS22DF_DataLog_Terminal/LPS22DF_DataLog_Terminal.ino b/examples/LPS22DF_DataLog_Terminal_I2C/LPS22DF_DataLog_Terminal_I2C.ino similarity index 84% rename from examples/LPS22DF_DataLog_Terminal/LPS22DF_DataLog_Terminal.ino rename to examples/LPS22DF_DataLog_Terminal_I2C/LPS22DF_DataLog_Terminal_I2C.ino index e74b837..542d8cf 100644 --- a/examples/LPS22DF_DataLog_Terminal/LPS22DF_DataLog_Terminal.ino +++ b/examples/LPS22DF_DataLog_Terminal_I2C/LPS22DF_DataLog_Terminal_I2C.ino @@ -1,8 +1,8 @@ /* - @file LPS22DF_DataLog_Terminal.ino + @file LPS22DF_DataLog_Terminal_I2C.ino @author Giuseppe Roberti @brief Example to use the LPS22DF 260-1260 hPa absolute digital - output barometer + output barometer with I2C bus ******************************************************************************* Copyright (c) 2022, STMicroelectronics All rights reserved. @@ -15,18 +15,21 @@ #include -LPS22DFSensor sensor (&Wire); +LPS22DFSensor sensor(&Wire); float pressure, temperature; -void setup() { +void setup() +{ pinMode(LED_BUILTIN, OUTPUT); Serial.begin(115200); Wire.begin(); sensor.begin(); sensor.Enable(); + Serial.println("LPS22DF ready"); } -void loop() { +void loop() +{ sensor.GetPressure(&pressure); sensor.GetTemperature(&temperature); @@ -38,7 +41,8 @@ void loop() { blink(LED_BUILTIN); } -inline void blink(int pin) { +inline void blink(int pin) +{ digitalWrite(pin, HIGH); delay(25); digitalWrite(pin, LOW); diff --git a/examples/LPS22DF_I3C_Basic/LPS22DF_I3C_Basic.ino b/examples/LPS22DF_DataLog_Terminal_I3C/LPS22DF_DataLog_Terminal_I3C.ino similarity index 70% rename from examples/LPS22DF_I3C_Basic/LPS22DF_I3C_Basic.ino rename to examples/LPS22DF_DataLog_Terminal_I3C/LPS22DF_DataLog_Terminal_I3C.ino index 206bed6..b66011f 100644 --- a/examples/LPS22DF_I3C_Basic/LPS22DF_I3C_Basic.ino +++ b/examples/LPS22DF_DataLog_Terminal_I3C/LPS22DF_DataLog_Terminal_I3C.ino @@ -1,5 +1,5 @@ /* - @file LPS22DF_I3C_Basic.ino + @file LPS22DF_DataLog_Terminal_I3C.ino @author STMicroelectronics @brief Example to use the LPS22DF pressure sensor with I3C and SETDASA command ******************************************************************************* @@ -15,9 +15,12 @@ */ #include "LPS22DFSensor.h" -LPS22DFSensor sensor(&I3C, LPS22DF_I3C_ADD_H, 0x30); +#define LPS22DF_DYNAMIC_ADDRESS 0x30 -void setup() { +LPS22DFSensor sensor(&I3C, LPS22DF_I3C_ADD_H); + +void setup() +{ Serial.begin(115200); while (!Serial) {} delay(1000); @@ -34,12 +37,12 @@ void setup() { while (1) {} } - if (!I3C.assignDynamicAddress(sensor.getStaticAddress(), sensor.getDynAddress())) { + if (!I3C.assignDynamicAddress(sensor.getStaticAddress(), LPS22DF_DYNAMIC_ADDRESS)) { Serial.println("assignDynamicAddress() failed"); while (1) {} } - if (sensor.begin() != LPS22DF_OK) { + if (sensor.begin(LPS22DF_DYNAMIC_ADDRESS) != LPS22DF_OK) { Serial.println("sensor.begin() failed"); while (1) {} } @@ -57,16 +60,16 @@ void setup() { Serial.println("LPS22DF ready"); } -void loop() { - float p = 0.0f; - float t = 0.0f; +void loop() +{ + float pressure = 0.0f; + float temperature = 0.0f; - if (sensor.GetPressure(&p) == LPS22DF_OK && sensor.GetTemperature(&t) == LPS22DF_OK) { - Serial.print("P = "); - Serial.print(p, 2); - Serial.print(" hPa T = "); - Serial.print(t, 1); - Serial.println(" C"); + if (sensor.GetPressure(&pressure) == LPS22DF_OK && sensor.GetTemperature(&temperature) == LPS22DF_OK) { + Serial.print("Pressure[hPa]:"); + Serial.print(pressure, 2); + Serial.print(", Temperature[C]:"); + Serial.println(temperature, 2); } else { Serial.println("Read failed"); } diff --git a/examples/LPS22DF_I3C_DynAddrAssign/LPS22DF_I3C_DynAddrAssign.ino b/examples/LPS22DF_DataLog_Terminal_I3C_ENTDAA/LPS22DF_DataLog_Terminal_I3C_ENTDAA.ino similarity index 71% rename from examples/LPS22DF_I3C_DynAddrAssign/LPS22DF_I3C_DynAddrAssign.ino rename to examples/LPS22DF_DataLog_Terminal_I3C_ENTDAA/LPS22DF_DataLog_Terminal_I3C_ENTDAA.ino index 2d7beda..afa26a5 100644 --- a/examples/LPS22DF_I3C_DynAddrAssign/LPS22DF_I3C_DynAddrAssign.ino +++ b/examples/LPS22DF_DataLog_Terminal_I3C_ENTDAA/LPS22DF_DataLog_Terminal_I3C_ENTDAA.ino @@ -1,5 +1,5 @@ /* - @file LPS22DF_I3C_DynAddrAssign.ino + @file LPS22DF_DataLog_Terminal_I3C_ENTDAA.ino @author STMicroelectronics @brief Example to use the LPS22DF pressure sensor with I3C dynamic address assignment ******************************************************************************* @@ -18,7 +18,8 @@ LPS22DFSensor sensor(&I3C); -void setup() { +void setup() +{ Serial.begin(115200); while (!Serial) {} delay(1000); @@ -30,7 +31,12 @@ void setup() { while (1) {} } - I3CDiscoveredDevice devices[8]{}; + if (!I3C.resetDynamicAddresses()) { + Serial.println("resetDynamicAddresses() failed"); + while (1) {} + } + + I3CDiscoveredDevice devices[8] {}; size_t found = 0; if (I3C.discover(devices, 8, &found)) { @@ -41,9 +47,11 @@ void setup() { uint8_t lpsDynAddr = 0U; for (size_t i = 0; i < found; ++i) { - Serial.println(devices[i].pid,HEX); + Serial.println(devices[i].pid, HEX); if (devices[i].pid == LPS22DF_I3C_PID_H) { lpsDynAddr = devices[i].dynAddr; + Serial.print("lpsDynAddr="); + Serial.println(lpsDynAddr, HEX); break; } } @@ -53,12 +61,7 @@ void setup() { while (1) {} } - if (sensor.set_address(lpsDynAddr) != LPS22DF_OK) { - Serial.println("set_address() failed"); - while (1) {} - } - - if (sensor.begin() != LPS22DF_OK) { + if (sensor.begin(lpsDynAddr) != LPS22DF_OK) { Serial.println("sensor.begin() failed"); while (1) {} } @@ -76,16 +79,16 @@ void setup() { Serial.println("LPS22DF ready"); } -void loop() { - float p = 0.0f; - float t = 0.0f; +void loop() +{ + float pressure = 0.0f; + float temperature = 0.0f; - if (sensor.GetPressure(&p) == LPS22DF_OK && sensor.GetTemperature(&t) == LPS22DF_OK) { - Serial.print("P = "); - Serial.print(p, 2); - Serial.print(" hPa T = "); - Serial.print(t, 1); - Serial.println(" C"); + if (sensor.GetPressure(&pressure) == LPS22DF_OK && sensor.GetTemperature(&temperature) == LPS22DF_OK) { + Serial.print("Pressure[hPa]:"); + Serial.print(pressure, 2); + Serial.print(", Temperature[C]:"); + Serial.println(temperature, 2); } else { Serial.println("Read failed"); } diff --git a/keywords.txt b/keywords.txt index 54b49b0..820e690 100644 --- a/keywords.txt +++ b/keywords.txt @@ -27,7 +27,6 @@ Read_Reg KEYWORD2 Write_Reg KEYWORD2 Set_One_Shot KEYWORD2 Get_One_Shot_Status KEYWORD2 -set_address KEYWORD2 getStaticAddress KEYWORD2 getDynAddress KEYWORD2 diff --git a/src/LPS22DFSensor.cpp b/src/LPS22DFSensor.cpp index a3f01ac..5b07503 100644 --- a/src/LPS22DFSensor.cpp +++ b/src/LPS22DFSensor.cpp @@ -53,6 +53,9 @@ LPS22DFSensor::LPS22DFSensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), addr reg_ctx.read_reg = LPS22DF_io_read; reg_ctx.handle = (void *)this; dev_spi = NULL; +#if defined(I3C_SUPPORTED) + dev_i3c = NULL; +#endif enabled = 0L; } @@ -67,12 +70,15 @@ LPS22DFSensor::LPS22DFSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : de reg_ctx.read_reg = LPS22DF_io_read; reg_ctx.handle = (void *)this; dev_i2c = NULL; +#if defined(I3C_SUPPORTED) + dev_i3c = NULL; +#endif address = 0L; enabled = 0L; } #if defined(I3C_SUPPORTED) -LPS22DFSensor::LPS22DFSensor(I3CBus *i3c, uint8_t staticAddr7, uint8_t dynAddr7) +LPS22DFSensor::LPS22DFSensor(I3CBus *i3c, uint8_t static_addr7) { reg_ctx.write_reg = LPS22DF_io_write; reg_ctx.read_reg = LPS22DF_io_read; @@ -82,9 +88,9 @@ LPS22DFSensor::LPS22DFSensor(I3CBus *i3c, uint8_t staticAddr7, uint8_t dynAddr7) dev_spi = NULL; dev_i3c = i3c; - address = (dynAddr7 != 0) ? dynAddr7 : staticAddr7; - i3c_static7 = staticAddr7; - i3c_dyn7 = dynAddr7; + address = static_addr7; + i3c_static7 = static_addr7; + i3c_dyn7 = 0; enabled = 0U; initialized = 0U; @@ -92,23 +98,21 @@ LPS22DFSensor::LPS22DFSensor(I3CBus *i3c, uint8_t staticAddr7, uint8_t dynAddr7) bus_type = LPS22DF_I3C_BUS; } -LPS22DFStatusTypeDef LPS22DFSensor::set_address(uint8_t dynAddr7) +uint8_t LPS22DFSensor::getStaticAddress() const { - if (initialized) { return LPS22DF_ERROR; } - address = dynAddr7; - i3c_dyn7 = dynAddr7; - return LPS22DF_OK; + return i3c_static7; +} +uint8_t LPS22DFSensor::getDynAddress() const +{ + return i3c_dyn7; } - -uint8_t LPS22DFSensor::getStaticAddress() const { return i3c_static7; } -uint8_t LPS22DFSensor::getDynAddress() const { return i3c_dyn7; } #endif /** * @brief Configure the sensor in order to be used * @retval 0 in case of success, an error code otherwise */ -LPS22DFStatusTypeDef LPS22DFSensor::begin() +LPS22DFStatusTypeDef LPS22DFSensor::begin(uint8_t new_address) { lps22df_md_t md; lps22df_bus_mode_t bus_mode; @@ -121,8 +125,11 @@ LPS22DFStatusTypeDef LPS22DFSensor::begin() #if defined(I3C_SUPPORTED) if (dev_i3c != nullptr) { - if (address < 0x08 || address > 0x77) { + if (new_address < 0x08 || new_address > 0x77) { return LPS22DF_ERROR; + } else { + address = new_address; + i3c_dyn7 = new_address; } uint8_t id = 0; if (ReadID(&id) != LPS22DF_OK || id != LPS22DF_ID) { diff --git a/src/LPS22DFSensor.h b/src/LPS22DFSensor.h index 3a9b0ed..cbd31b6 100644 --- a/src/LPS22DFSensor.h +++ b/src/LPS22DFSensor.h @@ -98,10 +98,10 @@ class LPS22DFSensor { LPS22DFSensor(TwoWire *i2c, uint8_t address = LPS22DF_I2C_ADD_H); LPS22DFSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed = 2000000); #if defined(I3C_SUPPORTED) - LPS22DFSensor(I3CBus *i3c, uint8_t staticAddr7 = 0, uint8_t dynAddr7 = 0); + LPS22DFSensor(I3CBus *i3c, uint8_t static_addr7 = 0); #endif - LPS22DFStatusTypeDef begin(); + LPS22DFStatusTypeDef begin(uint8_t new_address = 0); LPS22DFStatusTypeDef end(); LPS22DFStatusTypeDef ReadID(uint8_t *Id); LPS22DFStatusTypeDef Enable(); @@ -121,8 +121,6 @@ class LPS22DFSensor { LPS22DFStatusTypeDef Set_One_Shot(); LPS22DFStatusTypeDef Get_One_Shot_Status(uint8_t *Status); - LPS22DFStatusTypeDef set_address(uint8_t dynAddr7); - #if defined(I3C_SUPPORTED) uint8_t getStaticAddress() const; uint8_t getDynAddress() const;