wdt.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #include <stdio.h>
  27. #include "py/runtime.h"
  28. #include "wdt.h"
  29. #if defined(STM32H7)
  30. #define IWDG (IWDG1)
  31. #endif
  32. typedef struct _pyb_wdt_obj_t {
  33. mp_obj_base_t base;
  34. } pyb_wdt_obj_t;
  35. STATIC pyb_wdt_obj_t pyb_wdt = {{&pyb_wdt_type}};
  36. 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 *all_args) {
  37. // parse arguments
  38. enum { ARG_id, ARG_timeout };
  39. static const mp_arg_t allowed_args[] = {
  40. { MP_QSTR_id, MP_ARG_INT, {.u_int = 0} },
  41. { MP_QSTR_timeout, MP_ARG_INT, {.u_int = 5000} },
  42. };
  43. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  44. mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  45. mp_int_t id = args[ARG_id].u_int;
  46. if (id != 0) {
  47. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "WDT(%d) doesn't exist", id));
  48. }
  49. // timeout is in milliseconds
  50. mp_int_t timeout = args[ARG_timeout].u_int;
  51. // compute prescaler
  52. uint32_t prescaler;
  53. for (prescaler = 0; prescaler < 6 && timeout >= 512; ++prescaler, timeout /= 2) {
  54. }
  55. // convert milliseconds to ticks
  56. timeout *= 8; // 32kHz / 4 = 8 ticks per millisecond (approx)
  57. if (timeout <= 0) {
  58. mp_raise_ValueError("WDT timeout too short");
  59. } else if (timeout > 0xfff) {
  60. mp_raise_ValueError("WDT timeout too long");
  61. }
  62. timeout -= 1;
  63. // set the reload register
  64. while (IWDG->SR & 2) {
  65. }
  66. IWDG->KR = 0x5555;
  67. IWDG->RLR = timeout;
  68. // set the prescaler
  69. while (IWDG->SR & 1) {
  70. }
  71. IWDG->KR = 0x5555;
  72. IWDG->PR = prescaler;
  73. // start the watch dog
  74. IWDG->KR = 0xcccc;
  75. return MP_OBJ_FROM_PTR(&pyb_wdt);
  76. }
  77. STATIC mp_obj_t pyb_wdt_feed(mp_obj_t self_in) {
  78. (void)self_in;
  79. IWDG->KR = 0xaaaa;
  80. return mp_const_none;
  81. }
  82. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_wdt_feed_obj, pyb_wdt_feed);
  83. STATIC const mp_rom_map_elem_t pyb_wdt_locals_dict_table[] = {
  84. { MP_ROM_QSTR(MP_QSTR_feed), MP_ROM_PTR(&pyb_wdt_feed_obj) },
  85. };
  86. STATIC MP_DEFINE_CONST_DICT(pyb_wdt_locals_dict, pyb_wdt_locals_dict_table);
  87. const mp_obj_type_t pyb_wdt_type = {
  88. { &mp_type_type },
  89. .name = MP_QSTR_WDT,
  90. .make_new = pyb_wdt_make_new,
  91. .locals_dict = (mp_obj_dict_t*)&pyb_wdt_locals_dict,
  92. };