Skip to content

Commit acc4d91

Browse files
Aymane-STfpistm
authored andcommitted
feat(I3C): add I3C examples
Signed-off-by: Aymane Bahssain <aymane.bahssain@st.com>
1 parent 8593493 commit acc4d91

4 files changed

Lines changed: 179 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "I3C.h"
2+
#include "LPS22DFSensor.h"
3+
4+
#define I3C_BUS I3C1Bus
5+
6+
static const uint8_t LPS22DF_DYN_ADDR = 0x30;
7+
8+
LPS22DFSensor sensor(&I3C_BUS, LPS22DF_I3C_ADD_H, LPS22DF_DYN_ADDR);
9+
10+
void setup() {
11+
Serial.begin(115200);
12+
while (!Serial) {}
13+
delay(1000);
14+
15+
Serial.println("=== LPS22DF SETDASA ===");
16+
17+
if (!I3C_BUS.begin(I3C_SDA, I3C_SCL, 1000000U)) {
18+
Serial.println("begin() failed");
19+
while (1) {}
20+
}
21+
22+
if (!I3C_BUS.resetDynamicAddresses()) {
23+
Serial.println("resetDynamicAddresses() failed");
24+
while (1) {}
25+
}
26+
27+
if (sensor.begin() != LPS22DF_OK) {
28+
Serial.println("sensor.begin() failed");
29+
while (1) {}
30+
}
31+
32+
if (sensor.Enable() != LPS22DF_OK) {
33+
Serial.println("sensor.Enable() failed");
34+
while (1) {}
35+
}
36+
37+
Serial.println("LPS22DF ready");
38+
}
39+
40+
void loop() {
41+
float p = 0.0f;
42+
float t = 0.0f;
43+
44+
if (sensor.GetPressure(&p) == LPS22DF_OK && sensor.GetTemperature(&t) == LPS22DF_OK) {
45+
Serial.print("P = ");
46+
Serial.print(p, 2);
47+
Serial.print(" hPa T = ");
48+
Serial.print(t, 1);
49+
Serial.println(" C");
50+
} else {
51+
Serial.println("Read failed");
52+
}
53+
54+
delay(500);
55+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include "I3C.h"
2+
#include "LPS22DFSensor.h"
3+
4+
#define I3C_BUS I3C1Bus
5+
6+
LPS22DFSensor sensor(&I3C_BUS, 0x00);
7+
8+
void setup() {
9+
Serial.begin(115200);
10+
while (!Serial) {}
11+
delay(1000);
12+
13+
Serial.println("=== LPS22DF DAA ===");
14+
15+
if (!I3C_BUS.begin(I3C_SDA, I3C_SCL, 1000000U)) {
16+
Serial.println("begin() failed");
17+
while (1) {}
18+
}
19+
20+
I3CDiscoveredDevice devices[8]{};
21+
size_t found = 0;
22+
23+
if (I3C_BUS.discover(devices, 8, &found)) {
24+
Serial.println("discover() failed");
25+
while (1) {}
26+
}
27+
28+
uint8_t lpsDynAddr = 0U;
29+
30+
for (size_t i = 0; i < found; ++i) {
31+
Serial.println(devices[i].pid,HEX);
32+
if (devices[i].pid == LPS22DF_I3C_PID_H) {
33+
lpsDynAddr = devices[i].dynAddr;
34+
break;
35+
}
36+
}
37+
38+
sensor.set_address(lpsDynAddr);
39+
40+
if (lpsDynAddr == 0U) {
41+
Serial.println("Sensor not found failed");
42+
while (1) {}
43+
}
44+
45+
if (sensor.begin() != LPS22DF_OK) {
46+
Serial.println("sensor.begin() failed");
47+
while (1) {}
48+
}
49+
50+
if (sensor.Enable() != LPS22DF_OK) {
51+
Serial.println("sensor.Enable() failed");
52+
while (1) {}
53+
}
54+
55+
Serial.println("LPS22DF ready");
56+
}
57+
58+
void loop() {
59+
float p = 0.0f;
60+
float t = 0.0f;
61+
62+
if (sensor.GetPressure(&p) == LPS22DF_OK && sensor.GetTemperature(&t) == LPS22DF_OK) {
63+
Serial.print("P = ");
64+
Serial.print(p, 2);
65+
Serial.print(" hPa T = ");
66+
Serial.print(t, 1);
67+
Serial.println(" C");
68+
} else {
69+
Serial.println("Read failed");
70+
}
71+
72+
delay(500);
73+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "I3C.h"
2+
#include "LPS22DFSensor.h"
3+
4+
#define I3C_BUS I3C1Bus
5+
6+
LPS22DFSensor sensor(&I3C_BUS, LPS22DF_I3C_ADD_H, 0x30);
7+
static volatile bool ibiPending = false;
8+
9+
10+
11+
void setup() {
12+
Serial.begin(115200);
13+
while (!Serial) {}
14+
15+
I3C_BUS.begin(I3C_SDA, I3C_SCL, 1000000U);
16+
I3C_BUS.onIbi([](const I3CControllerIbiInfo&, void*) {
17+
ibiPending = true;
18+
},
19+
nullptr);
20+
21+
sensor.begin();
22+
sensor.SetOutputDataRate(10.0f);
23+
sensor.ConfigureDataReadyOnI3cIbi();
24+
sensor.EnableIbiOnBus();
25+
sensor.Enable();
26+
27+
Serial.println("Waiting for IBI ...");
28+
}
29+
30+
void loop() {
31+
if (!ibiPending && !I3C_BUS.hasIbi()) {
32+
return;
33+
}
34+
35+
ibiPending = false;
36+
37+
I3CControllerIbiInfo ibi{};
38+
if (!I3C_BUS.readIbi(ibi)) {
39+
return;
40+
}
41+
42+
float p, t;
43+
if (sensor.GetPressure(&p) == LPS22DF_OK && sensor.GetTemperature(&t) == LPS22DF_OK) {
44+
Serial.print(" P = ");
45+
Serial.print(p, 2);
46+
Serial.print(" hPa, T = ");
47+
Serial.print(t, 1);
48+
Serial.println(" C");
49+
}
50+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
daa
12
ths

0 commit comments

Comments
 (0)