Created
September 7, 2012 13:13
-
-
Save suapapa/3666173 to your computer and use it in GitHub Desktop.
generate firmware for s5k4ecgx driver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# s5k4ecgx_genfw.py - generate firmware for s5k4ecgx driver | |
# | |
# Copyright (C) 2012 Homin Lee <[email protected]> | |
# | |
# This program is free software; you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation; either version 2 of the License, or | |
# (at your option) any later version. | |
import sys | |
import struct | |
import zlib | |
import re | |
def appendCRC(fName): | |
fp = open(fName, 'rb') | |
crc = zlib.crc32(fp.read()) | |
fp.close() | |
crc ^= 0xffffffff | |
crc &= 0xffffffff | |
fp = open(fName, 'a+') | |
fp.write(struct.pack('I', crc)) | |
fp.close() | |
print "crc = 0x%08x"%crc | |
return crc | |
def makeFirmware(fIn, fOut): | |
""" | |
Write decode-firmware from decoded-form text; | |
{ 0xd0001082, 0x01aa }, // {addr, value} | |
firtst 6 bytes of output firmware is count of addr-valeu set. | |
""" | |
fpIn = open(fIn, 'r') | |
fpOut = open(fOut, 'wb') | |
rePack = re.compile(r'(0x[A-Fa-f0-9]*)[\s,]*(0x[A-Fa-f0-9]*)') | |
writeCount = 0 | |
fpOut.seek(6) | |
for line in fpIn: | |
line = line.strip() | |
if line.startswith('//') or not line: | |
continue | |
mPack = rePack.findall(line) | |
if len(mPack) != 1: | |
print line | |
continue | |
addr, value = map(lambda x : int(x, 16), mPack[0]) | |
bPack = struct.pack('IH', addr, value) | |
writeCount += 1 | |
fpOut.write(bPack) | |
fpIn.close() | |
print "count = %d"%writeCount | |
fpOut.seek(0) | |
fpOut.write(struct.pack('H', writeCount)) | |
fpOut.close() | |
appendCRC(fOut) | |
def makeEncodedFirmware(fIn, fOut): | |
""" | |
Write encoded-firmware from decoded-form text; | |
{ 0xd0001082, 0x01aa }, // {addr, value} | |
each addr-value set is written in encoded-form like; | |
0x0028 0xd000 // high-addr | |
0x002a 0x1082 // low-addr | |
0x0f12 0x01aa // value | |
same high-addr or 2-byte increasing can be skipped in this form. | |
firtst 4 bytes of output firmware is count of encoded-form. | |
""" | |
fpIn = open(fIn, 'r') | |
fpOut = open(fOut, 'wb') | |
rePack = re.compile(r'(0x[A-Fa-f0-9]*)[\s,]*(0x[A-Fa-f0-9]*)') | |
addrLast = 0 | |
writeCount = 0 | |
fpOut.seek(4) | |
for line in fpIn: | |
line = line.strip() | |
if line.startswith('//') or not line: | |
continue | |
mPack = rePack.findall(line) | |
if len(mPack) != 1: | |
print line | |
continue | |
addr, value = map(lambda x : int(x, 16), mPack[0]) | |
if addr - addrLast == 2: | |
bPack = struct.pack('HH', 0x0f12, value) | |
writeCount += 1 | |
elif (addr ^ addrLast) & 0xffff0000 == 0: | |
bPack = struct.pack('HH', 0x002a, addr & 0xffff) | |
bPack += struct.pack('HH', 0x0f12, value) | |
writeCount += 2 | |
else: | |
bPack = struct.pack('HH', 0x0028, (addr >> 16) & 0xffff) | |
bPack += struct.pack('HH', 0x002a, addr & 0xffff) | |
bPack += struct.pack('HH', 0x0f12, value) | |
writeCount += 3 | |
addrLast = addr | |
fpOut.write(bPack) | |
fpIn.close() | |
print "count = %d"%writeCount | |
fpOut.seek(0) | |
fpOut.write(struct.pack('I', writeCount)) | |
fpOut.close() | |
appendCRC(fOut) | |
if __name__ == '__main__': | |
if len(sys.argv) != 2: | |
print "Usage: %s input_file"%sys.argv[0] | |
sys.exit(1) | |
fileOut = "s5k4ecgx.bin" | |
//makeFirmware(sys.argv[1], "s5k4ecgx.bin") | |
makeEncodedFirmware(sys.argv[1], "s5k4ecgx.bin") | |
print "all done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment