Skip to content

Commit 6c59836

Browse files
committed
watchdog implementation for esp32s2
1 parent 61bf0e9 commit 6c59836

File tree

7 files changed

+157
-2
lines changed

7 files changed

+157
-2
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

@@ -992,6 +993,10 @@ msgstr ""
992993
msgid "Incorrect buffer size"
993994
msgstr ""
994995

996+
#: ports/esp32s2/common-hal/watchdog/WatchDogTimer.c
997+
msgid "Initialization failed due to lack of memory"
998+
msgstr ""
999+
9951000
#: ports/atmel-samd/common-hal/pulseio/PulseIn.c
9961001
msgid "Input taking too long"
9971002
msgstr ""
@@ -3201,6 +3206,7 @@ msgstr ""
32013206
msgid "pow() with 3 arguments requires integers"
32023207
msgstr ""
32033208

3209+
#: ports/esp32s2/boards/adafruit_magtag_2.9_grayscale/mpconfigboard.h
32043210
#: ports/esp32s2/boards/adafruit_metro_esp32s2/mpconfigboard.h
32053211
#: ports/esp32s2/boards/electroniccats_bastwifi/mpconfigboard.h
32063212
#: ports/esp32s2/boards/espressif_kaluga_1/mpconfigboard.h
@@ -3416,6 +3422,7 @@ msgstr ""
34163422
msgid "time.struct_time() takes a 9-sequence"
34173423
msgstr ""
34183424

3425+
#: ports/esp32s2/common-hal/watchdog/WatchDogTimer.c
34193426
#: ports/nrf/common-hal/watchdog/WatchDogTimer.c
34203427
msgid "timeout duration exceeded the maximum supported value"
34213428
msgstr ""
@@ -3599,6 +3606,10 @@ msgstr ""
35993606
msgid "vectors must have same lengths"
36003607
msgstr ""
36013608

3609+
#: ports/esp32s2/common-hal/watchdog/WatchDogTimer.c
3610+
msgid "watchdog not initialized"
3611+
msgstr ""
3612+
36023613
#: shared-bindings/watchdog/WatchDogTimer.c
36033614
msgid "watchdog timeout must be greater than 0"
36043615
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: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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/microcontroller/__init__.h"
31+
32+
#include "esp_task_wdt.h"
33+
34+
void esp_task_wdt_isr_user_handler(void) {
35+
36+
}
37+
38+
void common_hal_watchdog_feed(watchdog_watchdogtimer_obj_t *self) {
39+
if (esp_task_wdt_reset() != ESP_OK) {
40+
mp_raise_RuntimeError(translate("watchdog not initialized"));
41+
}
42+
}
43+
44+
void common_hal_watchdog_deinit(watchdog_watchdogtimer_obj_t *self) {
45+
if (esp_task_wdt_deinit() == ESP_OK) {
46+
self->mode = WATCHDOGMODE_NONE;
47+
}
48+
}
49+
50+
void watchdog_reset(void) {
51+
common_hal_watchdog_deinit(&common_hal_mcu_watchdogtimer_obj);
52+
}
53+
54+
static void wdt_config(watchdog_watchdogtimer_obj_t *self) {
55+
// enable panic hanler in WATCHDOGMODE_RESET mode
56+
// initialize Task Watchdog Timer (TWDT)
57+
if (esp_task_wdt_init((uint32_t)self->timeout, (self->mode == WATCHDOGMODE_RESET)) != ESP_OK) {
58+
mp_raise_RuntimeError(translate("Initialization failed due to lack of memory"));
59+
}
60+
esp_task_wdt_add(NULL);
61+
}
62+
63+
mp_float_t common_hal_watchdog_get_timeout(watchdog_watchdogtimer_obj_t *self) {
64+
return self->timeout;
65+
}
66+
67+
void common_hal_watchdog_set_timeout(watchdog_watchdogtimer_obj_t *self, mp_float_t new_timeout) {
68+
if ((uint64_t)new_timeout > UINT32_MAX) {
69+
mp_raise_ValueError(translate("timeout duration exceeded the maximum supported value"));
70+
}
71+
if ((uint32_t)self->timeout != (uint32_t)new_timeout) {
72+
self->timeout = new_timeout;
73+
wdt_config(self);
74+
}
75+
}
76+
77+
watchdog_watchdogmode_t common_hal_watchdog_get_mode(watchdog_watchdogtimer_obj_t *self) {
78+
return self->mode;
79+
}
80+
81+
void common_hal_watchdog_set_mode(watchdog_watchdogtimer_obj_t *self, watchdog_watchdogmode_t new_mode) {
82+
if (self->mode != new_mode) {
83+
self->mode = new_mode;
84+
wdt_config(self);
85+
}
86+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 if it's set to
41+
// "RAISE". If set to "RESET", then the watchdog cannot be reset.
42+
void watchdog_reset(void);
43+
44+
#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

0 commit comments

Comments
 (0)