From fe590b627bb42c1ac22b6b534e62f41f92fd8f75 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Wed, 17 Jul 2024 19:18:34 +0300 Subject: [PATCH] scripts/libraries: Add Alif boot scripts. Signed-off-by: iabdalkader --- scripts/libraries/alif/he/_boot.py | 70 ++++++++++++++++++++++++++++++ scripts/libraries/alif/hp/_boot.py | 19 ++++++++ 2 files changed, 89 insertions(+) create mode 100644 scripts/libraries/alif/he/_boot.py create mode 100644 scripts/libraries/alif/hp/_boot.py diff --git a/scripts/libraries/alif/he/_boot.py b/scripts/libraries/alif/he/_boot.py new file mode 100644 index 000000000..1d90a2009 --- /dev/null +++ b/scripts/libraries/alif/he/_boot.py @@ -0,0 +1,70 @@ +import os +import openamp +import asyncio +from asyncio import CancelledError +import struct +import marshal +from types import FunctionType + + +# MicroPython doesn't have this exception +class InvalidStateError(Exception): + pass + + +class DoneException(Exception): + pass + + +tasks = {} +_epts = {} + + +def vm_out_callback(src_addr, data): + try: + nlen, mlen = struct.unpack("II", data[0:8]) + name, mpy = struct.unpack(f"{nlen}s{mlen}s", data[8:]) + name = name.decode() + g = FunctionType(marshal.loads(mpy), globals()) + ept = openamp.Endpoint(name) + tasks[name] = asyncio.create_task(g(ept)) + except Exception as e: + print(str(e)) + + +async def main(): + while True: + if tasks: + task_except = None + try: + await asyncio.gather(*tasks.values(), return_exceptions=False) + except Exception as e: + task_except = e + + for name in list(tasks): + task = tasks[name] + try: + if task.done(): + tasks.pop(name) + if not isinstance(task_except, DoneException): + print(f'Task "{name}" raised: {task_except}') + break # Break after the first task is removed. + except (CancelledError, InvalidStateError): + pass + await asyncio.sleep(0.5) + print(f"running tasks:{len(tasks)}") + + +try: + os.chdir("/rom") +except: + pass + +try: + # openamp.new_service_callback(vm_ns_callback) + # Create the RPMsg endpoints to communicate with HP core. + _epts["vm"] = openamp.Endpoint("vm", callback=vm_out_callback) + os.dupterm(openamp.EndpointIO(_epts["vm"]), 0) + asyncio.run(main()) +except Exception as e: + print(e) diff --git a/scripts/libraries/alif/hp/_boot.py b/scripts/libraries/alif/hp/_boot.py new file mode 100644 index 000000000..afe4f0a9a --- /dev/null +++ b/scripts/libraries/alif/hp/_boot.py @@ -0,0 +1,19 @@ +import sys +import os +import alif +import vfs + +bdev = alif.Flash() +try: + fat = vfs.VfsFat(bdev) + vfs.mount(fat, "/flash") +except: + vfs.VfsFat.mkfs(bdev) + fat = vfs.VfsFat(bdev) + vfs.mount(fat, "/flash") + +sys.path.append("/flash") +sys.path.append("/flash/lib") +os.chdir("/flash") + +del sys, os, alif, bdev