@@ -12,6 +12,7 @@ uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag)
1212 if ((protocol == SnMR::TCP) || (protocol == SnMR::UDP) || (protocol == SnMR::IPRAW) || (protocol == SnMR::MACRAW) || (protocol == SnMR::PPPOE))
1313 {
1414 close (s);
15+ SPI.beginTransaction (SPI_ETHERNET_SETTINGS);
1516 W5100.writeSnMR (s, protocol | flag);
1617 if (port != 0 ) {
1718 W5100.writeSnPORT (s, port);
@@ -22,7 +23,7 @@ uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag)
2223 }
2324
2425 W5100.execCmdSn (s, Sock_OPEN);
25-
26+ SPI. endTransaction ();
2627 return 1 ;
2728 }
2829
@@ -32,7 +33,10 @@ uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag)
3233
3334uint8_t socketStatus (SOCKET s)
3435{
35- return W5100.readSnSR (s);
36+ SPI.beginTransaction (SPI_ETHERNET_SETTINGS);
37+ uint8_t status = W5100.readSnSR (s);
38+ SPI.endTransaction ();
39+ return status;
3640}
3741
3842
@@ -41,8 +45,10 @@ uint8_t socketStatus(SOCKET s)
4145 */
4246void close (SOCKET s)
4347{
48+ SPI.beginTransaction (SPI_ETHERNET_SETTINGS);
4449 W5100.execCmdSn (s, Sock_CLOSE);
4550 W5100.writeSnIR (s, 0xFF );
51+ SPI.endTransaction ();
4652}
4753
4854
@@ -52,9 +58,13 @@ void close(SOCKET s)
5258 */
5359uint8_t listen (SOCKET s)
5460{
55- if (W5100.readSnSR (s) != SnSR::INIT)
61+ SPI.beginTransaction (SPI_ETHERNET_SETTINGS);
62+ if (W5100.readSnSR (s) != SnSR::INIT) {
63+ SPI.endTransaction ();
5664 return 0 ;
65+ }
5766 W5100.execCmdSn (s, Sock_LISTEN);
67+ SPI.endTransaction ();
5868 return 1 ;
5969}
6070
@@ -76,9 +86,11 @@ uint8_t connect(SOCKET s, uint8_t * addr, uint16_t port)
7686 return 0 ;
7787
7888 // set destination IP
89+ SPI.beginTransaction (SPI_ETHERNET_SETTINGS);
7990 W5100.writeSnDIPR (s, addr);
8091 W5100.writeSnDPORT (s, port);
8192 W5100.execCmdSn (s, Sock_CONNECT);
93+ SPI.endTransaction ();
8294
8395 return 1 ;
8496}
@@ -91,7 +103,9 @@ uint8_t connect(SOCKET s, uint8_t * addr, uint16_t port)
91103 */
92104void disconnect (SOCKET s)
93105{
106+ SPI.beginTransaction (SPI_ETHERNET_SETTINGS);
94107 W5100.execCmdSn (s, Sock_DISCON);
108+ SPI.endTransaction ();
95109}
96110
97111
@@ -113,17 +127,21 @@ uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len)
113127 // if freebuf is available, start.
114128 do
115129 {
130+ SPI.beginTransaction (SPI_ETHERNET_SETTINGS);
116131 freesize = W5100.getTXFreeSize (s);
117132 status = W5100.readSnSR (s);
133+ SPI.endTransaction ();
118134 if ((status != SnSR::ESTABLISHED) && (status != SnSR::CLOSE_WAIT))
119135 {
120136 ret = 0 ;
121137 break ;
122138 }
139+ yield ();
123140 }
124141 while (freesize < ret);
125142
126143 // copy data
144+ SPI.beginTransaction (SPI_ETHERNET_SETTINGS);
127145 W5100.send_data_processing (s, (uint8_t *)buf, ret);
128146 W5100.execCmdSn (s, Sock_SEND);
129147
@@ -133,12 +151,17 @@ uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len)
133151 /* m2008.01 [bj] : reduce code */
134152 if ( W5100.readSnSR (s) == SnSR::CLOSED )
135153 {
154+ SPI.endTransaction ();
136155 close (s);
137156 return 0 ;
138157 }
158+ SPI.endTransaction ();
159+ yield ();
160+ SPI.beginTransaction (SPI_ETHERNET_SETTINGS);
139161 }
140162 /* +2008.01 bj */
141163 W5100.writeSnIR (s, SnIR::SEND_OK);
164+ SPI.endTransaction ();
142165 return ret;
143166}
144167
@@ -152,6 +175,7 @@ uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len)
152175int16_t recv (SOCKET s, uint8_t *buf, int16_t len)
153176{
154177 // Check how much data is available
178+ SPI.beginTransaction (SPI_ETHERNET_SETTINGS);
155179 int16_t ret = W5100.getRXReceivedSize (s);
156180 if ( ret == 0 )
157181 {
@@ -178,13 +202,17 @@ int16_t recv(SOCKET s, uint8_t *buf, int16_t len)
178202 W5100.recv_data_processing (s, buf, ret);
179203 W5100.execCmdSn (s, Sock_RECV);
180204 }
205+ SPI.endTransaction ();
181206 return ret;
182207}
183208
184209
185210int16_t recvAvailable (SOCKET s)
186211{
187- return W5100.getRXReceivedSize (s);
212+ SPI.beginTransaction (SPI_ETHERNET_SETTINGS);
213+ int16_t ret = W5100.getRXReceivedSize (s);
214+ SPI.endTransaction ();
215+ return ret;
188216}
189217
190218
@@ -195,8 +223,9 @@ int16_t recvAvailable(SOCKET s)
195223 */
196224uint16_t peek (SOCKET s, uint8_t *buf)
197225{
226+ SPI.beginTransaction (SPI_ETHERNET_SETTINGS);
198227 W5100.recv_data_processing (s, buf, 1 , 1 );
199-
228+ SPI. endTransaction ();
200229 return 1 ;
201230}
202231
@@ -225,6 +254,7 @@ uint16_t sendto(SOCKET s, const uint8_t *buf, uint16_t len, uint8_t *addr, uint1
225254 }
226255 else
227256 {
257+ SPI.beginTransaction (SPI_ETHERNET_SETTINGS);
228258 W5100.writeSnDIPR (s, addr);
229259 W5100.writeSnDPORT (s, port);
230260
@@ -239,12 +269,17 @@ uint16_t sendto(SOCKET s, const uint8_t *buf, uint16_t len, uint8_t *addr, uint1
239269 {
240270 /* +2008.01 [bj]: clear interrupt */
241271 W5100.writeSnIR (s, (SnIR::SEND_OK | SnIR::TIMEOUT)); /* clear SEND_OK & TIMEOUT */
272+ SPI.endTransaction ();
242273 return 0 ;
243274 }
275+ SPI.endTransaction ();
276+ yield ();
277+ SPI.beginTransaction (SPI_ETHERNET_SETTINGS);
244278 }
245279
246280 /* +2008.01 bj */
247281 W5100.writeSnIR (s, SnIR::SEND_OK);
282+ SPI.endTransaction ();
248283 }
249284 return ret;
250285}
@@ -264,6 +299,7 @@ uint16_t recvfrom(SOCKET s, uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t
264299
265300 if ( len > 0 )
266301 {
302+ SPI.beginTransaction (SPI_ETHERNET_SETTINGS);
267303 ptr = W5100.readSnRX_RD (s);
268304 switch (W5100.readSnMR (s) & 0x07 )
269305 {
@@ -318,6 +354,7 @@ uint16_t recvfrom(SOCKET s, uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t
318354 break ;
319355 }
320356 W5100.execCmdSn (s, Sock_RECV);
357+ SPI.endTransaction ();
321358 }
322359 return data_len;
323360}
@@ -341,6 +378,7 @@ uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len)
341378 if (ret == 0 )
342379 return 0 ;
343380
381+ SPI.beginTransaction (SPI_ETHERNET_SETTINGS);
344382 W5100.send_data_processing (s, (uint8_t *)buf, ret);
345383 W5100.execCmdSn (s, Sock_SEND);
346384
@@ -350,18 +388,24 @@ uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len)
350388 {
351389 /* in case of igmp, if send fails, then socket closed */
352390 /* if you want change, remove this code. */
391+ SPI.endTransaction ();
353392 close (s);
354393 return 0 ;
355394 }
395+ SPI.endTransaction ();
396+ yield ();
397+ SPI.beginTransaction (SPI_ETHERNET_SETTINGS);
356398 }
357399
358400 W5100.writeSnIR (s, SnIR::SEND_OK);
401+ SPI.endTransaction ();
359402 return ret;
360403}
361404
362405uint16_t bufferData (SOCKET s, uint16_t offset, const uint8_t * buf, uint16_t len)
363406{
364407 uint16_t ret =0 ;
408+ SPI.beginTransaction (SPI_ETHERNET_SETTINGS);
365409 if (len > W5100.getTXFreeSize (s))
366410 {
367411 ret = W5100.getTXFreeSize (s); // check size not to exceed MAX size.
@@ -371,6 +415,7 @@ uint16_t bufferData(SOCKET s, uint16_t offset, const uint8_t* buf, uint16_t len)
371415 ret = len;
372416 }
373417 W5100.send_data_processing_offset (s, offset, buf, ret);
418+ SPI.endTransaction ();
374419 return ret;
375420}
376421
@@ -386,14 +431,17 @@ int startUDP(SOCKET s, uint8_t* addr, uint16_t port)
386431 }
387432 else
388433 {
434+ SPI.beginTransaction (SPI_ETHERNET_SETTINGS);
389435 W5100.writeSnDIPR (s, addr);
390436 W5100.writeSnDPORT (s, port);
437+ SPI.endTransaction ();
391438 return 1 ;
392439 }
393440}
394441
395442int sendUDP (SOCKET s)
396443{
444+ SPI.beginTransaction (SPI_ETHERNET_SETTINGS);
397445 W5100.execCmdSn (s, Sock_SEND);
398446
399447 /* +2008.01 bj */
@@ -403,12 +451,17 @@ int sendUDP(SOCKET s)
403451 {
404452 /* +2008.01 [bj]: clear interrupt */
405453 W5100.writeSnIR (s, (SnIR::SEND_OK|SnIR::TIMEOUT));
454+ SPI.endTransaction ();
406455 return 0 ;
407456 }
457+ SPI.endTransaction ();
458+ yield ();
459+ SPI.beginTransaction (SPI_ETHERNET_SETTINGS);
408460 }
409461
410462 /* +2008.01 bj */
411463 W5100.writeSnIR (s, SnIR::SEND_OK);
464+ SPI.endTransaction ();
412465
413466 /* Sent ok */
414467 return 1 ;
0 commit comments