machine_timer.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 <stdint.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include "py/obj.h"
  30. #include "py/runtime.h"
  31. #include "modmachine.h"
  32. #include "mphalport.h"
  33. #ifdef MICROPYTHON_USING_MACHINE_TIMER
  34. #include <rtthread.h>
  35. #include <drivers/hwtimer.h>
  36. #include "machine_timer.h"
  37. #define MAX_TIMER 17
  38. typedef struct _machine_timer_obj_t {
  39. mp_obj_base_t base;
  40. rt_device_t timer_device;
  41. mp_obj_t timeout_cb;
  42. uint8_t timerid;
  43. uint32_t timeout;
  44. rt_bool_t is_repeat;
  45. rt_bool_t is_init;
  46. } machine_timer_obj_t;
  47. const mp_obj_type_t machine_timer_type;
  48. STATIC void error_check(bool status, const char *msg) {
  49. if (!status) {
  50. nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, msg));
  51. }
  52. }
  53. STATIC void machine_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  54. machine_timer_obj_t *self = self_in;
  55. mp_printf(print, "Timer(%p; ", self);
  56. mp_printf(print, "timerid=%d, ", self->timerid);
  57. mp_printf(print, "period=%d, ", self->timeout);
  58. mp_printf(print, "auto_reload=%d)", self->is_repeat);
  59. }
  60. STATIC mp_obj_t machine_timer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  61. machine_timer_obj_t *self = m_new_obj(machine_timer_obj_t);
  62. char timer_dev_name[RT_NAME_MAX] = {0};
  63. int device_id = 0;
  64. // check arguments
  65. mp_arg_check_num(n_args, n_kw, 1, 1, true);
  66. device_id = mp_obj_get_int(args[0]);
  67. // find timer device
  68. rt_snprintf(timer_dev_name, sizeof(timer_dev_name), "timer%d", device_id);
  69. self->timer_device = rt_device_find(timer_dev_name);
  70. if (self->timer_device == RT_NULL || self->timer_device->type != RT_Device_Class_Timer) {
  71. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Timer(%s) don't exist", timer_dev_name));
  72. }
  73. // initialize timer device
  74. self->base.type = &machine_timer_type;
  75. self->timerid = device_id;
  76. self->timeout = 0;
  77. self->timeout_cb = RT_NULL;
  78. self->is_repeat = RT_TRUE;
  79. self->is_init = RT_FALSE;
  80. self->timer_device->device_id = device_id-1;
  81. // return constant object
  82. return MP_OBJ_FROM_PTR(self);
  83. }
  84. static machine_timer_obj_t *timer_self[MAX_TIMER] = {RT_NULL};
  85. STATIC mp_obj_t machine_timer_deinit(mp_obj_t self_in) {
  86. machine_timer_obj_t *self = self_in;
  87. rt_err_t result = RT_EOK;
  88. if (self->is_init == RT_TRUE) {
  89. result = rt_device_close(self->timer_device);
  90. error_check(result == RT_EOK, "Timer device close error");
  91. self->is_init = RT_FALSE;
  92. timer_self[self->timer_device->device_id] = RT_NULL;
  93. }
  94. return mp_const_none;
  95. }
  96. STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_deinit_obj, machine_timer_deinit);
  97. STATIC rt_err_t timer_event_handler(rt_device_t dev, rt_size_t size) {
  98. machine_timer_obj_t *self = timer_self[dev->device_id];
  99. mp_sched_schedule(self->timeout_cb, MP_OBJ_FROM_PTR(self));
  100. return RT_EOK;
  101. }
  102. STATIC mp_obj_t machine_timer_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
  103. machine_timer_obj_t *self = (machine_timer_obj_t *)args[0];
  104. rt_bool_t result = RT_EOK;
  105. int mode = 0;
  106. enum {
  107. ARG_mode,
  108. ARG_period,
  109. ARG_callback,
  110. };
  111. static const mp_arg_t allowed_args[] = {
  112. { MP_QSTR_mode, MP_ARG_INT, {.u_int = 1} },
  113. { MP_QSTR_period, MP_ARG_INT, {.u_int = 0xffffffff} },
  114. { MP_QSTR_callback, MP_ARG_OBJ, {.u_obj = mp_const_none} },
  115. };
  116. mp_arg_val_t dargs[MP_ARRAY_SIZE(allowed_args)];
  117. mp_arg_parse_all(n_args - 1, args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, dargs);
  118. if (2 == n_args) {
  119. self->timeout = dargs[0].u_int;
  120. } else if (3 == n_args) {
  121. self->is_repeat = dargs[ARG_mode].u_int;
  122. self->timeout = dargs[ARG_period].u_int;
  123. } else if (4 == n_args) {
  124. self->is_repeat = dargs[ARG_mode].u_int;
  125. self->timeout = dargs[ARG_period].u_int;
  126. self->timeout_cb = dargs[ARG_callback].u_obj;
  127. } else {
  128. mp_raise_ValueError("invalid format");
  129. }
  130. error_check(self->timeout > 0, "Set timeout value error");
  131. if (self->is_init == RT_FALSE)
  132. {
  133. // open timer device
  134. result = rt_device_open(self->timer_device, RT_DEVICE_OFLAG_RDWR);
  135. error_check(result == RT_EOK, "Timer device open error");
  136. }
  137. if (self->timeout_cb != RT_NULL) {
  138. // set callback timer
  139. if (timer_self[self->timer_device->device_id] && timer_self[self->timer_device->device_id] != self) {
  140. // TODO add multi-timer device support
  141. error_check(result == RT_EOK, "Only supports one timer device work");
  142. } else {
  143. timer_self[self->timer_device->device_id] = self;
  144. }
  145. result = rt_device_set_rx_indicate(self->timer_device, timer_event_handler);
  146. error_check(result == RT_EOK, "Timer set timout callback error");
  147. }
  148. // set timer mode
  149. mode = self->is_repeat ? HWTIMER_MODE_PERIOD : HWTIMER_MODE_ONESHOT;
  150. result = rt_device_control(self->timer_device, HWTIMER_CTRL_MODE_SET, &mode);
  151. error_check(result == RT_EOK, "Timer set mode error");
  152. if (self->timeout) {
  153. rt_hwtimerval_t timeout_s;
  154. rt_size_t len;
  155. timeout_s.sec = self->timeout / 1000; // second
  156. timeout_s.usec = self->timeout % 1000; // microsecond
  157. len = rt_device_write(self->timer_device, 0, &timeout_s, sizeof(timeout_s));
  158. error_check(len == sizeof(timeout_s), "Timer set timout error");
  159. }
  160. self->is_init = RT_TRUE;
  161. return mp_const_none;
  162. }
  163. STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_init_obj, 1, machine_timer_init);
  164. STATIC mp_obj_t machine_timer_callback(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
  165. machine_timer_obj_t *self = (machine_timer_obj_t *)args[0];
  166. rt_bool_t result = RT_EOK;
  167. static const mp_arg_t allowed_args[] = {
  168. { MP_QSTR_callback, MP_ARG_OBJ, {.u_obj = mp_const_none} },
  169. };
  170. mp_arg_val_t dargs[MP_ARRAY_SIZE(allowed_args)];
  171. mp_arg_parse_all(n_args - 1, args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, dargs);
  172. self->timeout_cb = dargs[0].u_obj;
  173. if(n_args == 1)
  174. {
  175. self->timeout_cb = RT_NULL;
  176. self->timer_device->rx_indicate = RT_NULL;//注销回调函数
  177. }
  178. else if(n_args == 2)
  179. {
  180. if(self->timeout_cb != mp_const_none)
  181. {
  182. timer_self[self->timer_device->device_id] = self;
  183. result = rt_device_set_rx_indicate(self->timer_device, timer_event_handler); //注册回调函数
  184. error_check(result == RT_EOK, "Timer set timout callback error");
  185. }
  186. else
  187. {
  188. self->timeout_cb = RT_NULL;
  189. self->timer_device->rx_indicate = RT_NULL;//注销回调函数
  190. }
  191. }
  192. return mp_const_none;
  193. }
  194. STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_callback_obj, 0,machine_timer_callback);
  195. STATIC const mp_rom_map_elem_t machine_timer_locals_dict_table[] = {
  196. { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_timer_deinit_obj) },
  197. { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_timer_init_obj) },
  198. { MP_ROM_QSTR(MP_QSTR_callback), MP_ROM_PTR(&machine_timer_callback_obj) },
  199. { MP_ROM_QSTR(MP_QSTR_ONE_SHOT), MP_ROM_INT(RT_FALSE) },
  200. { MP_ROM_QSTR(MP_QSTR_PERIODIC), MP_ROM_INT(RT_TRUE) },
  201. };
  202. STATIC MP_DEFINE_CONST_DICT(machine_timer_locals_dict, machine_timer_locals_dict_table);
  203. const mp_obj_type_t machine_timer_type = {
  204. { &mp_type_type },
  205. .name = MP_QSTR_Timer,
  206. .print = machine_timer_print,
  207. .make_new = machine_timer_make_new,
  208. .locals_dict = (mp_obj_t) &machine_timer_locals_dict,
  209. };
  210. #endif // MICROPYTHON_USING_MACHINE_TIMER