Skip to content

Commit 99e010c

Browse files
committed
feat(hosted): Add OTA and version functionality to esp-hosted
1 parent 7a9a10c commit 99e010c

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

cores/esp32/esp32-hal-hosted.c

Lines changed: 94 additions & 2 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,91 @@ static sdio_pin_config_t sdio_pin_config = {
4647
#endif
4748
};
4849

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

74166
// Attach pins to PeriMan here

cores/esp32/esp32-hal-hosted.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ 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();
4754

4855
#ifdef __cplusplus
4956
}

0 commit comments

Comments
 (0)