SpenceKonde / AVR-Guidance

A guide to best practices when using the Arduino IDE to program AVR microcontrollers
146 stars 23 forks source link

Error using SerialUPDI #7

Closed mrWheel closed 2 years ago

mrWheel commented 2 years ago

I trying to upload a blink sketch to an AVR128DB32 processor.

Using a FTDI board (Robotdyn) witch has a CH340H chip onboard. Added the diode between Rx and Tx and have a 470R resistor in series with the UPDI line.

Updated the programmers.txt, boards.txt and platforms.txt files in my Arduino15 map and copied the python3 program to the map ../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/python3. The full path now is: ../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/python3/python3

This is what is in the tools map:

$ la -l
total 88
-rw-r--r--  1 User  staff   1523 May 18 17:35 ManualPython.md
-rw-r--r--  1 User  staff  26974 May 18 17:35 README.md
drwxr-xr-x  9 User  staff    288 Sep 30 13:52 libs
-rw-r--r--  1 User  staff   9258 May 18 17:35 prog.py
drwxr-xr-x  3 User  staff     96 Oct 22 12:44 python3

I get these errors uploading the sketch:

Sketch uses 5962 bytes (4%) of program storage space. Maximum is 131072 bytes.
Global variables use 534 bytes (3%) of dynamic memory, leaving 15850 bytes for local variables. Maximum is 16384 bytes.
../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/python3/python3 -u ../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/prog.py -t uart -u /dev/cu.usbserial-1434440 -b 230400 -d avr128db32 --fuses 5:0b11001001 7:0x00 8:0x00 -f../tmp/arduinoBuild/Blink.ino.hex -a write 
Arduino <---> pymcuprog bridge by Quentin Bolsee and Spence Konde
Version 1.1.0 - May 2021
Using serial port /dev/cu.usbserial-1434440 at 230400 baud.
Target: avr128db32
Set fuses: ['5:0b11001001', '7:0x00', '8:0x00']
Action: write
File: ../tmp/arduinoBuild/Blink.ino.hex
Traceback (most recent call last):
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/prog.py", line 266, in <module>
    main()
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/prog.py", line 121, in main
    return_code = pymcuprog_basic(args, fuses_dict)
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/prog.py", line 183, in pymcuprog_basic
    status = pymcu._start_session(backend,
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/libs/pymcuprog/pymcuprog_main.py", line 549, in _start_session
    backend.start_session(sessionconfig)
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/libs/pymcuprog/backend.py", line 359, in start_session
    self.programmer.setup_device(
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/libs/pymcuprog/programmer.py", line 78, in setup_device
    self.device_model = get_nvm_access_provider(self.transport,
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/libs/pymcuprog/nvm.py", line 42, in get_nvm_access_provider
    accessprovider = NvmAccessProviderSerial(transport, device_info, baud=frequency)
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/libs/pymcuprog/nvmserialupdi.py", line 53, in __init__
    self.avr = UpdiApplication(port, baud, self.dut)
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/libs/pymcuprog/serialupdi/application.py", line 79, in __init__
    datalink.init_datalink()
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/libs/pymcuprog/serialupdi/link.py", line 45, in init_datalink
    raise PymcuprogError("UPDI initialisation failed")
pymcuprog.pymcuprog_errors.PymcuprogError: UPDI initialisation failed
pymcuprog.pymcuprog_errors.PymcuprogError: UPDI initialisation failed

With the FTDI adaptor I got from you with the AVR128DB64 board I get these errors:

Global variables use 534 bytes (3%) of dynamic memory, leaving 15850 bytes for local variables. Maximum is 16384 bytes.
../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/python3/python3 -u ../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/prog.py -t uart -u /dev/cu.usbserial-1434440 -b 230400 -d avr128db32 --fuses 5:0b11001001 7:0x00 8:0x00 -f../tmp/arduinoBuild/Blink.ino.hex -a write 
Arduino <---> pymcuprog bridge by Quentin Bolsee and Spence Konde
Version 1.1.0 - May 2021
Using serial port /dev/cu.usbserial-1434440 at 230400 baud.
Target: avr128db32
Set fuses: ['5:0b11001001', '7:0x00', '8:0x00']
Action: write
File: ../tmp/arduinoBuild/Blink.ino.hex
pymcuprog.serialupdi.application - ERROR - Hard reset failed.
Traceback (most recent call last):
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/prog.py", line 266, in <module>
    main()
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/prog.py", line 121, in main
    return_code = pymcuprog_basic(args, fuses_dict)
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/prog.py", line 183, in pymcuprog_basic
    status = pymcu._start_session(backend,
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/libs/pymcuprog/pymcuprog_main.py", line 549, in _start_session
    backend.start_session(sessionconfig)
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/libs/pymcuprog/backend.py", line 359, in start_session
    self.programmer.setup_device(
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/libs/pymcuprog/programmer.py", line 78, in setup_device
    self.device_model = get_nvm_access_provider(self.transport,
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/libs/pymcuprog/nvm.py", line 42, in get_nvm_access_provider
    accessprovider = NvmAccessProviderSerial(transport, device_info, baud=frequency)
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/libs/pymcuprog/nvmserialupdi.py", line 56, in __init__
    self.avr.read_device_info()
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/libs/pymcuprog/serialupdi/application.py", line 104, in read_device_info
    raise RuntimeError("Failed to read device info.")
RuntimeError: Failed to read device info.
RuntimeError: Failed to read device info.

Have I forgotten something?

Update

I have added a 4k7 resistor between the Tx and Rx lines .. and now it works!

But, this (from ../UPDI/jtag2updi.md) is very confusing (to me anyway) as is the naming of this document ;-):

A note on breakout boards

Some tinyAVR and other UPDI-based part breakout boards have an on-board resistor. Sometimes this is a 4.7k one. That is NOT appropriate. I was part of the problem for a while. I think the original mistake came from people conflating the pyupdi resistor with a generally appropriate one. When I started megaTinyCore, my early collaborator was making hardware with a 4.7k resistor; I assumed he was doing it right. While this does work with dedicated programmers, including jtag2updi, it doesn't work with serial UPDIO. It will work with dedicated programmers like jtag2updi, as long as they don't have their own resistor. Suffice to say, for a time it was a very common belief. I use 470 ohms now, but I can't find fault with a design over it not having one at all.

Connections:

Vcc, Gnd of serial adapter to Vcc, Gnd of target 4.7k resistor between Tx and Rx of adapter (many adapters have built-in 1k, 1.5k, or 2.2k resistor in series with Tx; these should use a proportionally smaller resistor) For better results, a smaller resistor (that built-in one on most adapters, mentioned above, will do perfectly here) and a small schottky diode (band towards Tx, other end connected to Rx) can be used (use a "small signal diode" - larger general purpose diodes may have properties that make them less suitable for this) The diode substantially widens the tolerances of this programming method, and significantly improves reliability. My top pick here is the BAT54C,235; it's in a tiny SOT-23 package (it's 2 diodes both weith the "band" towards the pin thats' alone on one side) Why? Because, assuming your serial adapter has the pins on 0.1" header, and TX and RX are next to eachother (both extremely common) the the diode fits right in beteeen them. and with no lead that could later fatigue and break the result is less likely to be damaged by rough handling. Then if I want to more ovbviously mark it as a UPDI programmer, I might cut off the Tx, DTR and CTS pin; always remember that you can get serial adapters for a buck a piece on ebay. Rx of adapter to UPDI pin of target. A small resistor (under 1k - like the 470 ohm one we generally recommend) in series with this is fine.

mrWheel commented 2 years ago

Hm.. after all it still does not seem to work (it did a few times and it was blinding fast(!) but now it does not anymore).

I'm using the FDTI I received from @SpenceKonde. Between pin 2 of the CH340 and TXo there is zero resistance.

This is the wiring I have (tried 4k7 and 1k for R1 but at no avail): Wiring

On the UPDI line I see this on my scoop: UDPI_Line

The error is:

Sketch uses 5962 bytes (4%) of program storage space. Maximum is 131072 bytes.
Global variables use 534 bytes (3%) of dynamic memory, leaving 15850 bytes for local variables. Maximum is 16384 bytes.
../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/python3/python3 -u ../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/prog.py -t uart -u /dev/cu.usbserial-1434420 -b 230400 -d avr128db32 --fuses 5:0b11001001 7:0x00 8:0x00 -f../tmp/arduinoBuild/Blink.ino.hex -a write 
Arduino <---> pymcuprog bridge by Quentin Bolsee and Spence Konde
Version 1.1.0 - May 2021
Using serial port /dev/cu.usbserial-1434420 at 230400 baud.
Target: avr128db32
Set fuses: ['5:0b11001001', '7:0x00', '8:0x00']
Action: write
File: ../tmp/arduinoBuild/Blink.ino.hex
Traceback (most recent call last):
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/prog.py", line 266, in <module>
    main()
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/prog.py", line 121, in main
    return_code = pymcuprog_basic(args, fuses_dict)
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/prog.py", line 183, in pymcuprog_basic
    status = pymcu._start_session(backend,
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/libs/pymcuprog/pymcuprog_main.py", line 549, in _start_session
    backend.start_session(sessionconfig)
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/libs/pymcuprog/backend.py", line 359, in start_session
    self.programmer.setup_device(
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/libs/pymcuprog/programmer.py", line 78, in setup_device
    self.device_model = get_nvm_access_provider(self.transport,
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/libs/pymcuprog/nvm.py", line 42, in get_nvm_access_provider
    accessprovider = NvmAccessProviderSerial(transport, device_info, baud=frequency)
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/libs/pymcuprog/nvmserialupdi.py", line 53, in __init__
    self.avr = UpdiApplication(port, baud, self.dut)
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/libs/pymcuprog/serialupdi/application.py", line 79, in __init__
    datalink.init_datalink()
  File "../Arduino15/packages/DxCore/hardware/megaavr/1.3.6/tools/libs/pymcuprog/serialupdi/link.py", line 45, in init_datalink
    raise PymcuprogError("UPDI initialisation failed")
pymcuprog.pymcuprog_errors.PymcuprogError: UPDI initialisation failed
pymcuprog.pymcuprog_errors.PymcuprogError: UPDI initialisation failed

I feel I'm almost there but have no idea what I can do to make it work reliable ...

Please advice!

SpenceKonde commented 2 years ago

Sorry, I just saw this now. This is not the repo to create issues in if you want to be guaranteed a timely response. Go for one of the repos that the core you;'re working with in it,