diff --git a/stmhal/modpyb.c b/stmhal/modpyb.c index de7eac402436ac12684d393e47cacf1592a73ae7..e1d6d98bba0f8350ecf118a40714dc31d625e334 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 }, diff --git a/stmhal/qstrdefsport.h b/stmhal/qstrdefsport.h index 53b385dbfa5e1d960473ea745b4e40626347bcc9..04f49276b54cf61a9d70cbb625af2b01be9d3a3f 100644 --- a/stmhal/qstrdefsport.h +++ b/stmhal/qstrdefsport.h @@ -67,6 +67,7 @@ Q(/flash/lib) Q(/sd) Q(/sd/lib) Q(millis) +Q(micros) // for file class Q(seek) diff --git a/stmhal/stm32f4xx_it.c b/stmhal/stm32f4xx_it.c index 5fa7f8289d83ae427306e2b655932a29dead8265..4bd13c05cba311b0796da2c9b5f2cca03b98a402 100644 --- a/stmhal/stm32f4xx_it.c +++ b/stmhal/stm32f4xx_it.c @@ -174,6 +174,11 @@ void PendSV_Handler(void) { */ void SysTick_Handler(void) { HAL_IncTick(); + + // Read the systick control regster. This has the side effect of clearing + // the COUNTFLAG bit, which makes the logic in sys_tick_get_microseconds + // work properly. + SysTick->CTRL; } /******************************************************************************/ diff --git a/stmhal/systick.c b/stmhal/systick.c index 196f1fbcae66136147e4c9c580d81b405fb41fb4..660b777fc05264e6e0e8bc1757a376b242161c12 100644 --- a/stmhal/systick.c +++ b/stmhal/systick.c @@ -27,6 +27,10 @@ #include <stm32f4xx_hal.h> #include "mpconfig.h" #include "misc.h" +#include "nlr.h" +#include "qstr.h" +#include "obj.h" +#include "irq.h" #include "systick.h" bool sys_tick_has_passed(uint32_t start_tick, uint32_t delay_ms) { @@ -41,3 +45,35 @@ void sys_tick_wait_at_least(uint32_t start_tick, uint32_t delay_ms) { __WFI(); // enter sleep mode, waiting for interrupt } } + +// The SysTick timer counts down at 168 MHz, so we can use that knowledge +// to grab a microsecond counter. +// +// We assume that HAL_GetTickis returns milliseconds. +uint32_t sys_tick_get_microseconds(void) { + mp_int_t enabled = disable_irq(); + uint32_t counter = SysTick->VAL; + uint32_t milliseconds = HAL_GetTick(); + uint32_t status = SysTick->CTRL; + enable_irq(enabled); + + // 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 & SysTick_CTRL_COUNTFLAG_Msk) && 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 = SysTick->LOAD; + 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); +} diff --git a/stmhal/systick.h b/stmhal/systick.h index 1e7f623355e7d5e077e4e6f3111d26a8206e8d0d..98045d16c4c532d63e5deb1da0315f2f8856d8a2 100644 --- a/stmhal/systick.h +++ b/stmhal/systick.h @@ -26,3 +26,4 @@ void sys_tick_wait_at_least(uint32_t stc, uint32_t delay_ms); bool sys_tick_has_passed(uint32_t stc, uint32_t delay_ms); +uint32_t sys_tick_get_microseconds(void);