#!/usr/bin/env python3 """Ensure dtoverlay=spi1-3cs is under [all] for Pi 5 Model B.""" from __future__ import annotations import sys from pathlib import Path CONFIG = Path("/boot/firmware/config.txt") OVERLAY = "dtoverlay=spi1-3cs" def main() -> int: if not CONFIG.exists(): print(f"Error: {CONFIG} not found", file=sys.stderr) return 1 lines = CONFIG.read_text().splitlines() out: list[str] = [] in_cm5 = False has_overlay = False inserted = False for line in lines: stripped = line.strip() if stripped == "[cm5]": in_cm5 = True elif stripped.startswith("[") and stripped.endswith("]"): if in_cm5 and not inserted and not has_overlay: out.append(OVERLAY) inserted = True in_cm5 = False if stripped == "dtparam=spi1=on": continue if stripped == OVERLAY: has_overlay = True if in_cm5: continue out.append(line) continue out.append(line) if not has_overlay and not inserted: if out and out[-1].strip(): out.append("") out.append(OVERLAY) new_text = "\n".join(out) + "\n" if new_text == CONFIG.read_text(): print("config.txt already correct.") else: CONFIG.write_text(new_text) print("Updated /boot/firmware/config.txt:") print(f" - removed dtparam=spi1=on (ignored on Pi 5)") print(f" - moved {OVERLAY} out of [cm5] into [all]") print("Reboot for the change to take effect on boot.") print( "\nWiring:\n" " strip 0 DIN → GPIO 10 (SPI0 MOSI) /dev/spidev0.0\n" " strip 1 DIN → GPIO 20 (SPI1 MOSI) /dev/spidev1.0" ) return 0 if __name__ == "__main__": raise SystemExit(main())