Allow W5100 boards with SPI_EN exposed to run without SOFTSPI for radio

This commit is contained in:
Henrik Ekblad
2015-10-05 23:07:07 +02:00
parent 676e504fda
commit 2fd0f36054
3 changed files with 63 additions and 19 deletions
@@ -68,8 +68,32 @@ static inputBuffer inputString[MY_GATEWAY_MAX_CLIENTS];
void gatewayTransportRenewIP(); void gatewayTransportRenewIP();
#endif #endif
// On W5100 boards with SPI_EN exposed we can use the real SPI bus together with radio
// (if we enable it during usage)
#ifdef MY_W5100_SPI_EN
void _w5100_spi_en(boolean enable)
{
if (enable)
{
// Pull up pin
pinMode(MY_W5100_SPI_EN, INPUT);
digitalWrite(MY_W5100_SPI_EN, HIGH);
}
else
{
// Ground pin
pinMode(MY_W5100_SPI_EN, OUTPUT);
digitalWrite(MY_W5100_SPI_EN, LOW);
}
}
#else
#define _w5100_spi_en(x)
#endif
bool gatewayTransportInit() { bool gatewayTransportInit() {
_w5100_spi_en(true);
#if defined(MY_GATEWAY_ESP8266) #if defined(MY_GATEWAY_ESP8266)
(void)WiFi.begin(MY_ESP8266_SSID, MY_ESP8266_PASSWORD); (void)WiFi.begin(MY_ESP8266_SSID, MY_ESP8266_PASSWORD);
#ifdef MY_IP_ADDRESS #ifdef MY_IP_ADDRESS
@@ -109,34 +133,36 @@ bool gatewayTransportInit() {
_ethernetServer.setNoDelay(true); _ethernetServer.setNoDelay(true);
#endif #endif
#endif /* USE_UDP */ #endif /* USE_UDP */
_w5100_spi_en(false);
return true; return true;
} }
bool gatewayTransportSend(MyMessage &message) bool gatewayTransportSend(MyMessage &message)
{ {
bool ret = true;
char *_ethernetMsg = protocolFormat(message); char *_ethernetMsg = protocolFormat(message);
_w5100_spi_en(true);
#if defined(MY_CONTROLLER_IP_ADDRESS) #if defined(MY_CONTROLLER_IP_ADDRESS)
#if defined(MY_USE_UDP) #if defined(MY_USE_UDP)
_ethernetServer.beginPacket(_ethernetControllerIP, MY_PORT); _ethernetServer.beginPacket(_ethernetControllerIP, MY_PORT);
_ethernetServer.write(_ethernetMsg, strlen(_ethernetMsg)); _ethernetServer.write(_ethernetMsg, strlen(_ethernetMsg));
// returns 1 if the packet was sent successfully // returns 1 if the packet was sent successfully
return _ethernetServer.endPacket(); ret _ethernetServer.endPacket();
#else #else
EthernetClient client; EthernetClient client;
if (client.connect(_ethernetControllerIP, MY_PORT)) { if (client.connect(_ethernetControllerIP, MY_PORT)) {
client.write(_ethernetMsg, strlen(_ethernetMsg)); client.write(_ethernetMsg, strlen(_ethernetMsg));
client.stop(); client.stop();
return true;
} }
else { else {
// connecting to the server failed! // connecting to the server failed!
return false; ret = false;
} }
#endif #endif
#else #else
// Send message to connected clients // Send message to connected clients
_w5100_spi_en(true);
#if defined(MY_GATEWAY_ESP8266) #if defined(MY_GATEWAY_ESP8266)
for (uint8_t i = 0; i < ARRAY_SIZE(clients); i++) for (uint8_t i = 0; i < ARRAY_SIZE(clients); i++)
{ {
@@ -145,12 +171,13 @@ bool gatewayTransportSend(MyMessage &message)
clients[i].write((uint8_t*)_ethernetMsg, strlen(_ethernetMsg)); clients[i].write((uint8_t*)_ethernetMsg, strlen(_ethernetMsg));
} }
} }
return true;
#else #else
_ethernetServer.write(_ethernetMsg); _ethernetServer.write(_ethernetMsg);
return true;
#endif #endif
#endif #endif
_w5100_spi_en(false);
return ret;
} }
bool _readFromClient(uint8_t i) { bool _readFromClient(uint8_t i) {
@@ -169,8 +196,9 @@ bool _readFromClient(uint8_t i) {
bool ret = protocolParse(_ethernetMsg, inputString[i].string); bool ret = protocolParse(_ethernetMsg, inputString[i].string);
inputString[i].string[inputString[i].idx] = 0; inputString[i].string[inputString[i].idx] = 0;
if (ret) if (ret) {
return ret; return ret;
}
} else { } else {
// add it to the inputString: // add it to the inputString:
@@ -189,17 +217,20 @@ bool _readFromClient(uint8_t i) {
bool gatewayTransportAvailable() bool gatewayTransportAvailable()
{ {
_w5100_spi_en(true);
#if !defined(MY_IP_ADDRESS) && defined(MY_GATEWAY_W5100) #if !defined(MY_IP_ADDRESS) && defined(MY_GATEWAY_W5100)
// renew IP address using DHCP // renew IP address using DHCP
gatewayTransportRenewIP(); gatewayTransportRenewIP();
#endif #endif
#ifdef MY_USE_UDP #ifdef MY_USE_UDP
int packet_size = _ethernetServer.parsePacket(); int packet_size = _ethernetServer.parsePacket();
if (packet_size) { if (packet_size) {
//debug(PSTR("UDP packet available. Size:%d\n"), packet_size); //debug(PSTR("UDP packet available. Size:%d\n"), packet_size);
_ethernetServer.read(inputString[0].string, MY_GATEWAY_MAX_RECEIVE_LENGTH); _ethernetServer.read(inputString[0].string, MY_GATEWAY_MAX_RECEIVE_LENGTH);
_w5100_spi_en(false);
debug(PSTR("UDP packet received: %s\n"), inputString[0].string); debug(PSTR("UDP packet received: %s\n"), inputString[0].string);
return protocolParse(_ethernetMsg, inputString[0].string); return protocolParse(_ethernetMsg, inputString[0].string);
} }
@@ -219,7 +250,9 @@ bool gatewayTransportAvailable()
clients[i] = _ethernetServer.available(); clients[i] = _ethernetServer.available();
inputString[i].idx = 0; inputString[i].idx = 0;
debug(PSTR("Client %d connected\n"), i); debug(PSTR("Client %d connected\n"), i);
_w5100_spi_en(false);
gatewayTransportSend(buildGw(_msg, I_GATEWAY_READY).set("Gateway startup complete.")); gatewayTransportSend(buildGw(_msg, I_GATEWAY_READY).set("Gateway startup complete."));
_w5100_spi_en(true);
} }
} }
bool connected = clients[i].connected(); bool connected = clients[i].connected();
@@ -234,8 +267,10 @@ bool gatewayTransportAvailable()
} }
// Loop over clients connect and read available data // Loop over clients connect and read available data
for (uint8_t i = 0; i < ARRAY_SIZE(clients); i++) { for (uint8_t i = 0; i < ARRAY_SIZE(clients); i++) {
if (_readFromClient(i)) if (_readFromClient(i)) {
_w5100_spi_en(false);
return true; return true;
}
} }
#else #else
// W5100/ENC module does not have hasClient-method. We can only serve one client at the time. // W5100/ENC module does not have hasClient-method. We can only serve one client at the time.
@@ -246,6 +281,7 @@ bool gatewayTransportAvailable()
clients[0].stop(); clients[0].stop();
clients[0] = newclient; clients[0] = newclient;
debug(PSTR("Client connected\n")); debug(PSTR("Client connected\n"));
_w5100_spi_en(false);
gatewayTransportSend(buildGw(_msg, I_GATEWAY_READY).set("Gateway startup complete.")); gatewayTransportSend(buildGw(_msg, I_GATEWAY_READY).set("Gateway startup complete."));
} }
} }
@@ -254,12 +290,15 @@ bool gatewayTransportAvailable()
debug(PSTR("Client disconnected\n")); debug(PSTR("Client disconnected\n"));
clients[0].stop(); clients[0].stop();
} else { } else {
if (_readFromClient(0)) if (_readFromClient(0)) {
_w5100_spi_en(false);
return true; return true;
}
} }
} }
#endif #endif
#endif #endif
_w5100_spi_en(false);
return false; return false;
} }
@@ -286,13 +325,12 @@ void gatewayTransportRenewIP()
// http://playground.arduino.cc/Code/TimingRollover // http://playground.arduino.cc/Code/TimingRollover
if ((long)(now - next_time) < 0) if ((long)(now - next_time) < 0)
return; return;
if (Ethernet.maintain() & ~(0x06)) { if (Ethernet.maintain() & ~(0x06)) {
debug(PSTR("IP was not renewed correctly\n")); debug(PSTR("IP was not renewed correctly\n"));
/* Error occured -> IP was not renewed */ /* Error occured -> IP was not renewed */
return; return;
} }
_w5100_spi_en(false);
next_time = now + MY_IP_RENEWAL_INTERVAL; next_time = now + MY_IP_RENEWAL_INTERVAL;
} }
#endif /* IP_ADDRESS_DHCP */ #endif /* IP_ADDRESS_DHCP */
@@ -51,17 +51,22 @@
// Enable gateway ethernet module type // Enable gateway ethernet module type
#define MY_GATEWAY_W5100 #define MY_GATEWAY_W5100
// W5100 Ethernet module SPI enable (optional if using a shield/module that manages SPI_EN signal)
//#define MY_W5100_SPI_EN 4
// Enable Soft SPI for NRF radio (note different radio wiring is required) // Enable Soft SPI for NRF radio (note different radio wiring is required)
// The W5100 ethernet module seems to have a hard time co-operate with // The W5100 ethernet module seems to have a hard time co-operate with
// radio on the same spi bus. // radio on the same spi bus.
#define MY_SOFTSPI #if !defined(MY_W5100_SPI_EN)
#define MY_SOFT_SPI_SCK_PIN 14 #define MY_SOFTSPI
#define MY_SOFT_SPI_MISO_PIN 16 #define MY_SOFT_SPI_SCK_PIN 14
#define MY_SOFT_SPI_MOSI_PIN 15 #define MY_SOFT_SPI_MISO_PIN 16
#define MY_SOFT_SPI_MOSI_PIN 15
// When W5100 is connected we have to move CE/CSN pins for NRF radio
#define MY_RF24_CE_PIN 5 // When W5100 is connected we have to move CE/CSN pins for NRF radio
#define MY_RF24_CS_PIN 6 #define MY_RF24_CE_PIN 5
#define MY_RF24_CS_PIN 6
#endif
// Enable to UDP // Enable to UDP
//#define MY_USE_UDP //#define MY_USE_UDP
+1
View File
@@ -95,3 +95,4 @@ MY_ESP8266_SSID LITERAL1
MY_ESP8266_PASSWORD LITERAL1 MY_ESP8266_PASSWORD LITERAL1
MY_IP_GATEWAY_ADDRESS LITERAL1 MY_IP_GATEWAY_ADDRESS LITERAL1
MY_IP_SUBNET_ADDRESS LITERAL1 MY_IP_SUBNET_ADDRESS LITERAL1
MY_W5100_SPI_EN LITERAL1