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/] [fx3/] [ztex-i2c.c] - 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 FX3 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
    MAC-EEPROM support.
27
*/
28
 
29
#include <cyu3i2c.h>
30
 
31
#ifndef _ZTEX_I2C_C_
32
#define _ZTEX_I2C_C_
33
 
34
#define ZTEX_MAC_EEPROM_ADDR 0xA6
35
 
36
uint8_t ztex_mac_eeprom_ec = 0;
37
 
38
uint8_t ztex_config_data_valid = 0;
39
 
40
const char ztex_hexdigits[] = "0123456789ABCDEF";
41
 
42
uint8_t ztex_mac_eeprom_write(uint8_t addr, uint8_t *buf, uint8_t size) {
43
    CyU3PI2cPreamble_t preamble;
44
    uint8_t size2;
45
 
46
    ztex_mac_eeprom_ec = 1;
47
 
48
    while ( size > 0 ) {
49
        preamble.length    = 2;
50
        preamble.buffer[0] = ZTEX_MAC_EEPROM_ADDR;
51
        preamble.buffer[1] = addr;
52
        preamble.ctrlMask  = 0x0000;
53
        size2 = 8-(addr & 7);
54
        if (size<size2) size2 = size;
55
 
56
        ZTEX_REC_RET( CyU3PI2cTransmitBytes (&preamble, buf, size2, 0) );
57
 
58
        addr+=size2;
59
        buf+=size2;
60
        size-=size2;
61
 
62
        preamble.length = 1;
63
        ZTEX_REC_RET( CyU3PI2cWaitForAck(&preamble, 600) );
64
 
65
        CyU3PThreadSleep (1);
66
    }
67
 
68
    ztex_mac_eeprom_ec = 0;
69
    return 0;
70
}
71
 
72
uint8_t ztex_mac_eeprom_read(uint8_t addr, uint8_t *buf, uint8_t size) {
73
    CyU3PI2cPreamble_t preamble;
74
 
75
    ztex_mac_eeprom_ec = 1;
76
 
77
    preamble.length    = 3;
78
    preamble.buffer[0] = ZTEX_MAC_EEPROM_ADDR;
79
    preamble.buffer[1] = addr;
80
    preamble.buffer[2] = ZTEX_MAC_EEPROM_ADDR | 1;
81
    preamble.ctrlMask  = 0x0002;
82
 
83
    ZTEX_REC_RET ( CyU3PI2cReceiveBytes (&preamble, buf, size, 0) );
84
 
85
 
86
        ztex_mac_eeprom_ec = 0;
87
 
88
    return 0;
89
}
90
 
91
 
92
// VR 0x3D
93
uint8_t vr_mac_eeprom_info(uint16_t value, uint16_t index, uint16_t length ) {
94
    ztex_ep0buf[0]=ztex_mac_eeprom_ec;
95
    ZTEX_REC_RET( CyU3PUsbSendEP0Data( 1, ztex_ep0buf ) );
96
    return 0;
97
}
98
 
99
// VR 0x3B
100
uint8_t vr_mac_eeprom_read(uint16_t value, uint16_t index, uint16_t length ) {
101
    if ( ztex_mac_eeprom_read( (uint8_t)(value & 255), ztex_ep0buf, length) ) return 255;
102
    ZTEX_REC_RET ( CyU3PUsbSendEP0Data( length, ztex_ep0buf ) );
103
    return 0;
104
}
105
 
106
// VC 0x3C
107
uint8_t vc_mac_eeprom_write(uint16_t value, uint16_t index, uint16_t length ) {
108
    ZTEX_REC_RET ( CyU3PUsbGetEP0Data (length, ztex_ep0buf, NULL) );
109
    if ( ztex_mac_eeprom_write( (uint8_t)(value & 255), ztex_ep0buf, length)  ) return 255;
110
    return 0;
111
}
112
 
113
 
114
void ztex_i2c_init() {
115
    CyU3PReturnStatus_t status = CY_U3P_SUCCESS;
116
 
117
    CyU3PI2cConfig_t i2cConfig;
118
    uint8_t buf[5];
119
 
120
    ZTEX_REC( status=CyU3PI2cInit() );
121
    ztex_mac_eeprom_ec = status != CY_U3P_SUCCESS;
122
 
123
    CyU3PMemSet ((uint8_t *)&i2cConfig, 0, sizeof(i2cConfig));
124
    i2cConfig.bitRate    = 100000;
125
    i2cConfig.busTimeout = 0xFFFFFFFF;
126
    i2cConfig.dmaTimeout = 0xFFFF;
127
    i2cConfig.isDma      = CyFalse;
128
    ZTEX_REC( status = CyU3PI2cSetConfig (&i2cConfig, NULL) );
129
    ztex_mac_eeprom_ec = status != CY_U3P_SUCCESS;
130
 
131
    ztex_register_vendor_req(0x3D, vr_mac_eeprom_info);
132
    ztex_register_vendor_req(0x3B, vr_mac_eeprom_read);
133
    ztex_register_vendor_cmd(0x3C, vc_mac_eeprom_write);
134
 
135
    // check for configuration data
136
    if ( ztex_mac_eeprom_read(0, buf, 3)==0 && buf[0]==67 && buf[1]==68 && buf[2]==48 ) {  // check signature
137
        ztex_config_data_valid = 1;
138
        ztex_mac_eeprom_read ( 16, (uint8_t *)ztex_sn_string, 10 );     // copy serial number
139
 
140
        if ( ztex_mac_eeprom_read ( 32, buf, 5 )==0 ) {                  // USB ID's plus 1st char of product string
141
            if ( (buf[0]!=0) && (buf[0]!=255) && (buf[1]!=0) && (buf[1]!=255) ) {
142
                for (int i=0; i<4; i++ ) {
143
                    ztex_usb3_device_descriptor[8+i] = ztex_usb2_device_descriptor[8+i] = buf[i];
144
                }
145
            }
146
            if ( buf[4]!=0 ) {
147
                ztex_mac_eeprom_read ( 36, (uint8_t*)ztex_product_string, 32 ); // copy product string
148
                ztex_product_string[33]='\0';
149
            }
150
        }
151
    }
152
    else {
153
        ztex_config_data_valid = 0;
154
    }
155
 
156
    // check for configuration data
157
    for (int i=0; i<10; i++) {   // abort if SN != "0000000000"
158
        if ( ztex_sn_string[i] != '0' )
159
            return;
160
    }
161
 
162
    if ( ztex_mac_eeprom_read ( 0xfb, buf, 5 )) return;                 // read the last 5 MAC digits
163
    for (int i=0; i<5; i++) {                                            // convert to MAC to SN string
164
        ztex_sn_string[i*2]   = ztex_hexdigits[buf[i]>>4];
165
        ztex_sn_string[i*2+1] = ztex_hexdigits[buf[i] & 15];
166
    }
167
}
168
 
169
#endif // _ZTEX_I2C_C_

powered by: WebSVN 2.1.0

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