Skip to content

Commit 40edefb

Browse files
decsnykartben
authored andcommitted
drivers: mcux_os_timer: Optimize counter timeout set
We can both optimize and clean up the code by using short circuit logic and static variables instead of stack variables. Signed-off-by: Declan Snyder <[email protected]>
1 parent 513b164 commit 40edefb

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

drivers/timer/mcux_os_timer.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ void mcux_lpc_ostick_isr(const void *arg)
9494
}
9595

9696
#if DT_NODE_HAS_STATUS_OKAY(DT_NODELABEL(standby)) && CONFIG_PM
97+
98+
static struct counter_top_cfg top_cfg = {0};
99+
static struct counter_alarm_cfg alarm_cfg = {0};
100+
97101
/* The OS Timer is disabled in certain low power modes and cannot wakeup the system
98102
* on timeout. This function will be called by the low power code to allow the
99103
* OS Timer to save off the count if needed and also start a wakeup counter
@@ -102,7 +106,6 @@ void mcux_lpc_ostick_isr(const void *arg)
102106
static uint32_t mcux_lpc_ostick_set_counter_timeout(int32_t curr_timeout)
103107
{
104108
uint32_t ticks;
105-
struct counter_top_cfg top_cfg = {0};
106109

107110
if (counter_dev == NULL) {
108111
return 1;
@@ -125,21 +128,11 @@ static uint32_t mcux_lpc_ostick_set_counter_timeout(int32_t curr_timeout)
125128
ticks = CLAMP(ticks, 1, counter_max_val);
126129

127130
top_cfg.ticks = ticks;
128-
top_cfg.callback = NULL;
129-
top_cfg.user_data = NULL;
130-
top_cfg.flags = 0;
131-
if (counter_set_top_value(counter_dev, &top_cfg) != 0) {
132-
/* Setting top value failed, try setting an alarm */
133-
struct counter_alarm_cfg alarm_cfg;
134-
135-
alarm_cfg.ticks = ticks;
136-
alarm_cfg.callback = NULL;
137-
alarm_cfg.user_data = NULL;
138-
alarm_cfg.flags = 0;
139-
140-
if (counter_set_channel_alarm(counter_dev, 0, &alarm_cfg) != 0) {
141-
return 1;
142-
}
131+
alarm_cfg.ticks = ticks;
132+
/* short circuit conditional logic, if top value doesn't work, we try alarm */
133+
if (counter_set_top_value(counter_dev, &top_cfg) != 0 &&
134+
counter_set_channel_alarm(counter_dev, 0, &alarm_cfg) != 0) {
135+
return 1;
143136
}
144137

145138
/* Counter is set to wakeup the system after the requested time */

0 commit comments

Comments
 (0)