2020-05-26 23:25:24 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019 Nick Moore for Adafruit Industries
|
|
|
|
*
|
|
|
|
* 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 <math.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/objproperty.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
|
|
|
#include "common-hal/watchdog/WatchDogTimer.h"
|
|
|
|
|
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
|
|
|
#include "shared-bindings/watchdog/__init__.h"
|
|
|
|
#include "shared-bindings/watchdog/WatchDogTimer.h"
|
|
|
|
|
|
|
|
#include "supervisor/port.h"
|
|
|
|
|
2020-05-27 13:58:21 -04:00
|
|
|
//| class WatchDogTimer:
|
|
|
|
//| """Timer that is used to detect code lock ups and automatically reset the microcontroller
|
2022-09-27 16:21:42 -04:00
|
|
|
//| when one is detected.
|
2020-05-27 13:58:21 -04:00
|
|
|
//|
|
2022-09-27 16:21:42 -04:00
|
|
|
//| A lock up is detected when the watchdog hasn't been fed after a given duration. So, make
|
|
|
|
//| sure to call `feed` within the timeout.
|
2020-05-27 13:58:21 -04:00
|
|
|
//| """
|
2022-09-29 20:22:32 -04:00
|
|
|
//|
|
2020-05-27 13:58:21 -04:00
|
|
|
|
2020-07-03 14:00:06 -04:00
|
|
|
//| def __init__(self) -> None:
|
2023-03-16 01:54:43 -04:00
|
|
|
//| """Access the sole instance through `microcontroller.watchdog`."""
|
2020-05-27 13:58:21 -04:00
|
|
|
//| ...
|
|
|
|
|
2020-07-23 14:22:41 -04:00
|
|
|
//| def feed(self) -> None:
|
2020-05-26 23:25:24 -04:00
|
|
|
//| """Feed the watchdog timer. This must be called regularly, otherwise
|
2023-03-16 01:54:43 -04:00
|
|
|
//| the timer will expire. Silently does nothing if the watchdog isn't active."""
|
2020-05-26 23:25:24 -04:00
|
|
|
//| ...
|
|
|
|
STATIC mp_obj_t watchdog_watchdogtimer_feed(mp_obj_t self_in) {
|
|
|
|
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2023-03-16 01:54:43 -04:00
|
|
|
if (common_hal_watchdog_get_mode(self) != WATCHDOGMODE_NONE) {
|
|
|
|
common_hal_watchdog_feed(self);
|
2020-05-26 23:25:24 -04:00
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_feed_obj, watchdog_watchdogtimer_feed);
|
|
|
|
|
2020-07-23 14:22:41 -04:00
|
|
|
//| def deinit(self) -> None:
|
2023-03-15 02:35:38 -04:00
|
|
|
//| """Stop the watchdog timer.
|
|
|
|
//|
|
|
|
|
//| :raises RuntimeError: if the watchdog timer cannot be disabled on this platform.
|
|
|
|
//|
|
2023-09-24 11:23:38 -04:00
|
|
|
//| .. note:: This is deprecated in ``9.0.0`` and will be removed in ``10.0.0``.
|
|
|
|
//| Set watchdog `mode` to `None` instead.
|
2023-03-15 02:35:38 -04:00
|
|
|
//|
|
|
|
|
//| """
|
2020-05-26 23:25:24 -04:00
|
|
|
//| ...
|
|
|
|
STATIC mp_obj_t watchdog_watchdogtimer_deinit(mp_obj_t self_in) {
|
|
|
|
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
common_hal_watchdog_deinit(self);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_deinit_obj, watchdog_watchdogtimer_deinit);
|
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| timeout: float
|
2020-05-26 23:25:24 -04:00
|
|
|
//| """The maximum number of seconds that can elapse between calls
|
2023-03-16 01:54:43 -04:00
|
|
|
//| to `feed()`"""
|
2020-05-26 23:25:24 -04:00
|
|
|
STATIC mp_obj_t watchdog_watchdogtimer_obj_get_timeout(mp_obj_t self_in) {
|
|
|
|
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
return mp_obj_new_float(common_hal_watchdog_get_timeout(self));
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_get_timeout_obj, watchdog_watchdogtimer_obj_get_timeout);
|
|
|
|
|
|
|
|
STATIC mp_obj_t watchdog_watchdogtimer_obj_set_timeout(mp_obj_t self_in, mp_obj_t timeout_obj) {
|
|
|
|
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
mp_float_t timeout = mp_obj_get_float(timeout_obj);
|
|
|
|
|
2023-03-15 02:35:38 -04:00
|
|
|
mp_arg_validate_int_min(timeout, 0, MP_QSTR_timeout);
|
2020-05-26 23:25:24 -04:00
|
|
|
|
|
|
|
common_hal_watchdog_set_timeout(self, timeout);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(watchdog_watchdogtimer_set_timeout_obj, watchdog_watchdogtimer_obj_set_timeout);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETSET(watchdog_watchdogtimer_timeout_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&watchdog_watchdogtimer_get_timeout_obj,
|
|
|
|
(mp_obj_t)&watchdog_watchdogtimer_set_timeout_obj);
|
2020-05-26 23:25:24 -04:00
|
|
|
|
2023-09-24 11:23:38 -04:00
|
|
|
//| mode: Optional[WatchDogMode]
|
|
|
|
//| """The current operating mode of the WatchDogTimer `watchdog.WatchDogMode` or `None` when
|
|
|
|
//| the timer is disabled.
|
2020-05-26 23:25:24 -04:00
|
|
|
//|
|
2023-03-16 01:54:43 -04:00
|
|
|
//| Setting a `WatchDogMode` activates the WatchDog::
|
2020-05-26 23:25:24 -04:00
|
|
|
//|
|
2023-09-24 11:23:38 -04:00
|
|
|
//| from microcontroller import watchdog
|
2023-03-16 01:54:43 -04:00
|
|
|
//| from watchdog import WatchDogMode
|
2020-05-26 23:25:24 -04:00
|
|
|
//|
|
2023-09-24 11:23:38 -04:00
|
|
|
//| watchdog.timeout = 5
|
|
|
|
//| watchdog.mode = WatchDogMode.RESET
|
2020-05-26 23:25:24 -04:00
|
|
|
//|
|
|
|
|
//|
|
2023-03-16 01:54:43 -04:00
|
|
|
//| Once set, the `WatchDogTimer` will perform the specified action if the timer expires."""
|
2022-09-29 20:22:32 -04:00
|
|
|
//|
|
2020-05-26 23:25:24 -04:00
|
|
|
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);
|
2023-03-15 02:35:38 -04:00
|
|
|
return cp_enum_find(&watchdog_watchdogmode_type, common_hal_watchdog_get_mode(self));
|
2020-05-26 23:25:24 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_get_mode_obj, watchdog_watchdogtimer_obj_get_mode);
|
|
|
|
|
2023-03-15 02:35:38 -04:00
|
|
|
STATIC mp_obj_t watchdog_watchdogtimer_obj_set_mode(mp_obj_t self_in, mp_obj_t obj) {
|
2020-05-26 23:25:24 -04:00
|
|
|
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2023-09-24 11:23:38 -04:00
|
|
|
watchdog_watchdogmode_t mode = (obj == mp_const_none) ? WATCHDOGMODE_NONE : cp_enum_value(&watchdog_watchdogmode_type, obj, MP_QSTR_mode);
|
|
|
|
common_hal_watchdog_set_mode(self, mode);
|
2020-05-26 23:25:24 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(watchdog_watchdogtimer_set_mode_obj, watchdog_watchdogtimer_obj_set_mode);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETSET(watchdog_watchdogtimer_mode_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&watchdog_watchdogtimer_get_mode_obj,
|
|
|
|
(mp_obj_t)&watchdog_watchdogtimer_set_mode_obj);
|
2020-05-26 23:25:24 -04:00
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t watchdog_watchdogtimer_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_feed), MP_ROM_PTR(&watchdog_watchdogtimer_feed_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&watchdog_watchdogtimer_deinit_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_timeout), MP_ROM_PTR(&watchdog_watchdogtimer_timeout_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_mode), MP_ROM_PTR(&watchdog_watchdogtimer_mode_obj) },
|
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(watchdog_watchdogtimer_locals_dict, watchdog_watchdogtimer_locals_dict_table);
|
|
|
|
|
2023-09-19 21:09:29 -04:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
watchdog_watchdogtimer_type,
|
|
|
|
MP_QSTR_WatchDogTimer,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
|
|
|
locals_dict, &watchdog_watchdogtimer_locals_dict
|
|
|
|
);
|