view 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
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')
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(mode='wb'))
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(mode='rb'))
writeparser.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()
try:
   stl.open()
except stlink.STLinkException as e:
   print(e)
   sys.exit(2)

# 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':
   print('Reading!')
elif args.command == 'write':
   content = args.filename.read()
   print('Write flash :)', len(content))
   stl.writeFlash(args.address, content)
elif args.command == 'erase':
   print('erase')
else:
   print('unknown command', args.command)
   
stl.exitDebugMode()