Skip to content

Force inline Timer::attach() to get rid of floating-point instructions #11236

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions TESTS/mbed_hal/rtc/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static const uint32_t TOLERANCE_ACCURACY_US = (DELAY_10S *US_PER_SEC / ACCURACY_
#if DEVICE_LPTICKER
volatile bool expired;

void callback(void)
void set_flag_true(void)
{
expired = true;
}
Expand Down Expand Up @@ -72,7 +72,7 @@ void rtc_sleep_test_support(bool deepsleep_mode)

rtc_write(start);

timeout.attach(callback, DELAY_4S);
timeout.attach(set_flag_true, DELAY_4S);

TEST_ASSERT(sleep_manager_can_deep_sleep_test_check() == deepsleep_mode);

Expand Down
10 changes: 8 additions & 2 deletions drivers/Ticker.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef MBED_TICKER_H
#define MBED_TICKER_H

#include <mstd_utility>
#include "drivers/TimerEvent.h"
#include "platform/Callback.h"
#include "platform/mbed_toolchain.h"
Expand Down Expand Up @@ -75,12 +76,17 @@ class Ticker : public TimerEvent, private NonCopyable<Ticker> {

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

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