I updated the code to pass SPISettings and controlling of the CS line, but I know this is not the issue because sometimes it never makes it past the SPI.beginTransaction call without deadlocking. There is something happening on the ethernet driver that I can't quite figure out....
Here is updated code:
// SAMPLE APPLICATION
#include "Particle.h"
SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(SEMI_AUTOMATIC);
// Serial1LogHandler log1Handler(115200, LOG_LEVEL_ALL);
SerialLogHandler log1Handler(LOG_LEVEL_TRACE, {{"app", LOG_LEVEL_TRACE}});
retained uint8_t resetRetry = 0;
static uint8_t tx_buffer[5] = {0x00, 0x01, 0x02, 0x03, 0x04};
static uint8_t rx_buffer[5];
void setup() {
Serial.begin(9600);
waitFor(Serial.isConnected, 15000);
Serial.println("STARTING");
pinMode(D18, OUTPUT);
digitalWrite(D18, HIGH);
WiFi.clearCredentials();
WiFi.off();
#if HAL_PLATFORM_WIFI && !HAL_PLATFORM_WIFI_SCAN_ONLY
// To force Ethernet only, clear Wi-Fi credentials
Log.info("Clear Wi-Fi credentionals...");
WiFi.clearCredentials();
#endif // HAL_PLATFORM_WIFI && !HAL_PLATFORM_WIFI_SCAN_ONLY
// Disable Listening Mode if not required
if (System.featureEnabled(FEATURE_DISABLE_LISTENING_MODE)) {
Log.info("FEATURE_DISABLE_LISTENING_MODE enabled");
} else {
Log.info("Disabling Listening Mode...");
System.enableFeature(FEATURE_DISABLE_LISTENING_MODE);
}
Log.info("Checking if Ethernet is on...");
if (Ethernet.isOn()) {
Log.info("Ethernet is on");
uint8_t macAddrBuf[8] = {};
uint8_t* macAddr = Ethernet.macAddress(macAddrBuf);
if (macAddr != nullptr) {
Log.info("Ethernet MAC: %02x %02x %02x %02x %02x %02x",
macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]);
}
Ethernet.connect();
waitFor(Ethernet.ready, 30000);
Log.info("Ethernet.ready: %d", Ethernet.ready());
resetRetry = 0;
} else if (++resetRetry <= 3) {
Log.info("Ethernet is off or not detected, attmpting to remap pins: %d/3", resetRetry);
if_wiznet_pin_remap remap = {};
remap.base.type = IF_WIZNET_DRIVER_SPECIFIC_PIN_REMAP;
remap.cs_pin = D5;
remap.reset_pin = D3;
remap.int_pin = D4;
auto ret = if_request(nullptr, IF_REQ_DRIVER_SPECIFIC, &remap, sizeof(remap), nullptr);
if (ret != SYSTEM_ERROR_NONE) {
Log.error("Ethernet GPIO config error: %d", ret);
} else {
if (System.featureEnabled(FEATURE_ETHERNET_DETECTION)) {
Log.info("FEATURE_ETHERNET_DETECTION enabled");
} else {
Log.info("Enabling Ethernet...");
System.enableFeature(FEATURE_ETHERNET_DETECTION);
}
delay(500);
System.reset();
}
}
Particle.connect();
waitFor(Particle.connected, 100000);
}
void loop() {
static system_tick_t lastPublish = millis();
static int count = 0;
static bool reconnect = false;
if (Particle.connected()) {
if (millis() - lastPublish >= 10000UL) {
count++;
Particle.publish("mytest", String(count), PRIVATE, WITH_ACK);
lastPublish = millis();
Log.info("Sent mytest %d", count);
SPI.beginTransaction(SPISettings(4*MHZ, MSBFIRST, SPI_MODE0));
digitalWrite(D18, LOW);
SPI.transfer(0x00);
SPI.transfer(0x01);
SPI.transfer(0x02);
SPI.transfer(0x03);
digitalWrite(D18, HIGH);
SPI.endTransaction();
}
}
}
Also, @erik.fasnacht here is the logic analyzer capture:
When the deadlock happens MOSI stays high and CLK stay high and MISO stays low. The deadlock happens before I set my CS (D18) low
Sometimes it makes it through 1-3 loops before deadlocking....
Here is the log trace messages (before deadlocking):
0000105397 [system] INFO: All handshake messages have been processed
0000105527 [comm.coap] TRACE: Received CoAP message
0000105547 [comm.coap] TRACE: CON POST /E/particle/device/updates/pending size=47 token=02 id=35785
0000105589 [comm.coap] TRACE: Sending CoAP message
0000105609 [comm.coap] TRACE: ACK 0.00 size=4 token= id=35785
0000105742 [system] INFO: Cloud connected
0000105810 [comm.coap] TRACE: Sending CoAP message
0000105823 [comm.coap] TRACE: NON POST /E/mytest size=15 token= id=179
0000105853 [app] INFO: Sent mytest 1
0000115853 [comm.coap] TRACE: Sending CoAP message
0000115866 [comm.coap] TRACE: NON POST /E/mytest size=15 token= id=180
0000115896 [app] INFO: Sent mytest 2
0000125906 [comm.coap] TRACE: Sending CoAP message
0000126088 [comm.coap] TRACE: NON POST /E/mytest size=15 token= id=181
0000126354 [app] INFO: Sent mytest 3