Adding 3DR's stm32loader.py changes

This commit is contained in:
Allan Matthew 2015-08-05 08:48:36 -07:00
parent 7f30bbb2f9
commit c469fe573b

View File

@ -90,23 +90,41 @@ class CommandInterface:
# Unknown responce # Unknown responce
raise CmdException("Unknown response. "+info+": "+hex(ask)) raise CmdException("Unknown response. "+info+": "+hex(ask))
def setPin(self, pin, value):
try:
exp_fd = open("/sys/class/gpio/export", "w")
exp_fd.write(str(pin))
exp_fd.close()
except IOError:
pass
#print "Pin",pin," already exported"
dir_fd = open("/sys/class/gpio/gpio"+str(pin)+"/direction","w")
dir_fd.write("out")
dir_fd.close()
val_fd = open("/sys/class/gpio/gpio"+str(pin)+"/value","w")
val_fd.write(str(value))
dir_fd.close()
def reset(self): def reset(self):
self.sp.setDTR(0) self.setPin(46, 1)
time.sleep(0.1) time.sleep(0.1)
self.sp.setDTR(1) self.setPin(46, 0)
time.sleep(0.5) time.sleep(0.5)
def initChip(self): def initChip(self):
# Set boot # Set boot
self.sp.setRTS(0) self.setPin(45, 1)
self.reset() self.reset()
self.sp.flushInput()
self.sp.flushOutput()
self.sp.write("\x7F") # Syncro self.sp.write("\x7F") # Syncro
return self._wait_for_ask("Syncro") return self._wait_for_ask("Syncro")
def releaseChip(self): def releaseChip(self):
self.sp.setRTS(1) self.setPin(45,0)
self.reset() self.reset()
def cmdGeneric(self, cmd): def cmdGeneric(self, cmd):
@ -217,10 +235,14 @@ class CommandInterface:
self.sp.write(chr(0x00)) self.sp.write(chr(0x00))
else: else:
# Sectors erase # Sectors erase
self.sp.write(chr((len(sectors)-1) & 0xFF)) crc = 0x0
crc = 0xFF
sec_len = (len(sectors)-1) & 0xff
crc ^= sec_len
self.sp.write(chr(sec_len))
for c in sectors: for c in sectors:
crc = crc ^ c crc ^= c
self.sp.write(chr(c)) self.sp.write(chr(c))
self.sp.write(chr(crc)) self.sp.write(chr(crc))
self._wait_for_ask("0x43 erasing failed") self._wait_for_ask("0x43 erasing failed")
@ -280,9 +302,16 @@ class CommandInterface:
def cmdReadoutUnprotect(self): def cmdReadoutUnprotect(self):
if self.cmdGeneric(0x92): if self.cmdGeneric(0x92):
mdebug(10, "*** Readout Unprotect command") mdebug(10, "*** Readout Unprotect command")
self._wait_for_ask("0x92 readout unprotect failed") # first (N)ACK is already read as part of cmdGeneric()
self._wait_for_ask("0x92 readout unprotect 2 failed") self._wait_for_ask("0x92 readout unprotect 2 failed")
mdebug(10, " Read Unprotect done") mdebug(10, " Read Unprotect done")
# there does not appear to be a way to be notified when the
# mass erase is complete... boo. hopefully this will be rare, i guess.
time.sleep(20)
# following readout unprotect, the system resets, so we must init once again.
self.initChip()
else: else:
raise CmdException("Readout unprotect (0x92) failed") raise CmdException("Readout unprotect (0x92) failed")
@ -385,7 +414,7 @@ if __name__ == "__main__":
# http://www.python.org/doc/2.5.2/lib/module-getopt.html # http://www.python.org/doc/2.5.2/lib/module-getopt.html
try: try:
opts, args = getopt.getopt(sys.argv[1:], "hqVewvrp:b:a:l:g:") opts, args = getopt.getopt(sys.argv[1:], "hqVewvrs:p:b:a:l:g:")
except getopt.GetoptError, err: except getopt.GetoptError, err:
# print help information and exit: # print help information and exit:
print str(err) # will print something like "option -a not recognized" print str(err) # will print something like "option -a not recognized"
@ -404,6 +433,8 @@ if __name__ == "__main__":
sys.exit(0) sys.exit(0)
elif o == '-e': elif o == '-e':
conf['erase'] = 1 conf['erase'] = 1
elif o == '-s':
conf['erase-sectors'] = a
elif o == '-w': elif o == '-w':
conf['write'] = 1 conf['write'] = 1
elif o == '-v': elif o == '-v':
@ -446,8 +477,21 @@ if __name__ == "__main__":
if (conf['write'] or conf['verify']): if (conf['write'] or conf['verify']):
data = map(lambda c: ord(c), file(args[0], 'rb').read()) data = map(lambda c: ord(c), file(args[0], 'rb').read())
try:
if conf['erase']: if conf['erase']:
cmd.cmdEraseMemory() cmd.cmdEraseMemory()
elif 'erase-sectors' in conf:
# cmdEraseMemory() accepts a list of sectors,
# we treat the arg as a sector count, at offset 0
sectors = range(int(conf['erase-sectors']))
cmd.cmdEraseMemory(sectors)
except CmdException:
# assumption: erase failed due to readout protection.
# this was observed once as a process problem in production,
# there may be other reasons for this failure in the future.
mdebug(0, "EraseMemory failed, disabling readout protection")
cmd.cmdReadoutUnprotect()
if conf['write']: if conf['write']:
cmd.writeMemory(conf['address'], data) cmd.writeMemory(conf['address'], data)
@ -459,7 +503,10 @@ if __name__ == "__main__":
else: else:
print "Verification FAILED" print "Verification FAILED"
print str(len(data)) + ' vs ' + str(len(verify)) print str(len(data)) + ' vs ' + str(len(verify))
for i in xrange(0, len(data)): imax = len(data)
if imax > 100:
imax = 100
for i in xrange(0, imax):
if data[i] != verify[i]: if data[i] != verify[i]:
print hex(i) + ': ' + hex(data[i]) + ' vs ' + hex(verify[i]) print hex(i) + ': ' + hex(data[i]) + ' vs ' + hex(verify[i])