1 /*
2  * Copyright (c) 2018, Nordic Semiconductor ASA
3  * Copyright 2024, 2025 NXP
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <zephyr/drivers/counter.h>
9 #include <zephyr/ztest.h>
10 #include <zephyr/kernel.h>
11 #include <zephyr/logging/log.h>
12 LOG_MODULE_REGISTER(test);
13 
14 static struct k_sem top_cnt_sem;
15 static volatile uint32_t top_cnt;
16 static struct k_sem alarm_cnt_sem;
17 static volatile uint32_t alarm_cnt;
18 
19 static void top_handler(const struct device *dev, void *user_data);
20 
21 void *exp_user_data = (void *)199;
22 
23 struct counter_alarm_cfg cntr_alarm_cfg;
24 struct counter_alarm_cfg cntr_alarm_cfg2;
25 
26 #define DEVICE_DT_GET_AND_COMMA(node_id) DEVICE_DT_GET(node_id),
27 /* Generate a list of devices for all instances of the "compat" */
28 #define DEVS_FOR_DT_COMPAT(compat) \
29 	DT_FOREACH_STATUS_OKAY(compat, DEVICE_DT_GET_AND_COMMA)
30 
31 static const struct device *const devices[] = {
32 #ifdef CONFIG_COUNTER_NRF_TIMER
33 	DEVS_FOR_DT_COMPAT(nordic_nrf_timer)
34 #endif
35 #ifdef CONFIG_COUNTER_NRF_RTC
36 	DEVS_FOR_DT_COMPAT(nordic_nrf_rtc)
37 #endif
38 #ifdef CONFIG_COUNTER_TIMER_MAX32
39 #define ADI_MAX32_COUNTER_DEV(idx) \
40 	DEVICE_DT_GET(DT_INST(idx, adi_max32_counter)),
41 #define DT_DRV_COMPAT adi_max32_counter
42 	DT_INST_FOREACH_STATUS_OKAY(ADI_MAX32_COUNTER_DEV)
43 #undef DT_DRV_COMPAT
44 #undef ADI_MAX32_COUNTER_DEV
45 #endif
46 #ifdef CONFIG_COUNTER_TIMER_STM32
47 #define STM32_COUNTER_DEV(idx) \
48 	DEVICE_DT_GET(DT_INST(idx, st_stm32_counter)),
49 #define DT_DRV_COMPAT st_stm32_counter
50 	DT_INST_FOREACH_STATUS_OKAY(STM32_COUNTER_DEV)
51 #undef DT_DRV_COMPAT
52 #undef STM32_COUNTER_DEV
53 #endif
54 #ifdef CONFIG_COUNTER_NATIVE_SIM
55 	DEVICE_DT_GET(DT_NODELABEL(counter0)),
56 #endif
57 #ifdef CONFIG_COUNTER_INFINEON_CAT1
58 	DEVICE_DT_GET(DT_NODELABEL(counter0_0)),
59 #endif
60 	/* NOTE: there is no trailing comma, as the DEVS_FOR_DT_COMPAT
61 	 * handles it.
62 	 */
63 	DEVS_FOR_DT_COMPAT(arm_cmsdk_timer)
64 	DEVS_FOR_DT_COMPAT(arm_cmsdk_dtimer)
65 	DEVS_FOR_DT_COMPAT(microchip_xec_timer)
66 	DEVS_FOR_DT_COMPAT(nxp_imx_epit)
67 	DEVS_FOR_DT_COMPAT(nxp_imx_gpt)
68 #ifdef CONFIG_COUNTER_MCUX_TPM
69 	DEVS_FOR_DT_COMPAT(nxp_tpm_timer)
70 #endif
71 	DEVS_FOR_DT_COMPAT(renesas_smartbond_timer)
72 #ifdef CONFIG_COUNTER_MCUX_CTIMER
73 	DEVS_FOR_DT_COMPAT(nxp_lpc_ctimer)
74 #endif
75 #ifdef CONFIG_COUNTER_MCUX_RTC
76 	DEVS_FOR_DT_COMPAT(nxp_rtc)
77 #endif
78 #ifdef CONFIG_COUNTER_MCUX_RTC_JDP
79 	DEVS_FOR_DT_COMPAT(nxp_rtc_jdp)
80 #endif
81 #ifdef CONFIG_COUNTER_MCUX_QTMR
82 	DEVS_FOR_DT_COMPAT(nxp_imx_tmr)
83 #endif
84 #ifdef CONFIG_COUNTER_NXP_MRT
85 	DEVS_FOR_DT_COMPAT(nxp_mrt_channel)
86 #endif
87 #ifdef CONFIG_COUNTER_MCUX_LPC_RTC_1HZ
88 	DEVS_FOR_DT_COMPAT(nxp_lpc_rtc)
89 #endif
90 #ifdef CONFIG_TEST_DRIVER_COUNTER_MCUX_LPC_RTC_HIGHRES
91 	DEVS_FOR_DT_COMPAT(nxp_lpc_rtc_highres)
92 #endif
93 #ifdef CONFIG_COUNTER_GECKO_RTCC
94 	DEVS_FOR_DT_COMPAT(silabs_gecko_rtcc)
95 #endif
96 #ifdef CONFIG_COUNTER_RTC_STM32
97 	DEVS_FOR_DT_COMPAT(st_stm32_rtc)
98 #endif
99 #ifdef CONFIG_COUNTER_GECKO_STIMER
100 	DEVICE_DT_GET(DT_CHOSEN(silabs_sleeptimer)),
101 #endif
102 #ifdef CONFIG_COUNTER_NXP_PIT
103 	DEVS_FOR_DT_COMPAT(nxp_pit_channel)
104 #endif
105 #ifdef CONFIG_COUNTER_XLNX_AXI_TIMER
106 	DEVS_FOR_DT_COMPAT(xlnx_xps_timer_1_00_a)
107 #endif
108 #ifdef CONFIG_COUNTER_TMR_ESP32
109 	DEVS_FOR_DT_COMPAT(espressif_esp32_counter)
110 #endif
111 #ifdef CONFIG_COUNTER_RTC_ESP32
112 	DEVS_FOR_DT_COMPAT(espressif_esp32_rtc_timer)
113 #endif
114 #ifdef CONFIG_COUNTER_NXP_S32_SYS_TIMER
115 	DEVS_FOR_DT_COMPAT(nxp_s32_sys_timer)
116 #endif
117 #ifdef CONFIG_COUNTER_TIMER_GD32
118 	DEVS_FOR_DT_COMPAT(gd_gd32_timer)
119 #endif
120 #ifdef CONFIG_COUNTER_TIMER_RPI_PICO
121 	DEVS_FOR_DT_COMPAT(raspberrypi_pico_timer)
122 #endif
123 #ifdef CONFIG_COUNTER_RTC_MAX32
124 	DEVS_FOR_DT_COMPAT(adi_max32_rtc_counter)
125 #endif
126 #ifdef CONFIG_COUNTER_AMBIQ
127 	DEVS_FOR_DT_COMPAT(ambiq_counter)
128 #endif
129 #ifdef CONFIG_COUNTER_MCUX_LPTMR
130 	DEVS_FOR_DT_COMPAT(nxp_lptmr)
131 #endif
132 #ifdef CONFIG_COUNTER_MCUX_LPIT
133 	DEVS_FOR_DT_COMPAT(nxp_lpit_channel)
134 #endif
135 #ifdef CONFIG_COUNTER_MCUX_FTM
136 	DEVS_FOR_DT_COMPAT(nxp_ftm)
137 #endif
138 #ifdef CONFIG_COUNTER_MCUX_STM
139 	DEVS_FOR_DT_COMPAT(nxp_stm)
140 #endif
141 #ifdef CONFIG_COUNTER_RENESAS_RZ_GTM
142 	DEVS_FOR_DT_COMPAT(renesas_rz_gtm_counter)
143 #endif
144 #ifdef CONFIG_COUNTER_ITE_IT51XXX
145 	DEVS_FOR_DT_COMPAT(ite_it51xxx_counter)
146 #endif
147 #ifdef CONFIG_COUNTER_ITE_IT8XXX2
148 	DEVS_FOR_DT_COMPAT(ite_it8xxx2_counter)
149 #endif
150 #ifdef CONFIG_COUNTER_NEORV32_GPTMR
151 	DEVS_FOR_DT_COMPAT(neorv32_gptmr)
152 #endif
153 #ifdef CONFIG_COUNTER_RA_AGT
154 	DEVS_FOR_DT_COMPAT(renesas_ra_agt_counter)
155 #endif
156 #ifdef CONFIG_COUNTER_RENESAS_RZ_CMTW
157 	DEVS_FOR_DT_COMPAT(renesas_rz_cmtw_counter)
158 #endif
159 #ifdef CONFIG_COUNTER_INFINEON_TCPWM
160 	DEVS_FOR_DT_COMPAT(infineon_tcpwm_counter)
161 #endif
162 };
163 
164 static const struct device *const period_devs[] = {
165 #ifdef CONFIG_COUNTER_MCUX_RTC
166 	DEVS_FOR_DT_COMPAT(nxp_rtc)
167 #endif
168 #ifdef CONFIG_COUNTER_MCUX_RTC_JDP
169 	DEVS_FOR_DT_COMPAT(nxp_rtc_jdp)
170 #endif
171 #ifdef CONFIG_COUNTER_MCUX_LPC_RTC
172 	DEVS_FOR_DT_COMPAT(nxp_lpc_rtc)
173 #endif
174 #ifdef CONFIG_COUNTER_RTC_STM32
175 	DEVS_FOR_DT_COMPAT(st_stm32_rtc)
176 #endif
177 #ifdef CONFIG_COUNTER_RTC_MAX32
178 	DEVS_FOR_DT_COMPAT(adi_max32_rtc_counter)
179 #endif
180 };
181 
182 typedef void (*counter_test_func_t)(const struct device *dev);
183 typedef bool (*counter_capability_func_t)(const struct device *dev);
184 
get_counter_period_us(const struct device * dev)185 static inline uint32_t get_counter_period_us(const struct device *dev)
186 {
187 	for (int i = 0; i < ARRAY_SIZE(period_devs); i++) {
188 		if (period_devs[i] == dev) {
189 			return (USEC_PER_SEC * 2U);
190 		}
191 	}
192 
193 	/* if more counter drivers exist other than RTC,
194 	 * the test value set to 20000 by default
195 	 */
196 	return 20000;
197 }
198 
counter_setup_instance(const struct device * dev)199 static void counter_setup_instance(const struct device *dev)
200 {
201 	k_sem_reset(&alarm_cnt_sem);
202 	if (!k_is_user_context()) {
203 		alarm_cnt = 0;
204 	}
205 }
206 
counter_tear_down_instance(const struct device * dev)207 static void counter_tear_down_instance(const struct device *dev)
208 {
209 	int err;
210 	struct counter_top_cfg top_cfg = {
211 		.callback = NULL,
212 		.user_data = NULL,
213 		.flags = 0
214 	};
215 
216 	top_cfg.ticks = counter_get_max_top_value(dev);
217 	err = counter_set_top_value(dev, &top_cfg);
218 	if (err == -ENOTSUP) {
219 		/* If resetting is not support, attempt without reset. */
220 		top_cfg.flags = COUNTER_TOP_CFG_DONT_RESET;
221 		err = counter_set_top_value(dev, &top_cfg);
222 
223 	}
224 	zassert_true((err == 0) || (err == -ENOTSUP),
225 			"%s: Setting top value to default failed", dev->name);
226 
227 	err = counter_stop(dev);
228 	zassert_equal(0, err, "%s: Counter failed to stop", dev->name);
229 
230 }
231 
test_all_instances(counter_test_func_t func,counter_capability_func_t capability_check)232 static void test_all_instances(counter_test_func_t func,
233 				counter_capability_func_t capability_check)
234 {
235 	int devices_skipped = 0;
236 
237 	zassert_true(ARRAY_SIZE(devices) > 0, "No device found");
238 	for (int i = 0; i < ARRAY_SIZE(devices); i++) {
239 		counter_setup_instance(devices[i]);
240 		if ((capability_check == NULL) ||
241 		     capability_check(devices[i])) {
242 			TC_PRINT("Testing %s\n", devices[i]->name);
243 			func(devices[i]);
244 		} else {
245 			TC_PRINT("Skipped for %s\n", devices[i]->name);
246 			devices_skipped++;
247 		}
248 		counter_tear_down_instance(devices[i]);
249 		/* Allow logs to be printed. */
250 		k_sleep(K_MSEC(100));
251 	}
252 	if (devices_skipped == ARRAY_SIZE(devices)) {
253 		ztest_test_skip();
254 	}
255 }
256 
set_top_value_capable(const struct device * dev)257 static bool set_top_value_capable(const struct device *dev)
258 {
259 	struct counter_top_cfg cfg = {
260 		.ticks = counter_get_top_value(dev) - 1
261 	};
262 	int err;
263 
264 	err = counter_set_top_value(dev, &cfg);
265 	if (err == -ENOTSUP) {
266 		return false;
267 	}
268 
269 	cfg.ticks++;
270 	err = counter_set_top_value(dev, &cfg);
271 	if (err == -ENOTSUP) {
272 		return false;
273 	}
274 
275 	return true;
276 }
277 
top_handler(const struct device * dev,void * user_data)278 static void top_handler(const struct device *dev, void *user_data)
279 {
280 	zassert_true(user_data == exp_user_data,
281 			"%s: Unexpected callback", dev->name);
282 	if (IS_ENABLED(CONFIG_ZERO_LATENCY_IRQS)) {
283 		top_cnt++;
284 
285 		return;
286 	}
287 
288 	k_sem_give(&top_cnt_sem);
289 }
290 
test_set_top_value_with_alarm_instance(const struct device * dev)291 static void test_set_top_value_with_alarm_instance(const struct device *dev)
292 {
293 	int err;
294 	uint32_t cnt;
295 	uint32_t top_value;
296 	uint32_t counter_period_us;
297 	uint32_t top_handler_cnt;
298 	struct counter_top_cfg top_cfg = {
299 		.callback = top_handler,
300 		.user_data = exp_user_data,
301 		.flags = 0
302 	};
303 
304 	k_sem_reset(&top_cnt_sem);
305 	top_cnt = 0;
306 
307 	counter_period_us = get_counter_period_us(dev);
308 	top_cfg.ticks = counter_us_to_ticks(dev, counter_period_us);
309 	err = counter_start(dev);
310 	zassert_equal(0, err, "%s: Counter failed to start", dev->name);
311 
312 	k_busy_wait(5000);
313 
314 	err = counter_get_value(dev, &cnt);
315 	zassert_true(err == 0, "%s: Counter read failed (err: %d)", dev->name,
316 		     err);
317 	if (counter_is_counting_up(dev)) {
318 		err = (cnt > 0) ? 0 : 1;
319 	} else {
320 		top_value = counter_get_top_value(dev);
321 		err = (cnt < top_value) ? 0 : 1;
322 	}
323 	zassert_true(err == 0, "%s: Counter should progress", dev->name);
324 
325 	err = counter_set_top_value(dev, &top_cfg);
326 	zassert_equal(0, err, "%s: Counter failed to set top value (err: %d)",
327 			dev->name, err);
328 
329 	k_busy_wait(5.2*counter_period_us);
330 
331 	top_handler_cnt = IS_ENABLED(CONFIG_ZERO_LATENCY_IRQS) ?
332 		top_cnt : k_sem_count_get(&top_cnt_sem);
333 	zassert_true(top_handler_cnt == 5U,
334 			"%s: Unexpected number of turnarounds (%d).",
335 			dev->name, top_handler_cnt);
336 }
337 
ZTEST(counter_basic,test_set_top_value_with_alarm)338 ZTEST(counter_basic, test_set_top_value_with_alarm)
339 {
340 	test_all_instances(test_set_top_value_with_alarm_instance,
341 			   set_top_value_capable);
342 }
343 
test_set_top_value_without_alarm_instance(const struct device * dev)344 static void test_set_top_value_without_alarm_instance(const struct device *dev)
345 {
346 	int err;
347 	uint32_t cnt;
348 	uint32_t top_value;
349 	uint32_t counter_period_us;
350 	struct counter_top_cfg top_cfg = {
351 		.callback = NULL,
352 		.user_data = NULL,
353 		.flags = 0
354 	};
355 
356 	counter_period_us = get_counter_period_us(dev);
357 	top_cfg.ticks = counter_us_to_ticks(dev, counter_period_us);
358 	err = counter_start(dev);
359 	zassert_equal(0, err, "%s: Counter failed to start", dev->name);
360 
361 	k_busy_wait(5000);
362 
363 	err = counter_get_value(dev, &cnt);
364 	zassert_true(err == 0, "%s: Counter read failed (err: %d)", dev->name,
365 		     err);
366 	if (counter_is_counting_up(dev)) {
367 		err = (cnt > 0) ? 0 : 1;
368 	} else {
369 		top_value = counter_get_top_value(dev);
370 		err = (cnt < top_value) ? 0 : 1;
371 	}
372 	zassert_true(err == 0, "%s: Counter should progress", dev->name);
373 
374 	err = counter_set_top_value(dev, &top_cfg);
375 	zassert_equal(0, err, "%s: Counter failed to set top value (err: %d)",
376 			dev->name, err);
377 
378 	zassert_true(counter_get_top_value(dev) == top_cfg.ticks,
379 			"%s: new top value not in use.",
380 			dev->name);
381 }
382 
ZTEST_USER(counter_no_callback,test_set_top_value_without_alarm)383 ZTEST_USER(counter_no_callback, test_set_top_value_without_alarm)
384 {
385 	test_all_instances(test_set_top_value_without_alarm_instance,
386 			   set_top_value_capable);
387 }
388 
alarm_handler(const struct device * dev,uint8_t chan_id,uint32_t counter,void * user_data)389 static void alarm_handler(const struct device *dev, uint8_t chan_id,
390 			  uint32_t counter,
391 			  void *user_data)
392 {
393 	/* Arbitrary limit for alarm processing - time between hw expiration
394 	 * and read-out from counter in the handler.
395 	 */
396 	static const uint64_t processing_limit_us = 1000;
397 	uint32_t now;
398 	int err;
399 	uint32_t top;
400 	uint32_t diff;
401 
402 	err = counter_get_value(dev, &now);
403 	zassert_true(err == 0, "%s: Counter read failed (err: %d)",
404 		     dev->name, err);
405 
406 	top = counter_get_top_value(dev);
407 	if (counter_is_counting_up(dev)) {
408 		diff =  (now < counter) ?
409 			(now + top - counter) : (now - counter);
410 	} else {
411 		diff = (now > counter) ?
412 			(counter + top - now) : (counter - now);
413 	}
414 
415 	zassert_true(diff <= counter_us_to_ticks(dev, processing_limit_us),
416 			"Unexpected distance between reported alarm value(%u) "
417 			"and actual counter value (%u), top:%d (processing "
418 			"time limit (%d us) might be exceeded?",
419 			counter, now, top, (int)processing_limit_us);
420 
421 	if (user_data) {
422 		zassert_true(&cntr_alarm_cfg == user_data,
423 			"%s: Unexpected callback", dev->name);
424 	}
425 
426 	if (IS_ENABLED(CONFIG_ZERO_LATENCY_IRQS)) {
427 		alarm_cnt++;
428 		return;
429 	}
430 	zassert_true(k_is_in_isr(), "%s: Expected interrupt context",
431 			dev->name);
432 	k_sem_give(&alarm_cnt_sem);
433 }
434 
alarm_capable_handler(const struct device * dev,uint8_t chan_id,uint32_t counter,void * user_data)435 static void alarm_capable_handler(const struct device *dev, uint8_t chan_id,
436 				    uint32_t counter,
437 				    void *user_data)
438 {
439 	ARG_UNUSED(dev);
440 	ARG_UNUSED(chan_id);
441 	ARG_UNUSED(counter);
442 	ARG_UNUSED(user_data);
443 
444 	/* Intentionally empty - capability probe only */
445 }
446 
447 /* Any non-zero error (including -ENOTSUP) is treated as "not supported",
448  * and only returns true when there are no errors throughout the entire process.
449  */
alarm_capable(const struct device * dev)450 static bool alarm_capable(const struct device *dev)
451 {
452 	struct counter_alarm_cfg cfg = {
453 		.flags = 0U,
454 		.ticks = counter_us_to_ticks(dev, 1000U),
455 		.callback = alarm_capable_handler,
456 		.user_data = NULL,
457 	};
458 
459 	/* Avoid zero-tick requests on very low-frequency counters */
460 	if (cfg.ticks == 0U) {
461 		cfg.ticks = 1U;
462 	}
463 
464 	int err;
465 
466 	if (counter_get_num_of_channels(dev) < 1U) {
467 		return false;
468 	}
469 
470 	err = counter_start(dev);
471 	if (err != 0) {
472 		return false;
473 	}
474 
475 	err = counter_set_channel_alarm(dev, 0, &cfg);
476 	if (err != 0) {
477 		goto out_stop;
478 	}
479 
480 	err = counter_cancel_channel_alarm(dev, 0);
481 	if (err != 0) {
482 		goto out_stop;
483 	}
484 
485 out_stop:
486 	(void)counter_stop(dev);
487 
488 	return err == 0;
489 }
490 
test_single_shot_alarm_instance(const struct device * dev,bool set_top)491 static void test_single_shot_alarm_instance(const struct device *dev, bool set_top)
492 {
493 	int err;
494 	uint32_t ticks;
495 	uint32_t cnt;
496 	uint32_t counter_period_us;
497 	struct counter_top_cfg top_cfg = {
498 		.callback = top_handler,
499 		.user_data = exp_user_data,
500 		.flags = 0
501 	};
502 
503 	counter_period_us = get_counter_period_us(dev);
504 	ticks = counter_us_to_ticks(dev, counter_period_us);
505 	top_cfg.ticks = ticks;
506 
507 	cntr_alarm_cfg.flags = 0;
508 	cntr_alarm_cfg.callback = alarm_handler;
509 	cntr_alarm_cfg.user_data = &cntr_alarm_cfg;
510 
511 	k_sem_reset(&alarm_cnt_sem);
512 	alarm_cnt = 0;
513 
514 	if (counter_get_num_of_channels(dev) < 1U) {
515 		/* Counter does not support any alarm */
516 		return;
517 	}
518 
519 	err = counter_start(dev);
520 	zassert_equal(0, err, "%s: Counter failed to start", dev->name);
521 
522 	if (set_top) {
523 		err = counter_set_top_value(dev, &top_cfg);
524 
525 		zassert_equal(0, err,
526 			     "%s: Counter failed to set top value", dev->name);
527 
528 		cntr_alarm_cfg.ticks = ticks + 1;
529 		err = counter_set_channel_alarm(dev, 0, &cntr_alarm_cfg);
530 		zassert_equal(-EINVAL, err,
531 			      "%s: Counter should return error because ticks"
532 			      " exceeded the limit set alarm", dev->name);
533 	}
534 
535 	cntr_alarm_cfg.ticks = ticks;
536 	err = counter_set_channel_alarm(dev, 0, &cntr_alarm_cfg);
537 	zassert_equal(0, err, "%s: Counter set alarm failed (err: %d)",
538 			dev->name, err);
539 
540 	k_busy_wait(2*(uint32_t)counter_ticks_to_us(dev, ticks));
541 
542 	cnt = IS_ENABLED(CONFIG_ZERO_LATENCY_IRQS) ?
543 		alarm_cnt : k_sem_count_get(&alarm_cnt_sem);
544 	zassert_equal(1, cnt, "%s: Expecting alarm callback", dev->name);
545 
546 	k_busy_wait(1.5*counter_ticks_to_us(dev, ticks));
547 	cnt = IS_ENABLED(CONFIG_ZERO_LATENCY_IRQS) ?
548 		alarm_cnt : k_sem_count_get(&alarm_cnt_sem);
549 	zassert_equal(1, cnt, "%s: Expecting alarm callback", dev->name);
550 
551 	err = counter_cancel_channel_alarm(dev, 0);
552 	zassert_equal(0, err, "%s: Counter disabling alarm failed", dev->name);
553 
554 	top_cfg.ticks = counter_get_max_top_value(dev);
555 	top_cfg.callback = NULL;
556 	top_cfg.user_data = NULL;
557 	err = counter_set_top_value(dev, &top_cfg);
558 	if (err == -ENOTSUP) {
559 		/* If resetting is not support, attempt without reset. */
560 		top_cfg.flags = COUNTER_TOP_CFG_DONT_RESET;
561 		err = counter_set_top_value(dev, &top_cfg);
562 
563 	}
564 	zassert_true((err == 0) || (err == -ENOTSUP),
565 			"%s: Setting top value to default failed", dev->name);
566 
567 	err = counter_stop(dev);
568 	zassert_equal(0, err, "%s: Counter failed to stop", dev->name);
569 }
570 
test_single_shot_alarm_notop_instance(const struct device * dev)571 void test_single_shot_alarm_notop_instance(const struct device *dev)
572 {
573 	test_single_shot_alarm_instance(dev, false);
574 }
575 
test_single_shot_alarm_top_instance(const struct device * dev)576 void test_single_shot_alarm_top_instance(const struct device *dev)
577 {
578 	test_single_shot_alarm_instance(dev, true);
579 }
580 
single_channel_alarm_capable(const struct device * dev)581 static bool single_channel_alarm_capable(const struct device *dev)
582 {
583 	return alarm_capable(dev);
584 }
585 
single_channel_alarm_and_custom_top_capable(const struct device * dev)586 static bool single_channel_alarm_and_custom_top_capable(const struct device *dev)
587 {
588 	return alarm_capable(dev) && set_top_value_capable(dev);
589 }
590 
ZTEST(counter_basic,test_single_shot_alarm_notop)591 ZTEST(counter_basic, test_single_shot_alarm_notop)
592 {
593 	test_all_instances(test_single_shot_alarm_notop_instance,
594 			   single_channel_alarm_capable);
595 }
596 
ZTEST(counter_basic,test_single_shot_alarm_top)597 ZTEST(counter_basic, test_single_shot_alarm_top)
598 {
599 	test_all_instances(test_single_shot_alarm_top_instance,
600 			   single_channel_alarm_and_custom_top_capable);
601 }
602 
603 static void *clbk_data[10];
604 
alarm_handler2(const struct device * dev,uint8_t chan_id,uint32_t counter,void * user_data)605 static void alarm_handler2(const struct device *dev, uint8_t chan_id,
606 			   uint32_t counter,
607 			   void *user_data)
608 {
609 	if (IS_ENABLED(CONFIG_ZERO_LATENCY_IRQS)) {
610 		clbk_data[alarm_cnt] = user_data;
611 		alarm_cnt++;
612 
613 		return;
614 	}
615 
616 	clbk_data[k_sem_count_get(&alarm_cnt_sem)] = user_data;
617 	k_sem_give(&alarm_cnt_sem);
618 }
619 
620 /*
621  * Two alarms set. First alarm is absolute, second relative. Because
622  * setting of both alarms is delayed it is expected that second alarm
623  * will expire first (relative to the time called) while first alarm
624  * will expire after next wrap around.
625  */
test_multiple_alarms_instance(const struct device * dev)626 static void test_multiple_alarms_instance(const struct device *dev)
627 {
628 	int err;
629 	uint32_t ticks;
630 	uint32_t cnt;
631 	uint32_t counter_period_us;
632 	struct counter_top_cfg top_cfg = {
633 		.callback = top_handler,
634 		.user_data = exp_user_data,
635 		.flags = 0
636 	};
637 
638 	counter_period_us = get_counter_period_us(dev);
639 	ticks = counter_us_to_ticks(dev, counter_period_us);
640 
641 	err = counter_get_value(dev, &(top_cfg.ticks));
642 	zassert_equal(0, err, "%s: Counter get value failed", dev->name);
643 	top_cfg.ticks += ticks;
644 
645 	cntr_alarm_cfg.flags = COUNTER_ALARM_CFG_ABSOLUTE;
646 	cntr_alarm_cfg.ticks = counter_us_to_ticks(dev, 2000);
647 	cntr_alarm_cfg.callback = alarm_handler2;
648 	cntr_alarm_cfg.user_data = &cntr_alarm_cfg;
649 
650 	cntr_alarm_cfg2.flags = 0;
651 	cntr_alarm_cfg2.ticks = counter_us_to_ticks(dev, 2000);
652 	cntr_alarm_cfg2.callback = alarm_handler2;
653 	cntr_alarm_cfg2.user_data = &cntr_alarm_cfg2;
654 
655 	k_sem_reset(&alarm_cnt_sem);
656 	alarm_cnt = 0;
657 
658 	if (counter_get_num_of_channels(dev) < 2U) {
659 		/* Counter does not support two alarms */
660 		return;
661 	}
662 
663 	err = counter_start(dev);
664 	zassert_equal(0, err, "%s: Counter failed to start", dev->name);
665 
666 	if (set_top_value_capable(dev)) {
667 		err = counter_set_top_value(dev, &top_cfg);
668 		zassert_equal(0, err, "%s: Counter failed to set top value", dev->name);
669 	} else {
670 		/* Counter does not support top value, do not run this test
671 		 * as it might take a long time to wrap and trigger the alarm
672 		 * resulting in test failures.
673 		 */
674 		return;
675 	}
676 
677 	k_busy_wait(3*(uint32_t)counter_ticks_to_us(dev, cntr_alarm_cfg.ticks));
678 
679 	err = counter_set_channel_alarm(dev, 0, &cntr_alarm_cfg);
680 	zassert_equal(0, err, "%s: Counter set alarm failed", dev->name);
681 
682 	err = counter_set_channel_alarm(dev, 1, &cntr_alarm_cfg2);
683 	zassert_equal(0, err, "%s: Counter set alarm failed", dev->name);
684 
685 	k_busy_wait(1.2 * counter_ticks_to_us(dev, ticks * 2U));
686 
687 	cnt = IS_ENABLED(CONFIG_ZERO_LATENCY_IRQS) ?
688 		alarm_cnt : k_sem_count_get(&alarm_cnt_sem);
689 	zassert_equal(2, cnt,
690 			"%s: Invalid number of callbacks %d (expected: %d)",
691 			dev->name, cnt, 2);
692 
693 	zassert_equal(&cntr_alarm_cfg2, clbk_data[0],
694 			"%s: Expected different order or callbacks",
695 			dev->name);
696 	zassert_equal(&cntr_alarm_cfg, clbk_data[1],
697 			"%s: Expected different order or callbacks",
698 			dev->name);
699 
700 	/* tear down */
701 	err = counter_cancel_channel_alarm(dev, 0);
702 	zassert_equal(0, err, "%s: Counter disabling alarm failed", dev->name);
703 
704 	err = counter_cancel_channel_alarm(dev, 1);
705 	zassert_equal(0, err, "%s: Counter disabling alarm failed", dev->name);
706 }
707 
multiple_channel_alarm_capable(const struct device * dev)708 static bool multiple_channel_alarm_capable(const struct device *dev)
709 {
710 	return alarm_capable(dev) && (counter_get_num_of_channels(dev) > 1);
711 }
712 
ZTEST(counter_basic,test_multiple_alarms)713 ZTEST(counter_basic, test_multiple_alarms)
714 {
715 	test_all_instances(test_multiple_alarms_instance,
716 			   multiple_channel_alarm_capable);
717 }
718 
test_all_channels_instance(const struct device * dev)719 static void test_all_channels_instance(const struct device *dev)
720 {
721 	int err;
722 	const int n = 10;
723 	int nchan = 0;
724 	bool limit_reached = false;
725 	struct counter_alarm_cfg alarm_cfgs;
726 	uint32_t ticks;
727 	uint32_t cnt;
728 	uint32_t counter_period_us;
729 
730 	counter_period_us = get_counter_period_us(dev);
731 	ticks = counter_us_to_ticks(dev, counter_period_us);
732 
733 	alarm_cfgs.flags = 0;
734 	alarm_cfgs.ticks = ticks;
735 	alarm_cfgs.callback = alarm_handler2;
736 	alarm_cfgs.user_data = NULL;
737 
738 	err = counter_start(dev);
739 	zassert_equal(0, err, "%s: Counter failed to start", dev->name);
740 
741 	for (int i = 0; i < n; i++) {
742 		err = counter_set_channel_alarm(dev, i, &alarm_cfgs);
743 		if ((err == 0) && !limit_reached) {
744 			nchan++;
745 		} else if (err == -ENOTSUP) {
746 			limit_reached = true;
747 		} else {
748 			zassert_equal(0, 1,
749 			   "%s: Unexpected error on setting alarm", dev->name);
750 		}
751 	}
752 
753 	k_busy_wait(1.5*counter_ticks_to_us(dev, ticks));
754 	cnt = IS_ENABLED(CONFIG_ZERO_LATENCY_IRQS) ?
755 		alarm_cnt : k_sem_count_get(&alarm_cnt_sem);
756 	zassert_equal(nchan, cnt,
757 			"%s: Expecting alarm callback", dev->name);
758 
759 	for (int i = 0; i < nchan; i++) {
760 		err = counter_cancel_channel_alarm(dev, i);
761 		zassert_equal(0, err,
762 			"%s: Unexpected error on disabling alarm", dev->name);
763 	}
764 
765 	for (int i = nchan; i < n; i++) {
766 		err = counter_cancel_channel_alarm(dev, i);
767 		zassert_equal(-ENOTSUP, err,
768 			"%s: Unexpected error on disabling alarm", dev->name);
769 	}
770 }
771 
ZTEST(counter_basic,test_all_channels)772 ZTEST(counter_basic, test_all_channels)
773 {
774 	test_all_instances(test_all_channels_instance,
775 			   single_channel_alarm_capable);
776 }
777 
test_valid_function_without_alarm(const struct device * dev)778 static void test_valid_function_without_alarm(const struct device *dev)
779 {
780 	int err;
781 	uint32_t ticks;
782 	uint32_t ticks_expected;
783 	uint32_t tick_current;
784 	uint32_t ticks_tol;
785 	uint32_t wait_for_us;
786 	uint32_t freq = counter_get_frequency(dev);
787 
788 	/* For timers which cannot count to at least 2 ms before overflow
789 	 * the test is skipped by test_all_instances function because sufficient
790 	 * accuracy of the test cannot be achieved.
791 	 */
792 
793 	zassert_true(freq != 0, "%s: counter could not get frequency", dev->name);
794 
795 	/* Set time of counting based on counter frequency to
796 	 * ensure convenient run time and accuracy of the test.
797 	 */
798 	if (freq < 1000) {
799 		/* Ensure to have 1 tick for 1 sec clock */
800 		wait_for_us = 1100000;
801 		ticks_expected = counter_us_to_ticks(dev, wait_for_us);
802 	} else if (freq < 1000000) {
803 		/* Calculate wait time for convenient ticks count */
804 		ticks_expected = 1000;
805 		wait_for_us = (ticks_expected * 1000000) / freq;
806 	} else {
807 		/* Wait long enough for high frequency clocks to minimize
808 		 * impact of latencies and k_busy_wait function accuracy.
809 		 */
810 		wait_for_us = 1000;
811 		ticks_expected = counter_us_to_ticks(dev, wait_for_us);
812 	}
813 
814 	/* Set percentage or 2 ticks tolerance, whichever is greater */
815 	ticks_tol = (ticks_expected * CONFIG_TEST_DRIVER_COUNTER_TOLERANCE) / 100;
816 	ticks_tol = ticks_tol < 2 ? 2 : ticks_tol;
817 
818 	err = counter_start(dev);
819 	zassert_equal(0, err, "%s: counter failed to start", dev->name);
820 
821 	/* counter might not start from 0, use current value as offset */
822 	counter_get_value(dev, &tick_current);
823 	if (counter_is_counting_up(dev)) {
824 		ticks_expected += tick_current;
825 	} else {
826 		ticks_expected = tick_current - ticks_expected;
827 	}
828 
829 	k_busy_wait(wait_for_us);
830 
831 	err = counter_get_value(dev, &ticks);
832 
833 	zassert_equal(0, err, "%s: could not get counter value", dev->name);
834 	zassert_between_inclusive(
835 		ticks, ticks_expected > ticks_tol ? ticks_expected - ticks_tol : 0,
836 		ticks_expected + ticks_tol, "%s: counter ticks not in tolerance", dev->name);
837 
838 	/* ticks count is always within ticks_tol for RTC, therefor
839 	 * check, if ticks are greater than 0.
840 	 */
841 	zassert_true((ticks > 0), "%s: counter did not count", dev->name);
842 
843 	err = counter_stop(dev);
844 	zassert_equal(0, err, "%s: counter failed to stop", dev->name);
845 }
846 
ms_period_capable(const struct device * dev)847 static bool ms_period_capable(const struct device *dev)
848 {
849 	uint32_t freq_khz;
850 	uint32_t max_time_ms;
851 
852 	/* Assume 2 ms counter periode can be set for frequency below 1 kHz*/
853 	if (counter_get_frequency(dev) < 1000) {
854 		return true;
855 	}
856 
857 	freq_khz = counter_get_frequency(dev) / 1000;
858 	max_time_ms = counter_get_top_value(dev) / freq_khz;
859 
860 	if (max_time_ms >= 2) {
861 		return true;
862 	}
863 
864 	return false;
865 }
866 
ZTEST(counter_basic,test_valid_function_without_alarm)867 ZTEST(counter_basic, test_valid_function_without_alarm)
868 {
869 	test_all_instances(test_valid_function_without_alarm, ms_period_capable);
870 }
871 
872 /**
873  * Test validates if alarm set too late (current tick or current tick + 1)
874  * results in callback being called.
875  */
test_late_alarm_instance(const struct device * dev)876 static void test_late_alarm_instance(const struct device *dev)
877 {
878 	int err;
879 	uint32_t cnt;
880 	uint32_t tick_us = (uint32_t)counter_ticks_to_us(dev, 1);
881 	uint32_t guard = counter_us_to_ticks(dev, 200);
882 	struct counter_alarm_cfg alarm_cfg = {
883 		.callback = alarm_handler,
884 		.flags = COUNTER_ALARM_CFG_ABSOLUTE |
885 			 COUNTER_ALARM_CFG_EXPIRE_WHEN_LATE,
886 		.user_data = NULL
887 	};
888 
889 	/* for timers with very short ticks, counter_ticks_to_us() returns 0 */
890 	tick_us = MAX(tick_us, 1);
891 
892 	err = counter_set_guard_period(dev, guard,
893 					COUNTER_GUARD_PERIOD_LATE_TO_SET);
894 	zassert_equal(0, err, "%s: Unexpected error", dev->name);
895 
896 	err = counter_start(dev);
897 	zassert_equal(0, err, "%s: Unexpected error", dev->name);
898 
899 	k_busy_wait(2*tick_us);
900 
901 	alarm_cfg.ticks = counter_is_counting_up(dev) ? 0 : counter_get_top_value(dev);
902 	err = counter_set_channel_alarm(dev, 0, &alarm_cfg);
903 	zassert_equal(-ETIME, err, "%s: Unexpected error (%d)", dev->name, err);
904 
905 	/* wait couple of ticks */
906 	k_busy_wait(5*tick_us);
907 
908 	cnt = IS_ENABLED(CONFIG_ZERO_LATENCY_IRQS) ?
909 		alarm_cnt : k_sem_count_get(&alarm_cnt_sem);
910 	zassert_equal(1, cnt,
911 			"%s: Expected %d callbacks, got %d\n",
912 			dev->name, 1, cnt);
913 
914 	err = counter_get_value(dev, &(alarm_cfg.ticks));
915 	zassert_true(err == 0, "%s: Counter read failed (err: %d)", dev->name,
916 		     err);
917 
918 	err = counter_set_channel_alarm(dev, 0, &alarm_cfg);
919 	zassert_equal(-ETIME, err, "%s: Failed to set an alarm (err: %d)",
920 			dev->name, err);
921 
922 	/* wait to ensure that tick+1 timeout will expire. */
923 	k_busy_wait(3*tick_us);
924 
925 	cnt = IS_ENABLED(CONFIG_ZERO_LATENCY_IRQS) ?
926 		alarm_cnt : k_sem_count_get(&alarm_cnt_sem);
927 	zassert_equal(2, cnt,
928 			"%s: Expected %d callbacks, got %d\n",
929 			dev->name, 2, cnt);
930 }
931 
test_late_alarm_error_instance(const struct device * dev)932 static void test_late_alarm_error_instance(const struct device *dev)
933 {
934 	int err;
935 	uint32_t tick_us = (uint32_t)counter_ticks_to_us(dev, 1);
936 	uint32_t guard = counter_us_to_ticks(dev, 200);
937 	struct counter_alarm_cfg alarm_cfg = {
938 		.callback = alarm_handler,
939 		.flags = COUNTER_ALARM_CFG_ABSOLUTE,
940 		.user_data = NULL
941 	};
942 
943 	/* for timers with very short ticks, counter_ticks_to_us() returns 0 */
944 	tick_us = MAX(tick_us, 1);
945 
946 	err = counter_set_guard_period(dev, guard,
947 					COUNTER_GUARD_PERIOD_LATE_TO_SET);
948 	zassert_equal(0, err, "%s: Unexpected error", dev->name);
949 
950 	err = counter_start(dev);
951 	zassert_equal(0, err, "%s: Unexpected error", dev->name);
952 
953 	k_busy_wait(2*tick_us);
954 
955 	alarm_cfg.ticks = counter_is_counting_up(dev) ? 0 : counter_get_top_value(dev);
956 	err = counter_set_channel_alarm(dev, 0, &alarm_cfg);
957 	zassert_equal(-ETIME, err,
958 			"%s: Failed to detect late setting (err: %d)",
959 			dev->name, err);
960 
961 	err = counter_get_value(dev, &(alarm_cfg.ticks));
962 	zassert_true(err == 0, "%s: Counter read failed (err: %d)", dev->name,
963 		     err);
964 
965 	err = counter_set_channel_alarm(dev, 0, &alarm_cfg);
966 	zassert_equal(-ETIME, err,
967 			"%s: Counter failed to detect late setting (err: %d)",
968 			dev->name, err);
969 }
970 
late_detection_capable(const struct device * dev)971 static bool late_detection_capable(const struct device *dev)
972 {
973 	uint32_t guard = counter_get_guard_period(dev,
974 					COUNTER_GUARD_PERIOD_LATE_TO_SET);
975 	int err = counter_set_guard_period(dev, guard,
976 					COUNTER_GUARD_PERIOD_LATE_TO_SET);
977 
978 	if (err == -ENOSYS) {
979 		return false;
980 	}
981 
982 	if (single_channel_alarm_capable(dev) == false) {
983 		return false;
984 	}
985 
986 	return true;
987 }
988 
ZTEST(counter_basic,test_late_alarm)989 ZTEST(counter_basic, test_late_alarm)
990 {
991 	test_all_instances(test_late_alarm_instance, late_detection_capable);
992 }
993 
ZTEST(counter_basic,test_late_alarm_error)994 ZTEST(counter_basic, test_late_alarm_error)
995 {
996 	test_all_instances(test_late_alarm_error_instance,
997 			   late_detection_capable);
998 }
999 
test_short_relative_alarm_instance(const struct device * dev)1000 static void test_short_relative_alarm_instance(const struct device *dev)
1001 {
1002 	int err;
1003 	uint32_t cnt;
1004 	uint32_t tick_us = (uint32_t)counter_ticks_to_us(dev, 1);
1005 	struct counter_alarm_cfg alarm_cfg = {
1006 		.callback = alarm_handler,
1007 		.flags = 0,
1008 		.user_data = NULL
1009 	};
1010 
1011 	/* for timers with very short ticks, counter_ticks_to_us() returns 0 */
1012 	tick_us = tick_us == 0 ? 1 : tick_us;
1013 
1014 	err = counter_start(dev);
1015 	zassert_equal(0, err, "%s: Unexpected error", dev->name);
1016 
1017 	if (IS_ENABLED(CONFIG_COUNTER_NRF_RTC)) {
1018 		k_busy_wait(1000);
1019 	}
1020 
1021 	alarm_cfg.ticks = 1;
1022 
1023 	for (int i = 0; i < 100; ++i) {
1024 		err = counter_set_channel_alarm(dev, 0, &alarm_cfg);
1025 		zassert_equal(0, err,
1026 				"%s: Failed to set an alarm (err: %d)",
1027 				dev->name, err);
1028 
1029 		/* wait to ensure that tick+1 timeout will expire. */
1030 		k_busy_wait(3*tick_us);
1031 
1032 		cnt = IS_ENABLED(CONFIG_ZERO_LATENCY_IRQS) ?
1033 			alarm_cnt : k_sem_count_get(&alarm_cnt_sem);
1034 		zassert_equal(i + 1, cnt,
1035 				"%s: Expected %d callbacks, got %d\n",
1036 				dev->name, i + 1, cnt);
1037 	}
1038 }
1039 
1040 /* Function checks if relative alarm set for 1 tick will expire. If handler is
1041  * not called within near future it indicates that driver do not support it and
1042  * more extensive testing is skipped.
1043  */
short_relative_capable(const struct device * dev)1044 static bool short_relative_capable(const struct device *dev)
1045 {
1046 	struct counter_alarm_cfg alarm_cfg = {
1047 		.callback = alarm_handler,
1048 		.flags = 0,
1049 		.user_data = NULL,
1050 		.ticks = 1
1051 	};
1052 	int err;
1053 	uint32_t cnt;
1054 	bool ret;
1055 
1056 	if (single_channel_alarm_capable(dev) == false) {
1057 		return false;
1058 	}
1059 
1060 	err = counter_start(dev);
1061 	if (err != 0) {
1062 		ret = false;
1063 		goto end;
1064 	}
1065 
1066 	k_sem_reset(&alarm_cnt_sem);
1067 	alarm_cnt = 0;
1068 	err = counter_set_channel_alarm(dev, 0, &alarm_cfg);
1069 	if (err != 0) {
1070 		ret = false;
1071 		goto end;
1072 	}
1073 
1074 	k_busy_wait(counter_ticks_to_us(dev, 10));
1075 	cnt = IS_ENABLED(CONFIG_ZERO_LATENCY_IRQS) ?
1076 			alarm_cnt : k_sem_count_get(&alarm_cnt_sem);
1077 	if (cnt == 1) {
1078 		ret = true;
1079 	} else {
1080 		ret = false;
1081 		(void)counter_cancel_channel_alarm(dev, 0);
1082 	}
1083 
1084 end:
1085 	k_sem_reset(&alarm_cnt_sem);
1086 	alarm_cnt = 0;
1087 	counter_stop(dev);
1088 	k_busy_wait(1000);
1089 
1090 	return ret;
1091 }
1092 
ZTEST(counter_basic,test_short_relative_alarm)1093 ZTEST(counter_basic, test_short_relative_alarm)
1094 {
1095 	test_all_instances(test_short_relative_alarm_instance,
1096 			short_relative_capable);
1097 }
1098 
1099 /* Test checks if cancelled alarm does not get triggered when new alarm is
1100  * configured at the point where previous alarm was about to expire.
1101  */
test_cancelled_alarm_does_not_expire_instance(const struct device * dev)1102 static void test_cancelled_alarm_does_not_expire_instance(const struct device *dev)
1103 {
1104 	int err;
1105 	uint32_t cnt;
1106 	uint32_t us = 1000;
1107 	uint32_t ticks = counter_us_to_ticks(dev, us);
1108 	uint32_t top = counter_get_top_value(dev);
1109 
1110 	us = (uint32_t)counter_ticks_to_us(dev, ticks);
1111 
1112 	struct counter_alarm_cfg alarm_cfg = {
1113 		.callback = alarm_handler,
1114 		.flags = COUNTER_ALARM_CFG_ABSOLUTE,
1115 		.user_data = NULL
1116 	};
1117 
1118 	err = counter_start(dev);
1119 	zassert_equal(0, err, "%s: Unexpected error", dev->name);
1120 
1121 
1122 	for (int i = 0; i < us/2; ++i) {
1123 		err = counter_get_value(dev, &(alarm_cfg.ticks));
1124 		zassert_true(err == 0, "%s: Counter read failed (err: %d)",
1125 			     dev->name, err);
1126 
1127 		alarm_cfg.ticks	+= ticks;
1128 		alarm_cfg.ticks = alarm_cfg.ticks % top;
1129 		err = counter_set_channel_alarm(dev, 0, &alarm_cfg);
1130 		zassert_equal(0, err, "%s: Failed to set an alarm (err: %d)",
1131 				dev->name, err);
1132 
1133 		err = counter_cancel_channel_alarm(dev, 0);
1134 		zassert_equal(0, err, "%s: Failed to cancel an alarm (err: %d)",
1135 				dev->name, err);
1136 
1137 		k_busy_wait(us/2 + i);
1138 
1139 		alarm_cfg.ticks = alarm_cfg.ticks + 2*ticks;
1140 		alarm_cfg.ticks = alarm_cfg.ticks % top;
1141 		err = counter_set_channel_alarm(dev, 0, &alarm_cfg);
1142 		zassert_equal(0, err, "%s: Failed to set an alarm (err: %d)",
1143 				dev->name, err);
1144 
1145 		/* wait to ensure that tick+1 timeout will expire. */
1146 		k_busy_wait(us);
1147 
1148 		err = counter_cancel_channel_alarm(dev, 0);
1149 		zassert_equal(0, err, "%s: Failed to cancel an alarm (err: %d)",
1150 					dev->name, err);
1151 
1152 		cnt = IS_ENABLED(CONFIG_ZERO_LATENCY_IRQS) ?
1153 			alarm_cnt : k_sem_count_get(&alarm_cnt_sem);
1154 		zassert_equal(0, cnt,
1155 				"%s: Expected %d callbacks, got %d (i:%d)\n",
1156 				dev->name, 0, cnt, i);
1157 	}
1158 }
1159 
reliable_cancel_capable(const struct device * dev)1160 static bool reliable_cancel_capable(const struct device *dev)
1161 {
1162 	/* Test performed only for NRF_RTC instances. Other probably will fail.
1163 	 */
1164 #if defined(CONFIG_COUNTER_NRF_RTC) || defined(CONFIG_COUNTER_NRF_TIMER)
1165 	return true;
1166 #endif
1167 #ifdef CONFIG_COUNTER_TIMER_STM32
1168 	if (single_channel_alarm_capable(dev)) {
1169 		return true;
1170 	}
1171 #endif
1172 #ifdef CONFIG_COUNTER_TIMER_MAX32
1173 	if (single_channel_alarm_capable(dev)) {
1174 		return true;
1175 	}
1176 #endif
1177 #ifdef CONFIG_COUNTER_TIMER_GD32
1178 	if (single_channel_alarm_capable(dev)) {
1179 		return true;
1180 	}
1181 #endif
1182 #ifdef CONFIG_COUNTER_NATIVE_SIM
1183 	if (dev == DEVICE_DT_GET(DT_NODELABEL(counter0))) {
1184 		return true;
1185 	}
1186 #endif
1187 #ifdef CONFIG_COUNTER_AMBIQ
1188 	if (single_channel_alarm_capable(dev)) {
1189 		return true;
1190 	}
1191 #endif
1192 #ifdef CONFIG_COUNTER_NXP_S32_SYS_TIMER
1193 	if (single_channel_alarm_capable(dev)) {
1194 		return true;
1195 	}
1196 #endif
1197 #ifdef CONFIG_COUNTER_MCUX_RTC
1198 	if (single_channel_alarm_capable(dev)) {
1199 		return true;
1200 	}
1201 #endif
1202 #ifdef CONFIG_COUNTER_RENESAS_RZ_GTM
1203 	if (single_channel_alarm_capable(dev)) {
1204 		return true;
1205 	}
1206 #endif
1207 #ifdef CONFIG_COUNTER_TMR_ESP32
1208 	if (single_channel_alarm_capable(dev)) {
1209 		return true;
1210 	}
1211 #endif
1212 #ifdef CONFIG_COUNTER_RENESAS_RZ_CMTW
1213 	if (single_channel_alarm_capable(dev)) {
1214 		return true;
1215 	}
1216 #endif
1217 	return false;
1218 }
1219 
ZTEST(counter_basic,test_cancelled_alarm_does_not_expire)1220 ZTEST(counter_basic, test_cancelled_alarm_does_not_expire)
1221 {
1222 	test_all_instances(test_cancelled_alarm_does_not_expire_instance,
1223 			reliable_cancel_capable);
1224 }
1225 
counter_setup(void)1226 static void *counter_setup(void)
1227 {
1228 	int i;
1229 
1230 	/* Give required clocks some time to stabilize. In particular, nRF SoCs
1231 	 * need such delay for the Xtal LF clock source to start and for this
1232 	 * test to use the correct timing.
1233 	 */
1234 	k_busy_wait(USEC_PER_MSEC * 300);
1235 
1236 	k_sem_init(&top_cnt_sem, 0, UINT_MAX);
1237 	k_object_access_grant(&top_cnt_sem, k_current_get());
1238 
1239 	k_sem_init(&alarm_cnt_sem, 0, UINT_MAX);
1240 	k_object_access_grant(&alarm_cnt_sem, k_current_get());
1241 
1242 	for (i = 0; i < ARRAY_SIZE(devices); i++) {
1243 		zassert_true(device_is_ready(devices[i]),
1244 			     "Device %s is not ready", devices[i]->name);
1245 		k_object_access_grant(devices[i], k_current_get());
1246 	}
1247 
1248 	return NULL;
1249 }
1250 
1251 /* Uses callbacks, run in supervisor mode */
1252 ZTEST_SUITE(counter_basic, NULL, counter_setup, NULL, NULL, NULL);
1253 
1254 /* No callbacks, run in usermode */
1255 ZTEST_SUITE(counter_no_callback, NULL, counter_setup, NULL, NULL, NULL);
1256