22/*
33 * Copyright (C) 2013 Henrik Ekblad <henrik.ekblad@gmail.com>
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
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 * -----------------------------------------------------------------------------------------------------------
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);
8688char inputString[MAX_RECEIVE_LENGTH] = " " ; // A string to hold incoming commands from serial/ethernet interface
8789int 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+
89109void 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
104127void writeEthernet (char *writeBuffer) {
128+ w5100_spi_en (true );
105129 server.write (writeBuffer);
130+ w5100_spi_en (false );
106131}
107132
108133
109134void 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