Skip to content

Commit 030c922

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

9 files changed

Lines changed: 458 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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include "I3C.h"
2+
3+
static I3CDiscoveredDevice devices[8];
4+
5+
void printHex2(uint8_t v) {
6+
if (v < 0x10) Serial.print('0');
7+
Serial.print(v, HEX);
8+
}
9+
10+
void printHex64(uint64_t v) {
11+
uint32_t hi = (uint32_t)(v >> 32);
12+
uint32_t lo = (uint32_t)(v & 0xFFFFFFFFULL);
13+
Serial.print(hi, HEX);
14+
Serial.print(lo, HEX);
15+
}
16+
17+
void setup() {
18+
Serial.begin(115200);
19+
while (!Serial) {}
20+
delay(1000);
21+
22+
Serial.println("=== I3C ScanBus ===");
23+
24+
if (!I3C1Bus.begin(I3C_SDA, I3C_SCL, 1000000U)) {
25+
Serial.println("begin() failed");
26+
while (1) {}
27+
}
28+
29+
size_t found = 0;
30+
31+
int rc = I3C1Bus.discover(devices, 8, &found);
32+
33+
Serial.print("discover rc = ");
34+
Serial.println(rc);
35+
36+
Serial.print("found = ");
37+
Serial.println(found);
38+
39+
for (size_t i = 0; i < found; ++i) {
40+
Serial.print("#");
41+
Serial.print(i);
42+
Serial.print(" DA=0x");
43+
printHex2(devices[i].dynAddr);
44+
45+
Serial.print(" SA=0x");
46+
printHex2(devices[i].staticAddr);
47+
48+
Serial.print(" BCR=0x");
49+
printHex2(devices[i].bcr);
50+
51+
Serial.print(" DCR=0x");
52+
printHex2(devices[i].dcr);
53+
54+
Serial.print(" PID=0x");
55+
printHex64(devices[i].pid);
56+
57+
Serial.print(" RAW=0x");
58+
printHex64(devices[i].entdaaPayloadRaw);
59+
60+
Serial.print(" fromDaa=");
61+
Serial.print(devices[i].fromDaa ? "yes" : "no");
62+
63+
Serial.print(" setdasa=");
64+
Serial.print(devices[i].assignedBySetdasa ? "yes" : "no");
65+
66+
Serial.print(" matchedKnown=");
67+
Serial.println(devices[i].matchedKnown ? "yes" : "no");
68+
}
69+
}
70+
71+
void loop() {
72+
delay(1000);
73+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include "I3C.h"
2+
3+
#define I3C_BUS I3C1Bus
4+
static I3CDiscoveredDevice devices[8];
5+
static volatile bool callbackSeen = false;
6+
7+
void myIbiCallback(const I3CControllerIbiInfo &info, void *user) {
8+
(void)info;
9+
if (user != nullptr) {
10+
volatile bool *flag = reinterpret_cast<volatile bool *>(user);
11+
*flag = true;
12+
}
13+
}
14+
15+
void printHex2(uint8_t v) {
16+
if (v < 0x10) Serial.print('0');
17+
Serial.print(v, HEX);
18+
}
19+
20+
void setup() {
21+
Serial.begin(115200);
22+
while (!Serial) {}
23+
delay(1000);
24+
25+
Serial.println("=== Controller IBI Example ===");
26+
27+
if (!I3C_BUS.begin(I3C_SDA, I3C_SCL, 1000000U)) {
28+
Serial.println("begin() failed");
29+
while (1) {}
30+
}
31+
32+
size_t found = 0;
33+
int rc = I3C_BUS.discover(devices, 8, &found);
34+
35+
if (rc != 0 || found == 0) {
36+
Serial.println("No target found");
37+
while (1) {}
38+
}
39+
40+
uint8_t da = devices[0].dynAddr;
41+
42+
I3C_BUS.onIbi(myIbiCallback, (void *)&callbackSeen);
43+
44+
rc = I3C_BUS.enableIbi(1, da, false, false, 1000);
45+
Serial.print("enableIbi rc = ");
46+
Serial.println(rc);
47+
48+
rc = I3C_BUS.enableControllerEvents();
49+
Serial.print("enableControllerEvents rc = ");
50+
Serial.println(rc);
51+
52+
Serial.println("Waiting for IBI...");
53+
}
54+
55+
void loop() {
56+
if (I3C_BUS.hasIbi()) {
57+
I3CControllerIbiInfo ibi{};
58+
if (I3C_BUS.peekIbi(ibi)) {
59+
Serial.print("peekIbi source DA = 0x");
60+
printHex2(ibi.sourceDa);
61+
Serial.println();
62+
}
63+
64+
if (I3C_BUS.readIbi(ibi)) {
65+
Serial.print("readIbi source DA = 0x");
66+
printHex2(ibi.sourceDa);
67+
Serial.println();
68+
}
69+
70+
Serial.print("callbackSeen = ");
71+
Serial.println(callbackSeen ? "true" : "false");
72+
}
73+
74+
delay(100);
75+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "I3C.h"
2+
3+
#define I3C_BUS I3C1Bus
4+
5+
void setup() {
6+
Serial.begin(115200);
7+
while (!Serial) {}
8+
delay(1000);
9+
10+
Serial.println("=== Target IBI Example ===");
11+
12+
I3CTargetConfig cfg{};
13+
cfg.identifier = 0x62;
14+
cfg.mipiIdentifier = 0x1;
15+
cfg.ibiRequest = true;
16+
cfg.ibiPayload = false;
17+
18+
if (!I3C_BUS.beginTarget(I3C_SDA, I3C_SCL, cfg)) {
19+
Serial.println("beginTarget failed");
20+
while (1) {}
21+
}
22+
23+
while (!I3C_BUS.hasAddress()) {
24+
delay(10);
25+
}
26+
27+
Serial.print("DA = 0x");
28+
Serial.println(I3C_BUS.address(), HEX);
29+
30+
delay(2000);
31+
32+
int rc = I3C_BUS.sendIbi(nullptr, 0, 1000);
33+
Serial.print("sendIbi rc = ");
34+
Serial.println(rc);
35+
}
36+
37+
void loop() {
38+
delay(1000);
39+
}

0 commit comments

Comments
 (0)