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 * Copyright (c) 2017 Eric Poulsen
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
28 #include <string.h>
29
30 #include "py/nlr.h"
31 #include "py/obj.h"
32 #include "py/runtime.h"
33
34 #include "ulog/ulog.h"
35 #include "aos_hal_wdg.h"
36
37 #define LOG_TAG "machine-wdt"
38
39 #define DEFAULT_WDT_TIMEOUT (5000)
40
41 const mp_obj_type_t machine_wdt_type;
42
43 typedef struct _machine_wdt_obj_t {
44 mp_obj_base_t base;
45 wdg_dev_t dev;
46 } machine_wdt_obj_t;
47
48 STATIC machine_wdt_obj_t wdt_default = {{&machine_wdt_type}, {0, {DEFAULT_WDT_TIMEOUT}, NULL}};
49
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)50 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) {
51 enum { ARG_id, ARG_timeout };
52 static const mp_arg_t allowed_args[] = {
53 { MP_QSTR_id, MP_ARG_INT, {.u_int = 0} },
54 { MP_QSTR_timeout, MP_ARG_INT, {.u_int = DEFAULT_WDT_TIMEOUT} }
55 };
56 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
57 mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
58
59 if (args[ARG_id].u_int != 0) {
60 mp_raise_ValueError(NULL);
61 }
62
63 // Convert milliseconds to seconds
64 args[ARG_timeout].u_int /= 1000;
65
66 if (args[ARG_timeout].u_int <= 0) {
67 mp_raise_ValueError(MP_ERROR_TEXT("WDT timeout too short"));
68 }
69
70 wdg_dev_t *dev = &(wdt_default.dev);
71 wdg_config_t cfg = {0};
72 cfg.timeout = args[ARG_timeout].u_int;
73 dev->config = cfg;
74
75 mp_int_t rs_code = aos_hal_wdg_init(dev);
76 if (rs_code != 0) {
77 mp_raise_OSError(rs_code);
78 }
79
80 return &wdt_default;
81 }
82
machine_wdt_feed(mp_obj_t self_in)83 STATIC mp_obj_t machine_wdt_feed(mp_obj_t self_in) {
84 machine_wdt_obj_t *self = (machine_wdt_obj_t*)self_in;
85 aos_hal_wdg_reload(&self->dev);
86 return mp_const_none;
87 }
88 STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_wdt_feed_obj, machine_wdt_feed);
89
90 STATIC const mp_rom_map_elem_t machine_wdt_locals_dict_table[] = {
91 { MP_ROM_QSTR(MP_QSTR_feed), MP_ROM_PTR(&machine_wdt_feed_obj) },
92 };
93 STATIC MP_DEFINE_CONST_DICT(machine_wdt_locals_dict, machine_wdt_locals_dict_table);
94
95 const mp_obj_type_t machine_wdt_type = {
96 { &mp_type_type },
97 .name = MP_QSTR_WDT,
98 .make_new = machine_wdt_make_new,
99 .locals_dict = (mp_obj_t)&machine_wdt_locals_dict,
100 };
101