@@ -150,33 +150,25 @@ STATIC mp_obj_t time_sleep_us (mp_obj_t usec_in) {
150150STATIC MP_DEFINE_CONST_FUN_OBJ_1 (time_sleep_us_obj , time_sleep_us );
151151
152152STATIC mp_obj_t time_ticks_ms (void ) {
153- // We want to "cast" the 32 bit unsigned into a small-int. This means
154- // copying the MSB down 1 bit (extending the sign down), which is
155- // equivalent to just using the MP_OBJ_NEW_SMALL_INT macro.
153+ // We want to "cast" the 32 bit unsigned into a 30-bit small-int
156154 return MP_OBJ_NEW_SMALL_INT (HAL_GetTick () & MP_SMALL_INT_POSITIVE_MASK );
157155}
158156STATIC MP_DEFINE_CONST_FUN_OBJ_0 (time_ticks_ms_obj , time_ticks_ms );
159157
160158STATIC mp_obj_t time_ticks_us (void ) {
161- // We want to "cast" the 32 bit unsigned into a small-int. This means
162- // copying the MSB down 1 bit (extending the sign down), which is
163- // equivalent to just using the MP_OBJ_NEW_SMALL_INT macro.
159+ // We want to "cast" the 32 bit unsigned into a 30-bit small-int
164160 return MP_OBJ_NEW_SMALL_INT (sys_tick_get_microseconds () & MP_SMALL_INT_POSITIVE_MASK );
165161}
166162STATIC MP_DEFINE_CONST_FUN_OBJ_0 (time_ticks_us_obj , time_ticks_us );
167163
168164STATIC mp_obj_t time_ticks_cpu (void ) {
169- // We want to "cast" the 32 bit unsigned into a small-int. This means
170- // copying the MSB down 1 bit (extending the sign down), which is
171- // equivalent to just using the MP_OBJ_NEW_SMALL_INT macro.
172- return MP_OBJ_NEW_SMALL_INT (SysTickValueGet () & MP_SMALL_INT_POSITIVE_MASK );
165+ // We want to "cast" the 32 bit unsigned into a 30-bit small-int
166+ return MP_OBJ_NEW_SMALL_INT ((SysTickPeriodGet () - SysTickValueGet ()) & MP_SMALL_INT_POSITIVE_MASK );
173167}
174168STATIC MP_DEFINE_CONST_FUN_OBJ_0 (time_ticks_cpu_obj , time_ticks_cpu );
175169
176170STATIC mp_obj_t time_ticks_diff (mp_obj_t t0 , mp_obj_t t1 ) {
177- // We want to "cast" the 32 bit unsigned into a small-int. This means
178- // copying the MSB down 1 bit (extending the sign down), which is
179- // equivalent to just using the MP_OBJ_NEW_SMALL_INT macro.
171+ // We want to "cast" the 32 bit unsigned into a 30-bit small-int
180172 uint32_t start = mp_obj_get_int (t0 );
181173 uint32_t end = mp_obj_get_int (t1 );
182174 return MP_OBJ_NEW_SMALL_INT ((end - start ) & MP_SMALL_INT_POSITIVE_MASK );
0 commit comments