|
| 1 | +Import("env") |
| 2 | +import time |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | +from enum import Enum |
| 6 | + |
| 7 | +BOOT_GPIO = 24 |
| 8 | +RESET_GPIO = 23 |
| 9 | + |
| 10 | +""" |
| 11 | +This script is deployed onto the remote device and configures automatic reset/boot for the target esp32 to be flashed. |
| 12 | +""" |
| 13 | + |
| 14 | + |
| 15 | +class PinState(Enum): |
| 16 | + LOW = 0 |
| 17 | + HIGH = 1 |
| 18 | + |
| 19 | + |
| 20 | +def sysfs_gpio(pin, value) -> None: |
| 21 | + """Configures the pin and sets the value of the GPIO pin using the file system |
| 22 | + :param pin: Target pin |
| 23 | + :param value: Value to set to |
| 24 | + """ |
| 25 | + if not os.path.exists(f"/sys/class/gpio/gpio{pin}"): |
| 26 | + with open("/sys/class/gpio/export", "w") as f: |
| 27 | + f.write(str(pin)) |
| 28 | + time.sleep(0.1) |
| 29 | + with open(f"/sys/class/gpio/gpio{pin}/direction", "w") as f: |
| 30 | + f.write("out") |
| 31 | + with open(f"/sys/class/gpio/gpio{pin}/value", "w") as f: |
| 32 | + f.write(str(value.value)) |
| 33 | + |
| 34 | + |
| 35 | +def set_gpio(pin, value) -> None: |
| 36 | + """Sets the value of the given GPIO pin. Uses High or Low only on fallback. |
| 37 | + :param pin: Pin to set |
| 38 | + :param value: Value to set to |
| 39 | + """ |
| 40 | + try: |
| 41 | + sysfs_gpio(pin, value) |
| 42 | + except Exception: |
| 43 | + v = "dh" if value == PinState.HIGH else "dl" |
| 44 | + try: |
| 45 | + subprocess.run( |
| 46 | + ["pinctrl", "set", str(pin), "op", v], |
| 47 | + check=True, |
| 48 | + stdout=subprocess.DEVNULL, |
| 49 | + stderr=subprocess.DEVNULL, |
| 50 | + ) |
| 51 | + except Exception: |
| 52 | + subprocess.run( |
| 53 | + ["raspi-gpio", "set", str(pin), "op", v], |
| 54 | + check=False, |
| 55 | + stdout=subprocess.DEVNULL, |
| 56 | + stderr=subprocess.DEVNULL, |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +def before_upload(source, target, env) -> None: |
| 61 | + """Action to be run before firmware flashing. |
| 62 | +
|
| 63 | + :param source: Compiled firmware |
| 64 | + :param target: Build action name |
| 65 | + :param env: Environment variables |
| 66 | + """ |
| 67 | + print( |
| 68 | + f"Setting ESP32 to bootloader mode using GPIO {BOOT_GPIO} (BOOT) and GPIO {RESET_GPIO} (EN)..." |
| 69 | + ) |
| 70 | + set_gpio(BOOT_GPIO, PinState.LOW) |
| 71 | + time.sleep(0.1) |
| 72 | + set_gpio(RESET_GPIO, PinState.LOW) |
| 73 | + time.sleep(0.1) |
| 74 | + set_gpio(RESET_GPIO, PinState.HIGH) |
| 75 | + time.sleep(0.5) |
| 76 | + set_gpio(BOOT_GPIO, PinState.HIGH) |
| 77 | + |
| 78 | + |
| 79 | +def after_upload(source, target, env) -> None: |
| 80 | + """Action to be run after firmware flashing. |
| 81 | +
|
| 82 | + :param source: Compiled firmware |
| 83 | + :param target: Build action name |
| 84 | + :param env: Environment variables |
| 85 | + """ |
| 86 | + print("Resetting ESP32...") |
| 87 | + set_gpio(RESET_GPIO, PinState.LOW) |
| 88 | + time.sleep(0.1) |
| 89 | + set_gpio(RESET_GPIO, PinState.HIGH) |
| 90 | + |
| 91 | + |
| 92 | +# Attach pre-upload and post-upload hooks |
| 93 | +env.AddPreAction("upload", before_upload) |
| 94 | +env.AddPostAction("upload", after_upload) |
0 commit comments