pybwdt.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2015 Daniel Campora
  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 <stdint.h>
  27. #include "py/mpconfig.h"
  28. #include "py/obj.h"
  29. #include "py/runtime.h"
  30. #include "py/mperrno.h"
  31. #include "py/mphal.h"
  32. #include "inc/hw_types.h"
  33. #include "inc/hw_gpio.h"
  34. #include "inc/hw_ints.h"
  35. #include "inc/hw_memmap.h"
  36. #include "rom_map.h"
  37. #include "wdt.h"
  38. #include "prcm.h"
  39. #include "utils.h"
  40. #include "pybwdt.h"
  41. #include "mpexception.h"
  42. #include "mperror.h"
  43. /******************************************************************************
  44. DECLARE CONSTANTS
  45. ******************************************************************************/
  46. #define PYBWDT_MILLISECONDS_TO_TICKS(ms) ((80000000 / 1000) * (ms))
  47. #define PYBWDT_MIN_TIMEOUT_MS (1000)
  48. /******************************************************************************
  49. DECLARE TYPES
  50. ******************************************************************************/
  51. typedef struct {
  52. mp_obj_base_t base;
  53. bool servers;
  54. bool servers_sleeping;
  55. bool simplelink;
  56. bool running;
  57. } pyb_wdt_obj_t;
  58. /******************************************************************************
  59. DECLARE PRIVATE DATA
  60. ******************************************************************************/
  61. STATIC pyb_wdt_obj_t pyb_wdt_obj = {.servers = false, .servers_sleeping = false, .simplelink = false, .running = false};
  62. /******************************************************************************
  63. DEFINE PUBLIC FUNCTIONS
  64. ******************************************************************************/
  65. // must be called in main.c just after initializing the hal
  66. __attribute__ ((section (".boot")))
  67. void pybwdt_init0 (void) {
  68. }
  69. void pybwdt_srv_alive (void) {
  70. pyb_wdt_obj.servers = true;
  71. }
  72. void pybwdt_srv_sleeping (bool state) {
  73. pyb_wdt_obj.servers_sleeping = state;
  74. }
  75. void pybwdt_sl_alive (void) {
  76. pyb_wdt_obj.simplelink = true;
  77. }
  78. /******************************************************************************/
  79. // MicroPython bindings
  80. STATIC const mp_arg_t pyb_wdt_init_args[] = {
  81. { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} },
  82. { MP_QSTR_timeout, MP_ARG_INT, {.u_int = 5000} }, // 5 s
  83. };
  84. 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) {
  85. // check the arguments
  86. mp_map_t kw_args;
  87. mp_map_init_fixed_table(&kw_args, n_kw, all_args + n_args);
  88. mp_arg_val_t args[MP_ARRAY_SIZE(pyb_wdt_init_args)];
  89. mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(args), pyb_wdt_init_args, args);
  90. if (args[0].u_obj != mp_const_none && mp_obj_get_int(args[0].u_obj) > 0) {
  91. mp_raise_OSError(MP_ENODEV);
  92. }
  93. uint timeout_ms = args[1].u_int;
  94. if (timeout_ms < PYBWDT_MIN_TIMEOUT_MS) {
  95. mp_raise_ValueError(mpexception_value_invalid_arguments);
  96. }
  97. if (pyb_wdt_obj.running) {
  98. mp_raise_OSError(MP_EPERM);
  99. }
  100. // Enable the WDT peripheral clock
  101. MAP_PRCMPeripheralClkEnable(PRCM_WDT, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
  102. // Unlock to be able to configure the registers
  103. MAP_WatchdogUnlock(WDT_BASE);
  104. #ifdef DEBUG
  105. // make the WDT stall when the debugger stops on a breakpoint
  106. MAP_WatchdogStallEnable (WDT_BASE);
  107. #endif
  108. // set the watchdog timer reload value
  109. // the WDT trigger a system reset after the second timeout
  110. // so, divide by 2 the timeout value received
  111. MAP_WatchdogReloadSet(WDT_BASE, PYBWDT_MILLISECONDS_TO_TICKS(timeout_ms / 2));
  112. // start the timer. Once it's started, it cannot be disabled.
  113. MAP_WatchdogEnable(WDT_BASE);
  114. pyb_wdt_obj.base.type = &pyb_wdt_type;
  115. pyb_wdt_obj.running = true;
  116. return (mp_obj_t)&pyb_wdt_obj;
  117. }
  118. STATIC mp_obj_t pyb_wdt_feed(mp_obj_t self_in) {
  119. pyb_wdt_obj_t *self = self_in;
  120. if ((self->servers || self->servers_sleeping) && self->simplelink && self->running) {
  121. self->servers = false;
  122. self->simplelink = false;
  123. MAP_WatchdogIntClear(WDT_BASE);
  124. }
  125. return mp_const_none;
  126. }
  127. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_wdt_feed_obj, pyb_wdt_feed);
  128. STATIC const mp_rom_map_elem_t pybwdt_locals_dict_table[] = {
  129. { MP_ROM_QSTR(MP_QSTR_feed), MP_ROM_PTR(&pyb_wdt_feed_obj) },
  130. };
  131. STATIC MP_DEFINE_CONST_DICT(pybwdt_locals_dict, pybwdt_locals_dict_table);
  132. const mp_obj_type_t pyb_wdt_type = {
  133. { &mp_type_type },
  134. .name = MP_QSTR_WDT,
  135. .make_new = pyb_wdt_make_new,
  136. .locals_dict = (mp_obj_t)&pybwdt_locals_dict,
  137. };