1717
1818#include "esp32-hal-hosted.h"
1919#include "esp32-hal-log.h"
20+ #include "esp32-hal.h"
2021#include "pins_arduino.h"
2122
2223#include "esp_hosted.h"
@@ -53,6 +54,9 @@ static esp_hosted_coprocessor_fwver_t host_version_struct = {
5354 .major1 = ESP_HOSTED_VERSION_MAJOR_1 , .minor1 = ESP_HOSTED_VERSION_MINOR_1 , .patch1 = ESP_HOSTED_VERSION_PATCH_1
5455};
5556
57+ static bool hostedInit ();
58+ static bool hostedDeinit ();
59+
5660void hostedGetHostVersion (uint32_t * major , uint32_t * minor , uint32_t * patch ) {
5761 * major = host_version_struct .major1 ;
5862 * minor = host_version_struct .minor1 ;
@@ -66,6 +70,11 @@ void hostedGetSlaveVersion(uint32_t *major, uint32_t *minor, uint32_t *patch) {
6670}
6771
6872bool hostedHasUpdate () {
73+ if (!hosted_initialized ) {
74+ log_e ("ESP-Hosted is not initialized" );
75+ return false;
76+ }
77+
6978 uint32_t host_version = ESP_HOSTED_VERSION_VAL (host_version_struct .major1 , host_version_struct .minor1 , host_version_struct .patch1 );
7079 uint32_t slave_version = 0 ;
7180
@@ -106,6 +115,11 @@ char *hostedGetUpdateURL() {
106115}
107116
108117bool hostedBeginUpdate () {
118+ if (!hosted_initialized ) {
119+ log_e ("ESP-Hosted is not initialized" );
120+ return false;
121+ }
122+
109123 esp_err_t err = esp_hosted_slave_ota_begin ();
110124 if (err != ESP_OK ) {
111125 log_e ("Failed to begin Update: %s" , esp_err_to_name (err ));
@@ -114,6 +128,11 @@ bool hostedBeginUpdate() {
114128}
115129
116130bool hostedWriteUpdate (uint8_t * buf , uint32_t len ) {
131+ if (!hosted_initialized ) {
132+ log_e ("ESP-Hosted is not initialized" );
133+ return false;
134+ }
135+
117136 esp_err_t err = esp_hosted_slave_ota_write (buf , len );
118137 if (err != ESP_OK ) {
119138 log_e ("Failed to write Update: %s" , esp_err_to_name (err ));
@@ -122,6 +141,11 @@ bool hostedWriteUpdate(uint8_t *buf, uint32_t len) {
122141}
123142
124143bool hostedEndUpdate () {
144+ if (!hosted_initialized ) {
145+ log_e ("ESP-Hosted is not initialized" );
146+ return false;
147+ }
148+
125149 esp_err_t err = esp_hosted_slave_ota_end ();
126150 if (err != ESP_OK ) {
127151 log_e ("Failed to end Update: %s" , esp_err_to_name (err ));
@@ -130,16 +154,31 @@ bool hostedEndUpdate() {
130154}
131155
132156bool hostedActivateUpdate () {
157+ if (!hosted_initialized ) {
158+ log_e ("ESP-Hosted is not initialized" );
159+ return false;
160+ }
161+
162+ // Activate can fail on older firmwares and that is not critical
163+ uint32_t slave_version = ESP_HOSTED_VERSION_VAL (slave_version_struct .major1 , slave_version_struct .minor1 , slave_version_struct .patch1 );
164+ uint32_t min_version = ESP_HOSTED_VERSION_VAL (2 , 6 , 0 );
165+
166+ if (slave_version < min_version ) {
167+ // Silence messages caused by earlier versions
168+ esp_log_level_set ("rpc_core" , ESP_LOG_NONE );
169+ }
170+
133171 esp_err_t err = esp_hosted_slave_ota_activate ();
134- if (err != ESP_OK ) {
172+
173+ // Any further communication will result in logged errors
174+ esp_log_level_set ("sdmmc_io" , ESP_LOG_NONE );
175+ esp_log_level_set ("H_SDIO_DRV" , ESP_LOG_NONE );
176+
177+ if (err != ESP_OK && slave_version >= min_version ) {
135178 log_e ("Failed to activate Update: %s" , esp_err_to_name (err ));
179+ return false;
136180 }
137- // else {
138- // hostedDeinit();
139- // delay(1000);
140- // hostedInit();
141- // }
142- return err == ESP_OK ;
181+ return true;
143182}
144183
145184static bool hostedInit () {
@@ -158,15 +197,22 @@ static bool hostedInit() {
158197 conf .pin_d2 .pin = sdio_pin_config .pin_d2 ;
159198 conf .pin_d3 .pin = sdio_pin_config .pin_d3 ;
160199 conf .pin_reset .pin = sdio_pin_config .pin_reset ;
161- // esp_hosted_sdio_set_config() will fail on second attempt but here temporarily to not cause exception on reinit
162- if (esp_hosted_sdio_set_config (& conf ) != ESP_OK || esp_hosted_init () != ESP_OK ) {
163- log_e ("esp_hosted_init failed!" );
200+ esp_err_t err = esp_hosted_sdio_set_config (& conf );
201+ if (err != ESP_OK ) { //&& err != ESP_ERR_NOT_ALLOWED) { // uncomment when second init is fixed
202+ log_e ("esp_hosted_sdio_set_config failed: %s" , esp_err_to_name (err ));
203+ return false;
204+ }
205+ err = esp_hosted_init ();
206+ if (err != ESP_OK ) {
207+ log_e ("esp_hosted_init failed: %s" , esp_err_to_name (err ));
164208 hosted_initialized = false;
165209 return false;
166210 }
167211 log_i ("ESP-Hosted initialized!" );
168- if (esp_hosted_connect_to_slave () != ESP_OK ) {
169- log_e ("Failed to connect to slave" );
212+ err = esp_hosted_connect_to_slave ();
213+ if (err != ESP_OK ) {
214+ log_e ("esp_hosted_connect_to_slave failed: %s" , esp_err_to_name (err ));
215+ hosted_initialized = false;
170216 return false;
171217 }
172218 hostedHasUpdate ();
0 commit comments