Skip to content

Commit cd8e9e6

Browse files
Merge pull request #5194 from c1728p9/fix_sleep
Ensure us_ticker is initialized before it is used
2 parents cc0b3d0 + 114d60c commit cd8e9e6

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

hal/mbed_ticker_api.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ timestamp_t ticker_read(const ticker_data_t *const ticker)
263263

264264
us_timestamp_t ticker_read_us(const ticker_data_t *const ticker)
265265
{
266+
initialize(ticker);
266267
update_present_time(ticker);
267268
return ticker->queue->present_time;
268269
}

platform/mbed_retarget.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,9 +1049,8 @@ void operator delete[](void *ptr)
10491049
extern "C" clock_t clock()
10501050
{
10511051
_mutex->lock();
1052-
clock_t t = us_ticker_read();
1052+
clock_t t = ticker_read(get_us_ticker_data());
10531053
t /= 1000000 / CLOCKS_PER_SEC; // convert to processor time
10541054
_mutex->unlock();
10551055
return t;
10561056
}
1057-

platform/mbed_wait_api_no_rtos.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ void wait_ms(int ms) {
3030
}
3131

3232
void wait_us(int us) {
33-
uint32_t start = us_ticker_read();
34-
while ((us_ticker_read() - start) < (uint32_t)us);
33+
const ticker_data_t *const ticker = get_us_ticker_data();
34+
uint32_t start = ticker_read(ticker);
35+
while ((ticker_read(ticker) - start) < (uint32_t)us);
3536
}
3637

3738
#endif // #ifndef MBED_CONF_RTOS_PRESENT

platform/mbed_wait_api_rtos.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "hal/us_ticker_api.h"
2323
#include "rtos/rtos.h"
2424
#include "platform/mbed_critical.h"
25+
#include "platform/mbed_sleep.h"
2526

2627
void wait(float s) {
2728
wait_us(s * 1000000.0f);
@@ -32,15 +33,19 @@ void wait_ms(int ms) {
3233
}
3334

3435
void wait_us(int us) {
35-
uint32_t start = us_ticker_read();
36+
const ticker_data_t *const ticker = get_us_ticker_data();
37+
38+
uint32_t start = ticker_read(ticker);
3639
// Use the RTOS to wait for millisecond delays if possible
3740
int ms = us / 1000;
3841
if ((ms > 0) && core_util_are_interrupts_enabled()) {
42+
sleep_manager_lock_deep_sleep();
3943
Thread::wait((uint32_t)ms);
44+
sleep_manager_unlock_deep_sleep();
4045
}
4146
// Use busy waiting for sub-millisecond delays, or for the whole
4247
// interval if interrupts are not enabled
43-
while ((us_ticker_read() - start) < (uint32_t)us);
48+
while ((ticker_read(ticker) - start) < (uint32_t)us);
4449
}
4550

4651
#endif // #if MBED_CONF_RTOS_PRESENT

0 commit comments

Comments
 (0)