Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit 56f0513

Browse files
ak15199dpgeorge
authored andcommitted
esp32/machine_timer: Add support for esp32 hardware timer.
Available via the machine.Timer() class. The code provides access to the four hardware timers available on the ESP32, where bit 1 refers to the timer group, and bit 0 refers to the timer index. It is supposed that virtual timers could be built on top of this in Python. Code lineage: Timer() is based loosely on the version in esp8266, although the implementation is differs significantly because of the change in the underlying platform.
1 parent 965027d commit 56f0513

File tree

4 files changed

+195
-0
lines changed

4 files changed

+195
-0
lines changed

esp32/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ SRC_C = \
119119
esponewire.c \
120120
modutime.c \
121121
moduos.c \
122+
machine_timer.c \
122123
machine_pin.c \
123124
machine_touchpad.c \
124125
machine_adc.c \

esp32/machine_timer.c

+192
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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+
*
11+
* Permission is hereby granted, free of charge, to any person obtaining a copy
12+
* of this software and associated documentation files (the "Software"), to deal
13+
* in the Software without restriction, including without limitation the rights
14+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15+
* copies of the Software, and to permit persons to whom the Software is
16+
* furnished to do so, subject to the following conditions:
17+
*
18+
* The above copyright notice and this permission notice shall be included in
19+
* all copies or substantial portions of the Software.
20+
*
21+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27+
* THE SOFTWARE.
28+
*/
29+
30+
#include <stdint.h>
31+
#include <stdio.h>
32+
33+
#include "driver/timer.h"
34+
#include "py/obj.h"
35+
#include "py/runtime.h"
36+
#include "modmachine.h"
37+
38+
#define TIMER_INTR_SEL TIMER_INTR_LEVEL
39+
#define TIMER_DIVIDER 40000
40+
#define TIMER_SCALE (TIMER_BASE_CLK / TIMER_DIVIDER)
41+
42+
#define TIMER_FLAGS 0
43+
44+
typedef struct _machine_timer_obj_t {
45+
mp_obj_base_t base;
46+
mp_uint_t group;
47+
mp_uint_t index;
48+
49+
mp_uint_t repeat;
50+
mp_uint_t period;
51+
52+
mp_obj_t callback;
53+
54+
intr_handle_t handle;
55+
} machine_timer_obj_t;
56+
57+
const mp_obj_type_t machine_timer_type;
58+
59+
STATIC esp_err_t check_esp_err(esp_err_t code) {
60+
if (code) {
61+
mp_raise_OSError(code);
62+
}
63+
64+
return code;
65+
}
66+
67+
STATIC void machine_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
68+
machine_timer_obj_t *self = self_in;
69+
70+
timer_config_t config;
71+
mp_printf(print, "Timer(%p; ", self);
72+
73+
timer_get_config(self->group, self->index, &config);
74+
75+
mp_printf(print, "alarm_en=%d, ", config.alarm_en);
76+
mp_printf(print, "auto_reload=%d, ", config.auto_reload);
77+
mp_printf(print, "counter_en=%d)", config.counter_en);
78+
}
79+
80+
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) {
81+
mp_arg_check_num(n_args, n_kw, 1, 1, false);
82+
machine_timer_obj_t *self = m_new_obj(machine_timer_obj_t);
83+
self->base.type = &machine_timer_type;
84+
85+
self->group = (mp_obj_get_int(args[0]) >> 1) & 1;
86+
self->index = mp_obj_get_int(args[0]) & 1;
87+
88+
return self;
89+
}
90+
91+
STATIC void machine_timer_disable(machine_timer_obj_t *self) {
92+
if (self->handle) {
93+
timer_pause(self->group, self->index);
94+
esp_intr_free(self->handle);
95+
self->handle = NULL;
96+
}
97+
}
98+
99+
STATIC void machine_timer_isr(void *self_in) {
100+
machine_timer_obj_t *self = self_in;
101+
timg_dev_t *device = self->group ? &(TIMERG1) : &(TIMERG0);
102+
103+
device->hw_timer[self->index].update = 1;
104+
if (self->index) {
105+
device->int_clr_timers.t1 = 1;
106+
} else {
107+
device->int_clr_timers.t0 = 1;
108+
}
109+
device->hw_timer[self->index].config.alarm_en = self->repeat;
110+
111+
mp_sched_schedule(self->callback, self);
112+
}
113+
114+
STATIC void machine_timer_enable(machine_timer_obj_t *self) {
115+
timer_config_t config;
116+
config.alarm_en = TIMER_ALARM_EN;
117+
config.auto_reload = self->repeat;
118+
config.counter_dir = TIMER_COUNT_UP;
119+
config.divider = TIMER_DIVIDER;
120+
config.intr_type = TIMER_INTR_LEVEL;
121+
config.counter_en = TIMER_PAUSE;
122+
123+
check_esp_err(timer_init(self->group, self->index, &config));
124+
check_esp_err(timer_set_counter_value(self->group, self->index, 0x00000000));
125+
check_esp_err(timer_set_alarm_value(self->group, self->index, self->period));
126+
check_esp_err(timer_enable_intr(self->group, self->index));
127+
check_esp_err(timer_isr_register(self->group, self->index, machine_timer_isr, (void*)self, TIMER_FLAGS, &self->handle));
128+
check_esp_err(timer_start(self->group, self->index));
129+
}
130+
131+
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) {
132+
static const mp_arg_t allowed_args[] = {
133+
{ MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
134+
{ MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
135+
{ MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
136+
};
137+
138+
machine_timer_disable(self);
139+
140+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
141+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
142+
143+
// Timer uses an 80MHz base clock, which is divided by the divider/scalar, we then convert to ms.
144+
self->period = (args[0].u_int * TIMER_BASE_CLK) / (1000 * TIMER_DIVIDER);
145+
self->repeat = args[1].u_int;
146+
self->callback = args[2].u_obj;
147+
self->handle = NULL;
148+
149+
machine_timer_enable(self);
150+
151+
return mp_const_none;
152+
}
153+
154+
STATIC mp_obj_t machine_timer_deinit(mp_obj_t self_in) {
155+
machine_timer_disable(self_in);
156+
157+
return mp_const_none;
158+
}
159+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_deinit_obj, machine_timer_deinit);
160+
161+
STATIC mp_obj_t machine_timer_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
162+
return machine_timer_init_helper(args[0], n_args - 1, args + 1, kw_args);
163+
}
164+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_init_obj, 1, machine_timer_init);
165+
166+
STATIC mp_obj_t machine_timer_value(mp_obj_t self_in) {
167+
machine_timer_obj_t *self = self_in;
168+
double result;
169+
170+
timer_get_counter_time_sec(self->group, self->index, &result);
171+
172+
return MP_OBJ_NEW_SMALL_INT((mp_uint_t)(result * 1000)); // value in ms
173+
}
174+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_value_obj, machine_timer_value);
175+
176+
STATIC const mp_map_elem_t machine_timer_locals_dict_table[] = {
177+
{ MP_ROM_QSTR(MP_QSTR___del__), (mp_obj_t)&machine_timer_deinit_obj },
178+
{ MP_ROM_QSTR(MP_QSTR_deinit), (mp_obj_t)&machine_timer_deinit_obj },
179+
{ MP_ROM_QSTR(MP_QSTR_init), (mp_obj_t)&machine_timer_init_obj },
180+
{ MP_ROM_QSTR(MP_QSTR_value), (mp_obj_t)&machine_timer_value_obj },
181+
{ MP_ROM_QSTR(MP_QSTR_ONE_SHOT), MP_ROM_INT(false) },
182+
{ MP_ROM_QSTR(MP_QSTR_PERIODIC), MP_ROM_INT(true) },
183+
};
184+
STATIC MP_DEFINE_CONST_DICT(machine_timer_locals_dict, machine_timer_locals_dict_table);
185+
186+
const mp_obj_type_t machine_timer_type = {
187+
{ &mp_type_type },
188+
.name = MP_QSTR_Timer,
189+
.print = machine_timer_print,
190+
.make_new = machine_timer_make_new,
191+
.locals_dict = (mp_obj_t)&machine_timer_locals_dict,
192+
};

esp32/modmachine.c

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
113113

114114
{ MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) },
115115

116+
{ MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) },
116117
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) },
117118
{ MP_ROM_QSTR(MP_QSTR_TouchPad), MP_ROM_PTR(&machine_touchpad_type) },
118119
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },

esp32/modmachine.h

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "py/obj.h"
55

6+
extern const mp_obj_type_t machine_timer_type;
67
extern const mp_obj_type_t machine_pin_type;
78
extern const mp_obj_type_t machine_touchpad_type;
89
extern const mp_obj_type_t machine_adc_type;

0 commit comments

Comments
 (0)