12
12
UF2_MAGIC_START1 = 0x9E5D5157 # Randomly selected
13
13
UF2_MAGIC_END = 0x0AB16F30 # Ditto
14
14
15
+ families = {
16
+ 'SAMD21' : 0x68ed2b88 ,
17
+ 'SAMD51' : 0x55114460 ,
18
+ 'NRF52' : 0x1b57745f ,
19
+ 'STM32F1' : 0x5ee21072 ,
20
+ 'STM32F4' : 0x57755a57 ,
21
+ 'ATMEGA32' : 0x16573617 ,
22
+ }
23
+
15
24
INFO_FILE = "/INFO_UF2.TXT"
16
25
17
26
appstartaddr = 0x2000
27
+ familyid = 0x0
18
28
19
29
def isUF2 (buf ):
20
30
w = struct .unpack ("<II" , buf [0 :8 ])
@@ -63,6 +73,7 @@ def convertFromUF2(buf):
63
73
return outp
64
74
65
75
def convertToUF2 (fileContent ):
76
+ global familyid
66
77
datapadding = ""
67
78
while len (datapadding ) < 512 - 256 - 32 - 4 :
68
79
datapadding += "\x00 \x00 \x00 \x00 "
@@ -71,9 +82,12 @@ def convertToUF2(fileContent):
71
82
for blockno in range (0 , numblocks ):
72
83
ptr = 256 * blockno
73
84
chunk = fileContent [ptr :ptr + 256 ]
85
+ flags = 0x0
86
+ if familyid :
87
+ flags |= 0x2000
74
88
hd = struct .pack ("<IIIIIIII" ,
75
89
UF2_MAGIC_START0 , UF2_MAGIC_START1 ,
76
- 0 , ptr + appstartaddr , 256 , blockno , numblocks , 0 )
90
+ flags , ptr + appstartaddr , 256 , blockno , numblocks , familyid )
77
91
while len (chunk ) < 256 :
78
92
chunk += "\x00 "
79
93
block = hd + chunk + datapadding + struct .pack ("<I" , UF2_MAGIC_END )
@@ -89,9 +103,13 @@ def __init__(self, addr):
89
103
self .bytes .append (0 )
90
104
91
105
def encode (self , blockno , numblocks ):
106
+ global familyid
107
+ flags = 0x0
108
+ if familyid :
109
+ flags |= 0x2000
92
110
hd = struct .pack ("<IIIIIIII" ,
93
111
UF2_MAGIC_START0 , UF2_MAGIC_START1 ,
94
- 0 , self .addr , 256 , blockno , numblocks , 0 )
112
+ flags , self .addr , 256 , blockno , numblocks , familyid )
95
113
for i in range (0 , 256 ):
96
114
hd += chr (self .bytes [i ])
97
115
while len (hd ) < 512 - 4 :
@@ -182,7 +200,7 @@ def writeFile(name, buf):
182
200
print "Wrote %d bytes to %s." % (len (buf ), name )
183
201
184
202
def main ():
185
- global appstartaddr
203
+ global appstartaddr , familyid
186
204
def error (msg ):
187
205
print msg
188
206
sys .exit (1 )
@@ -200,8 +218,20 @@ def error(msg):
200
218
help = 'list connected devices' )
201
219
parser .add_argument ('-c' , '--convert' , action = 'store_true' ,
202
220
help = 'do not flash, just convert' )
221
+ parser .add_argument ('-f' , '--family' , dest = 'family' , type = str ,
222
+ default = "0x0" ,
223
+ help = 'specify familyID - number or name (default: 0x0)' )
203
224
args = parser .parse_args ()
204
225
appstartaddr = int (args .base , 0 )
226
+
227
+ if args .family .upper () in families :
228
+ familyid = families [args .family .upper ()]
229
+ else :
230
+ try :
231
+ familyid = int (args .family , 0 )
232
+ except ValueError :
233
+ error ("Family ID needs to be a number or one of: " + ", " .join (families .keys ()))
234
+
205
235
if args .list :
206
236
listdrives ()
207
237
else :
0 commit comments