Skip to content

Commit 114d60c

Browse files
c1728p90xc0170
authored andcommitted
Remove direct use of us ticker from platform
Update platform code to use the ticker common layer rather than using HAL us ticker directly. This both ensures that the underlying ticker is properly initialized and that the value read is in microseconds with full 32-bit range.
1 parent 8183749 commit 114d60c

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

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)