annotate python/st-flash.py @ 126:bbf4c9b138d4

Changes to elf reading
author Windel Bouwman
date Sun, 13 Jan 2013 12:14:27 +0100
parents 90b03bc018cf
children 51cc127648e4
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
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
4 import stlink
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)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
10 return int(s)
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()
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
33 print(args)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
34 if not args.command:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
35 parser.print_usage()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
36 sys.exit(1)
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
37
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
38 # In any command case, open a device:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
39 stl = stlink.STLink()
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
40 stl.open()
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
41
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
42 # Enter the right mode:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
43 if stl.CurrentMode == stlink.DFU_MODE:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
44 stl.exitDfuMode()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
45
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
46 if stl.CurrentMode != stlink.DEBUG_MODE:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
47 stl.enterSwdMode()
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
48
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
49 if stl.ChipId != 0x10016413:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
50 print('Only working on stm32f4discovery board for now.')
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
51 sys.exit(2)
113
1f40be088ee8 Added first start stlink
Windel Bouwman
parents:
diff changeset
52
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
53 if args.command == 'read':
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
54 dev_content = stl.readFlash(args.address, args.size)
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
55 args.filename.write(dev_content)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
56 elif args.command == 'write':
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
57 content = args.filename.read()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
58 stl.writeFlash(args.address, content)
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
59 elif args.command == 'verify':
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
60 content = args.filename.read()
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
61 stl.verifyFlash(args.address, content)
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
62 elif args.command == 'erase':
116
90b03bc018cf Added loader code from openocd
Windel Bouwman
parents: 115
diff changeset
63 stl.eraseFlash()
115
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
64 else:
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
65 print('unknown command', args.command)
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
66
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
67 stl.exitDebugMode()
92b2bf0da1ec Added erase and verify
Windel Bouwman
parents: 113
diff changeset
68