Skip to content

Commit 668f96e

Browse files
committed
update watchdog implementation
- add note about deprecation of `deinit` - refactor `WatchDogMode` implementation - make validation of watchdog mode port specific
1 parent 3fbe053 commit 668f96e

File tree

3 files changed

+35
-95
lines changed

3 files changed

+35
-95
lines changed
Lines changed: 21 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
2-
* This file is part of the Micro Python project, http://micropython.org/
2+
* This file is part of the MicroPython project, http://micropython.org/
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2020 Sean Cross for Adafruit Industries
6+
* Copyright (c) 2023 MicroDev
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -24,76 +24,33 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "py/enum.h"
28+
2729
#include "shared-bindings/watchdog/WatchDogMode.h"
2830

31+
MAKE_ENUM_VALUE(watchdog_watchdogmode_type, watchdogmode, NONE, WATCHDOGMODE_NONE);
32+
MAKE_ENUM_VALUE(watchdog_watchdogmode_type, watchdogmode, RAISE, WATCHDOGMODE_RAISE);
33+
MAKE_ENUM_VALUE(watchdog_watchdogmode_type, watchdogmode, RESET, WATCHDOGMODE_RESET);
34+
2935
//| class WatchDogMode:
30-
//| """run state of the watchdog timer"""
31-
//|
32-
//| def __init__(self) -> None:
33-
//| """Enum-like class to define the run mode of the watchdog timer."""
34-
//| RAISE: WatchDogMode
35-
//| """Raise an exception when the WatchDogTimer expires.
36+
//| """Run state of the watchdog timer."""
3637
//|
37-
//| **Limitations:** ``RAISE`` mode is not supported on SAMD or RP2040.
38+
//| NONE: WatchDogMode
39+
//| """The `WatchDogTimer` is disabled."""
3840
//|
39-
//| :type WatchDogMode:"""
41+
//| RAISE: WatchDogMode
42+
//| """Raise an exception when the `WatchDogTimer` expires."""
4043
//|
4144
//| RESET: WatchDogMode
42-
//| """Reset the system if the WatchDogTimer expires.
43-
//|
44-
//| :type WatchDogMode:"""
45+
//| """Reset the system when the `WatchDogTimer` expires."""
4546
//|
46-
const mp_obj_type_t watchdog_watchdogmode_type;
47-
48-
const watchdog_watchdogmode_obj_t watchdog_watchdogmode_raise_obj = {
49-
{ &watchdog_watchdogmode_type },
47+
MAKE_ENUM_MAP(watchdog_watchdogmode) {
48+
MAKE_ENUM_MAP_ENTRY(watchdogmode, NONE),
49+
MAKE_ENUM_MAP_ENTRY(watchdogmode, RAISE),
50+
MAKE_ENUM_MAP_ENTRY(watchdogmode, RESET),
5051
};
52+
STATIC MP_DEFINE_CONST_DICT(watchdog_watchdogmode_locals_dict, watchdog_watchdogmode_locals_table);
5153

52-
const watchdog_watchdogmode_obj_t watchdog_watchdogmode_reset_obj = {
53-
{ &watchdog_watchdogmode_type },
54-
};
55-
56-
watchdog_watchdogmode_t watchdog_watchdogmode_obj_to_type(mp_obj_t obj) {
57-
if (obj == MP_ROM_PTR(&watchdog_watchdogmode_raise_obj)) {
58-
return WATCHDOGMODE_RAISE;
59-
} else if (obj == MP_ROM_PTR(&watchdog_watchdogmode_reset_obj)) {
60-
return WATCHDOGMODE_RESET;
61-
}
62-
return WATCHDOGMODE_NONE;
63-
}
54+
MAKE_PRINTER(watchdog, watchdog_watchdogmode);
6455

65-
mp_obj_t watchdog_watchdogmode_type_to_obj(watchdog_watchdogmode_t mode) {
66-
switch (mode) {
67-
case WATCHDOGMODE_RAISE:
68-
return (mp_obj_t)MP_ROM_PTR(&watchdog_watchdogmode_raise_obj);
69-
case WATCHDOGMODE_RESET:
70-
return (mp_obj_t)MP_ROM_PTR(&watchdog_watchdogmode_reset_obj);
71-
case WATCHDOGMODE_NONE:
72-
default:
73-
return MP_ROM_NONE;
74-
}
75-
}
76-
77-
STATIC const mp_rom_map_elem_t watchdog_watchdogmode_locals_dict_table[] = {
78-
{MP_ROM_QSTR(MP_QSTR_RAISE), MP_ROM_PTR(&watchdog_watchdogmode_raise_obj)},
79-
{MP_ROM_QSTR(MP_QSTR_RESET), MP_ROM_PTR(&watchdog_watchdogmode_reset_obj)},
80-
};
81-
STATIC MP_DEFINE_CONST_DICT(watchdog_watchdogmode_locals_dict, watchdog_watchdogmode_locals_dict_table);
82-
83-
STATIC void watchdog_watchdogmode_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
84-
qstr runmode = MP_QSTR_None;
85-
if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&watchdog_watchdogmode_raise_obj)) {
86-
runmode = MP_QSTR_RAISE;
87-
} else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&watchdog_watchdogmode_reset_obj)) {
88-
runmode = MP_QSTR_RESET;
89-
}
90-
mp_printf(print, "%q.%q.%q", MP_QSTR_watchdog, MP_QSTR_WatchDogMode,
91-
runmode);
92-
}
93-
94-
const mp_obj_type_t watchdog_watchdogmode_type = {
95-
{ &mp_type_type },
96-
.name = MP_QSTR_WatchDogMode,
97-
.print = watchdog_watchdogmode_print,
98-
.locals_dict = (mp_obj_t)&watchdog_watchdogmode_locals_dict,
99-
};
56+
MAKE_ENUM_TYPE(watchdog, WatchDogMode, watchdog_watchdogmode);

shared-bindings/watchdog/WatchDogMode.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* This file is part of the Micro Python project, http://micropython.org/
2+
* This file is part of the MicroPython project, http://micropython.org/
33
*
44
* The MIT License (MIT)
55
*
@@ -27,7 +27,7 @@
2727
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG_WATCHDOGMODE_H
2828
#define MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG_WATCHDOGMODE_H
2929

30-
#include "py/obj.h"
30+
#include "py/enum.h"
3131

3232
typedef enum {
3333
WATCHDOGMODE_NONE,
@@ -37,13 +37,4 @@ typedef enum {
3737

3838
extern const mp_obj_type_t watchdog_watchdogmode_type;
3939

40-
watchdog_watchdogmode_t watchdog_watchdogmode_obj_to_type(mp_obj_t obj);
41-
mp_obj_t watchdog_watchdogmode_type_to_obj(watchdog_watchdogmode_t mode);
42-
43-
typedef struct {
44-
mp_obj_base_t base;
45-
} watchdog_watchdogmode_obj_t;
46-
extern const watchdog_watchdogmode_obj_t watchdog_watchdogmode_raise_obj;
47-
extern const watchdog_watchdogmode_obj_t watchdog_watchdogmode_reset_obj;
48-
4940
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WATCHDOG_WATCHDOGMODE_H

shared-bindings/watchdog/WatchDogTimer.c

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,14 @@ STATIC mp_obj_t watchdog_watchdogtimer_feed(mp_obj_t self_in) {
7171
STATIC MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_feed_obj, watchdog_watchdogtimer_feed);
7272

7373
//| def deinit(self) -> None:
74-
//| """Stop the watchdog timer. This may raise an error if the watchdog
75-
//| timer cannot be disabled on this platform."""
74+
//| """Stop the watchdog timer.
75+
//|
76+
//| :raises RuntimeError: if the watchdog timer cannot be disabled on this platform.
77+
//|
78+
//| .. note:: This is deprecated in ``8.1.0`` and will be removed in ``9.0.0``.
79+
//| Set watchdog `mode` to `WatchDogMode.NONE` instead.
80+
//|
81+
//| """
7682
//| ...
7783
STATIC mp_obj_t watchdog_watchdogtimer_deinit(mp_obj_t self_in) {
7884
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_
94100
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
95101
mp_float_t timeout = mp_obj_get_float(timeout_obj);
96102

97-
mp_arg_validate_int_min((int)timeout, 0, MP_QSTR_timeout);
103+
mp_arg_validate_int_min(timeout, 0, MP_QSTR_timeout);
98104

99105
common_hal_watchdog_set_timeout(self, timeout);
100106
return mp_const_none;
@@ -122,27 +128,13 @@ MP_PROPERTY_GETSET(watchdog_watchdogtimer_timeout_obj,
122128
//|
123129
STATIC mp_obj_t watchdog_watchdogtimer_obj_get_mode(mp_obj_t self_in) {
124130
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
125-
return watchdog_watchdogmode_type_to_obj(common_hal_watchdog_get_mode(self));
131+
return cp_enum_find(&watchdog_watchdogmode_type, common_hal_watchdog_get_mode(self));
126132
}
127133
MP_DEFINE_CONST_FUN_OBJ_1(watchdog_watchdogtimer_get_mode_obj, watchdog_watchdogtimer_obj_get_mode);
128134

129-
STATIC mp_obj_t watchdog_watchdogtimer_obj_set_mode(mp_obj_t self_in, mp_obj_t mode_obj) {
135+
STATIC mp_obj_t watchdog_watchdogtimer_obj_set_mode(mp_obj_t self_in, mp_obj_t obj) {
130136
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
131-
watchdog_watchdogmode_t current_mode = common_hal_watchdog_get_mode(self);
132-
watchdog_watchdogmode_t new_mode = watchdog_watchdogmode_obj_to_type(mode_obj);
133-
mp_float_t current_timeout = common_hal_watchdog_get_timeout(self);
134-
135-
// When setting the mode, the timeout value must be greater than zero
136-
if (new_mode == WATCHDOGMODE_RESET || new_mode == WATCHDOGMODE_RAISE) {
137-
mp_arg_validate_int_min((int)current_timeout, 0, MP_QSTR_timeout);
138-
}
139-
140-
// Don't allow changing the mode once the watchdog timer has been started
141-
if (current_mode == WATCHDOGMODE_RESET && new_mode != WATCHDOGMODE_RESET) {
142-
mp_raise_TypeError(translate("WatchDogTimer.mode cannot be changed once set to WatchDogMode.RESET"));
143-
}
144-
145-
common_hal_watchdog_set_mode(self, new_mode);
137+
common_hal_watchdog_set_mode(self, cp_enum_value(&watchdog_watchdogmode_type, obj, MP_QSTR_mode));
146138
return mp_const_none;
147139
}
148140
MP_DEFINE_CONST_FUN_OBJ_2(watchdog_watchdogtimer_set_mode_obj, watchdog_watchdogtimer_obj_set_mode);

0 commit comments

Comments
 (0)