Skip to content

Commit a66f598

Browse files
committed
zephyr: Create machine.Timer class implementation.
Signed-off-by: danicampora <[email protected]>
1 parent 1034b17 commit a66f598

File tree

5 files changed

+200
-0
lines changed

5 files changed

+200
-0
lines changed

ports/zephyr/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ set(MICROPY_SOURCE_PORT
4040
machine_i2c.c
4141
machine_spi.c
4242
machine_pin.c
43+
machine_timer.c
4344
modbluetooth_zephyr.c
4445
modsocket.c
4546
modzephyr.c

ports/zephyr/machine_timer.c

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* Development of the code in this file was sponsored by Microbric Pty Ltd
5+
*
6+
* The MIT License (MIT)
7+
*
8+
* Copyright (c) 2013-2015 Damien P. George
9+
* Copyright (c) 2016 Paul Sokolovsky
10+
* Copyright (c) 2025 Daniel Campora on behalf of REMOTE TECH LTD
11+
*
12+
* Permission is hereby granted, free of charge, to any person obtaining a copy
13+
* of this software and associated documentation files (the "Software"), to deal
14+
* in the Software without restriction, including without limitation the rights
15+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16+
* copies of the Software, and to permit persons to whom the Software is
17+
* furnished to do so, subject to the following conditions:
18+
*
19+
* The above copyright notice and this permission notice shall be included in
20+
* all copies or substantial portions of the Software.
21+
*
22+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28+
* THE SOFTWARE.
29+
*/
30+
31+
#include <stdint.h>
32+
#include <stdio.h>
33+
34+
#include "py/obj.h"
35+
#include "py/runtime.h"
36+
#include "modmachine.h"
37+
#include <zephyr/kernel.h>
38+
#include <zephyr/device.h>
39+
#include <zephyr/sys/printk.h>
40+
#include <stdbool.h>
41+
42+
#include "py/mperrno.h"
43+
44+
45+
#define TIMER_SCALE 1000 // period is given in ms
46+
47+
48+
typedef struct _machine_timer_obj_t {
49+
mp_obj_base_t base;
50+
51+
struct k_timer my_timer;
52+
53+
mp_uint_t repeat;
54+
uint32_t period; // in milliseconds
55+
56+
mp_obj_t callback;
57+
58+
struct _machine_timer_obj_t *next;
59+
} machine_timer_obj_t;
60+
61+
const mp_obj_type_t machine_timer_type;
62+
63+
static mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
64+
65+
void machine_timer_deinit_all(void) {
66+
// Disable, deallocate and remove all timers from list
67+
machine_timer_obj_t **t = &MP_STATE_PORT(machine_timer_obj_head);
68+
while (*t != NULL) {
69+
k_timer_stop(&((*t)->my_timer));
70+
machine_timer_obj_t *next = (*t)->next;
71+
m_del_obj(machine_timer_obj_t, *t);
72+
*t = next;
73+
}
74+
}
75+
76+
static void machine_timer_callback(struct k_timer *timer) {
77+
machine_timer_obj_t *self = (machine_timer_obj_t *)k_timer_user_data_get(timer);
78+
if (!self->repeat) {
79+
k_timer_stop(&self->my_timer);
80+
}
81+
if (self->callback != mp_const_none) {
82+
mp_sched_schedule(self->callback, MP_OBJ_FROM_PTR(self));
83+
}
84+
}
85+
86+
static void machine_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
87+
machine_timer_obj_t *self = self_in;
88+
qstr mode = self->repeat ? MP_QSTR_PERIODIC : MP_QSTR_ONE_SHOT;
89+
mp_printf(print, "Timer(-1, mode=%q, period=%lu ms)", mode, self->period);
90+
}
91+
92+
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) {
93+
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
94+
95+
if (mp_obj_get_int(args[0]) != -1) {
96+
mp_raise_ValueError(MP_ERROR_TEXT("Only virtual timers are supported!"));
97+
}
98+
99+
// Create the new timer.
100+
machine_timer_obj_t *self = mp_obj_malloc(machine_timer_obj_t, &machine_timer_type);
101+
102+
// Add the timer to the linked-list of timers
103+
self->next = MP_STATE_PORT(machine_timer_obj_head);
104+
MP_STATE_PORT(machine_timer_obj_head) = self;
105+
106+
if (n_args > 1 || n_kw > 0) {
107+
mp_map_t kw_args;
108+
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
109+
machine_timer_init_helper(self, n_args - 1, args + 1, &kw_args);
110+
}
111+
return self;
112+
}
113+
114+
static mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
115+
enum {
116+
ARG_mode,
117+
ARG_callback,
118+
ARG_period,
119+
ARG_freq,
120+
};
121+
static const mp_arg_t allowed_args[] = {
122+
{ MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
123+
{ MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
124+
{ MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
125+
#if MICROPY_PY_BUILTINS_FLOAT
126+
{ MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
127+
#else
128+
{ MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
129+
#endif
130+
};
131+
132+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
133+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
134+
135+
#if MICROPY_PY_BUILTINS_FLOAT
136+
if (args[ARG_freq].u_obj != mp_const_none) {
137+
self->period = (uint32_t)(TIMER_SCALE / mp_obj_get_float(args[ARG_freq].u_obj));
138+
}
139+
#else
140+
if (args[ARG_freq].u_int != 0xffffffff) {
141+
self->period = TIMER_SCALE / ((uint64_t)args[ARG_freq].u_int);
142+
}
143+
#endif
144+
else {
145+
self->period = args[ARG_period].u_int;
146+
}
147+
148+
self->repeat = args[ARG_mode].u_int;
149+
self->callback = args[ARG_callback].u_obj;
150+
151+
k_timer_init(&self->my_timer, machine_timer_callback, NULL);
152+
k_timer_user_data_set(&self->my_timer, self);
153+
k_timer_start(&self->my_timer, K_MSEC(self->period), K_MSEC(self->period));
154+
155+
return mp_const_none;
156+
}
157+
158+
static mp_obj_t machine_timer_deinit(mp_obj_t self_in) {
159+
machine_timer_obj_t *self = self_in;
160+
k_timer_stop(&self->my_timer);
161+
return mp_const_none;
162+
}
163+
static MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_deinit_obj, machine_timer_deinit);
164+
165+
static mp_obj_t machine_timer_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
166+
return machine_timer_init_helper(args[0], n_args - 1, args + 1, kw_args);
167+
}
168+
static MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_init_obj, 1, machine_timer_init);
169+
170+
static mp_obj_t machine_timer_value(mp_obj_t self_in) {
171+
machine_timer_obj_t *self = self_in;
172+
k_ticks_t ticks = k_timer_remaining_ticks(&self->my_timer);
173+
return MP_OBJ_NEW_SMALL_INT(k_ticks_to_ms_near32(ticks));
174+
}
175+
static MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_value_obj, machine_timer_value);
176+
177+
static const mp_rom_map_elem_t machine_timer_locals_dict_table[] = {
178+
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&machine_timer_deinit_obj) },
179+
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_timer_deinit_obj) },
180+
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_timer_init_obj) },
181+
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_timer_value_obj) },
182+
{ MP_ROM_QSTR(MP_QSTR_ONE_SHOT), MP_ROM_INT(false) },
183+
{ MP_ROM_QSTR(MP_QSTR_PERIODIC), MP_ROM_INT(true) },
184+
};
185+
static MP_DEFINE_CONST_DICT(machine_timer_locals_dict, machine_timer_locals_dict_table);
186+
187+
MP_DEFINE_CONST_OBJ_TYPE(
188+
machine_timer_type,
189+
MP_QSTR_Timer,
190+
MP_TYPE_FLAG_NONE,
191+
make_new, machine_timer_make_new,
192+
print, machine_timer_print,
193+
locals_dict, &machine_timer_locals_dict
194+
);
195+
196+
MP_REGISTER_ROOT_POINTER(struct _machine_timer_obj_t *machine_timer_obj_head);

ports/zephyr/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ int real_main(void) {
165165
#endif
166166
#if MICROPY_PY_MACHINE
167167
machine_pin_deinit();
168+
machine_timer_deinit_all();
168169
#endif
169170

170171
#if MICROPY_PY_THREAD

ports/zephyr/modmachine.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
MICROPY_PY_MACHINE_RESET_ENTRY \
4545
{ MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, \
4646
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, \
47+
{ MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, \
4748

4849
static mp_obj_t machine_reset(void) {
4950
sys_reboot(SYS_REBOOT_COLD);

ports/zephyr/modmachine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ typedef struct _machine_pin_obj_t {
1111
} machine_pin_obj_t;
1212

1313
void machine_pin_deinit(void);
14+
void machine_timer_deinit_all(void);
1415

1516
#endif // MICROPY_INCLUDED_ZEPHYR_MODMACHINE_H

0 commit comments

Comments
 (0)