comparison python/st-flash.py @ 116:90b03bc018cf

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