Skip to content
Snippets Groups Projects
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
Branches
No related tags found
No related merge requests found
......@@ -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,11 +51,11 @@ 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
......
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment