forked from PaulStoffregen/Ethernet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathw5100.cpp
More file actions
607 lines (579 loc) · 15.7 KB
/
w5100.cpp
File metadata and controls
607 lines (579 loc) · 15.7 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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
* Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 2
* or the GNU Lesser General Public License version 2.1, both as
* published by the Free Software Foundation.
*/
#include "w5100.h"
#if defined(__arm__) && defined(TEENSYDUINO)
#include "SPIFIFO.h"
#ifdef HAS_SPIFIFO
#define USE_SPIFIFO
#endif
#endif
//#define W5500_4K_BUFFERS
//#define W5200_4K_BUFFERS
// If the core library defines a SS pin, use it as the
// default. Otherwise, default the default to pin 10.
#if defined(__AVR__)
#define SS_PIN_DEFAULT 10
#elif defined(PIN_SPI_SS)
#define SS_PIN_DEFAULT PIN_SPI_SS
#elif defined(CORE_SS0_PIN)
#define SS_PIN_DEFAULT CORE_SS0_PIN
#else
#define SS_PIN_DEFAULT 10
#endif
// W5100 controller instance
uint16_t W5100Class::SBASE[MAX_SOCK_NUM];
uint16_t W5100Class::RBASE[MAX_SOCK_NUM];
uint16_t W5100Class::CH_BASE;
uint16_t W5100Class::SSIZE;
uint16_t W5100Class::SMASK;
uint8_t W5100Class::chip;
uint8_t W5100Class::ss_pin = SS_PIN_DEFAULT;
W5100Class W5100;
// pointers and bitmasks for optimized SS pin
#if defined(__AVR__)
volatile uint8_t * W5100Class::ss_pin_reg;
uint8_t W5100Class::ss_pin_mask;
#elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK66FX1M0__) || defined(__MK64FX512__)
volatile uint8_t * W5100Class::ss_pin_reg;
#elif defined(__MKL26Z64__)
volatile uint8_t * W5100Class::ss_pin_reg;
uint8_t W5100Class::ss_pin_mask;
#elif defined(__SAM3X8E__) || defined(__SAM3A8C__) || defined(__SAM3A4C__)
volatile uint32_t * W5100Class::ss_pin_reg;
uint32_t W5100Class::ss_pin_mask;
#elif defined(__PIC32MX__)
volatile uint32_t * W5100Class::ss_pin_reg;
uint32_t W5100Class::ss_pin_mask;
#elif defined(ARDUINO_ARCH_ESP8266)
volatile uint32_t * W5100Class::ss_pin_reg;
uint32_t W5100Class::ss_pin_mask;
#elif defined(__SAMD21G18A__)
volatile uint32_t * W5100Class::ss_pin_reg;
uint32_t W5100Class::ss_pin_mask;
#endif
uint8_t W5100Class::init(void)
{
uint16_t TXBUF_BASE, RXBUF_BASE;
uint8_t i;
// Many Ethernet shields have a CAT811 or similar reset chip
// connected to W5100 or W5200 chips. The W5200 will not work at
// all, and may even drive its MISO pin, until given an active low
// reset pulse! The CAT811 has a 240 ms typical pulse length, and
// a 400 ms worst case maximum pulse length. MAX811 has a worst
// case maximum 560 ms pulse length. This delay is meant to wait
// until the reset pulse is ended. If your hardware has a shorter
// reset time, this can be edited or removed.
delay(560);
//Serial.println("w5100 init");
#ifdef USE_SPIFIFO
SPI.begin();
SPIFIFO.begin(ss_pin, SPI_CLOCK_12MHz); // W5100 is 14 MHz max
#else
SPI.begin();
initSS();
resetSS();
#endif
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
// Attempt W5200 detection first, because W5200 does not properly
// reset its SPI state when CS goes high (inactive). Communication
// from detecting the other chips can leave the W5200 in a state
// where it won't recover, unless given a reset pulse.
if (isW5200()) {
CH_BASE = 0x4000;
#ifdef W5200_4K_BUFFERS
SSIZE = 4096;
SMASK = 0x0FFF;
#else
SSIZE = 2048; // 2K buffers
SMASK = 0x07FF;
#endif
TXBUF_BASE = 0x8000;
RXBUF_BASE = 0xC000;
for (i=0; i<MAX_SOCK_NUM; i++) {
writeSnRX_SIZE(i, SSIZE >> 10);
writeSnTX_SIZE(i, SSIZE >> 10);
}
for (; i<8; i++) {
writeSnRX_SIZE(i, 0);
writeSnTX_SIZE(i, 0);
}
// Try W5500 next. Wiznet finally seems to have implemented
// SPI well with this chip. It appears to be very resilient,
// so try it after the fragile W5200
} else if (isW5500()) {
CH_BASE = 0x1000;
#ifdef W5500_4K_BUFFERS
SSIZE = 4096; // 4K buffers
SMASK = 0x0FFF;
#else
SSIZE = 2048; // 2K buffers
SMASK = 0x07FF;
#endif
TXBUF_BASE = 0x8000;
RXBUF_BASE = 0xC000;
#ifdef W5500_4K_BUFFERS
for (i=0; i<MAX_SOCK_NUM; i++) {
writeSnRX_SIZE(i, SSIZE >> 10);
writeSnTX_SIZE(i, SSIZE >> 10);
}
for (; i<8; i++) {
writeSnRX_SIZE(i, 0);
writeSnTX_SIZE(i, 0);
}
#endif
// Try W5100 last. This simple chip uses fixed 4 byte frames
// for every 8 bit access. Terribly inefficient, but so simple
// it recovers from "hearing" unsuccessful W5100 or W5200
// communication. W5100 is also the only chip without a VERSIONR
// register for identification, so we check this last.
} else if (isW5100()) {
CH_BASE = 0x0400;
SSIZE = 2048;
SMASK = 0x07FF;
TXBUF_BASE = 0x4000;
RXBUF_BASE = 0x6000;
writeTMSR(0x55);
writeRMSR(0x55);
// No hardware seems to be present. Or it could be a W5200
// that's heard other SPI communication if its chip select
// pin wasn't high when a SD card or other SPI chip was used.
} else {
//Serial.println("no chip :-(");
chip = 0;
SPI.endTransaction();
return 0; // no known chip is responding :-(
}
SPI.endTransaction();
// Initialize the socket base addresses
for (int i=0; i<MAX_SOCK_NUM; i++) {
SBASE[i] = TXBUF_BASE + SSIZE * i;
RBASE[i] = RXBUF_BASE + SSIZE * i;
}
return 1; // successful init
}
// Soft reset the Wiznet chip, by writing to its MR register reset bit
uint8_t W5100Class::softReset(void)
{
uint16_t count=0;
//Serial.println("Wiznet soft reset");
// write to reset bit
writeMR(0x80);
// then wait for soft reset to complete
do {
uint8_t mr = readMR();
//Serial.print("mr=");
//Serial.println(mr, HEX);
if (mr == 0) return 1;
delay(1);
} while (++count < 20);
return 0;
}
uint8_t W5100Class::isW5100(void)
{
chip = 51;
//Serial.println("w5100.cpp: detect W5100 chip");
if (!softReset()) return 0;
writeMR(0x10);
if (readMR() != 0x10) return 0;
writeMR(0x12);
if (readMR() != 0x12) return 0;
writeMR(0x00);
if (readMR() != 0x00) return 0;
//Serial.println("chip is W5100");
return 1;
}
uint8_t W5100Class::isW5200(void)
{
chip = 52;
//Serial.println("w5100.cpp: detect W5200 chip");
if (!softReset()) return 0;
writeMR(0x08);
if (readMR() != 0x08) return 0;
writeMR(0x10);
if (readMR() != 0x10) return 0;
writeMR(0x00);
if (readMR() != 0x00) return 0;
int ver = readVERSIONR_W5200();
//Serial.print("version=");
//Serial.println(ver);
if (ver != 3) return 0;
//Serial.println("chip is W5200");
return 1;
}
uint8_t W5100Class::isW5500(void)
{
chip = 55;
//Serial.println("w5100.cpp: detect W5500 chip");
if (!softReset()) return 0;
writeMR(0x08);
if (readMR() != 0x08) return 0;
writeMR(0x10);
if (readMR() != 0x10) return 0;
writeMR(0x00);
if (readMR() != 0x00) return 0;
int ver = readVERSIONR_W5500();
//Serial.print("version=");
//Serial.println(ver);
if (ver != 4) return 0;
//Serial.println("chip is W5500");
return 1;
}
#ifdef USE_SPIFIFO
uint16_t W5100Class::write(uint16_t addr, const uint8_t *buf, uint16_t len)
{
uint32_t i;
if (chip == 51) {
for (i=0; i<len; i++) {
SPIFIFO.write16(0xF000 | (addr >> 8), SPI_CONTINUE);
SPIFIFO.write16((addr << 8) | buf[i]);
addr++;
SPIFIFO.read();
SPIFIFO.read();
}
} else if (chip == 52) {
SPIFIFO.clear();
SPIFIFO.write16(addr, SPI_CONTINUE);
SPIFIFO.write16(len | 0x8000, SPI_CONTINUE);
for (i=0; i<len; i++) {
SPIFIFO.write(buf[i], ((i+1<len) ? SPI_CONTINUE : 0));
SPIFIFO.read();
}
SPIFIFO.read();
SPIFIFO.read();
} else {
//SPIFIFO.clear();
SPIFIFO.write16(addr, SPI_CONTINUE);
if (addr < 0x100) {
// common registers 00nn
SPIFIFO.write16(0x0400 | *buf++,
((len > 1) ? SPI_CONTINUE : 0));
} else if (addr < 0x8000) {
// socket registers 10nn, 11nn, 12nn, 13nn, etc
SPIFIFO.write16(((addr << 5) & 0xE000) | 0x0C00 | *buf++,
((len > 1) ? SPI_CONTINUE : 0));
} else if (addr < 0xC000) {
// transmit buffers 8000-87FF, 8800-8FFF, 9000-97FF, etc
#ifdef W5500_4K_BUFFERS
SPIFIFO.write16(((addr << 1) & 0x6000) | 0x1400 | *buf++, // 4K buffers
((len > 1) ? SPI_CONTINUE : 0));
#else
SPIFIFO.write16(((addr << 2) & 0xE000) | 0x1400 | *buf++, // 2K buffers
((len > 1) ? SPI_CONTINUE : 0));
#endif
} else {
// receive buffers
#ifdef W5500_4K_BUFFERS
SPIFIFO.write16(((addr << 1) & 0x6000) | 0x1C00 | *buf++, // 4K buffers
((len > 1) ? SPI_CONTINUE : 0));
#else
SPIFIFO.write16(((addr << 2) & 0xE000) | 0x1C00 | *buf++, // 2K buffers
((len > 1) ? SPI_CONTINUE : 0));
#endif
}
len--;
while (len >= 2) {
len -= 2;
SPIFIFO.write16((*buf << 8) | *(buf+1), (len == 0) ? 0 : SPI_CONTINUE);
buf += 2;
SPIFIFO.read();
}
if (len) {
SPIFIFO.write(*buf);
SPIFIFO.read();
}
SPIFIFO.read();
SPIFIFO.read();
}
return len;
}
#else
uint16_t W5100Class::write(uint16_t addr, const uint8_t *buf, uint16_t len)
{
if (chip == 51) {
for (uint16_t i=0; i<len; i++) {
setSS();
SPI.transfer(0xF0);
SPI.transfer(addr >> 8);
SPI.transfer(addr & 0xFF);
addr++;
SPI.transfer(buf[i]);
resetSS();
}
} else if (chip == 52) {
setSS();
SPI.transfer(addr >> 8);
SPI.transfer(addr & 0xFF);
SPI.transfer(((len >> 8) & 0x7F) | 0x80);
SPI.transfer(len & 0xFF);
for (uint16_t i=0; i<len; i++) {
SPI.transfer(buf[i]);
}
resetSS();
} else {
setSS();
if (addr < 0x100) {
// common registers 00nn
SPI.transfer(0);
SPI.transfer(addr & 0xFF);
SPI.transfer(0x04);
} else if (addr < 0x8000) {
// socket registers 10nn, 11nn, 12nn, 13nn, etc
SPI.transfer(0);
SPI.transfer(addr & 0xFF);
SPI.transfer(((addr >> 3) & 0xE0) | 0x0C);
} else if (addr < 0xC000) {
// transmit buffers 8000-87FF, 8800-8FFF, 9000-97FF, etc
// 10## #nnn nnnn nnnn
SPI.transfer(addr >> 8);
SPI.transfer(addr & 0xFF);
#ifdef W5500_4K_BUFFERS
SPI.transfer(((addr >> 7) & 0x60) | 0x14); // 4K buffers
#else
SPI.transfer(((addr >> 6) & 0xE0) | 0x14); // 2K buffers
#endif
} else {
// receive buffers
SPI.transfer(addr >> 8);
SPI.transfer(addr & 0xFF);
#ifdef W5500_4K_BUFFERS
SPI.transfer(((addr >> 7) & 0x60) | 0x1C); // 4K buffers
#else
SPI.transfer(((addr >> 6) & 0xE0) | 0x1C); // 2K buffers
#endif
}
for (uint16_t i=0; i<len; i++) {
SPI.transfer(buf[i]);
}
resetSS();
}
return len;
}
#endif
#ifdef USE_SPIFIFO
uint16_t W5100Class::read(uint16_t addr, uint8_t *buf, uint16_t len)
{
uint32_t i;
if (chip == 51) {
for (i=0; i<len; i++) {
#if 1
SPIFIFO.write(0x0F, SPI_CONTINUE);
SPIFIFO.write16(addr, SPI_CONTINUE);
addr++;
SPIFIFO.read();
SPIFIFO.write(0);
SPIFIFO.read();
buf[i] = SPIFIFO.read();
#endif
#if 0
// this does not work, but why?
SPIFIFO.write16(0x0F00 | (addr >> 8), SPI_CONTINUE);
SPIFIFO.write16(addr << 8);
addr++;
SPIFIFO.read();
buf[i] = SPIFIFO.read();
#endif
}
} else if (chip == 52) {
// len = 1: write header, write 1 byte, read
// len = 2: write header, write 2 byte, read
// len = 3,5,7
SPIFIFO.clear();
SPIFIFO.write16(addr, SPI_CONTINUE);
SPIFIFO.write16(len & 0x7FFF, SPI_CONTINUE);
SPIFIFO.read();
if (len == 1) {
// read only 1 byte
SPIFIFO.write(0);
SPIFIFO.read();
*buf = SPIFIFO.read();
} else if (len == 2) {
// read only 2 bytes
SPIFIFO.write16(0);
SPIFIFO.read();
uint32_t val = SPIFIFO.read();
*buf++ = val >> 8;
*buf = val;
} else if ((len & 1)) {
// read 3 or more, odd length
//Serial.print("W5200 read, len=");
//Serial.println(len);
uint32_t count = len / 2;
SPIFIFO.write16(0, SPI_CONTINUE);
SPIFIFO.read();
do {
if (count > 1) SPIFIFO.write16(0, SPI_CONTINUE);
else SPIFIFO.write(0);
uint32_t val = SPIFIFO.read();
//TODO: WebClient_speedtest with READSIZE 7 is
//dramatically faster with this Serial.print(),
//and the 2 above, but not without both. Why?!
//Serial.println(val, HEX);
*buf++ = val >> 8;
*buf++ = val;
} while (--count > 0);
*buf = SPIFIFO.read();
//Serial.println(*buf, HEX);
} else {
// read 4 or more, even length
//Serial.print("W5200 read, len=");
//Serial.println(len);
uint32_t count = len / 2 - 1;
SPIFIFO.write16(0, SPI_CONTINUE);
SPIFIFO.read();
do {
SPIFIFO.write16(0, (count > 1) ? SPI_CONTINUE : 0);
uint32_t val = SPIFIFO.read();
*buf++ = val >> 8;
*buf++ = val;
} while (--count > 0);
uint32_t val = SPIFIFO.read();
*buf++ = val >> 8;
*buf++ = val;
}
} else {
//SPIFIFO.clear();
SPIFIFO.write16(addr, SPI_CONTINUE);
if (addr < 0x100) {
// common registers 00nn
SPIFIFO.write16(0,
((len > 1) ? SPI_CONTINUE : 0));
} else if (addr < 0x8000) {
// socket registers 10nn, 11nn, 12nn, 13nn, etc
SPIFIFO.write16(((addr << 5) & 0xE000) | 0x0800,
((len > 1) ? SPI_CONTINUE : 0));
} else if (addr < 0xC000) {
// transmit buffers 8000-87FF, 8800-8FFF, 9000-97FF, etc
#ifdef W5500_4K_BUFFERS
SPIFIFO.write16(((addr << 1) & 0x6000) | 0x1000, // 4K buffers
((len > 1) ? SPI_CONTINUE : 0));
#else
SPIFIFO.write16(((addr << 2) & 0xE000) | 0x1000, // 2K buffers
((len > 1) ? SPI_CONTINUE : 0));
#endif
} else {
// receive buffers
#ifdef W5500_4K_BUFFERS
SPIFIFO.write16(((addr << 1) & 0x6000) | 0x1800, // 4K buffers
((len > 1) ? SPI_CONTINUE : 0));
#else
SPIFIFO.write16(((addr << 2) & 0xE000) | 0x1800, // 2K buffers
((len > 1) ? SPI_CONTINUE : 0));
#endif
}
SPIFIFO.read();
if (len <= 1) {
*buf++ = SPIFIFO.read();
} else if (len == 2) {
SPIFIFO.write(0);
*buf++ = SPIFIFO.read();
*buf++ = SPIFIFO.read();
} else if (len & 1) {
uint32_t count = len >> 1;
SPIFIFO.write16(0, (count > 1) ? SPI_CONTINUE : 0);
*buf++ = SPIFIFO.read();
while (count > 1) {
count--;
SPIFIFO.write16(0, (count > 1) ? SPI_CONTINUE : 0);
uint32_t val = SPIFIFO.read();
*buf++ = val >> 8;
*buf++ = val;
}
uint32_t val = SPIFIFO.read();
*buf++ = val >> 8;
*buf++ = val;
} else {
SPIFIFO.write16(0, SPI_CONTINUE);
*buf++ = SPIFIFO.read();
uint32_t count = len >> 1;
while (count > 1) {
count--;
if (count > 1) {
SPIFIFO.write16(0, SPI_CONTINUE);
} else {
SPIFIFO.write(0, 0);
}
uint32_t val = SPIFIFO.read();
*buf++ = val >> 8;
*buf++ = val;
}
*buf = SPIFIFO.read();
}
}
return len;
}
#else
uint16_t W5100Class::read(uint16_t addr, uint8_t *buf, uint16_t len)
{
if (chip == 51) {
for (uint16_t i=0; i<len; i++) {
setSS();
SPI.transfer(0x0F);
SPI.transfer(addr >> 8);
SPI.transfer(addr & 0xFF);
addr++;
buf[i] = SPI.transfer(0);
resetSS();
}
} else if (chip == 52) {
setSS();
SPI.transfer(addr >> 8);
SPI.transfer(addr & 0xFF);
SPI.transfer((len >> 8) & 0x7F);
SPI.transfer(len & 0xFF);
for (uint16_t i=0; i<len; i++) {
buf[i] = SPI.transfer(0);
}
resetSS();
} else {
setSS();
if (addr < 0x100) {
// common registers 00nn
SPI.transfer(0);
SPI.transfer(addr & 0xFF);
SPI.transfer(0x00);
} else if (addr < 0x8000) {
// socket registers 10nn, 11nn, 12nn, 13nn, etc
SPI.transfer(0);
SPI.transfer(addr & 0xFF);
SPI.transfer(((addr >> 3) & 0xE0) | 0x08);
} else if (addr < 0xC000) {
// transmit buffers 8000-87FF, 8800-8FFF, 9000-97FF, etc
// 10## #nnn nnnn nnnn
SPI.transfer(addr >> 8);
SPI.transfer(addr & 0xFF);
#ifdef W5500_4K_BUFFERS
SPI.transfer(((addr >> 7) & 0x60) | 0x10); // 4K buffers
#else
SPI.transfer(((addr >> 6) & 0xE0) | 0x10); // 2K buffers
#endif
} else {
// receive buffers
SPI.transfer(addr >> 8);
SPI.transfer(addr & 0xFF);
#ifdef W5500_4K_BUFFERS
SPI.transfer(((addr >> 7) & 0x60) | 0x18); // 4K buffers
#else
SPI.transfer(((addr >> 6) & 0xE0) | 0x18); // 2K buffers
#endif
}
for (uint16_t i=0; i<len; i++) {
buf[i] = SPI.transfer(0);
}
resetSS();
}
return len;
}
#endif
void W5100Class::execCmdSn(SOCKET s, SockCMD _cmd) {
// Send command to socket
writeSnCR(s, _cmd);
// Wait for command to complete
while (readSnCR(s))
;
}