Skip to content

Commit

Permalink
Add Spresense Support for Ethernet lib
Browse files Browse the repository at this point in the history
  • Loading branch information
kamtom480 committed Feb 20, 2019
1 parent 4f4ac17 commit f939d8d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/EthernetClient.cpp
Expand Up @@ -47,7 +47,7 @@ int EthernetClient::connect(IPAddress ip, uint16_t port)
}
sockindex = MAX_SOCK_NUM;
}
#if defined(ESP8266) || defined(ESP32)
#if defined(ESP8266) || defined(ESP32) || defined(ARDUINO_spresense_ast)
if (ip == IPAddress((uint32_t)0) || ip == IPAddress(0xFFFFFFFFul)) return 0;
#else
if (ip == IPAddress(0ul) || ip == IPAddress(0xFFFFFFFFul)) return 0;
Expand Down
59 changes: 59 additions & 0 deletions src/utility/w5100.cpp
Expand Up @@ -294,16 +294,29 @@ W5100Linkstatus W5100Class::getLinkStatus()

uint16_t W5100Class::write(uint16_t addr, const uint8_t *buf, uint16_t len)
{
#if defined(ARDUINO_spresense_ast)
uint8_t *cmd = (uint8_t *) malloc(4 + len);
#else
uint8_t cmd[8];
#endif

if (chip == 51) {
for (uint16_t i=0; i<len; i++) {
setSS();
#if defined(ARDUINO_spresense_ast)
cmd[0] = 0x0F;
cmd[1] = addr >> 8;
cmd[2] = addr & 0xFF;
cmd[3] = buf[i];
SPI.transfer(cmd, 4);
addr++;
#else
SPI.transfer(0xF0);
SPI.transfer(addr >> 8);
SPI.transfer(addr & 0xFF);
addr++;
SPI.transfer(buf[i]);
#endif
resetSS();
}
} else if (chip == 52) {
Expand All @@ -312,6 +325,10 @@ uint16_t W5100Class::write(uint16_t addr, const uint8_t *buf, uint16_t len)
cmd[1] = addr & 0xFF;
cmd[2] = ((len >> 8) & 0x7F) | 0x80;
cmd[3] = len & 0xFF;
#if defined(ARDUINO_spresense_ast)
memcpy(&cmd[4], buf, len);
SPI.transfer(cmd, 4 + len);
#else
SPI.transfer(cmd, 4);
#ifdef SPI_HAS_TRANSFER_BUF
SPI.transfer(buf, NULL, len);
Expand All @@ -320,6 +337,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();
} else { // chip == 55
Expand Down Expand Up @@ -368,6 +386,10 @@ uint16_t W5100Class::write(uint16_t addr, const uint8_t *buf, uint16_t len)
}
SPI.transfer(cmd, len + 3);
} else {
#if defined(ARDUINO_spresense_ast)
memcpy(&cmd[3], buf, len);
SPI.transfer(cmd, 3 + len);
#else
SPI.transfer(cmd, 3);
#ifdef SPI_HAS_TRANSFER_BUF
SPI.transfer(buf, NULL, len);
Expand All @@ -376,26 +398,46 @@ 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();
}

#if defined(ARDUINO_spresense_ast)
free(cmd);
#endif

return len;
}

uint16_t W5100Class::read(uint16_t addr, uint8_t *buf, uint16_t len)
{
#if defined(ARDUINO_spresense_ast)
uint8_t *cmd = (uint8_t *) malloc(4 + len);
#else
uint8_t cmd[4];
#endif

if (chip == 51) {
for (uint16_t i=0; i < len; i++) {
setSS();
#if 1
#if defined(ARDUINO_spresense_ast)
cmd[0] = 0x0F;
cmd[1] = addr >> 8;
cmd[2] = addr & 0xFF;
cmd[3] = 0;
SPI.transfer(cmd, 4);
buf[i] = cmd[3];
addr++;
#else
SPI.transfer(0x0F);
SPI.transfer(addr >> 8);
SPI.transfer(addr & 0xFF);
addr++;
buf[i] = SPI.transfer(0);
#endif
#else
cmd[0] = 0x0F;
cmd[1] = addr >> 8;
Expand All @@ -413,9 +455,15 @@ uint16_t W5100Class::read(uint16_t addr, uint8_t *buf, uint16_t len)
cmd[1] = addr & 0xFF;
cmd[2] = (len >> 8) & 0x7F;
cmd[3] = len & 0xFF;
#if defined(ARDUINO_spresense_ast)
memset(&cmd[4], 0, len);
SPI.transfer(cmd, 4 + len);
memcpy(buf, &cmd[4], len);
#else
SPI.transfer(cmd, 4);
memset(buf, 0, len);
SPI.transfer(buf, len);
#endif
resetSS();
} else { // chip == 55
setSS();
Expand Down Expand Up @@ -457,11 +505,22 @@ 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_spresense_ast)
memset(&cmd[3], 0, len);
SPI.transfer(cmd, 3 + len);
memcpy(buf, &cmd[3], len);
#else
SPI.transfer(cmd, 3);
memset(buf, 0, len);
SPI.transfer(buf, len);
#endif
resetSS();
}

#if defined(ARDUINO_spresense_ast)
free(cmd);
#endif

return len;
}

Expand Down
15 changes: 15 additions & 0 deletions src/utility/w5100.h
Expand Up @@ -48,6 +48,11 @@
#define SPI_ETHERNET_SETTINGS SPISettings(8000000, MSBFIRST, SPI_MODE0)
#endif

#if defined(ARDUINO_spresense_ast)
#undef SPI_ETHERNET_SETTINGS
#define SPI_ETHERNET_SETTINGS SPISettings(14000000, MSBFIRST, SPI_MODE3)
#endif


typedef uint8_t SOCKET;

Expand Down Expand Up @@ -432,6 +437,16 @@ class W5100Class {
inline static void resetSS() {
*(ss_pin_reg+6) = ss_pin_mask;
}
#elif defined(ARDUINO_spresense_ast)
inline static void initSS() {
return;
}
inline static void setSS() {
return;
}
inline static void resetSS() {
return;
}
#else
inline static void initSS() {
pinMode(ss_pin, OUTPUT);
Expand Down

0 comments on commit f939d8d

Please sign in to comment.