Which parts of Arduino source code enables writing on Flash through USB?

Hello, I have a project which requires data to be stored on ROM on my Leonardo. I intend to write an Android application in order to be able to change this data from my phone. Can you please tell me which part of the Arduino code I can reuse in order to load the Flash memory (or even burn the entire sketch) for this purpose?

I'm thus in need for the USB stuff and also the access to the Arduino ROM.

Thank you in advance!

Your description is not at all clear.

If you mean that you want to save some data to the EEPROM memory on your Leonardo that is straightforward.

But you can't save an Arduino sketch into the EEPROM and have it run from there.

If you are not referring to the Arduino EEPROM memory please explain what you mean.

Then you refer to "change this data from my phone" and in your title you refer to "through USB". Generally speaking you cannot connect an Arduino to an Android phone using USB. The easiest way to communicate between an Arduino and a phone is with Bluetooth.

Please explain what the purpose of all this is - not how you think it might be done.

...R

As I said, I want to rewrite data stored into the Flash memory (not the EEPROM). So I can burn my program each time with the new data updated if there is no other way. I'd like to know which files in the source code of Arduino IDE deals with opening an USB port and burning the sketch. There's no room for Bluetooth in my project at present. Regarding "what" is my project, it's a game. I don't meet any difficulty in coding nor burning it.

emilbarton: I'd like to know which files in the source code of Arduino IDE deals with opening an USB port and burning the sketch.

I'm still not sure I understand.

From IDE 1.5.x onwards you can use the IDE from the command line to compile and upload programs. This Thread shows how to use that with the Geany editor.

If you want to get further into the nitty-gritty you can get set verbose mode in the IDE so that it lists all the steps in the compile or upload process. A program called avrdude is used for uploading. I believe it can be used to upload hex files that contain the compiled program code.

...R

The only code that can write to the flash memory has to be located in the boot loader. It is not possible to write to flash from other sectors of the memory space.

So look at the code for the boot loader for how to do this. However that area of memory is very nearly full. Some one did write a boot loader that included a write to flash function you could call but as I am on a mobile device I do not have access to it.

However it seems to me like you might not have a solid idea of what the Arduino is and what it does.

OK, for this "game" what are you storing that changes slowly over time? The high scores list? Parameters like difficulty levels and other player settings? Large maps of dungeon levels?

The EEPROM on a Leonardo is 2KB. This is not a lot. You can't store much 'map' there. It's perfect for high scores and other small pieces of information. If you need to store more than 2KB then the usual solution is an SD card. Then you can pop the card out and put it into your PC to update the data.

If the size of the EEPROM is suitable then it's not possible to write this direct from Android. You can do it with a PC but that is slightly awkward. The best way is to program your sketch to talk to the outside world (eg Bluetooth) and then it can edit the EEPROM data itself.

The SD card solution is clearly the best. I was hoping someone had already coded something in order to write static data onto flash without having to recompile the program, but it seems at least uneasy. I wish to store the player's environment in my game, at a low level it's only a few Kb, but users should be able to load (and define) richer contexts.

Thanks for your answers.

without having to recompile the program,

That is not the way the Arduino works. It can not cope with pre compiled libraries. There are a few SD card libraries around but they take up a substantial amount of Arduino flash memory and RAM.