Commit 29c92a40 authored by Damien George's avatar Damien George
Browse files

stmhal: Use MP_OBJ_NEW_SMALL_INT directly in pyb.micros/millis.

Also some whitespace cleanup.
parent 2bf04444
......@@ -198,11 +198,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_sync_obj, pyb_sync);
/// always get the right answer and not have to worry about whether pyb.millis()
/// wraps around.
STATIC mp_obj_t pyb_millis(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 = HAL_GetTick() << 1;
return mp_obj_new_int(val >> 1);
// 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(HAL_GetTick());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis);
......@@ -219,11 +218,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis);
/// 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);
// 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(sys_tick_get_microseconds());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_micros_obj, pyb_micros);
......
......@@ -51,29 +51,29 @@ void sys_tick_wait_at_least(uint32_t start_tick, uint32_t delay_ms) {
//
// We assume that HAL_GetTickis returns milliseconds.
uint32_t sys_tick_get_microseconds(void) {
mp_int_t enabled = disable_irq();
mp_uint_t irq_state = disable_irq();
uint32_t counter = SysTick->VAL;
uint32_t milliseconds = HAL_GetTick();
uint32_t status = SysTick->CTRL;
enable_irq(enabled);
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 & 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
// 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);
// ((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);
}
......@@ -5,7 +5,7 @@
void assert_failed(uint8_t* file, uint32_t line);
#else
#define assert_param(expr) ((void)0)
#endif /* USE_FULL_ASSERT */
#endif /* USE_FULL_ASSERT */
#define FTM0 ((FTM_TypeDef *)&FTM0_SC)
#define FTM1 ((FTM_TypeDef *)&FTM1_SC)
......@@ -113,25 +113,20 @@ typedef struct {
#define GPIO_AF6_I2C1 6
#define GPIO_AF7_FTM1 7
__attribute__(( always_inline )) static inline void __WFI(void)
{
__attribute__(( always_inline )) static inline void __WFI(void) {
__asm volatile ("wfi");
}
__attribute__(( always_inline )) static inline uint32_t __get_PRIMASK(void)
{
__attribute__(( always_inline )) static inline uint32_t __get_PRIMASK(void) {
uint32_t result;
__asm volatile ("MRS %0, primask" : "=r" (result));
return(result);
}
__attribute__(( always_inline )) static inline void __set_PRIMASK(uint32_t priMask)
{
__attribute__(( always_inline )) static inline void __set_PRIMASK(uint32_t priMask) {
__asm volatile ("MSR primask, %0" : : "r" (priMask) : "memory");
}
uint32_t HAL_GetTick(void);
void HAL_Delay(uint32_t Delay);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment