Skip to content

Commit 4e0d07d

Browse files
authored
Merge pull request #12897 from kjbracey-arm/tickeropt
Optimise mbed_ticker_api.c
2 parents 33a7e66 + c887290 commit 4e0d07d

File tree

7 files changed

+344
-133
lines changed

7 files changed

+344
-133
lines changed

hal/include/hal/lp_ticker_api.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ extern "C" {
4949
*
5050
* @see hal_lp_ticker_tests
5151
*
52+
* # Compile-time optimization macros
53+
*
54+
* To permit compile-time optimization, the following macros can be defined by a target's device.h:
55+
*
56+
* LP_TICKER_PERIOD_NUM, LP_TICKER_PERIOD_DEN: These denote the ratio (numerator, denominator)
57+
* of the ticker period to a microsecond. For example, a 64kHz ticker would have NUM = 125, DEN = 8;
58+
* a 4kHz ticker would have NUM = 250, DEN = 1; a 32.768kHz ticker would have NUM = 15625, DEN = 512.
59+
* Both numerator and denominator must be 32 bits or less. They do not need to be fully simplified,
60+
* so 32.768kHz could also be NUM = 1000000, DEN = 32768, but more simplification may be a minor
61+
* speed optimisation, as can matching numerator or denominator with US_TICKER.
62+
*
63+
* LP_TICKER_MASK: The value mask for the ticker - eg 0x07FFFFFF for a 27-bit ticker.
64+
*
65+
* If any are defined, all 3 must be defined, and the macros are checked for consistency with
66+
* lp_ticker_get_info by test ::lp_ticker_info_test.
67+
5268
* @{
5369
*/
5470

hal/include/hal/ticker_api.h

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,73 @@ typedef struct {
7070
bool runs_in_deep_sleep; /**< Whether ticker operates in deep sleep */
7171
} ticker_interface_t;
7272

73+
/* Optimizations to avoid run-time computation if custom ticker support is disabled and
74+
* there is exactly one of USTICKER or LPTICKER available, or if they have the same
75+
* parameter value(s).
76+
*/
77+
#define MBED_TICKER_JUST_US (!MBED_CONF_TARGET_CUSTOM_TICKERS && DEVICE_USTICKER && !DEVICE_LPTICKER)
78+
#define MBED_TICKER_JUST_LP (!MBED_CONF_TARGET_CUSTOM_TICKERS && DEVICE_LPTICKER && !DEVICE_USTICKER)
79+
80+
#if (MBED_TICKER_JUST_US && defined US_TICKER_PERIOD_NUM) || \
81+
(!MBED_CONF_TARGET_CUSTOM_TICKERS && defined US_TICKER_PERIOD_NUM && defined LP_TICKER_PERIOD_NUM && \
82+
US_TICKER_PERIOD_NUM == LP_TICKER_PERIOD_NUM)
83+
#define MBED_TICKER_CONSTANT_PERIOD_NUM US_TICKER_PERIOD_NUM
84+
#elif MBED_TICKER_JUST_LP && defined LP_TICKER_PERIOD_NUM
85+
#define MBED_TICKER_CONSTANT_PERIOD_NUM LP_TICKER_PERIOD_NUM
86+
#endif
87+
88+
#if (MBED_TICKER_JUST_US && defined US_TICKER_PERIOD_DEN) || \
89+
(!MBED_CONF_TARGET_CUSTOM_TICKERS && defined US_TICKER_PERIOD_DEN && defined LP_TICKER_PERIOD_DEN && \
90+
US_TICKER_PERIOD_DEN == LP_TICKER_PERIOD_DEN)
91+
#define MBED_TICKER_CONSTANT_PERIOD_DEN US_TICKER_PERIOD_DEN
92+
#elif MBED_TICKER_JUST_LP && defined LP_TICKER_PERIOD_DEN
93+
#define MBED_TICKER_CONSTANT_PERIOD_DEN LP_TICKER_PERIOD_DEN
94+
#endif
95+
96+
#if defined MBED_TICKER_CONSTANT_PERIOD_NUM && defined MBED_TICKER_CONSTANT_PERIOD_DEN
97+
#define MBED_TICKER_CONSTANT_PERIOD
98+
#endif
99+
100+
#if (MBED_TICKER_JUST_US && defined US_TICKER_MASK) || \
101+
(!MBED_CONF_TARGET_CUSTOM_TICKERS && defined US_TICKER_MASK && defined LP_TICKER_MASK && \
102+
US_TICKER_MASK == LP_TICKER_MASK)
103+
#define MBED_TICKER_CONSTANT_MASK US_TICKER_MASK
104+
#elif MBED_TICKER_JUST_LP && defined LP_TICKER_MASK
105+
#define MBED_TICKER_CONSTANT_MASK LP_TICKER_MASK
106+
#endif
107+
73108
/** Ticker's event queue structure
74109
*/
75110
typedef struct {
76111
ticker_event_handler event_handler; /**< Event handler */
77112
ticker_event_t *head; /**< A pointer to head */
78-
uint32_t frequency; /**< Frequency of the timer in Hz */
113+
#ifndef MBED_TICKER_CONSTANT_PERIOD_NUM
114+
uint32_t period_num; /**< Ratio of period to 1us, numerator */
115+
#endif
116+
#ifndef MBED_TICKER_CONSTANT_PERIOD_DEN
117+
uint32_t period_den; /**< Ratio of period to 1us, denominator */
118+
#endif
119+
#ifndef MBED_TICKER_CONSTANT_MASK
79120
uint32_t bitmask; /**< Mask to be applied to time values read */
80121
uint32_t max_delta; /**< Largest delta in ticks that can be used when scheduling */
122+
#endif
123+
#if !(defined MBED_TICKER_CONSTANT_PERIOD && defined MBED_TICKER_CONSTANT_MASK)
81124
uint64_t max_delta_us; /**< Largest delta in us that can be used when scheduling */
125+
#endif
82126
uint32_t tick_last_read; /**< Last tick read */
83-
uint64_t tick_remainder; /**< Ticks that have not been added to base_time */
127+
#if MBED_TICKER_CONSTANT_PERIOD_DEN != 1
128+
uint32_t tick_remainder; /**< Ticks that have not been added to base_time */
129+
#endif
84130
us_timestamp_t present_time; /**< Store the timestamp used for present time */
85131
bool initialized; /**< Indicate if the instance is initialized */
86132
bool dispatching; /**< The function ticker_irq_handler is dispatching */
87133
bool suspended; /**< Indicate if the instance is suspended */
88-
uint8_t frequency_shifts; /**< If frequency is a value of 2^n, this is n, otherwise 0 */
134+
#ifndef MBED_TICKER_CONSTANT_PERIOD_NUM
135+
int8_t period_num_shifts; /**< If numerator is a value of 2^n, this is n, otherwise -1 */
136+
#endif
137+
#ifndef MBED_TICKER_CONSTANT_PERIOD_DEN
138+
int8_t period_den_shifts; /**< If denominator is a value of 2^n, this is n, otherwise -1 */
139+
#endif
89140
} ticker_event_queue_t;
90141

91142
/** Ticker's data structure

hal/include/hal/us_ticker_api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ extern "C" {
5050
* US_TICKER_PERIOD_NUM, US_TICKER_PERIOD_DEN: These denote the ratio (numerator, denominator)
5151
* of the ticker period to a microsecond. For example, an 8MHz ticker would have NUM = 1, DEN = 8;
5252
* a 1MHz ticker would have NUM = 1, DEN = 1; a 250kHz ticker would have NUM = 4, DEN = 1.
53-
* Both numerator and denominator must be 16 bits or less.
53+
* Both numerator and denominator must be 16 bits or less, but need not be fully simplified.
5454
*
5555
* US_TICKER_MASK: The value mask for the ticker - eg 0x07FFFFFF for a 27-bit ticker.
5656
*

0 commit comments

Comments
 (0)