OpenCores
URL https://opencores.org/ocsvn/gecko3/gecko3/trunk

Subversion Repositories gecko3

[/] [gecko3/] [trunk/] [GECKO3COM/] [gecko3com-fw/] [firmware/] [src/] [build_eeprom.py] - Blame information for rev 9

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 9 nussgipfel
#!/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
 
34
def hex_to_bytes (s):
35
    if len (s) & 0x1:
36
        raise ValueError, "Length must be even"
37
    r = []
38
    for i in range (0, len(s), 2):
39
        r.append (int (s[i:i+2], 16))
40
    return r
41
 
42
def msb (x):
43
    return (x >> 8) & 0xff
44
 
45
def lsb (x):
46
    return x & 0xff
47
 
48
class ihx_rec (object):
49
    def __init__ (self, addr, type, data):
50
        self.addr = addr
51
        self.type = type
52
        self.data = data
53
 
54
class ihx_file (object):
55
    def __init__ (self):
56
        self.pat = re.compile (r':[0-9A-F]{10,}')
57
    def read (self, file):
58
        r = []
59
        for line in file:
60
            line = line.strip().upper ()
61
            if not self.pat.match (line):
62
                raise ValueError, "Invalid hex record format"
63
            bytes = hex_to_bytes (line[1:])
64
            sum = reduce (lambda x, y: x + y, bytes, 0) % 256
65
            if sum != 0:
66
                raise ValueError, "Bad hex checksum"
67
            lenx = bytes[0]
68
            addr = (bytes[1] << 8) + bytes[2]
69
            type = bytes[3]
70
            data = bytes[4:-1]
71
            if lenx != len (data):
72
                raise ValueError, "Invalid hex record (bad length)"
73
            if type != 0:
74
                break;
75
            r.append (ihx_rec (addr, type, data))
76
 
77
        return r
78
 
79
def get_code (filename):
80
    """Read the intel hex format file FILENAME and return a tuple
81
    of the code starting address and a list of bytes to load there.
82
    """
83
    f = open (filename, 'r')
84
    ifx = ihx_file ()
85
    r = ifx.read (f)
86
    r.sort (lambda a,b: a.addr - b.addr)
87
    code_start = r[0].addr
88
    code_end = r[-1].addr + len (r[-1].data)
89
    code_len = code_end - code_start
90
    code = [0] * code_len
91
    for x in r:
92
        a = x.addr
93
        l = len (x.data)
94
        code[a-code_start:a-code_start+l] = x.data
95
    return (code_start, code)
96
 
97
 
98
def build_eeprom_image (filename, rev):
99
    """Build a ``C2 Load'' EEPROM image.
100
 
101
    For details on this format, see section 3.4.3 of
102
    the EZ-USB FX2 Technical Reference Manual
103
    """
104
    # get the code we want to run
105
    (start_addr, bytes) = get_code (filename)
106
 
107
    devid = rev
108
 
109
    rom_header = [
110
        0xC2,                           # boot from EEPROM
111
        lsb (VID),
112
        msb (VID),
113
        lsb (PID),
114
        msb (PID),
115
        lsb (devid),
116
        msb (devid),
117
 
118
        ]
119
 
120
    # 4 byte header that indicates where to load
121
    # the immediately follow code bytes.
122
    code_header = [
123
        msb (len (bytes)),
124
        lsb (len (bytes)),
125
        msb (start_addr),
126
        lsb (start_addr)
127
        ]
128
 
129
    # writes 0 to CPUCS reg (brings FX2 out of reset)
130
    trailer = [
131
        0x80,
132
        0x01,
133
        0xe6,
134
        0x00,
135
        0x00
136
        ]
137
 
138
    image = rom_header + code_header + bytes + trailer
139
 
140
    assert (len (image) <= 256)
141
    return image
142
 
143
def build_shell_script (out, ihx_filename, rev):
144
 
145
    image = build_eeprom_image (ihx_filename, rev)
146
 
147
    out.write ('#!/bin/sh\n')
148
    out.write ('usrper -x load_firmware /usr/local/share/usrp/rev%d/std.ihx\n' % rev)
149
    out.write ('sleep 1\n')
150
 
151
    # print "len(image) =", len(image)
152
 
153
    i2c_addr = 0x50
154
    rom_addr = 0x00
155
 
156
    hex_image = map (lambda x : "%02x" % (x,), image)
157
 
158
    while (len (hex_image) > 0):
159
        l = min (len (hex_image), 16)
160
        out.write ('usrper i2c_write 0x%02x %02x%s\n' %
161
                   (i2c_addr, rom_addr, ''.join (hex_image[0:l])))
162
        hex_image = hex_image[l:]
163
        rom_addr = rom_addr + l
164
        out.write ('sleep 1\n')
165
 
166
if __name__ == '__main__':
167
    usage = "usage: %prog -r REV [options] bootfile.ihx"
168
    parser = OptionParser (usage=usage)
169
    parser.add_option ("-r", "--rev", type="int", default=-1,
170
                       help="Specify USRP revision number REV (2 or 4)")
171
    (options, args) = parser.parse_args ()
172
    if len (args) != 1:
173
        parser.print_help ()
174
        sys.exit (1)
175
    if options.rev < 0:
176
        sys.stderr.write (
177
            "You must specify the USRP revision number (2 or 4) with -r REV\n")
178
        sys.exit (1)
179
 
180
    ihx_filename = args[0]
181
 
182
    build_shell_script (sys.stdout, ihx_filename, options.rev)

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.