From 4ebdb1f2b217410cdc1cee0e0c0da8fceb7627f2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 18 Oct 2016 11:06:20 +1100 Subject: py: Be more specific with MP_DECLARE_CONST_FUN_OBJ macros. In order to have more fine-grained control over how builtin functions are constructed, the MP_DECLARE_CONST_FUN_OBJ macros are made more specific, with suffix of _0, _1, _2, _3, _VAR, _VAR_BETEEN or _KW. These names now match the MP_DEFINE_CONST_FUN_OBJ macros. --- extmod/fsusermount.h | 6 +++--- extmod/machine_pulse.h | 2 +- extmod/machine_spi.h | 8 ++++---- extmod/misc.h | 2 +- extmod/modubinascii.h | 10 +++++----- extmod/utime_mphal.h | 14 +++++++------- extmod/vfs_fat_file.h | 2 +- 7 files changed, 22 insertions(+), 22 deletions(-) (limited to 'extmod') diff --git a/extmod/fsusermount.h b/extmod/fsusermount.h index e1f26f2ce..af6867d23 100644 --- a/extmod/fsusermount.h +++ b/extmod/fsusermount.h @@ -57,6 +57,6 @@ typedef struct _fs_user_mount_t { fs_user_mount_t *fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args, bool mkfs); mp_obj_t fatfs_umount(mp_obj_t bdev_or_path_in); -MP_DECLARE_CONST_FUN_OBJ(fsuser_mount_obj); -MP_DECLARE_CONST_FUN_OBJ(fsuser_umount_obj); -MP_DECLARE_CONST_FUN_OBJ(fsuser_mkfs_obj); +MP_DECLARE_CONST_FUN_OBJ_KW(fsuser_mount_obj); +MP_DECLARE_CONST_FUN_OBJ_1(fsuser_umount_obj); +MP_DECLARE_CONST_FUN_OBJ_KW(fsuser_mkfs_obj); diff --git a/extmod/machine_pulse.h b/extmod/machine_pulse.h index 3d5d81c07..cc1c4eda5 100644 --- a/extmod/machine_pulse.h +++ b/extmod/machine_pulse.h @@ -32,6 +32,6 @@ mp_uint_t machine_time_pulse_us(mp_hal_pin_obj_t pin, int pulse_level, mp_uint_t timeout_us); -MP_DECLARE_CONST_FUN_OBJ(machine_time_pulse_us_obj); +MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_time_pulse_us_obj); #endif // __MICROPY_INCLUDED_EXTMOD_MACHINE_PULSE_H__ diff --git a/extmod/machine_spi.h b/extmod/machine_spi.h index 316d06646..62f42a885 100644 --- a/extmod/machine_spi.h +++ b/extmod/machine_spi.h @@ -47,9 +47,9 @@ typedef struct _mp_machine_soft_spi_obj_t { void mp_machine_soft_spi_transfer(mp_obj_base_t *self, size_t len, const uint8_t *src, uint8_t *dest); -MP_DECLARE_CONST_FUN_OBJ(mp_machine_spi_read_obj); -MP_DECLARE_CONST_FUN_OBJ(mp_machine_spi_readinto_obj); -MP_DECLARE_CONST_FUN_OBJ(mp_machine_spi_write_obj); -MP_DECLARE_CONST_FUN_OBJ(mp_machine_spi_write_readinto_obj); +MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_read_obj); +MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_readinto_obj); +MP_DECLARE_CONST_FUN_OBJ_2(mp_machine_spi_write_obj); +MP_DECLARE_CONST_FUN_OBJ_3(mp_machine_spi_write_readinto_obj); #endif // MICROPY_INCLUDED_EXTMOD_MACHINE_SPI_H diff --git a/extmod/misc.h b/extmod/misc.h index 634ea924d..d7ead0654 100644 --- a/extmod/misc.h +++ b/extmod/misc.h @@ -30,7 +30,7 @@ #include #include "py/runtime.h" -MP_DECLARE_CONST_FUN_OBJ(mp_uos_dupterm_obj); +MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_uos_dupterm_obj); #if MICROPY_PY_OS_DUPTERM void mp_uos_dupterm_tx_strn(const char *str, size_t len); diff --git a/extmod/modubinascii.h b/extmod/modubinascii.h index 71dd8a693..33d0f1cbd 100644 --- a/extmod/modubinascii.h +++ b/extmod/modubinascii.h @@ -33,10 +33,10 @@ extern mp_obj_t mod_binascii_a2b_base64(mp_obj_t data); extern mp_obj_t mod_binascii_b2a_base64(mp_obj_t data); extern mp_obj_t mod_binascii_crc32(size_t n_args, const mp_obj_t *args); -MP_DECLARE_CONST_FUN_OBJ(mod_binascii_hexlify_obj); -MP_DECLARE_CONST_FUN_OBJ(mod_binascii_unhexlify_obj); -MP_DECLARE_CONST_FUN_OBJ(mod_binascii_a2b_base64_obj); -MP_DECLARE_CONST_FUN_OBJ(mod_binascii_b2a_base64_obj); -MP_DECLARE_CONST_FUN_OBJ(mod_binascii_crc32_obj); +MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_hexlify_obj); +MP_DECLARE_CONST_FUN_OBJ_1(mod_binascii_unhexlify_obj); +MP_DECLARE_CONST_FUN_OBJ_1(mod_binascii_a2b_base64_obj); +MP_DECLARE_CONST_FUN_OBJ_1(mod_binascii_b2a_base64_obj); +MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_crc32_obj); #endif /* MICROPY_EXTMOD_MODUBINASCII */ diff --git a/extmod/utime_mphal.h b/extmod/utime_mphal.h index 4f2395a09..c7611e251 100644 --- a/extmod/utime_mphal.h +++ b/extmod/utime_mphal.h @@ -27,10 +27,10 @@ #include "py/obj.h" -MP_DECLARE_CONST_FUN_OBJ(mp_utime_sleep_obj); -MP_DECLARE_CONST_FUN_OBJ(mp_utime_sleep_ms_obj); -MP_DECLARE_CONST_FUN_OBJ(mp_utime_sleep_us_obj); -MP_DECLARE_CONST_FUN_OBJ(mp_utime_ticks_ms_obj); -MP_DECLARE_CONST_FUN_OBJ(mp_utime_ticks_us_obj); -MP_DECLARE_CONST_FUN_OBJ(mp_utime_ticks_cpu_obj); -MP_DECLARE_CONST_FUN_OBJ(mp_utime_ticks_diff_obj); +MP_DECLARE_CONST_FUN_OBJ_1(mp_utime_sleep_obj); +MP_DECLARE_CONST_FUN_OBJ_1(mp_utime_sleep_ms_obj); +MP_DECLARE_CONST_FUN_OBJ_1(mp_utime_sleep_us_obj); +MP_DECLARE_CONST_FUN_OBJ_0(mp_utime_ticks_ms_obj); +MP_DECLARE_CONST_FUN_OBJ_0(mp_utime_ticks_us_obj); +MP_DECLARE_CONST_FUN_OBJ_0(mp_utime_ticks_cpu_obj); +MP_DECLARE_CONST_FUN_OBJ_2(mp_utime_ticks_diff_obj); diff --git a/extmod/vfs_fat_file.h b/extmod/vfs_fat_file.h index 6d6888955..5c271b6ee 100644 --- a/extmod/vfs_fat_file.h +++ b/extmod/vfs_fat_file.h @@ -27,6 +27,6 @@ extern const byte fresult_to_errno_table[20]; mp_obj_t fatfs_builtin_open(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs); -MP_DECLARE_CONST_FUN_OBJ(mp_builtin_open_obj); +MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_open_obj); mp_obj_t fat_vfs_listdir(const char *path, bool is_str_type); -- cgit v1.2.3 From 56942019309645781d330312f5944db2d4cb5cd7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 24 Oct 2016 12:59:20 +1100 Subject: extmod/vfs_fat_file: Make file.close() a no-op if file already closed. As per CPython semantics. In particular, file.__del__() should not raise an exception if the file is already closed. --- extmod/vfs_fat_file.c | 9 ++++++--- tests/extmod/vfs_fat_fileio.py | 6 +----- tests/extmod/vfs_fat_fileio.py.exp | 1 - 3 files changed, 7 insertions(+), 9 deletions(-) (limited to 'extmod') diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index e269ef593..76ac23685 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -120,9 +120,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(file_obj_flush_obj, file_obj_flush); STATIC mp_obj_t file_obj_close(mp_obj_t self_in) { pyb_file_obj_t *self = MP_OBJ_TO_PTR(self_in); - FRESULT res = f_close(&self->fp); - if (res != FR_OK) { - mp_raise_OSError(fresult_to_errno_table[res]); + // if fs==NULL then the file is closed and in that case this method is a no-op + if (self->fp.fs != NULL) { + FRESULT res = f_close(&self->fp); + if (res != FR_OK) { + mp_raise_OSError(fresult_to_errno_table[res]); + } } return mp_const_none; } diff --git a/tests/extmod/vfs_fat_fileio.py b/tests/extmod/vfs_fat_fileio.py index 26fec7828..de8d4953c 100644 --- a/tests/extmod/vfs_fat_fileio.py +++ b/tests/extmod/vfs_fat_fileio.py @@ -48,6 +48,7 @@ print(str(f)[:17], str(f)[-1:]) f.write("hello!") f.flush() f.close() +f.close() # allowed try: f.write("world!") except OSError as e: @@ -63,11 +64,6 @@ try: except OSError as e: print(e.args[0] == uerrno.EINVAL) -try: - f.close() -except OSError as e: - print(e.args[0] == uerrno.EINVAL) - try: vfs.open("foo_file.txt", "x") except OSError as e: diff --git a/tests/extmod/vfs_fat_fileio.py.exp b/tests/extmod/vfs_fat_fileio.py.exp index 9f0edb31e..c438bc850 100644 --- a/tests/extmod/vfs_fat_fileio.py.exp +++ b/tests/extmod/vfs_fat_fileio.py.exp @@ -3,7 +3,6 @@ True True True True -True hello!world! 12 h -- cgit v1.2.3 From f00ecdb54d06f57e318d00db00df82e6450e5901 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 26 Oct 2016 02:08:37 +0300 Subject: extmod/moduos_dupterm: Renamed to uos_dupterm. As part of file naming clean up (moduos_dupterm doesn't implement a full module, so should skip "mod" prefix, similar to other files in extmod/). --- extmod/moduos_dupterm.c | 93 ------------------------------------------------- extmod/uos_dupterm.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++ py/py.mk | 2 +- 3 files changed, 94 insertions(+), 94 deletions(-) delete mode 100644 extmod/moduos_dupterm.c create mode 100644 extmod/uos_dupterm.c (limited to 'extmod') diff --git a/extmod/moduos_dupterm.c b/extmod/moduos_dupterm.c deleted file mode 100644 index d888099df..000000000 --- a/extmod/moduos_dupterm.c +++ /dev/null @@ -1,93 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Paul Sokolovsky - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include -#include -#include "py/mpconfig.h" - -#include "py/nlr.h" -#include "py/runtime.h" -#include "py/objtuple.h" -#include "py/objarray.h" -#include "py/stream.h" - -#if MICROPY_PY_OS_DUPTERM - -void mp_uos_deactivate(const char *msg, mp_obj_t exc) { - mp_obj_t term = MP_STATE_PORT(term_obj); - MP_STATE_PORT(term_obj) = NULL; - mp_printf(&mp_plat_print, msg); - if (exc != MP_OBJ_NULL) { - mp_obj_print_exception(&mp_plat_print, exc); - } - mp_stream_close(term); -} - -void mp_uos_dupterm_tx_strn(const char *str, size_t len) { - if (MP_STATE_PORT(term_obj) != MP_OBJ_NULL) { - nlr_buf_t nlr; - if (nlr_push(&nlr) == 0) { - mp_obj_t write_m[3]; - mp_load_method(MP_STATE_PORT(term_obj), MP_QSTR_write, write_m); - - mp_obj_array_t *arr = MP_OBJ_TO_PTR(MP_STATE_PORT(dupterm_arr_obj)); - void *org_items = arr->items; - arr->items = (void*)str; - arr->len = len; - write_m[2] = MP_STATE_PORT(dupterm_arr_obj); - mp_call_method_n_kw(1, 0, write_m); - arr = MP_OBJ_TO_PTR(MP_STATE_PORT(dupterm_arr_obj)); - arr->items = org_items; - arr->len = 1; - nlr_pop(); - } else { - mp_uos_deactivate("dupterm: Exception in write() method, deactivating: ", nlr.ret_val); - } - } -} - -STATIC mp_obj_t mp_uos_dupterm(mp_uint_t n_args, const mp_obj_t *args) { - if (n_args == 0) { - if (MP_STATE_PORT(term_obj) == MP_OBJ_NULL) { - return mp_const_none; - } else { - return MP_STATE_PORT(term_obj); - } - } else { - if (args[0] == mp_const_none) { - MP_STATE_PORT(term_obj) = MP_OBJ_NULL; - } else { - MP_STATE_PORT(term_obj) = args[0]; - if (MP_STATE_PORT(dupterm_arr_obj) == MP_OBJ_NULL) { - MP_STATE_PORT(dupterm_arr_obj) = mp_obj_new_bytearray(1, ""); - } - } - return mp_const_none; - } -} -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_uos_dupterm_obj, 0, 1, mp_uos_dupterm); - -#endif diff --git a/extmod/uos_dupterm.c b/extmod/uos_dupterm.c new file mode 100644 index 000000000..d888099df --- /dev/null +++ b/extmod/uos_dupterm.c @@ -0,0 +1,93 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Paul Sokolovsky + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include +#include "py/mpconfig.h" + +#include "py/nlr.h" +#include "py/runtime.h" +#include "py/objtuple.h" +#include "py/objarray.h" +#include "py/stream.h" + +#if MICROPY_PY_OS_DUPTERM + +void mp_uos_deactivate(const char *msg, mp_obj_t exc) { + mp_obj_t term = MP_STATE_PORT(term_obj); + MP_STATE_PORT(term_obj) = NULL; + mp_printf(&mp_plat_print, msg); + if (exc != MP_OBJ_NULL) { + mp_obj_print_exception(&mp_plat_print, exc); + } + mp_stream_close(term); +} + +void mp_uos_dupterm_tx_strn(const char *str, size_t len) { + if (MP_STATE_PORT(term_obj) != MP_OBJ_NULL) { + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + mp_obj_t write_m[3]; + mp_load_method(MP_STATE_PORT(term_obj), MP_QSTR_write, write_m); + + mp_obj_array_t *arr = MP_OBJ_TO_PTR(MP_STATE_PORT(dupterm_arr_obj)); + void *org_items = arr->items; + arr->items = (void*)str; + arr->len = len; + write_m[2] = MP_STATE_PORT(dupterm_arr_obj); + mp_call_method_n_kw(1, 0, write_m); + arr = MP_OBJ_TO_PTR(MP_STATE_PORT(dupterm_arr_obj)); + arr->items = org_items; + arr->len = 1; + nlr_pop(); + } else { + mp_uos_deactivate("dupterm: Exception in write() method, deactivating: ", nlr.ret_val); + } + } +} + +STATIC mp_obj_t mp_uos_dupterm(mp_uint_t n_args, const mp_obj_t *args) { + if (n_args == 0) { + if (MP_STATE_PORT(term_obj) == MP_OBJ_NULL) { + return mp_const_none; + } else { + return MP_STATE_PORT(term_obj); + } + } else { + if (args[0] == mp_const_none) { + MP_STATE_PORT(term_obj) = MP_OBJ_NULL; + } else { + MP_STATE_PORT(term_obj) = args[0]; + if (MP_STATE_PORT(dupterm_arr_obj) == MP_OBJ_NULL) { + MP_STATE_PORT(dupterm_arr_obj) = mp_obj_new_bytearray(1, ""); + } + } + return mp_const_none; + } +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_uos_dupterm_obj, 0, 1, mp_uos_dupterm); + +#endif diff --git a/py/py.mk b/py/py.mk index ab07bbbde..767fcac52 100644 --- a/py/py.mk +++ b/py/py.mk @@ -230,7 +230,7 @@ PY_O_BASENAME = \ ../extmod/vfs_fat_lexer.o \ ../extmod/vfs_fat_misc.o \ ../extmod/utime_mphal.o \ - ../extmod/moduos_dupterm.o \ + ../extmod/uos_dupterm.o \ ../lib/embed/abort_.o \ ../lib/utils/printf.o \ -- cgit v1.2.3 From 6ed5583f8cfa5aa0afb776654cd996abaaa5fba3 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 29 Oct 2016 05:02:24 +0300 Subject: extmod/utime_mphal: ticks_diff(): switch arg order, return signed value. Based on the earlier discussed RFC. Practice showed that the most natural order for arguments corresponds to mathematical subtraction: ticks_diff(x, y) <=> x - y Also, practice showed that in real life, it's hard to order events by time of occurance a priori, events tend to miss deadlines, etc. and the expected order breaks. And then there's a need to detect such cases. And ticks_diff can be used exactly for this purpose, if it returns a signed, instead of unsigned, value. E.g. if x is scheduled time for event, and y is the current time, then if ticks_diff(x, y) < 0 then event has missed a deadline (and e.g. needs to executed ASAP or skipped). Returning in this case a large unsigned number (like ticks_diff behaved previously) doesn't make sense, and such "large unsigned number" can't be reliably detected per our definition of ticks_* function (we don't expose to user level maximum value, it can be anything, relatively small or relatively large). --- extmod/utime_mphal.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'extmod') diff --git a/extmod/utime_mphal.c b/extmod/utime_mphal.c index 3ecdc9446..4a03bf94b 100644 --- a/extmod/utime_mphal.c +++ b/extmod/utime_mphal.c @@ -78,11 +78,11 @@ STATIC mp_obj_t time_ticks_cpu(void) { } MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_ticks_cpu_obj, time_ticks_cpu); -STATIC mp_obj_t time_ticks_diff(mp_obj_t start_in, mp_obj_t end_in) { +STATIC mp_obj_t time_ticks_diff(mp_obj_t end_in, mp_obj_t start_in) { // we assume that the arguments come from ticks_xx so are small ints uint32_t start = MP_OBJ_SMALL_INT_VALUE(start_in); uint32_t end = MP_OBJ_SMALL_INT_VALUE(end_in); - return MP_OBJ_NEW_SMALL_INT((end - start) & MP_SMALL_INT_POSITIVE_MASK); + return MP_OBJ_NEW_SMALL_INT((int32_t)(end - start)); } MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_diff_obj, time_ticks_diff); -- cgit v1.2.3 From 6a87084019e3e78eda0a21a3c80d600d0bd21991 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 29 Oct 2016 13:42:36 +0300 Subject: extmod/utime_mphal: Add MP_THREAD_GIL_EXIT/ENTER warppers for sleep functions. Ported from unix port. --- extmod/utime_mphal.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'extmod') diff --git a/extmod/utime_mphal.c b/extmod/utime_mphal.c index 4a03bf94b..9bdf84188 100644 --- a/extmod/utime_mphal.c +++ b/extmod/utime_mphal.c @@ -33,14 +33,17 @@ #include "py/obj.h" #include "py/mphal.h" #include "py/smallint.h" +#include "py/runtime.h" #include "extmod/utime_mphal.h" STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) { + MP_THREAD_GIL_EXIT(); #if MICROPY_PY_BUILTINS_FLOAT mp_hal_delay_ms(1000 * mp_obj_get_float(seconds_o)); #else mp_hal_delay_ms(1000 * mp_obj_get_int(seconds_o)); #endif + MP_THREAD_GIL_ENTER(); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_1(mp_utime_sleep_obj, time_sleep); @@ -48,7 +51,9 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_utime_sleep_obj, time_sleep); STATIC mp_obj_t time_sleep_ms(mp_obj_t arg) { mp_int_t ms = mp_obj_get_int(arg); if (ms > 0) { + MP_THREAD_GIL_EXIT(); mp_hal_delay_ms(ms); + MP_THREAD_GIL_ENTER(); } return mp_const_none; } @@ -57,7 +62,9 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_utime_sleep_ms_obj, time_sleep_ms); STATIC mp_obj_t time_sleep_us(mp_obj_t arg) { mp_int_t us = mp_obj_get_int(arg); if (us > 0) { + MP_THREAD_GIL_EXIT(); mp_hal_delay_us(us); + MP_THREAD_GIL_ENTER(); } return mp_const_none; } -- cgit v1.2.3 From d86cac4b8268bbe9203cdd7ab8d8c2a65d142986 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 29 Oct 2016 17:30:05 +0300 Subject: extmod/utime_mphal: Implement ticks_add(), add to all maintained ports. --- esp8266/modutime.c | 1 + extmod/utime_mphal.c | 8 ++++++++ extmod/utime_mphal.h | 1 + stmhal/modutime.c | 1 + unix/modtime.c | 1 + zephyr/modutime.c | 1 + 6 files changed, 13 insertions(+) (limited to 'extmod') diff --git a/esp8266/modutime.c b/esp8266/modutime.c index abfe069cc..f3dfc462d 100644 --- a/esp8266/modutime.c +++ b/esp8266/modutime.c @@ -119,6 +119,7 @@ STATIC const mp_map_elem_t time_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_ticks_ms), (mp_obj_t)&mp_utime_ticks_ms_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_ticks_us), (mp_obj_t)&mp_utime_ticks_us_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_ticks_cpu), (mp_obj_t)&mp_utime_ticks_cpu_obj }, + { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) }, { MP_OBJ_NEW_QSTR(MP_QSTR_ticks_diff), (mp_obj_t)&mp_utime_ticks_diff_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_time_obj }, }; diff --git a/extmod/utime_mphal.c b/extmod/utime_mphal.c index 9bdf84188..609a0da84 100644 --- a/extmod/utime_mphal.c +++ b/extmod/utime_mphal.c @@ -93,4 +93,12 @@ STATIC mp_obj_t time_ticks_diff(mp_obj_t end_in, mp_obj_t start_in) { } MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_diff_obj, time_ticks_diff); +STATIC mp_obj_t time_ticks_add(mp_obj_t ticks_in, mp_obj_t delta_in) { + // we assume that first argument come from ticks_xx so is small int + uint32_t ticks = MP_OBJ_SMALL_INT_VALUE(ticks_in); + uint32_t delta = (uint32_t)mp_obj_get_int(delta_in); + return MP_OBJ_NEW_SMALL_INT((ticks + delta) & MP_SMALL_INT_POSITIVE_MASK); +} +MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_add_obj, time_ticks_add); + #endif // MICROPY_PY_UTIME_MP_HAL diff --git a/extmod/utime_mphal.h b/extmod/utime_mphal.h index c7611e251..644387b67 100644 --- a/extmod/utime_mphal.h +++ b/extmod/utime_mphal.h @@ -34,3 +34,4 @@ MP_DECLARE_CONST_FUN_OBJ_0(mp_utime_ticks_ms_obj); MP_DECLARE_CONST_FUN_OBJ_0(mp_utime_ticks_us_obj); MP_DECLARE_CONST_FUN_OBJ_0(mp_utime_ticks_cpu_obj); MP_DECLARE_CONST_FUN_OBJ_2(mp_utime_ticks_diff_obj); +MP_DECLARE_CONST_FUN_OBJ_2(mp_utime_ticks_add_obj); diff --git a/stmhal/modutime.c b/stmhal/modutime.c index fae360bb0..501b62582 100644 --- a/stmhal/modutime.c +++ b/stmhal/modutime.c @@ -142,6 +142,7 @@ STATIC const mp_map_elem_t time_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_ticks_ms), (mp_obj_t)&mp_utime_ticks_ms_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_ticks_us), (mp_obj_t)&mp_utime_ticks_us_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_ticks_cpu), (mp_obj_t)&mp_utime_ticks_cpu_obj }, + { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) }, { MP_OBJ_NEW_QSTR(MP_QSTR_ticks_diff), (mp_obj_t)&mp_utime_ticks_diff_obj }, }; diff --git a/unix/modtime.c b/unix/modtime.c index 33cf1cdab..e90826f14 100644 --- a/unix/modtime.c +++ b/unix/modtime.c @@ -152,6 +152,7 @@ STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&mod_time_time_obj) }, { MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj) }, { MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) }, { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) }, { MP_ROM_QSTR(MP_QSTR_strftime), MP_ROM_PTR(&mod_time_strftime_obj) }, }; diff --git a/zephyr/modutime.c b/zephyr/modutime.c index b18d6b837..8b96a5ab1 100644 --- a/zephyr/modutime.c +++ b/zephyr/modutime.c @@ -53,6 +53,7 @@ STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj) }, { MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj) }, { MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) }, { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) }, }; -- cgit v1.2.3 From 76146b3d9ab0ab88ce0f0d1cfa0235ec0758a4a9 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 30 Oct 2016 03:02:07 +0300 Subject: extmod/utime_mphal: Allow ticks functions period be configurable by a port. Using MICROPY_PY_UTIME_TICKS_PERIOD config var. --- extmod/utime_mphal.c | 8 ++++---- py/mpconfig.h | 10 ++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) (limited to 'extmod') diff --git a/extmod/utime_mphal.c b/extmod/utime_mphal.c index 609a0da84..1a32180cf 100644 --- a/extmod/utime_mphal.c +++ b/extmod/utime_mphal.c @@ -71,17 +71,17 @@ STATIC mp_obj_t time_sleep_us(mp_obj_t arg) { MP_DEFINE_CONST_FUN_OBJ_1(mp_utime_sleep_us_obj, time_sleep_us); STATIC mp_obj_t time_ticks_ms(void) { - return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms() & MP_SMALL_INT_POSITIVE_MASK); + return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms() & (MICROPY_PY_UTIME_TICKS_PERIOD - 1)); } MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_ticks_ms_obj, time_ticks_ms); STATIC mp_obj_t time_ticks_us(void) { - return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_us() & MP_SMALL_INT_POSITIVE_MASK); + return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_us() & (MICROPY_PY_UTIME_TICKS_PERIOD - 1)); } MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_ticks_us_obj, time_ticks_us); STATIC mp_obj_t time_ticks_cpu(void) { - return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_cpu() & MP_SMALL_INT_POSITIVE_MASK); + return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_cpu() & (MICROPY_PY_UTIME_TICKS_PERIOD - 1)); } MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_ticks_cpu_obj, time_ticks_cpu); @@ -97,7 +97,7 @@ STATIC mp_obj_t time_ticks_add(mp_obj_t ticks_in, mp_obj_t delta_in) { // we assume that first argument come from ticks_xx so is small int uint32_t ticks = MP_OBJ_SMALL_INT_VALUE(ticks_in); uint32_t delta = (uint32_t)mp_obj_get_int(delta_in); - return MP_OBJ_NEW_SMALL_INT((ticks + delta) & MP_SMALL_INT_POSITIVE_MASK); + return MP_OBJ_NEW_SMALL_INT((ticks + delta) & (MICROPY_PY_UTIME_TICKS_PERIOD - 1)); } MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_add_obj, time_ticks_add); diff --git a/py/mpconfig.h b/py/mpconfig.h index dcdaffe0f..fce997692 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -866,6 +866,16 @@ typedef double mp_float_t; #define MICROPY_PY_UTIME_MP_HAL (0) #endif +// Period of values returned by utime.ticks_ms(), ticks_us(), ticks_cpu() +// functions. Should be power of two. All functions above use the same +// period, so if underlying hardware/API has different periods, the +// minimum of them should be used. The value below is the maximum value +// this parameter can take (corresponding to 30 bit tick values on 32-bit +// system). +#ifndef MICROPY_PY_UTIME_TICKS_PERIOD +#define MICROPY_PY_UTIME_TICKS_PERIOD (MP_SMALL_INT_POSITIVE_MASK + 1) +#endif + // Whether to provide "_thread" module #ifndef MICROPY_PY_THREAD #define MICROPY_PY_THREAD (0) -- cgit v1.2.3 From e429daa5724e5e3c5f8866742abc64eb90ded71d Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 30 Oct 2016 03:07:22 +0300 Subject: extmod/utime_mphal: Fix implementation of new semantics of ticks_diff(). Now the function properly uses ring arithmetic to return signed value in range (inclusive): [-MICROPY_PY_UTIME_TICKS_PERIOD/2, MICROPY_PY_UTIME_TICKS_PERIOD/2-1]. That means that function can properly process 2 time values away from each other within MICROPY_PY_UTIME_TICKS_PERIOD/2 ticks, but away in both directions. For example, if tick value 'a' predates tick value 'b', ticks_diff(a, b) will return negative value, and positive value otherwise. But at positive value of MICROPY_PY_UTIME_TICKS_PERIOD/2-1, the result of the function will wrap around to negative -MICROPY_PY_UTIME_TICKS_PERIOD/2, in other words, if a follows b in more than MICROPY_PY_UTIME_TICKS_PERIOD/2 - 1 ticks, the function will "consider" a to actually predate b. --- extmod/utime_mphal.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'extmod') diff --git a/extmod/utime_mphal.c b/extmod/utime_mphal.c index 1a32180cf..9aa2f1324 100644 --- a/extmod/utime_mphal.c +++ b/extmod/utime_mphal.c @@ -89,7 +89,13 @@ STATIC mp_obj_t time_ticks_diff(mp_obj_t end_in, mp_obj_t start_in) { // we assume that the arguments come from ticks_xx so are small ints uint32_t start = MP_OBJ_SMALL_INT_VALUE(start_in); uint32_t end = MP_OBJ_SMALL_INT_VALUE(end_in); - return MP_OBJ_NEW_SMALL_INT((int32_t)(end - start)); + int32_t diff = end - start; + if (diff < (signed)-(MICROPY_PY_UTIME_TICKS_PERIOD / 2)) { + diff += MICROPY_PY_UTIME_TICKS_PERIOD; + } else if (diff >= (signed)(MICROPY_PY_UTIME_TICKS_PERIOD / 2)) { + diff -= MICROPY_PY_UTIME_TICKS_PERIOD; + } + return MP_OBJ_NEW_SMALL_INT(diff); } MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_diff_obj, time_ticks_diff); -- cgit v1.2.3 From 10bde6933e45289eeb8b0a564b088fadc4309c17 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 2 Nov 2016 02:50:48 +0300 Subject: extmod/utime_mphal: ticks_diff(): Optimize to avoid if conditions. --- extmod/utime_mphal.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'extmod') diff --git a/extmod/utime_mphal.c b/extmod/utime_mphal.c index 9aa2f1324..a563b1757 100644 --- a/extmod/utime_mphal.c +++ b/extmod/utime_mphal.c @@ -89,12 +89,10 @@ STATIC mp_obj_t time_ticks_diff(mp_obj_t end_in, mp_obj_t start_in) { // we assume that the arguments come from ticks_xx so are small ints uint32_t start = MP_OBJ_SMALL_INT_VALUE(start_in); uint32_t end = MP_OBJ_SMALL_INT_VALUE(end_in); - int32_t diff = end - start; - if (diff < (signed)-(MICROPY_PY_UTIME_TICKS_PERIOD / 2)) { - diff += MICROPY_PY_UTIME_TICKS_PERIOD; - } else if (diff >= (signed)(MICROPY_PY_UTIME_TICKS_PERIOD / 2)) { - diff -= MICROPY_PY_UTIME_TICKS_PERIOD; - } + // Optimized formula avoiding if conditions. We adjust difference "forward", + // wrap it around and adjust back. + int32_t diff = ((end - start + MICROPY_PY_UTIME_TICKS_PERIOD / 2) & (MICROPY_PY_UTIME_TICKS_PERIOD - 1)) + - MICROPY_PY_UTIME_TICKS_PERIOD / 2; return MP_OBJ_NEW_SMALL_INT(diff); } MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_diff_obj, time_ticks_diff); -- cgit v1.2.3 From 9b345a9e482f28547e270b5c86ed3e022e56e237 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 3 Nov 2016 23:54:16 +0300 Subject: extmod/utime_mphal: ticks_diff/ticks_add: Don't hardcode 32-bit types. Use normal mp_int_t/mp_uint_t types, algorithms (hm, formulas) can work with any type width. --- extmod/utime_mphal.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'extmod') diff --git a/extmod/utime_mphal.c b/extmod/utime_mphal.c index a563b1757..f447b3a68 100644 --- a/extmod/utime_mphal.c +++ b/extmod/utime_mphal.c @@ -87,11 +87,11 @@ MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_ticks_cpu_obj, time_ticks_cpu); STATIC mp_obj_t time_ticks_diff(mp_obj_t end_in, mp_obj_t start_in) { // we assume that the arguments come from ticks_xx so are small ints - uint32_t start = MP_OBJ_SMALL_INT_VALUE(start_in); - uint32_t end = MP_OBJ_SMALL_INT_VALUE(end_in); + mp_uint_t start = MP_OBJ_SMALL_INT_VALUE(start_in); + mp_uint_t end = MP_OBJ_SMALL_INT_VALUE(end_in); // Optimized formula avoiding if conditions. We adjust difference "forward", // wrap it around and adjust back. - int32_t diff = ((end - start + MICROPY_PY_UTIME_TICKS_PERIOD / 2) & (MICROPY_PY_UTIME_TICKS_PERIOD - 1)) + mp_int_t diff = ((end - start + MICROPY_PY_UTIME_TICKS_PERIOD / 2) & (MICROPY_PY_UTIME_TICKS_PERIOD - 1)) - MICROPY_PY_UTIME_TICKS_PERIOD / 2; return MP_OBJ_NEW_SMALL_INT(diff); } @@ -99,8 +99,8 @@ MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_diff_obj, time_ticks_diff); STATIC mp_obj_t time_ticks_add(mp_obj_t ticks_in, mp_obj_t delta_in) { // we assume that first argument come from ticks_xx so is small int - uint32_t ticks = MP_OBJ_SMALL_INT_VALUE(ticks_in); - uint32_t delta = (uint32_t)mp_obj_get_int(delta_in); + mp_uint_t ticks = MP_OBJ_SMALL_INT_VALUE(ticks_in); + mp_uint_t delta = mp_obj_get_int(delta_in); return MP_OBJ_NEW_SMALL_INT((ticks + delta) & (MICROPY_PY_UTIME_TICKS_PERIOD - 1)); } MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_add_obj, time_ticks_add); -- cgit v1.2.3