Skip to content

Commit 8b25ba6

Browse files
committed
fix: fix board hang after sequential setup loop
1 parent 1d3e693 commit 8b25ba6

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

docs/flasher/script.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ const state = {
6161
isWriting: false
6262
};
6363

64+
const delay = async (n) => await new Promise((resolve) => setTimeout(resolve, n));
65+
6466
function setErrorMsg(msg) {
6567
errMsg.innerText = msg;
6668
setScene(scenes.error);
@@ -200,6 +202,7 @@ async function writePassword(value) {
200202
throw new Error(`Device acknowledged password length doesn't match (want: ${value.length}, got: ${val})`);
201203
}
202204

205+
await delay(500);
203206
setScene(scenes.finish);
204207
setTimeout(() => setScene(scenes.start), 3000);
205208
} finally {

include/utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ uint16_t getPasswdLen();
2020
void readPasswdStr(uint16_t len, uint8_t* dst);
2121
void savePasswd(uint16_t len, uint8_t* data);
2222
void wipePasswd();
23+
void reset();
2324

2425
#endif

src/saga.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class SetupSaga : public Saga {
5555
PromptReader m_reader;
5656
unsigned long m_prevMillis = 0;
5757
bool m_ledEnabled = false;
58+
bool m_changed = false;
5859

5960
void updateLedState(bool state) {
6061
m_prevMillis = millis();
@@ -95,6 +96,7 @@ class SetupSaga : public Saga {
9596
Serial.println();
9697
#endif
9798

99+
m_changed = true;
98100
savePasswd(result.len, result.data);
99101
state.pwdLen = result.len;
100102
if (state.pwdData != nullptr) {
@@ -108,11 +110,21 @@ class SetupSaga : public Saga {
108110
void enter(BoardState& state) override {
109111
Serial.begin(9600);
110112
updateLedState(true);
113+
m_changed = true;
111114
m_reader.reset();
112115
}
113116

114117
void leave(BoardState& state) override {
118+
Serial.flush();
115119
Serial.end();
120+
121+
if (m_changed) {
122+
// HACK: for some reason, board stuck in indeterminate state after a couple of sequential setup cycles.
123+
// Rebooting device helps.
124+
delay(250);
125+
reset();
126+
}
127+
116128
m_reader.reset();
117129
digitalWrite(PIN_LED_SETUP, LOW);
118130
}

src/utils.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <avr/wdt.h>
12
#include <Arduino.h>
23
#include <EEPROM.h>
34
#include "config.h"
@@ -61,3 +62,17 @@ void wipePasswd() {
6162
EEPROM.update(i, 0xFF);
6263
}
6364
}
65+
66+
void reset() {
67+
#if defined(__AVR__)
68+
wdt_disable();
69+
wdt_enable(WDTO_15MS);
70+
while (1) {}
71+
#elif defined(ESP32) || defined(ESP8266)
72+
ESP.restart();
73+
#else
74+
// Fallback for other architectures
75+
void (*resetFunc)(void) = 0;
76+
resetFunc();
77+
#endif
78+
}

0 commit comments

Comments
 (0)