Take the 2-minute tour ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
Would +1 for quoting datasheet (not enough rep) –  mypal125 Aug 18 at 14:18

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.

share|improve this answer
    
+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 –  Lưu Vĩnh Phúc Aug 18 at 7:41
    
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... –  Annonomus Penguin Aug 18 at 22:45
    
Yeah, but if you're going to do that then you may as well run something fun like Forth or Python. –  Ignacio Vazquez-Abrams Aug 18 at 22:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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