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"
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.");
-
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
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.
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 – Maximilian Gerhardt Oct 24 '20 at 23:19