summaryrefslogtreecommitdiff
path: root/tools/pyboard.py
AgeCommit message (Collapse)Author
2025-07-23tools/pyboard.py: Add timeout argument to Pyboard.exec_/exec.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2025-07-20tools/pyboard.py: Align execpty prefix.Yanfeng Liu
This aligns the prefix string in L285 to that in L284 though the two strings have equal length. Signed-off-by: Yanfeng Liu <yfliu2008@qq.com>
2025-06-02tools/pyboard.py: Add write_timeout and catch errors in enter_raw_repl.Damien George
If the USB serial device locks up, then writes to that device can hang forever. That can make the test runner `tests/run-tests.py` lock up, among other problems. This commit introduces a 5 second write-timeout, and catches any OSError's raised during `enter_raw_repl()`. Now, if a USB serial device locks up, `enter_raw_repl()` will eventually raise an exception. Signed-off-by: Damien George <damien@micropython.org>
2025-06-02tools/pyboard.py: Introduce timeout_overall for read_until().Damien George
This applies the mpremote commit 03fe9c55ea8a3ef8bede5547476f37d7fca20119 to pyboard.py. The `timeout_overall` is used in `enter_raw_repl()`. It prevents waiting forever for a serial device that does not respond to the Ctrl-C/Ctrl-D/etc commands and is constantly outputting data. Signed-off-by: Damien George <damien@micropython.org>
2025-06-02tools/pyboard.py: Avoid initial blocking read in read_until().Damien George
This applies the mpremote commit 0d46e45a1f72ee61a153c41aaaf6c63818ccffb0 to pyboard.py. If the target does not return any data then `read_until()` will block indefinitely. Fix this by making the initial read part of the general read look, which always checks `inWaiting() > 0` before reading from the serial device. Also added the UART timeout to the constructor. This is not currently used but may be used as an additional safeguard. Signed-off-by: Damien George <damien@micropython.org>
2025-02-25all: Upgrade to ruff v0.9.6.Christian Clauss
Signed-off-by: Christian Clauss <cclauss@me.com>
2025-02-12tools/pyboard.py: Make get_time use machine.RTC instead of pyb.RTC.rufusclark
The current code evaluates `pyb.RTC().datetime()` resulting in a remote side exception, as `pyb` is not defined on most ports (only stm32). The code should evaluate `machine.RTC().datetime()` and hence return the current time. Signed-off-by: rufusclark <50201718+rufusclark@users.noreply.github.com> Signed-off-by: Damien George <damien@micropython.org>
2025-01-16tools/pyboard.py: Wait a bit before accessing the PTY serial port.Alessandro Gatti
Some PTY targets, namely `NETDUINO2` and `MICROBIT` under Qemu, take a bit more time to present a REPL than usual. The pyboard tool is a bit too impatient and would bail out before any of those targets had a chance to respond to the raw REPL request. Co-authored-by: Damien George <damien@micropython.org> Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2024-10-15tools: Only issue a single Ctrl-C when entering raw REPL.Damien George
A long time ago when there was only the `stm` port, Ctrl-C would trigger a preemptive NLR jump to break out of running code. Then in commit 124df6f8d07b53542b6960dbeea9b63bff469a67 a more general approach to asynchronous `KeyboardInterrupt` exceptions was implemented, and `stmhal` supported both approaches, with the general (soft) interrupt taking priority. Then in commit bc1488a05f509cd5be8bfab9574babfcb993806f `pyboard.py` was updated with a corresponding change to make it issue a double Ctrl-C to break out of any existing code when entering the raw REPL (two Ctrl-C characters were sent in order to more reliably trigger the preemptive NLR jump). No other port has preemptive NLR jumps and so a double Ctrl-C doesn't really behave any differently to a single Ctrl-C: with USB CDC the double Ctrl-C would most likely be in the same USB packet and so processed in the same low-level USB callback, so it's just setting the keyboard interrupt flag twice in a row. The VM/runtime then just sees one keyboard interrupt and acts as though only one Ctrl-C was sent. This commit changes the double Ctrl-C to a single Ctrl-C in `pyboard.py` and `mpremote`. That keeps things as simple as they need to be. Signed-off-by: Damien George <damien@micropython.org>
2024-08-28tools/pyboard.py: Capture stdout for pts line.Damien George
The pts line printed by qemu-system-arm goes to stdout, not stderr. Redirect stderr to stdout in case other tools do print to stderr. Signed-off-by: Damien George <damien@micropython.org>
2023-11-03all: Update Python formatting to ruff-format.Jim Mussared
This updates a small number of files that change with ruff-format's (vs black's) rules. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-06-08all: Replace all uses of umodule in Python code.Jim Mussared
Applies to drivers/examples/extmod/port-modules/tools. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-06-02tools/mpremote: Handle `cp` without destination.Jim Mussared
This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-05-09tools/pyboard.py: Import errno to fix undefined name in PyboardError.Christian Clauss
This will keep line 96 from raising a NameError. Signed-off-by: Christian Clauss <cclauss@me.com>
2023-05-04tools/pyboard.py: Import serial.tools.list_ports.Damien George
This import is needed by newer versions of pyserial. Signed-off-by: Damien George <damien@micropython.org>
2023-04-27tools/pyboard.py: Rename ProcessPtyToTerminal member "ser" to "serial".Damien George
So that this file doesn't need to be excluded from codespell. Signed-off-by: Damien George <damien@micropython.org>
2023-04-04tools/pyboard.py: Fix ESPxx boards hanging in bootloader after reset.Jos Verlinde
This is a follow up to d263438a6e365d3199494498a9b734cda29dde52, which solved one problem (reset on disconnect) but introduced a second one (hang in bootloader). To solve both probles, False/False is needed for DTR/RTS for ESPxx, but that would then block stm32 and others. Any unconditional combination of DTR/RTS ends up blocking normal operation on some type of board or another. A simple overview (for windows only): DTR CTS ESP8266/ESP32 STM32/SAMD51/RP2040 unspecified unspecified Reset on disconnect OK True False Hang in bootloader OK False False OK No Repl True True Reset on disconnect No Repl False True Reset on disconnect No Repl serial.manufacturer: wch.cn/Silicon Labs Microsoft serial.description: USB-SERIAL CH340 / USB Serial Device CP210x USB to UART Bridge The updated logic will only set the DTR/RTS signals for boards that do not use standard Microsoft drivers (based on the manufacturer). It would also be possible to check against a list of known driver manufactures (like wch.cn or Silicon Labs) but this would require a list of known drivers for all ports. Signed-off-by: Jos Verlinde <jos_verlinde@hotmail.com>
2023-03-22tools/pyboard.py: Fix joining of path in filesystem_command.Damien George
This was broken by 5327cd1021dc92cad428ff44cb114c4a94c0bc45 Signed-off-by: Damien George <damien@micropython.org>
2023-02-24tools/pyboard.py: Use '/' exclusively when dealing with paths.Damien George
Currently, certain mpremote filesystem operations can fail on Windows due to a mixing of '/' and '\' for path separators. Eg if filesystem_command() is called with a destination that ends in / then dest.endswith(os.path.sep) will return False, which gives the wrong behaviour (it does end in a path separator). For similar reasons to 7e9a15966acf80ff50fdf5c52553dd56de164bb3, it's best to use '/' everywhere in pyboard.py and mpremote, because the target device understands only '/'. mpremote already does this, so the remaining place to fix it is in pyboard.y, to convert all incoming paths to use '/' instead of '\'. This effectively reverts 57fd66b80f8352e4859e6b71536b6083f9d7279c which tried to fix the problem in a different way. See also related 1f84440538a017e463aaad9686831ce9527122b5. Signed-off-by: Damien George <damien@micropython.org>
2023-01-13tools/pyboard.py: Set DTR on Windows to avoid ESPxx hard reset.Jos Verlinde
Fixes issue #9659. Signed-off-by: Jos Verlinde <Jos.Verlinde@Microsoft.com>
2023-01-13tools/pyboard.py: Add fs_{listdir,readfile,writefile,stat}.Jim Mussared
These are for working with the filesystem when using pyboard.py as a library, rather than at the command line. - fs_listdir returns a list of tuples, in the same format as os.ilistdir(). - fs_readfile returns the contents of a file as a bytes object. - fs_writefile allows writing a bytes object to a file. - fs_stat returns an os.statresult. All raise FileNotFoundError (or OSError(ENOENT) on Python 2) if the file is not found (or PyboardError on other errors). Updated fs_cp and fs_get to use fs_stat to compute file size. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-01-13tools/pyboard.py: Add parse kwarg to eval.Jim Mussared
This is useful when using pyboard.py as a library rather than at the command line. pyb.eval("1+1") --> b"2" pyb.eval("{'a': '\x00'}") --> b"{'a': '\\x00'}" Now you can also do pyb.eval("1+1", parse=True) --> 2 pyb.eval("{'a': '\x00'}", parse=True) --> {'a': '\x00'} This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-01-13tools/pyboard.py: Fix Python 2 compatibility.Jim Mussared
In Python 2, serial.read()[0] a string, not int. Use struct.unpack to do this instead. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-10-01tools/pyboard.py: Handle unsupported fs command.Jim Mussared
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-10-01tools/mpremote: Add `mpremote mip install` to install packages.Jim Mussared
This supports the same package sources as the new `mip` tool. - micropython-lib (by name) - http(s) & github packages with json description - directly downloading a .py/.mpy file The version is specified with an optional `@version` on the end of the package name. The target dir, index, and mpy/no-mpy can be set through command line args. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-13tools/pyboard.py: Support Windows pathname separators.Wind-stormger
Addresses issue #9132.
2022-08-26tools/pyboard.py: Add fs_cp function for direct device-to-device copy.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2022-08-26tools/pyboard.py: Remove implicit fs_put if source starts with ./.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2022-08-18tools/pyboard.py: Add "touch" filesystem command.Jim Mussared
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-07-08tools/pyboard.py: Add verbose option to filesystem_command.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2022-04-11tools/mpremote: Show progress indicator when copying large files.Rob Knegjens
When copying large files (> 2048 bytes) to or from a device with `mpremote cp` a progress bar and percentage counter are temporarily shown.
2021-08-25tools/pyboard.py: Make --no-soft-reset consistent with other args.Jim Mussared
This makes it work like --no-follow and --no-exclusive using a mutex group and dest. Although the current implementation with BooleanOptionAction is neater it requires Python 3.9, so don't use this feature. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-08-25tools/pyboard.py: Add --exclusive to match --no-exclusive.Jim Mussared
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-08-25tools/pyboard.py: Make --no-follow use same variable as --follow.Jim Mussared
You can set one or the other (or neither) but not both. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-08-25tools/pyboard.py: Move --no-exclusive/--soft-reset out of mutex group.Jim Mussared
The --no-exclusive flag was accidentally added to the mutex group in 178198a01df51b5f4c5ef9f38ab2fb8f6269d5f4. The --soft-reset flag was accidentally added to the mutex group in 41adf178309759d5965c15972f04987a2635314c. These flags can be specified independently to --[no-]follow so should not be in that mutex group. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-07-01tools/pyboard.py: Add cmd-line option to make soft reset configurable.Frank Pilhofer
Leaves the default as-is, but allows using --no-soft-reset to disable the soft reset when connecting.
2021-05-29tools/pyboard.py: Add "soft_reset" option to Pyboard.enter_raw_repl().Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-05-29tools/pyboard.py: Track raw REPL state via in_raw_repl variable.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-04-23tools/pyboard.py: Support opening serial port in exclusive mode.Damien George
This is now the default, but can be overridden with CLI `--no-exclusive`, or constructing `Pyboard(..., exclusive=False)`. Signed-off-by: Damien George <damien@micropython.org>
2021-02-13tools: Add filesystem action examples to pyboard.py help.Brianna Laugher
Signed-off-by: Brianna Laugher <brianna.laugher@gmail.com>
2020-12-01tools/pyboard.py: Add fast raw-paste mode.Damien George
This commit adds support to pyboard.py for the new raw REPL paste mode. Note that this new pyboard.py is fully backwards compatible with old devices (it detects if the device supports the new raw REPL paste mode). Signed-off-by: Damien George <damien@micropython.org>
2020-08-21tools/pyboard.py: Replace eval() of received data with alternative.Michael Buesch
Prior to this commit, pyboard.py used eval() to "parse" file data received from the board. Using eval() on received data from a device is dangerous, because a malicious device may inject arbitrary code execution on the PC that is doing the operation. Consider the following scenario: Eve may write a malicious script to Bob's board in his absence. On return Bob notices that something is wrong with the board, because it doesn't work as expected anymore. He wants to read out boot.py (or any other file) to see what is wrong. What he gets is a remote code execution on his PC. Proof of concept: Eve: $ cat boot.py _print = print print = lambda *x, **y: _print("os.system('ls /; echo Pwned!')", end="\r\n\x04") $ ./pyboard.py -f cp boot.py : cp boot.py :boot.py Bob: $ ./pyboard.py -f cp :boot.py /tmp/foo cp :boot.py /tmp/foo bin chroot dev home lib32 media opt root sbin sys usr boot config etc lib lib64 mnt proc run srv tmp var Pwned! There's also the possibility that the device is malfunctioning and sends random and possibly dangerous data back to the PC, to be eval'd. Fix this problem by using ast.literal_eval() to parse the received bytes, instead of eval(). Signed-off-by: Michael Buesch <m@bues.ch>
2020-03-30tools/pyboard.py: Add -d as an alias for --device.Lars Kellogg-Stedman
2020-03-30tools/pyboard.py: Support setting device/baudrate from shell env vars.Lars Kellogg-Stedman
Allow defaults for --device and --baudrate to be set in the environment using PYBOARD_DEVICE and PYBOARD_BAUDRATE.
2020-02-28all: Reformat C and Python source code with tools/codeformat.py.Damien George
This is run with uncrustify 0.70.1, and black 19.10b0.
2020-02-01tools/pyboard.py: Change shebang to use python3.Michael Buesch
This script still works with Python 2 but Python 3 is recommended.
2020-02-01tools/pyboard.py: Use slice del instead of list.clear() for Py2 compat.Michael Buesch
Python 2 does not have list.clear().
2020-02-01tools/pyboard.py: Add option --no-follow to detach after sending script.Michael Buesch
This option makes pyboard.py exit as soon as the script/command is successfully sent to the device, ie it does not wait for any output. This can help to avoid hangs if the board is being rebooted with --comman (for example). Example usage: $ python3 ./tools/pyboard.py --device /dev/ttyUSB0 --no-follow \ --command 'import machine; machine.reset()'
2019-12-19tools/pyboard.py: Support executing .mpy files directly.Damien George
This patch allows executing .mpy files (including native ones) directly on a target, eg a board over a serial connection. So there's no need to copy the file to its filesystem to test it. For example: $ mpy-cross foo.py $ pyboard.py foo.mpy
2019-07-25tools/pyboard.py: Add filesystem commands to ls/cat/cp/rm remote files.Damien George
Use "-f" to select filesystem mode, followed by the command to execute. Optionally put ":" at the start of a filename to indicate that it's on the remote device, if it would otherwise be ambiguous. Examples: $ pyboard.py -f ls $ pyboard.py -f cat main.py $ pyboard.py -f cp :main.py . # get from device $ pyboard.py -f cp main.py : # put to device $ pyboard.py -f rm main.py