micropython-stubber 1.24.1__py3-none-any.whl → 1.24.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. {micropython_stubber-1.24.1.dist-info → micropython_stubber-1.24.4.dist-info}/METADATA +9 -29
  2. micropython_stubber-1.24.4.dist-info/RECORD +107 -0
  3. {micropython_stubber-1.24.1.dist-info → micropython_stubber-1.24.4.dist-info}/WHEEL +1 -1
  4. stubber/__init__.py +1 -1
  5. stubber/board/createstubs.py +44 -38
  6. stubber/board/createstubs_db.py +17 -12
  7. stubber/board/createstubs_db_min.py +63 -63
  8. stubber/board/createstubs_db_mpy.mpy +0 -0
  9. stubber/board/createstubs_mem.py +17 -12
  10. stubber/board/createstubs_mem_min.py +99 -99
  11. stubber/board/createstubs_mem_mpy.mpy +0 -0
  12. stubber/board/createstubs_min.py +111 -112
  13. stubber/board/createstubs_mpy.mpy +0 -0
  14. stubber/board/modulelist.txt +27 -27
  15. stubber/codemod/board.py +1 -1
  16. stubber/codemod/enrich.py +13 -13
  17. stubber/codemod/merge_docstub.py +83 -53
  18. stubber/codemod/visitors/type_helpers.py +143 -41
  19. stubber/commands/enrich_folder_cmd.py +17 -17
  20. stubber/commands/get_docstubs_cmd.py +27 -9
  21. stubber/commands/get_frozen_cmd.py +1 -0
  22. stubber/commands/merge_cmd.py +2 -4
  23. stubber/merge_config.py +5 -36
  24. stubber/minify.py +3 -3
  25. stubber/modcat.py +118 -0
  26. stubber/publish/merge_docstubs.py +22 -5
  27. stubber/publish/stubpackage.py +33 -28
  28. stubber/rst/lookup.py +6 -23
  29. stubber/rst/reader.py +8 -13
  30. stubber/stubs_from_docs.py +2 -1
  31. stubber/tools/manifestfile.py +2 -1
  32. stubber/{cst_transformer.py → typing_collector.py} +36 -4
  33. micropython_stubber-1.24.1.dist-info/RECORD +0 -161
  34. mpflash/README.md +0 -220
  35. mpflash/libusb_flash.ipynb +0 -203
  36. mpflash/mpflash/__init__.py +0 -0
  37. mpflash/mpflash/add_firmware.py +0 -98
  38. mpflash/mpflash/ask_input.py +0 -236
  39. mpflash/mpflash/basicgit.py +0 -324
  40. mpflash/mpflash/bootloader/__init__.py +0 -2
  41. mpflash/mpflash/bootloader/activate.py +0 -60
  42. mpflash/mpflash/bootloader/detect.py +0 -82
  43. mpflash/mpflash/bootloader/manual.py +0 -101
  44. mpflash/mpflash/bootloader/micropython.py +0 -12
  45. mpflash/mpflash/bootloader/touch1200.py +0 -36
  46. mpflash/mpflash/cli_download.py +0 -129
  47. mpflash/mpflash/cli_flash.py +0 -224
  48. mpflash/mpflash/cli_group.py +0 -111
  49. mpflash/mpflash/cli_list.py +0 -87
  50. mpflash/mpflash/cli_main.py +0 -39
  51. mpflash/mpflash/common.py +0 -217
  52. mpflash/mpflash/config.py +0 -44
  53. mpflash/mpflash/connected.py +0 -96
  54. mpflash/mpflash/download.py +0 -364
  55. mpflash/mpflash/downloaded.py +0 -138
  56. mpflash/mpflash/errors.py +0 -9
  57. mpflash/mpflash/flash/__init__.py +0 -55
  58. mpflash/mpflash/flash/esp.py +0 -59
  59. mpflash/mpflash/flash/stm32.py +0 -19
  60. mpflash/mpflash/flash/stm32_dfu.py +0 -104
  61. mpflash/mpflash/flash/uf2/__init__.py +0 -88
  62. mpflash/mpflash/flash/uf2/boardid.py +0 -15
  63. mpflash/mpflash/flash/uf2/linux.py +0 -136
  64. mpflash/mpflash/flash/uf2/macos.py +0 -42
  65. mpflash/mpflash/flash/uf2/uf2disk.py +0 -12
  66. mpflash/mpflash/flash/uf2/windows.py +0 -43
  67. mpflash/mpflash/flash/worklist.py +0 -170
  68. mpflash/mpflash/list.py +0 -106
  69. mpflash/mpflash/logger.py +0 -41
  70. mpflash/mpflash/mpboard_id/__init__.py +0 -98
  71. mpflash/mpflash/mpboard_id/add_boards.py +0 -262
  72. mpflash/mpflash/mpboard_id/board.py +0 -37
  73. mpflash/mpflash/mpboard_id/board_id.py +0 -90
  74. mpflash/mpflash/mpboard_id/board_info.zip +0 -0
  75. mpflash/mpflash/mpboard_id/store.py +0 -48
  76. mpflash/mpflash/mpremoteboard/__init__.py +0 -271
  77. mpflash/mpflash/mpremoteboard/mpy_fw_info.py +0 -152
  78. mpflash/mpflash/mpremoteboard/runner.py +0 -140
  79. mpflash/mpflash/vendor/board_database.py +0 -185
  80. mpflash/mpflash/vendor/click_aliases.py +0 -91
  81. mpflash/mpflash/vendor/dfu.py +0 -165
  82. mpflash/mpflash/vendor/pydfu.py +0 -605
  83. mpflash/mpflash/vendor/readme.md +0 -12
  84. mpflash/mpflash/versions.py +0 -123
  85. mpflash/poetry.lock +0 -2603
  86. mpflash/pyproject.toml +0 -66
  87. mpflash/stm32_udev_rules.md +0 -63
  88. stubber/codemod/test_enrich.py +0 -87
  89. {micropython_stubber-1.24.1.dist-info → micropython_stubber-1.24.4.dist-info}/LICENSE +0 -0
  90. {micropython_stubber-1.24.1.dist-info → micropython_stubber-1.24.4.dist-info}/entry_points.txt +0 -0
@@ -1,60 +0,0 @@
1
- import time
2
-
3
- from mpflash.bootloader.detect import in_bootloader
4
- from mpflash.common import BootloaderMethod
5
- from mpflash.errors import MPFlashError
6
- from mpflash.logger import log
7
- from mpflash.mpremoteboard import MPRemoteBoard
8
-
9
- from .manual import enter_bootloader_manual
10
- from .micropython import enter_bootloader_mpy
11
- from .touch1200 import enter_bootloader_touch_1200bps
12
-
13
- BL_OPTIONS = {
14
- "stm32": [BootloaderMethod.TOUCH_1200, BootloaderMethod.MPY, BootloaderMethod.MANUAL],
15
- "rp2": [BootloaderMethod.TOUCH_1200, BootloaderMethod.MPY, BootloaderMethod.MANUAL],
16
- "samd": [BootloaderMethod.TOUCH_1200, BootloaderMethod.MPY, BootloaderMethod.MANUAL],
17
- "esp32": [BootloaderMethod.NONE],
18
- "esp8266": [BootloaderMethod.NONE],
19
- }
20
-
21
-
22
- def enter_bootloader(
23
- mcu: MPRemoteBoard,
24
- method: BootloaderMethod = BootloaderMethod.MPY,
25
- timeout: int = 10,
26
- wait_after: int = 2,
27
- ):
28
- """Enter the bootloader mode for the board"""
29
- if method == BootloaderMethod.NONE:
30
- # NO bootloader requested, so must be OK to flash
31
- return True
32
- elif method == BootloaderMethod.AUTO:
33
- # build a list of options to try for this board
34
- bl_list = BL_OPTIONS.get(mcu.port, [BootloaderMethod.MPY, BootloaderMethod.MANUAL])
35
- else:
36
- bl_list = [method, BootloaderMethod.MANUAL]
37
- log.info(f"Entering bootloader on {mcu.serialport} using methods {[bl.value for bl in bl_list]}")
38
- for method in bl_list:
39
- try:
40
- if method == BootloaderMethod.MPY:
41
- result = enter_bootloader_mpy(mcu, timeout=timeout)
42
- elif method == BootloaderMethod.MANUAL:
43
- result = enter_bootloader_manual(mcu, timeout=timeout)
44
- elif method == BootloaderMethod.TOUCH_1200:
45
- result = enter_bootloader_touch_1200bps(mcu, timeout=timeout)
46
- except MPFlashError as e:
47
- log.warning(f"Failed to enter bootloader on {mcu.serialport} using {method.value}")
48
- log.exception(e)
49
- result = False
50
- if not result:
51
- # try a next method
52
- continue
53
-
54
- # todo - check every second or so for up to max wait time
55
- time.sleep(wait_after)
56
- # check if bootloader was entered
57
- if in_bootloader(mcu):
58
- return True
59
-
60
- return result
@@ -1,82 +0,0 @@
1
- """ Detect if a board is in bootloader mode
2
- """
3
-
4
- import os
5
-
6
- from mpflash.common import PORT_FWTYPES
7
- from mpflash.flash.uf2 import waitfor_uf2
8
- from mpflash.logger import log
9
- from mpflash.mpremoteboard import MPRemoteBoard
10
-
11
-
12
- def in_bootloader(mcu: MPRemoteBoard) -> bool:
13
- """Check if the board is in bootloader mode"""
14
- if ".uf2" in PORT_FWTYPES[mcu.port]:
15
- return in_uf2_bootloader(mcu.port.upper())
16
- elif mcu.port in ["stm32"]:
17
- return in_stm32_bootloader()
18
- elif mcu.port in ["esp32", "esp8266"]:
19
- log.debug("esp32/esp8266 does not have a bootloader mode, Assume OK to flash")
20
- return True
21
-
22
- log.error(f"Bootloader mode not supported on {mcu.board} on {mcu.serialport}")
23
- return False
24
-
25
-
26
- def in_uf2_bootloader(board_id: str) -> bool:
27
- """
28
- Check if the board is in UF2 bootloader mode.
29
-
30
- :param board_id: The board ID to check for (SAMD or RP2)
31
- """
32
- return bool(waitfor_uf2(board_id=board_id))
33
-
34
-
35
- def in_stm32_bootloader() -> bool:
36
- """Check if the board is in STM32 bootloader mode"""
37
- if os.name == "nt":
38
- driver_installed, status = check_for_stm32_bootloader_device()
39
- if not driver_installed:
40
- log.warning("STM32 BOOTLOADER device not found.")
41
- return False
42
- print()
43
- if status != "OK":
44
- log.warning(f"STM32 BOOTLOADER device found, Device status: {status}")
45
- log.error("Please use Zadig to install a WinUSB (libusb) driver.\nhttps://github.com/pbatard/libwdi/wiki/Zadig")
46
- return False
47
- return check_dfu_devices()
48
-
49
-
50
- def check_dfu_devices():
51
- """Check if there are any DFU devices connected"""
52
- # JIT import
53
- from mpflash.flash.stm32_dfu import dfu_init
54
- from mpflash.vendor.pydfu import get_dfu_devices
55
-
56
- # need to init on windows to get the right usb backend
57
- dfu_init()
58
- devices = get_dfu_devices()
59
- return len(devices) > 0
60
-
61
-
62
- def check_for_stm32_bootloader_device():
63
- import win32com.client
64
-
65
- # Windows only
66
- # Create a WMI interface object
67
- wmi = win32com.client.GetObject("winmgmts:")
68
-
69
- # Query for USB devices
70
- for usb_device in wmi.InstancesOf("Win32_PnPEntity"):
71
- try:
72
- # Check if device name or description contains "STM32 BOOTLOADER"
73
- if str(usb_device.Name).strip() in {
74
- "STM32 BOOTLOADER",
75
- "STM BOOTLOADER",
76
- }:
77
- # Just the first match is enough
78
- return True, usb_device.Status
79
- except Exception:
80
- pass
81
- # If no matching device was found
82
- return False, "Not found."
@@ -1,101 +0,0 @@
1
- """Manual bootloader mode entry for various MCUs."""
2
-
3
- from click.exceptions import Abort
4
- from rich.console import Console
5
- from rich.highlighter import RegexHighlighter
6
- from rich.panel import Panel
7
- from rich.prompt import Confirm
8
- from rich.theme import Theme
9
-
10
- # from mpflash.logger import console, log
11
- from mpflash.mpremoteboard import MPRemoteBoard
12
-
13
-
14
- class MCUHighlighter(RegexHighlighter):
15
- """Apply style to things that should stand out."""
16
-
17
- base_style = "mcu."
18
- highlights = [
19
- r"(?P<bold>Method[\s\d\:]*)",
20
- r"(?P<bold> \d.)", # numbered items
21
- r"(?P<bold> - )", # bullets
22
- # mcu things
23
- r"(?P<pad>GPIO[\d]*)",
24
- r"(?P<pad>GPI[\d]*)",
25
- r"(?P<pad>IO[\d]*)",
26
- r"(?P<pad>RUN)",
27
- r"(?P<pad>GND)",
28
- r"(?P<pad>VCC)",
29
- r"(?P<pad>3.3V)",
30
- r"(?P<pad>5V)",
31
- # buttons
32
- r"(?P<button>BOOTSEL)",
33
- r"(?P<button>RESET)",
34
- r"(?P<button>reset)",
35
- # other
36
- r"(?P<cable>USB)",
37
- r"(?P<cable>USB-C)",
38
- r"(?P<cable>Serial)",
39
- ]
40
-
41
-
42
- # https://rich.readthedocs.io/en/stable/appendix/colors.html?highlight=colors#standard-colors
43
- # use 3 colors to keep things simple but clear
44
- mcu_theme = Theme(
45
- {
46
- "mcu.bold": "orange3", # readers guidance
47
- "mcu.button": "bold green", # things to press
48
- "mcu.pad": "dodger_blue2", # things to connect
49
- "mcu.cable": "dodger_blue2", # things to connect
50
- }
51
- )
52
-
53
-
54
- def enter_bootloader_manual(mcu: MPRemoteBoard, timeout: int = 10):
55
-
56
- message: str
57
- if mcu.port == "rp2":
58
- message = f"""\
59
- Please put your {" ".join([mcu.port,mcu.board])} device into bootloader mode by either:
60
- Method 1:
61
- 1. Unplug the USB cable,
62
- 2. Press and hold the BOOTSEL button on the device,
63
- 3. Plug the USB cable back in.
64
- 4. Release the BOOTSEL button.
65
-
66
- Method 2:
67
- 1. Press and hold the BOOTSEL button on the device,
68
- 2. Reset the device by either:
69
- - pressing the RESET button on the device
70
- - by power-cycling the device,
71
- - by briefly connecting the RUN pin to GND
72
- 3. Release the BOOTSEL button.
73
- """
74
- elif mcu.port == "samd":
75
- message = f"""\
76
- Please put your {mcu.port.upper()} device into bootloader mode by:
77
- - Pressing or sliding the RESET button twice in fast succession
78
- """
79
- else:
80
- message = f"""\
81
- Please put your {mcu.port.upper()} device into bootloader mode by:
82
- - Pressing the RESET button on the device
83
- """
84
-
85
- # todo: would be nice to re-use the console instance from logger
86
- console = Console(highlighter=MCUHighlighter(), theme=mcu_theme) # type: ignore
87
- message += "\nIf you are unsure how to enter bootloader mode, please refer to the device documentation."
88
- console.print(
89
- Panel(
90
- message,
91
- highlight=True,
92
- title="Manual Bootloader",
93
- title_align="left",
94
- expand=False,
95
- )
96
- )
97
- try:
98
- answer = Confirm.ask("Press Enter to continue", default="y")
99
- except Abort:
100
- return False
101
- return answer in ["y", "Y", True]
@@ -1,12 +0,0 @@
1
- """Module for handling the bootloader mode for micropython boards"""
2
-
3
- from mpflash.logger import log
4
- from mpflash.mpremoteboard import MPRemoteBoard
5
-
6
-
7
- def enter_bootloader_mpy(mcu: MPRemoteBoard, timeout: int = 10):
8
- """Enter the bootloader mode for the board using mpremote and micropython on the board"""
9
- log.info(f"Attempting bootloader on {mcu.serialport} using 'mpremote bootloader'")
10
- mcu.run_command("bootloader", timeout=timeout)
11
- # todo: check if mpremote command was successful
12
- return True
@@ -1,36 +0,0 @@
1
- """
2
- Enter bootloader using Touch 1200Bd for boards with bootloaders that support this.
3
-
4
- """
5
-
6
- import time
7
-
8
- import serial
9
-
10
- from mpflash.errors import MPFlashError
11
- from mpflash.logger import log
12
- from mpflash.mpremoteboard import MPRemoteBoard
13
-
14
-
15
- def enter_bootloader_touch_1200bps(mcu: MPRemoteBoard, timeout: int = 10):
16
- if not mcu.serialport:
17
- raise MPFlashError("No serial port specified")
18
- log.info(f"Attempting bootloader on {mcu.serialport} using 'Touch 1200Bd'")
19
- # if port argument is present perform soft reset
20
- # try to initiate serial port connection on PORT with 1200 baudrate
21
- try:
22
- com = serial.Serial(mcu.serialport, 1200, dsrdtr=True)
23
- com.rts = False # required
24
- com.dtr = False # might as well
25
- time.sleep(0.2)
26
- com.close()
27
-
28
- except serial.SerialException as e:
29
- log.exception(e)
30
- raise MPFlashError("pySerial error: " + str(e) + "\n") from e
31
- except Exception as e:
32
- log.exception(e)
33
- raise MPFlashError("Error: " + str(e) + "\n") from e
34
-
35
- # be optimistic
36
- return True
@@ -1,129 +0,0 @@
1
- """CLI to Download MicroPython firmware for specific ports, boards and versions."""
2
-
3
- from pathlib import Path
4
-
5
- import rich_click as click
6
- from loguru import logger as log
7
-
8
- from mpflash.connected import connected_ports_boards
9
- from mpflash.errors import MPFlashError
10
- from mpflash.mpboard_id import find_known_board
11
- from mpflash.versions import clean_version
12
-
13
- from .ask_input import ask_missing_params
14
- from .cli_group import cli
15
- from .common import DownloadParams
16
- from .config import config
17
- from .download import download
18
-
19
-
20
- @cli.command(
21
- "download",
22
- help="Download MicroPython firmware for specific ports, boards and versions.",
23
- )
24
- @click.option(
25
- "--destination",
26
- "-d",
27
- "fw_folder",
28
- type=click.Path(file_okay=False, dir_okay=True, path_type=Path),
29
- default=config.firmware_folder,
30
- show_default=True,
31
- help="The folder to download the firmware to.",
32
- )
33
- @click.option(
34
- "--version",
35
- "-v",
36
- "versions",
37
- default=["stable"],
38
- multiple=True,
39
- show_default=True,
40
- help="The version of MicroPython to to download.",
41
- metavar="SEMVER, 'stable', 'preview' or '?'",
42
- )
43
- @click.option(
44
- "--board",
45
- "-b",
46
- "boards",
47
- multiple=True,
48
- default=[],
49
- show_default=True,
50
- help="The board(s) to download the firmware for.",
51
- metavar="BOARD_ID or ?",
52
- )
53
- @click.option(
54
- "--serial",
55
- "--serial-port",
56
- "-s",
57
- "serial",
58
- default=["*"],
59
- show_default=True,
60
- multiple=True,
61
- help="Which serial port(s) (or globs) to flash",
62
- metavar="SERIALPORT",
63
- )
64
- @click.option(
65
- "--ignore",
66
- "-i",
67
- is_eager=True,
68
- help="Serial port(s) to ignore. Defaults to MPFLASH_IGNORE.",
69
- multiple=True,
70
- default=[],
71
- envvar="MPFLASH_IGNORE",
72
- show_default=True,
73
- metavar="SERIALPORT",
74
- )
75
- @click.option(
76
- "--clean/--no-clean",
77
- default=True,
78
- show_default=True,
79
- help="""Remove dates and hashes from the downloaded firmware filenames.""",
80
- )
81
- @click.option(
82
- "--force",
83
- "-f",
84
- default=False,
85
- is_flag=True,
86
- show_default=True,
87
- help="""Force download of firmware even if it already exists.""",
88
- )
89
- def cli_download(**kwargs) -> int:
90
- params = DownloadParams(**kwargs)
91
- params.versions = list(params.versions)
92
- params.boards = list(params.boards)
93
- params.serial = list(params.serial)
94
- params.ignore = list(params.ignore)
95
-
96
- # all_boards: List[MPRemoteBoard] = []
97
- if params.boards:
98
- if not params.ports:
99
- # no ports specified - resolve ports from specified boards by resolving board IDs
100
- for board in params.boards:
101
- if board != "?":
102
- try:
103
- board_ = find_known_board(board)
104
- params.ports.append(board_.port)
105
- except MPFlashError as e:
106
- log.error(f"{e}")
107
- else:
108
- # no boards specified - detect connected ports and boards
109
- params.ports, params.boards, _ = connected_ports_boards(include=params.serial, ignore=params.ignore)
110
-
111
- params = ask_missing_params(params)
112
- if not params: # Cancelled by user
113
- return 2
114
- params.versions = [clean_version(v, drop_v=True) for v in params.versions]
115
- assert isinstance(params, DownloadParams)
116
-
117
- try:
118
- download(
119
- params.fw_folder,
120
- params.ports,
121
- params.boards,
122
- params.versions,
123
- params.force,
124
- params.clean,
125
- )
126
- return 0
127
- except MPFlashError as e:
128
- log.error(f"{e}")
129
- return 1
@@ -1,224 +0,0 @@
1
- from pathlib import Path
2
- from typing import List
3
-
4
- import rich_click as click
5
- from loguru import logger as log
6
-
7
- from mpflash.ask_input import ask_missing_params
8
- from mpflash.cli_download import connected_ports_boards
9
- from mpflash.cli_group import cli
10
- from mpflash.cli_list import show_mcus
11
- from mpflash.common import BootloaderMethod, FlashParams, Params
12
- from mpflash.config import config
13
- from mpflash.errors import MPFlashError
14
- from mpflash.flash import flash_list
15
- from mpflash.flash.worklist import WorkList, full_auto_worklist, manual_worklist, single_auto_worklist
16
- from mpflash.mpboard_id import find_known_board
17
- from mpflash.mpremoteboard import MPRemoteBoard
18
- from mpflash.versions import clean_version
19
-
20
- # #########################################################################################################
21
- # CLI
22
- # #########################################################################################################
23
-
24
-
25
- @cli.command(
26
- "flash",
27
- short_help="Flash one or all connected MicroPython boards with a specific firmware and version.",
28
- )
29
- @click.option(
30
- "--firmware",
31
- "-f",
32
- "fw_folder",
33
- type=click.Path(file_okay=False, dir_okay=True, path_type=Path),
34
- default=config.firmware_folder,
35
- show_default=True,
36
- help="The folder to retrieve the firmware from.",
37
- )
38
- @click.option(
39
- "--version",
40
- "-v",
41
- "version", # single version
42
- default="stable",
43
- multiple=False,
44
- show_default=True,
45
- help="The version of MicroPython to flash.",
46
- metavar="SEMVER, 'stable', 'preview' or '?'",
47
- )
48
- @click.option(
49
- "--serial",
50
- "--serial-port",
51
- "-s",
52
- "serial",
53
- default=["*"],
54
- multiple=True,
55
- show_default=True,
56
- help="Which serial port(s) (or globs) to flash",
57
- metavar="SERIALPORT",
58
- )
59
- @click.option(
60
- "--ignore",
61
- "-i",
62
- is_eager=True,
63
- help="Serial port(s) to ignore. Defaults to MPFLASH_IGNORE.",
64
- multiple=True,
65
- default=[],
66
- envvar="MPFLASH_IGNORE",
67
- show_default=True,
68
- metavar="SERIALPORT",
69
- )
70
- @click.option(
71
- "--bluetooth/--no-bluetooth",
72
- "-b/-nb",
73
- is_flag=True,
74
- default=False,
75
- show_default=True,
76
- help="""Include bluetooth ports in the list""",
77
- )
78
- @click.option(
79
- "--port",
80
- "-p",
81
- "ports",
82
- help="The MicroPython port to flash",
83
- metavar="PORT",
84
- default=[],
85
- multiple=True,
86
- )
87
- @click.option(
88
- "--board",
89
- "-b",
90
- "board", # single board
91
- multiple=False,
92
- help="The MicroPython board ID to flash. If not specified will try to read the BOARD_ID from the connected MCU.",
93
- metavar="BOARD_ID or ?",
94
- )
95
- @click.option(
96
- "--cpu",
97
- "--chip",
98
- "-c",
99
- "cpu",
100
- help="The CPU type to flash. If not specified will try to read the CPU from the connected MCU.",
101
- metavar="CPU",
102
- )
103
- @click.option(
104
- "--erase/--no-erase",
105
- default=True,
106
- show_default=True,
107
- help="""Erase flash before writing new firmware. (Not supported on UF2 boards)""",
108
- )
109
- @click.option(
110
- "--bootloader",
111
- "-bl",
112
- "bootloader",
113
- type=click.Choice([e.value for e in BootloaderMethod]),
114
- default="auto",
115
- show_default=True,
116
- help="""How to enter the (MicroPython) bootloader before flashing.""",
117
- )
118
- def cli_flash_board(**kwargs) -> int:
119
- # version to versions, board to boards
120
- kwargs["versions"] = [kwargs.pop("version")] if kwargs["version"] != None else []
121
- if kwargs["board"] is None:
122
- kwargs["boards"] = []
123
- kwargs.pop("board")
124
- else:
125
- kwargs["boards"] = [kwargs.pop("board")]
126
-
127
- params = FlashParams(**kwargs)
128
- params.versions = list(params.versions)
129
- params.ports = list(params.ports)
130
- params.boards = list(params.boards)
131
- params.serial = list(params.serial)
132
- params.ignore = list(params.ignore)
133
- params.bootloader = BootloaderMethod(params.bootloader)
134
-
135
- # make it simple for the user to flash one board by asking for the serial port if not specified
136
- if params.boards == ["?"] and params.serial == "*":
137
- params.serial = ["?"]
138
-
139
- # Detect connected boards if not specified,
140
- # and ask for input if boards cannot be detected
141
- all_boards: List[MPRemoteBoard] = []
142
- if not params.boards:
143
- # nothing specified - detect connected boards
144
- params.ports, params.boards, all_boards = connected_ports_boards(include=params.ports, ignore=params.ignore, bluetooth=params.bluetooth)
145
- if params.boards == []:
146
- # No MicroPython boards detected, but it could be unflashed or in bootloader mode
147
- # Ask for serial port and board_id to flash
148
- params.serial = ["?"]
149
- params.boards = ["?"]
150
- # assume manual mode if no board is detected
151
- params.bootloader = BootloaderMethod("manual")
152
- else:
153
- resolve_board_ids(params)
154
-
155
- # Ask for missing input if needed
156
- params = ask_missing_params(params)
157
- if not params: # Cancelled by user
158
- return 2
159
- # TODO: Just in time Download of firmware
160
-
161
- assert isinstance(params, FlashParams)
162
-
163
- if len(params.versions) > 1:
164
- log.error(f"Only one version can be flashed at a time, not {params.versions}")
165
- raise MPFlashError("Only one version can be flashed at a time")
166
-
167
- params.versions = [clean_version(v) for v in params.versions]
168
- worklist: WorkList = []
169
- # if serial port == auto and there are one or more specified/detected boards
170
- if params.serial == ["*"] and params.boards:
171
- if not all_boards:
172
- log.trace("No boards detected yet, scanning for connected boards")
173
- _, _, all_boards = connected_ports_boards(include=params.ports, ignore=params.ignore)
174
- worklist = full_auto_worklist(
175
- all_boards=all_boards,
176
- version=params.versions[0],
177
- fw_folder=params.fw_folder,
178
- include=params.serial,
179
- ignore=params.ignore,
180
- )
181
- elif params.versions[0] and params.boards[0] and params.serial:
182
- # A one or more serial port including the board / variant
183
- worklist = manual_worklist(
184
- params.serial[0],
185
- board_id=params.boards[0],
186
- version=params.versions[0],
187
- fw_folder=params.fw_folder,
188
- )
189
- else:
190
- # just this serial port on auto
191
- worklist = single_auto_worklist(
192
- serial=params.serial[0],
193
- version=params.versions[0],
194
- fw_folder=params.fw_folder,
195
- )
196
-
197
- if flashed := flash_list(
198
- worklist,
199
- params.fw_folder,
200
- params.erase,
201
- params.bootloader,
202
- ):
203
- log.info(f"Flashed {len(flashed)} boards")
204
- show_mcus(flashed, title="Updated boards after flashing")
205
- return 0
206
- else:
207
- log.error("No boards were flashed")
208
- return 1
209
-
210
-
211
- def resolve_board_ids(params: Params):
212
- """Resolve board descriptions to board_id, and remove empty strings from list of boards"""
213
- for board_id in params.boards:
214
- if board_id == "":
215
- params.boards.remove(board_id)
216
- continue
217
- if " " in board_id:
218
- try:
219
- if info := find_known_board(board_id):
220
- log.info(f"Resolved board description: {info.board_id}")
221
- params.boards.remove(board_id)
222
- params.boards.append(info.board_id)
223
- except Exception as e:
224
- log.warning(f"Unable to resolve board description: {e}")