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

Subversion Repositories usb_fpga_2_13

[/] [usb_fpga_2_13/] [trunk/] [include/] [ztex-eeprom.h] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*!
2
   ZTEX Firmware Kit for EZ-USB FX2 Microcontrollers
3
   Copyright (C) 2009-2014 ZTEX GmbH.
4
   http://www.ztex.de
5
 
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License version 3 as
8
   published by the Free Software Foundation.
9
 
10
   This program is distributed in the hope that it will be useful, but
11
   WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
   General Public License for more details.
14
 
15
   You should have received a copy of the GNU General Public License
16
   along with this program; if not, see http://www.gnu.org/licenses/.
17
!*/
18
 
19
/*
20
    EEPROM support an some I2C helper routines
21
*/
22
 
23
#ifndef[ZTEX_EEPROM_H]
24
#define[ZTEX_EEPROM_H]
25
 
26
#define[@CAPABILITY_EEPROM;]
27
#define[EEPROM_ADDR][0xA2]
28
 
29
/* *********************************************************************
30
   ***** global variables **********************************************
31
   ********************************************************************* */
32
__xdata WORD eeprom_addr;
33
__xdata WORD eeprom_write_bytes;
34
__xdata BYTE eeprom_write_checksum;
35
 
36
 
37
/* *********************************************************************
38
   ***** i2c_waitWrite *************************************************
39
   ********************************************************************* */
40
/* Do the necessary steps after writing I2DAT register. Returns 1 on error. */
41
BYTE i2c_waitWrite()
42
{
43
    unsigned char i2csbuf,toc;
44
    for ( toc=0; toc<255 && !(I2CS & bmBIT0); toc++ );
45
    i2csbuf = I2CS;
46
    if ( (i2csbuf & bmBIT2) || (!(i2csbuf & bmBIT1)) ) {
47
        I2CS |= bmBIT6;
48
        return 1;
49
    }
50
    return 0;
51
}
52
 
53
/* *********************************************************************
54
   ***** i2c_waitRead **************************************************
55
   ********************************************************************* */
56
/* Do the necessary steps after reading I2DAT register. Returns 1 on error. */
57
BYTE i2c_waitRead(void)
58
{
59
    unsigned char i2csbuf, toc;
60
    for ( toc=0; toc<255 && !(I2CS & bmBIT0); toc++ );
61
    i2csbuf = I2CS;
62
    if (i2csbuf & bmBIT2) {
63
        I2CS |= bmBIT6;
64
        return 1;
65
    }
66
    return 0;
67
}
68
 
69
/* *********************************************************************
70
   ***** i2c_waitStart *************************************************
71
   ********************************************************************* */
72
/* Do the necessary steps after start bit. Returns 1 on error. */
73
BYTE i2c_waitStart()
74
{
75
    BYTE toc;
76
    for ( toc=0; toc<255; toc++ ) {
77
        if ( ! (I2CS & bmBIT2) )
78
            return 0;
79
    }
80
    return 1;
81
}
82
 
83
/* *********************************************************************
84
   ***** i2c_waitStop **************************************************
85
   ********************************************************************* */
86
/* Do the necessary steps after stop bit. Returns 1 on error. */
87
BYTE i2c_waitStop()
88
{
89
    BYTE toc;
90
    for ( toc=0; toc<255; toc++ ) {
91
        if ( ! (I2CS & bmBIT6) )
92
            return 0;
93
    }
94
    return 1;
95
}
96
 
97
/* *********************************************************************
98
   ***** eeprom_select *************************************************
99
   ********************************************************************* */
100
/* Select the EEPROM device, i.e. send the control Byte.
101
   <to> specifies the time to wait in 0.1ms steps if the EEPROM is busy (during a write cycle).
102
   if <stop>=0 no sop bit is sent. Returns 1 on error or if EEPROM is busy. */
103
BYTE eeprom_select (BYTE addr, BYTE to, BYTE stop ) {
104
    BYTE toc = 0;
105
eeprom_select_start:
106
    I2CS |= bmBIT7;             // start bit
107
    i2c_waitStart();
108
    I2DAT = addr;               // select device for writing
109
    if ( ! i2c_waitWrite() ) {
110
        if ( stop ) {
111
            I2CS |= bmBIT6;
112
            i2c_waitStop();
113
        }
114
        return 0;
115
    }
116
    else if (toc<to) {
117
        uwait(10);
118
        goto eeprom_select_start;
119
    }
120
    if ( stop ) {
121
        I2CS |= bmBIT6;
122
    }
123
    return 1;
124
}
125
 
126
/* *********************************************************************
127
   ***** eeprom_read ***************************************************
128
   ********************************************************************* */
129
/* Reads <length> bytes from EEPROM address <addr> and write them to buf.
130
   Returns the number of bytes read. */
131
BYTE eeprom_read ( __xdata BYTE *buf, WORD addr, BYTE length ) {
132
    BYTE bytes = 0,i;
133
 
134
    if ( length == 0 )
135
        return 0;
136
 
137
    if ( eeprom_select(EEPROM_ADDR, 100,0) )
138
        goto eeprom_read_end;
139
 
140
    I2DAT = HI(addr);           // write address
141
    if ( i2c_waitWrite() ) goto eeprom_read_end;
142
    I2DAT = LO(addr);           // write address
143
    if ( i2c_waitWrite() ) goto eeprom_read_end;
144
    I2CS |= bmBIT6;
145
    i2c_waitStop();
146
 
147
    I2CS |= bmBIT7;             // start bit
148
    i2c_waitStart();
149
    I2DAT = EEPROM_ADDR | 1;    // select device for reading
150
    if ( i2c_waitWrite() ) goto eeprom_read_end;
151
 
152
    *buf = I2DAT;               // dummy read
153
    if ( i2c_waitRead()) goto eeprom_read_end;
154
    for (; bytes<length; bytes++ ) {
155
        *buf = I2DAT;           // read data
156
        buf++;
157
        if ( i2c_waitRead()) goto eeprom_read_end;
158
    }
159
 
160
    I2CS |= bmBIT5;             // no ACK
161
    i = I2DAT;                  // dummy read
162
    if ( i2c_waitRead()) goto eeprom_read_end;
163
 
164
    I2CS |= bmBIT6;             // stop bit
165
    i = I2DAT;                  // dummy read
166
    i2c_waitStop();
167
 
168
eeprom_read_end:
169
    return bytes;
170
}
171
 
172
/* *********************************************************************
173
   ***** eeprom_write **************************************************
174
   ********************************************************************* */
175
/* Writes <length> bytes from buf to EEPROM address <addr>.
176
   <length> must be smaller or equal than 8. Returns the number of bytes
177
   read. */
178
BYTE eeprom_write ( __xdata BYTE *buf, WORD addr, BYTE length ) {
179
    BYTE bytes = 0;
180
 
181
    if ( length == 0 )
182
        return 0;
183
 
184
    if ( eeprom_select(EEPROM_ADDR, 100,0) )
185
        goto eeprom_write_end;
186
 
187
    I2DAT = HI(addr);           // write address
188
    if ( i2c_waitWrite() ) goto eeprom_write_end;
189
    I2DAT = LO(addr);           // write address
190
    if ( i2c_waitWrite() ) goto eeprom_write_end;
191
 
192
    for (; bytes<length; bytes++ ) {
193
        I2DAT = *buf;           // write data 
194
        eeprom_write_checksum += *buf;
195
        buf++;
196
        eeprom_write_bytes+=1;
197
        if ( i2c_waitWrite() ) goto eeprom_write_end;
198
    }
199
    I2CS |= bmBIT6;             // stop bit
200
    i2c_waitStop();
201
 
202
eeprom_write_end:
203
    return bytes;
204
}
205
 
206
/* *********************************************************************
207
   ***** EP0 vendor request 0x38 ***************************************
208
   ********************************************************************* */
209
BYTE eeprom_read_ep0 () {
210
    BYTE i, b;
211
    b = ep0_payload_transfer;
212
    i = eeprom_read(EP0BUF, eeprom_addr, b);
213
    eeprom_addr += b;
214
    return i;
215
}
216
 
217
ADD_EP0_VENDOR_REQUEST((0x38,,                          // read from EEPROM
218
    eeprom_addr =  (SETUPDAT[3] << 8) | SETUPDAT[2];    // Address
219
    EP0BCH = 0;
220
    EP0BCL = eeprom_read_ep0();
221
,,
222
    EP0BCH = 0;
223
    EP0BCL = eeprom_read_ep0();
224
));;
225
 
226
 
227
/* *********************************************************************
228
   ***** EP0 vendor command 0x39 ***************************************
229
   ********************************************************************* */
230
void eeprom_write_ep0 ( BYTE length ) {
231
    eeprom_write(EP0BUF, eeprom_addr, length);
232
    eeprom_addr += length;
233
}
234
 
235
ADD_EP0_VENDOR_COMMAND((0x39,,                          // write to EEPROM
236
    eeprom_write_checksum = 0;
237
    eeprom_write_bytes = 0;
238
    eeprom_addr =  ( SETUPDAT[3] << 8) | SETUPDAT[2];   // Address
239
,,
240
    eeprom_write_ep0(EP0BCL);
241
    ));;
242
 
243
/* *********************************************************************
244
   ***** EP0 vendor request 0x3A ***************************************
245
   ********************************************************************* */
246
ADD_EP0_VENDOR_REQUEST((0x3A,,                          // EEPROM state
247
    EP0BUF[0] = LSB(eeprom_write_bytes);
248
    EP0BUF[1] = MSB(eeprom_write_bytes);
249
    EP0BUF[2] = eeprom_write_checksum;
250
    EP0BUF[3] = eeprom_select(EEPROM_ADDR,0,1);          // 1 means busy or error
251
    EP0BCH = 0;
252
    EP0BCL = 4;
253
,,));;
254
 
255
 
256
#ifdef[MAC_EEPROM_ENABLED]
257
#define[EEPROM_MAC_ADDR][0xA6]
258
#define[@CAPABILITY_MAC_EEPROM;]
259
 
260
__xdata BYTE mac_eeprom_addr;
261
 
262
// details about the configuration data structure can be found at 
263
// http://www.ztex.de/firmware-kit/docs/java/ztex/ConfigData.html
264
 
265
__xdata BYTE config_data_valid;
266
 
267
/* *********************************************************************
268
   ***** mac_eeprom_read ***********************************************
269
   ********************************************************************* */
270
/* Reads <length> bytes from EEPROM address <addr> and write them to buf.
271
   Returns the number of bytes read. */
272
BYTE mac_eeprom_read ( __xdata BYTE *buf, BYTE addr, BYTE length ) {
273
    BYTE bytes = 0,i;
274
 
275
    if ( length == 0 )
276
        return 0;
277
 
278
    if ( eeprom_select(EEPROM_MAC_ADDR, 100,0) )
279
        goto mac_eeprom_read_end;
280
 
281
    I2DAT = addr;               // write address
282
    if ( i2c_waitWrite() ) goto mac_eeprom_read_end;
283
    I2CS |= bmBIT6;
284
    i2c_waitStop();
285
 
286
    I2CS |= bmBIT7;             // start bit
287
    i2c_waitStart();
288
    I2DAT = EEPROM_MAC_ADDR | 1;  // select device for reading
289
    if ( i2c_waitWrite() ) goto mac_eeprom_read_end;
290
 
291
    *buf = I2DAT;               // dummy read
292
    if ( i2c_waitRead()) goto mac_eeprom_read_end;
293
    for (; bytes<length; bytes++ ) {
294
        *buf = I2DAT;           // read data
295
        buf++;
296
        if ( i2c_waitRead()) goto mac_eeprom_read_end;
297
    }
298
 
299
    I2CS |= bmBIT5;             // no ACK
300
    i = I2DAT;                  // dummy read
301
    if ( i2c_waitRead()) goto mac_eeprom_read_end;
302
 
303
    I2CS |= bmBIT6;             // stop bit
304
    i = I2DAT;                  // dummy read
305
    i2c_waitStop();
306
 
307
mac_eeprom_read_end:
308
    return bytes;
309
}
310
 
311
/* *********************************************************************
312
   ***** mac_eeprom_write **********************************************
313
   ********************************************************************* */
314
/* Writes <length> bytes from buf to and write them EEPROM address <addr>.
315
   <length> must be smaller or equal than 8. Returns the number of bytes
316
   written. */
317
BYTE mac_eeprom_write ( __xdata BYTE *buf, BYTE addr, BYTE length ) {
318
    BYTE bytes = 0;
319
 
320
    if ( length == 0 )
321
        return 0;
322
 
323
    if ( eeprom_select(EEPROM_MAC_ADDR, 100,0) )
324
        goto mac_eeprom_write_end;
325
 
326
    I2DAT = addr;               // write address
327
    if ( i2c_waitWrite() ) goto mac_eeprom_write_end;
328
 
329
    while ( bytes<length ) {
330
        I2DAT = *buf;           // write data 
331
        buf++;
332
        if ( i2c_waitWrite() ) goto mac_eeprom_write_end;
333
 
334
        addr++;
335
        bytes++;
336
        if ( ( (addr & 8) == 0 ) && ( bytes<length ) ) {
337
            I2CS |= bmBIT6;             // stop bit
338
            i2c_waitStop();
339
 
340
            if ( eeprom_select(EEPROM_MAC_ADDR, 100,0) )
341
                goto mac_eeprom_write_end;
342
 
343
            I2DAT = addr;               // write address
344
            if ( i2c_waitWrite() ) goto mac_eeprom_write_end;
345
        }
346
    }
347
    I2CS |= bmBIT6;             // stop bit
348
    i2c_waitStop();
349
 
350
mac_eeprom_write_end:
351
    mac_eeprom_addr = addr;
352
    return bytes;
353
}
354
 
355
/* *********************************************************************
356
   ***** EP0 vendor request 0x3B ***************************************
357
   ********************************************************************* */
358
BYTE mac_eeprom_read_ep0 () {
359
    BYTE i, b;
360
    b = ep0_payload_transfer;
361
    i = mac_eeprom_read(EP0BUF, mac_eeprom_addr, b);
362
    mac_eeprom_addr += b;
363
    return i;
364
}
365
 
366
ADD_EP0_VENDOR_REQUEST((0x3B,,                          // read from EEPROM
367
    mac_eeprom_addr =  SETUPDAT[2];                     // Address
368
    EP0BCH = 0;
369
    EP0BCL = mac_eeprom_read_ep0();
370
,,
371
    EP0BCH = 0;
372
    EP0BCL = mac_eeprom_read_ep0();
373
));;
374
 
375
 
376
/* *********************************************************************
377
   ***** EP0 vendor command 0x3C ***************************************
378
   ********************************************************************* */
379
ADD_EP0_VENDOR_COMMAND((0x3C,,                          // write to EEPROM
380
    mac_eeprom_addr =  SETUPDAT[2];                     // address
381
,,
382
    mac_eeprom_write(EP0BUF, mac_eeprom_addr, EP0BCL);
383
    ));;
384
 
385
/* *********************************************************************
386
   ***** EP0 vendor request 0x3D ***************************************
387
   ********************************************************************* */
388
ADD_EP0_VENDOR_REQUEST((0x3D,,                          // EEPROM state
389
    EP0BUF[0] = eeprom_select(EEPROM_MAC_ADDR,0,1);       // 1 means busy or error
390
    EP0BCH = 0;
391
    EP0BCL = 1;
392
,,));;
393
 
394
 
395
#endif // MAC_EEPROM_ENABLED
396
 
397
#endif  /*ZTEX_EEPROM_H*/

powered by: WebSVN 2.1.0

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