@@ -27,11 +27,13 @@ extern int mbed_sdk_inited;
27
27
void init_16bit_timer (void );
28
28
void init_32bit_timer (void );
29
29
30
- #if TIM_MST_BIT_WIDTH = = 16
31
- // Variables also reset in us_ticker_init()
32
- uint32_t prev_time = 0 ;
33
- uint32_t elapsed_time = 0 ;
30
+ #if TIM_MST_BIT_WIDTH < = 16
31
+ static uint16_t prev_time ;
32
+ #else
33
+ static uint32_t prev_time ;
34
34
#endif
35
+ static uint32_t total_ticks ;
36
+ static uint16_t prev_tick_remainder ;
35
37
36
38
// Overwrite default HAL functions defined as "weak"
37
39
@@ -47,26 +49,24 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
47
49
48
50
uint32_t HAL_GetTick ()
49
51
{
50
- #if TIM_MST_BIT_WIDTH == 16
51
- uint32_t new_time ;
52
- if (mbed_sdk_inited ) {
53
- // Apply the latest time recorded just before the sdk is inited
54
- new_time = ticker_read_us (get_us_ticker_data ()) + prev_time ;
55
- prev_time = 0 ; // Use this time only once
56
- return (new_time / 1000 );
52
+ uint32_t new_time = us_ticker_read ();
53
+ uint32_t elapsed_time = (((new_time - prev_time ) & US_TICKER_MASK ) + prev_tick_remainder );
54
+ prev_time = new_time ;
55
+ uint32_t elapsed_ticks ;
56
+ // Speed optimisation for small time intervals, avoiding a potentially-slow C library divide.
57
+ if (TIM_MST_BIT_WIDTH <= 16 || elapsed_time < 65536 ) {
58
+ elapsed_ticks = 0 ;
59
+ while (elapsed_time >= 1000 ) {
60
+ elapsed_ticks ++ ;
61
+ elapsed_time -= 1000 ;
62
+ }
63
+ prev_tick_remainder = elapsed_time ;
57
64
} else {
58
- new_time = us_ticker_read ();
59
- elapsed_time += (new_time - prev_time ) & 0xFFFF ; // Only use the lower 16 bits
60
- prev_time = new_time ;
61
- return (elapsed_time / 1000 );
65
+ elapsed_ticks = elapsed_time / 1000 ;
66
+ prev_tick_remainder = elapsed_time % 1000 ;
62
67
}
63
- #else // 32-bit timer
64
- if (mbed_sdk_inited ) {
65
- return (ticker_read_us (get_us_ticker_data ()) / 1000 );
66
- } else {
67
- return (us_ticker_read () / 1000 );
68
- }
69
- #endif
68
+ total_ticks += elapsed_ticks ;
69
+ return total_ticks ;
70
70
}
71
71
72
72
void HAL_SuspendTick (void )
0 commit comments