113
|
1 #!/usr/bin/python
|
|
2
|
115
|
3 import argparse, sys
|
128
|
4 import stlink, stm32
|
246
|
5 import hexfile
|
113
|
6
|
|
7 def hex2int(s):
|
115
|
8 if s.startswith('0x'):
|
|
9 s = s[2:]
|
|
10 return int(s, 16)
|
128
|
11 raise ValueError('Hexadecimal value must begin with 0x')
|
113
|
12
|
116
|
13 parser = argparse.ArgumentParser(
|
|
14 description='ST-link flash utility by Windel Bouwman')
|
115
|
15 subparsers = parser.add_subparsers(title='commands',
|
|
16 description='possible commands', dest='command')
|
|
17
|
|
18 readparser = subparsers.add_parser('read', help='read flash contents')
|
116
|
19 readparser.add_argument('filename', type=argparse.FileType('wb', 0))
|
115
|
20 readparser.add_argument('address', type=hex2int)
|
|
21 readparser.add_argument('size', type=hex2int)
|
|
22
|
|
23 writeparser = subparsers.add_parser('write', help='write flash contents')
|
116
|
24 writeparser.add_argument('filename', type=argparse.FileType('rb'))
|
115
|
25 writeparser.add_argument('address', type=hex2int)
|
|
26
|
244
|
27 hexwriteparser = subparsers.add_parser('hexwrite', help='write hexfile to flash')
|
246
|
28 hexwriteparser.add_argument('hexfile', type=argparse.FileType('r'))
|
244
|
29
|
116
|
30 verifyparser = subparsers.add_parser('verify', help='verify flash contents')
|
|
31 verifyparser.add_argument('filename', type=argparse.FileType('rb'))
|
|
32 verifyparser.add_argument('address', type=hex2int)
|
|
33
|
115
|
34 eraseparser = subparsers.add_parser('erase', help='erase flash contents')
|
113
|
35
|
|
36 args = parser.parse_args()
|
115
|
37 if not args.command:
|
|
38 parser.print_usage()
|
|
39 sys.exit(1)
|
113
|
40
|
115
|
41 # In any command case, open a device:
|
128
|
42 stl = stlink.STLink2()
|
116
|
43 stl.open()
|
115
|
44
|
|
45 # Enter the right mode:
|
|
46 if stl.CurrentMode == stlink.DFU_MODE:
|
|
47 stl.exitDfuMode()
|
|
48
|
|
49 if stl.CurrentMode != stlink.DEBUG_MODE:
|
|
50 stl.enterSwdMode()
|
113
|
51
|
115
|
52 if stl.ChipId != 0x10016413:
|
|
53 print('Only working on stm32f4discovery board for now.')
|
|
54 sys.exit(2)
|
113
|
55
|
128
|
56 # Retrieve the connected device, if any:
|
129
|
57 dev = stl.createDevice()
|
128
|
58
|
115
|
59 if args.command == 'read':
|
128
|
60 dev_content = dev.readFlash(args.address, args.size)
|
116
|
61 args.filename.write(dev_content)
|
115
|
62 elif args.command == 'write':
|
|
63 content = args.filename.read()
|
128
|
64 dev.writeFlash(args.address, content)
|
246
|
65 elif args.command == 'hexwrite':
|
|
66 hf = hexfile.HexFile()
|
|
67 hf.load(args.hexfile)
|
|
68 r = hf.regions[0]
|
|
69 dev.writeFlash(r.address, r.data)
|
116
|
70 elif args.command == 'verify':
|
|
71 content = args.filename.read()
|
128
|
72 dev.verifyFlash(args.address, content)
|
115
|
73 elif args.command == 'erase':
|
128
|
74 dev.eraseFlash()
|
115
|
75 else:
|
|
76 print('unknown command', args.command)
|
241
|
77
|
|
78 stl.reset()
|
|
79 stl.run()
|
115
|
80 stl.exitDebugMode()
|
|
81
|