test: drivers: stepper: add tests for stepper api

This commit adds tests for stepper api

Signed-off-by: Jilay Pandya <jilay.pandya@outlook.com>
This commit is contained in:
Jilay Pandya 2024-08-29 15:16:48 +02:00 committed by Carles Cufí
commit 95f8292329
9 changed files with 156 additions and 1 deletions

View file

@ -2,6 +2,5 @@ CONFIG_TEST=y
CONFIG_TEST_USERSPACE=y
CONFIG_LOG=y
CONFIG_STEPPER_LOG_LEVEL_DBG=y
CONFIG_POLL=y
CONFIG_GPIO=y
CONFIG_STEPPER=y

View file

@ -0,0 +1,10 @@
# Copyright (c) 2024 Jilay Sandeep Pandya
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(stepper_api)
target_sources(app PRIVATE
src/main.c
)

View file

@ -0,0 +1,5 @@
# Copyright (c) 2024 Jilay Sandeep Pandya
# SPDX-License-Identifier: Apache-2.0
CONFIG_GPIO=y
CONFIG_GPIO_STEPPER=y

View file

@ -0,0 +1,13 @@
/ {
uln2003_motor: uln2003_1 {
compatible = "gpio-steppers";
status = "okay";
motor_1: motor_1 {
micro-step-res = <1>;
gpios = <&gpioa 9 GPIO_ACTIVE_HIGH>,
<&gpioc 7 GPIO_ACTIVE_HIGH>,
<&gpiob 0 GPIO_ACTIVE_HIGH>,
<&gpioa 7 GPIO_ACTIVE_HIGH>;
};
};
};

View file

@ -0,0 +1,5 @@
# Copyright (c) 2024 Jilay Sandeep Pandya
# SPDX-License-Identifier: Apache-2.0
CONFIG_GPIO=y
CONFIG_GPIO_STEPPER=y

View file

@ -0,0 +1,34 @@
/*
* Copyright (c) 2024 Jilay Sandeep Pandya
* SPDX-License-Identifier: Apache-2.0
*/
/ {
test {
#address-cells = <1>;
#size-cells = <1>;
test_gpio: gpio@deadbeef {
compatible = "vnd,gpio";
gpio-controller;
reg = <0xdeadbeef 0x1000>;
#gpio-cells = <0x2>;
status = "okay";
};
};
};
/ {
test_uln2003_motor_cluster: uln2003_motor_cluster {
compatible = "gpio-steppers";
status = "okay";
motor_1: motor_1 {
micro-step-res = <1>;
gpios = <&test_gpio 0 0>,
<&test_gpio 0 0>,
<&test_gpio 0 0>,
<&test_gpio 0 0>;
};
};
};

View file

@ -0,0 +1,9 @@
# Copyright (c) 2024 Jilay Sandeep Pandya
# SPDX-License-Identifier: Apache-2.0
CONFIG_ZTEST=y
CONFIG_TEST=y
CONFIG_TEST_USERSPACE=y
CONFIG_LOG=y
CONFIG_STEPPER_LOG_LEVEL_DBG=y
CONFIG_STEPPER=y

View file

@ -0,0 +1,68 @@
/*
* Copyright 2024 Jilay Sandeep Pandya
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/ztest.h>
#include <zephyr/drivers/stepper.h>
struct stepper_fixture {
const struct device *dev;
};
static void *stepper_setup(void)
{
static struct stepper_fixture fixture = {
.dev = DEVICE_DT_GET(DT_NODELABEL(motor_1)),
};
zassert_not_null(fixture.dev);
return &fixture;
}
static void stepper_before(void *f)
{
struct stepper_fixture *fixture = f;
(void)stepper_set_actual_position(fixture->dev, 0);
}
ZTEST_SUITE(stepper, NULL, stepper_setup, stepper_before, NULL, NULL);
ZTEST_F(stepper, test_micro_step_res)
{
(void)stepper_set_micro_step_res(fixture->dev, 2);
enum micro_step_resolution res;
(void)stepper_get_micro_step_res(fixture->dev, &res);
zassert_equal(res, 2, "Micro step resolution not set correctly");
}
ZTEST_F(stepper, test_actual_position)
{
int32_t pos = 100u;
(void)stepper_set_actual_position(fixture->dev, pos);
(void)stepper_get_actual_position(fixture->dev, &pos);
zassert_equal(pos, 100u, "Actual position not set correctly");
}
ZTEST_F(stepper, test_target_position)
{
int32_t pos = 100u;
struct k_poll_signal signal;
k_poll_signal_init(&signal);
struct k_poll_event event;
k_poll_event_init(&event, K_POLL_TYPE_SIGNAL, K_POLL_MODE_NOTIFY_ONLY, &signal);
(void)stepper_set_max_velocity(fixture->dev, 100u);
(void)stepper_set_target_position(fixture->dev, pos, &signal);
(void)k_poll(&event, 1, K_SECONDS(2));
unsigned int signaled;
int result;
k_poll_signal_check(&signal, &signaled, &result);
zassert_equal(signaled, 1, "Signal not set");
zassert_equal(result, STEPPER_SIGNAL_STEPS_COMPLETED, "Signal not set");
(void)stepper_get_actual_position(fixture->dev, &pos);
zassert_equal(pos, 100u, "Target position should be %d but is %d", 100u, pos);
}

View file

@ -0,0 +1,12 @@
# Copyright (c) 2024 Jilay Sandeep Pandya
# SPDX-License-Identifier: Apache-2.0
tests:
drivers.stepper.stepper_api:
tags:
- drivers
- stepper
- api
platform_allow:
- nucleo_g071rb
- qemu_x86_64