Skip to content

A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol

fix spi slave

Geo6ce7afcaa352
diff --git a/src/Controllers/SpiController.cpp b/src/Controllers/SpiController.cpp
index 5ab4638c..7bfc0932 100644
--- a/src/Controllers/SpiController.cpp
+++ b/src/Controllers/SpiController.cpp
@@ -70,10 +70,13 @@ void SpiController::handleEeprom(const TerminalCommand& cmd) {
Slave
*/
void SpiController::handleSlave() {
-#ifdef DEVICE_M5STICK
+ #ifdef DEVICE_M5STICK
+
terminalView.println("SPI Slave: Not supported on M5Stick devices due to shared SPI bus.");
return;
-#endif
+
+ #endif
+
spiService.end(); // Stop master mode if active
int sclk = state.getSpiCLKPin();
@@ -105,12 +108,9 @@ void SpiController::handleSlave() {
}
terminalView.println(ss.str());
}
- delay(1);
}
-
spiService.stopSlave(sclk, miso, mosi, cs);
- spiService.configure(mosi, miso, sclk, cs, state.getSpiFrequency()); // Reconfigure master
- terminalView.println("SPI Slave: Cancelled by user.");
+ terminalView.println("\nSPI Slave: Stopped by user.\n");
}
/*
diff --git a/src/Services/SpiService.cpp b/src/Services/SpiService.cpp
index f0483681..c756c376 100644
--- a/src/Services/SpiService.cpp
+++ b/src/Services/SpiService.cpp
@@ -255,12 +255,6 @@ void IRAM_ATTR slaveTransactionCallback(spi_slave_transaction_t* trans, void* ar
// Limit
if (slaveBuffer.size() > 100) slaveBuffer.pop_front();
}
-
- // Relaunch
- if (slave) {
- spiSlave.queue(slave_tx_buf, slave_rx_buf, SLAVE_BUFFER_SIZE);
- spiSlave.trigger();
- }
}
void SpiService::startSlave(int sclk, int miso, int mosi, int cs) {
@@ -273,19 +267,21 @@ void SpiService::startSlave(int sclk, int miso, int mosi, int cs) {
spiSlave.setUserPostTransCbAndArg(slaveTransactionCallback, nullptr);
spiSlave.begin(FSPI, sclk, miso, mosi, cs);
slaveConfigured = true;
+
+ // First launch then it loops until slave is true
+ spiSlave.queue(slave_tx_buf, slave_rx_buf, SLAVE_BUFFER_SIZE);
+ spiSlave.trigger();
}
- // First launch then it loops until slave is true
- spiSlave.queue(slave_tx_buf, slave_rx_buf, SLAVE_BUFFER_SIZE);
- spiSlave.trigger();
}
void SpiService::stopSlave(int sclk, int miso, int mosi, int cs) {
slave = false;
+ delay(100);
slaveBuffer.clear();
memset(slave_rx_buf, 0, SLAVE_BUFFER_SIZE);
memset(slave_tx_buf, 0, SLAVE_BUFFER_SIZE);
- //Calling spiSlave.end() and then reallocate it cause crashes
+ // spiSlave.end(); // and then reallocate it cause crashes
}
bool SpiService::isSlave() const {
@@ -293,10 +289,23 @@ bool SpiService::isSlave() const {
}
std::vector<std::vector<uint8_t>> SpiService::getSlaveData() {
- std::lock_guard<std::mutex> lock(slaveBufferMutex);
- std::vector<std::vector<uint8_t>> data(slaveBuffer.begin(), slaveBuffer.end());
- slaveBuffer.clear();
- return data;
+ std::vector<std::vector<uint8_t>> out;
+
+ if (spiSlave.hasTransactionsCompletedAndAllResultsReady(1)) {
+ size_t receivedBytes = spiSlave.numBytesReceived();
+ if (receivedBytes > SLAVE_BUFFER_SIZE) receivedBytes = SLAVE_BUFFER_SIZE;
+
+ if (receivedBytes > 0) {
+ out.emplace_back(slave_rx_buf, slave_rx_buf + receivedBytes);
+ }
+
+ // relaunch
+ memset(slave_rx_buf, 0, SLAVE_BUFFER_SIZE);
+ spiSlave.queue(/*tx*/nullptr, /*rx*/slave_rx_buf, SLAVE_BUFFER_SIZE);
+ spiSlave.trigger();
+ }
+
+ return out;
}
// #### EEPROM ######