Skip to content

Commit

Permalink
core: blueos_startup_update: load pi5 overlays in runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Williangalvani committed May 31, 2024
1 parent 592e8cb commit 060e44a
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions core/tools/blueos_startup_update/blueos_startup_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,78 @@ def ensure_user_data_structure_is_in_place() -> bool:
return False


def load_pi5_overlays_in_runtime() -> bool:
loaded_overlays = run_command("dtoverlay -l", check=False).stdout.split("\n")
loaded_overlays = [overlay.split(": ")[1].strip() for overlay in loaded_overlays if ": " in overlay]
loaded_overlay_names = [overlay.split(" ")[0] for overlay in loaded_overlays if "dtparam" not in overlay]
loaded_dt_params = [overlay.split(" ")[-1] for overlay in loaded_overlays if "dtparam" in overlay]

logger.info("Loaded overlays: ")
for overlay in loaded_overlays:
logger.info(overlay)

logger.info("Loaded dt params: ")
for dt_param in loaded_dt_params:
logger.info(dt_param)

dtparams_to_load = [
"i2c_arm=on",
"i2c_arm_baudrate=1000000",
]

overlays_to_load = [
# serial ports, checked individually
"uart0-pi5", # serial1
"uart3-pi5", # serial4
"uart4-pi5", # serial5
"uart2-pi5", # serial3
# i2c-6: bar30 and friends
"i2c-gpio i2c_gpio_sda=22 i2c_gpio_scl=23 bus=6 i2c_gpio_delay_us=0",
# i2c1: ADS1115, AK09915, BME280
"i2c1-pi5 baudrate=400000",
# i2c3: PCA
"i2c3-pi5 baudrate=400000",
# SPI1: MMC5983
"spi1-3cs",
# SPI0: LED
"spi0-led",
]

for dtparam in dtparams_to_load:
if dtparam in loaded_dt_params:
logger.info(f"dtparam {dtparam} is already loaded")
else:
logger.info(f"dtparam {dtparam} is not loaded, loading")
command_result = run_command(f"sudo dtparam {dtparam}", False)
logger.info(command_result)

logger.info(run_command("sudo modprobe i2c-dev", False))

for overlay in overlays_to_load:
overlay_name = overlay.split(" ", maxsplit=1)[0]
if overlay in loaded_overlays:
logger.info(f"Overlay {overlay} is already loaded")
elif overlay_name in loaded_overlay_names:
logger.info(f"Overlay {overlay} is already loaded with different parameters")
command_result = run_command(f"sudo dtoverlay -r {overlay_name} && sudo dtoverlay {overlay}", False)
logger.info(command_result)
else:
logger.info(f"Overlay not present, loading overlay {overlay}")
command_result = run_command(f"sudo dtoverlay {overlay}", False)
logger.info(command_result)
src = "/dev/i2c-3"
dst = "/dev/i2c-4"

if not os.path.islink(dst):
try:
os.symlink(src, dst)
except OSError as e:
logger.error(f"Failed to create symlink from {src} to {dst}: {e}")
else:
logger.info(f"Symlink or file at {dst} already exists. No action taken.")
return False


def run_command_is_working():
output = run_command("uname -a", check=False)
if output.returncode != 0:
Expand Down Expand Up @@ -489,6 +561,12 @@ def main() -> int:
update_dwc2,
]
)
if host_cpu == CpuType.PI5:
patches_to_apply.extend(
[
load_pi5_overlays_in_runtime,
]
)

logger.info("The following patches will be applied if needed:")
for patch in patches_to_apply:
Expand Down

0 comments on commit 060e44a

Please sign in to comment.