root / firmware / fx2 / src / common / build_eeprom.py @ b96088b6
History | View | Annotate | Download (2.9 kB)
| 1 |
#!/usr/bin/env python
|
|---|---|
| 2 |
#
|
| 3 |
# Copyright 2004,2006 Free Software Foundation, Inc.
|
| 4 |
#
|
| 5 |
# This file is part of GNU Radio
|
| 6 |
#
|
| 7 |
# GNU Radio is free software; you can redistribute it and/or modify
|
| 8 |
# it under the terms of the GNU General Public License as published by
|
| 9 |
# the Free Software Foundation; either version 3, or (at your option)
|
| 10 |
# any later version.
|
| 11 |
#
|
| 12 |
# GNU Radio is distributed in the hope that it will be useful,
|
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 15 |
# GNU General Public License for more details.
|
| 16 |
#
|
| 17 |
# You should have received a copy of the GNU General Public License
|
| 18 |
# along with GNU Radio; see the file COPYING. If not, write to
|
| 19 |
# the Free Software Foundation, Inc., 51 Franklin Street,
|
| 20 |
# Boston, MA 02110-1301, USA.
|
| 21 |
#
|
| 22 |
|
| 23 |
import re |
| 24 |
import sys |
| 25 |
import os, os.path |
| 26 |
from optparse import OptionParser |
| 27 |
|
| 28 |
# USB Vendor and Product ID's
|
| 29 |
|
| 30 |
VID = 0xfffe # Free Software Folks |
| 31 |
PID = 0x0002 # Universal Software Radio Peripheral |
| 32 |
|
| 33 |
def msb (x): |
| 34 |
return (x >> 8) & 0xff |
| 35 |
|
| 36 |
def lsb (x): |
| 37 |
return x & 0xff |
| 38 |
|
| 39 |
def build_eeprom_image (filename, rev): |
| 40 |
"""Build a ``C2 Load'' EEPROM image.
|
| 41 |
|
| 42 |
For details on this format, see section 3.4.3 of
|
| 43 |
the EZ-USB FX2 Technical Reference Manual
|
| 44 |
"""
|
| 45 |
# get the code we want to run
|
| 46 |
f = open(filename, 'rb') |
| 47 |
bytes = f.read() |
| 48 |
|
| 49 |
devid = rev |
| 50 |
start_addr = 0 #prove me wrong |
| 51 |
|
| 52 |
rom_header = [ |
| 53 |
0xC2, # boot from EEPROM |
| 54 |
lsb (VID), |
| 55 |
msb (VID), |
| 56 |
lsb (PID), |
| 57 |
msb (PID), |
| 58 |
lsb (devid), |
| 59 |
msb (devid), |
| 60 |
0 # configuration byte |
| 61 |
] |
| 62 |
|
| 63 |
# 4 byte header that indicates where to load
|
| 64 |
# the immediately follow code bytes.
|
| 65 |
code_header = [ |
| 66 |
msb (len (bytes)), |
| 67 |
lsb (len (bytes)), |
| 68 |
msb (start_addr), |
| 69 |
lsb (start_addr) |
| 70 |
] |
| 71 |
|
| 72 |
# writes 0 to CPUCS reg (brings FX2 out of reset)
|
| 73 |
trailer = [ |
| 74 |
0x80,
|
| 75 |
0x01,
|
| 76 |
0xe6,
|
| 77 |
0x00,
|
| 78 |
0x00
|
| 79 |
] |
| 80 |
|
| 81 |
image = rom_header + code_header + [ord(c) for c in bytes] + trailer |
| 82 |
|
| 83 |
assert (len (image) <= 256) |
| 84 |
return image
|
| 85 |
|
| 86 |
if __name__ == '__main__': |
| 87 |
usage = "usage: %prog -r REV [options] bootfile.bin outfile.bin"
|
| 88 |
parser = OptionParser (usage=usage) |
| 89 |
parser.add_option ("-r", "--rev", type="int", default=-1, |
| 90 |
help="Specify USRP revision number REV (2 or 4)")
|
| 91 |
(options, args) = parser.parse_args () |
| 92 |
if len (args) != 2: |
| 93 |
parser.print_help () |
| 94 |
sys.exit (1)
|
| 95 |
if options.rev < 0: |
| 96 |
sys.stderr.write ( |
| 97 |
"You must specify the USRP revision number (2 or 4) with -r REV\n")
|
| 98 |
sys.exit (1)
|
| 99 |
|
| 100 |
infile = args[0]
|
| 101 |
outfile = args[1]
|
| 102 |
|
| 103 |
image = "".join(chr(c) for c in build_eeprom_image(infile, options.rev)) |
| 104 |
|
| 105 |
f = open(outfile, 'wb') |
| 106 |
f.write(str(image))
|
| 107 |
f.close() |