|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2016 Damien P. George |
| 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 <stdio.h> |
| 28 | + |
| 29 | +#include STM32_HAL_H |
| 30 | + |
| 31 | +#include "py/runtime.h" |
| 32 | +#include "wdt.h" |
| 33 | + |
| 34 | +typedef struct _pyb_wdt_obj_t { |
| 35 | + mp_obj_base_t base; |
| 36 | +} pyb_wdt_obj_t; |
| 37 | + |
| 38 | +STATIC pyb_wdt_obj_t pyb_wdt = {{&pyb_wdt_type}}; |
| 39 | + |
| 40 | +STATIC mp_obj_t pyb_wdt_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
| 41 | + // check arguments |
| 42 | + mp_arg_check_num(n_args, n_kw, 2, 2, false); |
| 43 | + |
| 44 | + mp_int_t id = mp_obj_get_int(args[0]); |
| 45 | + if (id != 0) { |
| 46 | + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "WDT(%d) does not exist", id)); |
| 47 | + } |
| 48 | + |
| 49 | + // timeout is in milliseconds |
| 50 | + mp_int_t timeout = mp_obj_get_int(args[1]); |
| 51 | + |
| 52 | + // compute prescaler |
| 53 | + uint32_t prescaler; |
| 54 | + for (prescaler = 0; prescaler < 6 && timeout >= 512; ++prescaler, timeout /= 2) { |
| 55 | + } |
| 56 | + |
| 57 | + // convert milliseconds to ticks |
| 58 | + timeout *= 8; // 32kHz / 4 = 8 ticks per millisecond (approx) |
| 59 | + if (timeout <= 0) { |
| 60 | + mp_raise_ValueError("WDT timeout too short"); |
| 61 | + } else if (timeout > 0xfff) { |
| 62 | + mp_raise_ValueError("WDT timeout too long"); |
| 63 | + } |
| 64 | + timeout -= 1; |
| 65 | + |
| 66 | + // set the reload register |
| 67 | + while (IWDG->SR & 2) { |
| 68 | + } |
| 69 | + IWDG->KR = 0x5555; |
| 70 | + IWDG->RLR = timeout; |
| 71 | + |
| 72 | + // set the prescaler |
| 73 | + while (IWDG->SR & 1) { |
| 74 | + } |
| 75 | + IWDG->KR = 0x5555; |
| 76 | + IWDG->PR = prescaler; |
| 77 | + |
| 78 | + // start the watch dog |
| 79 | + IWDG->KR = 0xcccc; |
| 80 | + |
| 81 | + return (mp_obj_t)&pyb_wdt; |
| 82 | +} |
| 83 | + |
| 84 | +STATIC mp_obj_t pyb_wdt_feed(mp_obj_t self_in) { |
| 85 | + (void)self_in; |
| 86 | + IWDG->KR = 0xaaaa; |
| 87 | + return mp_const_none; |
| 88 | +} |
| 89 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_wdt_feed_obj, pyb_wdt_feed); |
| 90 | + |
| 91 | +STATIC const mp_map_elem_t pyb_wdt_locals_dict_table[] = { |
| 92 | + { MP_OBJ_NEW_QSTR(MP_QSTR_feed), (mp_obj_t)&pyb_wdt_feed_obj }, |
| 93 | +}; |
| 94 | + |
| 95 | +STATIC MP_DEFINE_CONST_DICT(pyb_wdt_locals_dict, pyb_wdt_locals_dict_table); |
| 96 | + |
| 97 | +const mp_obj_type_t pyb_wdt_type = { |
| 98 | + { &mp_type_type }, |
| 99 | + .name = MP_QSTR_WDT, |
| 100 | + .make_new = pyb_wdt_make_new, |
| 101 | + .locals_dict = (mp_obj_t)&pyb_wdt_locals_dict, |
| 102 | +}; |
0 commit comments