Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
Josverl committed Jul 1, 2024
2 parents d741c0a + f060d4e commit 9ebdc24
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 73 deletions.
21 changes: 11 additions & 10 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ stubber = "stubber.stubber:stubber_cli"
[tool.poetry.dependencies]
python = ">=3.9,<4.0"
python-minifier = { version = "^2.7.0", python = "<3.12" } # no support for 3.12 yet
requests = "^2.28.0"
requests = "^2.32.3"
mypy = "1.9.0"
mpflash = "^0.8.6"
mpflash = "^0.9.0"
mpremote = "^1.23.0"
# others
autoflake = ">=1.7,<3.0"
Expand All @@ -74,6 +74,7 @@ tenacity = "^8.2.2"
tomli = { version = "^2.0.1", python = "<3.11" }
tomli-w = "^1.0.0"
typed-config = "^1.3.0"
urllib3 = "^2.2.2"

[tool.poetry.group.docs]
optional = true
Expand Down
18 changes: 14 additions & 4 deletions src/mpflash/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ This tool was initially created to be used in a CI/CD pipeline to automate the p
`mpflash` has been tested on:
- OS: Windows x64, Linux X64, but not (yet) macOS.
- Micropython (hardware) ports:
- `rp2`, using `.uf2`, using filecopy (macos not tested yet)
- `samd`, using ` .uf2`, using filecopy (macos not tested yet)
- `rp2`, using `.uf2`, using filecopy
- `samd`, using ` .uf2`, using filecopy
- `esp32`, using `.bin`, using esptool,
- `esp8266`, using `.bin`, using esptool
- `stm32`, using ` .dfu`, using pydfu
Expand All @@ -25,7 +25,7 @@ Not yet implemented: `nrf`, `cc3200`, `mimxrt`
3. Flash one or all connected MicroPython boards with a specific firmware or version.

## Installation
To install mpflash, you can use pip: `pip install mpflash`
To install mpflash, you can use: `pipx install mpflash` or `pip install mpflash`

## Basic usage
You can use mpflash to perform various operations on your MicroPython boards. Here is an example of basic usage:
Expand Down Expand Up @@ -69,7 +69,7 @@ To download the MicroPython firmware for some boards, use the following command:

These will try to download the prebuilt MicroPython firmware for the boards from https://micropython.org/download/ and save it in your downloads folder in the `firmware` directory.
The stable version (default) is determined based on the most recent published release,
other versions are `--version preview` and `--version x.y.z` to download the latest preview or version x.y.z respectively.
other options are `--version stable`, `--version preview` and `--version x.y.z` to download the latest stable, preview or version x.y.z respectively.

By default the firmware will be downloaded to your OS's preferred `Downloads/firmware` folder, but you can speciy a different directory using the `--dir` option.

Expand All @@ -92,8 +92,18 @@ After you have downloaded a firmware you can flash the firmware to a board usin
This will (try to) autodetect the connected boards, and determine the correct firmware to flash to each board.

- `mpflash flash` will flash the latest stable firmware to all connected boards.
If you have a board withouth a running micropython version, you will need to specify the board and the serial port to flash.
- `mpflash flash --serial ? --board ?` will prompt to select a specific serial port and board to flash. (the firmware must be dowloaded earlier)

In order to flash the firmware some boards need to be put in bootloader mode, this is done automatically by mpflash where possible and supported by the boards hardware and current bootloader.
The supported `--bootloader` options are:

- `touch1200` bootloader is activated by connecting to the board at 1200 baud
- `mpy` using micropython to enter the bootloader
- `manual` manual intervention is needed to enter the bootloader
- `none` mpflash assumes the board is ready to flash

For ESP32 and ESP8266 boards the `esptool` is used to flash the firmware, and this includes activating the bootloader.

### Flashing all connected boards with the latest stable firmware
```bash
Expand Down
1 change: 0 additions & 1 deletion src/mpflash/mpflash/bootloader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def enter_bootloader(
# NO bootloader requested, so must be OK to flash
return True

log.info(f"Entering bootloader on {mcu.board} on {mcu.serialport} using method: {method.value}")
if method == BootloaderMethod.MPY:
result = enter_bootloader_mpy(mcu, timeout=timeout)
elif method == BootloaderMethod.MANUAL:
Expand Down
26 changes: 7 additions & 19 deletions src/mpflash/mpflash/bootloader/touch1200.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,20 @@
from mpflash.logger import log
from mpflash.mpremoteboard import MPRemoteBoard

from .manual import enter_bootloader_manual


def enter_bootloader_cdc_1200bps(mcu: MPRemoteBoard, timeout: int = 10):
if sys.platform == "win32":
log.warning("Touch 1200bps method is currently not supported on Windows, switching to manual")
return enter_bootloader_manual(mcu, timeout=timeout)

log.info(f"Entering bootloader on {mcu.board} on {mcu.serialport} using CDC 1200bps")
# if port argument is present perform soft reset
if not mcu.serialport:
raise MPFlashError("No serial port specified")
log.info(f"Entering bootloader on {mcu.serialport} using CDC toch 1200 bps method")
# if port argument is present perform soft reset
# try to initiate serial port connection on PORT with 1200 baudrate
try:
with serial.Serial(
port=mcu.serialport,
baudrate=1200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
rtscts=True,
) as connection:
print("Connection established")
connection.rts = True
connection.dtr = False
time.sleep(0.4)
com = serial.Serial(mcu.serialport, 1200, dsrdtr=True)
com.rts = False # required
com.dtr = False # might as well
time.sleep(0.2)
com.close()

except serial.SerialException as e:
log.exception(e)
Expand Down
15 changes: 10 additions & 5 deletions src/mpflash/mpflash/vendor/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
#############################################################
"""

from functools import lru_cache

from cache_to_disk import cache_to_disk, NoCacheCondition
from loguru import logger as log
from packaging.version import parse

from mpflash.common import GH_CLIENT

OLDEST_VERSION = "1.16"
"This is the oldest MicroPython version to build the stubs on"

Expand Down Expand Up @@ -70,9 +70,10 @@ def clean_version(
return version


@lru_cache(maxsize=10)
@cache_to_disk(n_days_to_cache=1)
def micropython_versions(minver: str = "v1.20", reverse: bool = False):
"""Get the list of micropython versions from github tags"""
cache_it = True
try:
gh_client = GH_CLIENT
repo = gh_client.get_repo("micropython/micropython")
Expand Down Expand Up @@ -100,10 +101,15 @@ def micropython_versions(minver: str = "v1.20", reverse: bool = False):
"v1.11",
"v1.10",
]
cache_it = False
versions = [v for v in versions if parse(v) >= parse(minver)]
# remove all but the most recent (preview) version
versions = versions[:1] + [v for v in versions if "preview" not in v]
return sorted(versions, reverse=reverse)
versions = sorted(versions, reverse=reverse)
if cache_it:
return versions
# returns - but does not cache
raise NoCacheCondition(function_value=versions)


def get_stable_mp_version() -> str:
Expand All @@ -116,4 +122,3 @@ def get_preview_mp_version() -> str:
# read the versions from the git tags
all_versions = micropython_versions(minver=OLDEST_VERSION)
return [v for v in all_versions if v.endswith(V_PREVIEW)][-1]

21 changes: 16 additions & 5 deletions src/mpflash/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/mpflash/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pyusb = "^1.2.1"
requests = "^2.31.0"
rich-click = "^1.8.1"
tenacity = "8.2.3"
cache-to-disk = "^2.0.0"


[tool.poetry.group.dev]
Expand Down
2 changes: 1 addition & 1 deletion src/stubber/commands/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@click.version_option(package_name="micropython-stubber", prog_name="micropython-stubber✏️ ")
@click.option(
"-V",
"-v",
"-V",
"--verbose",
count=True,
default=0,
Expand Down
1 change: 1 addition & 0 deletions src/stubber/commands/clone_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
type=click.Path(file_okay=False, dir_okay=True),
)
@click.option(
"--stubs/--no-stubs",
"--add-stubs/--no-stubs",
"stubs",
default=False,
Expand Down
2 changes: 1 addition & 1 deletion src/stubber/commands/get_frozen_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
@click.option(
"--version",
"--Version",
"-V",
"-v",
"version",
default="",
# default=[CONFIG.stable_version],
Expand Down
2 changes: 1 addition & 1 deletion src/stubber/commands/get_mcu_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
@stubber_cli.command(name="get-mcu-stubs")
@click.option(
"--variant",
"-v",
# "-v",
type=click.Choice(["Full", "Mem", "DB"], case_sensitive=False),
default="DB",
show_default=True,
Expand Down
3 changes: 1 addition & 2 deletions src/stubber/commands/merge_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
@click.option("--family", default="micropython", type=str, show_default=True)
@click.option(
"--version",
"--Version",
"-V",
"-v",
"versions",
multiple=True,
default=["all"],
Expand Down
3 changes: 1 addition & 2 deletions src/stubber/commands/publish_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
@click.option("--family", default="micropython", type=str, show_default=True)
@click.option(
"--version",
"--Version",
"-V",
"-v",
"versions",
multiple=True,
default=[CONFIG.stable_version],
Expand Down
3 changes: 1 addition & 2 deletions src/stubber/commands/variants_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

@click.option(
"--version",
"--Version",
"-V",
"-v",
"version",
default=CONFIG.stable_version,
show_default=True,
Expand Down
Loading

0 comments on commit 9ebdc24

Please sign in to comment.