0

I want to write a program in which I can select a HEX file recorded on a SD card (via a LED screen, etc.) and run it, i.e. transfer control to it, like launching programs on a PC. Is this possible and if so, how? I only found ways to directly run a sketch from an SD card without that "program"

4
  • this may work ... arduino-forth.com
    – jsotola
    Oct 24 '20 at 20:46
  • 2
    Bootloaders like Optiboot accept a program via UART and burn it into the flash of the MCU. That code is openly available: github.com/Optiboot/optiboot/blob/master/optiboot/bootloaders/…. With lots of effort, you too can write a bootloader which loads a compiled (or intel hex format) sketch from an SD card via SPI. The nearest project to that that aI could find is github.com/zevero/avr_boot which looks for a FIRMWARE.BIN on the SD card, programs the internal flash and runs it. You might want to modify that.. or, do it on another MCU with less restrictions Oct 24 '20 at 23:19
  • @MaximilianGerhardt, burn SD bootloader and then you use SD library to copy the selected file to firmware.bin and reset. the new firmware should have the same capability of selecting a firmware
    – Juraj
    Oct 25 '20 at 5:28
  • @MaximilianGerhardt If you wish to answer the question, please post an answer.
    – Nick Gammon
    Oct 25 '20 at 8:31
0

The HEX files on SD card would have to be linked for a specific address in flash. Normal program is linked for address zero on ATmega, but the bootloaders are an example on how to link for specific address in flash.

Then you could use the do_sdm() function provided by Optiboot 8 (see the example) to write the bytes from the HEX file to the right location in flash memory. Next step would be to call the start address as function same way as do_spm() is made accessible with optiboot.h in the example.

typedef void (*loaded_function_t)();

const loaded_function_t loaded_function = (loaded_function_t)(ADDRESS_OF_LOADED_FUNCTION);

then you can use

  Serial.prinln("Execute the loaded code...);
  loaded_function();
  Serial.prinln("Loaded code finished.");
2
  • 1) Sorry, but I don't understand how to link HEX files from SD card for a specific address in flash. How to do it? 2) I've not found do_sdm() function in Optiboot. What did you mean?
    – Andezit
    Oct 28 '20 at 20:52
  • 2) there is a link in the answer 1) see how bootloaders are compiled and linked for specific address
    – Juraj
    Oct 29 '20 at 6:40
0

Not with an Uno, no, it's not possible. Or it's very very hard and the results would be less than desirable. You need a proper MCU with lots of RAM and the ability to run code from it, which you can't with an Uno.

It would be less work to write your own scripting language to control things from a text file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.