Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Porting for SPRESENSE board #1

Merged
merged 1 commit into from Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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_ARCH_SPRESENSE)
if (ip == IPAddress((uint32_t)0) || ip == IPAddress(0xFFFFFFFFul)) return 0;
#else
if (ip == IPAddress(0ul) || ip == IPAddress(0xFFFFFFFFul)) return 0;
Expand Down
61 changes: 61 additions & 0 deletions src/utility/w5100.cpp
Expand Up @@ -299,11 +299,20 @@ uint16_t W5100Class::write(uint16_t addr, const uint8_t *buf, uint16_t len)
if (chip == 51) {
for (uint16_t i=0; i<len; i++) {
setSS();
#ifdef ARDUINO_ARCH_SPRESENSE
cmd[0] = 0xF0;
cmd[1] = addr >> 8;
cmd[2] = addr & 0xFF;
cmd[3] = buf[i];
SPI.transfer(cmd, 4);
addr++;
#else /* !ARDUINO_ARCH_SPRESENSE */
SPI.transfer(0xF0);
SPI.transfer(addr >> 8);
SPI.transfer(addr & 0xFF);
addr++;
SPI.transfer(buf[i]);
#endif /* ARDUINO_ARCH_SPRESENSE */
resetSS();
}
} else if (chip == 52) {
Expand All @@ -312,6 +321,15 @@ 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;
#ifdef ARDUINO_ARCH_SPRESENSE
uint8_t *txbuf = (uint8_t*)malloc(4 + len);
if (txbuf) {
memcpy(txbuf, cmd, 4);
memcpy(txbuf + 4, buf, len);
SPI.transfer(txbuf, 4 + len);
free(txbuf);
}
#else /* !ARDUINO_ARCH_SPRESENSE */
SPI.transfer(cmd, 4);
#ifdef SPI_HAS_TRANSFER_BUF
SPI.transfer(buf, NULL, len);
Expand All @@ -321,6 +339,7 @@ uint16_t W5100Class::write(uint16_t addr, const uint8_t *buf, uint16_t len)
SPI.transfer(buf[i]);
}
#endif
#endif /* ARDUINO_ARCH_SPRESENSE */
resetSS();
} else { // chip == 55
setSS();
Expand Down Expand Up @@ -368,6 +387,15 @@ uint16_t W5100Class::write(uint16_t addr, const uint8_t *buf, uint16_t len)
}
SPI.transfer(cmd, len + 3);
} else {
#ifdef ARDUINO_ARCH_SPRESENSE
uint8_t *txbuf = (uint8_t*)malloc(3 + len);
if (txbuf) {
memcpy(txbuf, cmd, 3);
memcpy(txbuf + 3, buf, len);
SPI.transfer(txbuf, 3 + len);
free(txbuf);
}
#else /* !ARDUINO_ARCH_SPRESENSE */
SPI.transfer(cmd, 3);
#ifdef SPI_HAS_TRANSFER_BUF
SPI.transfer(buf, NULL, len);
Expand All @@ -377,6 +405,7 @@ uint16_t W5100Class::write(uint16_t addr, const uint8_t *buf, uint16_t len)
SPI.transfer(buf[i]);
}
#endif
#endif /* ARDUINO_ARCH_SPRESENSE */
}
resetSS();
}
Expand All @@ -390,6 +419,15 @@ uint16_t W5100Class::read(uint16_t addr, uint8_t *buf, uint16_t len)
if (chip == 51) {
for (uint16_t i=0; i < len; i++) {
setSS();
#ifdef ARDUINO_ARCH_SPRESENSE
cmd[0] = 0x0F;
cmd[1] = addr >> 8;
cmd[2] = addr & 0xFF;
addr++;
cmd[3] = 0;
SPI.transfer(cmd, 4);
buf[i] = cmd[3];
#else /* !ARDUINO_ARCH_SPRESENSE */
#if 1
SPI.transfer(0x0F);
SPI.transfer(addr >> 8);
Expand All @@ -405,6 +443,7 @@ uint16_t W5100Class::read(uint16_t addr, uint8_t *buf, uint16_t len)
buf[i] = cmd[3];
addr++;
#endif
#endif /* ARDUINO_ARCH_SPRESENSE */
resetSS();
}
} else if (chip == 52) {
Expand All @@ -413,9 +452,20 @@ 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;
#ifdef ARDUINO_ARCH_SPRESENSE
uint8_t *rxbuf = (uint8_t*)malloc(4 + len);
if (rxbuf) {
memcpy(rxbuf, cmd, 4);
memset(rxbuf + 4, 0, len);
SPI.transfer(rxbuf, 4 + len);
memcpy(buf, rxbuf + 4, len);
free(rxbuf);
}
#else /* !ARDUINO_ARCH_SPRESENSE */
SPI.transfer(cmd, 4);
memset(buf, 0, len);
SPI.transfer(buf, len);
#endif /* ARDUINO_ARCH_SPRESENSE */
resetSS();
} else { // chip == 55
setSS();
Expand Down Expand Up @@ -457,9 +507,20 @@ uint16_t W5100Class::read(uint16_t addr, uint8_t *buf, uint16_t len)
cmd[2] = ((addr >> 6) & 0xE0) | 0x18; // 2K buffers
#endif
}
#ifdef ARDUINO_ARCH_SPRESENSE
uint8_t *rxbuf = (uint8_t*)malloc(3 + len);
if (rxbuf) {
memcpy(rxbuf, cmd, 3);
memset(rxbuf + 3, 0, len);
SPI.transfer(rxbuf, 3 + len);
memcpy(buf, rxbuf + 3, len);
free(rxbuf);
}
#else /* !ARDUINO_ARCH_SPRESENSE */
SPI.transfer(cmd, 3);
memset(buf, 0, len);
SPI.transfer(buf, len);
#endif /* ARDUINO_ARCH_SPRESENSE */
resetSS();
}
return len;
Expand Down
20 changes: 20 additions & 0 deletions src/utility/w5100.h
Expand Up @@ -48,6 +48,11 @@
#define SPI_ETHERNET_SETTINGS SPISettings(8000000, MSBFIRST, SPI_MODE0)
#endif

// Spresense's SPI can uses SPI_MODE3 with auto cs control by hardware.
#if defined(ARDUINO_ARCH_SPRESENSE)
#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,21 @@ class W5100Class {
inline static void resetSS() {
*(ss_pin_reg+6) = ss_pin_mask;
}

#elif defined(ARDUINO_ARCH_SPRESENSE)
inline static void initSS() {
if (ss_pin != 10)
pinMode(ss_pin, OUTPUT);
}
inline static void setSS() {
if (ss_pin != 10)
digitalWrite(ss_pin, LOW);
}
inline static void resetSS() {
if (ss_pin != 10)
digitalWrite(ss_pin, HIGH);
}

#else
inline static void initSS() {
pinMode(ss_pin, OUTPUT);
Expand Down