Skip to content

Commit f5ef712

Browse files
committed
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).
1 parent b97372e commit f5ef712

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

libraries/MySensors/examples/EthernetGateway/EthernetGateway.ino

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
* Copyright (C) 2013 Henrik Ekblad <[email protected]>
44
*
5-
* Contribution by a-lurker
5+
* Contribution by a-lurker and Anticimex
66
*
77
* This program is free software; you can redistribute it and/or
88
* modify it under the terms of the GNU General Public License
@@ -38,6 +38,7 @@
3838
* 7 Radio error LED (optional) +5V -> LED -> 270-330 Ohm resistor -> pin 7.
3939
* 6 Radio SPI Slave Select
4040
* 5 Radio Chip Enable
41+
* 4 Ethernet SPI Enable (optional if using a shield/module that manages SPI_EN signal)
4142
* 3 Inclusion mode button (optional), 10K pull down to GND, button to VCC)
4243
* 2 Radio IRQ pin (optional), W5100 Int, if linked to pin 2)
4344
* -----------------------------------------------------------------------------------------------------------
@@ -59,6 +60,7 @@
5960
#define INCLUSION_MODE_TIME 1 // Number of minutes inclusion mode is enabled
6061
#define INCLUSION_MODE_PIN 3 // Digital pin used for inclusion mode button
6162

63+
#define W5100_SPI_EN 4 // Ethernet SPI enable
6264
#define RADIO_CE_PIN 5 // radio chip enable
6365
#define RADIO_SPI_SS_PIN 6 // radio SPI serial select
6466
#define RADIO_ERROR_LED_PIN 7 // Error led pin
@@ -86,30 +88,54 @@ MyGateway gw(RADIO_CE_PIN, RADIO_SPI_SS_PIN, INCLUSION_MODE_TIME);
8688
char inputString[MAX_RECEIVE_LENGTH] = ""; // A string to hold incoming commands from serial/ethernet interface
8789
int inputPos = 0;
8890

91+
void w5100_spi_en(boolean enable)
92+
{
93+
#ifdef W5100_SPI_EN
94+
if (enable)
95+
{
96+
// Pull up pin
97+
pinMode(W5100_SPI_EN, INPUT);
98+
digitalWrite(W5100_SPI_EN, HIGH);
99+
}
100+
else
101+
{
102+
// Ground pin
103+
pinMode(W5100_SPI_EN, OUTPUT);
104+
digitalWrite(W5100_SPI_EN, LOW);
105+
}
106+
#endif
107+
}
108+
89109
void setup()
90110
{
91-
// Initialize gateway at maximum PA level, channel 70 and callback for write operations
92-
gw.begin(RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE, writeEthernet);
93-
111+
w5100_spi_en(true);
112+
Ethernet.begin(mac);
94113
Ethernet.begin(mac, myIp);
95114

96115
// give the Ethernet interface a second to initialize
97116
delay(1000);
98117

118+
// Initialize gateway at generic GW PA level, channel 70 and callback for write operations
119+
w5100_spi_en(false);
120+
gw.begin(RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE, writeEthernet);
121+
99122
// start listening for clients
100123
server.begin();
101124
}
102125

103126
// This will be called when data should be written to ethernet
104127
void writeEthernet(char *writeBuffer) {
128+
w5100_spi_en(true);
105129
server.write(writeBuffer);
130+
w5100_spi_en(false);
106131
}
107132

108133

109134
void loop()
110135
{
111136
// if an incoming client connects, there will be
112137
// bytes available to read via the client object
138+
w5100_spi_en(true);
113139
EthernetClient client = server.available();
114140

115141
if (client) {
@@ -128,7 +154,9 @@ void loop()
128154
// echo the string to the serial port
129155
Serial.print(inputString);
130156

157+
w5100_spi_en(false);
131158
gw.parseAndSend(inputString);
159+
w5100_spi_en(true);
132160

133161
// clear the string:
134162
inputPos = 0;
@@ -143,6 +171,7 @@ void loop()
143171
}
144172
}
145173
}
174+
w5100_spi_en(false);
146175
gw.processRadioMessage();
147176
}
148177

0 commit comments

Comments
 (0)