|
| 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 | +} |
0 commit comments