update watchdog implementation

- add note about deprecation of `deinit`
- refactor `WatchDogMode` implementation
- make validation of watchdog mode port specific
This commit is contained in:
MicroDev 2023-03-15 12:05:38 +05:30
parent 3fbe0533ab
commit 668f96e6e9
No known key found for this signature in database
GPG Key ID: 2C0867BE60967730
3 changed files with 35 additions and 95 deletions

View File

@ -1,9 +1,9 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Sean Cross for Adafruit Industries
* Copyright (c) 2023 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
@ -24,76 +24,33 @@
* THE SOFTWARE.
*/
#include "py/enum.h"
#include "shared-bindings/watchdog/WatchDogMode.h"
MAKE_ENUM_VALUE(watchdog_watchdogmode_type, watchdogmode, NONE, WATCHDOGMODE_NONE);
MAKE_ENUM_VALUE(watchdog_watchdogmode_type, watchdogmode, RAISE, WATCHDOGMODE_RAISE);
MAKE_ENUM_VALUE(watchdog_watchdogmode_type, watchdogmode, RESET, WATCHDOGMODE_RESET);
//| class WatchDogMode:
//| """run state of the watchdog timer"""
//| """Run state of the watchdog timer."""
//|
//| NONE: WatchDogMode
//| """The `WatchDogTimer` is disabled."""
//|
//| def __init__(self) -> None:
//| """Enum-like class to define the run mode of the watchdog timer."""
//| RAISE: WatchDogMode
//| """Raise an exception when the WatchDogTimer expires.
//|
//| **Limitations:** ``RAISE`` mode is not supported on SAMD or RP2040.
//|
//| :type WatchDogMode:"""
//| """Raise an exception when the `WatchDogTimer` expires."""
//|
//| RESET: WatchDogMode
//| """Reset the system if the WatchDogTimer expires.
//| """Reset the system when the `WatchDogTimer` expires."""
//|
//| :type WatchDogMode:"""
//|
const mp_obj_type_t watchdog_watchdogmode_type;
const watchdog_watchdogmode_obj_t watchdog_watchdogmode_raise_obj = {
{ &watchdog_watchdogmode_type },
MAKE_ENUM_MAP(watchdog_watchdogmode) {
MAKE_ENUM_MAP_ENTRY(watchdogmode, NONE),
MAKE_ENUM_MAP_ENTRY(watchdogmode, RAISE),
MAKE_ENUM_MAP_ENTRY(watchdogmode, RESET),
};
STATIC MP_DEFINE_CONST_DICT(watchdog_watchdogmode_locals_dict, watchdog_watchdogmode_locals_table);
const watchdog_watchdogmode_obj_t watchdog_watchdogmode_reset_obj = {
{ &watchdog_watchdogmode_type },
};
MAKE_PRINTER(watchdog, watchdog_watchdogmode);
watchdog_watchdogmode_t watchdog_watchdogmode_obj_to_type(mp_obj_t obj) {
if (obj == MP_ROM_PTR(&watchdog_watchdogmode_raise_obj)) {
return WATCHDOGMODE_RAISE;
} else if (obj == MP_ROM_PTR(&watchdog_watchdogmode_reset_obj)) {
return WATCHDOGMODE_RESET;
}
return WATCHDOGMODE_NONE;
}
mp_obj_t watchdog_watchdogmode_type_to_obj(watchdog_watchdogmode_t mode) {
switch (mode) {
case WATCHDOGMODE_RAISE:
return (mp_obj_t)MP_ROM_PTR(&watchdog_watchdogmode_raise_obj);
case WATCHDOGMODE_RESET:
return (mp_obj_t)MP_ROM_PTR(&watchdog_watchdogmode_reset_obj);
case WATCHDOGMODE_NONE:
default:
return MP_ROM_NONE;
}
}
STATIC const mp_rom_map_elem_t watchdog_watchdogmode_locals_dict_table[] = {
{MP_ROM_QSTR(MP_QSTR_RAISE), MP_ROM_PTR(&watchdog_watchdogmode_raise_obj)},
{MP_ROM_QSTR(MP_QSTR_RESET), MP_ROM_PTR(&watchdog_watchdogmode_reset_obj)},
};
STATIC MP_DEFINE_CONST_DICT(watchdog_watchdogmode_locals_dict, watchdog_watchdogmode_locals_dict_table);
STATIC void watchdog_watchdogmode_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
qstr runmode = MP_QSTR_None;
if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&watchdog_watchdogmode_raise_obj)) {
runmode = MP_QSTR_RAISE;
} else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&watchdog_watchdogmode_reset_obj)) {
runmode = MP_QSTR_RESET;
}
mp_printf(print, "%q.%q.%q", MP_QSTR_watchdog, MP_QSTR_WatchDogMode,
runmode);
}
const mp_obj_type_t watchdog_watchdogmode_type = {
{ &mp_type_type },
.name = MP_QSTR_WatchDogMode,
.print = watchdog_watchdogmode_print,
.locals_dict = (mp_obj_t)&watchdog_watchdogmode_locals_dict,
};
MAKE_ENUM_TYPE(watchdog, WatchDogMode, watchdog_watchdogmode);

View File

@ -1,5 +1,5 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
@ -27,7 +27,7 @@
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG_WATCHDOGMODE_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG_WATCHDOGMODE_H
#include "py/obj.h"
#include "py/enum.h"
typedef enum {
WATCHDOGMODE_NONE,
@ -37,13 +37,4 @@ typedef enum {
extern const mp_obj_type_t watchdog_watchdogmode_type;
watchdog_watchdogmode_t watchdog_watchdogmode_obj_to_type(mp_obj_t obj);
mp_obj_t watchdog_watchdogmode_type_to_obj(watchdog_watchdogmode_t mode);
typedef struct {
mp_obj_base_t base;
} watchdog_watchdogmode_obj_t;
extern const watchdog_watchdogmode_obj_t watchdog_watchdogmode_raise_obj;
extern const watchdog_watchdogmode_obj_t watchdog_watchdogmode_reset_obj;
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG_WATCHDOGMODE_H

View File

@ -71,8 +71,14 @@ STATIC mp_obj_t watchdog_watchdogtimer_feed(mp_obj_t self_in) {
STATIC MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_feed_obj, watchdog_watchdogtimer_feed);
//| def deinit(self) -> None:
//| """Stop the watchdog timer. This may raise an error if the watchdog
//| timer cannot be disabled on this platform."""
//| """Stop the watchdog timer.
//|
//| :raises RuntimeError: if the watchdog timer cannot be disabled on this platform.
//|
//| .. note:: This is deprecated in ``8.1.0`` and will be removed in ``9.0.0``.
//| Set watchdog `mode` to `WatchDogMode.NONE` instead.
//|
//| """
//| ...
STATIC mp_obj_t watchdog_watchdogtimer_deinit(mp_obj_t self_in) {
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
@ -94,7 +100,7 @@ STATIC mp_obj_t watchdog_watchdogtimer_obj_set_timeout(mp_obj_t self_in, mp_obj_
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_float_t timeout = mp_obj_get_float(timeout_obj);
mp_arg_validate_int_min((int)timeout, 0, MP_QSTR_timeout);
mp_arg_validate_int_min(timeout, 0, MP_QSTR_timeout);
common_hal_watchdog_set_timeout(self, timeout);
return mp_const_none;
@ -122,27 +128,13 @@ MP_PROPERTY_GETSET(watchdog_watchdogtimer_timeout_obj,
//|
STATIC mp_obj_t watchdog_watchdogtimer_obj_get_mode(mp_obj_t self_in) {
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
return watchdog_watchdogmode_type_to_obj(common_hal_watchdog_get_mode(self));
return cp_enum_find(&watchdog_watchdogmode_type, common_hal_watchdog_get_mode(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_get_mode_obj, watchdog_watchdogtimer_obj_get_mode);
STATIC mp_obj_t watchdog_watchdogtimer_obj_set_mode(mp_obj_t self_in, mp_obj_t mode_obj) {
STATIC mp_obj_t watchdog_watchdogtimer_obj_set_mode(mp_obj_t self_in, mp_obj_t obj) {
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
watchdog_watchdogmode_t current_mode = common_hal_watchdog_get_mode(self);
watchdog_watchdogmode_t new_mode = watchdog_watchdogmode_obj_to_type(mode_obj);
mp_float_t current_timeout = common_hal_watchdog_get_timeout(self);
// When setting the mode, the timeout value must be greater than zero
if (new_mode == WATCHDOGMODE_RESET || new_mode == WATCHDOGMODE_RAISE) {
mp_arg_validate_int_min((int)current_timeout, 0, MP_QSTR_timeout);
}
// Don't allow changing the mode once the watchdog timer has been started
if (current_mode == WATCHDOGMODE_RESET && new_mode != WATCHDOGMODE_RESET) {
mp_raise_TypeError(translate("WatchDogTimer.mode cannot be changed once set to WatchDogMode.RESET"));
}
common_hal_watchdog_set_mode(self, new_mode);
common_hal_watchdog_set_mode(self, cp_enum_value(&watchdog_watchdogmode_type, obj, MP_QSTR_mode));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(watchdog_watchdogtimer_set_mode_obj, watchdog_watchdogtimer_obj_set_mode);