Skip to content

Commit f785c23

Browse files
committed
HAL_GetTick returns elapsed time
1 parent 26cb388 commit f785c23

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

targets/TARGET_STM/hal_tick_16b.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#if TIM_MST_16BIT
2020

2121
extern TIM_HandleTypeDef TimMasterHandle;
22+
extern uint32_t prev_time;
23+
extern uint32_t elapsed_time;
2224

2325
volatile uint32_t PreviousVal = 0;
2426

@@ -108,6 +110,10 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
108110
TIM_MST_DBGMCU_FREEZE;
109111
#endif
110112

113+
// Used by HAL_GetTick()
114+
prev_time = 0;
115+
elapsed_time = 0;
116+
111117
return HAL_OK;
112118
}
113119

targets/TARGET_STM/hal_tick_common.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,25 @@
2121
// The ticker_read_us function must not be called until the mbed_sdk_init is terminated.
2222
extern int mbed_sdk_inited;
2323

24+
// Variables also reset in HAL_InitTick()
25+
uint32_t prev_time = 0;
26+
uint32_t elapsed_time = 0;
27+
28+
// 1 ms tick is required for ST HAL driver
2429
uint32_t HAL_GetTick()
2530
{
26-
// 1 ms tick is required for ST HAL driver
31+
uint32_t new_time;
2732
if (mbed_sdk_inited) {
28-
return (ticker_read_us(get_us_ticker_data()) / 1000);
33+
// Apply the latest time recorded just before the sdk is inited
34+
new_time = ticker_read_us(get_us_ticker_data()) + prev_time;
35+
prev_time = 0; // Use this time only once
36+
return (new_time / 1000);
2937
}
3038
else {
31-
return (us_ticker_read() / 1000);
39+
new_time = us_ticker_read();
40+
elapsed_time += (new_time - prev_time) & 0xFFFF; // Only use the lower 16 bits
41+
prev_time = new_time;
42+
return (elapsed_time / 1000);
3243
}
3344
}
3445

0 commit comments

Comments
 (0)