Skip to content

Commit 32c973d

Browse files
robert-hhdpgeorge
authored andcommitted
samd/machine_timer: Add machine.Timer based on the shared soft-timer.
1 parent 3625388 commit 32c973d

11 files changed

+307
-12
lines changed

ports/samd/Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,14 @@ SRC_C = \
9595
machine_led.c \
9696
machine_pin.c \
9797
machine_spi.c \
98+
machine_timer.c \
9899
machine_uart.c \
99100
main.c \
100101
modutime.c \
101102
modmachine.c \
102103
modsamd.c \
103104
mphalport.c \
105+
pendsv.c \
104106
pin_af.c \
105107
$(BUILD)/pins.c \
106108
samd_flash.c \
@@ -128,6 +130,7 @@ SRC_C = \
128130
shared/readline/readline.c \
129131
shared/runtime/gchelper_native.c \
130132
shared/runtime/pyexec.c \
133+
shared/runtime/softtimer.c \
131134
shared/runtime/stdout_helpers.c \
132135
shared/runtime/sys_stdio_mphal.c \
133136
shared/timeutils/timeutils.c \
@@ -150,6 +153,7 @@ SRC_QSTR += \
150153
machine_pin.c \
151154
machine_pwm.c \
152155
machine_spi.c \
156+
machine_timer.c \
153157
machine_uart.c \
154158
modutime.c \
155159
modmachine.c \

ports/samd/boards/mpconfig_samd21.h

+11
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,14 @@
77

88
#define CPU_FREQ (48000000)
99
#define APB_FREQ (48000000)
10+
11+
#define IRQ_PRI_PENDSV ((1 << __NVIC_PRIO_BITS) - 1)
12+
13+
static inline uint32_t raise_irq_pri(uint32_t pri) {
14+
(void)pri;
15+
return 0;
16+
}
17+
18+
static inline void restore_irq_pri(uint32_t basepri) {
19+
(void)basepri;
20+
}

ports/samd/boards/mpconfig_samd51.h

+20
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,23 @@ unsigned long trng_random_u32(void);
2222
#define CPU_FREQ (120000000)
2323
#define APB_FREQ (48000000)
2424
#define DPLLx_REF_FREQ (32768)
25+
26+
#define NVIC_PRIORITYGROUP_4 ((uint32_t)0x00000003)
27+
#define IRQ_PRI_PENDSV NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 7, 0)
28+
29+
static inline uint32_t raise_irq_pri(uint32_t pri) {
30+
uint32_t basepri = __get_BASEPRI();
31+
// If non-zero, the processor does not process any exception with a
32+
// priority value greater than or equal to BASEPRI.
33+
// When writing to BASEPRI_MAX the write goes to BASEPRI only if either:
34+
// - Rn is non-zero and the current BASEPRI value is 0
35+
// - Rn is non-zero and less than the current BASEPRI value
36+
pri <<= (8 - __NVIC_PRIO_BITS);
37+
__ASM volatile ("msr basepri_max, %0" : : "r" (pri) : "memory");
38+
return basepri;
39+
}
40+
41+
// "basepri" should be the value returned from raise_irq_pri
42+
static inline void restore_irq_pri(uint32_t basepri) {
43+
__set_BASEPRI(basepri);
44+
}

ports/samd/machine_timer.c

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Damien P. George
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 "py/runtime.h"
28+
#include "py/mphal.h"
29+
#include "shared/runtime/softtimer.h"
30+
31+
typedef soft_timer_entry_t machine_timer_obj_t;
32+
33+
const mp_obj_type_t machine_timer_type;
34+
35+
STATIC void machine_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
36+
machine_timer_obj_t *self = MP_OBJ_TO_PTR(self_in);
37+
qstr mode = self->mode == SOFT_TIMER_MODE_ONE_SHOT ? MP_QSTR_ONE_SHOT : MP_QSTR_PERIODIC;
38+
mp_printf(print, "Timer(mode=%q, period=%u)", mode, self->delta_ms);
39+
}
40+
41+
STATIC mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
42+
enum { ARG_mode, ARG_callback, ARG_period, ARG_tick_hz, ARG_freq, };
43+
static const mp_arg_t allowed_args[] = {
44+
{ MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = SOFT_TIMER_MODE_PERIODIC} },
45+
{ MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
46+
{ MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
47+
{ MP_QSTR_tick_hz, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000} },
48+
{ MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
49+
};
50+
51+
// Parse args
52+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
53+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
54+
55+
self->mode = args[ARG_mode].u_int;
56+
57+
uint64_t delta_ms = self->delta_ms;
58+
if (args[ARG_freq].u_obj != mp_const_none) {
59+
// Frequency specified in Hz
60+
#if MICROPY_PY_BUILTINS_FLOAT
61+
delta_ms = (uint32_t)(MICROPY_FLOAT_CONST(1000.0) / mp_obj_get_float(args[ARG_freq].u_obj));
62+
#else
63+
delta_ms = 1000 / mp_obj_get_int(args[ARG_freq].u_obj);
64+
#endif
65+
} else if (args[ARG_period].u_int != 0xffffffff) {
66+
// Period specified
67+
delta_ms = (uint64_t)args[ARG_period].u_int * 1000 / args[ARG_tick_hz].u_int;
68+
}
69+
70+
if (delta_ms < 1) {
71+
delta_ms = 1;
72+
} else if (delta_ms >= 0x40000000) {
73+
mp_raise_ValueError(MP_ERROR_TEXT("period too large"));
74+
}
75+
self->delta_ms = (uint32_t)delta_ms;
76+
77+
if (args[ARG_callback].u_obj != MP_OBJ_NULL) {
78+
self->py_callback = args[ARG_callback].u_obj;
79+
}
80+
81+
if (self->py_callback != mp_const_none) {
82+
soft_timer_insert(self, self->delta_ms);
83+
}
84+
85+
return mp_const_none;
86+
}
87+
88+
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) {
89+
machine_timer_obj_t *self = m_new_obj(machine_timer_obj_t);
90+
self->pairheap.base.type = &machine_timer_type;
91+
self->flags = SOFT_TIMER_FLAG_PY_CALLBACK | SOFT_TIMER_FLAG_GC_ALLOCATED;
92+
self->delta_ms = 1000;
93+
self->py_callback = mp_const_none;
94+
95+
// Get timer id (only soft timer (-1) supported at the moment)
96+
mp_int_t id = -1;
97+
if (n_args > 0) {
98+
id = mp_obj_get_int(args[0]);
99+
--n_args;
100+
++args;
101+
}
102+
if (id != -1) {
103+
mp_raise_ValueError(MP_ERROR_TEXT("Timer doesn't exist"));
104+
}
105+
106+
if (n_args > 0 || n_kw > 0) {
107+
// Start the timer
108+
mp_map_t kw_args;
109+
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
110+
machine_timer_init_helper(self, n_args, args, &kw_args);
111+
}
112+
113+
return MP_OBJ_FROM_PTR(self);
114+
}
115+
116+
STATIC mp_obj_t machine_timer_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
117+
machine_timer_obj_t *self = MP_OBJ_TO_PTR(args[0]);
118+
soft_timer_remove(self);
119+
return machine_timer_init_helper(self, n_args - 1, args + 1, kw_args);
120+
}
121+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_init_obj, 1, machine_timer_init);
122+
123+
STATIC mp_obj_t machine_timer_deinit(mp_obj_t self_in) {
124+
machine_timer_obj_t *self = MP_OBJ_TO_PTR(self_in);
125+
soft_timer_remove(self);
126+
return mp_const_none;
127+
}
128+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_deinit_obj, machine_timer_deinit);
129+
130+
STATIC const mp_rom_map_elem_t machine_timer_locals_dict_table[] = {
131+
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_timer_init_obj) },
132+
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_timer_deinit_obj) },
133+
134+
{ MP_ROM_QSTR(MP_QSTR_ONE_SHOT), MP_ROM_INT(SOFT_TIMER_MODE_ONE_SHOT) },
135+
{ MP_ROM_QSTR(MP_QSTR_PERIODIC), MP_ROM_INT(SOFT_TIMER_MODE_PERIODIC) },
136+
};
137+
STATIC MP_DEFINE_CONST_DICT(machine_timer_locals_dict, machine_timer_locals_dict_table);
138+
139+
MP_DEFINE_CONST_OBJ_TYPE(
140+
machine_timer_type,
141+
MP_QSTR_Timer,
142+
MP_TYPE_FLAG_NONE,
143+
make_new, machine_timer_make_new,
144+
print, machine_timer_print,
145+
locals_dict, &machine_timer_locals_dict
146+
);

ports/samd/main.c

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "py/stackctrl.h"
3232
#include "shared/runtime/gchelper.h"
3333
#include "shared/runtime/pyexec.h"
34+
#include "shared/runtime/softtimer.h"
3435

3536
extern uint8_t _sstack, _estack, _sheap, _eheap;
3637
extern void adc_deinit_all(void);
@@ -82,6 +83,7 @@ void samd_main(void) {
8283
pwm_deinit_all();
8384
sercom_deinit_all();
8485
uart_deinit_all();
86+
soft_timer_deinit();
8587
gc_sweep_all();
8688
mp_deinit();
8789
}

ports/samd/modmachine.c

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
157157
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hw_i2c_type) },
158158
{ MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },
159159
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) },
160+
{ MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) },
160161
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) },
161162

162163
{ MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) },

ports/samd/modmachine.h

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ extern const mp_obj_type_t machine_led_type;
3434
extern const mp_obj_type_t machine_pin_type;
3535
extern const mp_obj_type_t machine_pwm_type;
3636
extern const mp_obj_type_t machine_spi_type;
37+
extern const mp_obj_type_t machine_timer_type;
3738
extern const mp_obj_type_t machine_uart_type;
3839

3940
#endif // MICROPY_INCLUDED_SAMD_MODMACHINE_H

ports/samd/mphalport.h

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ extern volatile uint32_t systick_ms_upper;
4040

4141
void mp_hal_set_interrupt_char(int c);
4242

43+
// Define an alias fo systick_ms, because the shared softtimer.c uses
44+
// the symbol uwTick for the systick ms counter.
45+
#define uwTick systick_ms
46+
4347
#define mp_hal_delay_us_fast mp_hal_delay_us
4448

4549
static inline mp_uint_t mp_hal_ticks_ms(void) {

ports/samd/pendsv.c

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013, 2014 Damien P. George
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 <stdlib.h>
28+
29+
#include "py/runtime.h"
30+
#include "shared/runtime/interrupt_char.h"
31+
#include "sam.h"
32+
#include "pendsv.h"
33+
34+
35+
#if defined(PENDSV_DISPATCH_NUM_SLOTS)
36+
uint32_t pendsv_dispatch_active;
37+
pendsv_dispatch_t pendsv_dispatch_table[PENDSV_DISPATCH_NUM_SLOTS];
38+
#endif
39+
40+
void pendsv_init(void) {
41+
#if defined(PENDSV_DISPATCH_NUM_SLOTS)
42+
pendsv_dispatch_active = false;
43+
#endif
44+
45+
// set PendSV interrupt at lowest priority
46+
NVIC_SetPriority(PendSV_IRQn, IRQ_PRI_PENDSV);
47+
}
48+
49+
#if defined(PENDSV_DISPATCH_NUM_SLOTS)
50+
void pendsv_schedule_dispatch(size_t slot, pendsv_dispatch_t f) {
51+
pendsv_dispatch_table[slot] = f;
52+
pendsv_dispatch_active = true;
53+
SCB->ICSR = SCB_ICSR_PENDSVSET_Msk;
54+
}
55+
56+
void pendsv_dispatch_handler(void) {
57+
for (size_t i = 0; i < PENDSV_DISPATCH_NUM_SLOTS; ++i) {
58+
if (pendsv_dispatch_table[i] != NULL) {
59+
pendsv_dispatch_t f = pendsv_dispatch_table[i];
60+
pendsv_dispatch_table[i] = NULL;
61+
f();
62+
}
63+
}
64+
}
65+
66+
void PendSV_Handler(void) {
67+
if (pendsv_dispatch_active) {
68+
pendsv_dispatch_handler();
69+
}
70+
}
71+
#endif

ports/samd/pendsv.h

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013, 2014 Damien P. George
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+
#ifndef MICROPY_INCLUDED_SAMD_PENDSV_H
27+
#define MICROPY_INCLUDED_SAMD_PENDSV_H
28+
29+
enum {
30+
PENDSV_DISPATCH_SOFT_TIMER, // For later & for having at least one entry
31+
PENDSV_DISPATCH_MAX
32+
};
33+
34+
#define PENDSV_DISPATCH_NUM_SLOTS PENDSV_DISPATCH_MAX
35+
36+
typedef void (*pendsv_dispatch_t)(void);
37+
38+
void pendsv_init(void);
39+
void pendsv_schedule_dispatch(size_t slot, pendsv_dispatch_t f);
40+
41+
#endif // MICROPY_INCLUDED_SAMD_PENDSV_H

0 commit comments

Comments
 (0)