Mercurial > lcfOS
comparison python/utils/st-flash.py @ 292:534b94b40aa8
Fixup reorganize
author | Windel Bouwman |
---|---|
date | Wed, 27 Nov 2013 08:06:42 +0100 |
parents | python/st-flash.py@f254b87258e6 |
children | a7c444404df9 |
comparison
equal
deleted
inserted
replaced
290:7b38782ed496 | 292:534b94b40aa8 |
---|---|
1 #!/usr/bin/python | |
2 | |
3 import argparse, sys | |
4 import stlink, stm32 | |
5 import hexfile | |
6 | |
7 def hex2int(s): | |
8 if s.startswith('0x'): | |
9 s = s[2:] | |
10 return int(s, 16) | |
11 raise ValueError('Hexadecimal value must begin with 0x') | |
12 | |
13 parser = argparse.ArgumentParser( | |
14 description='ST-link flash utility by Windel Bouwman') | |
15 subparsers = parser.add_subparsers(title='commands', | |
16 description='possible commands', dest='command') | |
17 | |
18 readparser = subparsers.add_parser('read', help='read flash contents') | |
19 readparser.add_argument('filename', type=argparse.FileType('wb', 0)) | |
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') | |
24 writeparser.add_argument('filename', type=argparse.FileType('rb')) | |
25 writeparser.add_argument('address', type=hex2int) | |
26 | |
27 hexwriteparser = subparsers.add_parser('hexwrite', help='write hexfile to flash') | |
28 hexwriteparser.add_argument('hexfile', type=argparse.FileType('r')) | |
29 | |
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 | |
34 eraseparser = subparsers.add_parser('erase', help='erase flash contents') | |
35 | |
36 args = parser.parse_args() | |
37 if not args.command: | |
38 parser.print_usage() | |
39 sys.exit(1) | |
40 | |
41 # In any command case, open a device: | |
42 stl = stlink.STLink2() | |
43 stl.open() | |
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() | |
51 | |
52 if stl.ChipId != 0x10016413: | |
53 print('Only working on stm32f4discovery board for now.') | |
54 sys.exit(2) | |
55 | |
56 # Retrieve the connected device, if any: | |
57 dev = stl.createDevice() | |
58 | |
59 if args.command == 'read': | |
60 dev_content = dev.readFlash(args.address, args.size) | |
61 args.filename.write(dev_content) | |
62 elif args.command == 'write': | |
63 content = args.filename.read() | |
64 dev.writeFlash(args.address, content) | |
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) | |
70 elif args.command == 'verify': | |
71 content = args.filename.read() | |
72 dev.verifyFlash(args.address, content) | |
73 elif args.command == 'erase': | |
74 dev.eraseFlash() | |
75 else: | |
76 print('unknown command', args.command) | |
77 | |
78 stl.reset() | |
79 stl.run() | |
80 stl.exitDebugMode() | |
81 |