Pages: [1]   Go Down
Author Topic: Writing to flash memory at run-time?  (Read 1948 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley
Logged

Portugal
Offline Offline
God Member
*****
Karma: 6
Posts: 962
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thats what the bootloader does..
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 208
Posts: 12944
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Instead of Flash use EEPROM.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8535
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 303
Posts: 26354
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the responses guys. smiley

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
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8535
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: April 13, 2011, 06:48:04 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 303
Posts: 26354
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 135
Posts: 6765
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27147
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: