How can I programm with PlatformIO an attiny202?

I would like to program my Attiny202 in my PlatformIO IDE. But how can I do that best and which tools can I use for that?
I made already some unsuccessful experiments since I have not enought knowlage.

I currently have these tools which I might can use: https://www.amazon.de/dp/B08HP4X326?psc=1&ref=ppx_yo2ov_dt_b_product_details&language=en_GB
Arduino Nano
ESP32S2

For the uploading I would like to use the UPDI protocoll and I fund several videos about using an arduion (e.g. nano) to use it as programmer. After receiving the nano I noticed that the projects seems to be deprecated.
I used these videos with their additonal sources:

And I thought I could do something like this: Can the Arduino ISP board be used with PlatformIO? - #14 by Fernando_Fontana to make it compatible with PlatformIO.
(I don´t want to use the ArduionIDE since PlatformIO is better in general)

Can I use this or how do I have to modify it so I can use it to upload my code with an arduino to an attiny202
(Extracted from Atmel AVR — PlatformIO latest documentation)

[env:program_via_ArduinoISP]
platform = atmelavr
framework = arduino
board = nanoatmega328
upload_protocol = arduinoisp

upload_port = /dev/ttyUSB0
upload_speed = 19200
upload_flags =
    -C
    ; use "tool-avrdude-megaavr" for the atmelmegaavr platform
    ${platformio.packages_dir}/tool-avrdude/avrdude.conf
    -p
    $BOARD_MCU
    -P
    $UPLOAD_PORT
    -b
    $UPLOAD_SPEED
    -c
    stk500v1
upload_command = avrdude $UPLOAD_FLAGS -U flash:w:$SOURCE:i


Or do I need another tool for doing that and which options do I need to run it in PlatformIO.

Many Thanks in advance :smiley:

I also found this: AVR-Guidance/jtag2updi.md at master · SpenceKonde/AVR-Guidance · GitHub could that be a solution too? Could I use that with PlatformIO?

I have n o experience with UPDI devices, yet, but is there anything of any use on this page from the PlatformIO docs?

https://docs.platformio.org/en/latest/platforms/atmelmegaavr.html#upload-using-programmer

There’s a python UPDI programmer mentioned and some configuration for using it. I have no idea what hardware you might have to obtain, the list on the page mentions Atmel-ICE, Power Debugger, PICkit4, Snap, PKOB nano / nEDBG / Curiosity Nano debugger.

HTH

Cheers,
Norm.

PlatformIO does have the ATtiny202 target board.

There are pages worth of documentation and guidance on how to use the megaTinyCore (which besides many others, supports the ATtiny202) in PlatformIO at this page.

And you’ve already found the page on how to construct a UPDI programmer using a standard usb-to-serial adapter as a UPDI programmer. @normandunbar’s link above then tells you how to tell PlatformIO to use that upload method. If you already have your Uno configured as some programming device and you know the avrdude command for flashing it, then you of course can also use that.

So, an example platformio.ini could e.g. be

[env]
platform = atmelmegaavr
framework = arduino
; Chip in use
board = ATtiny202
; Clock frequency in [Hz]
board_build.f_cpu = 16000000L
; Oscillator type (internal or external)
board_hardware.oscillator = internal
; Serial monitor baud rate
monitor_speed = 9600

[env:attiny202_upload_pymcuprog]
upload_speed = 115200
upload_flags =
    --tool
    uart
    --device
    attiny202
    --uart
    $UPLOAD_PORT
    --clk
    $UPLOAD_SPEED
upload_command = pymcuprog write --erase $UPLOAD_FLAGS --filename $SOURCE
1 Like

Me neither this is my first time but since there are many controllers out of stock I thought I just try them since they where in stock and they were not to expensive.
I am not really sure about the upload flags, since I rode this"in the case of external programmers, it’s easy to brick a board simply by specifying incorrect upload flags."
I don´t know the hardware either but the Atmel-ICE is defintly too pricey for me. I mostly used ESP32-varinants so far.

So I need a standart USB-TTL adapter for that? Using the nano is not an option anymore?

I actually was on that site but it isn´t clear to me, do I need to install that or is it already in PlatformIO when it checks for the dependencys.
Do I need to run the command and which values do I need for the fuses:

pio run -t fuses -e set_fuses

Or is that just an example and it will just extract that information from the platform.ini file?

To to be honest I am not sure if it works since it didn´t work so far for me and I never used the acrdude command before.

Does just [env] mean that this is the default profile?

And when

$UPLOAD_PORT

is used does that mean that PIO fills this information and I do not have to provide that?

It still is as I’ve said above. If you had success with the programmer being selected as “jtag2updi” in the Arduino IDE, then you can use it in PlatformIO too. In fact, that’s the default upload method:

So you can erase all upload_x directives in the platformio.ini to get that.

The device already has some fuses set by default. In the first step, try not to modify them, only if you notice that it seems to be using the wrong clock source, divider, or some other thing.

As the documentation says, [env] is the place where you can put options that will be inherited by all other environments. Global settings, so to say. Individual [env:xyz] environments can then be smaller, just adding on top of the options defined in [env].

Yes, you can see $UPLOAD_PORT as the environment variable that PlatformIO will try to autodetect based on your available serial ports / COM devices. It’s a variable substitution. You can manually override this value by placing a upload_port directive in your environment.

When I try to upload it, I always get this error. I tried some different variants but it always prints something like this:

In file included from /home/djpx/.platformio/packages/toolchain-atmelavr/avr/include/avr/io.h:99:0,
                 from src/JICE_io.cpp:9:
src/JICE_io.cpp: In function 'uint8_t JICE_io::put(char)':
src/JICE_io.cpp:55:25: error: 'HOST_USART' was not declared in this scope
   loop_until_bit_is_set(HOST_USART.STATUS, USART_DREIF_bp);
                         ^
src/JICE_io.cpp:55:25: note: suggested alternative: 'IO_START'
In file included from src/JICE_io.cpp:10:0:
src/JICE_io.cpp: In function 'uint8_t JICE_io::get()':
src/JICE_io.cpp:67:38: error: 'HOST_USART' was not declared in this scope
   loop_until_bit_set_or_host_timeout(HOST_USART.STATUS, USART_RXCIF_bp); /* Wait until data exists. */
                                      ^
src/JICE_io.h:18:12: note: in definition of macro 'loop_until_bit_set_or_host_timeout'
   while(!((register&(1<<bitpos))||(SYS::checkTimeouts() & WAIT_FOR_HOST))); \
            ^~~~~~~~
src/JICE_io.cpp:67:38: note: suggested alternative: 'IO_START'
   loop_until_bit_set_or_host_timeout(HOST_USART.STATUS, USART_RXCIF_bp); /* Wait until data exists. */
                                      ^
src/JICE_io.h:18:12: note: in definition of macro 'loop_until_bit_set_or_host_timeout'
   while(!((register&(1<<bitpos))||(SYS::checkTimeouts() & WAIT_FOR_HOST))); \
            ^~~~~~~~
src/JICE_io.cpp:69:10: error: 'HOST_USART' was not declared in this scope
   return HOST_USART.RXDATAL;
          ^~~~~~~~~~
Compiling .pio/build/attiny202_upload_pymcuprog/src/dbg.cpp.o
src/JICE_io.cpp:69:10: note: suggested alternative: 'IO_START'
   return HOST_USART.RXDATAL;
          ^~~~~~~~~~
          IO_START
In file included from src/JTAG2.h:12:0,
                 from src/JICE_io.h:13,
                 from src/JICE_io.cpp:10:
src/JICE_io.cpp: In function 'void JICE_io::init()':
src/sys.h:47:27: error: 'VPORTHOST_TX_PORT' was not declared in this scope
   # define PORT(x) CONCAT(VPORT,x).OUT
                           ^
src/sys.h:39:21: note: in definition of macro 'CONCAT'
 #define CONCAT(A,B) A##B                // concatenate
                     ^
src/JICE_io.cpp:80:3: note: in expansion of macro 'PORT'
   PORT(HOST_TX_PORT) |= 1 << HOST_TX_PIN;
   ^~~~
src/JICE_io.cpp:80:30: error: 'HOST_TX_PIN' was not declared in this scope
   PORT(HOST_TX_PORT) |= 1 << HOST_TX_PIN;
                              ^~~~~~~~~~~
src/JICE_io.cpp:83:3: error: 'HOST_USART' was not declared in this scope
   HOST_USART.BAUD = baud_reg_val(19200);
   ^~~~~~~~~~
src/JICE_io.cpp:83:3: note: suggested alternative: 'IO_START'
   HOST_USART.BAUD = baud_reg_val(19200);
   ^~~~~~~~~~
   IO_START
In file included from src/JICE_io.cpp:10:0:
src/JICE_io.cpp: In function 'void JICE_io::flush()':
src/JICE_io.cpp:106:38: error: 'HOST_USART' was not declared in this scope
   loop_until_bit_set_or_host_timeout(HOST_USART.STATUS, USART_TXCIF_bp);
                                      ^
src/JICE_io.h:18:12: note: in definition of macro 'loop_until_bit_set_or_host_timeout'
   while(!((register&(1<<bitpos))||(SYS::checkTimeouts() & WAIT_FOR_HOST))); \
            ^~~~~~~~
src/JICE_io.cpp:106:38: note: suggested alternative: 'IO_START'
   loop_until_bit_set_or_host_timeout(HOST_USART.STATUS, USART_TXCIF_bp);
                                      ^
src/JICE_io.h:18:12: note: in definition of macro 'loop_until_bit_set_or_host_timeout'
   while(!((register&(1<<bitpos))||(SYS::checkTimeouts() & WAIT_FOR_HOST))); \
            ^~~~~~~~
src/JICE_io.cpp: In function 'void JICE_io::set_baud(JTAG2::baud_rate)':
src/JICE_io.cpp:114:3: error: 'HOST_USART' was not declared in this scope
   HOST_USART.BAUD = baud_tbl[rate - 1];
   ^~~~~~~~~~
src/JICE_io.cpp:114:3: note: suggested alternative: 'IO_START'
   HOST_USART.BAUD = baud_tbl[rate - 1];
   ^~~~~~~~~~
   IO_START
Compiling .pio/build/attiny202_upload_pymcuprog/src/jtag2updi.ino.cpp.o
*** [.pio/build/attiny202_upload_pymcuprog/src/JICE_io.cpp.o] Error 1
Compiling .pio/build/attiny202_upload_pymcuprog/src/main.cpp.o
================== [FAILED] Took 1.05 seconds ==================

Environment                 Status    Duration
--------------------------  --------  ------------
attiny202_upload_pymcuprog  FAILED    00:00:01.049
============= 1 failed, 0 succeeded in 00:00:01.049 =============

Do I need more variables (Host variables?) so that I can use that?

I tried it with the arduino-IDE to be sure that the wireing and the nano works. I was able to flash it succesful but currently I still have trouble with PlatformIO (my preferred development environment)

Are you trying to build the GitHub - ElTangas/jtag2updi: UPDI programmer software for Arduino (targets Tiny AVR-0/1/2, Mega AVR-0 and AVR-DA/DB MCUs) project using PlatformIO? It has a specific Makefile and build instructions that you should follow, the Makefile adds some macros too that influence the build. Better use the intended way of building or flash the pre-built .hex files on your Arduino Uno: jtag2updi/build at master · ElTangas/jtag2updi · GitHub

It seems there was still something cashed now it works. I also noticed that you often need to do an extra clear when you use different controllers. Thank you for your help.
Now I just can use:

[env:ATtiny202]
platform = atmelmegaavr
board = ATtiny202
framework = arduino

And it works