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