diff options
author | Krono <andreasgm93@gmail.com> | 2018-07-06 12:06:11 +0200 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2019-04-30 16:53:05 +1000 |
commit | fbd4e61e57185c0e9eb1aa7ced25926a140b23c9 (patch) | |
tree | 9c65e865157897d23e5ffdb2670bc84f6c50dbc2 | |
parent | 8031b7a25c21fb864fe9dd1fa40740030be66c11 (diff) |
esp32/machine_wdt: Add timeout arg to select interval, make WDT panic.
The machine.WDT() now accepts the "timeout" keyword argument to select the
WDT interval. And the WDT is changed to panic mode which means it will
reset the device if the interval expires (instead of just printing an error
message).
-rw-r--r-- | ports/esp32/machine_wdt.c | 35 |
1 files changed, 24 insertions, 11 deletions
diff --git a/ports/esp32/machine_wdt.c b/ports/esp32/machine_wdt.c index 88a58f056..5c4732f4b 100644 --- a/ports/esp32/machine_wdt.c +++ b/ports/esp32/machine_wdt.c @@ -41,21 +41,34 @@ typedef struct _machine_wdt_obj_t { STATIC machine_wdt_obj_t wdt_default = {{&machine_wdt_type}}; -STATIC mp_obj_t machine_wdt_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { - mp_arg_check_num(n_args, n_kw, 0, 1, false); +STATIC mp_obj_t machine_wdt_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + enum { ARG_id, ARG_timeout }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_id, MP_ARG_INT, {.u_int = 0} }, + { MP_QSTR_timeout, MP_ARG_INT, {.u_int = 5000} } + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - mp_int_t id = 0; - if (n_args > 0) { - id = mp_obj_get_int(args[0]); + if (args[ARG_id].u_int != 0) { + mp_raise_ValueError(NULL); } - switch (id) { - case 0: - esp_task_wdt_add(NULL); - return &wdt_default; - default: - mp_raise_ValueError(NULL); + // Convert milliseconds to seconds (esp_task_wdt_init needs seconds) + args[ARG_timeout].u_int /= 1000; + + if (args[ARG_timeout].u_int <= 0) { + mp_raise_ValueError("WDT timeout too short"); + } + + mp_int_t rs_code = esp_task_wdt_init(args[ARG_timeout].u_int, true); + if (rs_code != ESP_OK) { + mp_raise_OSError(rs_code); } + + esp_task_wdt_add(NULL); + + return &wdt_default; } STATIC mp_obj_t machine_wdt_feed(mp_obj_t self_in) { |