@@ -173,7 +173,7 @@ def __init__(self, status, timeout, state, status_message=None,
173
173
self .__state = state
174
174
self .__status_message = status_message
175
175
self .__extra = extra
176
- super (self , DFUError ).__init__ ()
176
+ super (DFUStatusError , self ).__init__ ()
177
177
178
178
def getStatus (self ):
179
179
return self .__status
@@ -257,7 +257,7 @@ def __init__(self, handle):
257
257
"""
258
258
self .__handle = handle
259
259
self .__attributes , self .__detach_timeout , self .__transfer_size , \
260
- dfu_version , self .__protocol , iface , self . __descriptor_list = \
260
+ dfu_version , self .__protocol , iface , descriptor_list = \
261
261
getDFUDescriptor (handle .getDevice ())
262
262
self .__interface = iface
263
263
self .__dfu_version = dfu_version
@@ -508,7 +508,7 @@ def checkStatus(self):
508
508
try :
509
509
status_message = self .__handle .getASCIIStringDescriptor (
510
510
status_descriptor )
511
- except :
511
+ except Exception :
512
512
extra = traceback .format_exc ()
513
513
status_message = None
514
514
else :
@@ -561,7 +561,7 @@ def _completeFieldLists():
561
561
for field_list in (DFU_SUFFIX_FIELD_LIST , DFU_STM_PREFIX_FIELD_LIST ,
562
562
DFU_STM_TARGET_PREFIX_FIELD_LIST ,
563
563
DFU_STM_ELEMENT_PREFIX_FIELD_LIST ):
564
- for field in DFU_SUFFIX_FIELD_LIST :
564
+ for field in field_list :
565
565
field_name = field [0 ]
566
566
if field_name in dfu_suffix_set :
567
567
raise ValueError ('Dulpicate field name: ' + field_name )
@@ -576,9 +576,8 @@ def _completeFieldLists():
576
576
x [2 ] for x in DFU_STM_ELEMENT_PREFIX_FIELD_LIST )
577
577
def _parseFieldList (data , field_list ):
578
578
result = {}
579
- read = stream .read
580
- for name , fmt , length in DFU_STM_PREFIX_FIELD_LIST :
581
- suffix_dict [name ] = unpack (fmt , data [:length ])[0 ]
579
+ for name , fmt , length in field_list :
580
+ result [name ] = unpack (fmt , data [:length ])[0 ]
582
581
data = data [length :]
583
582
if not data :
584
583
break
@@ -641,21 +640,26 @@ def download(self, data):
641
640
offset = DFU_STM_PREFIX_LENGTH
642
641
for _ in xrange (stm_head ['target_count' ]):
643
642
target_head = _parseFieldList (
644
- data [offset :offset + DFU_STM_TARGET_PREFIX_LENGTH ])
645
- if stm_target_head ['magic' ] != 'Target' :
646
- raise DFUFormatError ('Invalid DfuSe target magic ' \
643
+ data [offset :offset + DFU_STM_TARGET_PREFIX_LENGTH ],
644
+ DFU_STM_TARGET_PREFIX_FIELD_LIST )
645
+ if target_head ['magic' ] != 'Target' :
646
+ raise DFUFormatError ('Invalid DfuSe target magic '
647
647
'at 0x%x' % (offset , ))
648
648
offset += DFU_STM_TARGET_PREFIX_LENGTH
649
649
for _ in xrange (target_head ['element_count' ]):
650
- element_head = _parseFieldList (data [offset :offset + \
651
- DFU_STM_ELEMENT_PREFIX_LENGTH ])
650
+ element_head = _parseFieldList (data [offset :offset +
651
+ DFU_STM_ELEMENT_PREFIX_LENGTH ],
652
+ DFU_STM_ELEMENT_PREFIX_FIELD_LIST )
653
+ offset += DFU_STM_ELEMENT_PREFIX_LENGTH
652
654
iface .STM_setAddress (element_head ['address' ])
653
655
#iface.STM_erasePage(element_head['address'])
654
- iface .
656
+ self ._download (data [offset :offset +
657
+ element_head ['size' ]])
658
+ offset += element_head ['size' ]
655
659
else :
656
660
self ._download (data [:- suffix_length ])
657
661
# Notify of EOF with an empty transfer
658
- download ('' , blocknum )
662
+ self . _download ('' )
659
663
if not iface .isManifestationTolerant ():
660
664
self ._reset ('Download complete.' )
661
665
@@ -735,23 +739,3 @@ def upload():
735
739
traceback .format_exc ())
736
740
raise exc_info [0 ], exc_info [1 ], exc_info [2 ]
737
741
738
- if __name__ == '__main__' :
739
- import usb1
740
- context = usb1 .LibUSBContext ()
741
- handler = context .openByVendorIDAndProductID (0x0483 , 0xdf11 )
742
- dfu = DFU (handler )
743
- import pdb ; pdb .set_trace ()
744
-
745
- class LoggingStream (object ):
746
- def __init__ (self , stream ):
747
- self .__stream = stream
748
- self .__total = 0
749
-
750
- def write (self , data ):
751
- self .__stream .write (data )
752
- data_len = len (data )
753
- self .__total += data_len
754
- print 'Received:' , data_len , self .__total
755
-
756
- dfu .uploadStream (LoggingStream (open ('firmware.raw' , 'w' )))
757
-
0 commit comments