Calculating MSP430 BSL Checksum

Texas Instruments has a quick little java tool for calculating the checksum of a MSP430 dataframe. Recently, I’ve been seeing some forum posts looking for information on how to calculate the checksum so I thought I would post my quick Python 3 function for others.

def checksum_bsl_frame(frame):
  # http://docs.python.org/3.2/library/struct.html#format-characters
  fmt = "<" + len(frame) // 2*"H"

words = struct.unpack(fmt, frame)

words_xored = 0;

for word in words:
    words_xored ^= word

checksum = words_xored ^ 0xFFFF

return checksum.to_bytes(2, 'little')

Here, framis a bytes or bytesarray object and checksum returns a bytes objects.

Remember, strings are strings in Python3 and bytes and bytearray are data in Python3. Of course, if you are running PySerial on Python3 then this is the format you will be using.

The above code could be simplified I’m sure but it’s nice to have endianness defined out and in the open.

When you want to “human” check the data frame there is an easy one liner way of printing a formatted output.

def print_bsl_frame(frame):
  print(" ".join('{:02X}'.format(byte) for byte in frame))

Now, it’s easy to do things like…

password = bytearray.fromhex("80 10 24 24 00 00 00 00" + 32*" FF")
password.extend(checksum_bsl_frame(password))
print_bsl_frame(password)

The previous would print “80 10 24 24 00 00 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 5B CB” to the screen.

I hope this helps someone, it will help you more in a custom class definition.