-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathw5100.c
More file actions
346 lines (270 loc) · 7.27 KB
/
w5100.c
File metadata and controls
346 lines (270 loc) · 7.27 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
#include "w5100.h"
#include <string.h>
static uint8_t read_byte(W51Iface *iface, uint16_t addr) {
uint8_t haddr;
uint8_t laddr;
uint8_t out;
haddr = (uint8_t)(addr >> 8);
laddr = (uint8_t)(addr & 0xFF);
iface->sel();
iface->xchg(W5100_READ_OPCODE);
iface->xchg(haddr);
iface->xchg(laddr);
out = iface->xchg(0x00);
iface->desel();
return out;
}
static uint16_t read_word(W51Iface *iface, uint16_t addr) {
uint16_t h;
uint16_t l;
h = read_byte(iface, addr++);
l = read_byte(iface, addr);
return (h << 8) + l;
}
static void read_block(W51Iface *iface, uint16_t start_addr, uint16_t count,
uint8_t *data) {
for (uint16_t addr = start_addr; addr < (start_addr + count); addr++) {
*data = read_byte(iface, addr);
data++;
}
}
static void write_byte(W51Iface *iface, uint16_t addr, uint8_t data) {
uint8_t haddr;
uint8_t laddr;
haddr = (uint8_t)(addr >> 8);
laddr = (uint8_t)(addr & 0xFF);
iface->sel();
iface->xchg(W5100_WRITE_OPCODE);
iface->xchg(haddr);
iface->xchg(laddr);
iface->xchg(data);
iface->desel();
}
static void write_word(W51Iface *iface, uint16_t addr, uint16_t val) {
uint8_t d;
d = (uint8_t)(val >> 8);
write_byte(iface, addr++, d);
d = (uint8_t)(val & 0x00FF);
write_byte(iface, addr, d);
}
static void write_block(W51Iface *iface, uint16_t start_addr, uint16_t count,
uint8_t *data) {
for (uint16_t addr = start_addr; addr < (start_addr + count); addr++) {
write_byte(iface, addr, *data);
data++;
}
}
/* Read from 8-bit socket register */
static uint8_t read_sreg_byte(Socket *s, uint16_t offset) {
uint16_t addr;
addr = W5100_SKT_BASE(s->_fd) + offset;
return read_byte(s->_if, addr);
}
/* Read from 16-bit socket register */
static uint16_t read_sreg_word(Socket *s, uint16_t offset) {
uint16_t addr;
addr = W5100_SKT_BASE(s->_fd) + offset;
return read_word(s->_if, addr);
}
/* Write to 8-bit socket register */
static void write_sreg_byte(Socket *s, uint16_t offset, uint8_t val) {
uint16_t addr;
addr = W5100_SKT_BASE(s->_fd) + offset;
write_byte(s->_if, addr, val);
}
/* Write to 16-bit socket register */
static void write_sreg_word(Socket *s, uint16_t offset, uint16_t val) {
uint16_t addr;
addr = W5100_SKT_BASE(s->_fd) + offset;
write_word(s->_if, addr, val);
}
static void reset(W51Iface *iface) {
/* Will reset via RESET pin */
if (iface->rst)
iface->rst();
/* Perform soft reset */
write_byte(iface, W5100_MR, W5100_MR_SOFTRST);
}
int w51_init(W51Iface *iface) {
Socket *s;
if (iface->sel == NULL || iface->desel == NULL || iface->xchg == NULL)
return -1;
reset(iface);
/* Close all socktes */
for (int i = 0; i < W5100_NSOCK; i++) {
s = &iface->sockets[i];
s->_fd = i;
s->_if = iface;
}
return 0;
}
int w51_init_iface(W51Iface *iface, IfaceConfig *cfg) {
write_block(iface, W5100_GAR, 4, cfg->gw);
write_block(iface, W5100_SUBR, 4, cfg->sn);
write_block(iface, W5100_SHAR, 6, cfg->mac);
write_block(iface, W5100_SIPR, 4, cfg->ip);
return 0;
}
SockStatus sock_status(Socket *s) { return read_sreg_byte(s, W5100_SR_OFFSET); }
int sock_close(Socket *s) {
write_sreg_byte(s, W5100_CR_OFFSET, W5100_SKT_CR_CLOSE);
if (sock_status(s) == SockStatus_CLOSED)
return 0;
return -1;
}
bool sock_is_connected(Socket *s) {
if (sock_status(s) == SockStatus_ESTABLISHED) {
return true;
}
return false;
}
static Socket *get_free_socket(W51Iface *iface) {
Socket *s = NULL;
for (int i = 0; i < W5100_NSOCK; i++) {
/* Get a socket from pool */
s = &iface->sockets[i];
/* Check status */
switch (sock_status(s)) {
case SockStatus_CLOSEWAIT:
/* Clsoe the socket */
if (sock_close(s) != 0)
return NULL;
/* fallthrough */
case SockStatus_CLOSED:
return s;
default:
break;
}
}
return NULL;
}
int sock_init(W51Iface *iface, Socket *s) {
if (iface == NULL)
return -1;
memset(s, 0, sizeof(Socket));
s->_if = iface;
s->_fd = -1;
return 0;
}
int sock_bind(Socket *s, int port) {
Socket *new;
/* Validate input */
if (s->_if == NULL || port < 0)
return -1;
/* Get new free socket */
if ((new = get_free_socket(s->_if)) == NULL)
return -1;
/* Set socket port */
write_sreg_word(new, W5100_PORT_OFFSET, port);
/* Set protocol to TCP */
write_sreg_byte(new, W5100_MR_OFFSET, (uint8_t)SockProto_TCP);
/* Open the socket */
write_sreg_byte(new, W5100_CR_OFFSET, W5100_SKT_CR_OPEN);
/* Check status, should be INIT */
if (sock_status(new) != SockStatus_INIT)
return -2;
/* Assign binded socket */
s->_sport = port;
s->_fd = new->_fd;
return 0;
}
int sock_listen(Socket *s) {
if (s->_if == NULL || s->_fd == -1 || sock_status(s) != SockStatus_INIT)
return -1;
write_sreg_byte(s, W5100_CR_OFFSET, W5100_SKT_CR_LISTEN);
if (sock_status(s) != SockStatus_LISTEN)
return -1;
return 0;
}
Socket *sock_accept(Socket *p) {
Socket *out;
/* Check the passive socket, rebind if necessary */
if (p->_fd == -1) {
if (sock_bind(p, p->_sport) != 0) {
/* Bind error */
return NULL;
} else {
if (sock_listen(p) != 0) {
/* Listen error */
return NULL;
}
}
}
/* Check new connection */
if (sock_status(p) != SockStatus_ESTABLISHED)
return NULL;
out = &p->_if->sockets[p->_fd];
/* Clear passive socket descriptor to listen on new socket at next call */
p->_fd = -1;
return out;
}
int sock_read(Socket *s, uint8_t *buf, size_t count) {
uint16_t sz;
uint16_t rx_rd;
uint16_t offset;
uint16_t saddr;
uint16_t i;
uint16_t base;
uint16_t mask = 0x7FF;
if (sock_status(s) != SockStatus_ESTABLISHED)
return -1;
base = s->_fd * 0x800 + 0x6000;
sz = read_sreg_word(s, W5100_RX_RSR_OFFSET);
if (sz == 0)
return 0;
if (sz > count)
return -ENOMEM;
rx_rd = read_sreg_word(s, W5100_RX_RD_OFFSET);
offset = rx_rd & mask;
saddr = base + offset;
if ((offset + sz) > 0x800) {
i = 0x800 - offset;
read_block(s->_if, saddr, i, buf);
buf += i;
i = sz - i;
read_block(s->_if, base, i, buf);
} else {
// Normal
read_block(s->_if, saddr, sz, buf);
}
rx_rd += sz;
write_sreg_word(s, W5100_RX_RD_OFFSET, rx_rd);
write_sreg_byte(s, W5100_CR_OFFSET, W5100_SKT_CR_RECV);
return sz;
}
int sock_write(Socket *s, uint8_t *buf, size_t count) {
uint16_t free;
uint16_t tx_wr;
uint16_t offset;
uint16_t saddr;
uint16_t i;
uint16_t base;
uint16_t mask = 0x7FF;
if (sock_status(s) != SockStatus_ESTABLISHED)
return -1;
/* Get free memory size */
free = read_sreg_word(s, W5100_TX_FSR_OFFSET);
/* Return if no free memory available */
if (free == 0)
return -ENOMEM;
/* Decrease count to free memory size if needed */
if (count > free)
count = free;
tx_wr = read_sreg_word(s, W5100_TX_WR_OFFSET);
base = s->_fd * 0x800 + 0x4000;
offset = tx_wr & mask;
saddr = base + offset;
if ((offset + count) > 0x800) {
i = 0x800 - offset;
write_block(s->_if, saddr, i, buf);
buf += i;
i = count - i;
write_block(s->_if, base, i, buf);
} else {
write_block(s->_if, saddr, count, buf);
}
tx_wr += count;
write_sreg_word(s, W5100_TX_WR_OFFSET, tx_wr);
write_sreg_byte(s, W5100_CR_OFFSET, W5100_SKT_CR_SEND);
return count;
}