summaryrefslogtreecommitdiffstatshomepage
path: root/stmhal/modpyb.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-08-25 17:36:14 +0100
committerDamien George <damien.p.george@gmail.com>2014-08-25 17:38:55 +0100
commit29c92a407c8fa04a205f10c4efdb5696e0cf0b58 (patch)
tree5cb842957202c012732af682f7982031d45acd24 /stmhal/modpyb.c
parent2bf044442eae7dbdaff91051d2c135b4aa51f1b2 (diff)
downloadmicropython-29c92a407c8fa04a205f10c4efdb5696e0cf0b58.tar.gz
micropython-29c92a407c8fa04a205f10c4efdb5696e0cf0b58.zip
stmhal: Use MP_OBJ_NEW_SMALL_INT directly in pyb.micros/millis.
Also some whitespace cleanup.
Diffstat (limited to 'stmhal/modpyb.c')
-rw-r--r--stmhal/modpyb.c18
1 files changed, 8 insertions, 10 deletions
diff --git a/stmhal/modpyb.c b/stmhal/modpyb.c
index e1d6d98bba..09a8caf278 100644
--- a/stmhal/modpyb.c
+++ b/stmhal/modpyb.c
@@ -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);