annotate python/st-flash.py @ 244:58155c7c4a8e

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