Problems with setting up ATmega328PB project

I’m trying to set up a new blinky project on with an ATmega328PB on a custom PCB. It runs with 5V and a 16MHz external oscillator. I’d like to initially burn the bootloader and the fuses with a JTAG ICE mkII in ISP mode and afterwards upload software using the serial interface. And I’d like to use the serial also for debugging with avr-stub.

My platformio.ini was initially from the minicore repo with some modifications from several threads and doc pages:

[platformio]
default_envs = Upload_UART ; Default build target

[env]
framework = arduino
platform = atmelavr
board = ATmega328PB

board_build.variant = pb-variant
board_build.f_cpu = 16000000L
board_hardware.bod = disabled
board_hardware.eesave = yes
board_hardware.oscillator = external
board_hardware.uart = uart0
build_unflags = -flto

; GDB stub implementation
lib_deps =
    jdolinay/avr-debugger @ ~1.4
debug_tool = avr-stub
debug_port = /dev/ttyUSB0
build_type = debug
build_flags = -DAVR8_BREAKPOINT_MODE=1

monitor_port = ${env:Upload_UART.upload_port}
monitor_speed = 57600

; pio run -e Upload_UART -t upload
[env:Upload_UART]
upload_protocol = urclock
upload_port = /dev/ttyUSB0
board_upload.speed = ${env:fuses_bootloader.board_bootloader.speed}

; pio run -e Upload_ISP -t upload
[env:Upload_ISP]
upload_protocol = custom
upload_flags =
  -C$PROJECT_PACKAGES_DIR/tool-avrdude/avrdude.conf
  -p$BOARD_MCU
  -cjtag2isp
  -D
upload_command = avrdude $UPLOAD_FLAGS -U flash:w:$SOURCE:i
board_upload.speed = ${env:fuses_bootloader.board_bootloader.speed}

; pio run -e fuses_bootloader -t fuses
; pio run -e fuses_bootloader -t bootloader
[env:fuses_bootloader]
board_bootloader.type = urboot       ; urboot, optiboot or no_bootloader
board_bootloader.file = ~/.platformio/packages/framework-arduino-avr-minicore/bootloaders/urboot/atmega328pb/watchdog_1_s/autobaud/uart0_rxd0_txd1/no-led/urboot_atmega328pb_pr_ee_ce.hex
board_bootloader.speed = 115200      ; Bootloader baud rate
upload_protocol = custom
upload_flags =
  -C$PROJECT_PACKAGES_DIR/tool-avrdude/avrdude.conf
  -p$BOARD_MCU
  -cjtag2isp
  -v
upload_command = avrdude $UPLOAD_FLAGS -U flash:w:$SOURCE:i

Burning the bootloader and/or fuses doesn’t work at all from within PIO, so I do

./avrdude -p atmega328pb -C ~/.platformio/packages/tool-avrdude/avrdude.conf -cjtag2isp -Uflash:w:~/.platformio/packages/framework-arduino-avr-minicore/bootloaders/urboot/atmega328pb/watchdog_1_s/autobaud/uart0_rxd0_txd1/led+b5/urboot_atmega328pb_pr_ee_ce.hex:i -Ulock:w:0xFF:m

externally. After that I’m able to upload my program with the button (via Upload_UART). Upload_ISP also works but it seems to delete the bootloader and afterwards Upload_UART doesn’t work any more. So I have to repeat the external call.

Currently my program is only the following blinky example:

#include <Arduino.h>
#include "avr8-stub.h"
#include "app_api.h" // only needed with flash breakpoints

void setup()
{
  debug_init();
  pinMode(PIN_PD3, OUTPUT);
  pinMode(PIN_PD4, OUTPUT);
}

void loop()
{
//  breakpoint();

  digitalWrite(PIN_PD3, 1);
  delay(1000);
  digitalWrite(PIN_PD3, 0);
  delay(1000);
  digitalWrite(PIN_PD4, 1);
  delay(1000);
  digitalWrite(PIN_PD4, 0);
  delay(1000);
}

When I try to debug it, the debug menu opens for a very short moment and then closes again. With breakpoint(); the LEDs blink as expected when I managed to flash the application (see above). When I uncomment breakpoint(); the LEDs don’t blink at all.

I think the platformio.ini is screwed, but I have no idea what’s wrong, because I’m new to PlatformIO and its mechanisms. What has to be changed to make things work?

EDIT:
Updated the platformio.ini above and added the following:

When I enter pio run -e fuses_bootloader -t bootloader I get

Setting fuses

avrdude: Version 7.2-arduino.1
         Copyright the AVRDUDE authors;
         see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

         System wide configuration file is /home/michael/.platformio/packages/tool-avrdude/avrdude.conf
         User configuration file is /home/michael/.avrduderc
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : usb
         Using Programmer              : jtag2isp
avrdude: usbdev_open(): found JTAGICE mkII, serno: 00xxxxxxxxxx
JTAG ICE mkII sign-on message:
Communications protocol version: 1
M_MCU:
...

for setting the fuses and that works. Directly afterwards it tries to burn the bootloader and then I get

Uploading bootloader

avrdude: Version 7.2-arduino.1
         Copyright the AVRDUDE authors;
         see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

         System wide configuration file is /home/michael/.platformio/packages/tool-avrdude/avrdude.conf
         User configuration file is /home/michael/.avrduderc
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : usb
         Using Programmer              : jtag2isp
avrdude: usbdev_open(): did not find any USB device "usb" (0x03eb:0x2103)
avrdude main() error: unable to open programmer jtag2isp on port usb

avrdude done.  Thank you.

Seems like the mkII is still in use at that time.