Skip to content

Commit

Permalink
lib: update OTA loader to a working state
Browse files Browse the repository at this point in the history
  • Loading branch information
and3rson committed Feb 18, 2024
1 parent 4a3ad8a commit b66aeaf
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 46 deletions.
29 changes: 7 additions & 22 deletions firmware-doom/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,14 @@
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:v1]
platform = espressif32
board = adafruit_qtpy_esp32c3
framework = arduino
lib_deps =
; https://github.com/moononournation/arduino-nofrendo.git
https://github.com/moononournation/Arduino_GFX.git
olikraus/U8g2@^2.35.9
; moononournation/GFX Library for [email protected]
build_flags = -D LILKA_VERSION=1 -Wno-discarded-qualifiers
lib_extra_dirs = ../lib
[platformio]
boards_dir = ../boards
lib_dir = ../lib

[env:v2]
platform = espressif32
board = adafruit_metro_esp32s3
; board = lolin_s3
board = lilka_v2
framework = arduino
lib_deps =
; https://github.com/moononournation/arduino-nofrendo.git
https://github.com/moononournation/Arduino_GFX.git
olikraus/U8g2@^2.35.9
; moononournation/GFX Library for [email protected]
build_flags = -D LILKA_VERSION=2 -Wno-discarded-qualifiers
build_flags = -D LILKA_VERSION=2 -Wno-discarded-qualifiers -D CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY=1
lib_extra_dirs = ../lib
board_build.partitions = doom.csv
lib_extra_dirs = ./lib
; Define LILKA_BREADBOARD to lower SPI speed to 40 MHz (instead of normal 80)
build_flags = -D LILKA_BREADBOARD
2 changes: 1 addition & 1 deletion lib/lilka/src/lilka.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ namespace lilka {

void begin() {
serial_begin();
loader.begin();
spi_begin();
buzzer.begin(); // Play notification sound
display.begin(); // Must be initialized BEFORE SD card
sdcard.begin();
controller.begin(); // Must be initialized AFTER SD card (since SD card somehow messes with GPIO)
filesystem.begin();
battery.begin();
loader.begin();
// TODO: I2S
esp_wifi_deinit();
}
Expand Down
4 changes: 4 additions & 0 deletions lib/lilka/src/lilka/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# define LILKA_GPIO_START 10
# define LILKA_GPIO_A -1
# define LILKA_GPIO_B -1
# define LILKA_GPIO_C -1
# define LILKA_GPIO_D -1

# define LILKA_SPI_SCK 0
# define LILKA_SPI_MOSI 1
Expand All @@ -38,6 +40,8 @@
# define LILKA_GPIO_START 10
# define LILKA_GPIO_A 20
# define LILKA_GPIO_B 21
# define LILKA_GPIO_C -1
# define LILKA_GPIO_D -1

# define LILKA_DISPLAY_DC 2
# define LILKA_DISPLAY_CS 3
Expand Down
12 changes: 10 additions & 2 deletions lib/lilka/src/lilka/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void IRAM_ATTR Controller::handle_interrupt(int stateIndex) {
if (_seq[_seqIndex++] == stateIndex) {
if (_seqIndex == 10) {
_seqIndex = 0;
serial_log("do the barrel roll!");
// serial_log("do the barrel roll!");
}
} else {
_seqIndex = 0;
Expand Down Expand Up @@ -73,6 +73,14 @@ void IRAM_ATTR Controller::on_b() {
_instance->handle_interrupt(Button::B);
}

void IRAM_ATTR Controller::on_c() {
_instance->handle_interrupt(Button::C);
}

void IRAM_ATTR Controller::on_d() {
_instance->handle_interrupt(Button::D);
}

void IRAM_ATTR Controller::on_select() {
_instance->handle_interrupt(Button::SELECT);
}
Expand All @@ -92,7 +100,7 @@ void Controller::resetState() {
void Controller::begin() {
serial_log("initializing controller");
void (*handlers[])(void) = {
on_up, on_down, on_left, on_right, on_a, on_b, on_select, on_start,
on_up, on_down, on_left, on_right, on_a, on_b, on_c, on_d, on_select, on_start,
};

#if LILKA_VERSION == 1
Expand Down
12 changes: 1 addition & 11 deletions lib/lilka/src/lilka/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ typedef enum {
RIGHT,
A,
B,
#if LILKA_VERSION >= 2
C,
D,
#endif
SELECT,
START,
COUNT,
Expand All @@ -40,10 +38,8 @@ typedef union {
ButtonState right;
ButtonState a;
ButtonState b;
#if LILKA_VERSION >= 2
ButtonState c;
ButtonState d;
#endif
ButtonState select;
ButtonState start;
};
Expand All @@ -66,10 +62,8 @@ class Controller {
static void on_right();
static void on_a();
static void on_b();
#if LILKA_VERSION >= 2
static void on_c();
static void on_d();
#endif
static void on_select();
static void on_start();

Expand All @@ -78,11 +72,7 @@ class Controller {
static Controller *_instance;
State state;
int8_t pins[Button::COUNT] = {
LILKA_GPIO_UP, LILKA_GPIO_DOWN, LILKA_GPIO_LEFT, LILKA_GPIO_RIGHT, LILKA_GPIO_A, LILKA_GPIO_B,
#if LILKA_VERSION >= 2
LILKA_GPIO_C, LILKA_GPIO_D,
#endif
LILKA_GPIO_SELECT, LILKA_GPIO_START,
LILKA_GPIO_UP, LILKA_GPIO_DOWN, LILKA_GPIO_LEFT, LILKA_GPIO_RIGHT, LILKA_GPIO_A, LILKA_GPIO_B, LILKA_GPIO_C, LILKA_GPIO_D, LILKA_GPIO_SELECT, LILKA_GPIO_START,
};
void (*handlers[Button::COUNT])(bool);
void (*globalHandler)(Button, bool);
Expand Down
4 changes: 4 additions & 0 deletions lib/lilka/src/lilka/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ Display::Display() : Arduino_ST7789(&displayBus, LILKA_DISPLAY_RST, LILKA_DISPLA

void Display::begin() {
serial_log("initializing display");
#ifdef LILKA_BREADBOARD
Arduino_ST7789::begin(40000000);
#else
Arduino_ST7789::begin(80000000);
#endif
setFont(u8g2_font_10x20_t_cyrillic);
setUTF8Print(true);
uint16_t row[LILKA_DISPLAY_WIDTH];
Expand Down
57 changes: 51 additions & 6 deletions lib/lilka/src/lilka/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,49 @@ namespace lilka {

extern SDCard sdcard;

void Loader::begin() {}
void Loader::begin() {
const esp_partition_t *current_partition = esp_ota_get_running_partition();
serial_log("Current partition: %s, type: %d, subtype: %d, size: %d", current_partition->label, current_partition->type, current_partition->subtype, current_partition->size);
const esp_partition_t *ota_partition = esp_ota_get_next_update_partition(current_partition); // get ota1 (we're in ota0 now)
serial_log("OTA partition: %s, type: %d, subtype: %d, size: %d", ota_partition->label, ota_partition->type, ota_partition->subtype, ota_partition->size);

// Auto-rollback does not work properly - my dev board's bootloader might be messing it up,
// since every OTA update is marked as successful, even if we don't mark it as valid
// TODO: Maybe it will work with actual Lilka v2?

/*
// Check if rollback is possible
esp_ota_img_states_t ota_state;
esp_err_t err = esp_ota_get_state_partition(esp_ota_get_running_partition(), &ota_state);
if (err != ESP_OK) {
serial_err("Failed to get state partition: %d", err);
return;
}
serial_log("OTA state: %d", ota_state);
if (ota_state == ESP_OTA_IMG_PENDING_VERIFY) {
serial_log("Rollback is possible");
// Mark ota0 as active partition so that we return to main application after next restart
esp_ota_set_boot_partition(esp_ota_get_next_update_partition(NULL));
} else {
serial_log("Rollback is not possible");
}
*/

// So here's a workaround: I'm going to set ota0 as active partition anyway, so that we return to main application after next restart.

// Mark ota0 as active partition so that we return to main application after next restart
esp_ota_img_states_t ota_state;
esp_err_t err = esp_ota_get_state_partition(esp_ota_get_running_partition(), &ota_state);
if (err != ESP_OK) {
serial_err("Failed to get state partition: %d", err);
return;
}
if (current_partition->subtype == ESP_PARTITION_SUBTYPE_APP_OTA_1) {
esp_ota_set_boot_partition(esp_ota_get_next_update_partition(NULL));
}
}

int Loader::execute(String path) {
// Завантаження прошивки з microSD-картки.
Expand All @@ -18,17 +60,20 @@ int Loader::execute(String path) {
return -1;
}

String abspath = sdcard.abspath(path);
// String abspath = sdcard.abspath(path);

// TODO: Use sdcard instead of SD
File file = SD.open(abspath.c_str(), FILE_READ);
File file = SD.open(path, FILE_READ);
if (!file) {
serial_err("Failed to open file: %s", abspath.c_str());
serial_err("Failed to open file: %s", path.c_str());
return -2;
}

esp_ota_handle_t ota_handle;
const esp_partition_t *ota_partition = esp_ota_get_next_update_partition(NULL); // get ota1 (we're in ota0 now)
const esp_partition_t *current_partition = esp_ota_get_running_partition();
serial_log("Current partition: %s, type: %d, subtype: %d, size: %d", current_partition->label, current_partition->type, current_partition->subtype, current_partition->size);
const esp_partition_t *ota_partition = esp_ota_get_next_update_partition(current_partition); // get ota1 (we're in ota0 now)
serial_log("OTA partition: %s, type: %d, subtype: %d, size: %d", ota_partition->label, ota_partition->type, ota_partition->subtype, ota_partition->size);
if (ota_partition == NULL) {
serial_err("Failed to get next OTA partition");
return -3;
Expand Down Expand Up @@ -56,7 +101,7 @@ int Loader::execute(String path) {

written += len;

if (written % (1024 * 65536) == 0) {
if (written % (1024 * 64) == 0) {
serial_log("Written %d bytes", written);
}
}
Expand Down
4 changes: 4 additions & 0 deletions lib/lilka/src/lilka/loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class Loader {
public:
void begin();
int execute(String path);

int start(String path);
int process();
int finish();
};

extern Loader loader;
Expand Down
12 changes: 8 additions & 4 deletions lib/lilka/src/lilka/serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,26 @@ void serial_begin() {
}

void serial_log(const char *format, ...) {
// TODO: printf cannot be called from an ISR
char buffer[1024];
va_list args;
va_start(args, format);
vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
Serial.print("lilka: ");
Serial.println(buffer);
// Serial.print("lilka: ");
// Serial.println(buffer);
printf("lilka: %s\n", buffer);
}
void serial_err(const char *format, ...) {
// TODO: printf cannot be called from an ISR
char buffer[1024];
va_list args;
va_start(args, format);
vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
Serial.print("lilka: error: ");
Serial.println(buffer);
// Serial.print("lilka: error: ");
// Serial.println(buffer);
printf("lilka: error: %s\n", buffer);
}

} // namespace lilka

0 comments on commit b66aeaf

Please sign in to comment.