view 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
line wrap: on
line source

#!/usr/bin/python

import argparse, sys
import stlink 

def hex2int(s):
   if s.startswith('0x'):
      s = s[2:]
      return int(s, 16)
   return int(s)

parser = argparse.ArgumentParser(
   description='ST-link flash utility by Windel Bouwman')
subparsers = parser.add_subparsers(title='commands', 
         description='possible commands', dest='command')

readparser = subparsers.add_parser('read', help='read flash contents')
readparser.add_argument('filename', type=argparse.FileType('wb', 0))
readparser.add_argument('address', type=hex2int)
readparser.add_argument('size', type=hex2int)

writeparser = subparsers.add_parser('write', help='write flash contents')
writeparser.add_argument('filename', type=argparse.FileType('rb'))
writeparser.add_argument('address', type=hex2int)

verifyparser = subparsers.add_parser('verify', help='verify flash contents')
verifyparser.add_argument('filename', type=argparse.FileType('rb'))
verifyparser.add_argument('address', type=hex2int)

eraseparser = subparsers.add_parser('erase', help='erase flash contents')

args = parser.parse_args()
print(args)
if not args.command:
   parser.print_usage()
   sys.exit(1)

# In any command case, open a device:
stl = stlink.STLink()
stl.open()

# Enter the right mode:
if stl.CurrentMode == stlink.DFU_MODE:
   stl.exitDfuMode()

if stl.CurrentMode != stlink.DEBUG_MODE:
   stl.enterSwdMode()

if stl.ChipId != 0x10016413:
   print('Only working on stm32f4discovery board for now.')
   sys.exit(2)

if args.command == 'read':
   dev_content = stl.readFlash(args.address, args.size)
   args.filename.write(dev_content)
elif args.command == 'write':
   content = args.filename.read()
   stl.writeFlash(args.address, content)
elif args.command == 'verify':
   content = args.filename.read()
   stl.verifyFlash(args.address, content)
elif args.command == 'erase':
   stl.eraseFlash()
else:
   print('unknown command', args.command)
   
stl.exitDebugMode()