1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-11-29 10:24:12 +01:00

Backported Ethernet library from 1.5.x

This commit is contained in:
Cristian Maglie 2014-10-17 12:20:30 +02:00
parent 6ecb174c40
commit b9b0fcdadc
22 changed files with 348 additions and 265 deletions

4
libraries/Ethernet/Dhcp.cpp Executable file → Normal file
View File

@ -1,13 +1,13 @@
// DHCP Library v0.3 - April 25, 2009 // DHCP Library v0.3 - April 25, 2009
// Author: Jordan Terrell - blog.jordanterrell.com // Author: Jordan Terrell - blog.jordanterrell.com
#include "w5100.h" #include "utility/w5100.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include "Dhcp.h" #include "Dhcp.h"
#include "Arduino.h" #include "Arduino.h"
#include "util.h" #include "utility/util.h"
int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout)
{ {

0
libraries/Ethernet/Dhcp.h Executable file → Normal file
View File

View File

@ -2,9 +2,9 @@
// (c) Copyright 2009-2010 MCQN Ltd. // (c) Copyright 2009-2010 MCQN Ltd.
// Released under Apache License, version 2.0 // Released under Apache License, version 2.0
#include "w5100.h" #include "utility/w5100.h"
#include "EthernetUdp.h" #include "EthernetUdp.h"
#include "util.h" #include "utility/util.h"
#include "Dns.h" #include "Dns.h"
#include <string.h> #include <string.h>

View File

@ -1,4 +1,4 @@
#include "w5100.h" #include "utility/w5100.h"
#include "Ethernet.h" #include "Ethernet.h"
#include "Dhcp.h" #include "Dhcp.h"
@ -16,8 +16,10 @@ int EthernetClass::begin(uint8_t *mac_address)
// Initialise the basic info // Initialise the basic info
W5100.init(); W5100.init();
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.setMACAddress(mac_address); W5100.setMACAddress(mac_address);
W5100.setIPAddress(IPAddress(0,0,0,0).raw_address()); W5100.setIPAddress(IPAddress(0,0,0,0).raw_address());
SPI.endTransaction();
// Now try to get our config info from a DHCP server // Now try to get our config info from a DHCP server
int ret = _dhcp->beginWithDHCP(mac_address); int ret = _dhcp->beginWithDHCP(mac_address);
@ -25,9 +27,11 @@ int EthernetClass::begin(uint8_t *mac_address)
{ {
// We've successfully found a DHCP server and got our configuration info, so set things // We've successfully found a DHCP server and got our configuration info, so set things
// accordingly // accordingly
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.setIPAddress(_dhcp->getLocalIp().raw_address()); W5100.setIPAddress(_dhcp->getLocalIp().raw_address());
W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address()); W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address());
W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address()); W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address());
SPI.endTransaction();
_dnsServerAddress = _dhcp->getDnsServerIp(); _dnsServerAddress = _dhcp->getDnsServerIp();
} }
@ -61,10 +65,12 @@ void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dn
void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
{ {
W5100.init(); W5100.init();
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.setMACAddress(mac); W5100.setMACAddress(mac);
W5100.setIPAddress(local_ip._address); W5100.setIPAddress(local_ip.raw_address());
W5100.setGatewayIp(gateway._address); W5100.setGatewayIp(gateway.raw_address());
W5100.setSubnetMask(subnet._address); W5100.setSubnetMask(subnet.raw_address());
SPI.endTransaction();
_dnsServerAddress = dns_server; _dnsServerAddress = dns_server;
} }
@ -80,9 +86,11 @@ int EthernetClass::maintain(){
case DHCP_CHECK_RENEW_OK: case DHCP_CHECK_RENEW_OK:
case DHCP_CHECK_REBIND_OK: case DHCP_CHECK_REBIND_OK:
//we might have got a new IP. //we might have got a new IP.
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.setIPAddress(_dhcp->getLocalIp().raw_address()); W5100.setIPAddress(_dhcp->getLocalIp().raw_address());
W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address()); W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address());
W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address()); W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address());
SPI.endTransaction();
_dnsServerAddress = _dhcp->getDnsServerIp(); _dnsServerAddress = _dhcp->getDnsServerIp();
break; break;
default: default:
@ -96,21 +104,27 @@ int EthernetClass::maintain(){
IPAddress EthernetClass::localIP() IPAddress EthernetClass::localIP()
{ {
IPAddress ret; IPAddress ret;
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.getIPAddress(ret.raw_address()); W5100.getIPAddress(ret.raw_address());
SPI.endTransaction();
return ret; return ret;
} }
IPAddress EthernetClass::subnetMask() IPAddress EthernetClass::subnetMask()
{ {
IPAddress ret; IPAddress ret;
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.getSubnetMask(ret.raw_address()); W5100.getSubnetMask(ret.raw_address());
SPI.endTransaction();
return ret; return ret;
} }
IPAddress EthernetClass::gatewayIP() IPAddress EthernetClass::gatewayIP()
{ {
IPAddress ret; IPAddress ret;
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.getGatewayIp(ret.raw_address()); W5100.getGatewayIp(ret.raw_address());
SPI.endTransaction();
return ret; return ret;
} }

View File

@ -1,5 +1,5 @@
#include "w5100.h" #include "utility/w5100.h"
#include "socket.h" #include "utility/socket.h"
extern "C" { extern "C" {
#include "string.h" #include "string.h"
@ -40,7 +40,7 @@ int EthernetClient::connect(IPAddress ip, uint16_t port) {
return 0; return 0;
for (int i = 0; i < MAX_SOCK_NUM; i++) { for (int i = 0; i < MAX_SOCK_NUM; i++) {
uint8_t s = W5100.readSnSR(i); uint8_t s = socketStatus(i);
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) { if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) {
_sock = i; _sock = i;
break; break;
@ -88,7 +88,7 @@ size_t EthernetClient::write(const uint8_t *buf, size_t size) {
int EthernetClient::available() { int EthernetClient::available() {
if (_sock != MAX_SOCK_NUM) if (_sock != MAX_SOCK_NUM)
return W5100.getRXReceivedSize(_sock); return recvAvailable(_sock);
return 0; return 0;
} }
@ -120,8 +120,7 @@ int EthernetClient::peek() {
} }
void EthernetClient::flush() { void EthernetClient::flush() {
while (available()) ::flush(_sock);
read();
} }
void EthernetClient::stop() { void EthernetClient::stop() {
@ -154,7 +153,7 @@ uint8_t EthernetClient::connected() {
uint8_t EthernetClient::status() { uint8_t EthernetClient::status() {
if (_sock == MAX_SOCK_NUM) return SnSR::CLOSED; if (_sock == MAX_SOCK_NUM) return SnSR::CLOSED;
return W5100.readSnSR(_sock); return socketStatus(_sock);
} }
// the next function allows us to use the client returned by // the next function allows us to use the client returned by

View File

@ -1,5 +1,5 @@
#include "w5100.h" #include "utility/w5100.h"
#include "socket.h" #include "utility/socket.h"
extern "C" { extern "C" {
#include "string.h" #include "string.h"
} }

View File

@ -26,8 +26,8 @@
* bjoern@cs.stanford.edu 12/30/2008 * bjoern@cs.stanford.edu 12/30/2008
*/ */
#include "w5100.h" #include "utility/w5100.h"
#include "socket.h" #include "utility/socket.h"
#include "Ethernet.h" #include "Ethernet.h"
#include "Udp.h" #include "Udp.h"
#include "Dns.h" #include "Dns.h"
@ -41,7 +41,7 @@ uint8_t EthernetUDP::begin(uint16_t port) {
return 0; return 0;
for (int i = 0; i < MAX_SOCK_NUM; i++) { for (int i = 0; i < MAX_SOCK_NUM; i++) {
uint8_t s = W5100.readSnSR(i); uint8_t s = socketStatus(i);
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) { if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) {
_sock = i; _sock = i;
break; break;
@ -120,7 +120,7 @@ int EthernetUDP::parsePacket()
// discard any remaining bytes in the last packet // discard any remaining bytes in the last packet
flush(); flush();
if (W5100.getRXReceivedSize(_sock) > 0) if (recvAvailable(_sock) > 0)
{ {
//HACK - hand-parse the UDP packet using TCP recv method //HACK - hand-parse the UDP packet using TCP recv method
uint8_t tmpBuf[8]; uint8_t tmpBuf[8];

View File

@ -1,14 +1,14 @@
/* /*
SCP1000 Barometric Pressure Sensor Display SCP1000 Barometric Pressure Sensor Display
Serves the output of a Barometric Pressure Sensor as a web page. Serves the output of a Barometric Pressure Sensor as a web page.
Uses the SPI library. For details on the sensor, see: Uses the SPI library. For details on the sensor, see:
http://www.sparkfun.com/commerce/product_info.php?products_id=8161 http://www.sparkfun.com/commerce/product_info.php?products_id=8161
http://www.vti.fi/en/support/obsolete_products/pressure_sensors/ http://www.vti.fi/en/support/obsolete_products/pressure_sensors/
This sketch adapted from Nathan Seidle's SCP1000 example for PIC: This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip
Circuit: Circuit:
SCP1000 sensor attached to pins 6,7, and 11 - 13: SCP1000 sensor attached to pins 6,7, and 11 - 13:
DRDY: pin 6 DRDY: pin 6
@ -16,7 +16,7 @@
MOSI: pin 11 MOSI: pin 11
MISO: pin 12 MISO: pin 12
SCK: pin 13 SCK: pin 13
created 31 July 2010 created 31 July 2010
by Tom Igoe by Tom Igoe
*/ */
@ -28,16 +28,17 @@
// assign a MAC address for the ethernet controller. // assign a MAC address for the ethernet controller.
// fill in your address here: // fill in your address here:
byte mac[] = { byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
// assign an IP address for the controller: // assign an IP address for the controller:
IPAddress ip(192,168,1,20); IPAddress ip(192, 168, 1, 20);
IPAddress gateway(192,168,1,1); IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0); IPAddress subnet(255, 255, 255, 0);
// Initialize the Ethernet server library // Initialize the Ethernet server library
// with the IP address and port you want to use // with the IP address and port you want to use
// (port 80 is default for HTTP): // (port 80 is default for HTTP):
EthernetServer server(80); EthernetServer server(80);
@ -49,7 +50,7 @@ const int TEMPERATURE = 0x21; //16 bit temperature reading
// pins used for the connection with the sensor // pins used for the connection with the sensor
// the others you need are controlled by the SPI library): // the others you need are controlled by the SPI library):
const int dataReadyPin = 6; const int dataReadyPin = 6;
const int chipSelectPin = 7; const int chipSelectPin = 7;
float temperature = 0.0; float temperature = 0.0;
@ -83,9 +84,9 @@ void setup() {
} }
void loop() { void loop() {
// check for a reading no more than once a second. // check for a reading no more than once a second.
if (millis() - lastReadingTime > 1000){ if (millis() - lastReadingTime > 1000) {
// if there's a reading ready, read it: // if there's a reading ready, read it:
// don't do anything until the data ready pin is high: // don't do anything until the data ready pin is high:
if (digitalRead(dataReadyPin) == HIGH) { if (digitalRead(dataReadyPin) == HIGH) {
@ -109,13 +110,13 @@ void getData() {
temperature = (float)tempData / 20.0; temperature = (float)tempData / 20.0;
//Read the pressure data highest 3 bits: //Read the pressure data highest 3 bits:
byte pressureDataHigh = readRegister(0x1F, 1); byte pressureDataHigh = readRegister(0x1F, 1);
pressureDataHigh &= 0b00000111; //you only needs bits 2 to 0 pressureDataHigh &= 0b00000111; //you only needs bits 2 to 0
//Read the pressure data lower 16 bits: //Read the pressure data lower 16 bits:
unsigned int pressureDataLow = readRegister(0x20, 2); unsigned int pressureDataLow = readRegister(0x20, 2);
//combine the two parts into one 19-bit number: //combine the two parts into one 19-bit number:
pressure = ((pressureDataHigh << 16) | pressureDataLow)/4; pressure = ((pressureDataHigh << 16) | pressureDataLow) / 4;
Serial.print("Temperature: "); Serial.print("Temperature: ");
Serial.print(temperature); Serial.print(temperature);
@ -149,13 +150,13 @@ void listenForEthernetClients() {
client.println("<br />"); client.println("<br />");
client.print("Pressure: " + String(pressure)); client.print("Pressure: " + String(pressure));
client.print(" Pa"); client.print(" Pa");
client.println("<br />"); client.println("<br />");
break; break;
} }
if (c == '\n') { if (c == '\n') {
// you're starting a new line // you're starting a new line
currentLineIsBlank = true; currentLineIsBlank = true;
} }
else if (c != '\r') { else if (c != '\r') {
// you've gotten a character on the current line // you've gotten a character on the current line
currentLineIsBlank = false; currentLineIsBlank = false;
@ -167,7 +168,7 @@ void listenForEthernetClients() {
// close the connection: // close the connection:
client.stop(); client.stop();
} }
} }
//Send a write command to SCP1000 //Send a write command to SCP1000
@ -179,20 +180,20 @@ void writeRegister(byte registerName, byte registerValue) {
registerName |= 0b00000010; //Write command registerName |= 0b00000010; //Write command
// take the chip select low to select the device: // take the chip select low to select the device:
digitalWrite(chipSelectPin, LOW); digitalWrite(chipSelectPin, LOW);
SPI.transfer(registerName); //Send register location SPI.transfer(registerName); //Send register location
SPI.transfer(registerValue); //Send value to record into register SPI.transfer(registerValue); //Send value to record into register
// take the chip select high to de-select: // take the chip select high to de-select:
digitalWrite(chipSelectPin, HIGH); digitalWrite(chipSelectPin, HIGH);
} }
//Read register from the SCP1000: //Read register from the SCP1000:
unsigned int readRegister(byte registerName, int numBytes) { unsigned int readRegister(byte registerName, int numBytes) {
byte inByte = 0; // incoming from the SPI read byte inByte = 0; // incoming from the SPI read
unsigned int result = 0; // result to return unsigned int result = 0; // result to return
// SCP1000 expects the register name in the upper 6 bits // SCP1000 expects the register name in the upper 6 bits
// of the byte: // of the byte:
@ -201,22 +202,22 @@ unsigned int readRegister(byte registerName, int numBytes) {
registerName &= 0b11111100; //Read command registerName &= 0b11111100; //Read command
// take the chip select low to select the device: // take the chip select low to select the device:
digitalWrite(chipSelectPin, LOW); digitalWrite(chipSelectPin, LOW);
// send the device the register you want to read: // send the device the register you want to read:
int command = SPI.transfer(registerName); int command = SPI.transfer(registerName);
// send a value of 0 to read the first byte returned: // send a value of 0 to read the first byte returned:
inByte = SPI.transfer(0x00); inByte = SPI.transfer(0x00);
result = inByte; result = inByte;
// if there's more than one byte returned, // if there's more than one byte returned,
// shift the first byte then get the second byte: // shift the first byte then get the second byte:
if (numBytes > 1){ if (numBytes > 1) {
result = inByte << 8; result = inByte << 8;
inByte = SPI.transfer(0x00); inByte = SPI.transfer(0x00);
result = result |inByte; result = result | inByte;
} }
// take the chip select high to de-select: // take the chip select high to de-select:
digitalWrite(chipSelectPin, HIGH); digitalWrite(chipSelectPin, HIGH);
// return the result: // return the result:
return(result); return(result);
} }

View File

@ -1,20 +1,20 @@
/* /*
Chat Server Chat Server
A simple server that distributes any incoming messages to all A simple server that distributes any incoming messages to all
connected clients. To use telnet to your device's IP address and type. connected clients. To use telnet to your device's IP address and type.
You can see the client's input in the serial monitor as well. You can see the client's input in the serial monitor as well.
Using an Arduino Wiznet Ethernet shield. Using an Arduino Wiznet Ethernet shield.
Circuit: Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13 * Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional) * Analog inputs attached to pins A0 through A5 (optional)
created 18 Dec 2009 created 18 Dec 2009
by David A. Mellis by David A. Mellis
modified 9 Apr 2012 modified 9 Apr 2012
by Tom Igoe by Tom Igoe
*/ */
#include <SPI.h> #include <SPI.h>
@ -23,10 +23,11 @@
// Enter a MAC address and IP address for your controller below. // Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network. // The IP address will be dependent on your local network.
// gateway and subnet are optional: // gateway and subnet are optional:
byte mac[] = { byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
IPAddress ip(192,168,1, 177); };
IPAddress gateway(192,168,1, 1); IPAddress ip(192, 168, 1, 177);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0); IPAddress subnet(255, 255, 0, 0);
@ -39,9 +40,9 @@ void setup() {
Ethernet.begin(mac, ip, gateway, subnet); Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients // start listening for clients
server.begin(); server.begin();
// Open serial communications and wait for port to open: // Open serial communications and wait for port to open:
Serial.begin(9600); Serial.begin(9600);
while (!Serial) { while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only ; // wait for serial port to connect. Needed for Leonardo only
} }
@ -58,11 +59,11 @@ void loop() {
if (client) { if (client) {
if (!alreadyConnected) { if (!alreadyConnected) {
// clead out the input buffer: // clead out the input buffer:
client.flush(); client.flush();
Serial.println("We have a new client"); Serial.println("We have a new client");
client.println("Hello, client!"); client.println("Hello, client!");
alreadyConnected = true; alreadyConnected = true;
} }
if (client.available() > 0) { if (client.available() > 0) {
// read the bytes incoming from the client: // read the bytes incoming from the client:

View File

@ -1,17 +1,17 @@
/* /*
DHCP-based IP printer DHCP-based IP printer
This sketch uses the DHCP extensions to the Ethernet library This sketch uses the DHCP extensions to the Ethernet library
to get an IP address via DHCP and print the address obtained. to get an IP address via DHCP and print the address obtained.
using an Arduino Wiznet Ethernet shield. using an Arduino Wiznet Ethernet shield.
Circuit: Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13 * Ethernet shield attached to pins 10, 11, 12, 13
created 12 April 2011 created 12 April 2011
modified 9 Apr 2012 modified 9 Apr 2012
by Tom Igoe by Tom Igoe
*/ */
#include <SPI.h> #include <SPI.h>
@ -19,19 +19,20 @@
// Enter a MAC address for your controller below. // Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield // Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 }; 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
};
// Initialize the Ethernet client library // Initialize the Ethernet client library
// with the IP address and port of the server // with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP): // that you want to connect to (port 80 is default for HTTP):
EthernetClient client; EthernetClient client;
void setup() { void setup() {
// Open serial communications and wait for port to open: // Open serial communications and wait for port to open:
Serial.begin(9600); Serial.begin(9600);
// this check is only needed on the Leonardo: // this check is only needed on the Leonardo:
while (!Serial) { while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only ; // wait for serial port to connect. Needed for Leonardo only
} }
@ -39,7 +40,7 @@ void setup() {
if (Ethernet.begin(mac) == 0) { if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP"); Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore: // no point in carrying on, so do nothing forevermore:
for(;;) for (;;)
; ;
} }
// print your local IP address: // print your local IP address:
@ -47,7 +48,7 @@ void setup() {
for (byte thisByte = 0; thisByte < 4; thisByte++) { for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address: // print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC); Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print("."); Serial.print(".");
} }
Serial.println(); Serial.println();
} }

View File

@ -1,21 +1,21 @@
/* /*
DHCP Chat Server DHCP Chat Server
A simple server that distributes any incoming messages to all A simple server that distributes any incoming messages to all
connected clients. To use telnet to your device's IP address and type. connected clients. To use telnet to your device's IP address and type.
You can see the client's input in the serial monitor as well. You can see the client's input in the serial monitor as well.
Using an Arduino Wiznet Ethernet shield. Using an Arduino Wiznet Ethernet shield.
THis version attempts to get an IP address using DHCP THis version attempts to get an IP address using DHCP
Circuit: Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13 * Ethernet shield attached to pins 10, 11, 12, 13
created 21 May 2011 created 21 May 2011
modified 9 Apr 2012 modified 9 Apr 2012
by Tom Igoe by Tom Igoe
Based on ChatServer example by David A. Mellis Based on ChatServer example by David A. Mellis
*/ */
#include <SPI.h> #include <SPI.h>
@ -24,10 +24,11 @@
// Enter a MAC address and IP address for your controller below. // Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network. // The IP address will be dependent on your local network.
// gateway and subnet are optional: // gateway and subnet are optional:
byte mac[] = { byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 }; 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
IPAddress ip(192,168,1, 177); };
IPAddress gateway(192,168,1, 1); IPAddress ip(192, 168, 1, 177);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0); IPAddress subnet(255, 255, 0, 0);
// telnet defaults to port 23 // telnet defaults to port 23
@ -38,7 +39,7 @@ void setup() {
// Open serial communications and wait for port to open: // Open serial communications and wait for port to open:
Serial.begin(9600); Serial.begin(9600);
// this check is only needed on the Leonardo: // this check is only needed on the Leonardo:
while (!Serial) { while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only ; // wait for serial port to connect. Needed for Leonardo only
} }
@ -56,12 +57,12 @@ void setup() {
for (byte thisByte = 0; thisByte < 4; thisByte++) { for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address: // print the value of each byte of the IP address:
Serial.print(ip[thisByte], DEC); Serial.print(ip[thisByte], DEC);
Serial.print("."); Serial.print(".");
} }
Serial.println(); Serial.println();
// start listening for clients // start listening for clients
server.begin(); server.begin();
} }
void loop() { void loop() {
@ -72,7 +73,7 @@ void loop() {
if (client) { if (client) {
if (!gotAMessage) { if (!gotAMessage) {
Serial.println("We have a new client"); Serial.println("We have a new client");
client.println("Hello, client!"); client.println("Hello, client!");
gotAMessage = true; gotAMessage = true;
} }

View File

@ -1,21 +1,21 @@
/* /*
Telnet client Telnet client
This sketch connects to a a telnet server (http://www.google.com) This sketch connects to a a telnet server (http://www.google.com)
using an Arduino Wiznet Ethernet shield. You'll need a telnet server using an Arduino Wiznet Ethernet shield. You'll need a telnet server
to test this with. to test this with.
Processing's ChatServer example (part of the network library) works well, Processing's ChatServer example (part of the network library) works well,
running on port 10002. It can be found as part of the examples running on port 10002. It can be found as part of the examples
in the Processing application, available at in the Processing application, available at
http://processing.org/ http://processing.org/
Circuit: Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13 * Ethernet shield attached to pins 10, 11, 12, 13
created 14 Sep 2010 created 14 Sep 2010
modified 9 Apr 2012 modified 9 Apr 2012
by Tom Igoe by Tom Igoe
*/ */
#include <SPI.h> #include <SPI.h>
@ -23,15 +23,16 @@
// Enter a MAC address and IP address for your controller below. // Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network: // The IP address will be dependent on your local network:
byte mac[] = { byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
IPAddress ip(192,168,1,177); };
IPAddress ip(192, 168, 1, 177);
// Enter the IP address of the server you're connecting to: // Enter the IP address of the server you're connecting to:
IPAddress server(1,1,1,1); IPAddress server(1, 1, 1, 1);
// Initialize the Ethernet client library // Initialize the Ethernet client library
// with the IP address and port of the server // with the IP address and port of the server
// that you want to connect to (port 23 is default for telnet; // that you want to connect to (port 23 is default for telnet;
// if you're using Processing's ChatServer, use port 10002): // if you're using Processing's ChatServer, use port 10002):
EthernetClient client; EthernetClient client;
@ -39,9 +40,9 @@ EthernetClient client;
void setup() { void setup() {
// start the Ethernet connection: // start the Ethernet connection:
Ethernet.begin(mac, ip); Ethernet.begin(mac, ip);
// Open serial communications and wait for port to open: // Open serial communications and wait for port to open:
Serial.begin(9600); Serial.begin(9600);
while (!Serial) { while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only ; // wait for serial port to connect. Needed for Leonardo only
} }
@ -53,7 +54,7 @@ void setup() {
// if you get a connection, report back via serial: // if you get a connection, report back via serial:
if (client.connect(server, 10002)) { if (client.connect(server, 10002)) {
Serial.println("connected"); Serial.println("connected");
} }
else { else {
// if you didn't get a connection to the server: // if you didn't get a connection to the server:
Serial.println("connection failed"); Serial.println("connection failed");
@ -62,7 +63,7 @@ void setup() {
void loop() void loop()
{ {
// if there are incoming bytes available // if there are incoming bytes available
// from the server, read them and print them: // from the server, read them and print them:
if (client.available()) { if (client.available()) {
char c = client.read(); char c = client.read();
@ -74,7 +75,7 @@ void loop()
while (Serial.available() > 0) { while (Serial.available() > 0) {
char inChar = Serial.read(); char inChar = Serial.read();
if (client.connected()) { if (client.connected()) {
client.print(inChar); client.print(inChar);
} }
} }
@ -84,7 +85,7 @@ void loop()
Serial.println("disconnecting."); Serial.println("disconnecting.");
client.stop(); client.stop();
// do nothing: // do nothing:
while(true); while (true);
} }
} }

View File

@ -2,13 +2,13 @@
UDPSendReceive.pde: UDPSendReceive.pde:
This sketch receives UDP message strings, prints them to the serial port This sketch receives UDP message strings, prints them to the serial port
and sends an "acknowledge" string back to the sender and sends an "acknowledge" string back to the sender
A Processing sketch is included at the end of file that can be used to send A Processing sketch is included at the end of file that can be used to send
and received messages for testing with a computer. and received messages for testing with a computer.
created 21 Aug 2010 created 21 Aug 2010
by Michael Margolis by Michael Margolis
This code is in the public domain. This code is in the public domain.
*/ */
@ -20,8 +20,9 @@
// Enter a MAC address and IP address for your controller below. // Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network: // The IP address will be dependent on your local network:
byte mac[] = { byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177); IPAddress ip(192, 168, 1, 177);
unsigned int localPort = 8888; // local port to listen on unsigned int localPort = 8888; // local port to listen on
@ -35,7 +36,7 @@ EthernetUDP Udp;
void setup() { void setup() {
// start the Ethernet and UDP: // start the Ethernet and UDP:
Ethernet.begin(mac,ip); Ethernet.begin(mac, ip);
Udp.begin(localPort); Udp.begin(localPort);
Serial.begin(9600); Serial.begin(9600);
@ -44,13 +45,13 @@ void setup() {
void loop() { void loop() {
// if there's data available, read a packet // if there's data available, read a packet
int packetSize = Udp.parsePacket(); int packetSize = Udp.parsePacket();
if(packetSize) if (packetSize)
{ {
Serial.print("Received packet of size "); Serial.print("Received packet of size ");
Serial.println(packetSize); Serial.println(packetSize);
Serial.print("From "); Serial.print("From ");
IPAddress remote = Udp.remoteIP(); IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
Serial.print(remote[i], DEC); Serial.print(remote[i], DEC);
if (i < 3) if (i < 3)
@ -62,7 +63,7 @@ void loop() {
Serial.println(Udp.remotePort()); Serial.println(Udp.remotePort());
// read the packet into packetBufffer // read the packet into packetBufffer
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE); Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
Serial.println("Contents:"); Serial.println("Contents:");
Serial.println(packetBuffer); Serial.println(packetBuffer);
@ -78,40 +79,40 @@ void loop() {
/* /*
Processing sketch to run with this example Processing sketch to run with this example
===================================================== =====================================================
// Processing UDP example to send and receive string data from Arduino // Processing UDP example to send and receive string data from Arduino
// press any key to send the "Hello Arduino" message // press any key to send the "Hello Arduino" message
import hypermedia.net.*; import hypermedia.net.*;
UDP udp; // define the UDP object UDP udp; // define the UDP object
void setup() { void setup() {
udp = new UDP( this, 6000 ); // create a new datagram connection on port 6000 udp = new UDP( this, 6000 ); // create a new datagram connection on port 6000
//udp.log( true ); // <-- printout the connection activity //udp.log( true ); // <-- printout the connection activity
udp.listen( true ); // and wait for incoming message udp.listen( true ); // and wait for incoming message
} }
void draw() void draw()
{ {
} }
void keyPressed() { void keyPressed() {
String ip = "192.168.1.177"; // the remote IP address String ip = "192.168.1.177"; // the remote IP address
int port = 8888; // the destination port int port = 8888; // the destination port
udp.send("Hello World", ip, port ); // the message to send udp.send("Hello World", ip, port ); // the message to send
} }
void receive( byte[] data ) { // <-- default handler void receive( byte[] data ) { // <-- default handler
//void receive( byte[] data, String ip, int port ) { // <-- extended handler //void receive( byte[] data, String ip, int port ) { // <-- extended handler
for(int i=0; i < data.length; i++) for(int i=0; i < data.length; i++)
print(char(data[i])); print(char(data[i]));
println(); println();
} }
*/ */

View File

@ -1,55 +1,47 @@
/* /*
Udp NTP Client Udp NTP Client
Get the time from a Network Time Protocol (NTP) time server Get the time from a Network Time Protocol (NTP) time server
Demonstrates use of UDP sendPacket and ReceivePacket Demonstrates use of UDP sendPacket and ReceivePacket
For more on NTP time servers and the messages needed to communicate with them, For more on NTP time servers and the messages needed to communicate with them,
see http://en.wikipedia.org/wiki/Network_Time_Protocol see http://en.wikipedia.org/wiki/Network_Time_Protocol
Warning: NTP Servers are subject to temporary failure or IP address change.
Plese check
http://tf.nist.gov/tf-cgi/servers.cgi created 4 Sep 2010
if the time server used in the example didn't work.
created 4 Sep 2010
by Michael Margolis by Michael Margolis
modified 9 Apr 2012 modified 9 Apr 2012
by Tom Igoe by Tom Igoe
This code is in the public domain. This code is in the public domain.
*/ */
#include <SPI.h> #include <SPI.h>
#include <Ethernet.h> #include <Ethernet.h>
#include <EthernetUdp.h> #include <EthernetUdp.h>
// Enter a MAC address for your controller below. // Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield // Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
unsigned int localPort = 8888; // local port to listen for UDP packets unsigned int localPort = 8888; // local port to listen for UDP packets
IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov NTP server char timeServer[] = "time.nist.gov"; // time.nist.gov NTP server
// IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov NTP server
// IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov NTP server
const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
// A UDP instance to let us send and receive packets over UDP // A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp; EthernetUDP Udp;
void setup() void setup()
{ {
// Open serial communications and wait for port to open: // Open serial communications and wait for port to open:
Serial.begin(9600); Serial.begin(9600);
while (!Serial) { while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only ; // wait for serial port to connect. Needed for Leonardo only
} }
@ -58,7 +50,7 @@ void setup()
if (Ethernet.begin(mac) == 0) { if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP"); Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore: // no point in carrying on, so do nothing forevermore:
for(;;) for (;;)
; ;
} }
Udp.begin(localPort); Udp.begin(localPort);
@ -68,58 +60,58 @@ void loop()
{ {
sendNTPpacket(timeServer); // send an NTP packet to a time server sendNTPpacket(timeServer); // send an NTP packet to a time server
// wait to see if a reply is available // wait to see if a reply is available
delay(1000); delay(1000);
if ( Udp.parsePacket() ) { if ( Udp.parsePacket() ) {
// We've received a packet, read the data from it // We've received a packet, read the data from it
Udp.read(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
//the timestamp starts at byte 40 of the received packet and is four bytes, //the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, esxtract the two words: // or two words, long. First, esxtract the two words:
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer // combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900): // this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord; unsigned long secsSince1900 = highWord << 16 | lowWord;
Serial.print("Seconds since Jan 1 1900 = " ); Serial.print("Seconds since Jan 1 1900 = " );
Serial.println(secsSince1900); Serial.println(secsSince1900);
// now convert NTP time into everyday time: // now convert NTP time into everyday time:
Serial.print("Unix time = "); Serial.print("Unix time = ");
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800: // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL; const unsigned long seventyYears = 2208988800UL;
// subtract seventy years: // subtract seventy years:
unsigned long epoch = secsSince1900 - seventyYears; unsigned long epoch = secsSince1900 - seventyYears;
// print Unix time: // print Unix time:
Serial.println(epoch); Serial.println(epoch);
// print the hour, minute and second: // print the hour, minute and second:
Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT) Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day) Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
Serial.print(':'); Serial.print(':');
if ( ((epoch % 3600) / 60) < 10 ) { if ( ((epoch % 3600) / 60) < 10 ) {
// In the first 10 minutes of each hour, we'll want a leading '0' // In the first 10 minutes of each hour, we'll want a leading '0'
Serial.print('0'); Serial.print('0');
} }
Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute) Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
Serial.print(':'); Serial.print(':');
if ( (epoch % 60) < 10 ) { if ( (epoch % 60) < 10 ) {
// In the first 10 seconds of each minute, we'll want a leading '0' // In the first 10 seconds of each minute, we'll want a leading '0'
Serial.print('0'); Serial.print('0');
} }
Serial.println(epoch %60); // print the second Serial.println(epoch % 60); // print the second
} }
// wait ten seconds before asking for the time again // wait ten seconds before asking for the time again
delay(10000); delay(10000);
} }
// send an NTP request to the time server at the given address // send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress& address) unsigned long sendNTPpacket(char* address)
{ {
// set all bytes in the buffer to 0 // set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE); memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request // Initialize values needed to form NTP request
// (see URL above for details on the packets) // (see URL above for details on the packets)
packetBuffer[0] = 0b11100011; // LI, Version, Mode packetBuffer[0] = 0b11100011; // LI, Version, Mode
@ -127,16 +119,16 @@ unsigned long sendNTPpacket(IPAddress& address)
packetBuffer[2] = 6; // Polling Interval packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion // 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49; packetBuffer[12] = 49;
packetBuffer[13] = 0x4E; packetBuffer[13] = 0x4E;
packetBuffer[14] = 49; packetBuffer[14] = 49;
packetBuffer[15] = 52; packetBuffer[15] = 52;
// all NTP fields have been given values, now // all NTP fields have been given values, now
// you can send a packet requesting a timestamp: // you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123 Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer,NTP_PACKET_SIZE); Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket(); Udp.endPacket();
} }

View File

@ -1,17 +1,17 @@
/* /*
Web client Web client
This sketch connects to a website (http://www.google.com) This sketch connects to a website (http://www.google.com)
using an Arduino Wiznet Ethernet shield. using an Arduino Wiznet Ethernet shield.
Circuit: Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13 * Ethernet shield attached to pins 10, 11, 12, 13
created 18 Dec 2009 created 18 Dec 2009
by David A. Mellis by David A. Mellis
modified 9 Apr 2012 modified 9 Apr 2012
by Tom Igoe, based on work by Adrian McEwen by Tom Igoe, based on work by Adrian McEwen
*/ */
#include <SPI.h> #include <SPI.h>
@ -26,17 +26,17 @@ byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "www.google.com"; // name address for Google (using DNS) char server[] = "www.google.com"; // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign // Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,0,177); IPAddress ip(192, 168, 0, 177);
// Initialize the Ethernet client library // Initialize the Ethernet client library
// with the IP address and port of the server // with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP): // that you want to connect to (port 80 is default for HTTP):
EthernetClient client; EthernetClient client;
void setup() { void setup() {
// Open serial communications and wait for port to open: // Open serial communications and wait for port to open:
Serial.begin(9600); Serial.begin(9600);
while (!Serial) { while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only ; // wait for serial port to connect. Needed for Leonardo only
} }
@ -59,7 +59,7 @@ void setup() {
client.println("Host: www.google.com"); client.println("Host: www.google.com");
client.println("Connection: close"); client.println("Connection: close");
client.println(); client.println();
} }
else { else {
// kf you didn't get a connection to the server: // kf you didn't get a connection to the server:
Serial.println("connection failed"); Serial.println("connection failed");
@ -68,7 +68,7 @@ void setup() {
void loop() void loop()
{ {
// if there are incoming bytes available // if there are incoming bytes available
// from the server, read them and print them: // from the server, read them and print them:
if (client.available()) { if (client.available()) {
char c = client.read(); char c = client.read();
@ -82,7 +82,7 @@ void loop()
client.stop(); client.stop();
// do nothing forevermore: // do nothing forevermore:
while(true); while (true);
} }
} }

View File

@ -1,23 +1,25 @@
/* /*
Repeating Web client Repeating Web client
This sketch connects to a a web server and makes a request This sketch connects to a a web server and makes a request
using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
the Adafruit Ethernet shield, either one will work, as long as it's got the Adafruit Ethernet shield, either one will work, as long as it's got
a Wiznet Ethernet module on board. a Wiznet Ethernet module on board.
This example uses DNS, by assigning the Ethernet client with a MAC address, This example uses DNS, by assigning the Ethernet client with a MAC address,
IP address, and DNS address. IP address, and DNS address.
Circuit: Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13 * Ethernet shield attached to pins 10, 11, 12, 13
created 19 Apr 2012 created 19 Apr 2012
by Tom Igoe by Tom Igoe
modified 21 Jan 2014
by Federico Vanzati
http://arduino.cc/en/Tutorial/WebClientRepeating http://arduino.cc/en/Tutorial/WebClientRepeating
This code is in the public domain. This code is in the public domain.
*/ */
#include <SPI.h> #include <SPI.h>
@ -25,27 +27,33 @@
// assign a MAC address for the ethernet controller. // assign a MAC address for the ethernet controller.
// fill in your address here: // fill in your address here:
byte mac[] = { byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
// fill in an available IP address on your network here, // fill in an available IP address on your network here,
// for manual configuration: // for manual configuration:
IPAddress ip(10,0,0,20); IPAddress ip(192, 168, 1, 177);
// fill in your Domain Name Server address here: // fill in your Domain Name Server address here:
IPAddress myDns(1,1,1,1); IPAddress myDns(1, 1, 1, 1);
// initialize the library instance: // initialize the library instance:
EthernetClient client; EthernetClient client;
char server[] = "www.arduino.cc"; char server[] = "www.arduino.cc";
//IPAddress server(64,131,82,241);
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
const unsigned long postingInterval = 60*1000; // delay between updates, in milliseconds // the "L" is needed to use long type numbers
void setup() { void setup() {
// start serial port: // start serial port:
Serial.begin(9600); Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// give the ethernet module time to boot up: // give the ethernet module time to boot up:
delay(1000); delay(1000);
// start the Ethernet connection using a fixed IP address and DNS server: // start the Ethernet connection using a fixed IP address and DNS server:
@ -61,29 +69,23 @@ void loop() {
// purposes only: // purposes only:
if (client.available()) { if (client.available()) {
char c = client.read(); char c = client.read();
Serial.print(c); Serial.write(c);
} }
// if there's no net connection, but there was one last time // if ten seconds have passed since your last connection,
// through the loop, then stop the client: // then connect again and send data:
if (!client.connected() && lastConnected) { if (millis() - lastConnectionTime > postingInterval) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
httpRequest(); httpRequest();
} }
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
} }
// this method makes a HTTP connection to the server: // this method makes a HTTP connection to the server:
void httpRequest() { void httpRequest() {
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
// if there's a successful connection: // if there's a successful connection:
if (client.connect(server, 80)) { if (client.connect(server, 80)) {
Serial.println("connecting..."); Serial.println("connecting...");
@ -96,15 +98,11 @@ void httpRequest() {
// note the time that the connection was made: // note the time that the connection was made:
lastConnectionTime = millis(); lastConnectionTime = millis();
} }
else { else {
// if you couldn't make a connection: // if you couldn't make a connection:
Serial.println("connection failed"); Serial.println("connection failed");
Serial.println("disconnecting.");
client.stop();
} }
} }

View File

@ -1,18 +1,18 @@
/* /*
Web Server Web Server
A simple web server that shows the value of the analog input pins. A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield. using an Arduino Wiznet Ethernet shield.
Circuit: Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13 * Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional) * Analog inputs attached to pins A0 through A5 (optional)
created 18 Dec 2009 created 18 Dec 2009
by David A. Mellis by David A. Mellis
modified 9 Apr 2012 modified 9 Apr 2012
by Tom Igoe by Tom Igoe
*/ */
#include <SPI.h> #include <SPI.h>
@ -20,19 +20,20 @@
// Enter a MAC address and IP address for your controller below. // Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network: // The IP address will be dependent on your local network:
byte mac[] = { byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
IPAddress ip(192,168,1,177); };
IPAddress ip(192, 168, 1, 177);
// Initialize the Ethernet server library // Initialize the Ethernet server library
// with the IP address and port you want to use // with the IP address and port you want to use
// (port 80 is default for HTTP): // (port 80 is default for HTTP):
EthernetServer server(80); EthernetServer server(80);
void setup() { void setup() {
// Open serial communications and wait for port to open: // Open serial communications and wait for port to open:
Serial.begin(9600); Serial.begin(9600);
while (!Serial) { while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only ; // wait for serial port to connect. Needed for Leonardo only
} }
@ -64,7 +65,7 @@ void loop() {
client.println("HTTP/1.1 200 OK"); client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html"); client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println(); client.println();
client.println("<!DOCTYPE HTML>"); client.println("<!DOCTYPE HTML>");
client.println("<html>"); client.println("<html>");
@ -75,7 +76,7 @@ void loop() {
client.print(analogChannel); client.print(analogChannel);
client.print(" is "); client.print(" is ");
client.print(sensorReading); client.print(sensorReading);
client.println("<br />"); client.println("<br />");
} }
client.println("</html>"); client.println("</html>");
break; break;
@ -83,7 +84,7 @@ void loop() {
if (c == '\n') { if (c == '\n') {
// you're starting a new line // you're starting a new line
currentLineIsBlank = true; currentLineIsBlank = true;
} }
else if (c != '\r') { else if (c != '\r') {
// you've gotten a character on the current line // you've gotten a character on the current line
currentLineIsBlank = false; currentLineIsBlank = false;

View File

@ -1,5 +1,5 @@
#include "w5100.h" #include "utility/w5100.h"
#include "socket.h" #include "utility/socket.h"
static uint16_t local_port; static uint16_t local_port;
@ -12,6 +12,7 @@ uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag)
if ((protocol == SnMR::TCP) || (protocol == SnMR::UDP) || (protocol == SnMR::IPRAW) || (protocol == SnMR::MACRAW) || (protocol == SnMR::PPPOE)) if ((protocol == SnMR::TCP) || (protocol == SnMR::UDP) || (protocol == SnMR::IPRAW) || (protocol == SnMR::MACRAW) || (protocol == SnMR::PPPOE))
{ {
close(s); close(s);
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.writeSnMR(s, protocol | flag); W5100.writeSnMR(s, protocol | flag);
if (port != 0) { if (port != 0) {
W5100.writeSnPORT(s, port); W5100.writeSnPORT(s, port);
@ -22,7 +23,7 @@ uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag)
} }
W5100.execCmdSn(s, Sock_OPEN); W5100.execCmdSn(s, Sock_OPEN);
SPI.endTransaction();
return 1; return 1;
} }
@ -30,13 +31,24 @@ uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag)
} }
uint8_t socketStatus(SOCKET s)
{
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
uint8_t status = W5100.readSnSR(s);
SPI.endTransaction();
return status;
}
/** /**
* @brief This function close the socket and parameter is "s" which represent the socket number * @brief This function close the socket and parameter is "s" which represent the socket number
*/ */
void close(SOCKET s) void close(SOCKET s)
{ {
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.execCmdSn(s, Sock_CLOSE); W5100.execCmdSn(s, Sock_CLOSE);
W5100.writeSnIR(s, 0xFF); W5100.writeSnIR(s, 0xFF);
SPI.endTransaction();
} }
@ -46,9 +58,13 @@ void close(SOCKET s)
*/ */
uint8_t listen(SOCKET s) uint8_t listen(SOCKET s)
{ {
if (W5100.readSnSR(s) != SnSR::INIT) SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
if (W5100.readSnSR(s) != SnSR::INIT) {
SPI.endTransaction();
return 0; return 0;
}
W5100.execCmdSn(s, Sock_LISTEN); W5100.execCmdSn(s, Sock_LISTEN);
SPI.endTransaction();
return 1; return 1;
} }
@ -70,9 +86,11 @@ uint8_t connect(SOCKET s, uint8_t * addr, uint16_t port)
return 0; return 0;
// set destination IP // set destination IP
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.writeSnDIPR(s, addr); W5100.writeSnDIPR(s, addr);
W5100.writeSnDPORT(s, port); W5100.writeSnDPORT(s, port);
W5100.execCmdSn(s, Sock_CONNECT); W5100.execCmdSn(s, Sock_CONNECT);
SPI.endTransaction();
return 1; return 1;
} }
@ -85,7 +103,9 @@ uint8_t connect(SOCKET s, uint8_t * addr, uint16_t port)
*/ */
void disconnect(SOCKET s) void disconnect(SOCKET s)
{ {
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.execCmdSn(s, Sock_DISCON); W5100.execCmdSn(s, Sock_DISCON);
SPI.endTransaction();
} }
@ -107,17 +127,21 @@ uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len)
// if freebuf is available, start. // if freebuf is available, start.
do do
{ {
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
freesize = W5100.getTXFreeSize(s); freesize = W5100.getTXFreeSize(s);
status = W5100.readSnSR(s); status = W5100.readSnSR(s);
SPI.endTransaction();
if ((status != SnSR::ESTABLISHED) && (status != SnSR::CLOSE_WAIT)) if ((status != SnSR::ESTABLISHED) && (status != SnSR::CLOSE_WAIT))
{ {
ret = 0; ret = 0;
break; break;
} }
yield();
} }
while (freesize < ret); while (freesize < ret);
// copy data // copy data
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.send_data_processing(s, (uint8_t *)buf, ret); W5100.send_data_processing(s, (uint8_t *)buf, ret);
W5100.execCmdSn(s, Sock_SEND); W5100.execCmdSn(s, Sock_SEND);
@ -127,12 +151,17 @@ uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len)
/* m2008.01 [bj] : reduce code */ /* m2008.01 [bj] : reduce code */
if ( W5100.readSnSR(s) == SnSR::CLOSED ) if ( W5100.readSnSR(s) == SnSR::CLOSED )
{ {
SPI.endTransaction();
close(s); close(s);
return 0; return 0;
} }
SPI.endTransaction();
yield();
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
} }
/* +2008.01 bj */ /* +2008.01 bj */
W5100.writeSnIR(s, SnIR::SEND_OK); W5100.writeSnIR(s, SnIR::SEND_OK);
SPI.endTransaction();
return ret; return ret;
} }
@ -146,6 +175,7 @@ uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len)
int16_t recv(SOCKET s, uint8_t *buf, int16_t len) int16_t recv(SOCKET s, uint8_t *buf, int16_t len)
{ {
// Check how much data is available // Check how much data is available
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
int16_t ret = W5100.getRXReceivedSize(s); int16_t ret = W5100.getRXReceivedSize(s);
if ( ret == 0 ) if ( ret == 0 )
{ {
@ -172,6 +202,16 @@ int16_t recv(SOCKET s, uint8_t *buf, int16_t len)
W5100.recv_data_processing(s, buf, ret); W5100.recv_data_processing(s, buf, ret);
W5100.execCmdSn(s, Sock_RECV); W5100.execCmdSn(s, Sock_RECV);
} }
SPI.endTransaction();
return ret;
}
int16_t recvAvailable(SOCKET s)
{
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
int16_t ret = W5100.getRXReceivedSize(s);
SPI.endTransaction();
return ret; return ret;
} }
@ -183,8 +223,9 @@ int16_t recv(SOCKET s, uint8_t *buf, int16_t len)
*/ */
uint16_t peek(SOCKET s, uint8_t *buf) uint16_t peek(SOCKET s, uint8_t *buf)
{ {
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.recv_data_processing(s, buf, 1, 1); W5100.recv_data_processing(s, buf, 1, 1);
SPI.endTransaction();
return 1; return 1;
} }
@ -213,6 +254,7 @@ uint16_t sendto(SOCKET s, const uint8_t *buf, uint16_t len, uint8_t *addr, uint1
} }
else else
{ {
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.writeSnDIPR(s, addr); W5100.writeSnDIPR(s, addr);
W5100.writeSnDPORT(s, port); W5100.writeSnDPORT(s, port);
@ -227,12 +269,17 @@ uint16_t sendto(SOCKET s, const uint8_t *buf, uint16_t len, uint8_t *addr, uint1
{ {
/* +2008.01 [bj]: clear interrupt */ /* +2008.01 [bj]: clear interrupt */
W5100.writeSnIR(s, (SnIR::SEND_OK | SnIR::TIMEOUT)); /* clear SEND_OK & TIMEOUT */ W5100.writeSnIR(s, (SnIR::SEND_OK | SnIR::TIMEOUT)); /* clear SEND_OK & TIMEOUT */
SPI.endTransaction();
return 0; return 0;
} }
SPI.endTransaction();
yield();
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
} }
/* +2008.01 bj */ /* +2008.01 bj */
W5100.writeSnIR(s, SnIR::SEND_OK); W5100.writeSnIR(s, SnIR::SEND_OK);
SPI.endTransaction();
} }
return ret; return ret;
} }
@ -252,11 +299,12 @@ uint16_t recvfrom(SOCKET s, uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t
if ( len > 0 ) if ( len > 0 )
{ {
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
ptr = W5100.readSnRX_RD(s); ptr = W5100.readSnRX_RD(s);
switch (W5100.readSnMR(s) & 0x07) switch (W5100.readSnMR(s) & 0x07)
{ {
case SnMR::UDP : case SnMR::UDP :
W5100.read_data(s, (uint8_t *)ptr, head, 0x08); W5100.read_data(s, ptr, head, 0x08);
ptr += 8; ptr += 8;
// read peer's IP address, port number. // read peer's IP address, port number.
addr[0] = head[0]; addr[0] = head[0];
@ -268,14 +316,14 @@ uint16_t recvfrom(SOCKET s, uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t
data_len = head[6]; data_len = head[6];
data_len = (data_len << 8) + head[7]; data_len = (data_len << 8) + head[7];
W5100.read_data(s, (uint8_t *)ptr, buf, data_len); // data copy. W5100.read_data(s, ptr, buf, data_len); // data copy.
ptr += data_len; ptr += data_len;
W5100.writeSnRX_RD(s, ptr); W5100.writeSnRX_RD(s, ptr);
break; break;
case SnMR::IPRAW : case SnMR::IPRAW :
W5100.read_data(s, (uint8_t *)ptr, head, 0x06); W5100.read_data(s, ptr, head, 0x06);
ptr += 6; ptr += 6;
addr[0] = head[0]; addr[0] = head[0];
@ -285,19 +333,19 @@ uint16_t recvfrom(SOCKET s, uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t
data_len = head[4]; data_len = head[4];
data_len = (data_len << 8) + head[5]; data_len = (data_len << 8) + head[5];
W5100.read_data(s, (uint8_t *)ptr, buf, data_len); // data copy. W5100.read_data(s, ptr, buf, data_len); // data copy.
ptr += data_len; ptr += data_len;
W5100.writeSnRX_RD(s, ptr); W5100.writeSnRX_RD(s, ptr);
break; break;
case SnMR::MACRAW: case SnMR::MACRAW:
W5100.read_data(s,(uint8_t*)ptr,head,2); W5100.read_data(s, ptr, head, 2);
ptr+=2; ptr+=2;
data_len = head[0]; data_len = head[0];
data_len = (data_len<<8) + head[1] - 2; data_len = (data_len<<8) + head[1] - 2;
W5100.read_data(s,(uint8_t*) ptr,buf,data_len); W5100.read_data(s, ptr, buf, data_len);
ptr += data_len; ptr += data_len;
W5100.writeSnRX_RD(s, ptr); W5100.writeSnRX_RD(s, ptr);
break; break;
@ -306,14 +354,20 @@ uint16_t recvfrom(SOCKET s, uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t
break; break;
} }
W5100.execCmdSn(s, Sock_RECV); W5100.execCmdSn(s, Sock_RECV);
SPI.endTransaction();
} }
return data_len; return data_len;
} }
/**
* @brief Wait for buffered transmission to complete.
*/
void flush(SOCKET s) {
// TODO
}
uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len) uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len)
{ {
uint8_t status=0;
uint16_t ret=0; uint16_t ret=0;
if (len > W5100.SSIZE) if (len > W5100.SSIZE)
@ -324,28 +378,34 @@ uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len)
if (ret == 0) if (ret == 0)
return 0; return 0;
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.send_data_processing(s, (uint8_t *)buf, ret); W5100.send_data_processing(s, (uint8_t *)buf, ret);
W5100.execCmdSn(s, Sock_SEND); W5100.execCmdSn(s, Sock_SEND);
while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK ) while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK )
{ {
status = W5100.readSnSR(s);
if (W5100.readSnIR(s) & SnIR::TIMEOUT) if (W5100.readSnIR(s) & SnIR::TIMEOUT)
{ {
/* in case of igmp, if send fails, then socket closed */ /* in case of igmp, if send fails, then socket closed */
/* if you want change, remove this code. */ /* if you want change, remove this code. */
SPI.endTransaction();
close(s); close(s);
return 0; return 0;
} }
SPI.endTransaction();
yield();
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
} }
W5100.writeSnIR(s, SnIR::SEND_OK); W5100.writeSnIR(s, SnIR::SEND_OK);
SPI.endTransaction();
return ret; return ret;
} }
uint16_t bufferData(SOCKET s, uint16_t offset, const uint8_t* buf, uint16_t len) uint16_t bufferData(SOCKET s, uint16_t offset, const uint8_t* buf, uint16_t len)
{ {
uint16_t ret =0; uint16_t ret =0;
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
if (len > W5100.getTXFreeSize(s)) if (len > W5100.getTXFreeSize(s))
{ {
ret = W5100.getTXFreeSize(s); // check size not to exceed MAX size. ret = W5100.getTXFreeSize(s); // check size not to exceed MAX size.
@ -355,6 +415,7 @@ uint16_t bufferData(SOCKET s, uint16_t offset, const uint8_t* buf, uint16_t len)
ret = len; ret = len;
} }
W5100.send_data_processing_offset(s, offset, buf, ret); W5100.send_data_processing_offset(s, offset, buf, ret);
SPI.endTransaction();
return ret; return ret;
} }
@ -370,14 +431,17 @@ int startUDP(SOCKET s, uint8_t* addr, uint16_t port)
} }
else else
{ {
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.writeSnDIPR(s, addr); W5100.writeSnDIPR(s, addr);
W5100.writeSnDPORT(s, port); W5100.writeSnDPORT(s, port);
SPI.endTransaction();
return 1; return 1;
} }
} }
int sendUDP(SOCKET s) int sendUDP(SOCKET s)
{ {
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.execCmdSn(s, Sock_SEND); W5100.execCmdSn(s, Sock_SEND);
/* +2008.01 bj */ /* +2008.01 bj */
@ -387,12 +451,17 @@ int sendUDP(SOCKET s)
{ {
/* +2008.01 [bj]: clear interrupt */ /* +2008.01 [bj]: clear interrupt */
W5100.writeSnIR(s, (SnIR::SEND_OK|SnIR::TIMEOUT)); W5100.writeSnIR(s, (SnIR::SEND_OK|SnIR::TIMEOUT));
SPI.endTransaction();
return 0; return 0;
} }
SPI.endTransaction();
yield();
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
} }
/* +2008.01 bj */ /* +2008.01 bj */
W5100.writeSnIR(s, SnIR::SEND_OK); W5100.writeSnIR(s, SnIR::SEND_OK);
SPI.endTransaction();
/* Sent ok */ /* Sent ok */
return 1; return 1;

5
libraries/Ethernet/utility/socket.h Executable file → Normal file
View File

@ -1,18 +1,21 @@
#ifndef _SOCKET_H_ #ifndef _SOCKET_H_
#define _SOCKET_H_ #define _SOCKET_H_
#include "w5100.h" #include "utility/w5100.h"
extern uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag); // Opens a socket(TCP or UDP or IP_RAW mode) extern uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag); // Opens a socket(TCP or UDP or IP_RAW mode)
extern uint8_t socketStatus(SOCKET s);
extern void close(SOCKET s); // Close socket extern void close(SOCKET s); // Close socket
extern uint8_t connect(SOCKET s, uint8_t * addr, uint16_t port); // Establish TCP connection (Active connection) extern uint8_t connect(SOCKET s, uint8_t * addr, uint16_t port); // Establish TCP connection (Active connection)
extern void disconnect(SOCKET s); // disconnect the connection extern void disconnect(SOCKET s); // disconnect the connection
extern uint8_t listen(SOCKET s); // Establish TCP connection (Passive connection) extern uint8_t listen(SOCKET s); // Establish TCP connection (Passive connection)
extern uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len); // Send data (TCP) extern uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len); // Send data (TCP)
extern int16_t recv(SOCKET s, uint8_t * buf, int16_t len); // Receive data (TCP) extern int16_t recv(SOCKET s, uint8_t * buf, int16_t len); // Receive data (TCP)
extern int16_t recvAvailable(SOCKET s);
extern uint16_t peek(SOCKET s, uint8_t *buf); extern uint16_t peek(SOCKET s, uint8_t *buf);
extern uint16_t sendto(SOCKET s, const uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t port); // Send data (UDP/IP RAW) extern uint16_t sendto(SOCKET s, const uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t port); // Send data (UDP/IP RAW)
extern uint16_t recvfrom(SOCKET s, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t *port); // Receive data (UDP/IP RAW) extern uint16_t recvfrom(SOCKET s, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t *port); // Receive data (UDP/IP RAW)
extern void flush(SOCKET s); // Wait for transmission to complete
extern uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len); extern uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len);

View File

@ -1,7 +1,8 @@
#ifndef UTIL_H #ifndef UTIL_H
#define UTIL_H #define UTIL_H
#define htons(x) ( ((x)<<8) | (((x)>>8)&0xFF) ) #define htons(x) ( ((x)<< 8 & 0xFF00) | \
((x)>> 8 & 0x00FF) )
#define ntohs(x) htons(x) #define ntohs(x) htons(x)
#define htonl(x) ( ((x)<<24 & 0xFF000000UL) | \ #define htonl(x) ( ((x)<<24 & 0xFF000000UL) | \

View File

@ -9,9 +9,8 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <avr/interrupt.h>
#include "w5100.h" #include "utility/w5100.h"
// W5100 controller instance // W5100 controller instance
W5100Class W5100; W5100Class W5100;
@ -29,10 +28,11 @@ void W5100Class::init(void)
SPI.begin(); SPI.begin();
initSS(); initSS();
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
writeMR(1<<RST); writeMR(1<<RST);
writeTMSR(0x55); writeTMSR(0x55);
writeRMSR(0x55); writeRMSR(0x55);
SPI.endTransaction();
for (int i=0; i<MAX_SOCK_NUM; i++) { for (int i=0; i<MAX_SOCK_NUM; i++) {
SBASE[i] = TXBUF_BASE + SSIZE * i; SBASE[i] = TXBUF_BASE + SSIZE * i;
@ -98,7 +98,7 @@ void W5100Class::recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uin
{ {
uint16_t ptr; uint16_t ptr;
ptr = readSnRX_RD(s); ptr = readSnRX_RD(s);
read_data(s, (uint8_t *)ptr, data, len); read_data(s, ptr, data, len);
if (!peek) if (!peek)
{ {
ptr += len; ptr += len;
@ -106,13 +106,13 @@ void W5100Class::recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uin
} }
} }
void W5100Class::read_data(SOCKET s, volatile uint8_t *src, volatile uint8_t *dst, uint16_t len) void W5100Class::read_data(SOCKET s, volatile uint16_t src, volatile uint8_t *dst, uint16_t len)
{ {
uint16_t size; uint16_t size;
uint16_t src_mask; uint16_t src_mask;
uint16_t src_ptr; uint16_t src_ptr;
src_mask = (uint16_t)src & RMASK; src_mask = src & RMASK;
src_ptr = RBASE[s] + src_mask; src_ptr = RBASE[s] + src_mask;
if( (src_mask + len) > RSIZE ) if( (src_mask + len) > RSIZE )

6
libraries/Ethernet/utility/w5100.h Executable file → Normal file
View File

@ -10,9 +10,10 @@
#ifndef W5100_H_INCLUDED #ifndef W5100_H_INCLUDED
#define W5100_H_INCLUDED #define W5100_H_INCLUDED
#include <avr/pgmspace.h>
#include <SPI.h> #include <SPI.h>
#define SPI_ETHERNET_SETTINGS SPISettings(4000000, MSBFIRST, SPI_MODE0)
#define MAX_SOCK_NUM 4 #define MAX_SOCK_NUM 4
typedef uint8_t SOCKET; typedef uint8_t SOCKET;
@ -138,7 +139,7 @@ public:
* the data from Receive buffer. Here also take care of the condition while it exceed * the data from Receive buffer. Here also take care of the condition while it exceed
* the Rx memory uper-bound of socket. * the Rx memory uper-bound of socket.
*/ */
void read_data(SOCKET s, volatile uint8_t * src, volatile uint8_t * dst, uint16_t len); void read_data(SOCKET s, volatile uint16_t src, volatile uint8_t * dst, uint16_t len);
/** /**
* @brief This function is being called by send() and sendto() function also. * @brief This function is being called by send() and sendto() function also.
@ -340,7 +341,6 @@ private:
inline static void setSS() { PORTB &= ~_BV(2); }; inline static void setSS() { PORTB &= ~_BV(2); };
inline static void resetSS() { PORTB |= _BV(2); }; inline static void resetSS() { PORTB |= _BV(2); };
#endif #endif
}; };
extern W5100Class W5100; extern W5100Class W5100;