Skip to content

Commit 29c92a4

Browse files
committed
stmhal: Use MP_OBJ_NEW_SMALL_INT directly in pyb.micros/millis.
Also some whitespace cleanup.
1 parent 2bf0444 commit 29c92a4

3 files changed

Lines changed: 32 additions & 39 deletions

File tree

stmhal/modpyb.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_sync_obj, pyb_sync);
198198
/// always get the right answer and not have to worry about whether pyb.millis()
199199
/// wraps around.
200200
STATIC mp_obj_t pyb_millis(void) {
201-
// We want to "cast" the 32 bit unsigned into a small-int. So we shift it
202-
// left by 1 to throw away the top bit, and then shift it right by one
203-
// to sign extend.
204-
mp_int_t val = HAL_GetTick() << 1;
205-
return mp_obj_new_int(val >> 1);
201+
// We want to "cast" the 32 bit unsigned into a small-int. This means
202+
// copying the MSB down 1 bit (extending the sign down), which is
203+
// equivalent to just using the MP_OBJ_NEW_SMALL_INT macro.
204+
return MP_OBJ_NEW_SMALL_INT(HAL_GetTick());
206205
}
207206
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis);
208207

@@ -219,11 +218,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis);
219218
/// always get the right answer and not have to worry about whether pyb.micros()
220219
/// wraps around.
221220
STATIC mp_obj_t pyb_micros(void) {
222-
// We want to "cast" the 32 bit unsigned into a small-int. So we shift it
223-
// left by 1 to throw away the top bit, and then shift it right by one
224-
// to sign extend.
225-
mp_int_t val = sys_tick_get_microseconds() << 1;
226-
return mp_obj_new_int(val >> 1);
221+
// We want to "cast" the 32 bit unsigned into a small-int. This means
222+
// copying the MSB down 1 bit (extending the sign down), which is
223+
// equivalent to just using the MP_OBJ_NEW_SMALL_INT macro.
224+
return MP_OBJ_NEW_SMALL_INT(sys_tick_get_microseconds());
227225
}
228226
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_micros_obj, pyb_micros);
229227

stmhal/systick.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,29 @@ void sys_tick_wait_at_least(uint32_t start_tick, uint32_t delay_ms) {
5151
//
5252
// We assume that HAL_GetTickis returns milliseconds.
5353
uint32_t sys_tick_get_microseconds(void) {
54-
mp_int_t enabled = disable_irq();
54+
mp_uint_t irq_state = disable_irq();
5555
uint32_t counter = SysTick->VAL;
5656
uint32_t milliseconds = HAL_GetTick();
5757
uint32_t status = SysTick->CTRL;
58-
enable_irq(enabled);
58+
enable_irq(irq_state);
5959

60-
// It's still possible for the countflag bit to get set if the counter was
61-
// reloaded between reading VAL and reading CTRL. With interrupts disabled
62-
// it definitely takes less than 50 HCLK cycles between reading VAL and
63-
// reading CTRL, so the test (counter > 50) is to cover the case where VAL
64-
// is +ve and very close to zero, and the COUNTFLAG bit is also set.
65-
if ((status & SysTick_CTRL_COUNTFLAG_Msk) && counter > 50) {
66-
// This means that the HW reloaded VAL between the time we read VAL and the
67-
// time we read CTRL, which implies that there is an interrupt pending
68-
// to increment the tick counter.
69-
milliseconds++;
70-
}
71-
uint32_t load = SysTick->LOAD;
72-
counter = load - counter; // Convert from decrementing to incrementing
60+
// It's still possible for the countflag bit to get set if the counter was
61+
// reloaded between reading VAL and reading CTRL. With interrupts disabled
62+
// it definitely takes less than 50 HCLK cycles between reading VAL and
63+
// reading CTRL, so the test (counter > 50) is to cover the case where VAL
64+
// is +ve and very close to zero, and the COUNTFLAG bit is also set.
65+
if ((status & SysTick_CTRL_COUNTFLAG_Msk) && counter > 50) {
66+
// This means that the HW reloaded VAL between the time we read VAL and the
67+
// time we read CTRL, which implies that there is an interrupt pending
68+
// to increment the tick counter.
69+
milliseconds++;
70+
}
71+
uint32_t load = SysTick->LOAD;
72+
counter = load - counter; // Convert from decrementing to incrementing
7373

74-
// ((load + 1) / 1000) is the number of counts per microsecond.
75-
//
76-
// counter / ((load + 1) / 1000) scales from the systick clock to microseconds
77-
// and is the same thing as (counter * 1000) / (load + 1)
78-
return milliseconds * 1000 + (counter * 1000) / (load + 1);
74+
// ((load + 1) / 1000) is the number of counts per microsecond.
75+
//
76+
// counter / ((load + 1) / 1000) scales from the systick clock to microseconds
77+
// and is the same thing as (counter * 1000) / (load + 1)
78+
return milliseconds * 1000 + (counter * 1000) / (load + 1);
7979
}

teensy/teensy_hal.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
void assert_failed(uint8_t* file, uint32_t line);
66
#else
77
#define assert_param(expr) ((void)0)
8-
#endif /* USE_FULL_ASSERT */
8+
#endif /* USE_FULL_ASSERT */
99

1010
#define FTM0 ((FTM_TypeDef *)&FTM0_SC)
1111
#define FTM1 ((FTM_TypeDef *)&FTM1_SC)
@@ -113,25 +113,20 @@ typedef struct {
113113
#define GPIO_AF6_I2C1 6
114114
#define GPIO_AF7_FTM1 7
115115

116-
117-
__attribute__(( always_inline )) static inline void __WFI(void)
118-
{
116+
__attribute__(( always_inline )) static inline void __WFI(void) {
119117
__asm volatile ("wfi");
120118
}
121119

122-
__attribute__(( always_inline )) static inline uint32_t __get_PRIMASK(void)
123-
{
120+
__attribute__(( always_inline )) static inline uint32_t __get_PRIMASK(void) {
124121
uint32_t result;
125122
__asm volatile ("MRS %0, primask" : "=r" (result));
126123
return(result);
127124
}
128125

129-
__attribute__(( always_inline )) static inline void __set_PRIMASK(uint32_t priMask)
130-
{
126+
__attribute__(( always_inline )) static inline void __set_PRIMASK(uint32_t priMask) {
131127
__asm volatile ("MSR primask, %0" : : "r" (priMask) : "memory");
132128
}
133129

134-
135130
uint32_t HAL_GetTick(void);
136131
void HAL_Delay(uint32_t Delay);
137132

0 commit comments

Comments
 (0)