mraardvark / pyupdi

Python UPDI driver for programming "new" tinyAVR and megaAVR devices
MIT License
210 stars 73 forks source link

Solved "link UPDI not OK - reinitialisation required" with CH340 and CP2102 serial adapters #75

Open vermeer opened 3 years ago

vermeer commented 3 years ago

After much puzzling, trying a multitude of serial adapters, and some scope analysis, I've solved my instance of the infamous "UPDI not OK" error.

Cutting to the chase, the root cause was the indicator LEDs on my serial adapters. Desolder the current-limiting resistor next to the RX LED and you should be good to go.

The traces generally looked shapely (sharp transitions, low noise, minimal timing errors) on the oscilloscope, but the levels were off. I'm running the ATtiny at 3.0V for battery life, and my serial adapter was set to 3.3V, so I knew it was already questionable, but across a dozen serial adapters from various manufacturers only 3 worked and those only sometimes. There's a 33k pullup on my board to keep the !reset pin high in normal usage, and pyupdi is trying to drive that pin low through a 4.7k resistor on the TX, so I didn't expect the low side to be 0.00V, but it wasn't as good as I expected and it wasn't consistent. My Attiny was only sometimes catching the initialization pulse, and when it did, my serial adapter failed to see the echo that popped up 13 ms after the 0x55 0x80 data on the break/STCS/LDCS init sequence. I could see it on my scope, but the ground levels were poor.

The RX LED lit whenever I drove the TX LED, but it was much dimmer, which was weird - it should be digital. That was the clue that I ignored for an embarrassing number of hours. As it turns out, the CH340 and Silabs CP2102 chips do not have RXLED buffers. Yes, an FTDI chip can be configured to use its GPIO pins as an RXLED output, but only one of the FT232 boards I had was wired that way; I suspect counterfeit FTDIs (which is practically every one of them you'll find on Amazon or Alibaba) don't implement the whole CBUS multipurpose IO feature. The designer would need to add a 74xx or something, and they're not going to spend that 5 cents when you're buying them in packs of 5, shipped, for less than the cost of a genuine FTDI chip. They'll instead just load the RX line and not worry about it as long as they get an insufficient number of complaints.

We're effectively dealing with this:

                         Vcc                     Vcc
                        +-+                     +-+
                         |                       |
 +---------------------+ |                       | +--------------------+
 | Serial port         +-+                       +-+        AVR device  |
 |                     |                           |                    |
 |       LED           |                           |                    |
 |  Vcc--|>|--[330]-+  |      +----------+         |    +---[33k]--Vcc  |
 |                  ----------+   4k7    +--------------                |
 |      TX----------+  |      +----------+    |    |    +-----+ UPDI    |
 |                     |                      |    |                    |
 |       LED   cut!    |                      |    |                    |
 |  Vcc--|>|--[330]-+  |                      |    |                    |
 |                  |-------------------------+    |                    |
 |      RX----------+  |                           |                    |
 |                     +--+                     +--+                    |
 +---------------------+  |                     |  +--------------------+
                         +-+                   +-+
                         GND                   GND

which is not the same as the circuit in the README. The TX pin is trying to pull the UPDI line low through a 4.7k resistor, fighting (successfully) against not only the 33k pullup on my target device, but also (unsuccessfully) against a 330 (or 220, or 270, depending on which adapter I used) pullup minus the forward voltage of various indicator LEDs.

I removed the resistor marked "Cut" above. After rectifying this situation, things worked much better.