|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2016 Paul Sokolovsky |
| 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 <string.h> |
| 28 | + |
| 29 | +#include "py/obj.h" |
| 30 | +#include "py/objproperty.h" |
| 31 | +#include "py/runtime.h" |
| 32 | +#include "shared-bindings/wdt/__init__.h" |
| 33 | +#include "shared-bindings/wdt/WDT.h" |
| 34 | + |
| 35 | +static wdt_wdt_obj_t *wdt_singleton; |
| 36 | + |
| 37 | +//| class WDT: |
| 38 | +//| """Watchdog Timer""" |
| 39 | +//| |
| 40 | +//| def __init__(self, ): |
| 41 | +//| """This class represents the system's Watchdog Timer. It is a |
| 42 | +//| singleton and will always return the same instance.""" |
| 43 | +//| ... |
| 44 | +//| |
| 45 | +STATIC mp_obj_t wdt_wdt_make_new(const mp_obj_type_t *type, size_t n_args, |
| 46 | + const mp_obj_t *pos_args, |
| 47 | + mp_map_t *kw_args) { |
| 48 | + enum { ARG_timeout_ms, ARG_sleep }; |
| 49 | + static const mp_arg_t allowed_args[] = { |
| 50 | + {MP_QSTR_timeout_ms, MP_ARG_INT | MP_ARG_REQUIRED}, |
| 51 | + {MP_QSTR_sleep, MP_ARG_BOOL, {.u_bool = false}}, |
| 52 | + }; |
| 53 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 54 | + |
| 55 | + if (wdt_singleton) |
| 56 | + return wdt_singleton; |
| 57 | + |
| 58 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), |
| 59 | + allowed_args, args); |
| 60 | + |
| 61 | + if (args[ARG_timeout_ms].u_int <= 0) { |
| 62 | + mp_raise_ValueError(translate("watchdog timeout must be greater than 0")); |
| 63 | + } |
| 64 | + |
| 65 | + wdt_wdt_obj_t *self = m_new_obj(wdt_wdt_obj_t); |
| 66 | + self->base.type = &wdt_wdt_type; |
| 67 | + self->timeout = args[ARG_timeout_ms].u_int; |
| 68 | + self->sleep = args[ARG_sleep].u_bool; |
| 69 | + |
| 70 | + common_hal_wdt_init(self->timeout, self->sleep); |
| 71 | + wdt_singleton = self; |
| 72 | + return MP_OBJ_FROM_PTR(self); |
| 73 | +} |
| 74 | + |
| 75 | +//| def feed(self): |
| 76 | +//| """Feed the watchdog timer. This must be called regularly, otherwise |
| 77 | +//| the system will reset.""" |
| 78 | +//| ... |
| 79 | +//| |
| 80 | +STATIC mp_obj_t wdt_wdt_feed(mp_obj_t self_in) { |
| 81 | + (void)self_in; |
| 82 | + common_hal_wdt_feed(); |
| 83 | + return mp_const_none; |
| 84 | +} |
| 85 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(wdt_wdt_feed_obj, wdt_wdt_feed); |
| 86 | + |
| 87 | +//| def deinit(self): |
| 88 | +//| """Stop the watchdog timer. This may raise an error if the watchdog |
| 89 | +//| timer cannot be disabled on this platform.""" |
| 90 | +//| ... |
| 91 | +//| |
| 92 | +STATIC mp_obj_t wdt_wdt_deinit(mp_obj_t self_in) { |
| 93 | + (void)self_in; |
| 94 | + common_hal_wdt_disable(); |
| 95 | + return mp_const_none; |
| 96 | +} |
| 97 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(wdt_wdt_deinit_obj, wdt_wdt_deinit); |
| 98 | + |
| 99 | +//| timeout: int = ... |
| 100 | +//| """The maximum number of milliseconds that can elapse between calls |
| 101 | +//| to feed()""" |
| 102 | +//| |
| 103 | +STATIC mp_obj_t wdt_wdt_obj_get_timeout(mp_obj_t self_in) { |
| 104 | + wdt_wdt_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 105 | + return mp_obj_new_int(self->timeout); |
| 106 | +} |
| 107 | +MP_DEFINE_CONST_FUN_OBJ_1(wdt_wdt_obj_get_timeout_obj, wdt_wdt_obj_get_timeout); |
| 108 | + |
| 109 | +const mp_obj_property_t wdt_wdt_timeout_obj = { |
| 110 | + .base.type = &mp_type_property, |
| 111 | + .proxy = {(mp_obj_t)&wdt_wdt_obj_get_timeout_obj, |
| 112 | + (mp_obj_t)&mp_const_none_obj, |
| 113 | + (mp_obj_t)&mp_const_none_obj}, |
| 114 | +}; |
| 115 | + |
| 116 | +STATIC const mp_rom_map_elem_t wdt_wdt_locals_dict_table[] = { |
| 117 | + { MP_ROM_QSTR(MP_QSTR_feed), MP_ROM_PTR(&wdt_wdt_feed_obj) }, |
| 118 | + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&wdt_wdt_deinit_obj) }, |
| 119 | + { MP_ROM_QSTR(MP_QSTR_timeout), MP_ROM_PTR(&wdt_wdt_timeout_obj) }, |
| 120 | +}; |
| 121 | +STATIC MP_DEFINE_CONST_DICT(wdt_wdt_locals_dict, wdt_wdt_locals_dict_table); |
| 122 | + |
| 123 | +const mp_obj_type_t wdt_wdt_type = { |
| 124 | + { &mp_type_type }, |
| 125 | + .name = MP_QSTR_WDT, |
| 126 | + .make_new = wdt_wdt_make_new, |
| 127 | + .locals_dict = (mp_obj_dict_t*)&wdt_wdt_locals_dict, |
| 128 | +}; |
0 commit comments