annotate python/st-flash.py @ 149:74241ca312cc

Fixes on parser and semantics
author Windel Bouwman
date Fri, 01 Mar 2013 11:43:52 +0100
parents 654093a9a1e3
children ce6d390043a7
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
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
26 verifyparser = subparsers.add_parser('verify', help='verify flash contents')
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
27 verifyparser.add_argument('filename', type=argparse.FileType('rb'))
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
28 verifyparser.add_argument('address', type=hex2int)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
29
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
30 eraseparser = subparsers.add_parser('erase', help='erase flash contents')
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
31
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
32 args = parser.parse_args()
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
33 if not args.command:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
34 parser.print_usage()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
35 sys.exit(1)
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
36
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
37 # In any command case, open a device:
128
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents: 116
diff changeset
38 stl = stlink.STLink2()
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
39 stl.open()
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
40
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
41 # Enter the right mode:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
42 if stl.CurrentMode == stlink.DFU_MODE:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
43 stl.exitDfuMode()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
44
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
45 if stl.CurrentMode != stlink.DEBUG_MODE:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
46 stl.enterSwdMode()
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
47
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
48 if stl.ChipId != 0x10016413:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
49 print('Only working on stm32f4discovery board for now.')
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
50 sys.exit(2)
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
51
128
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents: 116
diff changeset
52 # Retrieve the connected device, if any:
129
9e350a7dde98 Changed architecture and updated the util
Windel Bouwman
parents: 128
diff changeset
53 dev = stl.createDevice()
128
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents: 116
diff changeset
54
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
55 if args.command == 'read':
128
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents: 116
diff changeset
56 dev_content = dev.readFlash(args.address, args.size)
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
57 args.filename.write(dev_content)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
58 elif args.command == 'write':
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
59 content = args.filename.read()
128
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents: 116
diff changeset
60 dev.writeFlash(args.address, content)
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
61 elif args.command == 'verify':
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
62 content = args.filename.read()
128
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents: 116
diff changeset
63 dev.verifyFlash(args.address, content)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
64 elif args.command == 'erase':
128
51cc127648e4 Splitup in interface and device
Windel Bouwman
parents: 116
diff changeset
65 dev.eraseFlash()
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
66 else:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
67 print('unknown command', args.command)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
68
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
69 stl.exitDebugMode()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
70