# MicroPython Studio — Live MicroPython Debugger for VS Code

**Set breakpoints on a real Raspberry Pi Pico 2 W, step through MicroPython code, inspect locals and call stacks, and capture live runtime traces.** MicroPython Studio also provides a REPL, device file manager, firmware tools, CircuitPython support and an optional local AI assistant.

[**Install from VS Code Marketplace**](https://marketplace.visualstudio.com/items?itemName=niwantha33.micropython-studio) · [**Watch the real-device debugger demo**](https://www.youtube.com/watch?v=or_aG-Rhnb8) · [**Read the debugger guide**](https://niwantha33.github.io/micropython-studio/micropython-live-debugger.html)

[![MicroPython](https://img.shields.io/badge/MicroPython-1.20%2B-blue?logo=micropython&logoColor=white)](https://micropython.org)
[![CircuitPython](https://img.shields.io/badge/CircuitPython-10.x-purple?logo=adafruit&logoColor=white)](https://circuitpython.org)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE.md)

## Real debugging on real hardware — 2.7.0

![MicroPython Studio native VS Code debugging on Pico 2 W, paused at a source breakpoint with locals, call stack, MCU Project, MCU Runtime and REPL output](resource/mps_v270_debugger_main.png)

*Actual 2.7.0 development build: Pico 2 W, native Run and Debug with separate REPL and debugger CDC ports.*

![MicroPython Studio debugger details showing recorded RTA task and function timing, debug console, verified breakpoint and REPL output](resource/mps_v270_debugger_rta.png)

*Actual 2.7.0 development build: captured RTA statistics after tracing was switched off. Invalid trace events are reported, not counted as valid measurements.*

Physical hardware qualification passed on a dedicated RP2350 test board with compatible scope/step firmware, using the 100-cycle configuration and separate REPL/debug CDC ports. The reported checks covered raw execution, streaming and Ctrl+C recovery, COM handoff, heap sampling, GC recovery, file transfer integrity, breakpoint execution, paused locals/globals, stepping, RTA events and debugger reattach. This is evidence for the tested build and board, not blanket approval of every firmware, target or final release commit.

## Native VS Code debugging and memory diagnostics

- **MCU Project and MCU Runtime:** project-scoped board and saved port details, passive connection state and timestamped heap snapshots from the existing analyzer. Refreshing these views does not probe the MCU. Third-party Python views can be hidden manually.

- **Native Run and Debug:** use VS Code F5 / Run and Debug with MicroPython Studio's Debug Adapter Protocol (DAP) integration. Standard Breakpoints, Variables, Watch (read-only lookup), Call Stack, and stepping connect to the **existing frozen-firmware debugger bridge**, not a second serial transport. Source frames are displayed only when mapping is available. The existing RTA webview remains available while a native session runs.
- **Memory & Garbage Collection Analyzer:** open `MicroPython: Memory & Garbage Collection Analyzer` or the **Memory & GC** status-bar item. **Sample heap** reads `gc.mem_alloc()` and `gc.mem_free()` without forcing garbage collection. **Collect GC + compare** explicitly runs `gc.collect()`, reports reclaimed bytes and measured collection duration when supported. Export snapshots to CSV, compare observed peaks and growth.
- **Safe measurements:** the Memory Analyzer uses an already-connected, idle USB REPL and refuses to create a competing COM connection. Do not sample a busy application; evaluating code via REPL may interrupt user code. Sampling during a native debug session requires explicitly stopping the session first. Heap totals reflect the GC-managed heap, not every RAM allocation or physical memory bank. Rising samples alone do not establish a memory leak.

### Start native debugging

1. Check the [board-specific debugger firmware matrix](https://niwantha33.github.io/micropython-studio/board-support.html) and ensure compatible firmware is already installed on the exact board.
2. Connect the board's **REPL/upload COM port** normally. Select **Start Debug**, or use F5 and choose **MicroPython Studio — Native Debugger**.
3. When prompted, select the **separate debugger CDC/COM port**. The native debugger only attaches to the frozen debugger; it never uploads Python helper files, overwrites `boot.py`, or flashes firmware.
4. Set a source breakpoint in a `.py` function. When the firmware confirms its slot, the native Breakpoints pane marks it verified. Use VS Code stepping and inspect Variables/Call Stack. Unmapped source locations remain labelled as such instead of being guessed.
5. To use the classic debugger/RTA panel by itself, run `MicroPython: Open Legacy Debug & RTA Panel`. Close any existing debug connection before switching modes.

Detailed workflow: [Native DAP and Memory Analyzer guide](docs/native-dap-memory.md).

---

## See what makes the debugger different

On compatible **debugger-enabled firmware**, MicroPython Studio provides source breakpoints, step/continue controls, local/global variable inspection, call stacks and live RTA function tracing, without requiring a separate JTAG probe for those software-debugging features. The [demo](https://www.youtube.com/watch?v=or_aG-Rhnb8) shows the debugger operating on a real Pico 2 W.

**Hardware-support status matters.** The existing Pico 2 W debugger workflow has been exercised on real hardware. The new Pico-family frozen-firmware builds and ESP32-S3 debugger improvements are under separate hardware qualification; an ESP32-C3 debugger has **not** been released. Do not interpret a successful CI build as evidence that your board is supported. Read the [board-specific firmware and test status](https://github.com/niwantha33/micropython_live_dbg_firmware#new-self-contained-firmware--test-only-not-in-this-repository) before flashing anything.

> **Experimental debugger firmware — for debugging and testing your own code ONLY. NOT FOR PRODUCTION USE.** This warning applies to unvalidated firmware in the [TestBuilds download directory](https://github.com/niwantha33/micropython_live_dbg_firmware/tree/main/TestBuilds), not to an approved, board-specific stable release. A successful CI build does not mean the image is hardware-approved. Verify the exact board model, back up your files and follow the matching firmware instructions before flashing. **Download Debug Firmware** opens the file or download page; Studio does **not** automatically flash the board or overwrite `boot.py`.

## Why Use It?

| Need | MicroPython Studio Helps With |
|------|-------------------------------|
| Start quickly | Project wizard, environment setup, board config, templates |
| Work with real boards | Serial REPL, file explorer, upload/download, package install |
| Debug harder problems | Breakpoints, stepping, locals, call stack, debug firmware flow |
| Test without hardware | QEMU-based simulator for fast prototype runs |
| Use wireless workflows | MicroPython WebREPL and CircuitPython Web Workflow support |
| Keep code private | Optional local AI assistant powered by Ollama |

---

## Main Features

- **Device File Explorer**
  Browse, upload, download, rename, delete, and create files on your board.

- **One-Click Run and Shell**
  Run the active Python file or open an interactive REPL from VS Code.

- **Live Debugger**
  Set breakpoints, step through code, inspect locals, and view call stack data.

- **CircuitPython Support**
  Detect CIRCUITPY drives, install libraries, sync files, and use Web Workflow.

- **MicroPython WebREPL**
  Configure Wi-Fi access and connect to supported boards over the network.

- **Firmware Tools**
  Download and flash firmware for supported targets, including debug firmware.

- **QEMU Simulator (temporarily disabled in security-hardening preview)**
  Requires verified, SHA-256-pinned QEMU and firmware artifacts before running on the host.

- **Private AI Assistant**
  Use local Ollama models for code help and device-error investigation.

---

## Quick Start

1. Install the extension in VS Code.
2. Run **MicroPython: Setup Development Environment**.
3. Connect your board by USB.
4. Run **MicroPython: Refresh Device Files**.
5. Create or open a project.
6. Press **Run** to execute your active Python file.

That is the normal daily loop: connect, edit, run, inspect files, repeat.

---

## Common Commands

| Command | What It Does |
|---------|--------------|
| `MicroPython: Setup Development Environment` | Creates the Python environment and installs required tools |
| `MicroPython: Create New Project` | Starts a guided project setup |
| `MicroPython: Open Existing Project` | Opens a project with existing device configuration |
| `MicroPython: Refresh Device Files` | Connects to the board and refreshes the device file tree |
| `MicroPython: Run Script on Device` | Runs the active Python file |
| `MicroPython: Open Device Shell` | Opens an interactive REPL |
| `MicroPython: Upload File to Device` | Copies one file to the board |
| `MicroPython: Upload Project to Device` | Copies the project to the board |
| `MicroPython: Start Debug` | Starts the live debugger setup/connect flow |
| `MicroPython: Flash Firmware` | Opens firmware flashing tools |

---

## Debugging

The debugger is designed for embedded Python workflows where JTAG is not always practical.

It supports:

- breakpoints
- stepping
- locals inspection
- call stack view
- runtime trace events

- live function/runtime tracing (observed elapsed-time share, **not** CPU utilisation)
- debugger-enabled firmware for supported board-specific configurations; existing published Pico firmware may need the legacy setup steps
- test-only frozen debugger images are not published as stable releases until hardware validation


For best results, start with a simple script first, confirm upload/run works, then enable the debugger.

### Debugger USB connections

The debugger uses a **separate connection from the REPL**. For compatible debugger-enabled **test firmware**, the connection layout is:

| Board | Project REPL and file upload | Debugger + RTA | USB cables |
| --- | --- | --- | --- |
| Pico / Pico W / Pico 2 / Pico 2 W (frozen UF2 candidate) | CDC0 COM | CDC1 COM | One |
| ESP32-S3 test candidate | Physical Serial/JTAG COM | Physical native-USB debugger COM | Two |

Firmware must match the exact board. See [board support and firmware status](https://niwantha33.github.io/micropython-studio/board-support.html) for stable versus experimental builds and connection guidance.

---

## Simulator

**Security qualification status:** Automatic QEMU simulator startup is temporarily disabled. The previous simulator downloaded executable binaries and firmware from mutable URLs without verified hashes. It now refuses to run until `resource/simulator-manifest.json` contains qualified release artifacts and approved SHA-256 checksums. Semihosting is disabled.

After independently verified artifacts are published and approved, the intended workflow will be:

- Start or stop it from the status bar.
- Run code through the same VS Code workflow.
- Use local `lib/` folders for dependencies.
- The simulated filesystem is read-only, so direct uploads are blocked with a clear warning.

---

## Security and trusted-device guidance

- MicroPython WebREPL (ws://) and CircuitPython Web Workflow (HTTP) are not encrypted by their standard on-device protocols. Use them only on trusted, isolated networks or with a properly authenticated secure tunnel. Do not reuse your Wi-Fi password for the device REPL.
- Device passwords are stored using VS Code SecretStorage and are removed from legacy plaintext `device.cfg` entries on migration. Existing copies in git history, shared ZIP files or backups are **not** automatically erased: rotate any previously exposed password.
- New projects create a `.gitignore` excluding `device.cfg` and CircuitPython `settings.toml`. Existing projects must apply equivalent rules manually; ignoring a file does not remove it from past git commits.
- Native debugging runs code on your connected MCU; only attach boards and firmware you trust. Device-derived data and filenames are treated as untrusted by the extension.
- Simulator execution is intentionally blocked until signed or checksum-pinned dependencies are verified. Do not bypass this integrity check.

---

## Local AI Assistant

MicroPython Studio can use [Ollama](https://ollama.com) for private local AI help.

1. Install Ollama.
2. Open the MicroPython Studio AI view.
3. Let the extension prepare the local model.
4. Ask questions about your active file, device errors, or MicroPython APIs.

Your source code stays on your machine.

---

## Supported Workflows

| Platform | USB Serial | File Explorer | Packages | Wireless | Debugging |
|----------|------------|---------------|----------|----------|-----------|
| MicroPython | Yes | Yes | `mip` | WebREPL | Supported targets |
| CircuitPython | Yes | CIRCUITPY drive / Web Workflow | `circup` | Web Workflow | Limited |
| Digi XBee | Yes | Project/library support | Bundled helpers | Device dependent | Device dependent |
| Simulator (temporarily disabled) | Planned TCP REPL | Planned read-only target | Local `lib/` | Local only | Requires verified QEMU/firmware manifest |

---

## Project Status

MicroPython Studio is actively developed and already used by many embedded Python developers. The current focus is reliability: port locking, debugger setup, ESP32-S3 workflows, automated CI checks, and better regression testing.

If something breaks, please open an issue with:

- board name
- firmware type and version
- operating system
- selected port
- output from the **MicroPython IDE** or **MPy Debugger Setup** panel

---

## Useful Links

- [Project Website](https://niwantha33.github.io/micropython-studio/)
- [MicroPython Documentation](https://docs.micropython.org/)
- [CircuitPython Documentation](https://docs.circuitpython.org/)
- [Project Repository](https://github.com/niwantha33/micropython-studio)
- [Issue Tracker](https://github.com/niwantha33/micropython-studio/issues)
- [Contributing Guide](CONTRIBUTING.md)
- [Code of Conduct](CODE_OF_CONDUCT.md)
- [YouTube Channel](https://www.youtube.com/@NiwanthaDev)

---

## Acknowledgements

MicroPython Studio builds on excellent open source projects:

| Project | License | Used For |
|---------|---------|----------|
| [MicroPython](https://micropython.org/) | MIT | Runtime, REPL, tools |
| [mpremote](https://github.com/micropython/micropython/tree/master/tools/mpremote) | MIT | Device communication |
| [CircuitPython](https://github.com/adafruit/circuitpython) | MIT | CircuitPython support |
| [circup](https://github.com/adafruit/circup) | MIT | CircuitPython library management |
| [MicroPython WebREPL](https://github.com/micropython/webrepl) | MIT | Wireless REPL workflow |
| [websocket-client](https://github.com/websocket-client/websocket-client) | Apache 2.0 | WebREPL transport |
| [term.js](https://github.com/chjj/term.js) | MIT | Terminal UI |
| [FileSaver.js](https://github.com/eligrey/FileSaver.js) | MIT | Browser-side file save support |
| [xbee-micropython](https://github.com/digidotcom/xbee-micropython) | MIT | XBee stubs and helpers |
| [esptool](https://github.com/espressif/esptool) | GPL-2.0 | ESP firmware flashing |
| [pyserial](https://github.com/pyserial/pyserial) | BSD | Serial communication |
| [QEMU](https://www.qemu.org) | GPL-2.0 | Simulator backend |
| [Ollama](https://ollama.com) | MIT | Local AI runtime |
| [Gemma](https://ai.google.dev/gemma) | Gemma Terms | Local AI model option |

All bundled third-party files retain their original license headers.

---

## Support

Questions, bugs, and ideas are welcome.

- Email: niwantha33@gmail.com
- Issues: [github.com/niwantha33/micropython-studio/issues](https://github.com/niwantha33/micropython-studio/issues)
- YouTube: [youtube.com/@NiwanthaDev](https://www.youtube.com/@NiwanthaDev)

---

## License

MicroPython Studio is licensed under the [MIT License](LICENSE.md).

XBee MicroPython type hints and libraries are sourced from [Digi International's xbee-micropython](https://github.com/digidotcom/xbee-micropython), also under the MIT License.
