Commit de04d33d authored by Martin Jäger's avatar Martin Jäger Committed by Carles Cufi
Browse files

tests: drivers: dac: Add write_value API test



Test runs with nucleo_l073rz board.

Signed-off-by: default avatarMartin Jäger <martin@libre.solar>
parent 8870a699
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.13.1)

find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})
project(dac_api)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
+4 −0
Original line number Diff line number Diff line
CONFIG_ZTEST=y

CONFIG_DAC=y
CONFIG_TEST_USERSPACE=y
+27 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2020 Libre Solar Technologies GmbH
 *
 * SPDX-License-Identifier: Apache-2.0
 */

/**
 * @addtogroup t_driver_dac
 * @{
 * @defgroup t_dac_basic test_dac_basic_operations
 * @}
 */

#include <zephyr.h>
#include <ztest.h>

extern void test_dac_write_value(void);
extern struct device *get_dac_device(void);

void test_main(void)
{
	k_object_access_grant(get_dac_device(), k_current_get());

	ztest_test_suite(dac_basic_test,
			 ztest_user_unit_test(test_dac_write_value));
	ztest_run_test_suite(dac_basic_test);
}
+77 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2020 Libre Solar Technologies GmbH
 *
 * SPDX-License-Identifier: Apache-2.0
 */

/*
 * @addtogroup test_dac_basic_operations
 * @{
 * @defgroup t_dac_basic_basic_operations test_dac_write
 * @brief TestPurpose: verify DAC driver handles single write
 * @}
 */

#include <drivers/dac.h>
#include <zephyr.h>
#include <ztest.h>

#if defined(CONFIG_BOARD_NUCLEO_L073RZ)

#define DAC_DEVICE_NAME		DT_LABEL(DT_ALIAS(dac1))
#define DAC_CHANNEL_ID		1
#define DAC_RESOLUTION		12

#else
#error "Unsupported board."
#endif

static const struct dac_channel_cfg dac_ch_cfg = {
	.channel_id  = DAC_CHANNEL_ID,
	.resolution  = DAC_RESOLUTION
};

struct device *get_dac_device(void)
{
	return device_get_binding(DAC_DEVICE_NAME);
}

static struct device *init_dac(void)
{
	int ret;
	struct device *dac_dev = device_get_binding(DAC_DEVICE_NAME);

	zassert_not_null(dac_dev, "Cannot get DAC device");

	ret = dac_channel_setup(dac_dev, &dac_ch_cfg);
	zassert_equal(ret, 0,
		"Setting up of the first channel failed with code %d", ret);

	return dac_dev;
}

/*
 * test_dac_write_value
 */
static int test_task_write_value(void)
{
	int ret;

	struct device *dac_dev = init_dac();

	if (!dac_dev) {
		return TC_FAIL;
	}

	/* write a value of half the full scale resolution */
	ret = dac_write_value(dac_dev, DAC_CHANNEL_ID,
						(1U << DAC_RESOLUTION) / 2);
	zassert_equal(ret, 0, "dac_write_value() failed with code %d", ret);

	return TC_PASS;
}

void test_dac_write_value(void)
{
	zassert_true(test_task_write_value() == TC_PASS, NULL);
}
+6 −0
Original line number Diff line number Diff line
common:
  tags: dac drivers userspace
tests:
  drivers.dac:
    platform_whitelist: nucleo_l073rz
    depends_on: dac