Skip to content

Commit 398515a

Browse files
authored
Merge pull request #11236 from hugueskamba/hk-iotcore-1315-remove-floating-point-ticker
Force inline Timer::attach() to get rid of floating-point instructions
2 parents 181f4f7 + 3373d78 commit 398515a

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

TESTS/mbed_hal/rtc/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static const uint32_t TOLERANCE_ACCURACY_US = (DELAY_10S *US_PER_SEC / ACCURACY_
4242
#if DEVICE_LPTICKER
4343
volatile bool expired;
4444

45-
void callback(void)
45+
void set_flag_true(void)
4646
{
4747
expired = true;
4848
}
@@ -72,7 +72,7 @@ void rtc_sleep_test_support(bool deepsleep_mode)
7272

7373
rtc_write(start);
7474

75-
timeout.attach(callback, DELAY_4S);
75+
timeout.attach(set_flag_true, DELAY_4S);
7676

7777
TEST_ASSERT(sleep_manager_can_deep_sleep_test_check() == deepsleep_mode);
7878

drivers/Ticker.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef MBED_TICKER_H
1818
#define MBED_TICKER_H
1919

20+
#include <mstd_utility>
2021
#include "drivers/TimerEvent.h"
2122
#include "platform/Callback.h"
2223
#include "platform/mbed_toolchain.h"
@@ -75,12 +76,17 @@ class Ticker : public TimerEvent, private NonCopyable<Ticker> {
7576

7677
/** Attach a function to be called by the Ticker, specifying the interval in seconds
7778
*
79+
* The method forwards its arguments to attach_us() rather than copying them which
80+
* may not be trivial depending on the callback copied.
81+
* The function is forcibly inlined to not use floating-point operations. This is
82+
* possible given attach_us() expects an integer value for the callback interval.
7883
* @param func pointer to the function to be called
7984
* @param t the time between calls in seconds
8085
*/
81-
void attach(Callback<void()> func, float t)
86+
template <typename F>
87+
MBED_FORCEINLINE void attach(F &&func, float t)
8288
{
83-
attach_us(func, t * 1000000.0f);
89+
attach_us(std::forward<F>(func), t * 1000000.0f);
8490
}
8591

8692
/** Attach a member function to be called by the Ticker, specifying the interval in seconds

0 commit comments

Comments
 (0)