zephyr/samples/drivers/dac/src/main.c
Gerard Marull-Paretas 79e6b0e0f6 includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.

The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.

NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-09-05 16:31:47 +02:00

73 lines
1.9 KiB
C

/*
* Copyright (c) 2020 Libre Solar Technologies GmbH
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <zephyr/drivers/dac.h>
#define ZEPHYR_USER_NODE DT_PATH(zephyr_user)
#if (DT_NODE_HAS_PROP(ZEPHYR_USER_NODE, dac) && \
DT_NODE_HAS_PROP(ZEPHYR_USER_NODE, dac_channel_id) && \
DT_NODE_HAS_PROP(ZEPHYR_USER_NODE, dac_resolution))
#define DAC_NODE DT_PHANDLE(ZEPHYR_USER_NODE, dac)
#define DAC_CHANNEL_ID DT_PROP(ZEPHYR_USER_NODE, dac_channel_id)
#define DAC_RESOLUTION DT_PROP(ZEPHYR_USER_NODE, dac_resolution)
#else
#error "Unsupported board: see README and check /zephyr,user node"
#define DAC_NODE DT_INVALID_NODE
#define DAC_CHANNEL_ID 0
#define DAC_RESOLUTION 0
#endif
static const struct device *const dac_dev = DEVICE_DT_GET(DAC_NODE);
static const struct dac_channel_cfg dac_ch_cfg = {
.channel_id = DAC_CHANNEL_ID,
.resolution = DAC_RESOLUTION
};
void main(void)
{
if (!device_is_ready(dac_dev)) {
printk("DAC device %s is not ready\n", dac_dev->name);
return;
}
int ret = dac_channel_setup(dac_dev, &dac_ch_cfg);
if (ret != 0) {
printk("Setting up of DAC channel failed with code %d\n", ret);
return;
}
printk("Generating sawtooth signal at DAC channel %d.\n",
DAC_CHANNEL_ID);
while (1) {
/* Number of valid DAC values, e.g. 4096 for 12-bit DAC */
const int dac_values = 1U << DAC_RESOLUTION;
/*
* 1 msec sleep leads to about 4 sec signal period for 12-bit
* DACs. For DACs with lower resolution, sleep time needs to
* be increased.
* Make sure to sleep at least 1 msec even for future 16-bit
* DACs (lowering signal frequency).
*/
const int sleep_time = 4096 / dac_values > 0 ?
4096 / dac_values : 1;
for (int i = 0; i < dac_values; i++) {
ret = dac_write_value(dac_dev, DAC_CHANNEL_ID, i);
if (ret != 0) {
printk("dac_write_value() failed with code %d\n", ret);
return;
}
k_sleep(K_MSEC(sleep_time));
}
}
}