summaryrefslogtreecommitdiffstatshomepage
path: root/stmhal/modpyb.c
diff options
context:
space:
mode:
authorDave Hylands <dhylands@gmail.com>2014-08-23 12:21:12 -0700
committerDamien George <damien.p.george@gmail.com>2014-08-25 17:38:55 +0100
commit2bf044442eae7dbdaff91051d2c135b4aa51f1b2 (patch)
treef22d657138d316cc1e3b1669d001c90d14809741 /stmhal/modpyb.c
parent8c0add4eeeb77b8b4c6cb5a0313e6f06b0a62d32 (diff)
downloadmicropython-2bf044442eae7dbdaff91051d2c135b4aa51f1b2.tar.gz
micropython-2bf044442eae7dbdaff91051d2c135b4aa51f1b2.zip
Add support for pyb.micros() by using the systick timer.
I also removed trailing spaces from modpyb.c which affected a couple of lines technically not part of this patch. Tested using: https://github.com/dhylands/upy-examples/blob/master/micros_test.py which eventually fails due to wraparound issues (I could fix the test to compensate but didn't bother)
Diffstat (limited to 'stmhal/modpyb.c')
-rw-r--r--stmhal/modpyb.c42
1 files changed, 39 insertions, 3 deletions
diff --git a/stmhal/modpyb.c b/stmhal/modpyb.c
index de7eac4024..e1d6d98bba 100644
--- a/stmhal/modpyb.c
+++ b/stmhal/modpyb.c
@@ -99,7 +99,7 @@ STATIC mp_obj_t pyb_info(uint n_args, const mp_obj_t *args) {
// get and print clock speeds
// SYSCLK=168MHz, HCLK=168MHz, PCLK1=42MHz, PCLK2=84MHz
{
- printf("S=%lu\nH=%lu\nP1=%lu\nP2=%lu\n",
+ printf("S=%lu\nH=%lu\nP1=%lu\nP2=%lu\n",
HAL_RCC_GetSysClockFreq(),
HAL_RCC_GetHCLKFreq(),
HAL_RCC_GetPCLK1Freq(),
@@ -187,11 +187,46 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_sync_obj, pyb_sync);
/// \function millis()
/// Returns the number of milliseconds since the board was last reset.
+///
+/// Note that this may return a negative number. This allows you to always
+/// do:
+/// start = pyb.millis()
+/// ...do some operation...
+/// elapsed = pyb.millis() - start
+///
+/// and as long as the time of your operation is less than 24 days, you'll
+/// always get the right answer and not have to worry about whether pyb.millis()
+/// wraps around.
STATIC mp_obj_t pyb_millis(void) {
- return mp_obj_new_int(HAL_GetTick());
+ // We want to "cast" the 32 bit unsigned into a small-int. So we shift it
+ // left by 1 to throw away the top bit, and then shift it right by one
+ // to sign extend.
+ mp_int_t val = HAL_GetTick() << 1;
+ return mp_obj_new_int(val >> 1);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis);
+/// \function micros()
+/// Returns the number of microseconds since the board was last reset.
+///
+/// Note that this may return a negative number. This allows you to always
+/// do:
+/// start = pyb.micros()
+/// ...do some operation...
+/// elapsed = pyb.micros() - start
+///
+/// and as long as the time of your operation is less than 35 minutes, you'll
+/// always get the right answer and not have to worry about whether pyb.micros()
+/// wraps around.
+STATIC mp_obj_t pyb_micros(void) {
+ // We want to "cast" the 32 bit unsigned into a small-int. So we shift it
+ // left by 1 to throw away the top bit, and then shift it right by one
+ // to sign extend.
+ mp_int_t val = sys_tick_get_microseconds() << 1;
+ return mp_obj_new_int(val >> 1);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_micros_obj, pyb_micros);
+
/// \function delay(ms)
/// Delay for the given number of milliseconds.
STATIC mp_obj_t pyb_delay(mp_obj_t ms_in) {
@@ -251,7 +286,7 @@ STATIC mp_obj_t pyb_stop(void) {
/* Enter Stop Mode */
PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
- /* Configures system clock after wake-up from STOP: enable HSE, PLL and select
+ /* Configures system clock after wake-up from STOP: enable HSE, PLL and select
* PLL as system clock source (HSE and PLL are disabled in STOP mode) */
SYSCLKConfig_STOP();
@@ -343,6 +378,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_USB_VCP), (mp_obj_t)&pyb_usb_vcp_type },
{ MP_OBJ_NEW_QSTR(MP_QSTR_millis), (mp_obj_t)&pyb_millis_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_micros), (mp_obj_t)&pyb_micros_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_delay), (mp_obj_t)&pyb_delay_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_udelay), (mp_obj_t)&pyb_udelay_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&pyb_sync_obj },