OSDN Git Service

Logger/Logger.h: __linux__ changed to __unix__ to support build on cygwin
[stock/stock.osdn.git] / v2h / v2ezusb.py
1 #!/usr/bin/python
2
3 """convert .v to Cypress EZ-USB EEPROM"""
4
5 import os, sys
6 import time
7
8 from load_v import *
9
10 def write_hdr(fout, prefix, in_name, in_base):
11         fout.write('// ' + prefix + ' autogenerated by v2ezusb.py\n')
12         fout.write('// Source file: ' + in_name + '\n')
13         fout.write('static const char ' + prefix + '_src[] = "' + in_base + '";\n')
14         tm = time.localtime(os.path.getmtime(in_name))
15         tm_str = '%d-%02d-%02d %d:%02d:%02d' % (tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec)
16         fout.write('// Source time: ' + tm_str + '\n')
17         fout.write('static const char ' + prefix + '_time[] = "' + tm_str + '";\n\n')
18
19 def write_data(fout, prefix, data):
20         length = len(data)
21         line  = 'enum { ' + prefix + '_length = %d };\n' % length
22         line += 'static const uint8_t ' + prefix + '_data[' + prefix + '_length] = {'
23         for i in range(length):
24            if i>0:
25               line += ','
26            if (i%16) == 0:
27               line += '\n'
28               fout.write(line)
29               line = ''
30            line += '0x%02x' % data[i]
31         if line[-1] != '\n':
32            line += '\n'
33         line += '};\n'
34         fout.write(line)
35
36 if (len(sys.argv) < 4) or (len(sys.argv) > 5):
37         print 'usage: ./v2ezusb <input> <output> <cfg_hex> [<prefix>]'
38         sys.exit(1)
39
40 cfg_byte = int(sys.argv[3], 0x10)
41 assert(cfg_byte >= 0)
42 assert(cfg_byte < 0x100)
43 print 'CFG:',
44 if cfg_byte & 0x80:
45         print 'USB FS (12Mb),',
46 else:
47         print 'USB HS (480Mb),',
48 if cfg_byte & 0x40:
49         print 'DisConnected,',
50 else:
51         print 'Connected,',
52 if cfg_byte & 0x01:
53         print 'I2C 400KHz'
54 else:
55         print 'I2C 100KHz'
56 assert((cfg_byte & ~0xC1) == 0)
57
58 fin_name = sys.argv[1]
59 if not os.path.isfile(fin_name):
60         print 'not a file:', fin_name
61 fin_base = os.path.basename(fin_name)
62 if fin_base.endswith('.v'):
63         fin_base = fin_base[:-2]
64
65 fin = open(fin_name, 'r')
66 sections = v_file_load(fin)
67 fin.close()
68
69 def data_LH(value):
70         return [ (value & 0xFF), (value >> 8) ]
71
72 def data_HL(value):
73         return [ (value >> 8), (value & 0xFF) ]
74
75 VID=0x04b4
76 PID=0x8613
77 DID=0x0010
78
79 # 'C2 Load' Format
80 data = [ 0xC2 ] # Header
81 data += data_LH(VID)
82 data += data_LH(PID)
83 data += data_LH(DID)
84 data += [ cfg_byte ] # Configuration byte
85
86 for sec in sections:
87    sec_addr = sec.start
88    sec_length = sec.len()
89    print 'sec addr:', hex(sec_addr), 'len:', sec_length
90    assert(sec_length > 0)
91
92    addr = sec_addr
93    offset = 0
94    while sec_length > 0:
95            length = sec_length
96            if length >= 1024:
97                    length = 1023
98            print '> addr:', hex(addr), 'len:', length
99            # validate EZ-USB limits
100            assert(addr < 0x4000) # 14-bit address
101            assert(length < 1024) # 10-bit length
102            data += data_HL(length)
103            data += data_HL(addr)
104            for i in range(length):
105                    data.append(int(sec.data[offset + i], 0x10))
106            addr += length
107            offset += length
108            sec_length -= length
109
110 data += data_HL(0x8001) # len=1, last record
111 data += data_HL(0xE600) # CPUCS
112 data += [ 0x00 ] # clear CPU reset
113 #print data
114 print 'eeprom len:', len(data) 
115
116 fout_name = sys.argv[2]
117 print 'file:', fout_name
118 fout = open(fout_name, 'w')
119
120 prefix = fin_base
121 if len(sys.argv) > 4:
122         prefix = sys.argv[4]
123 print 'prefix:', repr(prefix)
124
125 write_hdr(fout, prefix, fin_name, fin_base)
126 write_data(fout, prefix, data)
127 fout.close();