3232#include MICROPY_HAL_H
3333#include "py/nlr.h"
3434#include "py/obj.h"
35+ #include "py/smallint.h"
3536#include "timeutils.h"
3637#include "inc/hw_types.h"
3738#include "inc/hw_ints.h"
@@ -152,23 +153,23 @@ STATIC mp_obj_t time_ticks_ms(void) {
152153 // We want to "cast" the 32 bit unsigned into a small-int. This means
153154 // copying the MSB down 1 bit (extending the sign down), which is
154155 // equivalent to just using the MP_OBJ_NEW_SMALL_INT macro.
155- return MP_OBJ_NEW_SMALL_INT (HAL_GetTick ());
156+ return MP_OBJ_NEW_SMALL_INT (HAL_GetTick () & MP_SMALL_INT_POSITIVE_MASK );
156157}
157158STATIC MP_DEFINE_CONST_FUN_OBJ_0 (time_ticks_ms_obj , time_ticks_ms );
158159
159160STATIC mp_obj_t time_ticks_us (void ) {
160161 // We want to "cast" the 32 bit unsigned into a small-int. This means
161162 // copying the MSB down 1 bit (extending the sign down), which is
162163 // equivalent to just using the MP_OBJ_NEW_SMALL_INT macro.
163- return MP_OBJ_NEW_SMALL_INT (sys_tick_get_microseconds ());
164+ return MP_OBJ_NEW_SMALL_INT (sys_tick_get_microseconds () & MP_SMALL_INT_POSITIVE_MASK );
164165}
165166STATIC MP_DEFINE_CONST_FUN_OBJ_0 (time_ticks_us_obj , time_ticks_us );
166167
167168STATIC mp_obj_t time_ticks_cpu (void ) {
168169 // We want to "cast" the 32 bit unsigned into a small-int. This means
169170 // copying the MSB down 1 bit (extending the sign down), which is
170171 // equivalent to just using the MP_OBJ_NEW_SMALL_INT macro.
171- return MP_OBJ_NEW_SMALL_INT (SysTickValueGet ());
172+ return MP_OBJ_NEW_SMALL_INT (SysTickValueGet () & MP_SMALL_INT_POSITIVE_MASK );
172173}
173174STATIC MP_DEFINE_CONST_FUN_OBJ_0 (time_ticks_cpu_obj , time_ticks_cpu );
174175
@@ -178,7 +179,7 @@ STATIC mp_obj_t time_ticks_diff(mp_obj_t t0, mp_obj_t t1) {
178179 // equivalent to just using the MP_OBJ_NEW_SMALL_INT macro.
179180 uint32_t start = mp_obj_get_int (t0 );
180181 uint32_t end = mp_obj_get_int (t1 );
181- return MP_OBJ_NEW_SMALL_INT ((end > start ) ? ( end - start ) : ( start - end ) );
182+ return MP_OBJ_NEW_SMALL_INT ((end - start ) & MP_SMALL_INT_POSITIVE_MASK );
182183}
183184STATIC MP_DEFINE_CONST_FUN_OBJ_2 (time_ticks_diff_obj , time_ticks_diff );
184185
0 commit comments