Skip to content

Commit a6cf302

Browse files
committed
Force inline Timer::attach() to get rid of floating-point instruction
Calls to Timer::attach() are inlined in order not to use floating-point library functions calls given Timer::attach_us() expects an integer for the callback interval.
1 parent eb3dbcb commit a6cf302

File tree

5 files changed

+13
-38
lines changed

5 files changed

+13
-38
lines changed

TESTS/mbed_drivers/lp_ticker/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ void test_detach(void)
142142
{
143143
LowPowerTicker ticker;
144144
bool ret;
145-
const s_timestamp_t ticker_time_s = 1;
146-
const uint32_t wait_time_ms = 5000;
145+
const float ticker_time_s = 0.1f;
146+
const uint32_t wait_time_ms = 500;
147147
Semaphore sem(0, 1);
148148

149-
ticker.attach_s(callback(sem_release, &sem), ticker_time_s);
149+
ticker.attach(callback(sem_release, &sem), ticker_time_s);
150150

151151
sem.acquire();
152152

@@ -171,7 +171,7 @@ void test_attach_time(void)
171171

172172
gtimer.reset();
173173
gtimer.start();
174-
ticker.attach_s(callback(stop_gtimer_set_flag), MICROSECONDS_TO_SECONDS(DELAY_US));
174+
ticker.attach(callback(stop_gtimer_set_flag), ((float)DELAY_US) / 1000000.0f);
175175
while (!ticker_callback_flag);
176176
ticker.detach();
177177
const int time_diff = gtimer.read_us();

TESTS/mbed_drivers/ticker/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,11 @@ void test_detach(void)
264264
{
265265
Ticker ticker;
266266
bool ret;
267-
const s_timestamp_t ticker_time_s = 1;
268-
const uint32_t wait_time_ms = 5000;
267+
const float ticker_time_s = 0.1f;
268+
const uint32_t wait_time_ms = 500;
269269
Semaphore sem(0, 1);
270270

271-
ticker.attach_s(callback(sem_release, &sem), ticker_time_s);
271+
ticker.attach(callback(sem_release, &sem), ticker_time_s);
272272

273273
sem.acquire();
274274

@@ -293,7 +293,7 @@ void test_attach_time(void)
293293

294294
gtimer.reset();
295295
gtimer.start();
296-
ticker.attach_s(callback(stop_gtimer_set_flag), MICROSECONDS_TO_SECONDS(DELAY_US));
296+
ticker.attach(callback(stop_gtimer_set_flag), ((float)DELAY_US) / 1000000.0f);
297297
while (!ticker_callback_flag);
298298
ticker.detach();
299299
const int time_diff = gtimer.read_us();

drivers/Ticker.h

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,6 @@
2323
#include "platform/NonCopyable.h"
2424
#include "hal/lp_ticker_api.h"
2525

26-
/**
27-
* Number of microseconds in a second
28-
*/
29-
#define MICROSECONDS_IN_SECOND (us_timestamp_t)1000000
30-
31-
/**
32-
* Converts seconds to microseconds
33-
*/
34-
#define SECONDS_TO_MICROSECONDS(SECONDS) (us_timestamp_t)(MICROSECONDS_IN_SECOND * SECONDS)
35-
36-
/**
37-
* Converts microseconds to seconds
38-
*/
39-
#define MICROSECONDS_TO_SECONDS(MICROSECONDS) (s_timestamp_t)(MICROSECONDS / MICROSECONDS_IN_SECOND)
40-
4126
namespace mbed {
4227
/**
4328
* \addtogroup drivers_Ticker Ticker class
@@ -89,17 +74,12 @@ class Ticker : public TimerEvent, private NonCopyable<Ticker> {
8974
Ticker(const ticker_data_t *data);
9075

9176
/** Attach a function to be called by the Ticker, specifying the interval in seconds
92-
*
77+
* The method must be inlined to convert to not use floating-point operations
78+
* given attach_us() expects an integer value for the callback interval.
9379
* @param func pointer to the function to be called
9480
* @param t the time between calls in seconds
9581
*/
96-
void attach_s(Callback<void()> func, const s_timestamp_t t)
97-
{
98-
attach_us(func, SECONDS_TO_MICROSECONDS(t));
99-
}
100-
101-
MBED_DEPRECATED("This function has been deprecated, use attach_s(Callback<void()> func, const s_timestamp_t t)")
102-
void attach(Callback<void()> func, float t)
82+
MBED_FORCEINLINE void attach(Callback<void()> func, float t)
10383
{
10484
attach_us(func, t * 1000000.0f);
10585
}
@@ -119,7 +99,7 @@ class Ticker : public TimerEvent, private NonCopyable<Ticker> {
11999
"attach(callback(obj, method), t).")
120100
void attach(T *obj, M method, float t)
121101
{
122-
attach_s(callback(obj, method), (s_timestamp_t)t);
102+
attach(callback(obj, method), t);
123103
}
124104

125105
/** Attach a function to be called by the Ticker, specifying the interval in microseconds

features/nfc/source/nfc/NFCController.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ void NFCController::scheduler_process(bool hw_interrupt)
183183
// Process stack events
184184
uint32_t timeout = nfc_scheduler_iteration(_scheduler, hw_interrupt ? EVENT_HW_INTERRUPT : EVENT_NONE);
185185

186-
_timeout.attach_s(callback(this, &NFCController::on_timeout), timeout);
186+
_timeout.attach(callback(this, &NFCController::on_timeout), timeout);
187187
}
188188

189189
void NFCController::on_hw_interrupt()

hal/ticker_api.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ typedef uint32_t timestamp_t;
3838
*/
3939
typedef uint64_t us_timestamp_t;
4040

41-
/**
42-
* A second timestamp stored in a 64 bit integer.
43-
*/
44-
typedef uint64_t s_timestamp_t;
45-
4641
/** Ticker's event structure
4742
*/
4843
typedef struct ticker_event_s {

0 commit comments

Comments
 (0)