-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some devices have button connected to a strap pin and cannot be booted with button down (2.5, 1PM). Instead, we modify the algorithm to allow up to 3 seconds on power up for user to press and hold the button for at least 2 seconds. During wait LED is blinking fast (100 ms on/off) and stops blinking when the button is pressed. It then lights up solid when device boots into failsafe mode. Soft reboots do not have this delay, so most of reboots are just as fast.
- Loading branch information
Showing
7 changed files
with
202 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) Shelly-HomeKit Contributors | ||
* All rights reserved | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
namespace shelly { | ||
|
||
bool WipeDevice(); | ||
|
||
bool IsSoftReboot(); | ||
|
||
bool IsFailsafeMode(); | ||
|
||
} // namespace shelly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
description: Shelly device reset library | ||
type: lib | ||
version: 1.0.0 | ||
|
||
sources: | ||
- src | ||
|
||
includes: | ||
- include | ||
|
||
no_implicit_init_deps: true | ||
# Execute as early as possible, before pretty much anything else. | ||
init_after: | ||
- core | ||
init_before: | ||
- adc | ||
- i2c | ||
- file-logger | ||
- homekit-adk | ||
- http-server | ||
- ota-http-server | ||
- rpc-* | ||
- wifi | ||
|
||
manifest_version: 2019-07-28 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* Copyright (c) Shelly-HomeKit Contributors | ||
* All rights reserved | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "shelly_reset.hpp" | ||
|
||
#include "mgos.hpp" | ||
|
||
#if CS_PLATFORM == CS_P_ESP8266 | ||
extern "C" { | ||
#include "user_interface.h" | ||
} | ||
extern "C" uint32_t rtc_get_reset_reason(void); | ||
// This location is in RTC memory and was used for MEMP_NUM_TCP_PCB in original | ||
// LWIP It used RTC to communicate with espconn (ugh). This is not used anymore, | ||
// so we can repurpose this location for failsafe flag. | ||
#define RTC_SCRATCH_ADDR 0x600011fc | ||
#define FF_MODE_MAGIC 0x18365472 | ||
#endif | ||
|
||
#include "shelly_main.hpp" | ||
|
||
namespace shelly { | ||
|
||
static bool s_failsafe_mode = false; | ||
|
||
// Executed very early, pretty much nothing is available here. | ||
extern "C" void mgos_app_preinit(void) { | ||
#if RST_GPIO_INIT >= 0 | ||
mgos_gpio_setup_output(RST_GPIO_INIT, 0); | ||
#endif | ||
#if LED_GPIO >= 0 | ||
mgos_gpio_setup_output(LED_GPIO, !LED_ON); | ||
#endif | ||
#if BTN_GPIO >= 0 | ||
mgos_gpio_setup_input(BTN_GPIO, | ||
(BTN_DOWN ? MGOS_GPIO_PULL_DOWN : MGOS_GPIO_PULL_UP)); | ||
#if CS_PLATFORM == CS_P_ESP8266 | ||
// system_get_rst_info() is not available yet so we're on our own. | ||
uint32_t rr = rtc_get_reset_reason(); | ||
uint32_t rir = READ_PERI_REG(RTC_STORE0); // rst_info.reason | ||
// If this is not a power up / CH_PD reset, skip. | ||
if (!(rr == 1 && rir == REASON_DEFAULT_RST)) { | ||
s_failsafe_mode = (READ_PERI_REG(RTC_SCRATCH_ADDR) == FF_MODE_MAGIC); | ||
WRITE_PERI_REG(RTC_SCRATCH_ADDR, 0); | ||
return; | ||
} | ||
#endif | ||
// Give the user 3 seconds to press the button and hold it for 2 seconds. | ||
int num_down = 0; | ||
for (int i = 0; (i < 30 || num_down > 0) && num_down < 20; i++) { | ||
mgos_msleep(100); | ||
bool down = (mgos_gpio_read(BTN_GPIO) == BTN_DOWN); | ||
if (down) { | ||
mgos_cd_putc('!'); | ||
num_down++; | ||
} else { | ||
mgos_cd_putc('.'); | ||
num_down = 0; | ||
} | ||
#if LED_GPIO >= 0 | ||
if (down) { | ||
mgos_gpio_write(LED_GPIO, !LED_ON); | ||
} else { | ||
mgos_gpio_toggle(LED_GPIO); | ||
} | ||
#endif | ||
} | ||
mgos_cd_putc('\n'); | ||
#if LED_GPIO >= 0 | ||
mgos_gpio_write(LED_GPIO, !LED_ON); | ||
#endif | ||
if (num_down >= 20) { | ||
s_failsafe_mode = true; | ||
} | ||
#endif | ||
} | ||
|
||
bool IsFailsafeMode() { | ||
return s_failsafe_mode; | ||
} | ||
|
||
bool WipeDevice() { | ||
LOG(LL_INFO, ("== Wiping configuration")); | ||
static const char *s_wipe_files[] = { | ||
"conf2.json", | ||
"conf9.json", | ||
KVS_FILE_NAME, | ||
AUTH_FILE_NAME, | ||
}; | ||
bool wiped = false; | ||
for (const char *wipe_fn : s_wipe_files) { | ||
if (remove(wipe_fn) == 0) wiped = true; | ||
} | ||
#if defined(MGOS_HAVE_VFS_FS_SPIFFS) || defined(MGOS_HAVE_VFS_FS_LFS) | ||
if (wiped) { | ||
mgos_vfs_gc("/"); | ||
} | ||
#endif | ||
return wiped; | ||
} | ||
|
||
bool IsSoftReboot() { | ||
#if CS_PLATFORM == CS_P_ESP8266 | ||
const struct rst_info *ri = system_get_rst_info(); | ||
return (ri->reason == REASON_SOFT_RESTART); | ||
#else | ||
return false; | ||
#endif | ||
} | ||
|
||
extern "C" bool mgos_libreset_init(void) { | ||
if (!IsFailsafeMode()) return true; | ||
if (WipeDevice()) { | ||
LOG(LL_INFO, ("== Wiped config, rebooting")); | ||
#ifdef RTC_SCRATCH_ADDR | ||
WRITE_PERI_REG(RTC_SCRATCH_ADDR, FF_MODE_MAGIC); | ||
#endif | ||
mgos_system_restart_after(100); // Not needed, but just in case. | ||
return false; // Will reboot the device. | ||
} | ||
return true; | ||
} | ||
|
||
} // namespace shelly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters