fxload fx2lp dev board
[fx2fw-sdcc] / eeprom.c
1 /*-----------------------------------------------------------------------------\r
2  * FTDI EEPROM emulation\r
3  *-----------------------------------------------------------------------------\r
4  * Copyright (C) 2007 Kolja Waschk, ixo.de\r
5  *-----------------------------------------------------------------------------\r
6  * This code is part of usbjtag. usbjtag is free software; you can redistribute\r
7  * it and/or modify it under the terms of the GNU General Public License as\r
8  * published by the Free Software Foundation; either version 2 of the License,\r
9  * or (at your option) any later version. usbjtag is distributed in the hope\r
10  * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied\r
11  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
12  * GNU General Public License for more details.  You should have received a\r
13  * copy of the GNU General Public License along with this program in the file\r
14  * COPYING; if not, write to the Free Software Foundation, Inc., 51 Franklin\r
15  * St, Fifth Floor, Boston, MA  02110-1301  USA\r
16  *-----------------------------------------------------------------------------\r
17  */\r
18 \r
19 #include "eeprom.h"\r
20 #include "usb_descriptors.h"\r
21 \r
22 xdata unsigned char eeprom[128];\r
23 \r
24 extern xdata char dscr_vidpidver[6];\r
25 extern xdata char dscr_attrpow[2];\r
26 extern xdata char dscr_usbver[2];\r
27 extern xdata char dscr_strorder[4];\r
28 extern xdata char str1[];\r
29 extern xdata char str2[];\r
30 extern xdata char str3[];\r
31 \r
32 static unsigned char ee_ptr;\r
33 static unsigned short ee_cksum;\r
34 \r
35 void eeprom_append(unsigned char nb)\r
36 {\r
37   unsigned char pree_ptr = ee_ptr & ~1;\r
38   if(pree_ptr != ee_ptr)\r
39   {\r
40     ee_cksum = ee_cksum ^((unsigned short)nb << 8);\r
41     ee_cksum = ee_cksum ^ eeprom[pree_ptr];\r
42     ee_cksum = (ee_cksum << 1) | (ee_cksum >> 15);\r
43   };\r
44   eeprom[ee_ptr++] = nb;\r
45 }\r
46 \r
47 void eeprom_init(void)\r
48 {\r
49   char j,sofs;\r
50   ee_ptr = 0;\r
51   ee_cksum = 0xAAAA;\r
52 \r
53   eeprom_append(0x00);\r
54   eeprom_append(0x00);\r
55   for(j=0;j<6;j++) eeprom_append(dscr_vidpidver[j]);\r
56   for(j=0;j<2;j++) eeprom_append(dscr_attrpow[j]);\r
57   eeprom_append(0x1C);\r
58   eeprom_append(0x00);\r
59   for(j=0;j<2;j++) eeprom_append(dscr_usbver[j]);\r
60   sofs = 0x80 + ee_ptr + 6;\r
61   eeprom_append(sofs);\r
62   eeprom_append(str1[0]);\r
63   sofs += str1[0];\r
64   eeprom_append(sofs);\r
65   eeprom_append(str2[0]);\r
66   sofs += str2[0];\r
67   eeprom_append(sofs);\r
68   eeprom_append(str3[0]);\r
69   for(j=0;j<str1[0];j++) eeprom_append(str1[j]);\r
70   for(j=0;j<str2[0];j++) eeprom_append(str2[j]);\r
71   for(j=0;j<str3[0];j++) eeprom_append(str3[j]);\r
72   for(j=0;j<4;j++) eeprom_append(dscr_strorder[j]);\r
73   while(ee_ptr < 126) eeprom_append(0);\r
74   eeprom[126] = ee_cksum&0xFF;\r
75   eeprom[127] = (ee_cksum>>8)&0xFF;\r
76 }\r
77 \r