|
| 1 | +#include "lps22df.h" |
| 2 | + |
| 3 | +// limit the discovery |
| 4 | +static const size_t MAX_I3C_DEVICES = 4; |
| 5 | + |
| 6 | +// Low-level callbacks for C driver |
| 7 | +static int32_t lps_i3c_write(void *handle, uint8_t reg, uint8_t *buf, uint16_t len) |
| 8 | +{ |
| 9 | + auto *ctx = static_cast<Lps22dfI3cCtx*>(handle); |
| 10 | + int rc = ctx->bus->writeRegisterBlock(ctx->dynAddr, reg, buf, len, 1000); |
| 11 | + return (rc == 0) ? 0 : -1; |
| 12 | +} |
| 13 | + |
| 14 | +static int32_t lps_i3c_read(void *handle, uint8_t reg, uint8_t *buf, uint16_t len) |
| 15 | +{ |
| 16 | + auto *ctx = static_cast<Lps22dfI3cCtx*>(handle); |
| 17 | + int rc = ctx->bus->readRegisterBlock(ctx->dynAddr, reg, buf, len, 1000); |
| 18 | + return (rc == 0) ? 0 : -1; |
| 19 | +} |
| 20 | + |
| 21 | +bool LPS22DF_I3C_EntdaaInit(I3CBus &bus, |
| 22 | + uint8_t firstDynAddr, |
| 23 | + uint8_t &outDynAddr, |
| 24 | + lps22df_ctx_t &outDrvCtx, |
| 25 | + Lps22dfI3cCtx &outCtx) |
| 26 | +{ |
| 27 | + outDynAddr = 0; |
| 28 | + |
| 29 | + // 1) Optional: global RSTDAA |
| 30 | + bus.rstdaaOnce(); |
| 31 | + |
| 32 | + for (uint8_t addr = 1; addr < 0x7F; ++addr) { |
| 33 | + bus.isI2CDeviceReady(addr, 2, 10); |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + // 2) ENTDAA: discovery of I3C devices on the bus |
| 38 | + I3CDiscoveredDevice devices[MAX_I3C_DEVICES]; |
| 39 | + int n = bus.entdaa(devices, MAX_I3C_DEVICES, firstDynAddr, 1000); |
| 40 | + if (n < 0) { |
| 41 | + Serial.print("ENTDAA FAILED, rc="); |
| 42 | + Serial.println(n); |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + Serial.print("ENTDAA found "); |
| 47 | + Serial.print(n); |
| 48 | + Serial.println(" I3C device(s)"); |
| 49 | + |
| 50 | + // 3) Identify the LPS22DF via WHO_AM_I |
| 51 | + for (int i = 0; i < n; ++i) { |
| 52 | + uint8_t id = 0; |
| 53 | + int rc = bus.readRegister(devices[i].dynAddr, LPS22DF_WHO_AM_I, id, 1000); |
| 54 | + Serial.print("WHO_AM_I @DA 0x"); Serial.print(devices[i].dynAddr, HEX); |
| 55 | + Serial.print(" rc="); Serial.print(rc); |
| 56 | + Serial.print(" id=0x"); Serial.println(id, HEX); |
| 57 | + |
| 58 | + if (rc == 0 && id == LPS22DF_ID) { |
| 59 | + outDynAddr = devices[i].dynAddr; |
| 60 | + Serial.print("LPS22DF detected at DA=0x"); |
| 61 | + Serial.println(outDynAddr, HEX); |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if (outDynAddr == 0) { |
| 67 | + Serial.println("No LPS22DF detected via ENTDAA"); |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + // 4) Prepare contexts for the C driver |
| 72 | + outCtx.bus = &bus; |
| 73 | + outCtx.dynAddr = outDynAddr; |
| 74 | + |
| 75 | + outDrvCtx.write_reg = lps_i3c_write; |
| 76 | + outDrvCtx.read_reg = lps_i3c_read; |
| 77 | + outDrvCtx.handle = &outCtx; |
| 78 | + |
| 79 | + // 5) Recommended init (BDU + IF_INC etc.) |
| 80 | + if (lps22df_init_set(&outDrvCtx, LPS22DF_DRV_RDY) != LPS22DF_OK) { |
| 81 | + Serial.println("lps22df_init_set FAILED"); |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + // 6) Configure mode (ODR, AVG, LPF) |
| 86 | + lps22df_md_t md; |
| 87 | + md.odr = lps22df_md_t::LPS22DF_25Hz; |
| 88 | + md.avg = lps22df_md_t::LPS22DF_4_AVG; |
| 89 | + md.lpf = lps22df_md_t::LPS22DF_LPF_ODR_DIV_4; |
| 90 | + |
| 91 | + if (lps22df_mode_set(&outDrvCtx, &md) != LPS22DF_OK) { |
| 92 | + Serial.println("lps22df_mode_set FAILED"); |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + Serial.println("LPS22DF configured via ENTDAA path."); |
| 97 | + return true; |
| 98 | +} |
0 commit comments