Skip to content

Commit 6022437

Browse files
committed
add ota support for esp32s2
1 parent fc23a0c commit 6022437

File tree

3 files changed

+110
-4
lines changed

3 files changed

+110
-4
lines changed

locale/circuitpython.pot

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2020-11-27 23:57-0500\n"
11+
"POT-Creation-Date: 2020-12-08 10:30+0530\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -498,8 +498,8 @@ msgstr ""
498498
msgid "Buffer must be a multiple of 512 bytes"
499499
msgstr ""
500500

501-
#: shared-bindings/bitbangio/I2C.c shared-bindings/busdevice/I2CDevice.c
502-
#: shared-bindings/busio/I2C.c
501+
#: shared-bindings/adafruit_bus_device/I2CDevice.c
502+
#: shared-bindings/bitbangio/I2C.c shared-bindings/busio/I2C.c
503503
msgid "Buffer must be at least length 1"
504504
msgstr ""
505505

@@ -1296,7 +1296,7 @@ msgstr ""
12961296
msgid "No DMA channel found"
12971297
msgstr ""
12981298

1299-
#: shared-module/busdevice/I2CDevice.c
1299+
#: shared-module/adafruit_bus_device/I2CDevice.c
13001300
#, c-format
13011301
msgid "No I2C device at address: %x"
13021302
msgstr ""
@@ -1420,6 +1420,10 @@ msgstr ""
14201420
msgid "Not settable"
14211421
msgstr ""
14221422

1423+
#: ports/esp32s2/common-hal/ota/__init__.c
1424+
msgid "OTA Update Failed"
1425+
msgstr ""
1426+
14231427
#: shared-bindings/util.c
14241428
msgid ""
14251429
"Object has been deinitialized and can no longer be used. Create a new object."
@@ -1503,6 +1507,7 @@ msgstr ""
15031507
msgid "Pin does not have ADC capabilities"
15041508
msgstr ""
15051509

1510+
#: shared-bindings/adafruit_bus_device/SPIDevice.c
15061511
#: shared-bindings/digitalio/DigitalInOut.c
15071512
msgid "Pin is input only"
15081513
msgstr ""

ports/esp32s2/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ INC += -isystem esp-idf/components/heap/include
106106
INC += -isystem esp-idf/components/esp_system/include
107107
INC += -isystem esp-idf/components/spi_flash/include
108108
INC += -isystem esp-idf/components/nvs_flash/include
109+
INC += -isystem esp-idf/components/app_update/include
110+
INC += -isystem esp-idf/components/bootloader_support/include
109111
INC += -I$(BUILD)/esp-idf/config
110112

111113
CFLAGS += -DHAVE_CONFIG_H \

ports/esp32s2/common-hal/ota/__init__.c

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,105 @@
2626

2727
#include "shared-bindings/ota/__init__.h"
2828

29+
#include <string.h>
30+
31+
#include "esp_log.h"
32+
#include "esp_ota_ops.h"
33+
34+
static const char *TAG = "OTA";
35+
36+
static void __attribute__((noreturn)) task_fatal_error(void) {
37+
ESP_LOGE(TAG, "Exiting task due to fatal error...");
38+
mp_raise_RuntimeError(translate("OTA Update Failed"));
39+
}
40+
2941
void common_hal_ota_flash(const void *buf, const size_t len) {
42+
esp_err_t err;
43+
/* update handle : set by esp_ota_begin(), must be freed via esp_ota_end() */
44+
esp_ota_handle_t update_handle = 0 ;
45+
const esp_partition_t *update_partition = NULL;
46+
47+
ESP_LOGI(TAG, "Starting update");
48+
49+
const esp_partition_t *configured = esp_ota_get_boot_partition();
50+
const esp_partition_t *running = esp_ota_get_running_partition();
51+
52+
if (configured != running) {
53+
ESP_LOGW(TAG, "Configured OTA boot partition at offset 0x%08x, but running from offset 0x%08x",
54+
configured->address, running->address);
55+
ESP_LOGW(TAG, "(This can happen if either the OTA boot data or preferred boot image become corrupted somehow.)");
56+
}
57+
ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)",
58+
running->type, running->subtype, running->address);
59+
60+
update_partition = esp_ota_get_next_update_partition(NULL);
61+
ESP_LOGI(TAG, "Writing partition type %d subtype %d (offset 0x%08x)\n",
62+
update_partition->type, update_partition->subtype, update_partition->address);
63+
assert(update_partition != NULL);
64+
65+
66+
if (len > sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t)) {
67+
// check current version with downloading
68+
esp_app_desc_t new_app_info;
69+
memcpy(&new_app_info, &((char *)buf)[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t));
70+
ESP_LOGI(TAG, "New firmware version: %s", new_app_info.version);
71+
72+
esp_app_desc_t running_app_info;
73+
if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) {
74+
ESP_LOGI(TAG, "Running firmware version: %s", running_app_info.version);
75+
}
76+
77+
const esp_partition_t* last_invalid_app = esp_ota_get_last_invalid_partition();
78+
esp_app_desc_t invalid_app_info;
79+
if (esp_ota_get_partition_description(last_invalid_app, &invalid_app_info) == ESP_OK) {
80+
ESP_LOGI(TAG, "Last invalid firmware version: %s", invalid_app_info.version);
81+
}
82+
83+
// check current version with last invalid partition
84+
if (last_invalid_app != NULL) {
85+
if (memcmp(invalid_app_info.version, new_app_info.version, sizeof(new_app_info.version)) == 0) {
86+
ESP_LOGW(TAG, "New version is the same as invalid version.");
87+
ESP_LOGW(TAG, "Previously, there was an attempt to launch the firmware with %s version, but it failed.", invalid_app_info.version);
88+
ESP_LOGW(TAG, "The firmware has been rolled back to the previous version.");
89+
task_fatal_error();
90+
}
91+
}
92+
93+
if (memcmp(new_app_info.version, running_app_info.version, sizeof(new_app_info.version)) == 0) {
94+
ESP_LOGW(TAG, "Current running version is the same as a new. We will not continue the update.");
95+
task_fatal_error();
96+
}
97+
98+
err = esp_ota_begin(update_partition, OTA_WITH_SEQUENTIAL_WRITES, &update_handle);
99+
if (err != ESP_OK) {
100+
ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
101+
task_fatal_error();
102+
}
103+
ESP_LOGI(TAG, "esp_ota_begin succeeded");
104+
} else {
105+
ESP_LOGE(TAG, "received package is not fit len");
106+
task_fatal_error();
107+
}
108+
109+
err = esp_ota_write( update_handle, buf, len);
110+
if (err != ESP_OK) {
111+
ESP_LOGE(TAG, "esp_ota_write failed (%s)", esp_err_to_name(err));
112+
task_fatal_error();
113+
}
114+
ESP_LOGI(TAG, "Total Write binary data length: %d", len);
115+
116+
err = esp_ota_end(update_handle);
117+
if (err != ESP_OK) {
118+
if (err == ESP_ERR_OTA_VALIDATE_FAILED) {
119+
ESP_LOGE(TAG, "Image validation failed, image is corrupted");
120+
}
121+
ESP_LOGE(TAG, "esp_ota_end failed (%s)!", esp_err_to_name(err));
122+
task_fatal_error();
123+
}
30124

125+
err = esp_ota_set_boot_partition(update_partition);
126+
if (err != ESP_OK) {
127+
ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err));
128+
task_fatal_error();
129+
}
31130
}

0 commit comments

Comments
 (0)