machine_wdt.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2019 ChenYong (chenyong@rt-thread.com)
  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 <string.h>
  27. #include "py/nlr.h"
  28. #include "py/obj.h"
  29. #include "py/runtime.h"
  30. #include "modmachine.h"
  31. #include "mphalport.h"
  32. #ifdef MICROPYTHON_USING_MACHINE_WDT
  33. #include <rtthread.h>
  34. #include <drivers/watchdog.h>
  35. #include "machine_wdt.h"
  36. typedef struct _machine_wdt_obj_t {
  37. mp_obj_base_t base;
  38. rt_device_t wdt_device;
  39. }machine_wdt_obj_t;
  40. const mp_obj_type_t machine_wdt_type;
  41. STATIC void error_check(bool status, const char *msg) {
  42. if (!status) {
  43. nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, msg));
  44. }
  45. }
  46. 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 *args) {
  47. #define MP_WDT_DEV_NAME "wdt"
  48. machine_wdt_obj_t *self = m_new_obj(machine_wdt_obj_t);
  49. char wdt_dev_name[RT_NAME_MAX] = {0};
  50. rt_err_t result = RT_EOK;
  51. mp_int_t timeout = 5;
  52. // check arguments
  53. mp_arg_check_num(n_args, n_kw, 0, 2, false);
  54. if (n_args == 2) {
  55. // check input WDT device name or ID
  56. if (mp_obj_is_small_int(args[0])) {
  57. rt_snprintf(wdt_dev_name, sizeof(wdt_dev_name), "wdt%d", mp_obj_get_int(args[0]));
  58. } else if (mp_obj_is_qstr(args[0])) {
  59. rt_strncpy(wdt_dev_name, mp_obj_str_get_str(args[0]), RT_NAME_MAX);
  60. } else {
  61. error_check(0, "Input WDT device name or ID error.");
  62. }
  63. timeout = mp_obj_get_int(args[1]);
  64. error_check(timeout >= 1, "input timeout value error");
  65. } else if (n_args == 1) {
  66. if (mp_obj_is_small_int(args[0])) {
  67. timeout = mp_obj_get_int(args[0]);
  68. error_check(timeout >= 1, "input timeout value error");
  69. } else if (mp_obj_is_qstr(args[0])) {
  70. rt_strncpy(wdt_dev_name, mp_obj_str_get_str(args[0]), RT_NAME_MAX);
  71. } else {
  72. error_check(0, "Input WDT device name or ID error.");
  73. }
  74. } else {
  75. rt_strncpy(wdt_dev_name, MP_WDT_DEV_NAME, RT_NAME_MAX);
  76. }
  77. self->base.type = &machine_wdt_type;
  78. // find WDT device
  79. self->wdt_device = rt_device_find(wdt_dev_name);
  80. if (self->wdt_device == RT_NULL || self->wdt_device->type != RT_Device_Class_Miscellaneous) {
  81. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "WDT(%s) don't exist", wdt_dev_name));
  82. }
  83. result = rt_device_init(self->wdt_device);
  84. error_check(result == RT_EOK, "WDT init error");
  85. // set WDT device timout
  86. result = rt_device_control(self->wdt_device, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, (void *)&timeout);
  87. error_check(result == RT_EOK, "WDT set timout error");
  88. result = rt_device_control(self->wdt_device, RT_DEVICE_CTRL_WDT_START, RT_NULL);
  89. error_check(result == RT_EOK, "WDT start error");
  90. // return constant object
  91. return MP_OBJ_FROM_PTR(self);
  92. }
  93. STATIC mp_obj_t machine_wdt_feed(mp_obj_t self_in) {
  94. /* idle task feed */
  95. machine_wdt_obj_t *self = MP_OBJ_TO_PTR(self_in);
  96. rt_err_t result = RT_EOK;
  97. result = rt_device_control(self->wdt_device, RT_DEVICE_CTRL_WDT_KEEPALIVE, RT_NULL);
  98. error_check(result == RT_EOK, "WDT feed failed");
  99. return mp_const_none;
  100. }
  101. STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_wdt_feed_obj, machine_wdt_feed);
  102. STATIC const mp_rom_map_elem_t machine_wdt_locals_dict_table[] = {
  103. { MP_ROM_QSTR(MP_QSTR_feed), MP_ROM_PTR(&machine_wdt_feed_obj) },
  104. };
  105. STATIC MP_DEFINE_CONST_DICT(machine_wdt_locals_dict, machine_wdt_locals_dict_table);
  106. const mp_obj_type_t machine_wdt_type = {
  107. { &mp_type_type },
  108. .name = MP_QSTR_WDT,
  109. .make_new = machine_wdt_make_new,
  110. .locals_dict = (mp_obj_t) &machine_wdt_locals_dict,
  111. };
  112. #endif // MICROPYTHON_USING_MACHINE_WDT