Commit c4970880 authored by Josselin Bunt's avatar Josselin Bunt Committed by Benjamin Cabé
Browse files

drivers: stepper: Add unit tests for stepper API using work_q



This commit adds unit tests for the stepper API using work_q scheduler.

Signed-off-by: default avatarJosselin Bunt <josselin@sensible.health>
parent 902824db
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
# Copyright (c) 2024 Josselin Bunt
# SPDX-License-Identifier: Apache-2.0

mainmenu "Stepper API Test"

source "Kconfig.zephyr"

config STEPPER_TEST_TIMING_TIMEOUT_TOLERANCE_PCT
	int "Stepper timing tolerance percentage"
	default 20
	help
	  Additional margin (%) added to step timing during test timeouts.
	  Accounts for execution and scheduling jitter.
+24 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2025 Josselin Bunt
 * SPDX-License-Identifier: Apache-2.0
 */

#include "native_sim.overlay"

/ {
	aliases {
		stepper =  &adi_tmc2209;
	};
};

/ {
	adi_tmc2209: adi_tmc2209 {
		status = "okay";
		compatible = "adi,tmc2209";
		micro-step-res = <32>;
		dir-gpios = <&gpio1 0 0>;
		step-gpios = <&gpio1 1 0>;
		en-gpios = <&gpio2 1 0>;
		msx-gpios = <&gpio3 0 0>, <&gpio4 1 0>;
	};
};
+26 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2025 Josselin Bunt
 * SPDX-License-Identifier: Apache-2.0
 */

#include "native_sim.overlay"

/ {
	aliases {
		stepper =  &allegro_a4979;
	};
};

/ {
	allegro_a4979: allegro_a4979 {
		status = "okay";
		compatible = "allegro,a4979";
		micro-step-res = <1>;
		reset-gpios = <&gpio4 0 0>;
		dir-gpios = <&gpio1 0 0>;
		step-gpios = <&gpio1 1 0>;
		en-gpios = <&gpio2 1 0>;
		m0-gpios = <&gpio3 0 0>;
		m1-gpios = <&gpio3 1 0>;
	};
};
+26 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2025 Josselin Bunt
 * SPDX-License-Identifier: Apache-2.0
 */

#include "native_sim.overlay"

/ {
	aliases {
		stepper =  &ti_drv84xx;
	};
};

/ {
	ti_drv84xx: ti_drv84xx {
		status = "okay";
		compatible = "ti,drv84xx";
		micro-step-res = <8>;
		dir-gpios = <&gpio1 0 0>;
		step-gpios = <&gpio1 1 0>;
		sleep-gpios = <&gpio2 0 GPIO_ACTIVE_LOW>;
		en-gpios = <&gpio2 1 0>;
		m0-gpios = <&gpio3 0 0>;
		m1-gpios = <&gpio3 1 0>;
	};
};
+33 −3
Original line number Diff line number Diff line
@@ -140,15 +140,45 @@ ZTEST_F(stepper, test_target_position_w_fixed_step_interval)

	(void)stepper_move_to(fixture->dev, pos);

	/* timeout is set with 20% tolerance */
	POLL_AND_CHECK_SIGNAL(stepper_signal, stepper_event, STEPPER_EVENT_STEPS_COMPLETED,
			      K_MSEC(pos * 120));
	POLL_AND_CHECK_SIGNAL(
		stepper_signal, stepper_event, STEPPER_EVENT_STEPS_COMPLETED,
		K_MSEC(pos * (100 + CONFIG_STEPPER_TEST_TIMING_TIMEOUT_TOLERANCE_PCT)));

	(void)stepper_get_actual_position(fixture->dev, &pos);
	zassert_equal(pos, 10u, "Target position should be %d but is %d", 10u, pos);
	zassert_equal(user_data_received, fixture->dev, "User data not received");
}

ZTEST_F(stepper, test_move_by_positive_step_count)
{
	int32_t steps = 20;

	(void)stepper_set_microstep_interval(fixture->dev, 100 * USEC_PER_SEC);
	(void)stepper_set_event_callback(fixture->dev, fixture->callback, (void *)fixture->dev);
	(void)stepper_move_by(fixture->dev, steps);

	POLL_AND_CHECK_SIGNAL(
		stepper_signal, stepper_event, STEPPER_EVENT_STEPS_COMPLETED,
		K_MSEC(steps * (100 + CONFIG_STEPPER_TEST_TIMING_TIMEOUT_TOLERANCE_PCT)));
	(void)stepper_get_actual_position(fixture->dev, &steps);
	zassert_equal(steps, 20u, "Target position should be %d but is %d", 20u, steps);
}

ZTEST_F(stepper, test_move_by_negative_step_count)
{
	int32_t steps = -20;

	(void)stepper_set_microstep_interval(fixture->dev, 100 * USEC_PER_SEC);
	(void)stepper_set_event_callback(fixture->dev, fixture->callback, (void *)fixture->dev);
	(void)stepper_move_by(fixture->dev, steps);

	POLL_AND_CHECK_SIGNAL(
		stepper_signal, stepper_event, STEPPER_EVENT_STEPS_COMPLETED,
		K_MSEC(-steps * (100 + CONFIG_STEPPER_TEST_TIMING_TIMEOUT_TOLERANCE_PCT)));
	(void)stepper_get_actual_position(fixture->dev, &steps);
	zassert_equal(steps, -20u, "Target position should be %d but is %d", -20u, steps);
}

ZTEST_F(stepper, test_stop)
{
	(void)stepper_set_event_callback(fixture->dev, fixture->callback, (void *)fixture->dev);
Loading