forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add precise_time module #839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1a1aab8
add precise_time module
margaret 45d42c3
add missing semicolons
margaret 1b4e064
more detail in module docs
margaret 6ed180d
compiles on gemma_m0
margaret 411140a
fix rst spacing
margaret 0490d5b
add precise_time.monotonic to tick count
margaret f1ea5d3
add precise_time.sleep
margaret 419d2ee
fix rst
margaret 2bbda80
add precise_time to esp8266 common-hal
margaret cd717e0
add precise_time to nrf common-hal
margaret ab3d2a6
fix qstr def for precise_time_module in nrf
margaret File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2018 Margaret Sy | ||
* | ||
* 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/mphal.h" | ||
|
||
#include "shared-bindings/precise_time/__init__.h" | ||
|
||
#include "tick.h" | ||
|
||
inline uint64_t common_hal_precise_time_monotonic() { | ||
return ticks_ms; | ||
} | ||
|
||
void common_hal_precise_time_delay_ms(uint32_t delay) { | ||
mp_hal_delay_ms(delay); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2018 Margaret Sy | ||
* | ||
* 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/mphal.h" | ||
|
||
#include "shared-bindings/precise_time/__init__.h" | ||
|
||
#include "ets_alt_task.h" | ||
#include "user_interface.h" | ||
|
||
inline uint64_t common_hal_precise_time_monotonic() { | ||
return ((uint64_t)system_time_high_word << 32 | (uint64_t)system_get_time()) / 1000; | ||
} | ||
|
||
void common_hal_precise_time_delay_ms(uint32_t delay) { | ||
mp_hal_delay_ms(delay); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2018 Margaret Sy | ||
* | ||
* 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/mphal.h" | ||
|
||
#include "shared-bindings/precise_time/__init__.h" | ||
#include "tick.h" | ||
|
||
inline uint64_t common_hal_precise_time_monotonic() { | ||
return ticks_ms; | ||
} | ||
|
||
void common_hal_precise_time_delay_ms(uint32_t delay) { | ||
mp_hal_delay_ms(delay); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2018 Margaret Sy | ||
* | ||
* 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 <string.h> | ||
|
||
#include "py/obj.h" | ||
#include "py/runtime.h" | ||
#include "shared-bindings/precise_time/__init__.h" | ||
|
||
//| :mod:`precise_time` --- integer time and timing related functions | ||
//| ======================================================================= | ||
//| | ||
//| .. module:: precise_time | ||
//| :synopsis: integer time related functions | ||
//| :platform: SAMD21 | ||
//| | ||
//| The `precise_time` module. See the `time` module instead for an API that is | ||
//| also available in CPython. | ||
//| | ||
//| .. method:: monotonic() | ||
//| | ||
//| Returns an always increasing value of time with an unknown reference | ||
//| point. Only use it to compare against other values from `monotonic`. | ||
//| Unlike time.monotonic, which is a float, precise_time.monotonic returns an | ||
//| int so precision will not degrade over time. | ||
//| | ||
//| :return: the current monotonic time | ||
//| :rtype: int | ||
//| | ||
STATIC mp_obj_t precise_time_monotonic(void) { | ||
return mp_obj_new_int_from_uint(common_hal_precise_time_monotonic()); | ||
} | ||
MP_DEFINE_CONST_FUN_OBJ_0(precise_time_monotonic_obj, precise_time_monotonic); | ||
|
||
//| .. method:: sleep(milliseconds) | ||
//| | ||
//| Sleep for a given number of milliseconds. | ||
//| | ||
//| :param int seconds: the time to sleep in milliseconds | ||
//| | ||
STATIC mp_obj_t precise_time_sleep(mp_obj_t milliseconds_o) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense. I'll rename it to |
||
int milliseconds = mp_obj_get_int(milliseconds_o); | ||
if (milliseconds < 0) { | ||
mp_raise_ValueError("sleep length must be non-negative"); | ||
} | ||
common_hal_precise_time_delay_ms(milliseconds); | ||
return mp_const_none; | ||
} | ||
MP_DEFINE_CONST_FUN_OBJ_1(precise_time_sleep_obj, precise_time_sleep); | ||
|
||
STATIC const mp_rom_map_elem_t precise_time_module_globals_table[] = { | ||
{ MP_ROM_QSTR(MP_QSTR__name__), MP_ROM_QSTR(MP_QSTR_precise_time) }, | ||
|
||
{ MP_ROM_QSTR(MP_QSTR_monotonic), MP_ROM_PTR(&precise_time_monotonic_obj) }, | ||
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&precise_time_sleep_obj) } | ||
}; | ||
|
||
STATIC MP_DEFINE_CONST_DICT(precise_time_module_globals, precise_time_module_globals_table); | ||
|
||
const mp_obj_module_t precise_time_module = { | ||
.base = { &mp_type_module }, | ||
.globals = (mp_obj_dict_t*)&precise_time_module_globals, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2018 Margaret Sy | ||
* | ||
* 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. | ||
*/ | ||
|
||
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_PRECISE_TIME__INIT__H | ||
#define MICROPY_INCLUDED_SHARED_BINDINGS_PRECISE_TIME__INIT__H | ||
|
||
#include <stdint.h> | ||
|
||
extern uint64_t common_hal_precise_time_monotonic(void); | ||
extern void common_hal_precise_time_delay_ms(uint32_t); | ||
|
||
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_PRECISE_TIME__INIT__H |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I have not been in the loop on this, so @tannewt and @margaret please let me know if you've discussed this already).
mp_obj_new_int_from_uint()
will silently truncate thecommon_hal_precise_time_monotonic()
value when it overflows 2^32. 2^32 msecs is about 49.7 days. I could imagine a long-lived sensing program that hits that limit. If the incomingcommon_hal_precise_time_monotonic()
is > 2^32 you could callmp_obj_new_int_from_ull()
, which takes along long
(which is 64 bits).Note that on non-longint builds,
py/objint.c
is used, andmp_obj_new_int_from_ull()
throwsOverflowError
no matter what it's passed (even the arg would fit in a small int). On longint impls,py/objint_mpz.c
is used. So the value needs to be tested and one or the other needs to be called depending on the size.I think the behavior on non-longint impls should probably be documented:
precise_time.time_monotonic()
will raiseOverflowError
when the board has been running for 2^30 msecs or more (about 12.4 days)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We hadn't discussed how to deal with overflow, but these suggestions sound good. I'll work on these items later today.