mpflash 1.0.1__py3-none-any.whl → 1.0.2__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 (52) hide show
  1. mpflash/add_firmware.py +98 -98
  2. mpflash/ask_input.py +236 -236
  3. mpflash/basicgit.py +284 -284
  4. mpflash/bootloader/__init__.py +2 -2
  5. mpflash/bootloader/activate.py +60 -60
  6. mpflash/bootloader/detect.py +82 -82
  7. mpflash/bootloader/manual.py +101 -101
  8. mpflash/bootloader/micropython.py +12 -12
  9. mpflash/bootloader/touch1200.py +36 -36
  10. mpflash/cli_download.py +129 -129
  11. mpflash/cli_flash.py +224 -216
  12. mpflash/cli_group.py +111 -111
  13. mpflash/cli_list.py +87 -87
  14. mpflash/cli_main.py +39 -39
  15. mpflash/common.py +210 -177
  16. mpflash/config.py +44 -44
  17. mpflash/connected.py +104 -77
  18. mpflash/download.py +364 -364
  19. mpflash/downloaded.py +130 -130
  20. mpflash/errors.py +9 -9
  21. mpflash/flash/__init__.py +55 -55
  22. mpflash/flash/esp.py +59 -59
  23. mpflash/flash/stm32.py +19 -19
  24. mpflash/flash/stm32_dfu.py +104 -104
  25. mpflash/flash/uf2/__init__.py +88 -88
  26. mpflash/flash/uf2/boardid.py +15 -15
  27. mpflash/flash/uf2/linux.py +136 -130
  28. mpflash/flash/uf2/macos.py +42 -42
  29. mpflash/flash/uf2/uf2disk.py +12 -12
  30. mpflash/flash/uf2/windows.py +43 -43
  31. mpflash/flash/worklist.py +170 -170
  32. mpflash/list.py +106 -106
  33. mpflash/logger.py +41 -41
  34. mpflash/mpboard_id/__init__.py +93 -93
  35. mpflash/mpboard_id/add_boards.py +251 -251
  36. mpflash/mpboard_id/board.py +37 -37
  37. mpflash/mpboard_id/board_id.py +86 -86
  38. mpflash/mpboard_id/store.py +43 -43
  39. mpflash/mpremoteboard/__init__.py +266 -266
  40. mpflash/mpremoteboard/mpy_fw_info.py +141 -141
  41. mpflash/mpremoteboard/runner.py +140 -140
  42. mpflash/vendor/click_aliases.py +91 -91
  43. mpflash/vendor/dfu.py +165 -165
  44. mpflash/vendor/pydfu.py +605 -605
  45. mpflash/vendor/readme.md +2 -2
  46. mpflash/versions.py +135 -135
  47. {mpflash-1.0.1.dist-info → mpflash-1.0.2.dist-info}/LICENSE +20 -20
  48. {mpflash-1.0.1.dist-info → mpflash-1.0.2.dist-info}/METADATA +1 -1
  49. mpflash-1.0.2.dist-info/RECORD +53 -0
  50. mpflash-1.0.1.dist-info/RECORD +0 -53
  51. {mpflash-1.0.1.dist-info → mpflash-1.0.2.dist-info}/WHEEL +0 -0
  52. {mpflash-1.0.1.dist-info → mpflash-1.0.2.dist-info}/entry_points.txt +0 -0
@@ -1,60 +1,60 @@
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
+ 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 +1,82 @@
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
+ """ 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 +1,101 @@
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
+ """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 +1,12 @@
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
+ """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 +1,36 @@
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
+ """
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