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

Subversion Repositories usb_fpga_2_14

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

powered by: WebSVN 2.1.0

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