1 |
2 |
ZTEX |
/*!
|
2 |
|
|
ZTEX Firmware Kit for EZ-USB Microcontrollers
|
3 |
|
|
Copyright (C) 2009-2010 ZTEX e.K.
|
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 to, BYTE stop ) {
|
104 |
|
|
BYTE toc = 0;
|
105 |
|
|
eeprom_select_start:
|
106 |
|
|
I2CS |= bmBIT7; // start bit
|
107 |
|
|
i2c_waitStart();
|
108 |
|
|
I2DAT = EEPROM_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. This number is 0 during a write cycle. */
|
131 |
|
|
BYTE eeprom_read ( __xdata BYTE *buf, WORD addr, BYTE length ) {
|
132 |
|
|
BYTE bytes = 0,i;
|
133 |
|
|
|
134 |
|
|
if ( eeprom_select(100,0) )
|
135 |
|
|
goto eeprom_read_end;
|
136 |
|
|
|
137 |
|
|
I2DAT = HI(addr); // write address
|
138 |
|
|
if ( i2c_waitWrite() ) goto eeprom_read_end;
|
139 |
|
|
I2DAT = LO(addr); // write address
|
140 |
|
|
if ( i2c_waitWrite() ) goto eeprom_read_end;
|
141 |
|
|
I2CS |= bmBIT6;
|
142 |
|
|
i2c_waitStop();
|
143 |
|
|
|
144 |
|
|
I2CS |= bmBIT7; // start bit
|
145 |
|
|
i2c_waitStart();
|
146 |
|
|
I2DAT = EEPROM_ADDR | 1; // select device for reading
|
147 |
|
|
if ( i2c_waitWrite() ) goto eeprom_read_end;
|
148 |
|
|
|
149 |
|
|
*buf = I2DAT; // dummy read
|
150 |
|
|
if ( i2c_waitRead()) goto eeprom_read_end;
|
151 |
|
|
for (; bytes<length; bytes++ ) {
|
152 |
|
|
*buf = I2DAT; // read data
|
153 |
|
|
buf++;
|
154 |
|
|
if ( i2c_waitRead()) goto eeprom_read_end;
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
I2CS |= bmBIT5; // no ACK
|
158 |
|
|
i = I2DAT; // dummy read
|
159 |
|
|
if ( i2c_waitRead()) goto eeprom_read_end;
|
160 |
|
|
|
161 |
|
|
I2CS |= bmBIT6; // stop bit
|
162 |
|
|
i = I2DAT; // dummy read
|
163 |
|
|
i2c_waitStop();
|
164 |
|
|
|
165 |
|
|
eeprom_read_end:
|
166 |
|
|
return bytes;
|
167 |
|
|
}
|
168 |
|
|
|
169 |
|
|
/* *********************************************************************
|
170 |
|
|
***** eeprom_write **************************************************
|
171 |
|
|
********************************************************************* */
|
172 |
|
|
/* Writes <length> bytes from buf to and write them EEPROM address <addr>.
|
173 |
|
|
<length> must be smaller or equal than 64. Returns the number of bytes
|
174 |
|
|
read. This number is 0 during a write cycle. */
|
175 |
|
|
BYTE eeprom_write ( __xdata BYTE *buf, WORD addr, BYTE length ) {
|
176 |
|
|
BYTE bytes = 0;
|
177 |
|
|
|
178 |
|
|
if ( eeprom_select(100,0) )
|
179 |
|
|
goto eeprom_write_end;
|
180 |
|
|
|
181 |
|
|
I2DAT = HI(addr); // write address
|
182 |
|
|
if ( i2c_waitWrite() ) goto eeprom_write_end;
|
183 |
|
|
I2DAT = LO(addr); // write address
|
184 |
|
|
if ( i2c_waitWrite() ) goto eeprom_write_end;
|
185 |
|
|
|
186 |
|
|
for (; bytes<length; bytes++ ) {
|
187 |
|
|
I2DAT = *buf; // write data
|
188 |
|
|
eeprom_write_checksum += *buf;
|
189 |
|
|
buf++;
|
190 |
|
|
eeprom_write_bytes+=1;
|
191 |
|
|
if ( i2c_waitWrite() ) goto eeprom_write_end;
|
192 |
|
|
}
|
193 |
|
|
I2CS |= bmBIT6; // stop bit
|
194 |
|
|
i2c_waitStop();
|
195 |
|
|
|
196 |
|
|
eeprom_write_end:
|
197 |
|
|
return bytes;
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
/* *********************************************************************
|
201 |
|
|
***** EP0 vendor request 0x38 ***************************************
|
202 |
|
|
********************************************************************* */
|
203 |
|
|
BYTE eeprom_read_ep0 () {
|
204 |
|
|
BYTE i, b;
|
205 |
|
|
b = ep0_payload_transfer;
|
206 |
|
|
i = eeprom_read(EP0BUF, eeprom_addr, b);
|
207 |
|
|
eeprom_addr += b;
|
208 |
|
|
return i;
|
209 |
|
|
}
|
210 |
|
|
|
211 |
|
|
ADD_EP0_VENDOR_REQUEST((0x38,, // read from EEPROM
|
212 |
|
|
eeprom_addr = (SETUPDAT[3] << 8) | SETUPDAT[2]; // Address
|
213 |
|
|
EP0BCH = 0;
|
214 |
|
|
EP0BCL = eeprom_read_ep0();
|
215 |
|
|
,,
|
216 |
|
|
EP0BCH = 0;
|
217 |
|
|
EP0BCL = eeprom_read_ep0();
|
218 |
|
|
));;
|
219 |
|
|
|
220 |
|
|
|
221 |
|
|
/* *********************************************************************
|
222 |
|
|
***** EP0 vendor command 0x39 ***************************************
|
223 |
|
|
********************************************************************* */
|
224 |
|
|
void eeprom_write_ep0 ( BYTE length ) {
|
225 |
|
|
eeprom_write(EP0BUF, eeprom_addr, length);
|
226 |
|
|
eeprom_addr += length;
|
227 |
|
|
}
|
228 |
|
|
|
229 |
|
|
ADD_EP0_VENDOR_COMMAND((0x39,, // write to EEPROM
|
230 |
|
|
eeprom_write_checksum = 0;
|
231 |
|
|
eeprom_write_bytes = 0;
|
232 |
|
|
eeprom_addr = ( SETUPDAT[3] << 8) | SETUPDAT[2]; // Address
|
233 |
|
|
,,
|
234 |
|
|
eeprom_write_ep0(EP0BCL);
|
235 |
|
|
));;
|
236 |
|
|
|
237 |
|
|
/* *********************************************************************
|
238 |
|
|
***** EP0 vendor request 0x3A ***************************************
|
239 |
|
|
********************************************************************* */
|
240 |
|
|
ADD_EP0_VENDOR_REQUEST((0x3A,, // EEPROM state
|
241 |
|
|
EP0BUF[0] = LSB(eeprom_write_bytes);
|
242 |
|
|
EP0BUF[1] = MSB(eeprom_write_bytes);
|
243 |
|
|
EP0BUF[2] = eeprom_write_checksum;
|
244 |
|
|
EP0BUF[3] = eeprom_select(0,1); // 1 means busy or error
|
245 |
|
|
EP0BCH = 0;
|
246 |
|
|
EP0BCL = 4;
|
247 |
|
|
,,));;
|
248 |
|
|
|
249 |
|
|
|
250 |
|
|
#endif /*ZTEX_EEPROM_H*/
|