Skip to content

Commit 66fb095

Browse files
authored
Merge pull request #3667 from microDev1/watchdog-s2
ESP32S2: Support for WatchDog
2 parents f4fd236 + 146adca commit 66fb095

File tree

10 files changed

+172
-8
lines changed

10 files changed

+172
-8
lines changed

locale/circuitpython.pot

Lines changed: 13 additions & 2 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-04 21:18+0530\n"
11+
"POT-Creation-Date: 2020-11-10 15: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"
@@ -296,7 +296,8 @@ msgstr ""
296296
msgid "All I2C peripherals are in use"
297297
msgstr ""
298298

299-
#: ports/esp32s2/peripherals/pcnt_handler.c
299+
#: ports/esp32s2/common-hal/countio/Counter.c
300+
#: ports/esp32s2/common-hal/rotaryio/IncrementalEncoder.c
300301
msgid "All PCNT units in use"
301302
msgstr ""
302303

@@ -997,6 +998,10 @@ msgstr ""
997998
msgid "Incorrect buffer size"
998999
msgstr ""
9991000

1001+
#: ports/esp32s2/common-hal/watchdog/WatchDogTimer.c
1002+
msgid "Initialization failed due to lack of memory"
1003+
msgstr ""
1004+
10001005
#: ports/atmel-samd/common-hal/pulseio/PulseIn.c
10011006
msgid "Input taking too long"
10021007
msgstr ""
@@ -3206,6 +3211,7 @@ msgstr ""
32063211
msgid "pow() with 3 arguments requires integers"
32073212
msgstr ""
32083213

3214+
#: ports/esp32s2/boards/adafruit_magtag_2.9_grayscale/mpconfigboard.h
32093215
#: ports/esp32s2/boards/adafruit_metro_esp32s2/mpconfigboard.h
32103216
#: ports/esp32s2/boards/electroniccats_bastwifi/mpconfigboard.h
32113217
#: ports/esp32s2/boards/espressif_kaluga_1/mpconfigboard.h
@@ -3421,6 +3427,7 @@ msgstr ""
34213427
msgid "time.struct_time() takes a 9-sequence"
34223428
msgstr ""
34233429

3430+
#: ports/esp32s2/common-hal/watchdog/WatchDogTimer.c
34243431
#: ports/nrf/common-hal/watchdog/WatchDogTimer.c
34253432
msgid "timeout duration exceeded the maximum supported value"
34263433
msgstr ""
@@ -3604,6 +3611,10 @@ msgstr ""
36043611
msgid "vectors must have same lengths"
36053612
msgstr ""
36063613

3614+
#: ports/esp32s2/common-hal/watchdog/WatchDogTimer.c
3615+
msgid "watchdog not initialized"
3616+
msgstr ""
3617+
36073618
#: shared-bindings/watchdog/WatchDogTimer.c
36083619
msgid "watchdog timeout must be greater than 0"
36093620
msgstr ""

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ const mcu_processor_obj_t common_hal_mcu_processor_obj = {
8585
},
8686
};
8787

88+
#if CIRCUITPY_WATCHDOG
89+
// The singleton watchdog.WatchDogTimer object.
90+
watchdog_watchdogtimer_obj_t common_hal_mcu_watchdogtimer_obj = {
91+
.base = {
92+
.type = &watchdog_watchdogtimer_type,
93+
},
94+
.timeout = 0.0f,
95+
.mode = WATCHDOGMODE_NONE,
96+
};
97+
#endif
98+
8899
// This maps MCU pin names to pin objects.
89100
STATIC const mp_rom_map_elem_t mcu_pin_global_dict_table[] = {
90101
{ MP_ROM_QSTR(MP_QSTR_GPIO0), MP_ROM_PTR(&pin_GPIO0) },
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// No watchdog module functions.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/runtime.h"
28+
#include "common-hal/watchdog/WatchDogTimer.h"
29+
30+
#include "shared-bindings/watchdog/__init__.h"
31+
#include "shared-bindings/microcontroller/__init__.h"
32+
33+
#include "esp_task_wdt.h"
34+
35+
void esp_task_wdt_isr_user_handler(void) {
36+
mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&mp_watchdog_timeout_exception));
37+
MP_STATE_VM(mp_pending_exception) = &mp_watchdog_timeout_exception;
38+
#if MICROPY_ENABLE_SCHEDULER
39+
if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
40+
MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
41+
}
42+
#endif
43+
}
44+
45+
void common_hal_watchdog_feed(watchdog_watchdogtimer_obj_t *self) {
46+
if (esp_task_wdt_reset() != ESP_OK) {
47+
mp_raise_RuntimeError(translate("watchdog not initialized"));
48+
}
49+
}
50+
51+
void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self) {
52+
if (esp_task_wdt_deinit() == ESP_OK) {
53+
self->mode = WATCHDOGMODE_NONE;
54+
}
55+
}
56+
57+
void watchdog_reset(void) {
58+
common_hal_watchdog_deinit(&common_hal_mcu_watchdogtimer_obj);
59+
}
60+
61+
static void wdt_config(watchdog_watchdogtimer_obj_t *self) {
62+
// enable panic hanler in WATCHDOGMODE_RESET mode
63+
// initialize Task Watchdog Timer (TWDT)
64+
if (esp_task_wdt_init((uint32_t)self->timeout, (self->mode == WATCHDOGMODE_RESET)) != ESP_OK) {
65+
mp_raise_RuntimeError(translate("Initialization failed due to lack of memory"));
66+
}
67+
esp_task_wdt_add(NULL);
68+
}
69+
70+
mp_float_t common_hal_watchdog_get_timeout(watchdog_watchdogtimer_obj_t *self) {
71+
return self->timeout;
72+
}
73+
74+
void common_hal_watchdog_set_timeout(watchdog_watchdogtimer_obj_t *self, mp_float_t new_timeout) {
75+
if ((uint64_t)new_timeout > UINT32_MAX) {
76+
mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value"));
77+
}
78+
if ((uint32_t)self->timeout != (uint32_t)new_timeout) {
79+
self->timeout = new_timeout;
80+
wdt_config(self);
81+
}
82+
}
83+
84+
watchdog_watchdogmode_t common_hal_watchdog_get_mode(watchdog_watchdogtimer_obj_t *self) {
85+
return self->mode;
86+
}
87+
88+
void common_hal_watchdog_set_mode(watchdog_watchdogtimer_obj_t *self, watchdog_watchdogmode_t new_mode) {
89+
if (self->mode != new_mode) {
90+
self->mode = new_mode;
91+
wdt_config(self);
92+
}
93+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_WATCHDOG_WATCHDOGTIMER_H
28+
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_WATCHDOG_WATCHDOGTIMER_H
29+
30+
#include "py/obj.h"
31+
#include "shared-bindings/watchdog/WatchDogMode.h"
32+
#include "shared-bindings/watchdog/WatchDogTimer.h"
33+
34+
struct _watchdog_watchdogtimer_obj_t {
35+
mp_obj_base_t base;
36+
mp_float_t timeout;
37+
watchdog_watchdogmode_t mode;
38+
};
39+
40+
// This needs to be called in order to disable the watchdog
41+
void watchdog_reset(void);
42+
43+
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_WATCHDOG_WATCHDOGTIMER_H
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// No watchdog module functions.

ports/esp32s2/mpconfigport.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ CIRCUITPY_NVM = 0
2525
# We don't have enough endpoints to include MIDI.
2626
CIRCUITPY_USB_MIDI = 0
2727
CIRCUITPY_WIFI = 1
28+
CIRCUITPY_WATCHDOG ?= 1
2829
CIRCUITPY_ESPIDF = 1
2930

3031
ifndef CIRCUITPY_TOUCHIO_USE_NATIVE

ports/esp32s2/supervisor/port.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "common-hal/busio/UART.h"
4444
#include "common-hal/pulseio/PulseIn.h"
4545
#include "common-hal/pwmio/PWMOut.h"
46+
#include "common-hal/watchdog/WatchDogTimer.h"
4647
#include "common-hal/wifi/__init__.h"
4748
#include "supervisor/memory.h"
4849
#include "supervisor/shared/tick.h"
@@ -126,6 +127,10 @@ void reset_port(void) {
126127
rtc_reset();
127128
#endif
128129

130+
#if CIRCUITPY_WATCHDOG
131+
watchdog_reset();
132+
#endif
133+
129134
#if CIRCUITPY_WIFI
130135
wifi_reset();
131136
#endif

ports/nrf/common-hal/watchdog/WatchDogTimer.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ void common_hal_watchdog_feed(watchdog_watchdogtimer_obj_t *self) {
9393
}
9494

9595
void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self) {
96+
if (self->mode == WATCHDOGMODE_RESET) {
97+
mp_raise_NotImplementedError(translate("WatchDogTimer cannot be deinitialized once mode is set to RESET"));
98+
}
9699
if (timer) {
97100
timer_free();
98101
}

shared-bindings/watchdog/WatchDogTimer.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ STATIC mp_obj_t watchdog_watchdogtimer_feed(mp_obj_t self_in) {
6666
if (current_mode == WATCHDOGMODE_NONE) {
6767
mp_raise_ValueError(translate("WatchDogTimer is not currently running"));
6868
}
69+
6970
common_hal_watchdog_feed(self);
7071
return mp_const_none;
7172
}
@@ -78,12 +79,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_feed_obj, watchdog_watch
7879
//|
7980
STATIC mp_obj_t watchdog_watchdogtimer_deinit(mp_obj_t self_in) {
8081
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
81-
watchdog_watchdogmode_t current_mode = common_hal_watchdog_get_mode(self);
82-
83-
if (current_mode == WATCHDOGMODE_RESET) {
84-
mp_raise_NotImplementedError(translate("WatchDogTimer cannot be deinitialized once mode is set to RESET"));
85-
}
86-
8782
common_hal_watchdog_deinit(self);
8883
return mp_const_none;
8984
}

0 commit comments

Comments
 (0)