8000 [IDE 1.0.x (backport from 1.5.x)] SPI Transactions by cmaglie · Pull Request #2365 · arduino/Arduino · GitHub
[go: up one dir, main page]

Skip to content

[IDE 1.0.x (backport from 1.5.x)] SPI Transactions #2365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 2, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Backported Ethernet library from 1.5.x
  • Loading branch information
cmaglie committed Dec 2, 2014
commit b9b0fcdadc04a6a2d6954611b49aa1f7a6422e80
4 changes: 2 additions & 2 deletions libraries/Ethernet/Dhcp.cpp
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// DHCP Library v0.3 - April 25, 2009
// Author: Jordan Terrell - blog.jordanterrell.com

#include "w5100.h"
#include "utility/w5100.h"

#include <string.h>
#include <stdlib.h>
#include "Dhcp.h"
#include "Arduino.h"
#include "util.h"
#include "utility/util.h"

int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout)
{
Expand Down
Empty file modified libraries/Ethernet/Dhcp.h
100755 → 100644
Empty file.
4 changes: 2 additions & 2 deletions libraries/Ethernet/Dns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// (c) Copyright 2009-2010 MCQN Ltd.
// Released under Apache License, version 2.0

#include "w5100.h"
#include "utility/w5100.h"
#include "EthernetUdp.h"
#include "util.h"
#include "utility/util.h"

#include "Dns.h"
#include <string.h>
Expand Down
22 changes: 18 additions & 4 deletions libraries/Ethernet/Ethernet.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "w5100.h"
#include "utility/w5100.h"
#include "Ethernet.h"
#include "Dhcp.h"

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

// Initialise the basic info
W5100.init();
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.setMACAddress(mac_address);
W5100.setIPAddress(IPAddress(0,0,0,0).raw_address());
SPI.endTransaction();

// Now try to get our config info from a DHCP server
int ret = _dhcp->beginWithDHCP(mac_address);
if(ret == 1)
{
// We've successfully found a DHCP server and got our configuration info, so set things
// accordingly
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.setIPAddress(_dhcp->getLocalIp().raw_address());
W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address());
W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address());
SPI.endTransaction();
_dnsServerAddress = _dhcp->getDnsServerIp();
}

Expand Down Expand Up @@ -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)
{
W5100.init();
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.setMACAddress(mac);
W5100.setIPAddress(local_ip._address);
W5100.setGatewayIp(gateway._address);
W5100.setSubnetMask(subnet._address);
W5100.setIPAddress(local_ip.raw_address());
W5100.setGatewayIp(gateway.raw_address());
W5100.setSubnetMask(subnet.raw_address());
SPI.endTransaction();
_dnsServerAddress = dns_server;
}

Expand All @@ -80,9 +86,11 @@ int EthernetClass::maintain(){
case DHCP_CHECK_RENEW_OK:
case DHCP_CHECK_REBIND_OK:
//we might have got a new IP.
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.setIPAddress(_dhcp->getLocalIp().raw_address());
W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address());
W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address());
SPI.endTransaction();
_dnsServerAddress = _dhcp->getDnsServerIp();
break;
default:
Expand All @@ -96,21 +104,27 @@ int EthernetClass::maintain(){
IPAddress EthernetClass::localIP()
{
IPAddress ret;
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.getIPAddress(ret.raw_address());
SPI.endTransaction();
return ret;
}

IPAddress EthernetClass::subnetMask()
{
IPAddress ret;
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.getSubnetMask(ret.raw_address());
SPI.endTransaction();
return ret;
}

IPAddress EthernetClass::gatewayIP()
{
IPAddress ret;
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.getGatewayIp(ret.raw_address());
SPI.endTransaction();
return ret;
}

Expand Down
13 changes: 6 additions & 7 deletions libraries/Ethernet/EthernetClient.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "w5100.h"
#include "socket.h"
#include "utility/w5100.h"
#include "utility/socket.h"

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

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) {
_sock = i;
break;
Expand Down Expand Up @@ -88,7 +88,7 @@ size_t EthernetClient::write(const uint8_t *buf, size_t size) {

int EthernetClient::available() {
if (_sock != MAX_SOCK_NUM)
return W5100.getRXReceivedSize(_sock);
return recvAvailable(_sock);
return 0;
}

Expand Down Expand Up @@ -120,8 +120,7 @@ int EthernetClient::peek() {
}

void EthernetClient::flush() {
while (available())
read();
::flush(_sock);
}

void EthernetClient::stop() {
Expand Down Expand Up @@ -154,7 +153,7 @@ uint8_t EthernetClient::connected() {

uint8_t EthernetClient::status() {
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
Expand Down
4 changes: 2 additions & 2 deletions libraries/Ethernet/EthernetServer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "w5100.h"
#include "socket.h"
#include "utility/w5100.h"
#include "utility/socket.h"
extern "C" {
#include "string.h"
}
Expand Down
8 changes: 4 additions & 4 deletions libraries/Ethernet/EthernetUdp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
* bjoern@cs.stanford.edu 12/30/2008
*/

#include "w5100.h"
#include "socket.h"
#include "utility/w5100.h"
#include "utility/socket.h"
#include "Ethernet.h"
#include "Udp.h"
#include "Dns.h"
Expand All @@ -41,7 +41,7 @@ uint8_t EthernetUDP::begin(uint16_t port) {
return 0;

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) {
_sock = i;
break;
Expand Down Expand Up @@ -120,7 +120,7 @@ int EthernetUDP::parsePacket()
// discard any remaining bytes in the last packet
flush();

if (W5100.getRXReceivedSize(_sock) > 0)
if (recvAvailable(_sock) > 0)
{
//HACK - hand-parse the UDP packet using TCP recv method
uint8_t tmpBuf[8];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/*
SCP1000 Barometric Pressure Sensor Display

Serves the output of a Barometric Pressure Sensor as a web page.
Uses the SPI library. For details on the sensor, see:
http://www.sparkfun.com/commerce/product_info.php?products_id=8161
http://www.vti.fi/en/support/obsolete_products/pressure_sensors/

This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip

Circuit:
SCP1000 sensor attached to pins 6,7, and 11 - 13:
DRDY: pin 6
CSB: pin 7
MOSI: pin 11
MISO: pin 12
SCK: pin 13

created 31 July 2010
by Tom Igoe
*/
Expand All @@ -28,16 +28,17 @@

// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
// assign an IP address for the controller:
IPAddress ip(192,168,1,20);
IPAddress gateway(192,168,1,1);
IPAddress ip(192, 168, 1, 20);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);


// 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):
EthernetServer server(80);

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

// pins used for the connection with the sensor
// the others you need are controlled by the SPI library):
const int dataReadyPin = 6;
const int dataReadyPin = 6;
const int chipSelectPin = 7;

float temperature = 0.0;
Expand Down Expand Up @@ -83,9 +84,9 @@ void setup() {

}

void loop() {
void loop() {
// 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:
// don't do anything until the data ready pin is high:
if (digitalRead(dataReadyPin) == HIGH) {
Expand All @@ -109,13 +110,13 @@ void getData() {
temperature = (float)tempData / 20.0;

//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

//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:
pressure = ((pressureDataHigh << 16) | pressureDataLow)/4;
pressure = ((pressureDataHigh << 16) | pressureDataLow) / 4;

Serial.print("Temperature: ");
Serial.print(temperature);
Expand Down Expand Up @@ -149,13 +150,13 @@ void listenForEthernetClients() {
client.println("<br />");
client.print("Pressure: " + String(pressure));
client.print(" Pa");
client.println("<br />");
client.println("<br />");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
Expand All @@ -167,7 +168,7 @@ void listenForEthernetClients() {
// close the connection:
client.stop();
}
}
}


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

// take the chip select low to select the device:
digitalWrite(chipSelectPin, LOW);
digitalWrite(chipSelectPin, LOW);

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

// take the chip select high to de-select:
digitalWrite(chipSelectPin, HIGH);
digitalWrite(chipSelectPin, HIGH);
}


//Read register from the SCP1000:
unsigned int readRegister(byte registerName, int numBytes) {
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
// of the byte:
Expand All @@ -201,22 +202,22 @@ unsigned int readRegister(byte registerName, int numBytes) {
registerName &= 0b11111100; //Read command

// take the chip select low to select the device:
digitalWrite(chipSelectPin, LOW);
digitalWrite(chipSelectPin, LOW);
// 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:
inByte = SPI.transfer(0x00);
inByte = SPI.transfer(0x00);

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:
if (numBytes > 1){
if (numBytes > 1) {
result = inByte << 8;
inByte = SPI.transfer(0x00);
result = result |inByte;
inByte = SPI.transfer(0x00);
result = result | inByte;
}
// take the chip select high to de-select:
digitalWrite(chipSelectPin, HIGH);
digitalWrite(chipSelectPin, HIGH);
// return the result:
return(result);
}
Loading
0