📄 tos-bsl.in
字号:
while pstart<size:
length = self.maxData
if pstart+length > size:
length = size - pstart
data = data + self.bslTxRx(self.BSL_RXBLK,
pstart+startaddress,
length,
wait=wait)[:-2] #cut away checksum
pstart = pstart + length
return data
def txPasswd(self, passwd=None, wait=0):
"""transmit password, default if None is given."""
if DEBUG > 1: sys.stderr.write("* txPassword(%r)\n" % passwd)
if passwd is None:
#Send "standard" password to get access to protected functions.
sys.stderr.write("Transmit default password ...\n")
sys.stderr.flush()
#Flash is completely erased, the contents of all Flash cells is 0xff
passwd = chr(0xff)*32
else:
#sanity check of password
if len(passwd) != 32:
raise ValueError, "password has wrong length (%d)\n" % len(passwd)
sys.stderr.write('Transmit password ...\n')
sys.stderr.flush()
#send the password
self.bslTxRx(self.BSL_TXPWORD, #Command: Transmit Password
0xffe0, #Address of interupt vectors
0x0020, #Number of bytes
passwd, #password
wait=wait) #if wait is 1, try to sync forever
#-----------------------------------------------------------------
def actionMassErase(self):
"""Erase the flash memory completely (with mass erase command)"""
sys.stderr.write("Mass Erase...\n")
sys.stderr.flush()
self.bslReset(1) #Invoke the boot loader.
for i in range(self.meraseCycles):
if i == 1: sys.stderr.write("Additional Mass Erase Cycles...\n")
self.bslTxRx(self.BSL_MERAS, #Command: Mass Erase
0xff00, #Any address within flash memory.
0xa506) #Required setting for mass erase!
self.passwd = None #No password file required!
#print "Mass Erase complete"
#Transmit password to get access to protected BSL functions.
self.txPasswd()
def actionStartBSL(self, usepatch=1, adjsp=1, replacementBSL=None, forceBSL=0, mayuseBSL=0, speed=None, bslreset=1):
"""start BSL, download patch if desired and needed, adjust SP if desired"""
sys.stderr.write("Invoking BSL...\n")
sys.stderr.flush()
if bslreset:
self.bslReset(1) #Invoke the boot loader.
self.txPasswd(self.passwd) #transmit password
#Read actual bootstrap loader version.
#sys.stderr.write("Reading BSL version ...\n")
blkin = self.bslTxRx(self.BSL_RXBLK, #Command: Read/Receive Block
0x0ff0, #Start address
16) #No. of bytes to read
dev_id, bslVerHi, bslVerLo = struct.unpack(">H8xBB4x", blkin[:-2]) #cut away checksum and extract data
if self.cpu is None: #cpy type forced?
if deviceids.has_key(dev_id):
self.cpu = deviceids[dev_id] #try to autodectect CPU type
if DEBUG:
sys.stderr.write("Autodetect successful: %04x -> %s\n" % (dev_id, self.cpu))
else:
sys.stderr.write("Autodetect failed! Unkown ID: %04x. Trying to continue anyway.\n" % dev_id)
self.cpu = F1x #assume something and try anyway..
sys.stderr.write("Current bootstrap loader version: %x.%x (Device ID: %04x)\n" % (bslVerHi, bslVerLo, dev_id))
sys.stderr.flush()
self.bslVer = (bslVerHi << 8) | bslVerLo
if self.bslVer <= 0x0110: #check if patch is needed
self.BSLMemAccessWarning = 1
else:
self.BSLMemAccessWarning = 0 #Fixed in newer versions of BSL.
if self.bslVer <= 0x0130 and adjsp:
#only do this on BSL where it's needed to prevent
#malfunction with F4xx devices/ newer ROM-BSLs
#Execute function within bootstrap loader
#to prepare stack pointer for the following patch.
#This function will lock the protected functions again.
sys.stderr.write("Adjust SP. Load PC with 0x0C22 ...\n")
self.bslTxRx(self.BSL_LOADPC, #Command: Load PC
0x0C22) #Address to load into PC
#Re-send password to re-gain access to protected functions.
self.txPasswd(self.passwd)
#get internal BSL replacement if needed or forced by the user
#required if speed is set but an old BSL is in the device
#if a BSL is given by the user, that one is used and not the internal one
if ((mayuseBSL and speed and self.bslVer < 0x0150) or forceBSL) and replacementBSL is None:
replacementBSL = Memory() #File to program
if self.cpu == F4x:
if DEBUG:
sys.stderr.write("Using built in BSL replacement for F4x devices\n")
sys.stderr.flush()
replacementBSL.loadTIText(cStringIO.StringIO(F4X_BSL)) #parse embedded BSL
else:
if DEBUG:
sys.stderr.write("Using built in BSL replacement for F1x devices\n")
sys.stderr.flush()
replacementBSL.loadTIText(cStringIO.StringIO(F1X_BSL)) #parse embedded BSL
#now download the new BSL, if allowed and needed (version lower than the
#the replacement) or forced
if replacementBSL is not None:
self.actionDownloadBSL(replacementBSL)
#debug message with the real BSL version in use (may have changed after replacement BSL)
if DEBUG:
sys.stderr.write("Current bootstrap loader version: 0x%04x\n" % (self.bslVer,))
sys.stderr.flush()
#now apply workarounds or patches if BSL in use requires that
if self.bslVer <= 0x0110: #check if patch is needed
if usepatch: #test if patch is desired
sys.stderr.write("Patch for flash programming required!\n")
self.patchRequired = 1
sys.stderr.write("Load and verify patch ...\n")
sys.stderr.flush()
#Programming and verification is done in one pass.
#The patch file is only read and parsed once.
segments = Memory() #data to program
segments.loadTIText(cStringIO.StringIO(PATCH)) #parse embedded patch
#program patch
self.programData(segments, self.ACTION_PROGRAM | self.ACTION_VERIFY)
self.patchLoaded = 1
else:
if DEBUG:
sys.stderr.write("Device needs patch, but not applied (usepatch is false).\n") #message if not patched
#should the baudrate be changed?
if speed is not None:
self.actionChangeBaudrate(speed) #change baudrate
def actionDownloadBSL(self, bslsegments):
sys.stderr.write("Load new BSL into RAM...\n")
sys.stderr.flush()
self.programData(bslsegments, self.ACTION_PROGRAM)
sys.stderr.write("Verify new BSL...\n")
sys.stderr.flush()
self.programData(bslsegments, self.ACTION_VERIFY) #File to verify
#Read startvector of bootstrap loader
#blkin = self.bslTxRx(self.BSL_RXBLK, 0x0300, 2)
#blkin = self.bslTxRx(self.BSL_RXBLK, 0x0220, 2)
blkin = self.bslTxRx(self.BSL_RXBLK, bslsegments[0].startaddress, 2)
startaddr = struct.unpack("<H", blkin[:2])[0]
sys.stderr.write("Starting new BSL at 0x%04x...\n" % startaddr)
sys.stderr.flush()
self.bslTxRx(self.BSL_LOADPC, #Command: Load PC
startaddr) #Address to load into PC
#BSL-Bugs should be fixed within "new" BSL
self.BSLMemAccessWarning = 0
self.patchRequired = 0
self.patchLoaded = 0
#Re-send password to re-gain access to protected functions.
self.txPasswd(self.passwd)
#update version info
#verison only valid for the internal ones, but it also makes sure
#that the patches are not applied if the user d/ls one
self.bslVer = 0x0150
def actionEraseCheck(self):
"""check the erasure of required flash cells."""
sys.stderr.write("Erase Check by file ...\n")
sys.stderr.flush()
if self.data is not None:
self.programData(self.data, self.ACTION_ERASE_CHECK)
else:
raise BSLException, "cannot do erase check against data with not knowing the actual data"
def actionProgram(self):
"""program data into flash memory."""
if self.data is not None:
sys.stderr.write("Program ...\n")
sys.stderr.flush()
self.programData(self.data, self.ACTION_PROGRAM)
sys.stderr.write("%i bytes programmed.\n" % self.byteCtr)
sys.stderr.flush()
else:
raise BSLException, "programming without data not possible"
def actionVerify(self):
"""Verify programmed data"""
if self.data is not None:
sys.stderr.write("Verify ...\n")
sys.stderr.flush()
self.programData(self.data, self.ACTION_VERIFY)
else:
raise BSLException, "verify without data not possible"
def actionReset(self):
"""perform a reset, start user programm"""
sys.stderr.write("Reset device ...\n")
sys.stderr.flush()
self.bslReset(0) #only reset
def actionRun(self, address=0x220):
"""start program at specified address"""
sys.stderr.write("Load PC with 0x%04x ...\n" % address)
sys.stderr.flush()
self.bslTxRx(self.BSL_LOADPC, #Command: Load PC
address) #Address to load into PC
#table with values from slaa089a.pdf
bauratetable = {
F1x: {
9600:[0x8580, 0x0000],
19200:[0x86e0, 0x0001],
38400:[0x87e0, 0x0002],
},
F4x: {
9600:[0x9800, 0x0000],
19200:[0xb000, 0x0001],
38400:[0xc800, 0x0002],
},
}
def actionChangeBaudrate(self, baudrate=9600):
"""change baudrate. first the command is sent, then the comm
port is reprogrammed. only possible with newer MSP430-BSL versions.
(ROM >=1.6, downloadable >=1.5)"""
try:
baudconfigs = self.bauratetable[self.cpu]
except KeyError:
raise ValueError, "unknown CPU type %s, can't switch baudrate" % self.cpu
try:
a,l = baudconfigs[baudrate]
except KeyError:
raise ValueError, "baudrate not valid. valid values are %r" % baudconfigs.keys()
sys.stderr.write("Changing baudrate to %d ...\n" % baudrate)
sys.stderr.flush()
self.bslTxRx(self.BSL_CHANGEBAUD, #Command: change baudrate
a, l) #args are coded in adr and len
time.sleep(0.010) #recomended delay
self.serialport.setBaudrate(baudrate)
def actionReadBSLVersion(self):
"""informational output of BSL version number.
(newer MSP430-BSLs only)"""
ans = self.bslTxRx(self.BSL_TXVERSION, 0) #Command: receive version info
#the following values are in big endian style!!!
family_type, bsl_version = struct.unpack(">H8xH4x", ans[:-2]) #cut away checksum and extract data
print "Device Type: 0x%04x\nBSL version: 0x%04x\n" % (family_type, bsl_version)
def usage():
"""print some help message"""
sys.stderr.write("""
USAGE: %s [options] [file]
Version: %s
If "-" is specified as file the data is read from the stdinput.
A file ending with ".txt" is considered to be in TIText format,
'.a43' and '.hex' as IntelHex and all other filenames are
considered as ELF files.
General options:
-h, --help Show this help screen.
-c, --comport=port Specify the communication port to be used.
(Default is 0)
0->COM1 / ttyS0
1->COM2 / ttyS1
etc.
-P, --password=file Specify a file with the interrupt vectors that
are used as password. This can be any file that
has previously been used to program the device.
(e.g. -P INT_VECT.TXT).
-f, --framesize=num Max. number of data bytes within one transmitted
frame (16 to 240 in steps of 16) (e.g. -f 240).
-m, --erasecycles=num Number of mass erase cycles (default is 1). Some
old F149 devices need additional erase cycles.
On newer devices it is no longer needed. (e.g. for
an old F149: -m20)
-U, --unpatched Do not download the BSL patch, even when it is
needed. This is used when a program is downloaded
into RAM and executed from there (and where flash
programming is not needed.)
-D, --debug Increase level of debug messages. This won't be
very useful for the average user...
-I, --intelhex Force fileformat to IntelHex
-T, --titext Force fileformat to be TIText
-N, --notimeout Don't use timeout on serial port (use with care)
-B, --bsl=bsl.txt Load and use new BSL from the TI Text file
-S, --speed=baud Reconfigure speed, only possible with newer
MSP403-BSL versions (>1.5, read slaa089a.pdf for
details). If the --bsl option is not used, an
internal BSL replacement will be loaded.
Needs a target with at least 2kB RAM!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -