Writing to flash memory at run-time?

Hi,

I'd like to know if it is possible to write to the flash memory of my micro (Atmega 328P - Arduino Uno) during run-time.
I'm currently in the process of writing a simple interpreter for it, which I want to be able to receive user code via serial, and then store it in flash memory, so that the code remains on the micro after reset. The last part is what I'm having difficulty with. I'm not sure how or if it is even possible to store data in flash memory during run-time.

The only alternative I see to this, is making a program which generates a sketch for the user, which includes both the interpreter and the user's code. However, I'd really like to avoid having to do this.

Thanks. :slight_smile:

Thats what the bootloader does..

Instead of Flash use EEPROM.

You can write to flash but you'll have to delve into some special instructions. Better left alone really. As CB says, use the EEPROM, that's what it's for.


Rob

You can write to flash but you'll have to delve into some special instructions.

...and you probably need to rewrite the bootloader, or at least add to the code in the boot sector.
Fiddly, but not impossible.

Thanks for the responses guys. :slight_smile:

The problem with using thee EEPROM is that it is very limited. On the Atmega 328P I only have 1024 bytes of EEPROM. If I use 2 bytes per instruction in the user code (which is assembly-like and can therefore be mapped directly to a binary representation) that gives me a maximum of 512 instructions. Also this seems like a waste since the interpreter itself won't be very large and that means that a lot of the flash memory would be unused.

I'd really like it if there was an easy way to write to the flash memory like the read() and write() methods for the EEPROM. But I realize that probably isn't happening any time soon. :drooling_face:

You could use an off-chip flash or eeprom but this will slow the interpretation down. How fast does this have to run, presumably AFAP.

Do all your "opcodes" have to be 16 bits? Maybe if we see the format we can suggest other options such as static lookup tables in flash.


Rob

I'd really like it if there was an easy way to write to the flash memory like the read() and write() methods for the EEPROM

Flash writing is a page-at-a-time, having first erased the page in question.

How big is your interpreter?
Maybe with a rewrite, you could merge your interpreter with bootloader code, and keep the interpreter running in an expanded bootloader space.
It isn't really an Arduino anymore then, though.

I'd really like it if there was an easy way to write to the flash memory like the read() and write() methods for the EEPROM. But I realize that probably isn't happening any time soon.

Only code running in the bootloader section can write flash using the SPM instruction.
The easiest way would be to put a "write flash" function in the bootloader section (possibly expanding the bootloader section so that it will fit), at a "well known" location that code in the application section can call as an external C function. This would be a bit messy to set up within the compiler/sketch, but not impossible, I think.

you can Use a External EEPROM for me is easier than writing in the flash.

AS one kilobyte of EEPROM storage may isnt enough for your project : there are several external EEPROM chips available, as Microchip’s 24AA256 32 KB I2C-enabled EEPROM.

I would go with external FRAM. Non-volatile storage like EEPROM, but with SRAM access speeds (vs 3.3mS for EEPROM) for fast access via SPI.
How much memory did you need?

It became possible in 2015 : Writing to FLASH from application. Please test and enjoy :-) - Microcontrollers - Arduino Forum
(I have added this post with the link to this old topic, since Google shows this old topic and not the new topic about writing to flash memory).