New Arduino Due Library: DueEEPROM

This is going to be a long post, so, sorry... I would add it to the Playground Libraries page but it seems that pages can't be edited at the moment. I'll rewrite this once that happens.

I needed to have a little bit of EEPROM on the Due but of course the SAM3X doesn't come with any. Fortunately the Mega16u2 that is between the USB programming port and the 3X does. It is only 512 bytes but I didn't need much.

This library requires hardware and software modification. If you aren't comfortable with soldering I would suggest learning on something else besides your Due or having someone else who can solder do it for you.

This mod works by temporarily highjacking the serial connection between the Due and the 16u2. It uses a pin on the Due and a spare pin on the 16u2 to control what it does with serial data it receives. When the EEPROM library is not in use or you're not doing any EEPROM operations the Due and the 16u2 act normally. When you use the library to read or write to the EEPROM, the Due outputs a HIGH on the control pin which causes the 16u2 to use the incoming data from the Due as EEPROM data. When the Due is done, it sets the pin LOW and either continues on with the sketch or waits for a response from the 16u2.

This does not interfere with a connection to your computer other than to possibly slow down data transfer if you're doing a lot of EEPROM operations. Keep in mind that even if you're not using Serial() to communicate with your computer, this library is, so pins 0 and 1 will not be useable. While you still could technically use pins 0 and 1 to communicate with another serial device, that device must not care if some data comes across that it doesn't understand. For the technically inclined, it would have to ignore any byte that is 0xd4 or 0xd5 and the three bytes that follow.


Hardware Modifications
There are four pins on the 16u2 that are broken out between the six pin header labeled ICSP and the power jack. These need to be cleared of solder and male or female header pins put there instead.

When using the library, you'll need to connect a jumper from a free pin on the Due to the pin that the arrow is pointing to in the picture.

Software Modifications
All of the code that I used for this project is available at GitHub - adamkouse/due_eeprom: Arduino Due EEPROM Library, IDE Mods and 16u2 Firmware
First, you need to reprogram your 16u2. An excellent tutorial on how to do this is at http://arduino.cc/en/Hacking/Upgrading16U2Due. You'll want to flash Arduino-usbserial-with-eeprom.hex.

There is a bug in the Arduino IDE as of 1.5.4 that will prevent this from working. I'll do a pull request here someday soon so hopefully they'll be fixed permanently but in the mean time you'll want to change the lines in the following file:
/hardware/arduino/sam/cores/arduino/UARTClass.cpp

while ((_pUart->UART_SR & UART_SR_TXRDY) != UART_SR_TXRDY)

with

while ((_pUart->UART_SR & UART_SR_TXEMPTY) != UART_SR_TXEMPTY)

Next, simply copy the DueEEPROM folder into /hardware/arduino/sam/libraries.

Usage
Examples are included with the library which you can check out in the usual method, File -> Examples -> DueEEPROM. When using the library, put the pin number that goes to the 16u2 into the begin() function. For example, if you want to use pin 12, do "DueEeprom.begin(12)".

Convenience Hax
If you want to go a little further, the SAM3X has a few pins on it that don't go to anything. We can use one of them as a 16u2 control line. To make life easier, I soldered a jumper wire from actual pin 93 on the SAM3X to the 16u2. In the picture below I outlined pin 90 as it's pretty easy to find because of the via close to it, then it is simply a matter of counting over three pins.

I also added Arduino pin 90 by adding it to variant.c and variant.h. If you're going to attempt this hack, simply overwrite your variant.* files or do a diff on them and the originals to find my changes.

Enjoy!

Thanks to the following for reference during this project:

http://hunt.net.nz/users/darran/weblog/15f92/

That's a really impressive work you've done! Great!

Really impressive! I especially liked the 'Interactive' example on how to use. Wish more Arduino examples were so complete and helpfull.

Did notice in the picture you have soldered to pin 93 but say pin 90. The github files also reference pin 90?

I haven't tried the mods yet. If the 512 is big enough for my use I will.

thanks

Hi,

thanks for the great library, its a cool idea and impressive work. I build everything according to your instructions. The interactive test sketch works great.

If i want to use it in my program, it doesnt work however. I am just guessing, but the data frm the 16u2 EEPROM cannot be transfered to the RAM of the SAM3X right?

In my program i am reading certain values stored in the EEPROM on startup and then use those values in my program. I also store them in variables and print those variables via a bluetooth module on Serial2. The program hangs here. So i guessed a real transfer of the EEPROM stored values to the running program is not possible.

Am i missing something?

Thank you,

Julia85

Caro amico, questa è una buona idea!
defraggler alternative