Skip to content
Snippets Groups Projects
Commit a3e7d6d6 authored by aticu's avatar aticu
Browse files

Improved stability of the ESP32 driver

parent 9360df65
No related branches found
No related tags found
No related merge requests found
......@@ -116,6 +116,7 @@ enum esp32_result esp32_recv(void) {
// Checks whether the expected value and length was received, without performing a new receive operation.
enum esp32_result esp32_check_expectation(int how, const char *expected_value, uint32_t expected_len) {
if(strncmp(expected_value, recv_buf, min(expected_len, recv_len)) != 0 || recv_len != expected_len) {
#if VERBOSITY > 0
if(how == EXPECT_NOISY) {
printf("Got an unexpected reply from ESP32:\r\n");
......@@ -140,10 +141,13 @@ enum esp32_result esp32_check_expectation(int how, const char *expected_value, u
}
if(recv_buf[0] == '\r' && recv_buf[1] == '\n') {
printf(" Data (string): \\r\\n%s\r\n", &recv_buf[2]);
} else if(recv_len == 0) {
printf(" Data (string): <no data>\r\n");
} else {
printf(" Data (string): %s\r\n", recv_buf);
}
}
#endif
return ESP32_ERR;
}
......@@ -197,33 +201,27 @@ enum esp32_result esp32_connect_to_ap(const char *ssid, const char *pwd) {
// Join the AP
spi_send(join_msg);
// Check the result of the command
if(esp32_expect("WIFI CONNECTED\r\n", 16) == ESP32_ERR) {
// We expect three messages in return if everything goes well:
// - WIFI CONNECTED
// - WIFI GOT IP
// - OK
//
// These messages take some time to appear however, so we just try receiving messages until they arrive.
while(esp32_recv() == ESP32_ERR);
if(esp32_check_expectation(EXPECT_NOISY, "WIFI CONNECTED\r\n", 16) == ESP32_ERR) {
return ESP32_ERR;
}
// Check the status until we are connected.
do {
spi_send("AT+CIPSTATUS\r\n");
esp32_recv();
delay(DELAY);
} while(esp32_check_expectation(EXPECT_SILENT, MSG_BUSY) == ESP32_OK ||
esp32_check_expectation(EXPECT_SILENT, "ERR CODE:0x010b0000\r\n", 21) == ESP32_OK ||
esp32_check_expectation(EXPECT_SILENT, "WIFI GOT IP\r\n", 13) == ESP32_OK);
// At most one "OK" message can occur here, preceding the status
while(esp32_check_expectation(EXPECT_SILENT, MSG_OK) == ESP32_OK) {
esp32_recv();
while(esp32_recv() == ESP32_ERR);
if(esp32_check_expectation(EXPECT_NOISY, "WIFI GOT IP\r\n", 13) == ESP32_ERR) {
return ESP32_ERR;
}
// Check the status reports to ensure that we are connected
if(esp32_check_expectation(EXPECT_NOISY, "STATUS:2\r\n", 10) == ESP32_ERR) {
while(esp32_recv() == ESP32_ERR);
if(esp32_check_expectation(EXPECT_NOISY, MSG_OK) == ESP32_ERR) {
return ESP32_ERR;
}
// Soak up any extra messages generated by the status requests
while(esp32_recv() == ESP32_OK) {}
return ESP32_OK;
}
......@@ -358,7 +356,6 @@ enum esp32_event esp32_wait_for_event(uint32_t *id, uint32_t *len, char **data)
return ESP32_CLOSED_CONNECTION;
}
if(data != NULL) {
*data = buf;
}
......@@ -384,15 +381,18 @@ enum esp32_result esp32_send(uint32_t id, uint32_t len, char *data) {
// Actually send the data
spi_send_with_len(data, len);
esp32_recv();
if(strncmp(recv_buf, "\r\nRecv ", 7) != 0) {
printf("Unexpected message from the ESP32.\r\n");
return ESP32_ERR;
}
buf_len = 35;
char recv_msg_buf[buf_len];
snprintf(recv_msg_buf, buf_len, "\r\nRecv %d bytes\r\n", len);
esp32_expect(recv_msg_buf, strlen(recv_msg_buf));
if(esp32_expect("\r\nSEND OK\r\n", 11) == ESP32_ERR) {
// Usually the ESP32 will send a SEND OK message here, but there appear to be some situations in which
// that message is not sent, so we check it here, but don't error if it doesn't exist.
if(esp32_recv() == ESP32_OK) {
if(esp32_check_expectation(EXPECT_NOISY, "\r\nSEND OK\r\n", 11) == ESP32_ERR) {
return ESP32_ERR;
}
}
return ESP32_OK;
}
......@@ -404,9 +404,18 @@ enum esp32_result esp32_close_connection(uint32_t id) {
snprintf(send_msg, buf_len, "AT+CIPCLOSE=%d\r\n", id);
spi_send(send_msg);
if(esp32_expect(MSG_OK) == ESP32_ERR) {
// Sometimes the ESP32 sends no message here (but with extreme delay), so we also accept no message.
if(esp32_recv() == ESP32_OK) {
// After trying it a couple of times, it became apparent that sometimes an error is returned here.
// The connection will still appear to be closed on the ESP32 (at least the ID is reused).
// This is likely because the other party disconnected first.
// Because of this we accept both OK and ERROR messages here.
if(esp32_check_expectation(EXPECT_SILENT, MSG_OK) != ESP32_OK && esp32_check_expectation(EXPECT_SILENT, "\r\nERROR\r\n", 9) != ESP32_OK) {
// We did not get the expected message, now we just do a noisy comparison to report the error.
esp32_check_expectation(EXPECT_NOISY, MSG_OK);
return ESP32_ERR;
}
}
return ESP32_OK;
}
......
......@@ -110,7 +110,9 @@ void spi_init(uint32_t spi_clock)
// Enable IOF for pin 3,4,5,9 (don't touch other pins)
IOF_EN |= IOF_SPI_ENABLE;
#if VERBOSITY > 2
printf("DONE\r\n");
#endif
}
//----------------------------------------------------------------------
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment