2016-10-14 17:13:02 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013-2016 Damien P. George
|
|
|
|
* 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 "py/mpconfig.h"
|
|
|
|
#if MICROPY_PY_UTIME_MP_HAL
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/mphal.h"
|
|
|
|
#include "py/smallint.h"
|
2016-10-29 10:42:36 +00:00
|
|
|
#include "py/runtime.h"
|
2016-10-14 17:13:02 +00:00
|
|
|
#include "extmod/utime_mphal.h"
|
|
|
|
|
|
|
|
STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) {
|
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2017-06-30 23:23:29 +00:00
|
|
|
mp_hal_delay_ms((mp_uint_t)(1000 * mp_obj_get_float(seconds_o)));
|
2016-10-14 17:13:02 +00:00
|
|
|
#else
|
|
|
|
mp_hal_delay_ms(1000 * mp_obj_get_int(seconds_o));
|
|
|
|
#endif
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(mp_utime_sleep_obj, time_sleep);
|
|
|
|
|
|
|
|
STATIC mp_obj_t time_sleep_ms(mp_obj_t arg) {
|
2016-10-14 19:19:45 +00:00
|
|
|
mp_int_t ms = mp_obj_get_int(arg);
|
|
|
|
if (ms > 0) {
|
|
|
|
mp_hal_delay_ms(ms);
|
|
|
|
}
|
2016-10-14 17:13:02 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
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) {
|
2016-10-14 19:19:45 +00:00
|
|
|
mp_int_t us = mp_obj_get_int(arg);
|
|
|
|
if (us > 0) {
|
|
|
|
mp_hal_delay_us(us);
|
|
|
|
}
|
2016-10-14 17:13:02 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(mp_utime_sleep_us_obj, time_sleep_us);
|
|
|
|
|
|
|
|
STATIC mp_obj_t time_ticks_ms(void) {
|
2016-10-30 00:02:07 +00:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms() & (MICROPY_PY_UTIME_TICKS_PERIOD - 1));
|
2016-10-14 17:13:02 +00:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_ticks_ms_obj, time_ticks_ms);
|
|
|
|
|
|
|
|
STATIC mp_obj_t time_ticks_us(void) {
|
2016-10-30 00:02:07 +00:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_us() & (MICROPY_PY_UTIME_TICKS_PERIOD - 1));
|
2016-10-14 17:13:02 +00:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_ticks_us_obj, time_ticks_us);
|
|
|
|
|
|
|
|
STATIC mp_obj_t time_ticks_cpu(void) {
|
2016-10-30 00:02:07 +00:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_cpu() & (MICROPY_PY_UTIME_TICKS_PERIOD - 1));
|
2016-10-14 17:13:02 +00:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_ticks_cpu_obj, time_ticks_cpu);
|
|
|
|
|
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).
2016-10-29 02:02:24 +00:00
|
|
|
STATIC mp_obj_t time_ticks_diff(mp_obj_t end_in, mp_obj_t start_in) {
|
2016-10-14 17:13:02 +00:00
|
|
|
// we assume that the arguments come from ticks_xx so are small ints
|
2016-11-03 20:54:16 +00:00
|
|
|
mp_uint_t start = MP_OBJ_SMALL_INT_VALUE(start_in);
|
|
|
|
mp_uint_t end = MP_OBJ_SMALL_INT_VALUE(end_in);
|
2016-11-01 23:50:48 +00:00
|
|
|
// Optimized formula avoiding if conditions. We adjust difference "forward",
|
|
|
|
// wrap it around and adjust back.
|
2016-11-03 20:54:16 +00:00
|
|
|
mp_int_t diff = ((end - start + MICROPY_PY_UTIME_TICKS_PERIOD / 2) & (MICROPY_PY_UTIME_TICKS_PERIOD - 1))
|
2016-11-01 23:50:48 +00:00
|
|
|
- MICROPY_PY_UTIME_TICKS_PERIOD / 2;
|
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.
2016-10-30 00:07:22 +00:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(diff);
|
2016-10-14 17:13:02 +00:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_diff_obj, time_ticks_diff);
|
|
|
|
|
2016-10-29 14:30:05 +00:00
|
|
|
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
|
2016-11-03 20:54:16 +00:00
|
|
|
mp_uint_t ticks = MP_OBJ_SMALL_INT_VALUE(ticks_in);
|
|
|
|
mp_uint_t delta = mp_obj_get_int(delta_in);
|
2016-10-30 00:02:07 +00:00
|
|
|
return MP_OBJ_NEW_SMALL_INT((ticks + delta) & (MICROPY_PY_UTIME_TICKS_PERIOD - 1));
|
2016-10-29 14:30:05 +00:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_add_obj, time_ticks_add);
|
|
|
|
|
2016-10-14 17:13:02 +00:00
|
|
|
#endif // MICROPY_PY_UTIME_MP_HAL
|