From 1de5f9dedbd7b392fd8aed64e014fe9279915001 Mon Sep 17 00:00:00 2001 From: Aymane Bahssain Date: Fri, 6 Mar 2026 16:27:55 +0100 Subject: [PATCH 1/2] 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..428d8cb 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) != 0) { + 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(HAL_I3C_IT_IBIIE) != 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 282f2b0dc676fd53150fbaa4d8cdc83157919f09 Mon Sep 17 00:00:00 2001 From: Aymane Bahssain Date: Mon, 27 Apr 2026 11:29:08 +0200 Subject: [PATCH 2/2] 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..dc1e671 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 */