2020-02-17 03:55:58 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Henrik Brix Andersen <henrik@brixandersen.dk>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief DAC shell commands.
|
|
|
|
*/
|
|
|
|
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/shell/shell.h>
|
|
|
|
#include <zephyr/drivers/dac.h>
|
2020-02-17 03:55:58 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
struct args_index {
|
2020-05-28 00:26:57 +08:00
|
|
|
uint8_t device;
|
|
|
|
uint8_t channel;
|
|
|
|
uint8_t value;
|
|
|
|
uint8_t resolution;
|
2024-09-19 23:14:09 +08:00
|
|
|
uint8_t options;
|
2020-02-17 03:55:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct args_index args_indx = {
|
|
|
|
.device = 1,
|
|
|
|
.channel = 2,
|
|
|
|
.value = 3,
|
2020-02-17 03:57:20 +08:00
|
|
|
.resolution = 3,
|
2024-09-19 23:14:09 +08:00
|
|
|
.options = 4,
|
2020-02-17 03:55:58 +08:00
|
|
|
};
|
|
|
|
|
2023-04-14 00:59:37 +08:00
|
|
|
static int cmd_setup(const struct shell *sh, size_t argc, char **argv)
|
2020-02-17 03:55:58 +08:00
|
|
|
{
|
2024-09-19 23:14:09 +08:00
|
|
|
struct dac_channel_cfg cfg = {0};
|
2020-05-01 02:33:38 +08:00
|
|
|
const struct device *dac;
|
2024-09-19 23:14:09 +08:00
|
|
|
int argidx;
|
2020-02-17 03:55:58 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
dac = device_get_binding(argv[args_indx.device]);
|
|
|
|
if (!dac) {
|
2023-04-14 00:59:37 +08:00
|
|
|
shell_error(sh, "DAC device not found");
|
2020-02-17 03:55:58 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.channel_id = strtoul(argv[args_indx.channel], NULL, 0);
|
2020-02-17 03:57:20 +08:00
|
|
|
cfg.resolution = strtoul(argv[args_indx.resolution], NULL, 0);
|
2020-02-17 03:55:58 +08:00
|
|
|
|
2024-09-19 23:14:09 +08:00
|
|
|
argidx = args_indx.options;
|
|
|
|
while (argidx < argc && strncmp(argv[argidx], "-", 1) == 0) {
|
|
|
|
if (strcmp(argv[argidx], "-b") == 0) {
|
|
|
|
cfg.buffered = true;
|
|
|
|
argidx++;
|
|
|
|
} else if (strcmp(argv[argidx], "-i") == 0) {
|
|
|
|
cfg.internal = true;
|
|
|
|
argidx++;
|
|
|
|
} else {
|
|
|
|
shell_error(sh, "unsupported option %s", argv[argidx]);
|
|
|
|
shell_help(sh);
|
|
|
|
return SHELL_CMD_HELP_PRINTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 03:55:58 +08:00
|
|
|
err = dac_channel_setup(dac, &cfg);
|
|
|
|
if (err) {
|
2023-04-14 00:59:37 +08:00
|
|
|
shell_error(sh, "Failed to setup DAC channel (err %d)", err);
|
2020-02-17 03:55:58 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-04-14 00:59:37 +08:00
|
|
|
static int cmd_write_value(const struct shell *sh, size_t argc, char **argv)
|
2020-02-17 03:55:58 +08:00
|
|
|
{
|
2020-05-01 02:33:38 +08:00
|
|
|
const struct device *dac;
|
2020-05-28 00:26:57 +08:00
|
|
|
uint8_t channel;
|
|
|
|
uint32_t value;
|
2020-02-17 03:55:58 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
dac = device_get_binding(argv[args_indx.device]);
|
|
|
|
if (!dac) {
|
2023-04-14 00:59:37 +08:00
|
|
|
shell_error(sh, "DAC device not found");
|
2020-02-17 03:55:58 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
channel = strtoul(argv[args_indx.channel], NULL, 0);
|
|
|
|
value = strtoul(argv[args_indx.value], NULL, 0);
|
|
|
|
|
2020-02-17 03:57:20 +08:00
|
|
|
err = dac_write_value(dac, channel, value);
|
2020-02-17 03:55:58 +08:00
|
|
|
if (err) {
|
2023-04-14 00:59:37 +08:00
|
|
|
shell_error(sh, "Failed to write DAC value (err %d)", err);
|
2020-02-17 03:55:58 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SHELL_STATIC_SUBCMD_SET_CREATE(dac_cmds,
|
2024-09-19 23:14:09 +08:00
|
|
|
SHELL_CMD_ARG(setup, NULL,
|
|
|
|
"Setup DAC channel\n"
|
|
|
|
"Usage: setup <device> <channel> <resolution> [-b] [-i]\n"
|
|
|
|
"-b Enable output buffer\n"
|
|
|
|
"-i Connect internally",
|
|
|
|
cmd_setup, 4, 2),
|
|
|
|
SHELL_CMD_ARG(write_value, NULL,
|
|
|
|
"Write DAC value\n"
|
|
|
|
"Usage: write <device> <channel> <value>",
|
2020-02-17 03:57:20 +08:00
|
|
|
cmd_write_value, 4, 0),
|
2020-02-17 03:55:58 +08:00
|
|
|
SHELL_SUBCMD_SET_END
|
|
|
|
);
|
|
|
|
|
|
|
|
SHELL_CMD_REGISTER(dac, &dac_cmds, "DAC shell commands", NULL);
|