-
Notifications
You must be signed in to change notification settings - Fork 3k
STM32: Fix RTC test issue on targets using a 16-bit timer for us_ticker #7352
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
Changes from all commits
26cb388
f785c23
fcdd529
9523bcf
a838cb2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,12 +14,45 @@ | |
* limitations under the License. | ||
*/ | ||
#include "hal/us_ticker_api.h" | ||
#include "hal_tick.h" | ||
|
||
// Overwrite default HAL functions defined as "weak" | ||
|
||
// This variable is set to 1 at the of mbed_sdk_init function. | ||
// The ticker_read_us function must not be called until the mbed_sdk_init is terminated. | ||
extern int mbed_sdk_inited; | ||
|
||
#if TIM_MST_16BIT | ||
// Variables also reset in HAL_InitTick() | ||
uint32_t prev_time = 0; | ||
uint32_t elapsed_time = 0; | ||
#endif | ||
|
||
// 1 ms tick is required for ST HAL driver | ||
uint32_t HAL_GetTick() | ||
{ | ||
return ticker_read_us(get_us_ticker_data()) / 1000; // 1 ms tick is required for ST HAL | ||
#if TIM_MST_16BIT | ||
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); | ||
} | ||
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); | ||
} | ||
#else // 32-bit timer | ||
if (mbed_sdk_inited) { | ||
return (ticker_read_us(get_us_ticker_data()) / 1000); | ||
} | ||
else { | ||
return (us_ticker_read() / 1000); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the 16 bit timer this will overflow in ~66ms. If anything in the SDK init takes longer than this it may get stuck as HAL_GetTick will never count higher than this during init. As a potential solution you could accumulate elapsed time rather than reading it directly. Something like: uint32_t 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; Note - You'll need to make sure There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in commit f785c23 |
||
} | ||
#endif | ||
} | ||
|
||
void HAL_SuspendTick(void) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this really needed ?
What about 32b targets ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
commit 9523bcf do this only for 16bits timer