platformio / platform-atmelmegaavr

Atmel megaAVR: development platform for PlatformIO
https://registry.platformio.org/platforms/platformio/atmelmegaavr
Apache License 2.0
29 stars 21 forks source link

ATMega4808 and bootloader with UPDI #49

Open JulesGren opened 1 year ago

JulesGren commented 1 year ago

If one uses the Arduino IDE to load a bootloader (or hex including a bootloader) into a 4808 (perhaps also other chips as well) by default it places the bootload at0x0 and the main program at 0x200.

If you now try to load a program via platformio/vscode using UPDI, it loads this into memory at 0x0 and not 0x200, unfortunately the chip still looks for the start of the code at 0x200 therefore when trying to upload code from platformio it does not work (although it does upload successfully - it doesn't direct the chip to start in the correct place).

The workaround (a bit unreliable) is to upload blink or similar from Arduino IDE with the nobootloader option selected - a few times, at which point platformio can now be used to upload code via UPDI, it does mean platformio needs Arduino IDE to configure the chip first.

Options:

1) Enable the user to select the start point as 0x0 or 0x200 - ideal as this retains the bootloader for serial upload and UPDI, or at 0x0 when there is no bootloader 2) Ensure that when uploading it configures the chip to start at 0x0 (noting that normal serial loads on a bootloader still need to start at 0x200)

brunob45 commented 1 year ago

Have you selected the correct bootloader option in your platformio.ini file?

You should have the line board_hardware.uart=...

Have a look at this for the explanation https://github.com/MCUdude/MegaCoreX/blob/master/PlatformIO.md

JulesGren commented 1 year ago

Yes, have run it as blank (no ref to bootloader), and as no_bootloader and as uart1 / uart1_def, which is the bootloader installed.

There are slight differences, in some cases the in-built LED is illuminated in others it is not.

Sorry should add, if I program no bootloader via arduino, (the workaround) then I can program via platformio as detailed above, and a build with/without bootloader works as described in the above workaround.

but it does not write over an existing bootloader installed on the chip

brunob45 commented 1 year ago

Could you try to include the flag -DUSING_OPTIBOOT in the build_flags?

I made a pull request so it is included by default, but it is not yet merged.

JulesGren commented 1 year ago

Doesn't appear to work.

The 4808 has a bootloader from Arduino installed, if I load with: [env:ATmega4808] platform = atmelmegaavr board = ATmega4808 framework = arduino ;upload_protocol= arduino ;board_hardware.uart = uart1 / uart1_def board_hardware.uart = no_bootloader build_flags = -DUSING_OPTIBOOT

Then it builds and transfers ok, but doesn't flash (it's just std blink) this is the same if I set no bootloader and uart1.

build blink in arduino ide with no bootloader, then both no bootloader and uart1 bootloader may be loaded.

it appears as if platformio can't overwrite an arduino installed bootloader.

brunob45 commented 1 year ago

In platformio, installing the bootloader and the application is a 2 step process :

matou78 commented 1 year ago

uploading with UPDI erases automatically the bootloader, You need to reconfigure the chip flags by clicking the button "set flags" in PIO, this should help with your issue I had similar issues with AVRxxDAxx chips https://github.com/platformio/platform-atmelmegaavr/issues/42#issue-1175368640

JulesGren commented 1 year ago

Nope, neither of these last two suggestions work.

Yes I can write the bootloader Yes I can write the code

No I cannot overwrite an existing Arduino bootloader No I cannot write code on an existing Arduino bootloader

All of the above works though if i don't use UPDI for writing code

brunob45 commented 1 year ago

The Arduino bootloader, as well as the Platformio bootloader (I think they are the same) enables the upload of a program using the serial port.

If you are using UPDI, you will overwrite all of the memory, including the bootloader.

When using UPDI with the Arduino IDE, it may re-upload the bootloader each time, so that's why it is working there but not Platformio.

Once the bootloader is installed, please do not use UPDI.

If you want to use UPDI, you do not need the bootloader.

matou78 commented 1 year ago

Arduino IDE selects which way you upload the program

Selecting " burn bootloader" the ide will use updi to put the bootloader When pressing the button "upload" the ide automatically use serial if not specified other options

brunob45 commented 1 year ago

@matou78 correction :

When pressing the button "upload" the ide automatically use serial if ~not specified other options~ the selected board is AVR DA-series (Optiboot).

The IDE uses UPDI if the selected board is AVR DA-series (no bootloader).