Skip to content
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

Implemented conditional compilation to switch between SPI and SPI5. #270

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vscode/*
!.vscode/c_c_cpp_properties.json
.build/*
17 changes: 16 additions & 1 deletion examples/LinkStatus/LinkStatus.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetSP.h>

void setup() {
// You can use Ethernet.init(pin) to configure the CS pin
Expand All @@ -23,6 +23,21 @@ void setup() {
//Ethernet.init(15); // ESP8266 with Adafruit FeatherWing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit FeatherWing Ethernet

// ====================================================================
// W5500-Ether add-on for Spresense

// Jumper setting is SJ1=12, use D21/EMMC_DATA3
W5500ETH_reset(SJ1_12);

// Jumper setting is SJ1=23, use D26/I2S_BCK
// W5500ETH_reset(SJ1_23);

// When Jumper setting is SJ2=23, init() is not need.

// Jumper setting is SJ2=12, use D19/I2S_DIN
// Ethernet.init(SJ2_12);
// ====================================================================

Serial.begin(9600);
}

Expand Down
17 changes: 16 additions & 1 deletion examples/TelnetClient/TelnetClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetSP.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
Expand All @@ -45,6 +45,21 @@ void setup() {
//Ethernet.init(15); // ESP8266 with Adafruit FeatherWing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit FeatherWing Ethernet

// ====================================================================
// W5500-Ether add-on for Spresense

// Jumper setting is SJ1=12, use D21/EMMC_DATA3
W5500ETH_reset(SJ1_12);

// Jumper setting is SJ1=23, use D26/I2S_BCK
// W5500ETH_reset(SJ1_23);

// When Jumper setting is SJ2=23, init() is not need.

// Jumper setting is SJ2=12, use D19/I2S_DIN
// Ethernet.init(SJ2_12);
// ====================================================================

// start the Ethernet connection:
Ethernet.begin(mac, ip);

Expand Down
17 changes: 16 additions & 1 deletion examples/WebServer/WebServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetSP.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
Expand All @@ -41,6 +41,21 @@ void setup() {
//Ethernet.init(15); // ESP8266 with Adafruit FeatherWing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit FeatherWing Ethernet

// ====================================================================
// W5500-Ether add-on for Spresense

// Jumper setting is SJ1=12, use D21/EMMC_DATA3
W5500ETH_reset(SJ1_12);

// Jumper setting is SJ1=23, use D26/I2S_BCK
// W5500ETH_reset(SJ1_23);

// When Jumper setting is SJ2=23, init() is not need.

// Jumper setting is SJ2=12, use D19/I2S_DIN
// Ethernet.init(SJ2_12);
// ====================================================================

// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
Expand Down
File renamed without changes.
File renamed without changes.
18 changes: 9 additions & 9 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name=Ethernet
version=2.0.2
author=Various (see AUTHORS file for details)
maintainer=Arduino <[email protected]>
sentence=Enables network connection (local and Internet) using the Arduino Ethernet Board or Shield.
paragraph=With this library you can use the Arduino Ethernet (shield or board) to connect to Internet. The library provides both client and server functionalities. The library permits you to connect to a local network also with DHCP and to resolve DNS.
name=EthernetSP
version=1.0.0
author=crane-elec based on Various
maintainer=crane-elec <[email protected]>
sentence=Enables network connection using the W5500-Ether add-on for Spresense.
paragraph=This library is a modified version of the standard Arduino Ethernet library, specifically adapted for the "W5500-Ether" add-on board for Spresense. https://crane-elec.co.jp/products/vol-20/
category=Communication
url=https://www.arduino.cc/en/Reference/Ethernet
architectures=*
includes=Ethernet.h
url=https://github.com/crane-elec/EthernetSP
architectures=SPRESENSE
includes=
6 changes: 6 additions & 0 deletions src/Ethernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#include "utility/w5100.h"
#include "Dhcp.h"

#if defined(ARDUINO_ARCH_SPRESENSE)
#define SPI SPI5
#endif

IPAddress EthernetClass::_dnsServerAddress;
DhcpClass* EthernetClass::_dhcp = NULL;

Expand Down Expand Up @@ -234,3 +238,5 @@ void EthernetClass::setRetransmissionCount(uint8_t num)


EthernetClass Ethernet;

#undef SPI
6 changes: 6 additions & 0 deletions src/EthernetClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#include "Dns.h"
#include "utility/w5100.h"

#if defined(ARDUINO_ARCH_SPRESENSE)
#define SPI SPI5
#endif

int EthernetClient::connect(const char * host, uint16_t port)
{
DNSClient dns; // Look up the host first
Expand Down Expand Up @@ -211,3 +215,5 @@ uint16_t EthernetClient::remotePort()
SPI.endTransaction();
return port;
}

#undef SPI
31 changes: 31 additions & 0 deletions src/EthernetSP.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef _ETHERNETSP_H_
#define _ETHERNETSP_H_

// reset
#define SJ1_12 (21) // Jumper setting is SJ1=12, use D21/EMMC_DATA3
#define SJ1_23 (26) // Jumper setting is SJ1=23, use D26/I2S_BCK

// cs
#define SJ2_12 (19) // Jumper setting is SJ2=12, use D19/I2S_DIN
#define SJ2_23 (24) // Jumper setting is SJ2=23, use D24/SPI5_CS_X


#if defined(ARDUINO_ARCH_SPRESENSE)

#include "Ethernet.h"
void W5500ETH_reset(uint8_t sj1_rst)
{
pinMode(sj1_rst, OUTPUT);
digitalWrite(sj1_rst, LOW);
delay(50);
digitalWrite(sj1_rst, HIGH);
return;
}

#else
#error ("This library use only Sony spresense with W5500-Ether add-on board.")
#endif



#endif
6 changes: 6 additions & 0 deletions src/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
#include "Ethernet.h"
#include "utility/w5100.h"

#if defined(ARDUINO_ARCH_SPRESENSE)
#define SPI SPI5
#endif

#if ARDUINO >= 156 && !defined(ARDUINO_ARCH_PIC32)
extern void yield(void);
#else
Expand Down Expand Up @@ -536,3 +540,5 @@ bool EthernetClass::socketSendUDP(uint8_t s)
/* Sent ok */
return true;
}

#undef SPI
39 changes: 39 additions & 0 deletions src/utility/w5100.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include "Ethernet.h"
#include "w5100.h"

#if defined(ARDUINO_ARCH_SPRESENSE)
#define SPI SPI5
#endif

/***************************************************/
/** Default SS pin setting **/
Expand Down Expand Up @@ -218,6 +221,10 @@ uint8_t W5100Class::softReset(void)

uint8_t W5100Class::isW5100(void)
{
#if defined(ARDUINO_ARCH_SPRESENSE)
// ignore to use W5500-Ether.
return 0;
#else
chip = 51;
//Serial.println("w5100.cpp: detect W5100 chip");
if (!softReset()) return 0;
Expand All @@ -229,10 +236,15 @@ uint8_t W5100Class::isW5100(void)
if (readMR() != 0x00) return 0;
//Serial.println("chip is W5100");
return 1;
#endif
}

uint8_t W5100Class::isW5200(void)
{
#if defined(ARDUINO_ARCH_SPRESENSE)
// ignore to use W5500-Ether.
return 0;
#else
chip = 52;
//Serial.println("w5100.cpp: detect W5200 chip");
if (!softReset()) return 0;
Expand All @@ -248,6 +260,7 @@ uint8_t W5100Class::isW5200(void)
if (ver != 3) return 0;
//Serial.println("chip is W5200");
return 1;
#endif
}

uint8_t W5100Class::isW5500(void)
Expand Down Expand Up @@ -368,6 +381,17 @@ uint16_t W5100Class::write(uint16_t addr, const uint8_t *buf, uint16_t len)
}
SPI.transfer(cmd, len + 3);
} else {
#if defined(ARDUINO_ARCH_SPRESENSE)
// Spresense SPI peripheral must send one transfer, one time.
uint8_t *txbuf = (uint8_t*)(malloc(3 + len));
if (txbuf)
{
memcpy(txbuf, cmd, 3);
memcpy(txbuf + 3, buf, len);
SPI.transfer(txbuf, 3 + len);
free(txbuf);
}
#else
SPI.transfer(cmd, 3);
#ifdef SPI_HAS_TRANSFER_BUF
SPI.transfer(buf, NULL, len);
Expand All @@ -376,6 +400,7 @@ uint16_t W5100Class::write(uint16_t addr, const uint8_t *buf, uint16_t len)
for (uint16_t i=0; i < len; i++) {
SPI.transfer(buf[i]);
}
#endif
#endif
}
resetSS();
Expand Down Expand Up @@ -457,9 +482,21 @@ uint16_t W5100Class::read(uint16_t addr, uint8_t *buf, uint16_t len)
cmd[2] = ((addr >> 6) & 0xE0) | 0x18; // 2K buffers
#endif
}
#if defined(ARDUINO_ARCH_SPRESENSE)
uint8_t *rxbuf = (uint8_t*)(malloc(3 + len));
if (rxbuf)
{
memset(rxbuf, 0, 3 + len);
memcpy(rxbuf, cmd, 3);
SPI.transfer(rxbuf, 3 + len);// [in,out] buf Buffer to send and receive.
memcpy(buf, rxbuf + 3, len);
free(rxbuf);
}
#else
SPI.transfer(cmd, 3);
memset(buf, 0, len);
SPI.transfer(buf, len);
#endif
resetSS();
}
return len;
Expand All @@ -472,3 +509,5 @@ void W5100Class::execCmdSn(SOCKET s, SockCMD _cmd)
// Wait for command to complete
while (readSnCR(s)) ;
}

#undef SPI
19 changes: 19 additions & 0 deletions src/utility/w5100.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
#error "Ethernet.h must be included before w5100.h"
#endif

// Spresense with W5500-Ether add-on. When SJ2 is "2-3 short", must be SPI_MODE3
#if defined(ARDUINO_ARCH_SPRESENSE)
#undef SPI_ETHERNET_SETTINGS
#define SPI_ETHERNET_SETTINGS SPISettings(13000000, MSBFIRST, SPI_MODE3)
#define SPI5_CS 24
#endif

// Arduino 101's SPI can not run faster than 8 MHz.
#if defined(ARDUINO_ARCH_ARC32)
Expand Down Expand Up @@ -432,6 +438,19 @@ class W5100Class {
inline static void resetSS() {
*(ss_pin_reg+6) = ss_pin_mask;
}
#elif defined(ARDUINO_ARCH_SPRESENSE)
inline static void initSS() {
if (ss_pin != SPI5_CS)
pinMode(ss_pin, OUTPUT);
}
inline static void setSS() {
if (ss_pin != SPI5_CS)
digitalWrite(ss_pin, LOW);
}
inline static void resetSS() {
if (ss_pin != SPI5_CS)
digitalWrite(ss_pin, HIGH);
}
#else
inline static void initSS() {
pinMode(ss_pin, OUTPUT);
Expand Down
Loading