Support for WIZnet W5100 ethernet module SPI_EN

The W5100 module has a SPI_EN signal that enables or disables the SPI
bus.
A bug in the module causes MISO to be continously driven by the module
when SPI_EN is pulled high. This is described in this application note:
https://www.google.se/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CCAQFjAA&url=http%3A%2F%2Fwww.wiznet.co.kr%2FUpLoad_Files%2FReferenceFiles%2FW5100_AN_SPI.pdf&ei=ij84VK7uM6nNygPqvICYDw&usg=AFQjCNGz1qttBEwG1azc1Eza4j69v1rpDQ&sig2=UUUtb4Dr0F35g8ByHMsyig&bvm=bv.77161500,d.bGQ

This commit changes the Ethernet gateway sketch to drive SPI_EN high or
low, depending on which module is in use (SPI bus is shared between
Ethernet and RF modules).
The pin attached to SPI_EN is defined by W5100_SPI_EN. This define can
be commented out if SPI_EN is not needed (different module) or if the
module manages SPI_EN in hardware (some W5100 shields does this).
This commit is contained in:
Patrick Fallberg
2014-10-10 22:39:47 +02:00
parent b97372ed01
commit f5ef712308
@@ -2,7 +2,7 @@
/*
* Copyright (C) 2013 Henrik Ekblad <henrik.ekblad@gmail.com>
*
* Contribution by a-lurker
* Contribution by a-lurker and Anticimex
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -38,6 +38,7 @@
* 7 Radio error LED (optional) +5V -> LED -> 270-330 Ohm resistor -> pin 7.
* 6 Radio SPI Slave Select
* 5 Radio Chip Enable
* 4 Ethernet SPI Enable (optional if using a shield/module that manages SPI_EN signal)
* 3 Inclusion mode button (optional), 10K pull down to GND, button to VCC)
* 2 Radio IRQ pin (optional), W5100 Int, if linked to pin 2)
* -----------------------------------------------------------------------------------------------------------
@@ -59,6 +60,7 @@
#define INCLUSION_MODE_TIME 1 // Number of minutes inclusion mode is enabled
#define INCLUSION_MODE_PIN 3 // Digital pin used for inclusion mode button
#define W5100_SPI_EN 4 // Ethernet SPI enable
#define RADIO_CE_PIN 5 // radio chip enable
#define RADIO_SPI_SS_PIN 6 // radio SPI serial select
#define RADIO_ERROR_LED_PIN 7 // Error led pin
@@ -86,23 +88,46 @@ MyGateway gw(RADIO_CE_PIN, RADIO_SPI_SS_PIN, INCLUSION_MODE_TIME);
char inputString[MAX_RECEIVE_LENGTH] = ""; // A string to hold incoming commands from serial/ethernet interface
int inputPos = 0;
void w5100_spi_en(boolean enable)
{
#ifdef W5100_SPI_EN
if (enable)
{
// Pull up pin
pinMode(W5100_SPI_EN, INPUT);
digitalWrite(W5100_SPI_EN, HIGH);
}
else
{
// Ground pin
pinMode(W5100_SPI_EN, OUTPUT);
digitalWrite(W5100_SPI_EN, LOW);
}
#endif
}
void setup()
{
// Initialize gateway at maximum PA level, channel 70 and callback for write operations
gw.begin(RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE, writeEthernet);
w5100_spi_en(true);
Ethernet.begin(mac);
Ethernet.begin(mac, myIp);
// give the Ethernet interface a second to initialize
delay(1000);
// Initialize gateway at generic GW PA level, channel 70 and callback for write operations
w5100_spi_en(false);
gw.begin(RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE, writeEthernet);
// start listening for clients
server.begin();
}
// This will be called when data should be written to ethernet
void writeEthernet(char *writeBuffer) {
w5100_spi_en(true);
server.write(writeBuffer);
w5100_spi_en(false);
}
@@ -110,6 +135,7 @@ void loop()
{
// if an incoming client connects, there will be
// bytes available to read via the client object
w5100_spi_en(true);
EthernetClient client = server.available();
if (client) {
@@ -128,7 +154,9 @@ void loop()
// echo the string to the serial port
Serial.print(inputString);
w5100_spi_en(false);
gw.parseAndSend(inputString);
w5100_spi_en(true);
// clear the string:
inputPos = 0;
@@ -143,6 +171,7 @@ void loop()
}
}
}
w5100_spi_en(false);
gw.processRadioMessage();
}