machine_timer.c 7.2 KB

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