Skip to content

Commit 2f17bd8

Browse files
mnkpnashif
authored andcommitted
drivers: Add Atmel SAM DAC (DACC) driver
Add Digital-to-Analog Converter driver (based on DACC module) for Atmel SAM MCU family. Only SAME70, SAMV71 series devices are supported in this version. Tested on Atmel SMART SAM E70 Xplained board. Origin: Original Signed-off-by: Piotr Mienkowski <[email protected]>
1 parent cd294c1 commit 2f17bd8

File tree

6 files changed

+237
-0
lines changed

6 files changed

+237
-0
lines changed

drivers/dac/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ zephyr_library()
55
zephyr_library_sources_ifdef(CONFIG_DAC_MCUX_DAC dac_mcux_dac.c)
66
zephyr_library_sources_ifdef(CONFIG_DAC_MCUX_DAC32 dac_mcux_dac32.c)
77
zephyr_library_sources_ifdef(CONFIG_DAC_STM32 dac_stm32.c)
8+
zephyr_library_sources_ifdef(CONFIG_DAC_SAM dac_sam.c)
89
zephyr_library_sources_ifdef(CONFIG_DAC_SAM0 dac_sam0.c)
910
zephyr_library_sources_ifdef(CONFIG_DAC_DACX0508 dac_dacx0508.c)
1011
zephyr_library_sources_ifdef(CONFIG_DAC_DACX3608 dac_dacx3608.c)

drivers/dac/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ source "drivers/dac/Kconfig.mcux"
2828

2929
source "drivers/dac/Kconfig.stm32"
3030

31+
source "drivers/dac/Kconfig.sam"
32+
3133
source "drivers/dac/Kconfig.sam0"
3234

3335
source "drivers/dac/Kconfig.dacx0508"

drivers/dac/Kconfig.sam

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) 2021 Piotr Mienkowski
2+
# SPDX-License-Identifier: Apache-2.0
3+
#
4+
5+
# Workaround for not being able to have commas in macro arguments
6+
DT_COMPAT_ATMEL_SAM_DAC := atmel,sam-dac
7+
8+
config DAC_SAM
9+
bool "Atmel SAM DAC driver"
10+
default $(dt_compat_enabled,$(DT_COMPAT_ATMEL_SAM_DAC))
11+
depends on SOC_FAMILY_SAM
12+
help
13+
Enable Atmel SAM MCU Family Digital Audio Converter (DAC) driver.

drivers/dac/dac_sam.c

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/*
2+
* Copyright (c) 2021 Piotr Mienkowski
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
/** @file
7+
* @brief DAC driver for Atmel SAM MCU family.
8+
*
9+
* Remarks:
10+
* Only SAME70, SAMV71 series devices are currently supported. Please submit a
11+
* patch.
12+
*/
13+
14+
#define DT_DRV_COMPAT atmel_sam_dac
15+
16+
#include <errno.h>
17+
#include <sys/__assert.h>
18+
#include <soc.h>
19+
#include <device.h>
20+
#include <drivers/dac.h>
21+
22+
#include <logging/log.h>
23+
LOG_MODULE_REGISTER(dac_sam, CONFIG_DAC_LOG_LEVEL);
24+
25+
BUILD_ASSERT(IS_ENABLED(CONFIG_SOC_SERIES_SAME70) ||
26+
IS_ENABLED(CONFIG_SOC_SERIES_SAMV71),
27+
"Only SAME70, SAMV71 series devices are currently supported.");
28+
29+
#define DAC_CHANNEL_NO 2
30+
31+
/* Device constant configuration parameters */
32+
struct dac_sam_dev_cfg {
33+
Dacc *regs;
34+
void (*irq_config)(void);
35+
uint8_t irq_id;
36+
uint8_t periph_id;
37+
uint8_t prescaler;
38+
};
39+
40+
struct dac_channel {
41+
struct k_sem sem;
42+
};
43+
44+
/* Device run time data */
45+
struct dac_sam_dev_data {
46+
struct dac_channel dac_channels[DAC_CHANNEL_NO];
47+
};
48+
49+
#define DEV_NAME(dev) ((dev)->name)
50+
#define DEV_CFG(dev) \
51+
((const struct dac_sam_dev_cfg *const)(dev)->config)
52+
#define DEV_DATA(dev) \
53+
((struct dac_sam_dev_data *const)(dev)->data)
54+
55+
static void dac_sam_isr(void *arg)
56+
{
57+
const struct device *dev = (const struct device *)arg;
58+
const struct dac_sam_dev_cfg *const dev_cfg = DEV_CFG(dev);
59+
struct dac_sam_dev_data *const dev_data = DEV_DATA(dev);
60+
Dacc *const dac = dev_cfg->regs;
61+
uint32_t int_stat;
62+
63+
/* Retrieve interrupt status */
64+
int_stat = dac->DACC_ISR & dac->DACC_IMR;
65+
66+
if ((int_stat & DACC_ISR_TXRDY0) != 0) {
67+
/* Disable Transmit Ready Interrupt */
68+
dac->DACC_IDR = DACC_IDR_TXRDY0;
69+
k_sem_give(&dev_data->dac_channels[0].sem);
70+
}
71+
if ((int_stat & DACC_ISR_TXRDY1) != 0) {
72+
/* Disable Transmit Ready Interrupt */
73+
dac->DACC_IDR = DACC_IDR_TXRDY1;
74+
k_sem_give(&dev_data->dac_channels[1].sem);
75+
}
76+
}
77+
78+
static int dac_sam_channel_setup(const struct device *dev,
79+
const struct dac_channel_cfg *channel_cfg)
80+
{
81+
const struct dac_sam_dev_cfg *const dev_cfg = DEV_CFG(dev);
82+
Dacc *const dac = dev_cfg->regs;
83+
84+
if (channel_cfg->channel_id >= DAC_CHANNEL_NO) {
85+
return -EINVAL;
86+
}
87+
if (channel_cfg->resolution != 12) {
88+
return -ENOTSUP;
89+
}
90+
91+
/* Enable Channel */
92+
dac->DACC_CHER = DACC_CHER_CH0 << channel_cfg->channel_id;
93+
94+
return 0;
95+
}
96+
97+
static int dac_sam_write_value(const struct device *dev, uint8_t channel,
98+
uint32_t value)
99+
{
100+
struct dac_sam_dev_data *const dev_data = DEV_DATA(dev);
101+
const struct dac_sam_dev_cfg *const dev_cfg = DEV_CFG(dev);
102+
Dacc *const dac = dev_cfg->regs;
103+
104+
if (channel >= DAC_CHANNEL_NO) {
105+
return -EINVAL;
106+
}
107+
108+
if (dac->DACC_IMR & (DACC_IMR_TXRDY0 << channel)) {
109+
/* Attempting to send data on channel that's already in use */
110+
return -EINVAL;
111+
}
112+
113+
k_sem_take(&dev_data->dac_channels[channel].sem, K_FOREVER);
114+
115+
/* Trigger conversion */
116+
dac->DACC_CDR[channel] = DACC_CDR_DATA0(value);
117+
118+
/* Enable Transmit Ready Interrupt */
119+
dac->DACC_IER = DACC_IER_TXRDY0 << channel;
120+
121+
return 0;
122+
}
123+
124+
static int dac_sam_init(const struct device *dev)
125+
{
126+
const struct dac_sam_dev_cfg *const dev_cfg = DEV_CFG(dev);
127+
struct dac_sam_dev_data *const dev_data = DEV_DATA(dev);
128+
Dacc *const dac = dev_cfg->regs;
129+
130+
/* Configure interrupts */
131+
dev_cfg->irq_config();
132+
133+
/* Initialize semaphores */
134+
for (int i = 0; i < ARRAY_SIZE(dev_data->dac_channels); i++) {
135+
k_sem_init(&dev_data->dac_channels[i].sem, 1, 1);
136+
}
137+
138+
/* Enable DAC clock in PMC */
139+
soc_pmc_peripheral_enable(dev_cfg->periph_id);
140+
141+
/* Set Mode Register */
142+
dac->DACC_MR = DACC_MR_PRESCALER(dev_cfg->prescaler);
143+
144+
/* Enable module's IRQ */
145+
irq_enable(dev_cfg->irq_id);
146+
147+
LOG_INF("Device %s initialized", DEV_NAME(dev));
148+
149+
return 0;
150+
}
151+
152+
static const struct dac_driver_api dac_sam_driver_api = {
153+
.channel_setup = dac_sam_channel_setup,
154+
.write_value = dac_sam_write_value,
155+
};
156+
157+
/* DACC */
158+
159+
static void dacc_irq_config(void)
160+
{
161+
IRQ_CONNECT(DT_INST_IRQN(0), DT_INST_IRQ(0, priority), dac_sam_isr,
162+
DEVICE_DT_INST_GET(0), 0);
163+
}
164+
165+
static const struct dac_sam_dev_cfg dacc_sam_config = {
166+
.regs = (Dacc *)DT_INST_REG_ADDR(0),
167+
.irq_id = DT_INST_IRQN(0),
168+
.irq_config = dacc_irq_config,
169+
.periph_id = DT_INST_PROP(0, peripheral_id),
170+
.prescaler = DT_INST_PROP(0, prescaler),
171+
};
172+
173+
static struct dac_sam_dev_data dacc_sam_data;
174+
175+
DEVICE_DT_INST_DEFINE(0, dac_sam_init, NULL, &dacc_sam_data, &dacc_sam_config,
176+
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
177+
&dac_sam_driver_api);

dts/arm/atmel/same70.dtsi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,16 @@
238238
pinctrl-0 = <&pd9c_afec1_adtrg>;
239239
};
240240

241+
dacc: dacc@40040000 {
242+
compatible = "atmel,sam-dac";
243+
reg = <0x40040000 0x100>;
244+
interrupts = <30 0>;
245+
peripheral-id = <30>;
246+
status = "disabled";
247+
label = "DACC";
248+
#io-channel-cells = <1>;
249+
};
250+
241251
pinctrl@400e0e00 {
242252
compatible = "atmel,sam-pinctrl";
243253
#address-cells = <1>;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright (c) 2021 Piotr Mienkowski
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: Atmel SAM family DAC
5+
6+
compatible: "atmel,sam-dac"
7+
8+
include: dac-controller.yaml
9+
10+
properties:
11+
reg:
12+
required: true
13+
14+
peripheral-id:
15+
type: int
16+
required: true
17+
description: |
18+
peripheral ID
19+
20+
prescaler:
21+
type: int
22+
required: false
23+
default: 15
24+
description: |
25+
Peripheral Clock to DAC Clock Ratio. Prescaler value is calcuated as
26+
PRESCAL = (MCK / DACClock) - 2. Should be in range from 0 to 15. The
27+
value will be written to DACC_MR.PRESCALER bit-field. The property is
28+
applicable only to SAME70, SAMV71 series devices.
29+
30+
"#io-channel-cells":
31+
const: 1
32+
33+
io-channel-cells:
34+
- output

0 commit comments

Comments
 (0)