From 1f2daf430410c7c8a05026ccab4b047bb928fbdc Mon Sep 17 00:00:00 2001 From: danicampora Date: Wed, 21 Oct 2015 11:05:45 +0200 Subject: [PATCH] cc3200: Correct ticks_cpu and ticks_us functions in time module. --- cc3200/misc/mpsystick.c | 22 ++-------------------- cc3200/mods/modutime.c | 18 +++++------------- tests/wipy/time.py | 6 ++---- 3 files changed, 9 insertions(+), 37 deletions(-) diff --git a/cc3200/misc/mpsystick.c b/cc3200/misc/mpsystick.c index 21c147449..05fe4fca7 100644 --- a/cc3200/misc/mpsystick.c +++ b/cc3200/misc/mpsystick.c @@ -58,32 +58,14 @@ void sys_tick_wait_at_least(uint32_t start_tick, uint32_t delay_ms) { // The SysTick timer counts down at HAL_FCPU_HZ, so we can use that knowledge // to grab a microsecond counter. -// // We assume that HAL_GetTick returns milliseconds. uint32_t sys_tick_get_microseconds(void) { mp_uint_t irq_state = disable_irq(); uint32_t counter = SysTickValueGet(); uint32_t milliseconds = HAL_GetTick(); - uint32_t status = (HWREG(NVIC_ST_CTRL)); enable_irq(irq_state); - // It's still possible for the countflag bit to get set if the counter was - // reloaded between reading VAL and reading CTRL. With interrupts disabled - // it definitely takes less than 50 HCLK cycles between reading VAL and - // reading CTRL, so the test (counter > 50) is to cover the case where VAL - // is +ve and very close to zero, and the COUNTFLAG bit is also set. - if ((status & NVIC_ST_CTRL_COUNT) && counter > 50) { - // This means that the HW reloaded VAL between the time we read VAL and the - // time we read CTRL, which implies that there is an interrupt pending - // to increment the tick counter. - milliseconds++; - } - uint32_t load = (HWREG(NVIC_ST_RELOAD)); + uint32_t load = SysTickPeriodGet(); counter = load - counter; // Convert from decrementing to incrementing - - // ((load + 1) / 1000) is the number of counts per microsecond. - // - // counter / ((load + 1) / 1000) scales from the systick clock to microseconds - // and is the same thing as (counter * 1000) / (load + 1) - return milliseconds * 1000 + (counter * 1000) / (load + 1); + return (milliseconds * 1000) + ((counter * 1000) / load); } diff --git a/cc3200/mods/modutime.c b/cc3200/mods/modutime.c index 8c56ec92a..81a2d3291 100644 --- a/cc3200/mods/modutime.c +++ b/cc3200/mods/modutime.c @@ -150,33 +150,25 @@ STATIC mp_obj_t time_sleep_us (mp_obj_t usec_in) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_us_obj, time_sleep_us); STATIC mp_obj_t time_ticks_ms(void) { - // We want to "cast" the 32 bit unsigned into a small-int. This means - // copying the MSB down 1 bit (extending the sign down), which is - // equivalent to just using the MP_OBJ_NEW_SMALL_INT macro. + // We want to "cast" the 32 bit unsigned into a 30-bit small-int return MP_OBJ_NEW_SMALL_INT(HAL_GetTick() & MP_SMALL_INT_POSITIVE_MASK); } STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_ms_obj, time_ticks_ms); STATIC mp_obj_t time_ticks_us(void) { - // We want to "cast" the 32 bit unsigned into a small-int. This means - // copying the MSB down 1 bit (extending the sign down), which is - // equivalent to just using the MP_OBJ_NEW_SMALL_INT macro. + // We want to "cast" the 32 bit unsigned into a 30-bit small-int return MP_OBJ_NEW_SMALL_INT(sys_tick_get_microseconds() & MP_SMALL_INT_POSITIVE_MASK); } STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_us_obj, time_ticks_us); STATIC mp_obj_t time_ticks_cpu(void) { - // We want to "cast" the 32 bit unsigned into a small-int. This means - // copying the MSB down 1 bit (extending the sign down), which is - // equivalent to just using the MP_OBJ_NEW_SMALL_INT macro. - return MP_OBJ_NEW_SMALL_INT(SysTickValueGet() & MP_SMALL_INT_POSITIVE_MASK); + // We want to "cast" the 32 bit unsigned into a 30-bit small-int + return MP_OBJ_NEW_SMALL_INT((SysTickPeriodGet() - SysTickValueGet()) & MP_SMALL_INT_POSITIVE_MASK); } STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_cpu_obj, time_ticks_cpu); STATIC mp_obj_t time_ticks_diff(mp_obj_t t0, mp_obj_t t1) { - // We want to "cast" the 32 bit unsigned into a small-int. This means - // copying the MSB down 1 bit (extending the sign down), which is - // equivalent to just using the MP_OBJ_NEW_SMALL_INT macro. + // We want to "cast" the 32 bit unsigned into a 30-bit small-int uint32_t start = mp_obj_get_int(t0); uint32_t end = mp_obj_get_int(t1); return MP_OBJ_NEW_SMALL_INT((end - start) & MP_SMALL_INT_POSITIVE_MASK); diff --git a/tests/wipy/time.py b/tests/wipy/time.py index f0bfc8876..e6237de35 100644 --- a/tests/wipy/time.py +++ b/tests/wipy/time.py @@ -70,8 +70,6 @@ print(abs(time.ticks_diff(t1, t2)- 50) <= 1) t1 = time.ticks_us() time.sleep_us(1000) t2 = time.ticks_us() -print(time.ticks_diff(t1, t2) < 2000) +print(time.ticks_diff(t1, t2) < 1500) -t1 = time.ticks_cpu() -t2 = time.ticks_cpu() -print(time.ticks_diff(t1, t2) >= 0) +print(time.ticks_diff(time.ticks_cpu(), time.ticks_cpu()) < 16384)