w5500.c
11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/**
******************************************************************************
* @file w5500.c
* @version V1.2
* @date 2017-11-01
* @brief 读写W5500寄存器底层函数
*
* @company 深圳炜世科技有限公司
* @information WIZnet W5500官方代理商,全程技术支持,价格绝对优势!
* @website www.wisioe.com
* @forum www.w5500.cn
* @qqGroup 383035001
******************************************************************************
*/
#include <stdio.h>
#include <string.h>
#include "w5500_conf.h"
#include "w5500.h"
#include "socket.h"
#include "utility.h"
#ifdef __DEF_IINCHIP_PPP__
#include "md5.h"
#endif
uint16 SSIZE[MAX_SOCK_NUM] = {0,0,0,0,0,0,0,0}; // Max Tx buffer
uint16 RSIZE[MAX_SOCK_NUM] = {0,0,0,0,0,0,0,0}; // Max Rx buffer
uint8 txsize[MAX_SOCK_NUM] = {2,2,2,2,2,2,2,2}; // tx buffer set K bits
uint8 rxsize[MAX_SOCK_NUM] = {2,2,2,2,2,2,2,2}; // rx buffet set K bits
/**
*@brief This function is to get the Max size to receive.
*@param s: socket number
*@return This function return the RX buffer size has been setted
*/
void iinchip_init(void)
{
setMR( MR_RST );
#ifdef __DEF_IINCHIP_DBG__
//printf("MR value is %02x \r\n",IINCHIP_READ_COMMON(MR));
#endif
}
/**
*@brief This function is to get the Max size to receive.
*@param s: socket number
*@return This function return the RX buffer size has been setted
*/
uint16 getIINCHIP_RxMAX(SOCKET s)
{
return RSIZE[s];
}
/**
*@brief This function is to get the Max size to receive.
*@param s: socket number
*@return This function return the RX buffer size has been setted
*/
uint16 getIINCHIP_TxMAX(SOCKET s)
{
return SSIZE[s];
}
/**
*@brief This function is to set up gateway IP address.
*@param addr: a pointer to a 4 -byte array responsible to set the Gateway IP address
*@return None
*/
void setGAR(uint8 * addr )
{
wiz_write_buf(GAR0, addr, 4);
}
/**
*@brief This function is to get gateway IP address.
*@param addr: a pointer to a 4 -byte array responsible to get the Gateway IP address
*@return None
*/
void getGAR(uint8 * addr)
{
wiz_read_buf(GAR0, addr, 4);
}
/**
*@brief This function is to set up SubnetMask address
*@param addr: a pointer to a 4 -byte array responsible to set the subway IP address.
*@return None
*/
void setSUBR(uint8 * addr)
{
wiz_write_buf(SUBR0, addr, 4);
}
/**
*@brief This function is to set up MAC address.
*@param addr: a pointer to a 6 -byte array responsible to set the MAC address.
*@return None
*/
void setSHAR(uint8 * addr)
{
wiz_write_buf(SHAR0, addr, 6);
}
/**
*@brief This function is to set up Source IP address.
*@param addr:a pointer to a 4 -byte array responsible to set the Source IP addres.
*@return None
*/
void setSIPR(uint8 * addr)
{
wiz_write_buf(SIPR0, addr, 4);
}
/**
*@brief This function is to get Subnet mask.
*@param addr:a pointer to a 4 -byte array responsible to set the Subnet mask.
*@return None
*/
void getSUBR(uint8 * addr)
{
wiz_read_buf(SUBR0, addr, 4);
}
/**
*@brief This function is to get up Source MAC .
*@param addr: a pointer to a 6 -byte array responsible to get the MAC
*@return None
*/
void getSHAR(uint8 * addr)
{
wiz_read_buf(SHAR0, addr, 6);
}
/**
*@brief This function is to get up Source IP .
*@param addr: a pointer to a 4 -byte array responsible to get the Source IP
*@return None
*/
void getSIPR(uint8 * addr)
{
wiz_read_buf(SIPR0, addr, 4);
}
/**
*@brief This function is to set the MR register.
*@param val: the value to set to MR
*@return None
*/
void setMR(uint8 val)
{
IINCHIP_WRITE(MR,val);
}
/**
*@brief This function is to get Interrupt register in common register.
*@param None
*@return The value read from the IR register
*/
uint8 getIR( void )
{
return IINCHIP_READ(IR);
}
/**
@brief This function is to set up Retransmission time.
If there is no response from the peer or delay in response then retransmission
will be there as per RTR (Retry Time-value Register)setting
*@param timeout: The value write to the RTR0 register
*@return None
*/
void setRTR(uint16 timeout)
{
IINCHIP_WRITE(RTR0,(uint8)((timeout & 0xff00) >> 8));
IINCHIP_WRITE(RTR1,(uint8)(timeout & 0x00ff));
}
/**
@brief This function is to set the number of Retransmission.
* If there is no response from the peer or delay in response then recorded time
* as per RTR & RCR register seeting then time out will occur.
*@param retry: Times to retry
*@return None
*/
void setRCR(uint8 retry)
{
IINCHIP_WRITE(WIZ_RCR,retry);
}
/**
*@brief This function is to the interrupt mask Enable/Disable appropriate Interrupt. ('1' : interrupt enable)
If any bit in IMR is set as '0' then there is not interrupt signal though the bit is
set in IR register.
*@param mask: the bits to clear
*@return None
*/
void clearIR(uint8 mask)
{
IINCHIP_WRITE(IR, ~mask | getIR() );
}
/**
*@brief This function is to set the maximum segment size of TCP in Active Mode), while in Passive Mode this is set by peer
*@param s: socket number
*@param Sn_MSSR: the maximum segment size
*@return None
*/
void setSn_MSS(SOCKET s, uint16 Sn_MSSR)
{
IINCHIP_WRITE( Sn_MSSR0(s), (uint8)((Sn_MSSR & 0xff00) >> 8));
IINCHIP_WRITE( Sn_MSSR1(s), (uint8)(Sn_MSSR & 0x00ff));
}
/**
*@brief This function is to set the IP Time to live(TTL) Register
*@param s: socket number
*@param Sn_MSSR: the IP Time to live
*@return None
*/
void setSn_TTL(SOCKET s, uint8 ttl)
{
IINCHIP_WRITE( Sn_TTL(s) , ttl);
}
/**
*@brief This function is to read the Interrupt & Soket Status registe
*@param s: socket number
*@return socket interrupt status
*/
uint8 getSn_IR(SOCKET s)
{
return IINCHIP_READ(Sn_IR(s));
}
/**
*@brief This function is to write the Interrupt & Soket Status register to clear the interrupt
*@param s: socket number
*@return socket interrupt status
*/
void setSn_IR(uint8 s, uint8 val)
{
IINCHIP_WRITE(Sn_IR(s), val);
}
/**
*@brief This function is to get socket status
*@param s: socket number
*@return socket status
*/
uint8 getSn_SR(SOCKET s)
{
return IINCHIP_READ(Sn_SR(s));
}
/**
*@brief This fuction is to get socket TX free buf size
* This gives free buffer size of transmit buffer. This is the data size that user can transmit.
* User shuold check this value first and control the size of transmitting data
*@param s: socket number
*@return socket TX free buf size
*/
uint16 getSn_TX_FSR(SOCKET s)
{
uint16 val=0,val1=0;
do
{
val1 = IINCHIP_READ(Sn_TX_FSR0(s));
val1 = (val1 << 8) + IINCHIP_READ(Sn_TX_FSR1(s));
if (val1 != 0)
{
val = IINCHIP_READ(Sn_TX_FSR0(s));
val = (val << 8) + IINCHIP_READ(Sn_TX_FSR1(s));
}
} while (val != val1);
return val;
}
/**
*@brief This fuction is to give size of received data in receive buffer.
*@param s: socket number
*@return socket TX free buf size
*/
uint16 getSn_RX_RSR(SOCKET s)
{
uint16 val=0,val1=0;
do
{
val1 = IINCHIP_READ(Sn_RX_RSR0(s));
val1 = (val1 << 8) + IINCHIP_READ(Sn_RX_RSR1(s));
if(val1 != 0)
{
val = IINCHIP_READ(Sn_RX_RSR0(s));
val = (val << 8) + IINCHIP_READ(Sn_RX_RSR1(s));
}
} while (val != val1);
return val;
}
/**
*@brief This function is being called by send() and sendto() function also.
* This function read the Tx write pointer register and after copy the data in buffer update the Tx write pointer
* register. User should read upper byte first and lower byte later to get proper value.
*@param s: socket number
*@param data: data buffer to send
*@param len: data length
*@return socket TX free buf size
*/
void send_data_processing(SOCKET s, uint8 *data, uint16 len)
{
uint16 ptr =0;
uint32 addrbsb =0;
if(len == 0)
{
//printf("CH: %d Unexpected1 length 0\r\n", s);
return;
}
ptr = IINCHIP_READ( Sn_TX_WR0(s) );
ptr = ((ptr & 0x00ff) << 8) + IINCHIP_READ(Sn_TX_WR1(s));
addrbsb = (uint32)(ptr<<8) + (s<<5) + 0x10;
wiz_write_buf(addrbsb, data, len);
ptr += len;
IINCHIP_WRITE( Sn_TX_WR0(s) ,(uint8)((ptr & 0xff00) >> 8));
IINCHIP_WRITE( Sn_TX_WR1(s),(uint8)(ptr & 0x00ff));
}
/**
*@brief This function is being called by recv() also.
* This function read the Rx read pointer register
* and after copy the data from receive buffer update the Rx write pointer register.
* User should read upper byte first and lower byte later to get proper value.
*@param s: socket number
*@param data: data buffer to receive
*@param len: data length
*@return None
*/
void recv_data_processing(SOCKET s, uint8 *data, uint16 len)
{
uint16 ptr = 0;
uint32 addrbsb = 0;
if(len == 0)
{
//printf(" CH: %d Unexpected2 length 0\r\n", s);
return;
}
ptr = IINCHIP_READ( Sn_RX_RD0(s) );
ptr = ((ptr & 0x00ff) << 8) + IINCHIP_READ( Sn_RX_RD1(s) );
addrbsb = (uint32)(ptr<<8) + (s<<5) + 0x18;
wiz_read_buf(addrbsb, data, len);
ptr += len;
IINCHIP_WRITE( Sn_RX_RD0(s), (uint8)((ptr & 0xff00) >> 8));
IINCHIP_WRITE( Sn_RX_RD1(s), (uint8)(ptr & 0x00ff));
}
/**
*@brief This function set the transmit & receive buffer size as per the channels is used
*@Note: TMSR and RMSR bits are as follows\n
* Maximum memory size for Tx, Rx in the W5500 is 16K Bytes,\n
* In the range of 16KBytes, the memory size could be allocated dynamically by each channel.\n
* Be attentive to sum of memory size shouldn't exceed 8Kbytes\n
* and to data transmission and receiption from non-allocated channel may cause some problems.\n
* If the 16KBytes memory is already assigned to centain channel, \n
* other 3 channels couldn't be used, for there's no available memory.\n
* If two 4KBytes memory are assigned to two each channels, \n
* other 2 channels couldn't be used, for there's no available memory.\n
*@param tx_size: tx buffer size to set=tx_size[s]*(1024)
*@param rx_size: rx buffer size to set=rx_size[s]*(1024)
*@return None
*/
void socket_buf_init( uint8 * tx_size, uint8 * rx_size )
{
int16 i;
int16 ssum=0,rsum=0;
for (i = 0 ; i < MAX_SOCK_NUM; i++) // Set the size, masking and base address of Tx & Rx memory by each channel
{
IINCHIP_WRITE( (Sn_TXMEM_SIZE(i)), tx_size[i]);
IINCHIP_WRITE( (Sn_RXMEM_SIZE(i)), rx_size[i]);
#ifdef __DEF_IINCHIP_DBG__
//printf(" tx_size[%d]: %d, Sn_TXMEM_SIZE = %d\r\n",i, tx_size[i], IINCHIP_READ(Sn_TXMEM_SIZE(i)));
//printf(" rx_size[%d]: %d, Sn_RXMEM_SIZE = %d\r\n",i, rx_size[i], IINCHIP_READ(Sn_RXMEM_SIZE(i)));
#endif
SSIZE[i] = (int16)(0);
RSIZE[i] = (int16)(0);
if (ssum <= 16384)
{
SSIZE[i] = (int16)tx_size[i]*(1024);
}
if (rsum <= 16384)
{
RSIZE[i]=(int16)rx_size[i]*(1024);
}
ssum += SSIZE[i];
rsum += RSIZE[i];
}
}
void setKPALVTR(SOCKET i,uint8 val)
{
IINCHIP_WRITE(Sn_KPALVTR(i),val);
}
/**
*@brief 检测物理层连接
*@param 无
*@return 无
*/
void PHY_check(void)
{
uint8 PHY_connect=0;
PHY_connect=0x01&getPHYStatus();
if(PHY_connect==0)
{
//printf(" \r\n 请检查网线是否连接?\r\n");
while(PHY_connect == 0) {
PHY_connect=0x01&getPHYStatus();
//printf(" .");
delay_ms(500);
}
//printf(" \r\n");
}
// if(PHY_connect)
// delay_ms(1);
}
uint8 getPHYStatus( void )
{
return IINCHIP_READ(PHYCFGR);
}