-# Erases an image volume
-def op_erase(s, img_num):
- sreqpkt = SerialReqPacket((MSG_ERASE, img_num, 0, 0, []))
- success = s.write_packet(SERIAL_AMGROUP, SERIAL_AMID, sreqpkt.payload())
- if success == True:
- packet = s.read_packet(SERIAL_AMGROUP, SERIAL_AMID)
- sreplypkt = SerialReplyPacket(packet[1])
- if sreplypkt.error == ERROR_SUCCESS:
- return True
- else:
- print "ERROR: Unable to erase the flash volume"
- return False
-
- print "ERROR: Unable to send the command"
- return False
+def erase(imgNum):
+ # Note: the normal erase doesn't work properly on AT45DB. A
+ # workaround is to do the normal erase (to make happy STM25P)
+ # and then overwrite the metadata (to make happy AT45DB).
+
+ sreqpkt = FMReqPacket((FM_CMD_ERASE, imgNum, 0, 0, []))
+ success = am.write(sreqpkt, FM_AMID)
+ result = handleResponse(success, "ERROR: Unable to erase the flash volume")
+ if result: return True;
+
+ print 'Attempt the workaround for AT45DB...'
+ sreqpkt = FMReqPacket((FM_CMD_WRITE, imgNum, 0, 0, []))
+ sreqpkt.data = [0xFF] * DELUGE_IDENT_SIZE
+ sreqpkt.length = DELUGE_IDENT_SIZE
+ success = am.write(sreqpkt, FM_AMID)
+ result = handleResponse(success, "ERROR: Unable to erase the flash volume")
+ if not result: return False;
+ return sync(imgNum)
+
+def sync(imgNum):
+ sreqpkt = FMReqPacket((FM_CMD_SYNC, imgNum, 0, 0, []))
+ success = am.write(sreqpkt, FM_AMID)
+ return handleResponse(success, "ERROR: Unable to sync the flash volume")
+
+def write(imgNum, data):
+ sreqpkt = FMReqPacket((FM_CMD_WRITE, imgNum, 0, 0, []))
+ length = len(data)
+ total_length = length # For progress bar
+ next_tick = 100 # For progress bar
+ start_time = time.time()
+
+ print "[0% 25% 50% 75% 100%]\r[",