3
3
import usb1
4
4
import struct
5
5
import time
6
+ import binascii
6
7
7
8
# *** DFU mode ***
8
9
@@ -46,41 +47,40 @@ def list():
46
47
def st_serial_to_dfu_serial (st ):
47
48
if st == None or st == "none" :
48
49
return None
49
- uid_base = struct .unpack ("H" * 6 , st .decode ("hex" ))
50
- return struct .pack ("!HHH" , uid_base [1 ] + uid_base [5 ], uid_base [0 ] + uid_base [4 ] + 0xA , uid_base [3 ]).encode ("hex" ).upper ()
51
-
50
+ uid_base = struct .unpack ("H" * 6 , bytes .fromhex (st ))
51
+ return binascii .hexlify (struct .pack ("!HHH" , uid_base [1 ] + uid_base [5 ], uid_base [0 ] + uid_base [4 ] + 0xA , uid_base [3 ])).upper ().decode ("utf-8" )
52
52
53
53
def status (self ):
54
54
while 1 :
55
- dat = str ( self ._handle .controlRead (0x21 , DFU_GETSTATUS , 0 , 0 , 6 ) )
56
- if dat [1 ] == " \x00 " :
55
+ dat = self ._handle .controlRead (0x21 , DFU_GETSTATUS , 0 , 0 , 6 )
56
+ if dat [1 ] == 0 :
57
57
break
58
58
59
59
def clear_status (self ):
60
60
# Clear status
61
- stat = str ( self ._handle .controlRead (0x21 , DFU_GETSTATUS , 0 , 0 , 6 ) )
62
- if stat [4 ] == " \x0a " :
61
+ stat = self ._handle .controlRead (0x21 , DFU_GETSTATUS , 0 , 0 , 6 )
62
+ if stat [4 ] == 0xa :
63
63
self ._handle .controlRead (0x21 , DFU_CLRSTATUS , 0 , 0 , 0 )
64
- elif stat [4 ] == " \x09 " :
65
- self ._handle .controlWrite (0x21 , DFU_ABORT , 0 , 0 , "" )
64
+ elif stat [4 ] == 0x9 :
65
+ self ._handle .controlWrite (0x21 , DFU_ABORT , 0 , 0 , b "" )
66
66
self .status ()
67
67
stat = str (self ._handle .controlRead (0x21 , DFU_GETSTATUS , 0 , 0 , 6 ))
68
68
69
69
def erase (self , address ):
70
- self ._handle .controlWrite (0x21 , DFU_DNLOAD , 0 , 0 , "\x41 " + struct .pack ("I" , address ))
70
+ self ._handle .controlWrite (0x21 , DFU_DNLOAD , 0 , 0 , b "\x41 " + struct .pack ("I" , address ))
71
71
self .status ()
72
72
73
73
def program (self , address , dat , block_size = None ):
74
74
if block_size == None :
75
75
block_size = len (dat )
76
76
77
77
# Set Address Pointer
78
- self ._handle .controlWrite (0x21 , DFU_DNLOAD , 0 , 0 , "\x21 " + struct .pack ("I" , address ))
78
+ self ._handle .controlWrite (0x21 , DFU_DNLOAD , 0 , 0 , b "\x21 " + struct .pack ("I" , address ))
79
79
self .status ()
80
80
81
81
# Program
82
- dat += "\xFF " * ((block_size - len (dat )) % block_size )
83
- for i in range (0 , len (dat )/ block_size ):
82
+ dat += b "\xFF "* ((block_size - len (dat )) % block_size )
83
+ for i in range (0 , len (dat )// block_size ):
84
84
ldat = dat [i * block_size :(i + 1 )* block_size ]
85
85
print ("programming %d with length %d" % (i , len (ldat )))
86
86
self ._handle .controlWrite (0x21 , DFU_DNLOAD , 2 + i , 0 , ldat )
@@ -105,17 +105,17 @@ def recover(self):
105
105
build_st (fn )
106
106
fn = os .path .join (BASEDIR , "board" , fn )
107
107
108
- with open (fn ) as f :
108
+ with open (fn , "rb" ) as f :
109
109
code = f .read ()
110
110
111
111
self .program_bootstub (code )
112
112
113
113
def reset (self ):
114
114
# **** Reset ****
115
- self ._handle .controlWrite (0x21 , DFU_DNLOAD , 0 , 0 , "\x21 " + struct .pack ("I" , 0x8000000 ))
115
+ self ._handle .controlWrite (0x21 , DFU_DNLOAD , 0 , 0 , b "\x21 " + struct .pack ("I" , 0x8000000 ))
116
116
self .status ()
117
117
try :
118
- self ._handle .controlWrite (0x21 , DFU_DNLOAD , 2 , 0 , "" )
118
+ self ._handle .controlWrite (0x21 , DFU_DNLOAD , 2 , 0 , b "" )
119
119
stat = str (self ._handle .controlRead (0x21 , DFU_GETSTATUS , 0 , 0 , 6 ))
120
120
except Exception :
121
121
pass
0 commit comments