8000 Backported Ethernet library from 1.5.x · arduino-collections/Arduino-1@b9b0fcd · GitHub
[go: up one dir, main page]

Skip to content

Commit b9b0fcd

Browse files
committed
Backported Ethernet library from 1.5.x
1 parent 6ecb174 commit b9b0fcd

File tree

22 files changed

+348
-265
lines changed

22 files changed

+348
-265
lines changed

libraries/Ethernet/Dhcp.cpp

100755100644
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// DHCP Library v0.3 - April 25, 2009
22
// Author: Jordan Terrell - blog.jordanterrell.com
33

4-
#include "w5100.h"
4+
#include "utility/w5100.h"
55

66
#include <string.h>
77
#include <stdlib.h>
88
#include "Dhcp.h"
99
#include "Arduino.h"
10-
#include "util.h"
10+
#include "utility/util.h"
1111

1212
int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout)
1313
{

libraries/Ethernet/Dhcp.h

100755100644
File mode changed.

libraries/Ethernet/Dns.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// (c) Copyright 2009-2010 MCQN Ltd.
33
// Released under Apache License, version 2.0
44

5-
#include "w5100.h"
5+
#include "utility/w5100.h"
66
#include "EthernetUdp.h"
7-
#include "util.h"
7+
#include "utility/util.h"
88

99
#include "Dns.h"
1010
#include <string.h>

libraries/Ethernet/Ethernet.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "w5100.h"
1+
#include "utility/w5100.h"
22
#include "Ethernet.h"
33
#include "Dhcp.h"
44

@@ -16,18 +16,22 @@ int EthernetClass::begin(uint8_t *mac_address)
1616

1717
// Initialise the basic info
1818
W5100.init();
19+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
1920
W5100.setMACAddress(mac_address);
2021
W5100.setIPAddress(IPAddress(0,0,0,0).raw_address());
22+
SPI.endTransaction();
2123

2224
// Now try to get our config info from a DHCP server
2325
int ret = _dhcp->beginWithDHCP(mac_address);
2426
if(ret == 1)
2527
{
2628
// We've successfully found a DHCP server and got our configuration info, so set things
2729
// accordingly
30+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
2831
W5100.setIPAddress(_dhcp->getLocalIp().raw_address());
2932
W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address());
3033
W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address());
34+
SPI.endTransaction();
3135
_dnsServerAddress = _dhcp->getDnsServerIp();
3236
}
3337

@@ -61,10 +65,12 @@ void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dn
6165
void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
6266
{
6367
W5100.init();
68+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
6469
W5100.setMACAddress(mac);
65-
W5100.setIPAddress(local_ip._address);
66-
W5100.setGatewayIp(gateway._address);
67-
W5100.setSubnetMask(subnet._address);
70+
W5100.setIPAddress(local_ip.raw_address());
71+
W5100.setGatewayIp(gateway.raw_address());
72+
W5100.setSubnetMask(subnet.raw_address());
73+
SPI.endTransaction();
6874
_dnsServerAddress = dns_server;
6975
}
7076

@@ -80,9 +86,11 @@ int EthernetClass::maintain(){
8086
case DHCP_CHECK_RENEW_OK:
8187
case DHCP_CHECK_REBIND_OK:
8288
//we might have got a new IP.
89+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
8390
W5100.setIPAddress(_dhcp->getLocalIp().raw_address());
8491
W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address());
8592
W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address());
93+
SPI.endTransaction();
8694
_dnsServerAddress = _dhcp->getDnsServerIp();
8795
break;
8896
default:
@@ -96,21 +104,27 @@ int EthernetClass::maintain(){
96104
IPAddress EthernetClass::localIP()
97105
{
98106
IPAddress ret;
107+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
99108
W5100.getIPAddress(ret.raw_address());
109+
SPI.endTransaction();
100110
return ret;
101111
}
102112

103113
IPAddress EthernetClass::subnetMask()
104114
{
105115
IPAddress ret;
116+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
106117
W5100.getSubnetMask(ret.raw_address());
118+
SPI.endTransaction();
107119
return ret;
108120
}
109121

110122
IPAddress EthernetClass::gatewayIP()
111123
{
112124
IPAddress ret;
125+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
113126
W5100.getGatewayIp(ret.raw_address());
127+
SPI.endTransaction();
114128
return ret;
115129
}
116130

libraries/Ethernet/EthernetClient.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include "w5100.h"
2-
#include "socket.h"
1+
#include "utility/w5100.h"
2+
#include "utility/socket.h"
33

44
extern "C" {
55
#include "string.h"
@@ -40,7 +40,7 @@ int EthernetClient::connect(IPAddress ip, uint16_t port) {
4040
return 0;
4141

4242
for (int i = 0; i < MAX_SOCK_NUM; i++) {
43-
uint8_t s = W5100.readSnSR(i);
43+
uint8_t s = socketStatus(i);
4444
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) {
4545
_sock = i;
4646
break;
@@ -88,7 +88,7 @@ size_t EthernetClient::write(const uint8_t *buf, size_t size) {
8888

8989
int EthernetClient::available() {
9090
if (_sock != MAX_SOCK_NUM)
91-
return W5100.getRXReceivedSize(_sock);
91+
return recvAvailable(_sock);
9292
return 0;
9393
}
9494

@@ -120,8 +120,7 @@ int EthernetClient::peek() {
120120
}
121121

122122
void EthernetClient::flush() {
123-
while (available())
124-
read();
123+
::flush(_sock);
125124
}
126125

127126
void EthernetClient::stop() {
@@ -154,7 +153,7 @@ uint8_t EthernetClient::connected() {
154153

155154
uint8_t EthernetClient::status() {
156155
if (_sock == MAX_SOCK_NUM) return SnSR::CLOSED;
157-
return W5100.readSnSR(_sock);
156+
return socketStatus(_sock);
158157
}
159158

160159
// the next function allows us to use the client returned by

libraries/Ethernet/EthernetServer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include "w5100.h"
2-
#include "socket.h"
1+
#include "utility/w5100.h"
2+
#include "utility/socket.h"
33
extern "C" {
44
#include "string.h"
55
}

libraries/Ethernet/EthernetUdp.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
* bjoern@cs.stanford.edu 12/30/2008
2727
*/
2828

29-
#include "w5100.h"
30-
#include "socket.h"
29+
#include "utility/w5100.h"
30+
#include "utility/socket.h"
3131
#include "Ethernet.h"
3232
#include "Udp.h"
3333
#include "Dns.h"
@@ -41,7 +41,7 @@ uint8_t EthernetUDP::begin(uint16_t port) {
4141
return 0;
4242

4343
for (int i = 0; i < MAX_SOCK_NUM; i++) {
44-
uint8_t s = W5100.readSnSR(i);
44+
uint8_t s = socketStatus(i);
4545
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) {
4646
_sock = i;
4747
break;
@@ -120,7 +120,7 @@ int EthernetUDP::parsePacket()
120120
// discard any remaining bytes in the last packet
121121
flush();
122122

123-
if (W5100.getRXReceivedSize(_sock) > 0)
123+
if (recvAvailable(_sock) > 0)
124124
{
125125
//HACK - hand-parse the UDP packet using TCP recv method
126126
uint8_t tmpBuf[8];

libraries/Ethernet/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
/*
22
SCP1000 Barometric Pressure Sensor Display
3-
3+
44
Serves the output of a Barometric Pressure Sensor as a web page.
55
Uses the SPI library. For details on the sensor, see:
66
http://www.sparkfun.com/commerce/product_info.php?products_id=8161
77
http://www.vti.fi/en/support/obsolete_products/pressure_sensors/
8-
8+
99
This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
1010
http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip
11-
11+
1212
Circuit:
1313
SCP1000 sensor attached to pins 6,7, and 11 - 13:
1414
DRDY: pin 6
1515
CSB: pin 7
1616
MOSI: pin 11
1717
MISO: pin 12
1818
SCK: pin 13
19-
19+
2020
created 31 July 2010
2121
by Tom Igoe
2222
*/
@@ -28,16 +28,17 @@
2828

2929
// assign a MAC address for the ethernet controller.
3030
// fill in your address here:
31-
byte mac[] = {
32-
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
31+
byte mac[] = {
32+
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
33+
};
3334
// assign an IP address for the controller:
34-
IPAddress ip(192,168,1,20);
35-
IPAddress gateway(192,168,1,1);
35+
IPAddress ip(192, 168, 1, 20);
36+
IPAddress gateway(192, 168, 1, 1);
3637
IPAddress subnet(255, 255, 255, 0);
3738

3839

3940
// Initialize the Ethernet server library
40-
// with the IP address and port you want to use
41+
// with the IP address and port you want to use
4142
// (port 80 is default for HTTP):
4243
EthernetServer server(80);
4344

@@ -49,7 +50,7 @@ const int TEMPERATURE = 0x21; //16 bit temperature reading
4950

5051
// pins used for the connection with the sensor
5152
// the others you need are controlled by the SPI library):
52-
const int dataReadyPin = 6;
53+
const int dataReadyPin = 6;
5354
const int chipSelectPin = 7;
5455

5556
float temperature = 0.0;
@@ -83,9 +84,9 @@ void setup() {
8384

8485
}
8586

86-
void loop() {
87+
void loop() {
8788
// check for a reading no more than once a second.
88-
if (millis() - lastReadingTime > 1000){
89+
if (millis() - lastReadingTime > 1000) {
8990
// if there's a reading ready, read it:
9091
// don't do anything until the data ready pin is high:
9192
if (digitalRead(dataReadyPin) == HIGH) {
@@ -109,13 +110,13 @@ void getData() {
109110
temperature = (float)tempData / 20.0;
110111

111112
//Read the pressure data highest 3 bits:
112-
byte pressureDataHigh = readRegister(0x1F, 1);
113+
byte pressureDataHigh = readRegister(0x1F, 1);
113114
pressureDataHigh &= 0b00000111; //you only needs bits 2 to 0
114115

115116
//Read the pressure data lower 16 bits:
116-
unsigned int pressureDataLow = readRegister(0x20, 2);
117+
unsigned int pressureDataLow = readRegister(0x20, 2);
117118
//combine the two parts into one 19-bit number:
118-
pressure = ((pressureDataHigh << 16) | pressureDataLow)/4;
119+
pressure = ((pressureDataHigh << 16) | pressureDataLow) / 4;
119120

120121
Serial.print("Temperature: ");
121122
Serial.print(temperature);
@@ -149,13 +150,13 @@ void listenForEthernetClients() {
149150
client.println("<br />");
150151
client.print("Pressure: " + String(pressure));
151152
client.print(" Pa");
152-
client.println("<br />");
153+
client.println("<br />");
153154
break;
154155
}
155156
if (c == '\n') {
156157
// you're starting a new line
157158
currentLineIsBlank = true;
158-
}
159+
}
159160
else if (c != '\r') {
160161
// you've gotten a character on the current line
161162
currentLineIsBlank = false;
@@ -167,7 +168,7 @@ void listenForEthernetClients() {
167168
// close the connection:
168169
client.stop();
169170
}
170-
}
171+
}
171172

172173

173174
//Send a write command to SCP1000
@@ -179,20 +180,20 @@ void writeRegister(byte registerName, byte registerValue) {
179180
registerName |= 0b00000010; //Write command
180181

181182
// take the chip select low to select the device:
182-
digitalWrite(chipSelectPin, LOW);
183+
digitalWrite(chipSelectPin, LOW);
183184

184185
SPI.transfer(registerName); //Send register location
185186
SPI.transfer(registerValue); //Send value to record into register
186187

187188
// take the chip select high to de-select:
188-
digitalWrite(chipSelectPin, HIGH);
189+
digitalWrite(chipSelectPin, HIGH);
189190
}
190191

191192

192193
//Read register from the SCP1000:
193194
unsigned int readRegister(byte registerName, int numBytes) {
194195
byte inByte = 0; // incoming from the SPI read
195-
unsigned int result = 0; // result to return
196+
unsigned int result = 0; // result to return
196197

197198
// SCP1000 expects the register name in the upper 6 bits
198199
// of the byte:
@@ -201,22 +202,22 @@ unsigned int readRegister(byte registerName, int numBytes) {
201202
registerName &= 0b11111100; //Read command
202203

203204
// take the chip select low to select the device:
204-
digitalWrite(chipSelectPin, LOW);
205+
digitalWrite(chipSelectPin, LOW);
205206
// send the device the register you want to read:
206-
int command = SPI.transfer(registerName);
207+
int command = SPI.transfer(registerName);
207208
// send a value of 0 to read the first byte returned:
208-
inByte = SPI.transfer(0x00);
209-
209+
inByte = SPI.transfer(0x00);
210+
210211
result = inByte;
211-
// if there's more than one byte returned,
212+
// if there's more than one byte returned,
212213
// shift the first byte then get the second byte:
213-
if (numBytes > 1){
214+
if (numBytes > 1) {
214215
result = inByte << 8;
215-
inByte = SPI.transfer(0x00);
216-
result = result |inByte;
216+
inByte = SPI.transfer(0x00);
217+
result = result | inByte;
217218
}
218219
// take the chip select high to de-select:
219-
digitalWrite(chipSelectPin, HIGH);
220+
digitalWrite(chipSelectPin, HIGH);
220221
// return the result:
221222
return(result);
222223
}

0 commit comments

Comments
 (0)
0