@@ -99,7 +99,7 @@ STATIC mp_obj_t pyb_info(uint n_args, const mp_obj_t *args) {
9999 // get and print clock speeds
100100 // SYSCLK=168MHz, HCLK=168MHz, PCLK1=42MHz, PCLK2=84MHz
101101 {
102- printf ("S=%lu\nH=%lu\nP1=%lu\nP2=%lu\n" ,
102+ printf ("S=%lu\nH=%lu\nP1=%lu\nP2=%lu\n" ,
103103 HAL_RCC_GetSysClockFreq (),
104104 HAL_RCC_GetHCLKFreq (),
105105 HAL_RCC_GetPCLK1Freq (),
@@ -187,11 +187,46 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_sync_obj, pyb_sync);
187187
188188/// \function millis()
189189/// Returns the number of milliseconds since the board was last reset.
190+ ///
191+ /// Note that this may return a negative number. This allows you to always
192+ /// do:
193+ /// start = pyb.millis()
194+ /// ...do some operation...
195+ /// elapsed = pyb.millis() - start
196+ ///
197+ /// and as long as the time of your operation is less than 24 days, you'll
198+ /// always get the right answer and not have to worry about whether pyb.millis()
199+ /// wraps around.
190200STATIC mp_obj_t pyb_millis (void ) {
191- return mp_obj_new_int (HAL_GetTick ());
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 );
192206}
193207STATIC MP_DEFINE_CONST_FUN_OBJ_0 (pyb_millis_obj , pyb_millis );
194208
209+ /// \function micros()
210+ /// Returns the number of microseconds since the board was last reset.
211+ ///
212+ /// Note that this may return a negative number. This allows you to always
213+ /// do:
214+ /// start = pyb.micros()
215+ /// ...do some operation...
216+ /// elapsed = pyb.micros() - start
217+ ///
218+ /// and as long as the time of your operation is less than 35 minutes, you'll
219+ /// always get the right answer and not have to worry about whether pyb.micros()
220+ /// wraps around.
221+ 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 );
227+ }
228+ STATIC MP_DEFINE_CONST_FUN_OBJ_0 (pyb_micros_obj , pyb_micros );
229+
195230/// \function delay(ms)
196231/// Delay for the given number of milliseconds.
197232STATIC mp_obj_t pyb_delay (mp_obj_t ms_in ) {
@@ -251,7 +286,7 @@ STATIC mp_obj_t pyb_stop(void) {
251286 /* Enter Stop Mode */
252287 PWR_EnterSTOPMode (PWR_Regulator_LowPower , PWR_STOPEntry_WFI );
253288
254- /* Configures system clock after wake-up from STOP: enable HSE, PLL and select
289+ /* Configures system clock after wake-up from STOP: enable HSE, PLL and select
255290 * PLL as system clock source (HSE and PLL are disabled in STOP mode) */
256291 SYSCLKConfig_STOP ();
257292
@@ -343,6 +378,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
343378 { MP_OBJ_NEW_QSTR (MP_QSTR_USB_VCP ), (mp_obj_t )& pyb_usb_vcp_type },
344379
345380 { MP_OBJ_NEW_QSTR (MP_QSTR_millis ), (mp_obj_t )& pyb_millis_obj },
381+ { MP_OBJ_NEW_QSTR (MP_QSTR_micros ), (mp_obj_t )& pyb_micros_obj },
346382 { MP_OBJ_NEW_QSTR (MP_QSTR_delay ), (mp_obj_t )& pyb_delay_obj },
347383 { MP_OBJ_NEW_QSTR (MP_QSTR_udelay ), (mp_obj_t )& pyb_udelay_obj },
348384 { MP_OBJ_NEW_QSTR (MP_QSTR_sync ), (mp_obj_t )& pyb_sync_obj },
0 commit comments