annotate python/st-flash.py @ 271:cf7d5fb7d9c8

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