Imagine using a single circuit for Serial (USB Communications Device Class CDC) to communicate with the MCU and also to program it with UPDI. This is possible thanks to the 4052 IC acting as a switch, controlled by the CH340 IC.

In the example I describe here, I use this circuit to program an AVR128DA28 MCU.

I got this idea from MCS Electronics. They don’t use avrdude and in their example they use a 4053 IC. By understanding how their solution works, I made what I think are the most logical changes using a 4052.

It’s possible to use the same IC for both functions, but an additional IC is needed as a switch (4052) to alternate between UART (where CDC is) and UPDI. Both circuits are controlled by the CH340’s RTS (Request to Send) pin. When RTS is set HIGH, the 4052 control pins A and B also go HIGH, activating the “programmer” mode as I call it. When RTS is set LOW, A and B are also LOW, switching to “data” or serial mode so we can communicate normally.

Components

  • One CH340 IC
  • One CD4052B IC
  • 10nF capacitor
  • 100nF / 0.1uF capacitor, Code: 104, Quantity: 2
  • 4.7k resistor
  • Schottky diode 1N5817 (or another, e.g. BAT43)

Schematic

The 4052 IC will use control pins A and B in parallel to redirect the CH340’s TX and RX lines either to the UPDI circuit or to the MCU’s UART pins.

Warning: The UART must be connected to pins X0 and Y0, and the UPDI to pins X3 and Y3 of the 4052 IC. This setup works perfectly. If reversed, even though it might look logical, it does not work properly. This is because UART communication needs to be interrupted when UPDI is in use.

How to program using UPDI?

We need to tell avrdude to set the RTS pin HIGH like this: -x rtsdtr=high.

avrdude -c serialupdi \
  -p avr128da28 \
  -P /dev/tty.usbserial-2110 \
  -e -F -x rtsdtr=high
avrdude -c serialupdi \
  -p avr128da28 \
  -P /dev/tty.usbserial-2110 \
  -x rtsdtr=high -F -V -D -U flash:w:main.hex

How to use CDC?

You can use clients like CoolTerm or picocom, whichever you prefer.

picocom --lower-rts -b 115200 /dev/tty.usbserial-2110

How to exit picocom?

Press: Control + A, then Control + X.

The spanish version can you see in my personal page.