Skip to content

Commit 233f503

Browse files
danicamporadpgeorge
authored andcommitted
zephyr/machine_timer: Add machine.Timer class implementation.
Simple `machine.Timer` implementation in-line with the rest of the MicroPython ports. Note: Only virtual timers are supported (not linked to any particular hardware peripheral). Tested with the nRF5340 and the nRF52840. Signed-off-by: danicampora <[email protected]>
1 parent 859b6ef commit 233f503

File tree

5 files changed

+197
-0
lines changed

5 files changed

+197
-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: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2025 Daniel Campora on behalf of REMOTE TECH LTD
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+
30+
#include "py/mperrno.h"
31+
#include "py/obj.h"
32+
#include "py/runtime.h"
33+
#include "modmachine.h"
34+
#include <zephyr/kernel.h>
35+
#include <zephyr/device.h>
36+
#include <zephyr/sys/printk.h>
37+
38+
#define TIMER_MS_PER_TICK (1000)
39+
40+
typedef enum _machine_timer_mode_t {
41+
TIMER_MODE_ONE_SHOT = 0,
42+
TIMER_MODE_PERIODIC
43+
} machine_timer_mode_t;
44+
45+
typedef struct _machine_timer_obj_t {
46+
mp_obj_base_t base;
47+
48+
struct k_timer my_timer;
49+
50+
machine_timer_mode_t mode;
51+
uint32_t period_ms;
52+
53+
mp_obj_t callback;
54+
55+
struct _machine_timer_obj_t *next;
56+
} machine_timer_obj_t;
57+
58+
const mp_obj_type_t machine_timer_type;
59+
60+
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);
61+
62+
void machine_timer_deinit_all(void) {
63+
// Disable, deallocate and remove all timers from list
64+
machine_timer_obj_t **t = &MP_STATE_PORT(machine_timer_obj_head);
65+
while (*t != NULL) {
66+
k_timer_stop(&((*t)->my_timer));
67+
machine_timer_obj_t *next = (*t)->next;
68+
m_del_obj(machine_timer_obj_t, *t);
69+
*t = next;
70+
}
71+
}
72+
73+
static void machine_timer_callback(struct k_timer *timer) {
74+
machine_timer_obj_t *self = (machine_timer_obj_t *)k_timer_user_data_get(timer);
75+
if (self->mode == TIMER_MODE_ONE_SHOT) {
76+
k_timer_stop(&self->my_timer);
77+
}
78+
if (self->callback != mp_const_none) {
79+
mp_sched_schedule(self->callback, MP_OBJ_FROM_PTR(self));
80+
}
81+
}
82+
83+
static void machine_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
84+
machine_timer_obj_t *self = self_in;
85+
qstr mode_str = (self->mode == TIMER_MODE_PERIODIC) ? MP_QSTR_PERIODIC : MP_QSTR_ONE_SHOT;
86+
mp_printf(print, "Timer(-1, mode=%q, period=%lu ms)", mode_str, self->period_ms);
87+
}
88+
89+
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) {
90+
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
91+
92+
if (mp_obj_get_int(args[0]) != -1) {
93+
mp_raise_ValueError(MP_ERROR_TEXT("only virtual timers are supported"));
94+
}
95+
96+
// Create the new timer.
97+
machine_timer_obj_t *self = mp_obj_malloc(machine_timer_obj_t, &machine_timer_type);
98+
99+
// Add the timer to the linked-list of timers
100+
self->next = MP_STATE_PORT(machine_timer_obj_head);
101+
MP_STATE_PORT(machine_timer_obj_head) = self;
102+
103+
if (n_args > 1 || n_kw > 0) {
104+
mp_map_t kw_args;
105+
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
106+
machine_timer_init_helper(self, n_args - 1, args + 1, &kw_args);
107+
}
108+
return self;
109+
}
110+
111+
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) {
112+
enum {
113+
ARG_mode,
114+
ARG_callback,
115+
ARG_period,
116+
ARG_freq,
117+
};
118+
static const mp_arg_t allowed_args[] = {
119+
{ MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = TIMER_MODE_PERIODIC} },
120+
{ MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
121+
{ MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
122+
#if MICROPY_PY_BUILTINS_FLOAT
123+
{ MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
124+
#else
125+
{ MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
126+
#endif
127+
};
128+
129+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
130+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
131+
132+
#if MICROPY_PY_BUILTINS_FLOAT
133+
if (args[ARG_freq].u_obj != mp_const_none) {
134+
self->period_ms = (uint32_t)(TIMER_MS_PER_TICK / mp_obj_get_float(args[ARG_freq].u_obj));
135+
}
136+
#else
137+
if (args[ARG_freq].u_int != 0xffffffff) {
138+
self->period_ms = TIMER_MS_PER_TICK / ((uint64_t)args[ARG_freq].u_int);
139+
}
140+
#endif
141+
else {
142+
self->period_ms = args[ARG_period].u_int;
143+
}
144+
145+
self->mode = args[ARG_mode].u_int;
146+
self->callback = args[ARG_callback].u_obj;
147+
148+
k_timer_init(&self->my_timer, machine_timer_callback, NULL);
149+
k_timer_user_data_set(&self->my_timer, self);
150+
k_timer_start(&self->my_timer, K_MSEC(self->period_ms), K_MSEC(self->period_ms));
151+
152+
return mp_const_none;
153+
}
154+
155+
static mp_obj_t machine_timer_deinit(mp_obj_t self_in) {
156+
machine_timer_obj_t *self = self_in;
157+
k_timer_stop(&self->my_timer);
158+
return mp_const_none;
159+
}
160+
static MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_deinit_obj, machine_timer_deinit);
161+
162+
static mp_obj_t machine_timer_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
163+
return machine_timer_init_helper(args[0], n_args - 1, args + 1, kw_args);
164+
}
165+
static MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_init_obj, 1, machine_timer_init);
166+
167+
static mp_obj_t machine_timer_value(mp_obj_t self_in) {
168+
machine_timer_obj_t *self = self_in;
169+
k_ticks_t ticks = k_timer_remaining_ticks(&self->my_timer);
170+
return MP_OBJ_NEW_SMALL_INT(k_ticks_to_ms_near32(ticks));
171+
}
172+
static MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_value_obj, machine_timer_value);
173+
174+
static const mp_rom_map_elem_t machine_timer_locals_dict_table[] = {
175+
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&machine_timer_deinit_obj) },
176+
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_timer_deinit_obj) },
177+
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_timer_init_obj) },
178+
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_timer_value_obj) },
179+
{ MP_ROM_QSTR(MP_QSTR_ONE_SHOT), MP_ROM_INT(TIMER_MODE_ONE_SHOT) },
180+
{ MP_ROM_QSTR(MP_QSTR_PERIODIC), MP_ROM_INT(TIMER_MODE_PERIODIC) },
181+
};
182+
static MP_DEFINE_CONST_DICT(machine_timer_locals_dict, machine_timer_locals_dict_table);
183+
184+
MP_DEFINE_CONST_OBJ_TYPE(
185+
machine_timer_type,
186+
MP_QSTR_Timer,
187+
MP_TYPE_FLAG_NONE,
188+
make_new, machine_timer_make_new,
189+
print, machine_timer_print,
190+
locals_dict, &machine_timer_locals_dict
191+
);
192+
193+
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)