summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Buesch <mb@bu3sch.de>2011-01-01 13:37:01 +0100
committerMichael Buesch <mb@bu3sch.de>2011-01-01 13:37:01 +0100
commit2082eb09b4c0ff32d1a65c823998169329c67a79 (patch)
treed205e4040612d4d8df8c2394230292163fb76934
parent01c95e2936b0fbd7067c272300ca73bcff93c202 (diff)
downloadtoprammer-2082eb09b4c0ff32d1a65c823998169329c67a79.tar.xz
toprammer-2082eb09b4c0ff32d1a65c823998169329c67a79.zip
GUI: Support ihex
Signed-off-by: Michael Buesch <mb@bu3sch.de>
-rw-r--r--libtoprammer/util.py120
-rwxr-xr-xtoprammer121
-rwxr-xr-xtoprammer-gui41
3 files changed, 159 insertions, 123 deletions
diff --git a/libtoprammer/util.py b/libtoprammer/util.py
index d4bcbd3..8f8d5a0 100644
--- a/libtoprammer/util.py
+++ b/libtoprammer/util.py
@@ -73,3 +73,123 @@ def generateHexdump(mem):
def dumpMem(mem):
sys.stdout.write(generateHexdump(mem))
+
+class IO_ihex:
+ TYPE_DATA = 0
+ TYPE_EOF = 1
+ TYPE_ESAR = 2
+ TYPE_SSAR = 3
+ TYPE_ELAR = 4
+ TYPE_SLAR = 5
+
+ def autodetect(self, data):
+ try:
+ self.toBinary(data)
+ except (TOPException), e:
+ return False
+ return True
+
+ def toBinary(self, ihexData):
+ bin = []
+ checksumWarned = False
+ doublewriteWarned = False
+ try:
+ lines = ihexData.splitlines()
+ hiAddr = 0
+ for line in lines:
+ line = line.strip()
+ if len(line) == 0:
+ continue
+ if len(line) < 11 or (len(line) - 1) % 2 != 0:
+ raise TOPException("Invalid IHEX format (length error)")
+ if line[0] != ':':
+ raise TOPException("Invalid IHEX format (magic error)")
+ count = int(line[1:3], 16)
+ if len(line) != count * 2 + 11:
+ raise TOPException("Invalid IHEX format (count error)")
+ addr = (int(line[3:5], 16) << 8) | int(line[5:7], 16)
+ addr |= hiAddr << 16
+ type = int(line[7:9], 16)
+ checksum = 0
+ for i in range(1, len(line), 2):
+ byte = int(line[i:i+2], 16)
+ checksum = (checksum + byte) & 0xFF
+ checksum = checksum & 0xFF
+ if checksum != 0 and not checksumWarned:
+ checksumWarned = True
+ print "WARNING: Invalid IHEX format (checksum error)"
+
+ if type == self.TYPE_EOF:
+ break
+ if type == self.TYPE_ELAR:
+ if count != 2:
+ raise TOPException("Invalid IHEX format (inval ELAR)")
+ hiAddr = (int(line[9:11], 16) << 8) | int(line[11:13], 16)
+ continue
+ if type == self.TYPE_DATA:
+ if len(bin) < addr + count: # Reallocate
+ bin += ['\xFF'] * (addr + count - len(bin))
+ for i in range(9, 9 + count * 2, 2):
+ byte = chr(int(line[i:i+2], 16))
+ if bin[(i - 9) / 2 + addr] != '\xFF' and \
+ not doublewriteWarned:
+ doublewriteWarned = True
+ print "Invalid IHEX format (Wrote twice to same location)"
+ bin[(i - 9) / 2 + addr] = byte
+ continue
+ raise TOPException("Invalid IHEX format (unsup type %d)" % type)
+ except ValueError:
+ raise TOPException("Invalid IHEX format (digit format)")
+ return "".join(bin)
+
+ def fromBinary(self, binData):
+ ihex = []
+ addr = 0
+ for i in range(0, len(binData), 16):
+ if addr > 0xFFFF:
+ checksum = 0
+ ihex.append(":%02X%04X%02X" % (2, 0, self.TYPE_ELAR))
+ checksum += 2 + 0 + 0 + self.TYPE_ELAR
+ a = (addr >> 16) & 0xFFFF
+ ihex.append("%04X" % a)
+ checksum += ((a >> 8) & 0xFF) + (a & 0xFF)
+ checksum = ((checksum ^ 0xFF) + 1) & 0xFF
+ ihex.append("%02X\n" % checksum)
+ addr -= 0xFFFF
+ checksum = 0
+ size = min(len(binData) - i, 16)
+ ihex.append(":%02X%04X%02X" % (size, addr, self.TYPE_DATA))
+ checksum += size + ((addr >> 8) & 0xFF) + (addr & 0xFF) + self.TYPE_DATA
+ for j in range(0, size):
+ data = ord(binData[i + j])
+ checksum = (checksum + data) & 0xFF
+ ihex.append("%02X" % data)
+ checksum = ((checksum ^ 0xFF) + 1) & 0xFF
+ ihex.append("%02X\n" % checksum)
+ addr += size
+ ihex.append(":00000001FF\n")
+ return "".join(ihex)
+
+class IO_hex:
+ def autodetect(self, data):
+ try:
+ self.toBinary(data)
+ except (TOPException), e:
+ return False
+ return True
+
+ def toBinary(self, data):
+ return parseHexdump(data)
+
+ def fromBinary(self, data):
+ return generateHexdump(data)
+
+class IO_binary:
+ def autodetect(self, data):
+ return True
+
+ def toBinary(self, data):
+ return data
+
+ def fromBinary(self, data):
+ return data
diff --git a/toprammer b/toprammer
index 23b0ae8..b1ecaa8 100755
--- a/toprammer
+++ b/toprammer
@@ -22,6 +22,7 @@
"""
from libtoprammer.toprammer_main import *
+from libtoprammer.util import *
import getopt
@@ -74,126 +75,6 @@ def usage():
print " ihex Intel hex"
print " hex Human readable hex"
-class IO_ihex:
- TYPE_DATA = 0
- TYPE_EOF = 1
- TYPE_ESAR = 2
- TYPE_SSAR = 3
- TYPE_ELAR = 4
- TYPE_SLAR = 5
-
- def autodetect(self, data):
- try:
- self.toBinary(data)
- except (TOPException), e:
- return False
- return True
-
- def toBinary(self, ihexData):
- bin = []
- checksumWarned = False
- doublewriteWarned = False
- try:
- lines = ihexData.splitlines()
- hiAddr = 0
- for line in lines:
- line = line.strip()
- if len(line) == 0:
- continue
- if len(line) < 11 or (len(line) - 1) % 2 != 0:
- raise TOPException("Invalid IHEX format (length error)")
- if line[0] != ':':
- raise TOPException("Invalid IHEX format (magic error)")
- count = int(line[1:3], 16)
- if len(line) != count * 2 + 11:
- raise TOPException("Invalid IHEX format (count error)")
- addr = (int(line[3:5], 16) << 8) | int(line[5:7], 16)
- addr |= hiAddr << 16
- type = int(line[7:9], 16)
- checksum = 0
- for i in range(1, len(line), 2):
- byte = int(line[i:i+2], 16)
- checksum = (checksum + byte) & 0xFF
- checksum = checksum & 0xFF
- if checksum != 0 and not checksumWarned:
- checksumWarned = True
- print "WARNING: Invalid IHEX format (checksum error)"
-
- if type == self.TYPE_EOF:
- break
- if type == self.TYPE_ELAR:
- if count != 2:
- raise TOPException("Invalid IHEX format (inval ELAR)")
- hiAddr = (int(line[9:11], 16) << 8) | int(line[11:13], 16)
- continue
- if type == self.TYPE_DATA:
- if len(bin) < addr + count: # Reallocate
- bin += ['\xFF'] * (addr + count - len(bin))
- for i in range(9, 9 + count * 2, 2):
- byte = chr(int(line[i:i+2], 16))
- if bin[(i - 9) / 2 + addr] != '\xFF' and \
- not doublewriteWarned:
- doublewriteWarned = True
- print "Invalid IHEX format (Wrote twice to same location)"
- bin[(i - 9) / 2 + addr] = byte
- continue
- raise TOPException("Invalid IHEX format (unsup type %d)" % type)
- except ValueError:
- raise TOPException("Invalid IHEX format (digit format)")
- return "".join(bin)
-
- def fromBinary(self, binData):
- ihex = []
- addr = 0
- for i in range(0, len(binData), 16):
- if addr > 0xFFFF:
- checksum = 0
- ihex.append(":%02X%04X%02X" % (2, 0, self.TYPE_ELAR))
- checksum += 2 + 0 + 0 + self.TYPE_ELAR
- a = (addr >> 16) & 0xFFFF
- ihex.append("%04X" % a)
- checksum += ((a >> 8) & 0xFF) + (a & 0xFF)
- checksum = ((checksum ^ 0xFF) + 1) & 0xFF
- ihex.append("%02X\n" % checksum)
- addr -= 0xFFFF
- checksum = 0
- size = min(len(binData) - i, 16)
- ihex.append(":%02X%04X%02X" % (size, addr, self.TYPE_DATA))
- checksum += size + ((addr >> 8) & 0xFF) + (addr & 0xFF) + self.TYPE_DATA
- for j in range(0, size):
- data = ord(binData[i + j])
- checksum = (checksum + data) & 0xFF
- ihex.append("%02X" % data)
- checksum = ((checksum ^ 0xFF) + 1) & 0xFF
- ihex.append("%02X\n" % checksum)
- addr += size
- ihex.append(":00000001FF\n")
- return "".join(ihex)
-
-class IO_hex:
- def autodetect(self, data):
- try:
- self.toBinary(data)
- except (TOPException), e:
- return False
- return True
-
- def toBinary(self, data):
- return parseHexdump(data)
-
- def fromBinary(self, data):
- return generateHexdump(data)
-
-class IO_binary:
- def autodetect(self, data):
- return True
-
- def toBinary(self, data):
- return data
-
- def fromBinary(self, data):
- return data
-
IO_handlers = {
"bin" : IO_binary,
"ihex" : IO_ihex,
diff --git a/toprammer-gui b/toprammer-gui
index bc45722..77b06fd 100755
--- a/toprammer-gui
+++ b/toprammer-gui
@@ -22,6 +22,7 @@
"""
from libtoprammer.toprammer_main import *
+from libtoprammer.util import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
@@ -761,7 +762,6 @@ class MainWindow(QMainWindow):
QMessageBox.information(self, "About TOPrammer",
"Copyright (c) Michael Buesch <mb@bu3sch.de>")
- #TODO support ihex
def loadBuffer(self):
bufWidget = self.bufferTab.getCurrentBuffer()
if not bufWidget:
@@ -772,10 +772,17 @@ class MainWindow(QMainWindow):
"The buffer is read-only." %\
bufWidget.getName())
return
+ selectedFilter = QString()
fn = QFileDialog.getOpenFileName(self,
- "%s - binary file" % bufWidget.getName())
+ "%s - binary file" % bufWidget.getName(),
+ QString(),
+ "Intel hex file (*.hex *.ihex);;"
+ "Binary file (*)",
+ selectedFilter)
if not fn:
return
+ extensions = str(selectedFilter).split("(")[1].\
+ split(")")[0].replace("*", "").strip().split()
try:
data = file(fn, "rb").read()
except (IOError), e:
@@ -783,6 +790,17 @@ class MainWindow(QMainWindow):
"Failed to read %s:\n%s" %\
(str(fn), str(e.strerror)))
return
+ try:
+ if ".hex" in extensions or ".ihex" in extensions:
+ data = IO_ihex().toBinary(data)
+ elif not extensions:
+ data = IO_binary().toBinary(data)
+ else:
+ assert(0)
+ except (TOPException), e:
+ QMessageBox.critical(self, "Failed to convert data",
+ "Failed to convert the input file data to binary\n%s" % str(e))
+ return
if not bufWidget.setRawData(data):
QMessageBox.critical(self, "Failed to load data",
"Failed to load the file into the buffer")
@@ -794,10 +812,27 @@ class MainWindow(QMainWindow):
data = bufWidget.getRawData()
if not data:
return
+ selectedFilter = QString()
fn = QFileDialog.getSaveFileName(self,
- "%s - binary file" % bufWidget.getName())
+ "%s - binary file" % bufWidget.getName(),
+ QString(),
+ "Intel hex file (*.hex);;"
+ "Binary file (*)",
+ selectedFilter)
if not fn:
return
+ extensions = str(selectedFilter).split("(")[1].\
+ split(")")[0].replace("*", "").strip().split()
+ if not extensions:
+ extensions = [ "" ]
+ if not fn.endsWith(extensions[0]):
+ fn += extensions[0]
+ if ".hex" in extensions or ".ihex" in extensions:
+ data = IO_ihex().fromBinary(data)
+ elif not extensions[0]:
+ data = IO_binary().fromBinary(data)
+ else:
+ assert(0)
try:
file(fn, "wb").write(data)
except (IOError), e:
bues.ch cgit interface