Skip to content

Optimise HAL_GetTick API #12915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions targets/TARGET_STM/hal_tick_overrides.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ extern int mbed_sdk_inited;
void init_16bit_timer(void);
void init_32bit_timer(void);

#if TIM_MST_BIT_WIDTH == 16
// Variables also reset in us_ticker_init()
uint32_t prev_time = 0;
uint32_t elapsed_time = 0;
#if TIM_MST_BIT_WIDTH <= 16
static uint16_t prev_time;
#else
static uint32_t prev_time;
#endif
static uint32_t total_ticks;
static uint16_t prev_tick_remainder;

// Overwrite default HAL functions defined as "weak"

Expand All @@ -47,26 +49,24 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)

uint32_t HAL_GetTick()
{
#if TIM_MST_BIT_WIDTH == 16
uint32_t new_time;
if (mbed_sdk_inited) {
// Apply the latest time recorded just before the sdk is inited
new_time = ticker_read_us(get_us_ticker_data()) + prev_time;
prev_time = 0; // Use this time only once
return (new_time / 1000);
uint32_t new_time = us_ticker_read();
uint32_t elapsed_time = (((new_time - prev_time) & US_TICKER_MASK) + prev_tick_remainder);
prev_time = new_time;
uint32_t elapsed_ticks;
// Speed optimisation for small time intervals, avoiding a potentially-slow C library divide.
if (TIM_MST_BIT_WIDTH <= 16 || elapsed_time < 65536) {
elapsed_ticks = 0;
while (elapsed_time >= 1000) {
elapsed_ticks++;
elapsed_time -= 1000;
}
prev_tick_remainder = elapsed_time;
} else {
new_time = us_ticker_read();
elapsed_time += (new_time - prev_time) & 0xFFFF; // Only use the lower 16 bits
prev_time = new_time;
return (elapsed_time / 1000);
elapsed_ticks = elapsed_time / 1000;
prev_tick_remainder = elapsed_time % 1000;
}
#else // 32-bit timer
if (mbed_sdk_inited) {
return (ticker_read_us(get_us_ticker_data()) / 1000);
} else {
return (us_ticker_read() / 1000);
}
#endif
total_ticks += elapsed_ticks;
return total_ticks;
}

void HAL_SuspendTick(void)
Expand Down
7 changes: 0 additions & 7 deletions targets/TARGET_STM/us_ticker.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ void us_ticker_irq_handler(void);
// ************************************ 16-bit timer ************************************
#if TIM_MST_BIT_WIDTH == 16

extern uint32_t prev_time;
extern uint32_t elapsed_time;

#if defined(TARGET_STM32F0)
void timer_update_irq_handler(void)
{
Expand Down Expand Up @@ -128,10 +125,6 @@ void init_16bit_timer(void)
#endif

__HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);

// Used by HAL_GetTick()
prev_time = 0;
elapsed_time = 0;
}

// ************************************ 32-bit timer ************************************
Expand Down