Skip to content

Optimise mbed_ticker_api.c #12897

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 4 commits into from
Nov 30, 2020
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
16 changes: 16 additions & 0 deletions hal/include/hal/lp_ticker_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ extern "C" {
*
* @see hal_lp_ticker_tests
*
* # Compile-time optimization macros
*
* To permit compile-time optimization, the following macros can be defined by a target's device.h:
*
* LP_TICKER_PERIOD_NUM, LP_TICKER_PERIOD_DEN: These denote the ratio (numerator, denominator)
* of the ticker period to a microsecond. For example, a 64kHz ticker would have NUM = 125, DEN = 8;
* a 4kHz ticker would have NUM = 250, DEN = 1; a 32.768kHz ticker would have NUM = 15625, DEN = 512.
* Both numerator and denominator must be 32 bits or less. They do not need to be fully simplified,
* so 32.768kHz could also be NUM = 1000000, DEN = 32768, but more simplification may be a minor
* speed optimisation, as can matching numerator or denominator with US_TICKER.
*
* LP_TICKER_MASK: The value mask for the ticker - eg 0x07FFFFFF for a 27-bit ticker.
*
* If any are defined, all 3 must be defined, and the macros are checked for consistency with
* lp_ticker_get_info by test ::lp_ticker_info_test.

* @{
*/

Expand Down
57 changes: 54 additions & 3 deletions hal/include/hal/ticker_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,73 @@ typedef struct {
bool runs_in_deep_sleep; /**< Whether ticker operates in deep sleep */
} ticker_interface_t;

/* Optimizations to avoid run-time computation if custom ticker support is disabled and
* there is exactly one of USTICKER or LPTICKER available, or if they have the same
* parameter value(s).
*/
#define MBED_TICKER_JUST_US (!MBED_CONF_TARGET_CUSTOM_TICKERS && DEVICE_USTICKER && !DEVICE_LPTICKER)
#define MBED_TICKER_JUST_LP (!MBED_CONF_TARGET_CUSTOM_TICKERS && DEVICE_LPTICKER && !DEVICE_USTICKER)

#if (MBED_TICKER_JUST_US && defined US_TICKER_PERIOD_NUM) || \
(!MBED_CONF_TARGET_CUSTOM_TICKERS && defined US_TICKER_PERIOD_NUM && defined LP_TICKER_PERIOD_NUM && \
US_TICKER_PERIOD_NUM == LP_TICKER_PERIOD_NUM)
#define MBED_TICKER_CONSTANT_PERIOD_NUM US_TICKER_PERIOD_NUM
#elif MBED_TICKER_JUST_LP && defined LP_TICKER_PERIOD_NUM
#define MBED_TICKER_CONSTANT_PERIOD_NUM LP_TICKER_PERIOD_NUM
#endif

#if (MBED_TICKER_JUST_US && defined US_TICKER_PERIOD_DEN) || \
(!MBED_CONF_TARGET_CUSTOM_TICKERS && defined US_TICKER_PERIOD_DEN && defined LP_TICKER_PERIOD_DEN && \
US_TICKER_PERIOD_DEN == LP_TICKER_PERIOD_DEN)
#define MBED_TICKER_CONSTANT_PERIOD_DEN US_TICKER_PERIOD_DEN
#elif MBED_TICKER_JUST_LP && defined LP_TICKER_PERIOD_DEN
#define MBED_TICKER_CONSTANT_PERIOD_DEN LP_TICKER_PERIOD_DEN
#endif

#if defined MBED_TICKER_CONSTANT_PERIOD_NUM && defined MBED_TICKER_CONSTANT_PERIOD_DEN
#define MBED_TICKER_CONSTANT_PERIOD
#endif

#if (MBED_TICKER_JUST_US && defined US_TICKER_MASK) || \
(!MBED_CONF_TARGET_CUSTOM_TICKERS && defined US_TICKER_MASK && defined LP_TICKER_MASK && \
US_TICKER_MASK == LP_TICKER_MASK)
#define MBED_TICKER_CONSTANT_MASK US_TICKER_MASK
#elif MBED_TICKER_JUST_LP && defined LP_TICKER_MASK
#define MBED_TICKER_CONSTANT_MASK LP_TICKER_MASK
#endif

/** Ticker's event queue structure
*/
typedef struct {
ticker_event_handler event_handler; /**< Event handler */
ticker_event_t *head; /**< A pointer to head */
uint32_t frequency; /**< Frequency of the timer in Hz */
#ifndef MBED_TICKER_CONSTANT_PERIOD_NUM
uint32_t period_num; /**< Ratio of period to 1us, numerator */
#endif
#ifndef MBED_TICKER_CONSTANT_PERIOD_DEN
uint32_t period_den; /**< Ratio of period to 1us, denominator */
#endif
#ifndef MBED_TICKER_CONSTANT_MASK
uint32_t bitmask; /**< Mask to be applied to time values read */
uint32_t max_delta; /**< Largest delta in ticks that can be used when scheduling */
#endif
#if !(defined MBED_TICKER_CONSTANT_PERIOD && defined MBED_TICKER_CONSTANT_MASK)
uint64_t max_delta_us; /**< Largest delta in us that can be used when scheduling */
#endif
uint32_t tick_last_read; /**< Last tick read */
uint64_t tick_remainder; /**< Ticks that have not been added to base_time */
#if MBED_TICKER_CONSTANT_PERIOD_DEN != 1
uint32_t tick_remainder; /**< Ticks that have not been added to base_time */
#endif
us_timestamp_t present_time; /**< Store the timestamp used for present time */
bool initialized; /**< Indicate if the instance is initialized */
bool dispatching; /**< The function ticker_irq_handler is dispatching */
bool suspended; /**< Indicate if the instance is suspended */
uint8_t frequency_shifts; /**< If frequency is a value of 2^n, this is n, otherwise 0 */
#ifndef MBED_TICKER_CONSTANT_PERIOD_NUM
int8_t period_num_shifts; /**< If numerator is a value of 2^n, this is n, otherwise -1 */
#endif
#ifndef MBED_TICKER_CONSTANT_PERIOD_DEN
int8_t period_den_shifts; /**< If denominator is a value of 2^n, this is n, otherwise -1 */
#endif
} ticker_event_queue_t;

/** Ticker's data structure
Expand Down
2 changes: 1 addition & 1 deletion hal/include/hal/us_ticker_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ extern "C" {
* US_TICKER_PERIOD_NUM, US_TICKER_PERIOD_DEN: These denote the ratio (numerator, denominator)
* of the ticker period to a microsecond. For example, an 8MHz ticker would have NUM = 1, DEN = 8;
* a 1MHz ticker would have NUM = 1, DEN = 1; a 250kHz ticker would have NUM = 4, DEN = 1.
* Both numerator and denominator must be 16 bits or less.
* Both numerator and denominator must be 16 bits or less, but need not be fully simplified.
*
* US_TICKER_MASK: The value mask for the ticker - eg 0x07FFFFFF for a 27-bit ticker.
*
Expand Down
Loading