summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--stmhal/modpyb.c42
-rw-r--r--stmhal/qstrdefsport.h1
-rw-r--r--stmhal/stm32f4xx_it.c5
-rw-r--r--stmhal/systick.c36
-rw-r--r--stmhal/systick.h1
5 files changed, 82 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 },
diff --git a/stmhal/qstrdefsport.h b/stmhal/qstrdefsport.h
index 53b385dbfa..04f49276b5 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 5fa7f8289d..4bd13c05cb 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 196f1fbcae..660b777fc0 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 1e7f623355..98045d16c4 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);