Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# v3.21 implemented semantic changes regarding $<TARGET_OBJECTS:...>
# 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
$<TARGET_OBJECTS:LPS22DF_bin>
)

55 changes: 55 additions & 0 deletions examples/LPS22DF_I3C_Basic/LPS22DF_I3C_Basic.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "I3C.h"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename all the I3C examples "LPS22DF_I3C_Basic.ino", "LPS22DF_I3C_DynAddrAssign.ino" and "LPS22DF_I3C_IBI.ino" putting them in the root of the "examples" folder and avoiding the inner folders. What about this proposal?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

#include "LPS22DFSensor.h"

#define I3C_BUS I3C1Bus

static const uint8_t LPS22DF_DYN_ADDR = 0x30;

LPS22DFSensor sensor(&I3C_BUS, LPS22DF_I3C_ADD_H, LPS22DF_DYN_ADDR);

void setup() {
Serial.begin(115200);
while (!Serial) {}
delay(1000);

Serial.println("=== LPS22DF SETDASA ===");

if (!I3C_BUS.begin(I3C_SDA, I3C_SCL, 1000000U)) {
Serial.println("begin() failed");
while (1) {}
}

if (!I3C_BUS.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);
}
73 changes: 73 additions & 0 deletions examples/LPS22DF_I3C_DynAddrAssign/LPS22DF_I3C_DynAddrAssign.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "I3C.h"
#include "LPS22DFSensor.h"

#define I3C_BUS I3C1Bus

LPS22DFSensor sensor(&I3C_BUS, 0x00);

void setup() {
Serial.begin(115200);
while (!Serial) {}
delay(1000);

Serial.println("=== LPS22DF DAA ===");

if (!I3C_BUS.begin(I3C_SDA, I3C_SCL, 1000000U)) {
Serial.println("begin() failed");
while (1) {}
}

I3CDiscoveredDevice devices[8]{};
size_t found = 0;

if (I3C_BUS.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);
}
50 changes: 50 additions & 0 deletions examples/LPS22DF_I3C_IBI/LPS22DF_I3C_IBI.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "I3C.h"
#include "LPS22DFSensor.h"

#define I3C_BUS I3C1Bus

LPS22DFSensor sensor(&I3C_BUS, LPS22DF_I3C_ADD_H, 0x30);
static volatile bool ibiPending = false;



void setup() {
Serial.begin(115200);
while (!Serial) {}

I3C_BUS.begin(I3C_SDA, I3C_SCL, 1000000U);
I3C_BUS.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_BUS.hasIbi()) {
return;
}

ibiPending = false;

I3CControllerIbiInfo ibi{};
if (!I3C_BUS.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");
}
}
1 change: 1 addition & 0 deletions extras/codespell-ignore-words-list.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
daa
ths
107 changes: 105 additions & 2 deletions src/LPS22DFSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(&reg_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(&reg_ctx, &bus_mode) != LPS22DF_OK) {
return LPS22DF_ERROR;
}
Expand Down Expand Up @@ -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;
}
Loading
Loading