This commit is contained in:
david rice
2026-04-20 16:06:01 +01:00
parent 712a42ecb7
commit ac65270cef
6 changed files with 103 additions and 70 deletions

View File

@@ -27,7 +27,7 @@ REGISTER_COMMANDS = [
# ---------------------------------------------------------------------------
# SN65DSI83 I2C configuration
# ---------------------------------------------------------------------------
SN65_I2C_BUS = 2 # i2c-2 on i.MX 8M Mini — change if bridge is on a different bus
SN65_I2C_BUS = 4 # i2c-4 on this board
SN65_I2C_ADDR = 0x2C # SN65DSI83 fixed 7-bit I2C address
# Known Samsung DSIM register names (base 0x32E10000, i.MX 8M Mini)
@@ -124,25 +124,28 @@ def get_registers():
}), 200
def _i2c_read_byte(bus: int, addr: int, reg: int) -> int | None:
"""Read one byte from an I2C device using i2cget. Returns None on failure."""
def _i2c_read_byte(bus: int, addr: int, reg: int) -> tuple[int | None, str]:
"""Read one byte via i2cget. Returns (value, "") on success or (None, error_str) on failure."""
try:
result = subprocess.run(
["i2cget", "-y", str(bus), f"0x{addr:02x}", f"0x{reg:02x}"],
["i2cget", "-y", "-f", str(bus), f"0x{addr:02x}", f"0x{reg:02x}"],
capture_output=True, text=True, timeout=3
)
if result.returncode == 0:
return int(result.stdout.strip(), 16)
except Exception:
pass
return None
return int(result.stdout.strip(), 16), ""
stderr = result.stderr.strip() or f"exit code {result.returncode}"
return None, stderr
except FileNotFoundError:
return None, "i2cget not found in PATH"
except Exception as e:
return None, str(e)
@app.route("/sn65_registers", methods=["GET"])
def get_sn65_registers():
"""Read SN65DSI83 CSR 0x0A (PLL/CLK status) and 0xE5 (error flags) via I2C."""
csr_0a = _i2c_read_byte(SN65_I2C_BUS, SN65_I2C_ADDR, 0x0A)
csr_e5 = _i2c_read_byte(SN65_I2C_BUS, SN65_I2C_ADDR, 0xE5)
csr_0a, err_0a = _i2c_read_byte(SN65_I2C_BUS, SN65_I2C_ADDR, 0x0A)
csr_e5, err_e5 = _i2c_read_byte(SN65_I2C_BUS, SN65_I2C_ADDR, 0xE5)
regs = {}
errors = []
@@ -151,10 +154,10 @@ def get_sn65_registers():
regs["csr_0a"] = {
"value": f"0x{csr_0a:02x}",
"pll_lock": bool(csr_0a & 0x80), # bit 7: PLL_EN_STAT (powered + locked)
"clk_det": bool(csr_0a & 0x40), # bit 6: high-speed CLK lane detected
"clk_det": bool(csr_0a & 0x08), # bit 3: CHA_CLK_DET (HS clock detected)
}
else:
errors.append("CSR 0x0A: i2cget failed")
errors.append(f"CSR 0x0A: {err_0a}")
if csr_e5 is not None:
regs["csr_e5"] = {
@@ -167,7 +170,7 @@ def get_sn65_registers():
"cha_crc_err": bool(csr_e5 & 0x40),
}
else:
errors.append("CSR 0xE5: i2cget failed")
errors.append(f"CSR 0xE5: {err_e5}")
return jsonify({
"i2c_bus": SN65_I2C_BUS,