machine_timer.c 9.3 KB

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