changeset 246:f254b87258e6

Added hexfile to zcc
author Windel Bouwman
date Thu, 25 Jul 2013 08:11:30 +0200
parents 66912720d712
children dd8bbb963458
files python/hexfile.py python/hexutil.py python/st-flash.py python/stm32.py python/zcc.py
diffstat 5 files changed, 43 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/python/hexfile.py	Wed Jul 24 22:40:29 2013 +0200
+++ b/python/hexfile.py	Thu Jul 25 08:11:30 2013 +0200
@@ -80,7 +80,8 @@
                 raise HexFileException('record type {0} not implemented'.format(typ))
 
     def __repr__(self):
-        return 'Hexfile with {} regions'.format(len(self.regions))
+        size = sum(len(r.data) for r in self.regions)
+        return 'Hexfile containing {} bytes'.format(size)
 
     def dump(self):
         print(self)
@@ -114,6 +115,9 @@
                     self.regions.remove(r2)
                     change = True
 
+    def merge(self, other):
+        for r in other.regions:
+            self.addRegion(r.address, r.data)
 
     def save(self, f):
         def emit(address, typ, data=bytes()):
--- a/python/hexutil.py	Wed Jul 24 22:40:29 2013 +0200
+++ b/python/hexutil.py	Thu Jul 25 08:11:30 2013 +0200
@@ -18,13 +18,15 @@
 p = subparsers.add_parser('info', help='dump info about hexfile')
 p.add_argument('hexfile', type=argparse.FileType('r'))
 
-p = subparsers.add_parser('new', help='create empty hexfile')
-p.add_argument('hexfile', type=argparse.FileType('x'))
+p = subparsers.add_parser('new', help='create a hexfile')
+p.add_argument('hexfile', type=argparse.FileType('w'))
+p.add_argument('address', type=hex2int, help="hex address of the data")
+p.add_argument('datafile', type=argparse.FileType('rb'), help='binary file to add')
 
-p = subparsers.add_parser('add', help='add binary data from file to hexfile')
-p.add_argument('hexfile', type=argparse.FileType('r+'), help="the hexfile to add the data to")
-p.add_argument('address', type=hex2int, help="hex address")
-p.add_argument('datafile', type=argparse.FileType('rb'), help='binary file to add')
+p = subparsers.add_parser('merge', help='merge two hexfiles into a third')
+p.add_argument('hexfile1', type=argparse.FileType('r'), help="hexfile 1")
+p.add_argument('hexfile2', type=argparse.FileType('r'), help="hexfile 2")
+p.add_argument('rhexfile', type=argparse.FileType('w'), help="resulting hexfile")
 
 def main(args):
     if args.command == 'info':
@@ -35,12 +37,16 @@
             print(region)
     elif args.command == 'new':
         hf = HexFile()
-        hf.save(args.hexfile)
-    elif args.command == 'add':
-        hf = HexFile()
-        hf.load(args.hexfile)
         data = args.datafile.read()
         hf.addRegion(args.address, data)
+        hf.save(args.hexfile)
+    elif args.command == 'merge':
+        hf = HexFile()
+        hf.load(args.hexfile1)
+        hf2 = HexFile()
+        hf2.load(args.hexfile2)
+        hf.merge(hf2)
+        hf.save(args.rhexfile)
     else:
         raise NotImplementedError()
 
--- a/python/st-flash.py	Wed Jul 24 22:40:29 2013 +0200
+++ b/python/st-flash.py	Thu Jul 25 08:11:30 2013 +0200
@@ -2,6 +2,7 @@
 
 import argparse, sys
 import stlink, stm32
+import hexfile
 
 def hex2int(s):
    if s.startswith('0x'):
@@ -24,7 +25,7 @@
 writeparser.add_argument('address', type=hex2int)
 
 hexwriteparser = subparsers.add_parser('hexwrite', help='write hexfile to flash')
-hexwriteparser.add_argument('filename', type=argparse.FileType('r'))
+hexwriteparser.add_argument('hexfile', type=argparse.FileType('r'))
 
 verifyparser = subparsers.add_parser('verify', help='verify flash contents')
 verifyparser.add_argument('filename', type=argparse.FileType('rb'))
@@ -61,6 +62,11 @@
 elif args.command == 'write':
    content = args.filename.read()
    dev.writeFlash(args.address, content)
+elif args.command == 'hexwrite':
+    hf = hexfile.HexFile()
+    hf.load(args.hexfile)
+    r = hf.regions[0]
+    dev.writeFlash(r.address, r.data)
 elif args.command == 'verify':
    content = args.filename.read()
    dev.verifyFlash(args.address, content)
--- a/python/stm32.py	Wed Jul 24 22:40:29 2013 +0200
+++ b/python/stm32.py	Thu Jul 25 08:11:30 2013 +0200
@@ -27,9 +27,11 @@
    """
    def __init__(self, iface):
       super().__init__(iface)
+
    def __str__(self):
       return 'STM32F4 device size=0x{1:X} id=0x{0:X}'.format(\
          self.UID, self.FlashSize)
+
    def calculate_F4_sector(self, address):
       sectorstarts = []
       a = STM32_FLASH_BASE
@@ -59,14 +61,17 @@
       uid2 = self.iface.read_debug32(uid_base + 0x4)
       uid3 = self.iface.read_debug32(uid_base + 0x8)
       return (uid3 << 64) | (uid2 << 32) | uid1
+
    @property
    def FlashSize(self):
       f_id = self.iface.read_debug32(0x1FFF7A22)
       f_id = f_id >> 16
       return f_id * 1024
+
    @property
    def Running(self):
       return self.iface.Status == stlink.CORE_RUNNING
+
    # flashing commands:
    def writeFlash(self, address, content):
       flashsize = self.FlashSize
@@ -164,9 +169,9 @@
       return image
 
    def waitFlashBusy(self):
-      """ block until flash operation completes. """
-      while self.isFlashBusy():
-         pass
+        """ block until flash operation completes. """
+        while self.isFlashBusy():
+            time.sleep(0.01)
    def isFlashLocked(self):
       cr = self.readFlashCr()
       mask = 1 << FLASH_F4_CR_LOCK
--- a/python/zcc.py	Wed Jul 24 22:40:29 2013 +0200
+++ b/python/zcc.py	Thu Jul 25 08:11:30 2013 +0200
@@ -5,6 +5,7 @@
 import codegenarm
 from transform import CleanPass, SameImmLoadDeletePass
 import outstream
+import hexfile
 
 # Parse arguments:
 parser = argparse.ArgumentParser(description='lcfos Compiler')
@@ -12,6 +13,7 @@
   help='the source file to build')
 parser.add_argument('-d', '--dumpir', action='store_true', help="Dump IR-code")
 parser.add_argument('-o', '--output', help='Output file', metavar='filename')
+parser.add_argument('--hexfile', help='Output hexfile', type=argparse.FileType('w'))
 
 def main(args):
     # Front end:
@@ -55,6 +57,11 @@
     with open(output_filename, 'wb') as f:
         f.write(code_bytes)
 
+    if args.hexfile:
+        hf = hexfile.HexFile()
+        hf.addRegion(0x08000000, code_bytes)
+        hf.save(args.hexfile)
+
 if __name__ == '__main__':
     arguments = parser.parse_args()
     main(arguments)