22
22
23
23
#define CYC_PER_TICK ((uint32_t)((uint64_t)sys_clock_hw_cycles_per_sec() \
24
24
/ (uint64_t)CONFIG_SYS_CLOCK_TICKS_PER_SEC))
25
+ #define CYC_PER_US ((uint32_t)((uint64_t)sys_clock_hw_cycles_per_sec() \
26
+ / (uint64_t)USEC_PER_SEC))
25
27
#define MAX_CYC INT_MAX
26
28
#define MAX_TICKS ((MAX_CYC - CYC_PER_TICK) / CYC_PER_TICK)
27
29
#define MIN_DELAY 1000
@@ -37,7 +39,12 @@ static OSTIMER_Type *base;
37
39
*/
38
40
static uint64_t cyc_sys_compensated ;
39
41
#if DT_NODE_HAS_STATUS_OKAY (DT_NODELABEL (standby )) && CONFIG_PM
42
+ /* This is the counter device used when OS timer is not available in
43
+ * standby mode.
44
+ */
40
45
static const struct device * counter_dev ;
46
+ /* Indicates if the counter is running. */
47
+ bool counter_running ;
41
48
#endif
42
49
43
50
static uint64_t mcux_lpc_ostick_get_compensated_timer_value (void )
@@ -86,8 +93,7 @@ static uint32_t mcux_lpc_ostick_set_counter_timeout(int32_t curr_timeout)
86
93
uint32_t timeout ;
87
94
int32_t ticks ;
88
95
struct counter_top_cfg top_cfg = { 0 };
89
-
90
- timeout = k_ticks_to_us_ceil32 (curr_timeout );
96
+ timeout = k_ticks_to_us_near32 (curr_timeout );
91
97
92
98
ticks = counter_us_to_ticks (counter_dev , timeout );
93
99
ticks = CLAMP (ticks , 1 , counter_get_max_top_value (counter_dev ));
@@ -111,17 +117,18 @@ static uint32_t mcux_lpc_ostick_set_counter_timeout(int32_t curr_timeout)
111
117
}
112
118
}
113
119
120
+ /* Counter is set to wakeup the system after the requested time */
121
+ if (counter_start (counter_dev ) != 0 ) {
122
+ ret = 1 ;
123
+ goto done ;
124
+ }
125
+ counter_running = true;
114
126
#if CONFIG_MCUX_OS_TIMER_PM_POWERED_OFF
115
127
/* Capture the current timer value for cases where it loses its state
116
128
* in low power modes.
117
129
*/
118
130
cyc_sys_compensated += OSTIMER_GetCurrentTimerValue (base );
119
131
#endif
120
-
121
- /* Counter is set to wakeup the system after the requested time */
122
- if (counter_start (counter_dev ) != 0 ) {
123
- ret = 1 ;
124
- }
125
132
} else {
126
133
ret = 1 ;
127
134
}
@@ -137,34 +144,37 @@ static uint32_t mcux_lpc_ostick_set_counter_timeout(int32_t curr_timeout)
137
144
*/
138
145
static uint32_t mcux_lpc_ostick_compensate_system_timer (void )
139
146
{
140
- uint32_t ret = 0 ;
141
-
142
- if (counter_dev ) {
143
- uint32_t slept_time_ticks ;
144
- uint32_t slept_time_us ;
147
+ uint32_t slept_time_ticks ;
148
+ uint32_t slept_time_us ;
145
149
146
- counter_stop (counter_dev );
150
+ if (!counter_dev ) {
151
+ return 1 ;
152
+ }
147
153
148
- counter_get_value (counter_dev , & slept_time_ticks );
154
+ if (!counter_running ) {
155
+ return 0 ;
156
+ }
149
157
150
- if (!(counter_is_counting_up (counter_dev ))) {
151
- slept_time_ticks = counter_get_top_value (counter_dev ) - slept_time_ticks ;
152
- }
153
- slept_time_us = counter_ticks_to_us (counter_dev , slept_time_ticks );
154
- cyc_sys_compensated += (k_us_to_ticks_floor32 (slept_time_us ) * CYC_PER_TICK );
158
+ counter_stop (counter_dev );
159
+ counter_running = false;
160
+ counter_get_value (counter_dev , & slept_time_ticks );
155
161
162
+ if (!(counter_is_counting_up (counter_dev ))) {
163
+ slept_time_ticks = counter_get_top_value (counter_dev ) -
164
+ slept_time_ticks ;
165
+ }
166
+ slept_time_us = counter_ticks_to_us (counter_dev , slept_time_ticks );
167
+ cyc_sys_compensated += CYC_PER_US * slept_time_us ;
156
168
#if CONFIG_MCUX_OS_TIMER_PM_POWERED_OFF
157
- /* Reactivate os_timer for cases where it loses its state */
158
- OSTIMER_Init (base );
169
+ /* Reset the OS Timer to a known state */
170
+ RESET_PeripheralReset (kOSEVENT_TIMER_RST_SHIFT_RSTn );
171
+ /* Reactivate os_timer for cases where it loses its state */
172
+ OSTIMER_Init (base );
159
173
#endif
174
+ /* Announce the time slept to the kernel*/
175
+ mcux_lpc_ostick_isr (NULL );
160
176
161
- /* Announce the time slept to the kernel*/
162
- mcux_lpc_ostick_isr (NULL );
163
- } else {
164
- ret = 1 ;
165
- }
166
-
167
- return ret ;
177
+ return 0 ;
168
178
}
169
179
170
180
#endif
0 commit comments