Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add offset option to freedom-bin2hex script for supporting SPI flash #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions freedom-bin2hex.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ def grouper(iterable, n, fillvalue=None):
return zip_longest(*args, fillvalue=fillvalue)


def convert(bit_width, infile, outfile):
def convert(bit_width, offset, infile, outfile):
byte_width = bit_width // 8

for i in range(offset // byte_width):
outfile.write('00'.zfill(2 * byte_width) + '\n')

if sys.version_info >= (3, 0):
for row in grouper(infile.read(), byte_width, fillvalue=0):
# Reverse because in Verilog most-significant bit of vectors is first.
Expand All @@ -33,7 +37,6 @@ def convert(bit_width, infile, outfile):
hex_row = ''.join('{:02x}'.format(ord(b)) for b in reversed(row))
outfile.write(hex_row + '\n')


def main():
parser = argparse.ArgumentParser(
description='Convert a binary file to a format that can be read in '
Expand All @@ -54,6 +57,11 @@ def main():
nargs='?',
type=argparse.FileType('w'),
default=sys.stdout)
parser.add_argument('--offset',
type=lambda x: int(x,0),
default=0x0,
help='How many zeros to prepend.')

parser.add_argument('--bit-width', '-w',
type=int,
required=True,
Expand All @@ -62,8 +70,8 @@ def main():

if args.bit_width % 8 != 0:
sys.exit("Cannot handle non-multiple-of-8 bit width yet.")
convert(args.bit_width, args.infile, args.outfile)
convert(args.bit_width, args.offset, args.infile, args.outfile)


if __name__ == '__main__':
main()
main()