version 4.1.0 from http://fpga4u.epfl.ch/wiki/FX2
[fx2fw-sdcc] / eeprom.c
1 /*-----------------------------------------------------------------------------\r
2 \r
3  * FTDI EEPROM emulation\r
4 \r
5  *-----------------------------------------------------------------------------\r
6 \r
7  * Copyright (C) 2007 Kolja Waschk, ixo.de\r
8 \r
9  *-----------------------------------------------------------------------------\r
10 \r
11  * This code is part of usbjtag. usbjtag is free software; you can redistribute\r
12 \r
13  * it and/or modify it under the terms of the GNU General Public License as\r
14 \r
15  * published by the Free Software Foundation; either version 2 of the License,\r
16 \r
17  * or (at your option) any later version. usbjtag is distributed in the hope\r
18 \r
19  * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied\r
20 \r
21  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
22 \r
23  * GNU General Public License for more details.  You should have received a\r
24 \r
25  * copy of the GNU General Public License along with this program in the file\r
26 \r
27  * COPYING; if not, write to the Free Software Foundation, Inc., 51 Franklin\r
28 \r
29  * St, Fifth Floor, Boston, MA  02110-1301  USA\r
30 \r
31  *-----------------------------------------------------------------------------\r
32 \r
33  */\r
34 \r
35 \r
36 \r
37 #include "eeprom.h"\r
38 \r
39 #include "usb_descriptors.h"\r
40 \r
41 \r
42 \r
43 xdata unsigned char eeprom[128];\r
44 \r
45 \r
46 \r
47 extern xdata char dscr_vidpidver[6];\r
48 \r
49 extern xdata char dscr_attrpow[2];\r
50 \r
51 extern xdata char dscr_usbver[2];\r
52 \r
53 extern xdata char dscr_strorder[4];\r
54 \r
55 extern xdata char str1[];\r
56 \r
57 extern xdata char str2[];\r
58 \r
59 extern xdata char str3[];\r
60 \r
61 \r
62 \r
63 static unsigned char ee_ptr;\r
64 \r
65 static unsigned short ee_cksum;\r
66 \r
67 \r
68 \r
69 void eeprom_append(unsigned char nb)\r
70 \r
71 {\r
72 \r
73   unsigned char pree_ptr = ee_ptr & ~1;\r
74 \r
75   if(pree_ptr != ee_ptr)\r
76 \r
77   {\r
78 \r
79     ee_cksum = ee_cksum ^((unsigned short)nb << 8);\r
80 \r
81     ee_cksum = ee_cksum ^ eeprom[pree_ptr];\r
82 \r
83     ee_cksum = (ee_cksum << 1) | (ee_cksum >> 15);\r
84 \r
85   };\r
86 \r
87   eeprom[ee_ptr++] = nb;\r
88 \r
89 }\r
90 \r
91 \r
92 \r
93 void eeprom_init(void)\r
94 \r
95 {\r
96 \r
97   char j,sofs;\r
98 \r
99   ee_ptr = 0;\r
100 \r
101   ee_cksum = 0xAAAA;\r
102 \r
103 \r
104 \r
105   eeprom_append(0x00);\r
106 \r
107   eeprom_append(0x00);\r
108 \r
109   for(j=0;j<6;j++) eeprom_append(dscr_vidpidver[j]);\r
110 \r
111   for(j=0;j<2;j++) eeprom_append(dscr_attrpow[j]);\r
112 \r
113   eeprom_append(0x1C);\r
114 \r
115   eeprom_append(0x00);\r
116 \r
117   for(j=0;j<2;j++) eeprom_append(dscr_usbver[j]);\r
118 \r
119   sofs = 0x80 + ee_ptr + 6;\r
120 \r
121   eeprom_append(sofs);\r
122 \r
123   eeprom_append(str1[0]);\r
124 \r
125   sofs += str1[0];\r
126 \r
127   eeprom_append(sofs);\r
128 \r
129   eeprom_append(str2[0]);\r
130 \r
131   sofs += str2[0];\r
132 \r
133   eeprom_append(sofs);\r
134 \r
135   eeprom_append(str3[0]);\r
136 \r
137   for(j=0;j<str1[0];j++) eeprom_append(str1[j]);\r
138 \r
139   for(j=0;j<str2[0];j++) eeprom_append(str2[j]);\r
140 \r
141   for(j=0;j<str3[0];j++) eeprom_append(str3[j]);\r
142 \r
143   for(j=0;j<4;j++) eeprom_append(dscr_strorder[j]);\r
144 \r
145   while(ee_ptr < 126) eeprom_append(0);\r
146 \r
147   eeprom[126] = ee_cksum&0xFF;\r
148 \r
149   eeprom[127] = (ee_cksum>>8)&0xFF;\r
150 \r
151 }\r
152 \r
153 \r
154 \r