jtag2updi

repository·master·Indexed 18 days ago

https://github.com/eltangas/jtag2updi

A firmware bridge that allows an AVR MCU, such as an Arduino Uno or Nano, to act as a programmer for UPDI-enabled AVR microcontrollers. It enables the host MCU to interface with avrdude using the jtagice Mk2 protocol via a serial link. The project includes build instructions for avr-gcc and the Arduino IDE, as well as specific support for Logic Green AVR clones and AVR JTAG ICE v2.0 hardware.

Tokens
2.3K
Snippets
6
Records
10
Agent score
14%

What's inside jtag2updi

  1. Overview of jtag2updi

    master

    jtag2updi is firmware designed to be uploaded to an AVR MCU (such as an ATmega328P) to act as a bridge. It enables the host MCU to interface with avrdude using the jtagice Mk2 protocol via a serial link, allowing you to program newer UPDI-based MCUs (like the ATtiny817 family) using standard hardware like an Arduino Uno or Nano.

    Connection Workflow: avrdude $\rightarrow$ HW Serial $\rightarrow$ Programmer MCU (e.g., Mega328P) $\rightarrow$ SW Serial on PD6 $\rightarrow$ Target MCU (e.g., tiny817)

    Hardware Requirements & Warnings:

    • Voltage Compatibility: The UPDI pin is high-voltage tolerant on supported devices, meaning $V_{prog} > V_{target}$ is generally okay, but the reverse is not.
    • Warning: Do NOT assume MegaAVR 0-series or AVR-DA series UPDI pins are high-voltage tolerant.
    • Logic Levels: $V_{target}$ cannot be lower than approximately 60% of $V_{prog}$. For example, a 3.3V target works with a 5.0V programmer, but a 2.5V target will likely cause communication errors.
  2. Wire AVR JTAG ICE v2.0 to a target MCU

    master

    When using the AVR JTAG ICE v2.0 as a UPDI programmer, follow this wiring pattern.

    Voltage Warning: The VTref pin supplies 5V to the target MCU. If your target board uses a different voltage (e.g., 3.3V or 1.8V), leave VTref unconnected. Alternatively, you can use a voltage regulator to step down the voltage from VTref if the target MCU requires it.

     +----------+          +---------------------+                           +--------------------+
     | PC       |          | AVR JTAG ICE  VTref +(4)--------->--------------+ Vcc                |
     | avrdude  |          |                     |      +----------+         |                    |
     |          |          |               nSRST +(6)---+   4k7    +---------+ UPDI               |
     |      USB +----------+ USB                 |      +----------+         |                    |
     |          |          |                     |                           |       Target       |
     |          |          |                     |                           |                    |
     |          |          |                 GND +(2)-+                   +--+ GND                |
     +----------+          +---------------------+    |                   |  +--------------------+
                 JTAGICE MkII             +-+     UPDI        +-+ 
                 Protocol                 |     Protocol    | 
                                          GND               GND
  3. Build jtag2updi with avr-gcc

    master

    To build the firmware manually using avr-gcc, use the provided make.bat file. You must edit the file first to specify the following:

    1. The path to avr-gcc on your system.
    2. The correct target MCU.
    3. The MCU frequency (F_CPU, defaults to 16MHz).
    4. The UPDI link baud rate (UPDI_BAUD, defaults to 225 kbaud).

    Building for Logic Green AVR Clones:

    • Select atmega328p as the target.
    • Define the macro ARDUINO_AVR_LARDU_328E either in sys.h using #define or via the gcc command line using -DARDUINO_AVR_LARDU_328E.
    • Recommended F_CPU values are 32MHz or 16MHz.
    # Example of how the gcc command might look after editing make.bat
    avr-gcc -DARDUINO_AVR_LARDU_328E -DF_CPU=16000000L ...
  4. Use jtag2updi with avrdude

    master

    The firmware emulates a jtagice mk2 programmer. Use the -c jtag2updi flag in avrdude.

    Basic Command (Windows):

    avrdude -c jtag2updi -P com7 -p t1614

    Handling Locked Chips: If the target chip is locked, you will see RSP_ILLEGAL_MCU_STATE. To unlock the chip, you must erase it using the -e flag:

    avrdude -c jtag2updi -P com7 -p t1614 -e

    Interactive Mode (Terminal): To erase a chip via interactive mode, use -t and -F (to override errors). Note: You must build the firmware with DISABLE_HOST_TIMEOUT defined in sys.h for terminal mode to work.

    avrdude -c jtag2updi -P com7 -p t1614 -t -F

    Once in the avrdude> prompt, type erase then quit.

  5. Flash firmware to AVR JTAG ICE v2.0

    master

    Use the avr-aosp.py uploader tool (running as superuser) to burn the firmware onto the USB module.

    Timing Note: Plug in the USB module and perform these steps during the 5-second window at startup when the LED is blinking fast red-blue.

    1. Erase the module: Run the erase command to clear existing content.
    2. Upload the firmware: Run the write command pointing to the compiled .hex file.
    3. Activate: Unplug and replug the USB stick. The blinking should stop, and a permanent red LED should turn on, indicating the firmware is active and ready for use with avrdude.
    # 1. Erase content
    python3 avr-aosp.py -op erase
    
    # 2. Upload firmware
    python3 avr-aosp.py -op write -file ../../build/JTAG2UPDI.hex
  6. Build jtag2updi with Arduino IDE

    master

    You can build the firmware as an Arduino sketch:

    1. Copy all files from the source directory into a new directory named jtag2updi within your Arduino sketches folder.
    2. Open jtag2updi.ino in the Arduino IDE.
    3. The IDE handles MCU model and F_CPU automatically.
    4. To change the UPDI link speed, you must manually edit UPDI_BAUD in the source code.

    Logic Green AVR Clones: It is recommended to install the board definitions from https://github.com/dbuezas/lgt8fx.

    Default UPDI Communication Pins:

    • Arduino Mega (1280/2560): PD3 $\rightarrow$ Digital Pin 18
    • Arduino (328P): PD6 $\rightarrow$ Digital Pin 6
  7. Build firmware for AVR JTAG ICE v2.0

    master

    To enable support for the AVR JTAG ICE v2.0 hardware, you must modify the build scripts to uncomment the target MCU and definition lines before compiling. This overrides the default target.

    Windows

    Edit make.bat and remove the rem remarks from the TARGETMCU and DEFINES lines.

    Linux

    Edit make.sh and uncomment the TARGETMCU and DEFINES lines (remove the ## remarks).

    rem select AVRJTAGICE v2.0 as target (override upper)
    set TARGETMCU=atmega16
    set DEFINES=-DNDEBUG -DUPDI_BAUD=120000U -DF_CPU=7372800 ...
    ## select AVRJTAGICE v2.0 as target (override upper)
    TARGETMCU=atmega16
    DEFINES="-DNDEBUG -DUPDI_BAUD=120000U -DF_CPU=7372800 ..."
  8. Configure timeouts and debug channels in sys.h

    master

    The firmware includes configurable timeouts and debug options in sys.h:

    Timeouts:

    • Target Timeout (100ms): Prevents hangs if the target is unresponsive. Returns RSP_NO_TARGET_POWER on failure. Disable via #define DISABLE_TARGET_TIMEOUT.
    • Host Timeout (250ms): Prevents hangs if the host is slow. Note: If you need to use avrdude interactive/terminal mode, you must disable this by uncommenting #define DISABLE_HOST_TIMEOUT.

    Debug Channels:

    • SPI Debugging: Can be enabled in sys.h to output data on SPI (useful for parts without a second USART).
    • Extended Info: In dbg.h, you can enable extended information for most UPDI calls.
  9. Troubleshoot UPDI link issues

    master

    If the issue is specifically on the UPDI link, you can attempt the following hardware/software adjustments by modifying the sys.h header file and recompiling:

    1. Reduce UPDI speed: Change the UPDI_BAUD setting.
    2. Change IO type: Set UPDI_IO_TYPE to 2 to use the alternate bit-banging USART.

    If the programmer becomes unresponsive, perform a hardware reset.

  10. Troubleshoot serial link speed errors

    master

    If connections are verified but errors persist, the serial link baud rate may be too high for your MCU's clock speed (e.g., an 8MHz MCU may struggle with the default 115200 baud).

    You can adjust the baud rate using the avrdude -b option. Valid baud rates are: 2400, 4800, 9600, 14400, 19200, 38400, 57600, and 115200. To make this change permanent, edit the jtag2updi entry in your avrdude.conf file.

    avrdude -c jtag2updi -p <part> -B <baud_rate> -U flash:w:file.hex:i