提交 | 用户 | age
|
483170
|
1 |
//***************************************************************************** |
Q |
2 |
// |
|
3 |
//! \file w5500.c |
|
4 |
//! \brief W5500 HAL Interface. |
|
5 |
//! \version 1.0.2 |
|
6 |
//! \date 2013/10/21 |
|
7 |
//! \par Revision history |
|
8 |
//! <2014/05/01> V1.0.2 |
|
9 |
//! 1. Implicit type casting -> Explicit type casting. Refer to M20140501 |
|
10 |
//! Fixed the problem on porting into under 32bit MCU |
|
11 |
//! Issued by Mathias ClauBen, wizwiki forum ID Think01 and bobh |
|
12 |
//! Thank for your interesting and serious advices. |
|
13 |
//! <2013/12/20> V1.0.1 |
|
14 |
//! 1. Remove warning |
|
15 |
//! 2. WIZCHIP_READ_BUF WIZCHIP_WRITE_BUF in case _WIZCHIP_IO_MODE_SPI_FDM_ |
|
16 |
//! for loop optimized(removed). refer to M20131220 |
|
17 |
//! <2013/10/21> 1st Release |
|
18 |
//! \author MidnightCow |
|
19 |
//! \copyright |
|
20 |
//! |
|
21 |
//! Copyright (c) 2013, WIZnet Co., LTD. |
|
22 |
//! All rights reserved. |
|
23 |
//! |
|
24 |
//! Redistribution and use in source and binary forms, with or without |
|
25 |
//! modification, are permitted provided that the following conditions |
|
26 |
//! are met: |
|
27 |
//! |
|
28 |
//! * Redistributions of source code must retain the above copyright |
|
29 |
//! notice, this list of conditions and the following disclaimer. |
|
30 |
//! * Redistributions in binary form must reproduce the above copyright |
|
31 |
//! notice, this list of conditions and the following disclaimer in the |
|
32 |
//! documentation and/or other materials provided with the distribution. |
|
33 |
//! * Neither the name of the <ORGANIZATION> nor the names of its |
|
34 |
//! contributors may be used to endorse or promote products derived |
|
35 |
//! from this software without specific prior written permission. |
|
36 |
//! |
|
37 |
//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
38 |
//! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
39 |
//! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
40 |
//! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
|
41 |
//! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
42 |
//! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
43 |
//! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
44 |
//! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
45 |
//! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
46 |
//! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
|
47 |
//! THE POSSIBILITY OF SUCH DAMAGE. |
|
48 |
// |
|
49 |
//***************************************************************************** |
|
50 |
//#include <stdio.h> |
|
51 |
#include "w5500.h" |
|
52 |
|
|
53 |
|
|
54 |
#define _W5500_SPI_VDM_OP_ 0x00 |
|
55 |
#define _W5500_SPI_FDM_OP_LEN1_ 0x01 |
|
56 |
#define _W5500_SPI_FDM_OP_LEN2_ 0x02 |
|
57 |
#define _W5500_SPI_FDM_OP_LEN4_ 0x03 |
|
58 |
|
|
59 |
//////////////////////////////////////////////////// |
|
60 |
|
|
61 |
uint8_t WIZCHIP_READ(uint32_t AddrSel) |
|
62 |
{ |
|
63 |
uint8_t ret; |
|
64 |
|
|
65 |
WIZCHIP_CRITICAL_ENTER(); |
|
66 |
WIZCHIP.CS._select(); |
|
67 |
|
|
68 |
#if( (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SPI_)) |
|
69 |
|
|
70 |
#if ( _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_VDM_ ) |
|
71 |
AddrSel |= (_W5500_SPI_READ_ | _W5500_SPI_VDM_OP_); |
|
72 |
#elif( _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_FDM_ ) |
|
73 |
AddrSel |= (_W5500_SPI_READ_ | _W5500_SPI_FDM_OP_LEN1_); |
|
74 |
#else |
|
75 |
#error "Unsupported _WIZCHIP_IO_SPI_ in W5500 !!!" |
|
76 |
#endif |
|
77 |
|
|
78 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16); |
|
79 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8); |
|
80 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0); |
|
81 |
ret = WIZCHIP.IF.SPI._read_byte(); |
|
82 |
|
|
83 |
#elif ( (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_BUS_) ) |
|
84 |
|
|
85 |
#if (_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_DIR_) |
|
86 |
|
|
87 |
#elif(_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_INDIR_) |
|
88 |
|
|
89 |
#else |
|
90 |
#error "Unsupported _WIZCHIP_IO_MODE_BUS_ in W5500 !!!" |
|
91 |
#endif |
|
92 |
#else |
|
93 |
#error "Unknown _WIZCHIP_IO_MODE_ in W5000. !!!" |
|
94 |
#endif |
|
95 |
|
|
96 |
WIZCHIP.CS._deselect(); |
|
97 |
WIZCHIP_CRITICAL_EXIT(); |
|
98 |
return ret; |
|
99 |
} |
|
100 |
|
|
101 |
void WIZCHIP_WRITE(uint32_t AddrSel, uint8_t wb ) |
|
102 |
{ |
|
103 |
WIZCHIP_CRITICAL_ENTER(); |
|
104 |
WIZCHIP.CS._select(); |
|
105 |
|
|
106 |
#if( (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SPI_)) |
|
107 |
|
|
108 |
#if ( _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_VDM_ ) |
|
109 |
AddrSel |= (_W5500_SPI_WRITE_ | _W5500_SPI_VDM_OP_); |
|
110 |
#elif( _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_FDM_ ) |
|
111 |
AddrSel |= (_W5500_SPI_WRITE_ | _W5500_SPI_FDM_OP_LEN1_); |
|
112 |
#else |
|
113 |
#error "Unsupported _WIZCHIP_IO_SPI_ in W5500 !!!" |
|
114 |
#endif |
|
115 |
|
|
116 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16); |
|
117 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8); |
|
118 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0); |
|
119 |
WIZCHIP.IF.SPI._write_byte(wb); |
|
120 |
|
|
121 |
#elif ( (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_BUS_) ) |
|
122 |
|
|
123 |
#if (_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_DIR_) |
|
124 |
|
|
125 |
#elif(_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_INDIR_) |
|
126 |
|
|
127 |
#else |
|
128 |
#error "Unsupported _WIZCHIP_IO_MODE_BUS_ in W5500 !!!" |
|
129 |
#endif |
|
130 |
#else |
|
131 |
#error "Unknown _WIZCHIP_IO_MODE_ in W5500. !!!" |
|
132 |
#endif |
|
133 |
|
|
134 |
WIZCHIP.CS._deselect(); |
|
135 |
WIZCHIP_CRITICAL_EXIT(); |
|
136 |
} |
|
137 |
|
|
138 |
void WIZCHIP_READ_BUF (uint32_t AddrSel, uint8_t* pBuf, uint16_t len) |
|
139 |
{ |
|
140 |
uint16_t i = 0; |
|
141 |
uint16_t j = 0; |
|
142 |
WIZCHIP_CRITICAL_ENTER(); |
|
143 |
WIZCHIP.CS._select(); |
|
144 |
|
|
145 |
#if( (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SPI_)) |
|
146 |
|
|
147 |
#if ( _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_VDM_ ) |
|
148 |
AddrSel |= (_W5500_SPI_READ_ | _W5500_SPI_VDM_OP_); |
|
149 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16); |
|
150 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8); |
|
151 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0); |
|
152 |
for(i = 0; i < len; i++,j) |
|
153 |
pBuf[i] = WIZCHIP.IF.SPI._read_byte(); |
|
154 |
#elif( _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_FDM_ ) |
|
155 |
AddrSel |= (_W5500_SPI_READ_ | _W5500_SPI_FDM_OP_LEN4_); |
|
156 |
for(i = 0; i < len/4; i++, j) |
|
157 |
{ |
|
158 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16); |
|
159 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8); |
|
160 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0); |
|
161 |
pBuf[i*4] = WIZCHIP.IF.SPI._read_byte(); |
|
162 |
pBuf[i*4+1] = WIZCHIP.IF.SPI._read_byte(); |
|
163 |
pBuf[i*4+2] = WIZCHIP.IF.SPI._read_byte(); |
|
164 |
pBuf[i*4+3] = WIZCHIP.IF.SPI._read_byte(); |
|
165 |
AddrSel = WIZCHIP_OFFSET_INC(AddrSel,4); |
|
166 |
} |
|
167 |
len %= 4; // for the rest data |
|
168 |
// M20131220 : remove for loop |
|
169 |
i *= 4; |
|
170 |
if(len >= 2) |
|
171 |
{ |
|
172 |
AddrSel -= 1; // change _W5500_SPI_FDM_OP_LEN4_ to _W5500_SPI_FDM_OP_LEN2_ |
|
173 |
|
|
174 |
//for(j = 0; j < len/2 ; j++) |
|
175 |
{ |
|
176 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16); |
|
177 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8); |
|
178 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0); |
|
179 |
pBuf[i] = WIZCHIP.IF.SPI._read_byte(); |
|
180 |
pBuf[i+1] = WIZCHIP.IF.SPI._read_byte(); |
|
181 |
i += 2; |
|
182 |
AddrSel = WIZCHIP_OFFSET_INC(AddrSel,2); |
|
183 |
} |
|
184 |
} |
|
185 |
len %= 2; |
|
186 |
if(len) |
|
187 |
{ |
|
188 |
AddrSel -= 1; // change _W5500_SPI_FDM_OP_LEN2_ to _W5500_SPI_FDM_OP_LEN1_ |
|
189 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16); |
|
190 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8); |
|
191 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0); |
|
192 |
pBuf[i] = WIZCHIP.IF.SPI._read_byte(); |
|
193 |
} |
|
194 |
#else |
|
195 |
#error "Unsupported _WIZCHIP_IO_MODE_SPI_ in W5500 !!!" |
|
196 |
#endif |
|
197 |
|
|
198 |
#elif ( (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_BUS_) ) |
|
199 |
|
|
200 |
#if (_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_DIR_) |
|
201 |
|
|
202 |
#elif(_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_INDIR_) |
|
203 |
|
|
204 |
#else |
|
205 |
#error "Unsupported _WIZCHIP_IO_MODE_BUS_ in W5500 !!!" |
|
206 |
#endif |
|
207 |
#else |
|
208 |
#error "Unknown _WIZCHIP_IO_MODE_ in W5500. !!!!" |
|
209 |
#endif |
|
210 |
|
|
211 |
WIZCHIP.CS._deselect(); |
|
212 |
WIZCHIP_CRITICAL_EXIT(); |
|
213 |
} |
|
214 |
|
|
215 |
void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len) |
|
216 |
{ |
|
217 |
uint16_t i = 0; |
|
218 |
uint16_t j = 0; |
|
219 |
WIZCHIP_CRITICAL_ENTER(); |
|
220 |
WIZCHIP.CS._select(); |
|
221 |
|
|
222 |
#if( (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SPI_)) |
|
223 |
|
|
224 |
#if ( _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_VDM_ ) |
|
225 |
AddrSel |= (_W5500_SPI_WRITE_ | _W5500_SPI_VDM_OP_); |
|
226 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16); |
|
227 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8); |
|
228 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0); |
|
229 |
for(i = 0; i < len; i++,j) |
|
230 |
WIZCHIP.IF.SPI._write_byte(pBuf[i]); |
|
231 |
#elif( _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_FDM_ ) |
|
232 |
AddrSel |= (_W5500_SPI_WRITE_ | _W5500_SPI_FDM_OP_LEN4_); |
|
233 |
for(i = 0; i < len/4; i++, j) |
|
234 |
{ |
|
235 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16); |
|
236 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8); |
|
237 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0); |
|
238 |
WIZCHIP.IF.SPI._write_byte(pBuf[i*4] ); |
|
239 |
WIZCHIP.IF.SPI._write_byte(pBuf[i*4+1]); |
|
240 |
WIZCHIP.IF.SPI._write_byte(pBuf[i*4+2]); |
|
241 |
WIZCHIP.IF.SPI._write_byte(pBuf[i*4+3]); |
|
242 |
AddrSel = WIZCHIP_OFFSET_INC(AddrSel,4); |
|
243 |
} |
|
244 |
len %= 4; // for the rest data |
|
245 |
// M20131220 : Remove for loop |
|
246 |
i *= 4; |
|
247 |
if(len >= 2) |
|
248 |
{ |
|
249 |
AddrSel -= 1; // change _W5500_SPI_FDM_OP_LEN4_ to _W5500_SPI_FDM_OP_LEN2_ |
|
250 |
|
|
251 |
//for(j = 0; j < len/2 ; j++) |
|
252 |
{ |
|
253 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16); |
|
254 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8); |
|
255 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0); |
|
256 |
WIZCHIP.IF.SPI._write_byte(pBuf[i] ); |
|
257 |
WIZCHIP.IF.SPI._write_byte(pBuf[i+1]); |
|
258 |
i += 2; |
|
259 |
AddrSel = WIZCHIP_OFFSET_INC(AddrSel, 2); |
|
260 |
} |
|
261 |
len %= 2; |
|
262 |
if(len) |
|
263 |
{ |
|
264 |
AddrSel -= 1; // change _W5500_SPI_FDM_OP_LEN2_ to _W5500_SPI_FDM_OP_LEN1_ |
|
265 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16); |
|
266 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8); |
|
267 |
WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0); |
|
268 |
WIZCHIP.IF.SPI._write_byte(pBuf[i]); |
|
269 |
} |
|
270 |
} |
|
271 |
#else |
|
272 |
#error "Unsupported _WIZCHIP_IO_SPI_ in W5500 !!!" |
|
273 |
#endif |
|
274 |
|
|
275 |
#elif ( (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_BUS_) ) |
|
276 |
|
|
277 |
#if (_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_DIR_) |
|
278 |
|
|
279 |
#elif(_WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_BUS_INDIR_) |
|
280 |
|
|
281 |
#else |
|
282 |
#error "Unsupported _WIZCHIP_IO_MODE_BUS_ in W5500 !!!" |
|
283 |
#endif |
|
284 |
#else |
|
285 |
#error "Unknown _WIZCHIP_IO_MODE_ in W5500. !!!!" |
|
286 |
#endif |
|
287 |
|
|
288 |
WIZCHIP.CS._deselect(); |
|
289 |
WIZCHIP_CRITICAL_EXIT(); |
|
290 |
} |
|
291 |
|
|
292 |
|
|
293 |
uint16_t getSn_TX_FSR(uint8_t sn) |
|
294 |
{ |
|
295 |
uint16_t val=0,val1=0; |
|
296 |
do |
|
297 |
{ |
|
298 |
val1 = WIZCHIP_READ(Sn_TX_FSR(sn)); |
|
299 |
val1 = (val1 << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_FSR(sn),1)); |
|
300 |
if (val1 != 0) |
|
301 |
{ |
|
302 |
val = WIZCHIP_READ(Sn_TX_FSR(sn)); |
|
303 |
val = (val << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_FSR(sn),1)); |
|
304 |
} |
|
305 |
}while (val != val1); |
|
306 |
return val; |
|
307 |
} |
|
308 |
|
|
309 |
|
|
310 |
uint16_t getSn_RX_RSR(uint8_t sn) |
|
311 |
{ |
|
312 |
uint16_t val=0,val1=0; |
|
313 |
do |
|
314 |
{ |
|
315 |
val1 = WIZCHIP_READ(Sn_RX_RSR(sn)); |
|
316 |
val1 = (val1 << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_RSR(sn),1)); |
|
317 |
if (val1 != 0) |
|
318 |
{ |
|
319 |
val = WIZCHIP_READ(Sn_RX_RSR(sn)); |
|
320 |
val = (val << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_RSR(sn),1)); |
|
321 |
} |
|
322 |
}while (val != val1); |
|
323 |
return val; |
|
324 |
} |
|
325 |
|
|
326 |
void wiz_send_data(uint8_t sn, uint8_t *wizdata, uint16_t len) |
|
327 |
{ |
|
328 |
uint16_t ptr = 0; |
|
329 |
uint32_t addrsel = 0; |
|
330 |
if(len == 0) return; |
|
331 |
ptr = getSn_TX_WR(sn); |
|
332 |
//M20140501 : implict type casting -> explict type casting |
|
333 |
//addrsel = (ptr << 8) + (WIZCHIP_TXBUF_BLOCK(sn) << 3); |
|
334 |
addrsel = ((uint32_t)ptr << 8) + (WIZCHIP_TXBUF_BLOCK(sn) << 3); |
|
335 |
// |
|
336 |
WIZCHIP_WRITE_BUF(addrsel,wizdata, len); |
|
337 |
|
|
338 |
ptr += len; |
|
339 |
setSn_TX_WR(sn,ptr); |
|
340 |
} |
|
341 |
|
|
342 |
void wiz_recv_data(uint8_t sn, uint8_t *wizdata, uint16_t len) |
|
343 |
{ |
|
344 |
uint16_t ptr = 0; |
|
345 |
uint32_t addrsel = 0; |
|
346 |
|
|
347 |
if(len == 0) return; |
|
348 |
ptr = getSn_RX_RD(sn); |
|
349 |
//M20140501 : implict type casting -> explict type casting |
|
350 |
//addrsel = ((ptr << 8) + (WIZCHIP_RXBUF_BLOCK(sn) << 3); |
|
351 |
addrsel = ((uint32_t)ptr << 8) + (WIZCHIP_RXBUF_BLOCK(sn) << 3); |
|
352 |
// |
|
353 |
WIZCHIP_READ_BUF(addrsel, wizdata, len); |
|
354 |
ptr += len; |
|
355 |
|
|
356 |
setSn_RX_RD(sn,ptr); |
|
357 |
} |
|
358 |
|
|
359 |
|
|
360 |
void wiz_recv_ignore(uint8_t sn, uint16_t len) |
|
361 |
{ |
|
362 |
uint16_t ptr = 0; |
|
363 |
ptr = getSn_RX_RD(sn); |
|
364 |
ptr += len; |
|
365 |
setSn_RX_RD(sn,ptr); |
|
366 |
} |
|
367 |
|