Go Down

Topic: Writing to flash memory at run-time? (Read 2286 times) previous topic - next topic

ecustic

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. :)

Senso


Coding Badly


Graynomad

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
Rob Gray aka the GRAYnomad www.robgray.com

AWOL

Quote
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

ecustic

Thanks for the responses guys. :)

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.  :smiley-fat:

Graynomad

#6
Apr 13, 2011, 01:43 pm Last Edit: Apr 13, 2011, 01:48 pm by Graynomad Reason: 1
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
Rob Gray aka the GRAYnomad www.robgray.com

AWOL

Quote
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

westfw

Quote
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.

sndrader

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.

CrossRoads

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?

http://www.mouser.com/Semiconductors/Memory/F-RAM/_/N-488wv?Keyword=fram+memory&FS=True&Ns=Pricing|0

http://www.digikey.com/product-search/en?FV=fff40027%2Cfff80434&k=fram+memory&mnonly=0&newproducts=0&ColumnSort=0&page=1&stock=1&quantity=0&ptm=0&fid=0&pageSize=25
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up