0

For a project i need to extract some data from an external eeprom and then store it somewhere. Suddenly after some manipulations (not necessarly with Arduino board) i need to put them in an external eeprom.

What I would do is to convert the extracted data from the EEPROM in Intel Hex data and vice versa. To do this I wanted to know if there is already a library for that purpose "ready to use" made for Arduino boards.

thanks

0

Why does it have to be in Intel Hex format? You could write out the EEPROM to the serial port one byte to a line, or something. Something like this:

#include <EEPROM.h>

void setup ()
{
  Serial.begin (115200);
  Serial.println ();

  for (int addr = 0x0000; addr < 0x0010; addr++)
    {
    Serial.print ("Address = ");
    Serial.print (addr, HEX);
    Serial.print (", value = ");
    Serial.println (EEPROM.read(addr), HEX);
    }

}  // end of setup

void loop ()
{
}  // end of loop

I'm not personally aware of a library that happens to write out/read in, Intel Hex format from/to the EEPROM.


I have code that uploads and downloads files to the Arduino. Inside that is code to read/write Intel hex format. However it isn't a library.

Inside File_Utils.ino are some comments that show the format:

/*
Line format:

  :nnaaaatt(data)ss

  Where:
  :      = a colon

  (All of below in hex format)

  nn     = length of data part
  aaaa   = address (eg. where to write data)
  tt     = transaction type
           00 = data
           01 = end of file
           02 = extended segment address (changes high-order byte of the address)
           03 = start segment address *
           04 = linear address *
           05 = start linear address *
  (data) = variable length data
  ss     = sumcheck

            * We don't use these

*/

In that file is code to write Intel Hex format (reading from Flash, not EEPROM):

void readFlashContents ()
  {
  if (!haveSDcard)
    {
    Serial.println (F("*** No SD card detected."));
    return;
    }

  progressBarCount = 0;
  pagesize = currentSignature.pageSize;
  pagemask = ~(pagesize - 1);
  oldPage = NO_PAGE;
  byte lastMSBwritten = 0;

  while (true)
    {

    Serial.println ();  
    Serial.println (F("Choose file to save as: "));

    getline (name, sizeof name);
    int len = strlen (name);

    if (len < 5 || strcmp (&name [len - 4], ".HEX") != 0)
      {
      Serial.println (F("File name must end in .HEX"));  
      return;
      }

    // if file doesn't exist, proceed
    if (!sd.vwd()->exists (name))
      break;

    Serial.print (F("File "));
    Serial.print (name);    
    Serial.println (F(" exists. Overwrite? Type 'YES' to confirm ..."));  

    if (getYesNo ())
      break;

    }  // end of checking if file exists

  // ensure back in programming mode  
  if (!startProgramming ())
    return;  

  SdFile myFile;

  // open the file for writing
  if (!myFile.open(name, O_WRITE | O_CREAT | O_TRUNC)) 
    {
    Serial.print (F("Could not open file "));
    Serial.print (name);
    Serial.println (F(" for writing."));
    return;    
    }

  byte memBuf [16];
  bool allFF;
  unsigned int i;
  char linebuf [50];
  byte sumCheck;

  Serial.println (F("Copying flash memory to SD card (disk) ..."));  

  for (unsigned long address = 0; address < currentSignature.flashSize; address += sizeof memBuf)
    {

    unsigned long thisPage = address & pagemask;
    // page changed? show progress
    if (thisPage != oldPage && oldPage != NO_PAGE)
      showProgress ();
    // now this is the current page
    oldPage = thisPage;

    // don't write lines that are all 0xFF
    allFF = true;

    for (i = 0; i < sizeof memBuf; i++)
      {
      memBuf [i] = readFlash (address + i); 
      if (memBuf [i] != 0xFF)
        allFF = false;
      }  // end of reading 16 bytes
    if (allFF)
      continue;

    byte MSB = address >> 16;
    if (MSB != lastMSBwritten)
      {
      sumCheck = 2 + 2 + (MSB << 4);
      sumCheck = ~sumCheck + 1;      
      // hexExtendedSegmentAddressRecord (02)
      sprintf (linebuf, ":02000002%02X00%02X\r\n", MSB << 4, sumCheck);    
      myFile.print (linebuf);   
      lastMSBwritten = MSB;
      }  // end if different MSB

    sumCheck = 16 + lowByte (address) + highByte (address);
    sprintf (linebuf, ":10%04X00", (unsigned int) address & 0xFFFF);
    for (i = 0; i < sizeof memBuf; i++)
      {
      sprintf (&linebuf [(i * 2) + 9] , "%02X",  memBuf [i]);
      sumCheck += memBuf [i];
      }  // end of reading 16 bytes

    // 2's complement
    sumCheck = ~sumCheck + 1;
    // append sumcheck
    sprintf (&linebuf [(sizeof memBuf * 2) + 9] , "%02X\r\n",  sumCheck);

    myFile.clearWriteError ();
    myFile.print (linebuf);   
    if (myFile.getWriteError ())
       {
       Serial.println ();  // finish off progress bar
       Serial.println (F("Error writing file."));
       myFile.close ();
       return; 
       }   // end of an error

    }  // end of reading flash 

  Serial.println ();  // finish off progress bar
  myFile.print (":00000001FF\r\n");    // end of file record
  myFile.close ();  
  // ensure written to disk
  sd.vwd()->sync ();
  Serial.print (F("File "));
  Serial.print (name);
  Serial.println (F(" saved."));
  }  // end of readFlashContents

That should give you some tips about how to write out the file. Look in my code to see the reading code as well. However as I said, you don't necessarily have to use that format if you just want to manipulate the data yourself.

  • But the Intel Hex isn't made for that purpose? – xxx Mar 27 '16 at 11:15
  • Hex is Hex. Intel Hex is a file format for storing data (in ASCII-HEX) to be loaded to specific addresses. It's a very plain ASCII format and easy to read, write (by humans) and to code for. There really isn't much more to do than skip past an address value and start reading byte data. What might a library to do for you? – JRobert Mar 27 '16 at 15:33
0

Nick's answer assumes you just want to read/process/write the data from one place to another. If that's the case, there's no need to store the data into an Intel Hex Data formatted file – just keep it in a flat array, or a file that's formatted conveniently rather than in a form that requires parsing.

Of course, if you actually need the formatted file for some reason – such as an intermediate program only working with Hex files – you could edit the question and say so.

Note, Python Software Foundation's IntelHex page provides a

Python library to read, write, create from scratch and manipulate data from HEX (also known as Intel HEX) file format.

To use it, you would write a Python program that (on your host computer) reads serial-port data with values to be stored, and uses the IntelHex library to write a Hex file. After the intermediate program fiddles with the Hex file, another Python program on your host computer uses IntelHex to read the modified Hex file and writes it to EEPROM or to serial for an Arduino to write it to an EEPROM.

0

avrdude himself is very good at providing/inferring the various formats. We are ourselves manipulating output - EEPROMs, for example - in an external editor.

Of course, avrdude is neither external nor a library, but...

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.