Merge pull request #3667 from microDev1/watchdog-s2

ESP32S2: Support for WatchDog
This commit is contained in:
Scott Shawcroft 2020-11-16 15:01:54 -08:00 committed by GitHub
commit 66fb095069
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 172 additions and 8 deletions

View File

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-11-04 21:18+0530\n" "POT-Creation-Date: 2020-11-10 15:30+0530\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@ -296,7 +296,8 @@ msgstr ""
msgid "All I2C peripherals are in use" msgid "All I2C peripherals are in use"
msgstr "" msgstr ""
#: ports/esp32s2/peripherals/pcnt_handler.c #: ports/esp32s2/common-hal/countio/Counter.c
#: ports/esp32s2/common-hal/rotaryio/IncrementalEncoder.c
msgid "All PCNT units in use" msgid "All PCNT units in use"
msgstr "" msgstr ""
@ -997,6 +998,10 @@ msgstr ""
msgid "Incorrect buffer size" msgid "Incorrect buffer size"
msgstr "" msgstr ""
#: ports/esp32s2/common-hal/watchdog/WatchDogTimer.c
msgid "Initialization failed due to lack of memory"
msgstr ""
#: ports/atmel-samd/common-hal/pulseio/PulseIn.c #: ports/atmel-samd/common-hal/pulseio/PulseIn.c
msgid "Input taking too long" msgid "Input taking too long"
msgstr "" msgstr ""
@ -3206,6 +3211,7 @@ msgstr ""
msgid "pow() with 3 arguments requires integers" msgid "pow() with 3 arguments requires integers"
msgstr "" msgstr ""
#: ports/esp32s2/boards/adafruit_magtag_2.9_grayscale/mpconfigboard.h
#: ports/esp32s2/boards/adafruit_metro_esp32s2/mpconfigboard.h #: ports/esp32s2/boards/adafruit_metro_esp32s2/mpconfigboard.h
#: ports/esp32s2/boards/electroniccats_bastwifi/mpconfigboard.h #: ports/esp32s2/boards/electroniccats_bastwifi/mpconfigboard.h
#: ports/esp32s2/boards/espressif_kaluga_1/mpconfigboard.h #: ports/esp32s2/boards/espressif_kaluga_1/mpconfigboard.h
@ -3421,6 +3427,7 @@ msgstr ""
msgid "time.struct_time() takes a 9-sequence" msgid "time.struct_time() takes a 9-sequence"
msgstr "" msgstr ""
#: ports/esp32s2/common-hal/watchdog/WatchDogTimer.c
#: ports/nrf/common-hal/watchdog/WatchDogTimer.c #: ports/nrf/common-hal/watchdog/WatchDogTimer.c
msgid "timeout duration exceeded the maximum supported value" msgid "timeout duration exceeded the maximum supported value"
msgstr "" msgstr ""
@ -3604,6 +3611,10 @@ msgstr ""
msgid "vectors must have same lengths" msgid "vectors must have same lengths"
msgstr "" msgstr ""
#: ports/esp32s2/common-hal/watchdog/WatchDogTimer.c
msgid "watchdog not initialized"
msgstr ""
#: shared-bindings/watchdog/WatchDogTimer.c #: shared-bindings/watchdog/WatchDogTimer.c
msgid "watchdog timeout must be greater than 0" msgid "watchdog timeout must be greater than 0"
msgstr "" msgstr ""

View File

@ -85,6 +85,17 @@ const mcu_processor_obj_t common_hal_mcu_processor_obj = {
}, },
}; };
#if CIRCUITPY_WATCHDOG
// The singleton watchdog.WatchDogTimer object.
watchdog_watchdogtimer_obj_t common_hal_mcu_watchdogtimer_obj = {
.base = {
.type = &watchdog_watchdogtimer_type,
},
.timeout = 0.0f,
.mode = WATCHDOGMODE_NONE,
};
#endif
// This maps MCU pin names to pin objects. // This maps MCU pin names to pin objects.
STATIC const mp_rom_map_elem_t mcu_pin_global_dict_table[] = { STATIC const mp_rom_map_elem_t mcu_pin_global_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_GPIO0), MP_ROM_PTR(&pin_GPIO0) }, { MP_ROM_QSTR(MP_QSTR_GPIO0), MP_ROM_PTR(&pin_GPIO0) },

View File

@ -0,0 +1 @@
// No watchdog module functions.

View File

@ -0,0 +1,93 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/runtime.h"
#include "common-hal/watchdog/WatchDogTimer.h"
#include "shared-bindings/watchdog/__init__.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "esp_task_wdt.h"
void esp_task_wdt_isr_user_handler(void) {
mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&mp_watchdog_timeout_exception));
MP_STATE_VM(mp_pending_exception) = &mp_watchdog_timeout_exception;
#if MICROPY_ENABLE_SCHEDULER
if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
}
#endif
}
void common_hal_watchdog_feed(watchdog_watchdogtimer_obj_t *self) {
if (esp_task_wdt_reset() != ESP_OK) {
mp_raise_RuntimeError(translate("watchdog not initialized"));
}
}
void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self) {
if (esp_task_wdt_deinit() == ESP_OK) {
self->mode = WATCHDOGMODE_NONE;
}
}
void watchdog_reset(void) {
common_hal_watchdog_deinit(&common_hal_mcu_watchdogtimer_obj);
}
static void wdt_config(watchdog_watchdogtimer_obj_t *self) {
// enable panic hanler in WATCHDOGMODE_RESET mode
// initialize Task Watchdog Timer (TWDT)
if (esp_task_wdt_init((uint32_t)self->timeout, (self->mode == WATCHDOGMODE_RESET)) != ESP_OK) {
mp_raise_RuntimeError(translate("Initialization failed due to lack of memory"));
}
esp_task_wdt_add(NULL);
}
mp_float_t common_hal_watchdog_get_timeout(watchdog_watchdogtimer_obj_t *self) {
return self->timeout;
}
void common_hal_watchdog_set_timeout(watchdog_watchdogtimer_obj_t *self, mp_float_t new_timeout) {
if ((uint64_t)new_timeout > UINT32_MAX) {
mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value"));
}
if ((uint32_t)self->timeout != (uint32_t)new_timeout) {
self->timeout = new_timeout;
wdt_config(self);
}
}
watchdog_watchdogmode_t common_hal_watchdog_get_mode(watchdog_watchdogtimer_obj_t *self) {
return self->mode;
}
void common_hal_watchdog_set_mode(watchdog_watchdogtimer_obj_t *self, watchdog_watchdogmode_t new_mode) {
if (self->mode != new_mode) {
self->mode = new_mode;
wdt_config(self);
}
}

View File

@ -0,0 +1,43 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_WATCHDOG_WATCHDOGTIMER_H
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_WATCHDOG_WATCHDOGTIMER_H
#include "py/obj.h"
#include "shared-bindings/watchdog/WatchDogMode.h"
#include "shared-bindings/watchdog/WatchDogTimer.h"
struct _watchdog_watchdogtimer_obj_t {
mp_obj_base_t base;
mp_float_t timeout;
watchdog_watchdogmode_t mode;
};
// This needs to be called in order to disable the watchdog
void watchdog_reset(void);
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_WATCHDOG_WATCHDOGTIMER_H

View File

@ -0,0 +1 @@
// No watchdog module functions.

View File

@ -25,6 +25,7 @@ CIRCUITPY_NVM = 0
# We don't have enough endpoints to include MIDI. # We don't have enough endpoints to include MIDI.
CIRCUITPY_USB_MIDI = 0 CIRCUITPY_USB_MIDI = 0
CIRCUITPY_WIFI = 1 CIRCUITPY_WIFI = 1
CIRCUITPY_WATCHDOG ?= 1
CIRCUITPY_ESPIDF = 1 CIRCUITPY_ESPIDF = 1
ifndef CIRCUITPY_TOUCHIO_USE_NATIVE ifndef CIRCUITPY_TOUCHIO_USE_NATIVE

View File

@ -43,6 +43,7 @@
#include "common-hal/busio/UART.h" #include "common-hal/busio/UART.h"
#include "common-hal/pulseio/PulseIn.h" #include "common-hal/pulseio/PulseIn.h"
#include "common-hal/pwmio/PWMOut.h" #include "common-hal/pwmio/PWMOut.h"
#include "common-hal/watchdog/WatchDogTimer.h"
#include "common-hal/wifi/__init__.h" #include "common-hal/wifi/__init__.h"
#include "supervisor/memory.h" #include "supervisor/memory.h"
#include "supervisor/shared/tick.h" #include "supervisor/shared/tick.h"
@ -126,6 +127,10 @@ void reset_port(void) {
rtc_reset(); rtc_reset();
#endif #endif
#if CIRCUITPY_WATCHDOG
watchdog_reset();
#endif
#if CIRCUITPY_WIFI #if CIRCUITPY_WIFI
wifi_reset(); wifi_reset();
#endif #endif

View File

@ -93,6 +93,9 @@ void common_hal_watchdog_feed(watchdog_watchdogtimer_obj_t *self) {
} }
void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self) { void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self) {
if (self->mode == WATCHDOGMODE_RESET) {
mp_raise_NotImplementedError(translate("WatchDogTimer cannot be deinitialized once mode is set to RESET"));
}
if (timer) { if (timer) {
timer_free(); timer_free();
} }

View File

@ -66,6 +66,7 @@ STATIC mp_obj_t watchdog_watchdogtimer_feed(mp_obj_t self_in) {
if (current_mode == WATCHDOGMODE_NONE) { if (current_mode == WATCHDOGMODE_NONE) {
mp_raise_ValueError(translate("WatchDogTimer is not currently running")); mp_raise_ValueError(translate("WatchDogTimer is not currently running"));
} }
common_hal_watchdog_feed(self); common_hal_watchdog_feed(self);
return mp_const_none; return mp_const_none;
} }
@ -78,12 +79,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_feed_obj, watchdog_watch
//| //|
STATIC mp_obj_t watchdog_watchdogtimer_deinit(mp_obj_t self_in) { STATIC mp_obj_t watchdog_watchdogtimer_deinit(mp_obj_t self_in) {
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in); watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
watchdog_watchdogmode_t current_mode = common_hal_watchdog_get_mode(self);
if (current_mode == WATCHDOGMODE_RESET) {
mp_raise_NotImplementedError(translate("WatchDogTimer cannot be deinitialized once mode is set to RESET"));
}
common_hal_watchdog_deinit(self); common_hal_watchdog_deinit(self);
return mp_const_none; return mp_const_none;
} }