Storing Code on an I2C EEPROM

Greetings all!

I have recently been experimenting with different Arduino projects. I have been playing with NeoPixels, shift registers, LED matrices, and I2C EEPROMs. All fun stuff! :slight_smile:

I may be new to the Arduino platform, but I have been tinkering with electronics since I was little and I am a retired computer programmer. I am learning the limits and strengths of micro controllers (ATMegas and ATTinys). Wish I had this stuff when I was a kid! LOL But I digress!

I was just thinking about using an EEPROM to store some data to transfer to a matrix or a color sequence for an array of NeoPixels and I understand how to store and retrieve data from one of these little gems.

What I wanted to know is this: Can I store executable code on the EEPROM and load it into my Arduino then call it as a function?

I believe that there is no way to directly execute code that could be stored on an external EEPROM nor is there a way to do that on the internal EEPROM. But is there a way to load that code from the external EEPROM into the code space of the micro controller and call it?

I was thinking that the use for this might be creating a module you could plug in and any special functions could be stored on the module then transferred into the micro controller’s flash and do its thing. Or store an animation routine with data on a replaceable EEPROM that you can swap out without having to reprogram the main controller when you want to change it.

Anyway thanks for the input in advance.

Don

Can I store executable code on the EEPROM and load it into my Arduino then call it as a function?

Do you feel ready to write your own boot loader?

You would have to write a custom bootloader that does not trash the main body of the code, just grabs the function and places it at a known location. Having done that it would also have to have some way of determining what needs to be done so you can still load the main code. Maybe have a GPIO pin that's tested on entry to the bootloader.

Also is it possible to get GCC to generate code at a given address? Or is the code position independent anyway?


Rob

What I wanted to know is this: Can I store executable code on the EEPROM and load it into my Arduino then call it as a function?

no you can’t use it for native AVR assembly
however you can write a scripting language interpreter that fetches its commands from EEPROM.
Check - bitlash.net -

dmilton2004: What I wanted to know is this: Can I store executable code on the EEPROM and load it into my Arduino then call it as a function?

Assume you make a switch-case statement large enough to cover all math operations & transcendals such as sin & cos. You could read the verb from eeprom/SD, look it up in a table, and use the table pointer to switch-case. This avoids a BASIC style interpreter but stills requires an offset either through an array search or by entering the offset value directly in the eeprom (short circuit the lookup.) You can then read the arguments from serial, eeporom, SD, or a pheripheral device.

As an example of how this may be put together, consider this very simple application that parses the serial stream and functions as a calculator: http://www.hackster.io/rayburne/scientific-calculator

There really is no limit on how diverse you can build such a machine... You just need to ensure that during the sketch compile that you have linked-in all of the AVR/Arduino functions required to support your interpreter... The switch case construct ensures this, but other implementations must ensure that the compiler "knows" about external references.

Ray

As the others have suggested, the simplest thing would be to store in EEPROM some sort of sequence information. You can make up your own system, but maybe something like: action / duration.

WOW! Now that I think of it, that does make sense. So what I am getting is even if I was able to store the AVR Assembler code that the compiler generates, that code would be specific and have direct addressing. That and the only way to put anything into the Flash potion of the micro controller, that can only be done from the boot loader.

That definitely does NOT sound like a quick and easy solution! LOL :fearful:

So the short answer is "No there is no way to add code into Flash storage"

And the long answer is "Study how to do what the boot loaders does, and re-write it to suit your purposes"

Just a quick thought... Assuming that I could re-write the boot loader, would you still have access to I2C components? Like an external EEPROM? And the boot loader would have to contain any libraries to access routines you would use?

Just a mind-numbing thought! LOL

On the Atmega328 you have 32k of PROGMEM and 1k of EEPROM. I don't know what the big attraction is of trying to put your code in something that is 32 x smaller. (Oh, I see you are using an external EEPROM. Even so, it would have to be pretty large).

However if there is some reason (eg. downloading new sequences on the fly) then I would go for some sort of P-code system as suggested earlier, where the large program in program memory uses the the codes in the EEPROM to guide its behaviour.

Just a quick thought... Assuming that I could re-write the boot loader, would you still have access to I2C components? Like an external EEPROM? And the boot loader would have to contain any libraries to access routines you would use?

You could make a bootloader that could access I2C. The current one doesn't need to.

I think this is overcomplicating whatever-it-is you are trying to do. Another solution is to attach an SD card reader, and have your code read in a file and have that file direct the behaviour of the code (eg. light patterns).

I have code here that lets one Arduino program another via ICSP. Conceivably you could have two processors, and the first programs the second, and then lets the second run the new program.

dmilton2004: Just a quick thought... Assuming that I could re-write the boot loader, would you still have access to I2C components? Like an external EEPROM? And the boot loader would have to contain any libraries to access routines you would use?

Existing I2C AVR boot loader: http://docwiki.gumstix.org/index.php?title=Robostix_i2c_bootloader

A little google goes a longggg way.

Ray