8

Say I wrote a compiled sketch to EEPROM then read it. Could I run the program from EEPROM? I guess the question is: Can an Arduino run software not in flash memory in the middle of executing the software in flash?

2 Answers 2

9

Short answer: No; From the atmega328's data sheet (though it applies to all AVR's):

AVR uses a Harvard architecture – with separate memories and buses for program and data. Instructions in the program memory are executed with a single level pipelining. While one instruction is being executed, the next instruction is pre-fetched from the program memory

...

Program Flash memory space is divided in two sections, the Boot Program section and the Application Program section. Both sections have dedicated Lock bits for write and read/write protection. The SPM instruction that writes into the Application Flash memory section must reside in the Boot Program section

It's architecture prevents using external program memory, but you can load anything into program memory at boot. At that point I would venture to say you are just programming AVR and not arduino, since you would need to replace the arduino bootloader and break the arduino IDE's ability to upload programs.

Alternately you could also use an emulator or interpreter of some intermediate language; basically code that steps through and runs other code. As a general rule an emulator runs around 8x slower.

There are micro-controllers from other brands that do support this functionality, I know a couple different PIC's do.

0
6

No. The foundation of a Harvard architecture such as AVR is to only allow code that exists within program space to be executed, and EEPROM is not within program space. It is possible, however, to write a virtual machine that will run from flash. This VM can then read program-become-data from anywhere and take action based on it.

3
  • +1. Note that some MCUs use modified Harvard architecture that allows the CPU to read/write data in program space, so you can copy the code from RAM to ROM and run it like self-modifying code
    – phuclv
    Commented Aug 18, 2014 at 7:41
  • 1
    For the VM thing, all you'd have to do is get the compiled C++, check the EEPROM for which instruction to be ran, and run that instruction. Of course, running it nativity on the chip would be multiple times faster and that is no easy task, but it'd be a fun project to learn about lower level stuff on the 328P... Commented Aug 18, 2014 at 22:45
  • Yeah, but if you're going to do that then you may as well run something fun like Forth or Python. Commented Aug 18, 2014 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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