|
| 1 | +[](https://www.microchip.com) |
| 2 | + |
| 3 | +# pymcuprog - Python MCU programmer |
| 4 | +pymcuprog is a Python utility for programming various Microchip MCU devices using Microchip CMSIS-DAP based debuggers |
| 5 | + |
| 6 | +Install using pip from [pypi](https://pypi.org/project/pymcuprog): |
| 7 | +```bash |
| 8 | +pip install pymcuprog |
| 9 | +``` |
| 10 | + |
| 11 | +Browse source code on [github](https://github.com/microchip-pic-avr-tools/pymcuprog) |
| 12 | + |
| 13 | +Read API documentation on [github](https://microchip-pic-avr-tools.github.io/pymcuprog) |
| 14 | + |
| 15 | +Read the changelog on [github](https://github.com/microchip-pic-avr-tools/pymcuprog/blob/main/CHANGELOG.md) |
| 16 | + |
| 17 | +## Usage |
| 18 | +pymcuprog can be used as a command-line interface or a library |
| 19 | + |
| 20 | +### CLI help |
| 21 | +For more help with using pymcuprog CLI see [help](./help.md) |
| 22 | + |
| 23 | +### CLI examples |
| 24 | +When installed using pip, pymcuprog CLI is located in the Python scripts folder. |
| 25 | + |
| 26 | +Test connectivity by reading the device ID using Curiosity Nano: |
| 27 | +```bash |
| 28 | +pymcuprog ping |
| 29 | +``` |
| 30 | + |
| 31 | +Erase memories then write contents of an Intel(R) hex file to flash using Curiosity Nano (pymcuprog does NOT automatically erase before writing): |
| 32 | +```bash |
| 33 | +pymcuprog erase |
| 34 | +pymcuprog write -f app.hex |
| 35 | +``` |
| 36 | + |
| 37 | +Erase memories and write an Intel hex file (using the --erase switch): |
| 38 | +```bash |
| 39 | +pymcuprog write -f app.hex --erase |
| 40 | +``` |
| 41 | + |
| 42 | +Erase memories, write an Intel hex file and verify the content: |
| 43 | +```bash |
| 44 | +pymcuprog write -f app.hex --erase --verify |
| 45 | +``` |
| 46 | + |
| 47 | + |
| 48 | +### Serial port UPDI (pyupdi) |
| 49 | +The AVR UPDI interface implements a UART protocol, which means that it can be used by simply connecting TX and RX pins of a serial port together with the UPDI pin; with a series resistor (eg: 1k) between TX and UPDI to handle contention. (This configuration is also known as "pyupdi".) Be sure to connect a common ground, and use a TTL serial adapter running at the same voltage as the AVR device. |
| 50 | + |
| 51 | +<pre> |
| 52 | + Vcc Vcc |
| 53 | + +-+ +-+ |
| 54 | + | | |
| 55 | + +---------------------+ | | +--------------------+ |
| 56 | + | Serial port +-+ +-+ AVR device | |
| 57 | + | | +----------+ | | |
| 58 | + | TX +------+ 1k +---------+ UPDI | |
| 59 | + | | +----------+ | | | |
| 60 | + | | | | | |
| 61 | + | RX +----------------------+ | | |
| 62 | + | | | | |
| 63 | + | +--+ +--+ | |
| 64 | + +---------------------+ | | +--------------------+ |
| 65 | + +-+ +-+ |
| 66 | + GND GND |
| 67 | +</pre> |
| 68 | + |
| 69 | +pymcuprog includes this implementation as an alternative to USB/EDBG-based tools. To connect via a serial port, use the "uart" tool type with the UART switch in addition. |
| 70 | + |
| 71 | +Example: checks connectivity by reading the device identity |
| 72 | +```bash |
| 73 | +pymcuprog ping -d avr128da48 -t uart -u com35 |
| 74 | +``` |
| 75 | + |
| 76 | +When using serial port UPDI it is optional to use: |
| 77 | +* --clk BAUD to specify the baud rate (defaults to 115200) |
| 78 | +* --uart-timeout TIMEOUT to specify the uart read timeout (defaults to 1.0s) |
| 79 | + |
| 80 | +Increasing the baud rate can decrease programming time. Decreasing the timeout can decrease the initial connection latency when UPDI is disabled and does not respond. These parameters can be tweaked to suit the serial port adapter in use. |
| 81 | + |
| 82 | +For more examples see [pymcuprog on pypi.org](https://pypi.org/project/pymcuprog/) |
| 83 | + |
| 84 | +### Library usage example |
| 85 | +pymcuprog can be used as a library using its backend API. For example: |
| 86 | +```python |
| 87 | +""" |
| 88 | +Example usage of pymcuprog as a library to read the device ID |
| 89 | +""" |
| 90 | +# pymcuprog uses the Python logging module |
| 91 | +import logging |
| 92 | +logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) |
| 93 | + |
| 94 | +# Configure the session |
| 95 | +from pymcuprog.backend import SessionConfig |
| 96 | +sessionconfig = SessionConfig("atmega4808") |
| 97 | + |
| 98 | +# Instantiate USB transport (only 1 tool connected) |
| 99 | +from pymcuprog.toolconnection import ToolUsbHidConnection |
| 100 | +transport = ToolUsbHidConnection() |
| 101 | + |
| 102 | +# Instantiate backend |
| 103 | +from pymcuprog.backend import Backend |
| 104 | +backend = Backend() |
| 105 | + |
| 106 | +# Connect to tool using transport |
| 107 | +backend.connect_to_tool(transport) |
| 108 | + |
| 109 | +# Start the session |
| 110 | +backend.start_session(sessionconfig) |
| 111 | + |
| 112 | +# Read the target device_id |
| 113 | +device_id = backend.read_device_id() |
| 114 | +print ("Device ID is {0:06X}".format(int.from_bytes(device_id, byteorder="little"))) |
| 115 | +``` |
| 116 | + |
| 117 | +## Supported devices and tools |
| 118 | +pymcuprog is primarily intended for use with PKOB nano (nEDBG) debuggers which are found on Curiosity Nano kits and other development boards. This means that it is continuously tested with a selection of AVR devices with UPDI interface as well as a selection of PIC devices. However since the protocol is compatible between all EDBG-based debuggers (pyedbglib) it is possible to use pymcuprog with a wide range of debuggers and devices, although not all device families/interfaces have been implemented. |
| 119 | + |
| 120 | +### Debuggers / Tools |
| 121 | +pymcuprog supports: |
| 122 | +* PKOB nano (nEDBG) - on-board debugger on Curiosity Nano |
| 123 | +* MPLAB PICkit 4 In-Circuit Debugger (when in 'AVR mode') |
| 124 | +* MPLAB Snap In-Circuit Debugger (when in 'AVR mode') |
| 125 | +* Atmel-ICE |
| 126 | +* Power Debugger |
| 127 | +* EDBG - on-board debugger on Xplained Pro/Ultra |
| 128 | +* mEDBG - on-board debugger on Xplained Mini/Nano |
| 129 | +* JTAGICE3 (firmware version 3.0 or newer) |
| 130 | + |
| 131 | +Although not all functionality is provided on all debuggers/boards. See device support section below. |
| 132 | + |
| 133 | +### Devices |
| 134 | +pymcuprog supports: |
| 135 | +* All UPDI devices, whether mounted on kits or standalone |
| 136 | +* PIC devices mounted on Curiosity Nano kits, or similar board with PKOB nano (nEDBG) debugger |
| 137 | + |
| 138 | +Other devices (eg ATmega328P, ATsamd21e18a) may be partially supported for experimental purposes |
| 139 | + |
| 140 | +## Notes for Linux® systems |
| 141 | +This package uses pyedbglib and other libraries for USB transport and some udev rules are required. For details see the pyedbglib package: https://pypi.org/project/pyedbglib |
0 commit comments