1

I'm pretty new to Arduino and I'm developing my first real-world project.

I have a Arduino Mega with a full working sketch in it which is going to be placed in several distant places and I have a number of "mantainers" that will need to get to the place, connect the board with their Windows (Vista+) laptop (USB), upload a new version of the sketch (if available), download some data from EEPROM and change (if needed) some parameters stored in PROGMEM. This just to give you the full picture, now I come to the questions.

What I'm trying to attempt is to write (in c++) a software that gives non-programmers a UI to do this stuff easily, so I wonder:

  1. How do I load the .hex file to the board? I kinda got I have to use AVRdude: is there any c++ example/tutorial/library I can use not to reinvent the wheel?
  2. When can I write to PROGMEM? When I connect with the serial cable Arduino stops the running sketch, waits for a new sketch and, if it does not arrive, it starts the old sketch back again. I guess that is the time to write to PROGMEM: if there is a running sketch I cannot do it, am I right?
  • The data in PROGMEM is actually part of your program: it is embedded in the .hex file you upload to the board. If you want to change persistent data without re-uploading the whole program, use the EEPROM instead of PROGMEM. – Edgar Bonet Jul 10 '15 at 10:40
  • @Edgar That's bad news, but thank you for pointing it out! This simplifies the process (update the sketch - talk to the sketch through serial - the sketch writes on EEPROM) but EEPROM is little enough as it is... Does it make sense to you to write my parameters in a text file and then compile the sketch on-the-fly (with this data in) through my UI using the CLI of Arduino IDE (which I would include in my software)? Choose one: A - it makes sense; B - no it does not; C - ask a new question for this – dirluca Jul 10 '15 at 10:51
  • I would go for the EEPROM if at all possible. If not, write the parameters to a binary file and link the sketch with that file (c.f. my updated answer). No need to recompile. Not sure the CLI of the Arduino IDE can do that, but avr-ld can. – Edgar Bonet Jul 10 '15 at 11:08
1

How do I load the .hex file to the board? I kinda got I have to use AVRdude: is there any c++ example/tutorial/library I can use not to reinvent the wheel?

Yes, you have to use avrdude. The simplest way is just to bundle avrdude.exe and the avrdude.conf file with your program and call it, along with its required parameters, with system() or some similar (eg execv()) method (or whatever Windows has decided to provide instead of those standard functions - who knows what goes through the minds of Windows developers...).

When can I write to PROGMEM? When I connect with the serial cable Arduino stops the running sketch, waits for a new sketch and, if it does not arrive, it starts the old sketch back again. I guess that is the time to write to PROGMEM: if there is a running sketch I cannot do it, am I right?

When you upload a new sketch you are writing to PROGMEM. That's really the only time you can do it. Yes, there are ways you can write pages of data to PROGMEM, and that's exactly what the bootloader does when you upload a sketch, but manipulating individual variables in PROGMEM is not possible without major brain surgery being done in your sketch. To store these kind of tweakable parameters you would normally use the EEPROM.

It can also be good to use external storage for some of these things to - either an SD card (overkill) or an external SPI or I2C EEPROM or Flash chip. This keeps all the data completely separate from your sketch so you can never overwrite that data when you upload a new sketch.

It is, of course, up to your sketch to provide some kind of interface to tweak the settings, whether stored in internal EEPROM or an external EEPROM chip. Creating some kind of serial protocol which your C++ program can interface with will be essential. Only while your sketch is running will you be able to change those variables. If you don't want to allow end users access to that interface then one method could be to have a separate sketch which is a "calibration and configuration" sketch which your program uploads in order to adjust the settings. Once you have finished it then re-uploads the original (or new) sketch to allow normal operation again.

  • When I'm on air I'm quite comfortable, I already have a serial protocol to communicate with the sketch. It was the upload part I had no clue about! Answer accepted, thanks a lot! – dirluca Jul 10 '15 at 11:05
  • Does it make sense to you to write my parameters in a text file and then compile the sketch on-the-fly (with this data in) through my UI using the CLI of Arduino IDE (which I would include in my softwareas well)? Choose one: A - it makes sense; B - no it does not; C - ask a new question for this – dirluca Jul 10 '15 at 11:06
  • I choose D: it depends on what the data parameters are and how big they are and if it's worth the extra effort. If you choose to do it that way you will need to include the compiler with your program which will make it much bigger and more complex to install. – Majenko Jul 10 '15 at 11:08
3

1) Why not just

system("avrdude ...");

That's more or less what the Arduino IDE does.

2) If you really want to embed the data in the executable, see Embedding Blobs in Binaries. You would need to re-link and re-upload the program each time you change that data. You won't need the Arduino IDE, as it's only job is basically to call avr-g++ and avrdude. You can use the IDE if you find it's CLI easier than those of the standard tools.

If you put the data in EPPROM instead, the avrdude can upload it without re-uploading the program.

  • So I would have to include the avrdude executable into my software installation? – dirluca Jul 10 '15 at 10:55
  • Definitely. It's part of the Arduino installation BTW. – Edgar Bonet Jul 10 '15 at 11:05
1

The avrdude/upload part has been fairly well covered by the other answers, but what has not been mentioned yet is that it is fairy trivial to modify program memory contents by modifying the hex file to be uploaded.

Basically, you just write hex-encoded bytes in the correct place in the file, and then update the checksum at the end of each line you modify. It can help if you put a readily recognizable placeholder sequence in the desired location in your source code, which you can look for in the hex file as an extra verification you are making your substitutions in the correct place. This is fairly straightforward string processing and unsigned byte math which you can accomplish in the language of your choice. There are also tools which can convert between hex files and binary images, and probably tools or libraries which can fix the checksums for you.

Do pay attention to the license terms of avrdude and any other non-original code or tools you distribute.

  • I'll keep this possibility very much into consideration, thanks – dirluca Jul 13 '15 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.