Skip to content

Commit a972844

Browse files
committed
extmod/utime_mphal: Factor out implementations in terms of mp_hal_* for reuse.
As long as a port implement mp_hal_sleep_ms(), mp_hal_ticks_ms(), etc. functions, it can just use standard implementations of utime.sleel_ms(), utime.ticks_ms(), etc. Python-level functions.
1 parent 824f5c5 commit a972844

File tree

7 files changed

+139
-54
lines changed

7 files changed

+139
-54
lines changed

esp8266/modutime.c

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "modpybrtc.h"
3939
#include "timeutils.h"
4040
#include "user_interface.h"
41+
#include "extmod/utime_mphal.h"
4142

4243
/// \module time - time related functions
4344
///
@@ -99,53 +100,6 @@ STATIC mp_obj_t time_mktime(mp_obj_t tuple) {
99100
}
100101
MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime);
101102

102-
/// \function sleep(seconds)
103-
/// Sleep for the given number of seconds.
104-
STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) {
105-
#if MICROPY_PY_BUILTINS_FLOAT
106-
mp_hal_delay_ms(1000 * mp_obj_get_float(seconds_o));
107-
#else
108-
mp_hal_delay_ms(1000 * mp_obj_get_int(seconds_o));
109-
#endif
110-
return mp_const_none;
111-
}
112-
MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep);
113-
114-
STATIC mp_obj_t time_sleep_ms(mp_obj_t arg) {
115-
mp_hal_delay_ms(mp_obj_get_int(arg));
116-
return mp_const_none;
117-
}
118-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_ms_obj, time_sleep_ms);
119-
120-
STATIC mp_obj_t time_sleep_us(mp_obj_t arg) {
121-
mp_hal_delay_us(mp_obj_get_int(arg));
122-
return mp_const_none;
123-
}
124-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_us_obj, time_sleep_us);
125-
126-
STATIC mp_obj_t time_ticks_ms(void) {
127-
return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms() & MP_SMALL_INT_POSITIVE_MASK);
128-
}
129-
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_ms_obj, time_ticks_ms);
130-
131-
STATIC mp_obj_t time_ticks_us(void) {
132-
return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_us() & MP_SMALL_INT_POSITIVE_MASK);
133-
}
134-
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_us_obj, time_ticks_us);
135-
136-
STATIC mp_obj_t time_ticks_cpu(void) {
137-
return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_cpu() & MP_SMALL_INT_POSITIVE_MASK);
138-
}
139-
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_cpu_obj, time_ticks_cpu);
140-
141-
STATIC mp_obj_t time_ticks_diff(mp_obj_t start_in, mp_obj_t end_in) {
142-
// we assume that the arguments come from ticks_xx so are small ints
143-
uint32_t start = MP_OBJ_SMALL_INT_VALUE(start_in);
144-
uint32_t end = MP_OBJ_SMALL_INT_VALUE(end_in);
145-
return MP_OBJ_NEW_SMALL_INT((end - start) & MP_SMALL_INT_POSITIVE_MASK);
146-
}
147-
STATIC MP_DEFINE_CONST_FUN_OBJ_2(time_ticks_diff_obj, time_ticks_diff);
148-
149103
/// \function time()
150104
/// Returns the number of seconds, as an integer, since 1/1/2000.
151105
STATIC mp_obj_t time_time(void) {
@@ -159,13 +113,13 @@ STATIC const mp_map_elem_t time_module_globals_table[] = {
159113

160114
{ MP_OBJ_NEW_QSTR(MP_QSTR_localtime), (mp_obj_t)&time_localtime_obj },
161115
{ MP_OBJ_NEW_QSTR(MP_QSTR_mktime), (mp_obj_t)&time_mktime_obj },
162-
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep), (mp_obj_t)&time_sleep_obj },
163-
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep_ms), (mp_obj_t)&time_sleep_ms_obj },
164-
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep_us), (mp_obj_t)&time_sleep_us_obj },
165-
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_ms), (mp_obj_t)&time_ticks_ms_obj },
166-
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_us), (mp_obj_t)&time_ticks_us_obj },
167-
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_cpu), (mp_obj_t)&time_ticks_cpu_obj },
168-
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_diff), (mp_obj_t)&time_ticks_diff_obj },
116+
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep), (mp_obj_t)&mp_utime_sleep_obj },
117+
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep_ms), (mp_obj_t)&mp_utime_sleep_ms_obj },
118+
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep_us), (mp_obj_t)&mp_utime_sleep_us_obj },
119+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_ms), (mp_obj_t)&mp_utime_ticks_ms_obj },
120+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_us), (mp_obj_t)&mp_utime_ticks_us_obj },
121+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_cpu), (mp_obj_t)&mp_utime_ticks_cpu_obj },
122+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_diff), (mp_obj_t)&mp_utime_ticks_diff_obj },
169123
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_time_obj },
170124
};
171125

esp8266/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#define MICROPY_PY_UJSON (1)
6363
#define MICROPY_PY_URANDOM (1)
6464
#define MICROPY_PY_URE (1)
65+
#define MICROPY_PY_UTIME_MP_HAL (1)
6566
#define MICROPY_PY_UZLIB (1)
6667
#define MICROPY_PY_LWIP (1)
6768
#define MICROPY_PY_MACHINE (1)

extmod/utime_mphal.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013-2016 Damien P. George
7+
* Copyright (c) 2016 Paul Sokolovsky
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include "py/mpconfig.h"
29+
#if MICROPY_PY_UTIME_MP_HAL
30+
31+
#include <string.h>
32+
33+
#include "py/obj.h"
34+
#include "py/mphal.h"
35+
#include "py/smallint.h"
36+
#include "extmod/utime_mphal.h"
37+
38+
STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) {
39+
#if MICROPY_PY_BUILTINS_FLOAT
40+
mp_hal_delay_ms(1000 * mp_obj_get_float(seconds_o));
41+
#else
42+
mp_hal_delay_ms(1000 * mp_obj_get_int(seconds_o));
43+
#endif
44+
return mp_const_none;
45+
}
46+
MP_DEFINE_CONST_FUN_OBJ_1(mp_utime_sleep_obj, time_sleep);
47+
48+
STATIC mp_obj_t time_sleep_ms(mp_obj_t arg) {
49+
mp_hal_delay_ms(mp_obj_get_int(arg));
50+
return mp_const_none;
51+
}
52+
MP_DEFINE_CONST_FUN_OBJ_1(mp_utime_sleep_ms_obj, time_sleep_ms);
53+
54+
STATIC mp_obj_t time_sleep_us(mp_obj_t arg) {
55+
mp_hal_delay_us(mp_obj_get_int(arg));
56+
return mp_const_none;
57+
}
58+
MP_DEFINE_CONST_FUN_OBJ_1(mp_utime_sleep_us_obj, time_sleep_us);
59+
60+
STATIC mp_obj_t time_ticks_ms(void) {
61+
return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms() & MP_SMALL_INT_POSITIVE_MASK);
62+
}
63+
MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_ticks_ms_obj, time_ticks_ms);
64+
65+
STATIC mp_obj_t time_ticks_us(void) {
66+
return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_us() & MP_SMALL_INT_POSITIVE_MASK);
67+
}
68+
MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_ticks_us_obj, time_ticks_us);
69+
70+
STATIC mp_obj_t time_ticks_cpu(void) {
71+
return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_cpu() & MP_SMALL_INT_POSITIVE_MASK);
72+
}
73+
MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_ticks_cpu_obj, time_ticks_cpu);
74+
75+
STATIC mp_obj_t time_ticks_diff(mp_obj_t start_in, mp_obj_t end_in) {
76+
// we assume that the arguments come from ticks_xx so are small ints
77+
uint32_t start = MP_OBJ_SMALL_INT_VALUE(start_in);
78+
uint32_t end = MP_OBJ_SMALL_INT_VALUE(end_in);
79+
return MP_OBJ_NEW_SMALL_INT((end - start) & MP_SMALL_INT_POSITIVE_MASK);
80+
}
81+
MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_diff_obj, time_ticks_diff);
82+
83+
#endif // MICROPY_PY_UTIME_MP_HAL

extmod/utime_mphal.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013-2016 Damien P. George
7+
* Copyright (c) 2016 Paul Sokolovsky
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include "py/obj.h"
29+
30+
MP_DECLARE_CONST_FUN_OBJ(mp_utime_sleep_obj);
31+
MP_DECLARE_CONST_FUN_OBJ(mp_utime_sleep_ms_obj);
32+
MP_DECLARE_CONST_FUN_OBJ(mp_utime_sleep_us_obj);
33+
MP_DECLARE_CONST_FUN_OBJ(mp_utime_ticks_ms_obj);
34+
MP_DECLARE_CONST_FUN_OBJ(mp_utime_ticks_us_obj);
35+
MP_DECLARE_CONST_FUN_OBJ(mp_utime_ticks_cpu_obj);
36+
MP_DECLARE_CONST_FUN_OBJ(mp_utime_ticks_diff_obj);

py/mpconfig.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,12 @@ typedef double mp_float_t;
860860
#define MICROPY_PY_UERRNO (0)
861861
#endif
862862

863+
// Whether to provide "utime" module functions implementation
864+
// in terms of mp_hal_* functions.
865+
#ifndef MICROPY_PY_UTIME_MP_HAL
866+
#define MICROPY_PY_UTIME_MP_HAL (0)
867+
#endif
868+
863869
// Whether to provide "_thread" module
864870
#ifndef MICROPY_PY_THREAD
865871
#define MICROPY_PY_THREAD (0)

py/mphal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ mp_uint_t mp_hal_ticks_ms(void);
6666
mp_uint_t mp_hal_ticks_us(void);
6767
#endif
6868

69+
#ifndef mp_hal_ticks_cpu
70+
mp_uint_t mp_hal_ticks_cpu(void);
71+
#endif
72+
6973
// If port HAL didn't define its own pin API, use generic
7074
// "virtual pin" API from the core.
7175
#ifndef mp_hal_pin_obj_t

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ PY_O_BASENAME = \
229229
../extmod/vfs_fat_file.o \
230230
../extmod/vfs_fat_lexer.o \
231231
../extmod/vfs_fat_misc.o \
232+
../extmod/utime_mphal.o \
232233
../extmod/moduos_dupterm.o \
233234
../lib/embed/abort_.o \
234235
../lib/utils/printf.o \

0 commit comments

Comments
 (0)