Reprogram MBED via Serial Port

13 Sep 2012

Hi all!

I have my MBED integrated with another device via serial port. This second device has GPRS connection, so I just got intrigued by the possibility to transfer a new binary file to the MBED using the Serial Port.

After the transfer I would delete the other binary files from the LocalFileSystem and execute the mbed_reset() instruction to let it load the new program.

Unfortunately I am struggling when creating the new binary file. I do not know which function can I call to write each incoming byte from the serial port into a new binary file inside the LocalFileSystem.

Diving into the forums I have seen some people are using the fwrite() call, but my compiler does not agree with it. Am I missing a library??

Any help would be much appreciatted!

Thanks in advance!

Mario

28 Sep 2016

Did you ever find a solution to this? I want to do the same thing.

29 Sep 2016

It should be something along the lines of

// open the file for binary writes
FILE *newFile = fopen(path,"wb");

if (!newFile)
  return;

bool gotStartFlag = false;

// look for a fixed 32 bit start of file flag. 
while (!gotStartFlag) {
  while (serial.getc() != startChar) {}
  if (serial.getc() == secondChar)
    if (serial.getc() == thirdChar)
      if (serial.getc() == fourthChar)
        gotStartFlag = true;
}

// get the file length (check the endienness)
uint32_t fileLength = 0;
fileLength += (uint32_t)serial.getc() 
fileLength += ((uint32_t)serial.getc()) << 8;
fileLength += ((uint32_t)serial.getc()) << 16;
fileLength += ((uint32_t)serial.getc()) << 24;

// write the file
while (fileLength) {
  char dataIn = serial.getc();
  fwrite(&dataIn,1,1,newFile);
  fileLength--;
}

// All done. close the file
fclose(newFile);

Having said that adding some sort of checksum system would probably be a good idea.

29 Sep 2016

Thank you!

One more question: when I copy a binary file generated in the online compiler and copy it to the mbed device mass storage device, am I copying it to flash memory on the ARM microcontroller or to flash on the debugger chip (when the mbed device has a built in debugger like the ST Nucleo boards)?