Skip to content

Commit 7692e3d

Browse files
FPlohlkartben
authored andcommitted
drivers: dac: add driver for the NXP DAC12
Add driver shim for the NXP Digital-to-Analog (DAC12) module. Signed-off-by: Florijan Plohl <florijan.plohl@norik.com>
1 parent a7ad18d commit 7692e3d

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

drivers/dac/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ zephyr_library()
66

77
zephyr_library_sources_ifdef(CONFIG_DAC_MCUX_LPDAC dac_mcux_lpdac.c)
88
zephyr_library_sources_ifdef(CONFIG_DAC_MCUX_DAC dac_mcux_dac.c)
9+
zephyr_library_sources_ifdef(CONFIG_DAC_MCUX_DAC12 dac_mcux_dac12.c)
910
zephyr_library_sources_ifdef(CONFIG_DAC_MCUX_DAC32 dac_mcux_dac32.c)
1011
zephyr_library_sources_ifdef(CONFIG_DAC_STM32 dac_stm32.c)
1112
zephyr_library_sources_ifdef(CONFIG_DAC_SAM dac_sam.c)

drivers/dac/Kconfig.mcux

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ config DAC_MCUX_DAC
1111
help
1212
Enable the driver for the NXP Kinetis MCUX DAC.
1313

14+
config DAC_MCUX_DAC12
15+
bool "NXP MCUX DAC12 driver"
16+
default y
17+
depends on DT_HAS_NXP_DAC12_ENABLED
18+
help
19+
Enable the driver for the NXP MCUX DAC12.
20+
1421
config DAC_MCUX_DAC32
1522
bool "NXP Kinetis MCUX DAC32 driver"
1623
default y

drivers/dac/dac_mcux_dac12.c

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright (c) 2020 Henrik Brix Andersen <henrik@brixandersen.dk>
3+
* Copyright (c) 2023, NXP
4+
* Copyright (c) 2025 PHYTEC America LLC
5+
*
6+
* SPDX-License-Identifier: Apache-2.0
7+
*/
8+
9+
#define DT_DRV_COMPAT nxp_dac12
10+
11+
#include <zephyr/kernel.h>
12+
#include <zephyr/drivers/dac.h>
13+
#include <zephyr/logging/log.h>
14+
15+
#include <fsl_dac12.h>
16+
17+
LOG_MODULE_REGISTER(dac_mcux_dac12, CONFIG_DAC_LOG_LEVEL);
18+
19+
struct mcux_dac12_config {
20+
DAC_Type *base;
21+
dac12_reference_voltage_source_t reference;
22+
bool buffered;
23+
};
24+
25+
struct mcux_dac12_data {
26+
bool configured;
27+
};
28+
29+
static int mcux_dac12_channel_setup(const struct device *dev,
30+
const struct dac_channel_cfg *channel_cfg)
31+
{
32+
const struct mcux_dac12_config *config = dev->config;
33+
struct mcux_dac12_data *data = dev->data;
34+
dac12_config_t dac12_config;
35+
36+
if (channel_cfg->channel_id != 0) {
37+
LOG_ERR("unsupported channel %d", channel_cfg->channel_id);
38+
return -ENOTSUP;
39+
}
40+
41+
if (channel_cfg->resolution != 12) {
42+
LOG_ERR("unsupported resolution %d", channel_cfg->resolution);
43+
return -ENOTSUP;
44+
}
45+
46+
if (channel_cfg->internal) {
47+
LOG_ERR("Internal channels not supported");
48+
return -ENOTSUP;
49+
}
50+
51+
DAC12_GetDefaultConfig(&dac12_config);
52+
dac12_config.referenceVoltageSource = config->reference;
53+
54+
DAC12_Init(config->base, &dac12_config);
55+
DAC12_Enable(config->base, true);
56+
57+
data->configured = true;
58+
59+
return 0;
60+
}
61+
62+
static int mcux_dac12_write_value(const struct device *dev, uint8_t channel, uint32_t value)
63+
{
64+
const struct mcux_dac12_config *config = dev->config;
65+
struct mcux_dac12_data *data = dev->data;
66+
67+
if (!data->configured) {
68+
LOG_ERR("channel not initialized");
69+
return -EINVAL;
70+
}
71+
72+
if (channel != 0) {
73+
LOG_ERR("unsupported channel %d", channel);
74+
return -ENOTSUP;
75+
}
76+
77+
if (value >= 4096) {
78+
LOG_ERR("value %d out of range", value);
79+
return -EINVAL;
80+
}
81+
82+
DAC12_SetData(config->base, value);
83+
84+
return 0;
85+
}
86+
87+
static const struct dac_driver_api mcux_dac12_driver_api = {
88+
.channel_setup = mcux_dac12_channel_setup,
89+
.write_value = mcux_dac12_write_value,
90+
};
91+
92+
#define TO_DAC12_VREF_SRC(val) _DO_CONCAT(kDAC12_ReferenceVoltageSourceAlt, val)
93+
94+
#define MCUX_DAC12_INIT(n) \
95+
static struct mcux_dac12_data mcux_dac12_data_##n; \
96+
\
97+
static const struct mcux_dac12_config mcux_dac12_config_##n = { \
98+
.base = (DAC_Type *)DT_INST_REG_ADDR(n), \
99+
.reference = \
100+
TO_DAC12_VREF_SRC(DT_INST_PROP(n, voltage_reference)), \
101+
.buffered = DT_INST_PROP(n, buffered), \
102+
}; \
103+
\
104+
DEVICE_DT_INST_DEFINE(n, NULL, NULL, \
105+
&mcux_dac12_data_##n, \
106+
&mcux_dac12_config_##n, \
107+
POST_KERNEL, CONFIG_DAC_INIT_PRIORITY, \
108+
&mcux_dac12_driver_api);
109+
110+
DT_INST_FOREACH_STATUS_OKAY(MCUX_DAC12_INIT)

dts/bindings/dac/nxp,dac12.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright (c) 2020 Henrik Brix Andersen <henrik@brixandersen.dk>
2+
# Copyright (c) 2025 PHYTEC America LLC
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
description: NXP MCUX DAC12
6+
7+
compatible: "nxp,dac12"
8+
9+
include: dac-controller.yaml
10+
11+
properties:
12+
reg:
13+
required: true
14+
15+
voltage-reference:
16+
type: int
17+
required: true
18+
description: DAC voltage reference select
19+
20+
buffered:
21+
type: boolean
22+
description: Enable output buffer
23+
24+
"#io-channel-cells":
25+
const: 1
26+
27+
io-channel-cells:
28+
- output

0 commit comments

Comments
 (0)