platformio / platform-atmelavr

Atmel AVR: development platform for PlatformIO
https://registry.platformio.org/platforms/platformio/atmelavr
Apache License 2.0
136 stars 104 forks source link

Wrong flash sizes on 8KB flash AVR parts #207

Closed ospilos closed 3 years ago

ospilos commented 3 years ago

There is an issue of incorretly staded flash size on bare AVR parts with 8KB of flash memory, namely: https://github.com/platformio/platform-atmelavr/blob/develop/boards/ATmega8.json https://github.com/platformio/platform-atmelavr/blob/develop/boards/ATmega88.json https://github.com/platformio/platform-atmelavr/blob/develop/boards/ATmega88P.json https://github.com/platformio/platform-atmelavr/blob/develop/boards/ATmega88PB.json

The value of "upload" -> "maximum_size": is 7680, while it should probably be 8192 as these are bare ICS, therefore without any bootloader in them.

On the other side https://github.com/platformio/platform-atmelavr/blob/develop/boards/ATmega48.json seems to advertise the whole flash size in the json definitions.

In fact this is incosistent across most of the bare IC definitions, as ATMEGA parts mostly has the flash size liited, while ATTINY parts seem to advertise the whole flash size.

MCUdude commented 3 years ago

By default, the PlatformIO/MiniCore implementation includes a 512-byte bootloader by default. If you don't want to use a bootloader you can just redefine the flash size in platformio.ini.

build_upload.maximum_size = 8192

The ATmega48 doesn't have a bootloader section. Thats why all 4096 bytes are "available" by default.

MCUdude commented 3 years ago

@valeros would it be possible to automatically add 512 bytes to the build_upload.maximum_size somewhere if a bootloader is in use? I looked through the main.py file in this repo, but I couldn't find any references to this.

valeros commented 3 years ago

I agree that all these manifests rather represent bare MCUs than full-fledged boards. IMO, the manifests should contain the precise memory size without any assumptions on bootloader presence. Anyone who want to use bootloader then should use the build_upload.maximum_size option.

MCUdude commented 3 years ago

Anyone who wants to use bootloader then should use the build_upload.maximum_size option.

@valeros is this something that could be handled automatically for bare MCUs that are supported by Mighty/Mega/Mini/Major/MicroCore? We know the bootloader sizes for the various targets (512B or 1024B). Mabe a field in the manifest file: build_upload.bootloader_size = 512?

valeros commented 3 years ago

TBH, I'm a bit reluctant to pollute manifests with core-specific configurations. I guess users who know how to use bootloader should be aware of memory constraints it brings. Besides, how can we determine whether a target is using bootloader to reduce the memory size accordingly?

MCUdude commented 3 years ago

For those who aren't using a bootloader, they are probably using the manifest files meant to be used with "my" Arduino cores. Since I've compiled all the bootloaders for all these boards, I know how big they are and how much space is allocated in every microcontroller.

Just for fun, the bootloader build scrip I'm using also generates a file that contains some information about every file, for example atmega1284p_build_info.txt. But what matters here is the actual allocated space set by the fuse bits.

Here's the rule for MightyCore, MegaCore, MiniCore and MajorCore:

valeros commented 3 years ago

Thanks for the info, but the question still remains, how can we detect that a bootloader is used and accordingly subtract the default bootloader size from the maximum_size field.

MCUdude commented 3 years ago

Thanks for the info, but the question still remains, how can we detect that a bootloader is used and accordingly subtract the default bootloader size from the maximum_size field.

My cores support board_hardware.uart. Subtract 512 or 1024 bytes if board_hardware.uart is not no_bootloader. board_hardware.uart = uart0 is the default value for all these chips anyways at the moment. The user has to explicitly write in the platformio.ini file that no bootloader should be used: board_hardware.uart = no_bootloader.

valeros commented 3 years ago

@MCUdude Could you please update the maximum_size field in your manifests so it will represent the whole flash size. Then I will be able to subtract the bootloader size if no_bootloader isn't set. Thanks!

MCUdude commented 3 years ago

MCUdude Could you please update the maximum_size field in your manifests so it will represent the whole flash size. Then I will be able to subtract the bootloader size if no_bootloader isn't set. Thanks!

Sure! I'll see if I can submit a PR tonight.

valeros commented 3 years ago

@MCUdude Could you please take a look at https://github.com/platformio/platform-atmelavr/commit/216f574d0ca485570c6f8985574f229006c10e08. Did I miss anything in the calculation process?

MCUdude commented 3 years ago

@valeros looks good to me!