comparison python/st-flash.py @ 115:92b2bf0da1ec

Added erase and verify
author Windel Bouwman
date Sun, 06 Jan 2013 18:13:17 +0100
parents 1f40be088ee8
children 90b03bc018cf
comparison
equal deleted inserted replaced
114:f42268da614f 115:92b2bf0da1ec
1 #!/usr/bin/python 1 #!/usr/bin/python
2 2
3 import argparse 3 import argparse, sys
4 from stlink import STLink 4 import stlink
5 5
6 def hex2int(s): 6 def hex2int(s):
7 return int(s, 16) 7 if s.startswith('0x'):
8 s = s[2:]
9 return int(s, 16)
10 return int(s)
8 11
9 parser = argparse.ArgumentParser(description='ST-link flash utility') 12 parser = argparse.ArgumentParser(description='ST-link flash utility')
10 #parser.add_argument('filename', type=str) 13 subparsers = parser.add_subparsers(title='commands',
11 #parser.add_argument('address', type=hex2int) 14 description='possible commands', dest='command')
15
16 readparser = subparsers.add_parser('read', help='read flash contents')
17 readparser.add_argument('filename', type=argparse.FileType(mode='wb'))
18 readparser.add_argument('address', type=hex2int)
19 readparser.add_argument('size', type=hex2int)
20
21 writeparser = subparsers.add_parser('write', help='write flash contents')
22 writeparser.add_argument('filename', type=argparse.FileType(mode='rb'))
23 writeparser.add_argument('address', type=hex2int)
24
25 eraseparser = subparsers.add_parser('erase', help='erase flash contents')
12 26
13 args = parser.parse_args() 27 args = parser.parse_args()
14 print(args) 28 print(args)
29 if not args.command:
30 parser.print_usage()
31 sys.exit(1)
15 32
16 stl = STLink() 33 # In any command case, open a device:
17 print(stl) 34 stl = stlink.STLink()
18 stl.open() 35 try:
19 if stl.CurrentMode == STLink.DFU_MODE: 36 stl.open()
20 pass 37 except stlink.STLinkException as e:
38 print(e)
39 sys.exit(2)
21 40
41 # Enter the right mode:
42 if stl.CurrentMode == stlink.DFU_MODE:
43 stl.exitDfuMode()
22 44
45 if stl.CurrentMode != stlink.DEBUG_MODE:
46 stl.enterSwdMode()
47
48 if stl.ChipId != 0x10016413:
49 print('Only working on stm32f4discovery board for now.')
50 sys.exit(2)
51
52 if args.command == 'read':
53 print('Reading!')
54 elif args.command == 'write':
55 content = args.filename.read()
56 print('Write flash :)', len(content))
57 stl.writeFlash(args.address, content)
58 elif args.command == 'erase':
59 print('erase')
60 else:
61 print('unknown command', args.command)
62
63 stl.exitDebugMode()
64