-
Notifications
You must be signed in to change notification settings - Fork 2
/
enc28j60.c
391 lines (312 loc) · 9.21 KB
/
enc28j60.c
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
// ENC28J60 Driver
// Author: Jason Losh
// Modifications: Aditya Mall
// The University of Texas at Arlington
//-----------------------------------------------------------------------------
// Hardware Target
//-----------------------------------------------------------------------------
// Target uC: TM4C123GH6PM
// System Clock: 40 MHz
//-----------------------------------------------------------------------------
// Device includes, defines, and assembler directives
//-----------------------------------------------------------------------------
#include <stdint.h>
#include <stdbool.h>
#include "tm4c123gh6pm.h"
#include "enc28j60.h"
#include "wait.h"
#include <string.h>
#pragma pack(1)
// ------------------------------------------------------------------------------
// Globals
// ------------------------------------------------------------------------------
uint8_t nextPacketLsb = 0x00;
uint8_t nextPacketMsb = 0x00;
//-----------------------------------------------------------------------------
// Subroutines
//-----------------------------------------------------------------------------
void spiWrite(uint8_t data)
{
#if IOT_COURSE_TEST
SSI0_DR_R = data;
while (SSI0_SR_R & SSI_SR_BSY);
#else
SSI2_DR_R = data;
while (SSI2_SR_R & SSI_SR_BSY);
#endif
}
uint8_t spiRead()
{
#if IOT_COURSE_TEST
return SSI0_DR_R;
#else
return SSI2_DR_R;
#endif
}
void etherCsOn()
{
PIN_ETHER_CS = 0;
__asm (" NOP"); // allow line to settle
__asm (" NOP");
__asm (" NOP");
__asm (" NOP");
}
void etherCsOff()
{
PIN_ETHER_CS = 1;
}
void etherWriteReg(uint8_t reg, uint8_t data)
{
etherCsOn();
spiWrite(0x40 | (reg & 0x1F));
spiRead();
spiWrite(data);
spiRead();
etherCsOff();
}
uint8_t etherReadReg(uint8_t reg)
{
uint8_t data;
etherCsOn();
spiWrite(0x00 | (reg & 0x1F));
spiRead();
spiWrite(0);
data = spiRead();
etherCsOff();
return data;
}
void etherSetReg(uint8_t reg, uint8_t mask)
{
etherCsOn();
spiWrite(0x80 | (reg & 0x1F));
spiRead();
spiWrite(mask);
spiRead();
etherCsOff();
}
void etherClearReg(uint8_t reg, uint8_t mask)
{
etherCsOn();
spiWrite(0xA0 | (reg & 0x1F));
spiRead();
spiWrite(mask);
spiRead();
etherCsOff();
}
void etherSetBank(uint8_t reg)
{
etherClearReg(ECON1, 0x03);
etherSetReg(ECON1, reg >> 5);
}
void etherWritePhy(uint8_t reg, uint16_t data)
{
etherSetBank(MIREGADR);
etherWriteReg(MIREGADR, reg);
etherWriteReg(MIWRL, data & 0xFF);
etherWriteReg(MIWRH, (data >> 8) & 0xFF);
}
uint16_t etherReadPhy(uint8_t reg)
{
uint16_t data, data2;
etherSetBank(MIREGADR);
etherWriteReg(MIREGADR, reg);
etherWriteReg(MICMD, etherReadReg(MICMD) | MIIRD);
waitMicrosecond(50);
while ((etherReadReg(MISTAT) | MIBUSY) != 0);
etherWriteReg(MICMD, etherReadReg(MICMD) & ~MIIRD);
data = etherReadReg(MIRDL);
data2 = etherReadReg(MIRDH);
data |= (data2 << 8);
return data;
}
void etherWriteMemStart()
{
etherCsOn();
spiWrite(0x7A);
spiRead();
}
void etherWriteMem(uint8_t data)
{
spiWrite(data);
spiRead();
}
void etherWriteMemStop()
{
etherCsOff();
}
void etherReadMemStart()
{
etherCsOn();
spiWrite(0x3A);
spiRead();
}
uint8_t etherReadMem()
{
spiWrite(0);
return spiRead();
}
void etherReadMemStop()
{
etherCsOff();
}
void setPhyMacAddr(uint8_t *macAddr)
{
// setup mac address
etherSetBank(MAADR0);
etherWriteReg(MAADR5, macAddr[0]);
etherWriteReg(MAADR4, macAddr[1]);
etherWriteReg(MAADR3, macAddr[2]);
etherWriteReg(MAADR2, macAddr[3]);
etherWriteReg(MAADR1, macAddr[4]);
etherWriteReg(MAADR0, macAddr[5]);
}
// Initializes ethernet device
// Uses order suggested in Chapter 6 of datasheet except 6.4 OST which is first here
void etherInit(uint8_t mode, uint8_t *macAddress)
{
// make sure that oscillator start-up timer has expired
while ((etherReadReg(ESTAT) & CLKRDY) == 0) {}
// disable transmission and reception of packets
etherClearReg(ECON1, RXEN);
etherClearReg(ECON1, TXRTS);
// initialize receive buffer space
etherSetBank(ERXSTL);
etherWriteReg(ERXSTL, LOBYTE(0x0000));
etherWriteReg(ERXSTH, HIBYTE(0x0000));
etherWriteReg(ERXNDL, LOBYTE(0x1A09));
etherWriteReg(ERXNDH, HIBYTE(0x1A09));
// initialize receiver write and read ptrs
// at startup, will write from 0 to 1A08 only and will not overwrite rd ptr
etherWriteReg(ERXWRPTL, LOBYTE(0x0000));
etherWriteReg(ERXWRPTH, HIBYTE(0x0000));
etherWriteReg(ERXRDPTL, LOBYTE(0x1A09));
etherWriteReg(ERXRDPTH, HIBYTE(0x1A09));
etherWriteReg(ERDPTL, LOBYTE(0x0000));
etherWriteReg(ERDPTH, HIBYTE(0x0000));
// setup receive filter
// always check CRC, use OR mode
etherSetBank(ERXFCON);
etherWriteReg(ERXFCON, (mode | 0x20) & 0xBF);
// bring mac out of reset
etherSetBank(MACON2);
etherWriteReg(MACON2, 0);
// enable mac rx, enable pause control for full duplex
etherWriteReg(MACON1, TXPAUS | RXPAUS | MARXEN);
// enable padding to 60 bytes (no runt packets)
// add crc to tx packets, set full or half duplex
if ((mode & ETHER_FULLDUPLEX) != 0)
etherWriteReg(MACON3, FULDPX | FRMLNEN | TXCRCEN | PAD60);
else
etherWriteReg(MACON3, FRMLNEN | TXCRCEN | PAD60);
// leave MACON4 as reset
// set maximum rx packet size
etherWriteReg(MAMXFLL, LOBYTE(1518));
etherWriteReg(MAMXFLH, HIBYTE(1518));
// set back-to-back uint8_ter-packet gap to 9.6us
if ((mode & ETHER_FULLDUPLEX) != 0)
etherWriteReg(MABBIPG, 0x15);
else
etherWriteReg(MABBIPG, 0x12);
// set non-back-to-back uint8_ter-packet gap registers
etherWriteReg(MAIPGL, 0x12);
etherWriteReg(MAIPGH, 0x0C);
// leave collision window MACLCON2 as reset
setPhyMacAddr(macAddress);
// initialize phy duplex
if ((mode & ETHER_FULLDUPLEX) != 0)
etherWritePhy(PHCON1, PDPXMD);
else
etherWritePhy(PHCON1, 0);
// disable phy loopback if in half-duplex mode
etherWritePhy(PHCON2, HDLDIS);
// set LEDA (link status) and LEDB (tx/rx activity)
// stretch LED on to 40ms (default)
etherWritePhy(PHLCON, 0x0472);
// enable reception
etherSetReg(ECON1, RXEN);
}
// Returns TRUE if packet received
uint8_t etherKbhit()
{
return ((etherReadReg(EIR) & PKTIF) != 0);
}
// Returns up to max_size characters in data buffer
// Returns number of bytes copied to buffer
// Contents written are 16-bit size, 16-bit status, payload excl crc
uint16_t etherGetPacket(uint8_t data[], uint16_t max_size)
{
uint16_t i = 0, size, tmp;
// enable read from FIFO buffers
etherReadMemStart();
// get next pckt information
nextPacketLsb = etherReadMem();
nextPacketMsb = etherReadMem();
// calc size
// don't return crc, instead return size + status, so size is correct
size = etherReadMem();
data[i++] = size;
tmp = etherReadMem();
data[i++] = tmp;
size |= (tmp << 8);
// copy status + data
if (size > max_size)
size = max_size;
while (i < size)
data[i++] = etherReadMem();
// end read from FIFO buffers
etherReadMemStop();
// advance read ptr
etherSetBank(ERXRDPTL);
etherWriteReg(ERXRDPTL, nextPacketLsb); // hw ptr
etherWriteReg(ERXRDPTH, nextPacketMsb);
etherWriteReg(ERDPTL, nextPacketLsb); // dma rd ptr
etherWriteReg(ERDPTH, nextPacketMsb);
// decrement packet counter so that PKTIF is maintained correctly
etherSetReg(ECON2, PKTDEC);
return size;
}
// Returns TRUE is rx buffer overflowed after correcting the problem
uint8_t etherIsOverflow()
{
uint8_t err;
err = (etherReadReg(EIR) & RXERIF) != 0;
if (err)
etherClearReg(EIR, RXERIF);
return err;
}
// Writes a packet
int16_t etherPutPacket(uint8_t data[], uint16_t size)
{
uint16_t i;
// clear out any tx errors
if ((etherReadReg(EIR) & TXERIF) != 0)
{
etherClearReg(EIR, TXERIF);
etherSetReg(ECON1, TXRTS);
etherClearReg(ECON1, TXRTS);
}
// set DMA start address
etherSetBank(EWRPTL);
etherWriteReg(EWRPTL, LOBYTE(0x1A0A));
etherWriteReg(EWRPTH, HIBYTE(0x1A0A));
// start FIFO buffer write
etherWriteMemStart();
// write control byte
etherWriteMem(0);
// write data
for (i = 0; i < size; i++)
etherWriteMem(data[i]);
// stop write
etherWriteMemStop();
// request transmit
etherWriteReg(ETXSTL, LOBYTE(0x1A0A));
etherWriteReg(ETXSTH, HIBYTE(0x1A0A));
etherWriteReg(ETXNDL, LOBYTE(0x1A0A+size));
etherWriteReg(ETXNDH, HIBYTE(0x1A0A+size));
etherClearReg(EIR, TXIF);
etherSetReg(ECON1, TXRTS);
// wait for completion
while ((etherReadReg(ECON1) & TXRTS) != 0);
// determine success
return ((etherReadReg(ESTAT) & TXABORT) == 0);
}