Skip to content

Commit 916bc49

Browse files
committed
feat(I3C): Add I3C examples
Signed-off-by: Aymane Bahssain <aymane.bahssain@st.com>
1 parent 94e20e8 commit 916bc49

12 files changed

Lines changed: 636 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <I3C.h>
2+
3+
// Choose the bus instance available on your board.
4+
5+
#define I3C_BUS = I3C1Bus;
6+
7+
void setup() {
8+
Serial.begin(115200);
9+
while (!Serial) {}
10+
11+
Serial.println("I3C Basic Begin");
12+
13+
// Option 1: use default board I3C pins
14+
if (!I3C_BUS.begin(1000000)) {
15+
Serial.println("Failed to initialize I3C bus");
16+
while (1) {}
17+
}
18+
19+
Serial.println("I3C bus initialized at 1 MHz");
20+
}
21+
22+
void loop() {
23+
delay(1000);
24+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "I3C.h"
2+
3+
#define I3C_BUS I3C1Bus
4+
static I3CDiscoveredDevice devices[8];
5+
6+
void printHex2(uint8_t v) {
7+
if (v < 0x10) Serial.print('0');
8+
Serial.print(v, HEX);
9+
}
10+
11+
void setup() {
12+
Serial.begin(115200);
13+
while (!Serial) {}
14+
delay(1000);
15+
16+
Serial.println("=== Controller Write / Target Receive ===");
17+
18+
if (!I3C_BUS.begin(I3C_SDA, I3C_SCL, 1000000U)) {
19+
Serial.println("begin() failed");
20+
while (1) {}
21+
}
22+
23+
size_t found = 0;
24+
int rc = I3C_BUS.discover(devices, 8, &found);
25+
26+
if (rc != 0 || found == 0) {
27+
Serial.println("No target found");
28+
while (1) {}
29+
}
30+
31+
uint8_t da = devices[0].dynAddr;
32+
33+
Serial.print("Target DA=0x");
34+
printHex2(da);
35+
Serial.println();
36+
37+
delay(1000);
38+
39+
uint8_t tx[16] = {
40+
0x01, 0x02, 0x03, 0x04,
41+
0x05, 0x06, 0x07, 0x08,
42+
0x09, 0x0A, 0x0B, 0x0C,
43+
0x0D, 0x0E, 0x0F, 0x10
44+
};
45+
46+
int wr = I3C_BUS.write(da, tx, sizeof(tx), 1000);
47+
Serial.print("write rc = ");
48+
Serial.println(wr);
49+
}
50+
51+
void loop() {
52+
delay(1000);
53+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "I3C.h"
2+
3+
#define I3C_BUS I3C1Bus
4+
static uint8_t rxBuf[16];
5+
6+
void printHex2(uint8_t v) {
7+
if (v < 0x10) Serial.print('0');
8+
Serial.print(v, HEX);
9+
}
10+
11+
void setup() {
12+
Serial.begin(115200);
13+
while (!Serial) {}
14+
delay(1000);
15+
16+
Serial.println("=== Target Receive ===");
17+
18+
I3CTargetConfig cfg{};
19+
cfg.identifier = 0x62;
20+
cfg.mipiIdentifier = 0x1;
21+
22+
if (!I3C_BUS.beginTarget(I3C_SDA, I3C_SCL, cfg)) {
23+
Serial.println("beginTarget failed");
24+
while (1) {}
25+
}
26+
27+
while (!I3C_BUS.hasAddress()) {
28+
delay(10);
29+
}
30+
31+
Serial.print("DA = 0x");
32+
Serial.println(I3C_BUS.address(), HEX);
33+
34+
HAL_I3C_FlushAllFifo(I3C_BUS.halHandle());
35+
36+
int rc = I3C_BUS.receive(rxBuf, sizeof(rxBuf), 10000);
37+
Serial.print("receive rc = ");
38+
Serial.println(rc);
39+
40+
if (rc == 0) {
41+
Serial.print("RX: ");
42+
for (size_t i = 0; i < sizeof(rxBuf); ++i) {
43+
printHex2(rxBuf[i]);
44+
Serial.print(' ');
45+
}
46+
Serial.println();
47+
}
48+
}
49+
50+
void loop() {
51+
delay(1000);
52+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <I3C.h>
2+
3+
#define I3C_BUS = I3C1Bus;
4+
5+
6+
void setup() {
7+
Serial.begin(115200);
8+
while (!Serial) {}
9+
10+
Serial.println("I3C Device Ready Example");
11+
12+
if (!I3C_BUS.begin(1000000)) {
13+
Serial.println("Failed to initialize I3C bus");
14+
while (1) {}
15+
}
16+
17+
for (uint8_t addr = 1; addr < 0x7F; ++addr) {
18+
19+
bool i3cReady = I3C_BUS.isI3CDeviceReady(addr);
20+
if (i3cReady) {
21+
Serial.print("I3C device at DA 0x");
22+
Serial.print(addr, HEX);
23+
Serial.print(": ");
24+
Serial.println("READY");
25+
}
26+
}
27+
}
28+
29+
void loop() {
30+
delay(1000);
31+
}
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+
}

0 commit comments

Comments
 (0)