Upload binary file to board over serial

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
blnkjns
 
Posts: 963
Joined: Fri Oct 02, 2020 3:33 am

Upload binary file to board over serial

Post by blnkjns »

I'm trying to upload firmware over serial but I get a time-out error.
I wrote this code for the Metro M4 (that board can hold 50kB of firmware):

Code: Select all

void uploadSignature(){
  Serial1.print("signature ");
  Serial1.print(SIGNATURESIZE);
  Serial1.write(' ');
  Serial1.write(0x02);
  for (int n=0;n<SIGNATURESIZE;n++){
    //Serial1.print(signature[n],HEX);
    Serial1.write(signature[n]);
  }
  Serial1.write(0x03);
}
The signature and file are encoded in a C .h file as HEX array.
I was trying to follow the instruction that is send to the serial monitor if you type help:

Code: Select all

BuildHAT bootloader version 1631704133 2021-09-15T12:08:53+01:00
Commands available:
  help, ?                  : display this text
  version                  : display version string
  load <length> <checksum> : initiate upload
                             followed by <STX><<length> data bytes><ETX>
  signature <length>       : upload signature
                             followed by <STX><<length> data bytes><ETX>
  clear                    : clear any uploaded image
  verify                   : verify upload
  reboot
If I use the code I get an error on the serial monitor:
"Timed out waiting for data".
What am I doing wrong? Should the data be send as HEX, are the STX/ETX commands wrong? What is checksum and what is signature? The latter was in a 64 byte file.

This might clearify things: under Python the firmware is uploaded with this code I believe:

Code: Select all

def loadfirmware(self, firmware, signature):
        """Load firmware

        :param firmware: Firmware to load
        :param signature: Signature to load
        """
        with open(firmware, "rb") as f:
            firm = f.read()
        with open(signature, "rb") as f:
            sig = f.read()
        self.write(b"clear\r")
        self.getprompt()
        self.write(f"load {len(firm)} {self.checksum(firm)}\r".encode())
        time.sleep(0.1)
        self.write(b"\x02", replace="0x02")
        self.write(firm, replace="--firmware file--")
        self.write(b"\x03\r", replace="0x03")
        self.getprompt()
        self.write(f"signature {len(sig)}\r".encode())
        time.sleep(0.1)
        self.write(b"\x02", replace="0x02")
        self.write(sig, replace="--signature file--")
        self.write(b"\x03\r", replace="0x03")
        self.getprompt()

User avatar
blnkjns
 
Posts: 963
Joined: Fri Oct 02, 2020 3:33 am

Re: Upload binary file to board over serial

Post by blnkjns »

Figured it all out. I needed to calculate a checksum, and first send a return after the "header" and a small time delay (100ms worked) before sensing the data stream wrapped in 0x02 and 0x03.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”