Skip to content

Commit 088d416

Browse files
me-no-devlucasssvazpre-commit-ci-lite[bot]
authored
feat(hosted): Add OTA and version functionality to esp-hosted (#11936)
* feat(hosted): Add OTA and version functionality to esp-hosted * feat(hosted): Add hostedActivateUpdate() function * feat(hosted): Add new BLE init/deinit API calls * fix(hosted): Apply suggestion from @lucasssvaz Co-authored-by: Lucas Saavedra Vaz <[email protected]> * fix(hosted): Apply suggestion from @lucasssvaz Co-authored-by: Lucas Saavedra Vaz <[email protected]> * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: Lucas Saavedra Vaz <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 7bfecf2 commit 088d416

File tree

2 files changed

+135
-3
lines changed

2 files changed

+135
-3
lines changed

cores/esp32/esp32-hal-hosted.c

Lines changed: 127 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
#include "esp32-hal-hosted.h"
1919
#include "esp32-hal-log.h"
2020

21+
#include "esp_hosted.h"
2122
#include "esp_hosted_transport_config.h"
22-
extern esp_err_t esp_hosted_init();
23-
extern esp_err_t esp_hosted_deinit();
23+
// extern esp_err_t esp_hosted_init();
24+
// extern esp_err_t esp_hosted_deinit();
2425

2526
static bool hosted_initialized = false;
2627
static bool hosted_ble_active = false;
@@ -46,6 +47,100 @@ static sdio_pin_config_t sdio_pin_config = {
4647
#endif
4748
};
4849

50+
static esp_hosted_coprocessor_fwver_t slave_version_struct = {.major1 = 0, .minor1 = 0, .patch1 = 0};
51+
static esp_hosted_coprocessor_fwver_t host_version_struct = {
52+
.major1 = ESP_HOSTED_VERSION_MAJOR_1, .minor1 = ESP_HOSTED_VERSION_MINOR_1, .patch1 = ESP_HOSTED_VERSION_PATCH_1
53+
};
54+
55+
void hostedGetHostVersion(uint32_t *major, uint32_t *minor, uint32_t *patch) {
56+
*major = host_version_struct.major1;
57+
*minor = host_version_struct.minor1;
58+
*patch = host_version_struct.patch1;
59+
}
60+
61+
void hostedGetSlaveVersion(uint32_t *major, uint32_t *minor, uint32_t *patch) {
62+
*major = slave_version_struct.major1;
63+
*minor = slave_version_struct.minor1;
64+
*patch = slave_version_struct.patch1;
65+
}
66+
67+
bool hostedHasUpdate() {
68+
uint32_t host_version = ESP_HOSTED_VERSION_VAL(host_version_struct.major1, host_version_struct.minor1, host_version_struct.patch1);
69+
uint32_t slave_version = 0;
70+
71+
esp_err_t ret = esp_hosted_get_coprocessor_fwversion(&slave_version_struct);
72+
if (ret != ESP_OK) {
73+
log_e("Could not get slave firmware version: %s", esp_err_to_name(ret));
74+
} else {
75+
slave_version = ESP_HOSTED_VERSION_VAL(slave_version_struct.major1, slave_version_struct.minor1, slave_version_struct.patch1);
76+
}
77+
78+
log_i("Host firmware version: %" PRIu32 ".%" PRIu32 ".%" PRIu32, host_version_struct.major1, host_version_struct.minor1, host_version_struct.patch1);
79+
log_i("Slave firmware version: %" PRIu32 ".%" PRIu32 ".%" PRIu32, slave_version_struct.major1, slave_version_struct.minor1, slave_version_struct.patch1);
80+
81+
// compare major.minor only
82+
// slave_version &= 0xFFFFFF00;
83+
// host_version &= 0xFFFFFF00;
84+
85+
if (host_version == slave_version) {
86+
log_i("Versions Match!");
87+
} else if (host_version > slave_version) {
88+
log_w("Version on Host is NEWER than version on co-processor");
89+
log_w("Update URL: %s", hostedGetUpdateURL());
90+
return true;
91+
} else {
92+
log_w("Version on Host is OLDER than version on co-processor");
93+
}
94+
return false;
95+
}
96+
97+
char *hostedGetUpdateURL() {
98+
// https://espressif.github.io/arduino-esp32/hosted/esp32c6-v1.2.3.bin
99+
static char url[92] = {0};
100+
snprintf(
101+
url, 92, "https://espressif.github.io/arduino-esp32/hosted/%s-v%" PRIu32 ".%" PRIu32 ".%" PRIu32 ".bin", CONFIG_ESP_HOSTED_IDF_SLAVE_TARGET,
102+
host_version_struct.major1, host_version_struct.minor1, host_version_struct.patch1
103+
);
104+
return url;
105+
}
106+
107+
bool hostedBeginUpdate() {
108+
esp_err_t err = esp_hosted_slave_ota_begin();
109+
if (err != ESP_OK) {
110+
log_e("Failed to begin Update: %s", esp_err_to_name(err));
111+
}
112+
return err == ESP_OK;
113+
}
114+
115+
bool hostedWriteUpdate(uint8_t *buf, uint32_t len) {
116+
esp_err_t err = esp_hosted_slave_ota_write(buf, len);
117+
if (err != ESP_OK) {
118+
log_e("Failed to write Update: %s", esp_err_to_name(err));
119+
}
120+
return err == ESP_OK;
121+
}
122+
123+
bool hostedEndUpdate() {
124+
esp_err_t err = esp_hosted_slave_ota_end();
125+
if (err != ESP_OK) {
126+
log_e("Failed to end Update: %s", esp_err_to_name(err));
127+
}
128+
return err == ESP_OK;
129+
}
130+
131+
bool hostedActivateUpdate() {
132+
esp_err_t err = esp_hosted_slave_ota_activate();
133+
if (err != ESP_OK) {
134+
log_e("Failed to activate Update: %s", esp_err_to_name(err));
135+
}
136+
// else {
137+
// hostedDeinit();
138+
// delay(1000);
139+
// hostedInit();
140+
// }
141+
return err == ESP_OK;
142+
}
143+
49144
static bool hostedInit() {
50145
if (!hosted_initialized) {
51146
log_i("Initializing ESP-Hosted");
@@ -69,6 +164,12 @@ static bool hostedInit() {
69164
return false;
70165
}
71166
log_i("ESP-Hosted initialized!");
167+
if (esp_hosted_connect_to_slave() != ESP_OK) {
168+
log_e("Failed to connect to slave");
169+
return false;
170+
}
171+
hostedHasUpdate();
172+
return true;
72173
}
73174

74175
// Attach pins to PeriMan here
@@ -101,8 +202,21 @@ static bool hostedDeinit() {
101202

102203
bool hostedInitBLE() {
103204
log_i("Initializing ESP-Hosted for BLE");
205+
if (!hostedInit()) {
206+
return false;
207+
}
208+
esp_err_t err = esp_hosted_bt_controller_init();
209+
if (err != ESP_OK) {
210+
log_e("esp_hosted_bt_controller_init failed: %s", esp_err_to_name(err));
211+
return false;
212+
}
213+
err = esp_hosted_bt_controller_enable();
214+
if (err != ESP_OK) {
215+
log_e("esp_hosted_bt_controller_enable failed: %s", esp_err_to_name(err));
216+
return false;
217+
}
104218
hosted_ble_active = true;
105-
return hostedInit();
219+
return true;
106220
}
107221

108222
bool hostedInitWiFi() {
@@ -113,6 +227,16 @@ bool hostedInitWiFi() {
113227

114228
bool hostedDeinitBLE() {
115229
log_i("Deinitializing ESP-Hosted for BLE");
230+
esp_err_t err = esp_hosted_bt_controller_disable();
231+
if (err != ESP_OK) {
232+
log_e("esp_hosted_bt_controller_disable failed: %s", esp_err_to_name(err));
233+
return false;
234+
}
235+
err = esp_hosted_bt_controller_deinit(false);
236+
if (err != ESP_OK) {
237+
log_e("esp_hosted_bt_controller_deinit failed: %s", esp_err_to_name(err));
238+
return false;
239+
}
116240
hosted_ble_active = false;
117241
if (!hosted_wifi_active) {
118242
return hostedDeinit();

cores/esp32/esp32-hal-hosted.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ bool hostedIsBLEActive();
4444
bool hostedIsWiFiActive();
4545
bool hostedSetPins(int8_t clk, int8_t cmd, int8_t d0, int8_t d1, int8_t d2, int8_t d3, int8_t rst);
4646
void hostedGetPins(int8_t *clk, int8_t *cmd, int8_t *d0, int8_t *d1, int8_t *d2, int8_t *d3, int8_t *rst);
47+
void hostedGetHostVersion(uint32_t *major, uint32_t *minor, uint32_t *patch);
48+
void hostedGetSlaveVersion(uint32_t *major, uint32_t *minor, uint32_t *patch);
49+
bool hostedHasUpdate();
50+
char *hostedGetUpdateURL();
51+
bool hostedBeginUpdate();
52+
bool hostedWriteUpdate(uint8_t *buf, uint32_t len);
53+
bool hostedEndUpdate();
54+
bool hostedActivateUpdate();
4755

4856
#ifdef __cplusplus
4957
}

0 commit comments

Comments
 (0)