platformio / platform-atmelavr

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

Extra script for setting fuses and burning bootloader #153

Closed MCUdude closed 4 years ago

MCUdude commented 5 years ago

Hi!

As a developer working with AVRs I feel like the last thing missing from PlatformIO now is the ability to set the correct fuse bits and burn the correct bootloader based on the information given in platformio.ini.

MightyCore, MiniCore, and MegaCore (all supported by PlatformIO) comes with a bunch of pre-compiled bootloaders where all shares the same file names rules.

As long as board, board_build.f_cpu, and board_upload.speed is present in platformio.ini Is it possible for a script to figure out the correct fuse bits and load the correct bootloader hex file.

Preferably there should be three flags, one to only set the fuse bits, one to only burn the bootloader and one for doing both. Since the name and path of the bootloader are so extensive, we can check if the user entered a valid board_build.f_cpu, and board_upload.speed by checking if a bootloader with these values exists.

A bootloader for a >=64kB device is named like this:

bootloaders/atmega1284p/16000000L/optiboot_flash_atmega1284p_UART0_1000000_16000000L_BIGBOOT.hex

A bootloader for a <=32kB device is named like this:

bootloaders/atmega328p/16000000L/optiboot_flash_atmega328p_UART0_115200_16000000L.hex

By adding a new parameter to platformio.ini (board_upload.uart_port maybe?) we can specify if UART0, UART1 or UARTn should be used for uploading. Again, this decides what bootloader hex file is loaded.

Another fuse related feature is the ability to set the brownout detection level. This is possible in Arduino IDE through a separate menu option. Something like board_fuses.bod = 2.7V?

The last option should be to manually set fuses and override everything. My idea here would be to check if board_fuses.high, board_fuses.low and board_fuses.extended is present. If one or more is present, then this fuse is overridden by whatever value the particular boards_fuses fields holds

I have very little experience with python, but I think this should be doable, even for me if someone points me in the right direction. I also haven't worked with PlatformIO advanced scripting before, so forgive me if some of the following questions are silly to some.

EDIT: It seems like boards_mightycore.txt boards_minicore.txt and boards_megacore.txt already exist in my .platformio folder. Maybe these files can be parsed to make this job a little easier?

ivankravets commented 4 years ago

Thanks for the amazing work!

2.0.0 is out! https://github.com/platformio/platform-atmelavr/releases/tag/v2.0.0

MCUdude commented 4 years ago

Thank you for letting enthusiasts like me contribute to this great project. I will update the PlatformIO readme doc for MightyCore, MegaCore, and MiniCore to reflect these new changes.