Skip to content

Commit f5bf295

Browse files
drivers: adc: add support sar adc driver
Add driver for the SAR ADC Signed-off-by: Qiang Zhao <qiang.zhao@nxp.com>
1 parent 732a3a5 commit f5bf295

File tree

5 files changed

+277
-0
lines changed

5 files changed

+277
-0
lines changed

drivers/adc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ zephyr_library_sources_ifdef(CONFIG_ADC_MCUX_ADC12 adc_mcux_adc12.c)
1313
zephyr_library_sources_ifdef(CONFIG_ADC_MCUX_ADC16 adc_mcux_adc16.c)
1414
zephyr_library_sources_ifdef(CONFIG_ADC_MCUX_12B1MSPS_SAR adc_mcux_12b1msps_sar.c)
1515
zephyr_library_sources_ifdef(CONFIG_ADC_MCUX_LPADC adc_mcux_lpadc.c)
16+
zephyr_library_sources_ifdef(CONFIG_ADC_MCUX_SAR_ADC adc_mcux_sar_adc.c)
1617
zephyr_library_sources_ifdef(CONFIG_ADC_VF610 adc_vf610.c)
1718
zephyr_library_sources_ifdef(CONFIG_ADC_SAM_AFEC adc_sam_afec.c)
1819
zephyr_library_sources_ifdef(CONFIG_ADC_NRFX_ADC adc_nrfx_adc.c)

drivers/adc/Kconfig.mcux

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ config ADC_MCUX_LPADC
3737
help
3838
Enable the MCUX LPADC driver.
3939

40+
config ADC_MCUX_SAR_ADC
41+
bool "MCUX SAR ADC driver"
42+
default y
43+
select ADC_CONFIGURABLE_INPUTS
44+
depends on DT_HAS_NXP_SAR_ADC_ENABLED
45+
help
46+
Enable the MCUX SAR ADC driver.
47+
4048
if ADC_MCUX_12B1MSPS_SAR || ADC_MCUX_LPADC
4149
config ADC_MCUX_ETC
4250
bool "MCUX ADC ETC driver"
@@ -123,3 +131,18 @@ config LPADC_CHANNEL_COUNT
123131

124132

125133
endif # ADC_MCUX_LPADC
134+
135+
if ADC_MCUX_SAR_ADC
136+
137+
config SAR_ADC_CHANNEL_COUNT
138+
int "SAR_ADC channel count"
139+
default 8
140+
range 1 8
141+
help
142+
Amount of hardware command channels to use, reduce to save RAM.
143+
The user can reduce this value if their application uses fewer than
144+
8 ADC channels. This value corresponds to how many of the CMD
145+
registers can be configured within the ADC.
146+
147+
148+
endif # ADC_MCUX_SAR_ADC

drivers/adc/adc_mcux_sar_adc.c

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
/*
2+
* Copyright 2025 NXP
3+
*
4+
* Based on adc_mcux_sar_adc.c, which is:
5+
* Copyright 2023-2024 NXP
6+
* Copyright (c) 2020 Toby Firth
7+
*
8+
* SPDX-License-Identifier: Apache-2.0
9+
*/
10+
11+
#define DT_DRV_COMPAT nxp_sar_adc
12+
13+
#include <errno.h>
14+
#include <zephyr/drivers/adc.h>
15+
#include <zephyr/sys/util.h>
16+
#include <zephyr/drivers/clock_control.h>
17+
#include <zephyr/drivers/pinctrl.h>
18+
19+
#define LOG_LEVEL CONFIG_ADC_LOG_LEVEL
20+
#include <zephyr/logging/log.h>
21+
#include <zephyr/irq.h>
22+
#include <fsl_sar_adc.h>
23+
LOG_MODULE_REGISTER(adc_mcux_sar_adc);
24+
25+
#define ADC_CONTEXT_USES_KERNEL_TIMER
26+
#include "adc_context.h"
27+
28+
struct mcux_sar_adc_config {
29+
ADC_Type *base;
30+
void (*irq_config_func)(const struct device *dev);
31+
const struct device *clock_dev;
32+
clock_control_subsys_t clock_subsys;
33+
};
34+
35+
struct mcux_sar_adc_data {
36+
const struct device *dev;
37+
struct adc_context ctx;
38+
uint16_t *buffer;
39+
uint16_t *repeat_buffer;
40+
uint32_t channels;
41+
};
42+
43+
static int mcux_sar_adc_channel_setup(const struct device *dev,
44+
const struct adc_channel_cfg *channel_cfg)
45+
{
46+
/* User may configure maximum number of active channels */
47+
if (channel_cfg->channel_id >= CONFIG_SAR_ADC_CHANNEL_COUNT) {
48+
LOG_ERR("Channel %d is not valid", channel_cfg->channel_id);
49+
return -EINVAL;
50+
}
51+
52+
if (channel_cfg->acquisition_time != ADC_ACQ_TIME_DEFAULT) {
53+
LOG_ERR("Unsupported channel acquisition time");
54+
return -ENOTSUP;
55+
}
56+
57+
if (channel_cfg->gain != ADC_GAIN_1) {
58+
LOG_ERR("Unsupported channel gain %d", channel_cfg->gain);
59+
return -ENOTSUP;
60+
}
61+
62+
if (channel_cfg->reference != ADC_REF_INTERNAL) {
63+
LOG_ERR("Unsupported channel reference");
64+
return -ENOTSUP;
65+
}
66+
67+
return 0;
68+
}
69+
70+
static int mcux_sar_adc_start_read(const struct device *dev, const struct adc_sequence *sequence)
71+
{
72+
const struct mcux_sar_adc_config *config = dev->config;
73+
struct mcux_sar_adc_data *data = dev->data;
74+
ADC_Type *base = config->base;
75+
uint8_t channel_id;
76+
77+
if (sequence->resolution != 12) {
78+
LOG_ERR("Unsupported resolution %d", sequence->resolution);
79+
return -ENOTSUP;
80+
}
81+
82+
channel_id = CONFIG_SAR_ADC_CHANNEL_COUNT;
83+
while (channel_id-- > 0) {
84+
if (sequence->channels & BIT(channel_id)) {
85+
ADC_EnableSpecificChannelNormalConv(base, channel_id);
86+
} else {
87+
ADC_DisableSpecificChannelNormalConv(base, channel_id);
88+
}
89+
}
90+
91+
data->buffer = sequence->buffer;
92+
93+
adc_context_start_read(&data->ctx, sequence);
94+
int error = adc_context_wait_for_completion(&data->ctx);
95+
96+
return error;
97+
}
98+
99+
static int mcux_sar_adc_read_async(const struct device *dev, const struct adc_sequence *sequence,
100+
struct k_poll_signal *async)
101+
{
102+
struct mcux_sar_adc_data *data = dev->data;
103+
int error;
104+
105+
adc_context_lock(&data->ctx, async ? true : false, async);
106+
error = mcux_sar_adc_start_read(dev, sequence);
107+
adc_context_release(&data->ctx, error);
108+
109+
return error;
110+
}
111+
112+
static int mcux_sar_adc_read(const struct device *dev, const struct adc_sequence *sequence)
113+
{
114+
return mcux_sar_adc_read_async(dev, sequence, NULL);
115+
}
116+
117+
static void adc_context_start_sampling(struct adc_context *ctx)
118+
{
119+
struct mcux_sar_adc_data *data = CONTAINER_OF(ctx, struct mcux_sar_adc_data, ctx);
120+
const struct mcux_sar_adc_config *config = data->dev->config;
121+
ADC_Type *base = config->base;
122+
123+
data->channels = ctx->sequence.channels;
124+
data->repeat_buffer = data->buffer;
125+
126+
ADC_StartConvChain(base, kADC_NormalConvOneShotMode);
127+
}
128+
129+
static void adc_context_update_buffer_pointer(struct adc_context *ctx, bool repeat_sampling)
130+
{
131+
struct mcux_sar_adc_data *data = CONTAINER_OF(ctx, struct mcux_sar_adc_data, ctx);
132+
133+
if (repeat_sampling) {
134+
data->buffer = data->repeat_buffer;
135+
}
136+
}
137+
138+
static void mcux_sar_adc_isr(const struct device *dev)
139+
{
140+
const struct mcux_sar_adc_config *config = dev->config;
141+
struct mcux_sar_adc_data *data = dev->data;
142+
ADC_Type *base = config->base;
143+
adc_conv_result_t conv_result;
144+
uint16_t channel_id;
145+
146+
if (((ADC_GetConvIntStatus(base) & kADC_NormalConvChainEndIntFlag))) {
147+
ADC_ClearConvIntStatus(base, kADC_NormalConvChainEndIntFlag);
148+
}
149+
150+
for (channel_id = 0; channel_id < CONFIG_SAR_ADC_CHANNEL_COUNT; channel_id++) {
151+
if (ADC_GetChannelConvResult(base, &conv_result, channel_id)) {
152+
data->channels &= ~BIT(channel_id);
153+
*(data->buffer++) = conv_result.convData;
154+
if (data->channels == 0) {
155+
adc_context_on_sampling_done(&data->ctx, dev);
156+
}
157+
}
158+
}
159+
}
160+
161+
static int mcux_sar_adc_init(const struct device *dev)
162+
{
163+
const struct mcux_sar_adc_config *config = dev->config;
164+
struct mcux_sar_adc_data *data = dev->data;
165+
ADC_Type *base = config->base;
166+
adc_config_t adc_config;
167+
adc_calibration_config_t calibrationConfig;
168+
169+
ADC_GetDefaultConfig(&adc_config);
170+
ADC_Init(base, &adc_config);
171+
ADC_SetConvMode(base, kADC_NormalConvOneShotMode);
172+
ADC_EnableConvInt(base, (uint32_t)kADC_NormalConvChainEndIntEnable);
173+
174+
/* Do calibration to reduce or eliminate the various error contribution effects. */
175+
calibrationConfig.enableAverage = true;
176+
calibrationConfig.sampleTime = kADC_SampleTime22;
177+
#if (defined(FSL_FEATURE_ADC_HAS_CALBISTREG) && (FSL_FEATURE_ADC_HAS_CALBISTREG == 1U))
178+
calibrationConfig.averageSampleNumbers = kADC_AverageSampleNumbers32;
179+
#else
180+
calibrationConfig.averageSampleNumbers = kADC_AverageSampleNumbers512;
181+
#endif /* FSL_FEATURE_ADC_HAS_CALBISTREG */
182+
183+
if (!(ADC_DoCalibration(base, &calibrationConfig))) {
184+
LOG_WRN("Calibration failed.");
185+
}
186+
187+
config->irq_config_func(dev);
188+
data->dev = dev;
189+
190+
adc_context_unlock_unconditionally(&data->ctx);
191+
192+
return 0;
193+
}
194+
195+
static DEVICE_API(adc, mcux_sar_adc_driver_api) = {
196+
.channel_setup = mcux_sar_adc_channel_setup,
197+
.read = mcux_sar_adc_read,
198+
#ifdef CONFIG_ADC_ASYNC
199+
.read_async = mcux_sar_adc_read_async,
200+
#endif
201+
};
202+
203+
#define SAR_ADC_MCUX_INIT(n) \
204+
\
205+
static void mcux_sar_adc_config_func_##n(const struct device *dev); \
206+
\
207+
static const struct mcux_sar_adc_config mcux_sar_adc_config_##n = { \
208+
.base = (ADC_Type *)DT_INST_REG_ADDR(n), \
209+
.irq_config_func = mcux_sar_adc_config_func_##n, \
210+
.clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)), \
211+
.clock_subsys = (clock_control_subsys_t)DT_INST_CLOCKS_CELL(n, name), \
212+
}; \
213+
static struct mcux_sar_adc_data mcux_sar_adc_data_##n = { \
214+
ADC_CONTEXT_INIT_TIMER(mcux_sar_adc_data_##n, ctx), \
215+
ADC_CONTEXT_INIT_LOCK(mcux_sar_adc_data_##n, ctx), \
216+
ADC_CONTEXT_INIT_SYNC(mcux_sar_adc_data_##n, ctx), \
217+
}; \
218+
\
219+
DEVICE_DT_INST_DEFINE(n, &mcux_sar_adc_init, NULL, &mcux_sar_adc_data_##n, \
220+
&mcux_sar_adc_config_##n, POST_KERNEL, CONFIG_ADC_INIT_PRIORITY, \
221+
&mcux_sar_adc_driver_api); \
222+
\
223+
static void mcux_sar_adc_config_func_##n(const struct device *dev) \
224+
{ \
225+
IRQ_CONNECT(DT_INST_IRQN(n), DT_INST_IRQ(n, priority), mcux_sar_adc_isr, \
226+
DEVICE_DT_INST_GET(n), 0); \
227+
\
228+
irq_enable(DT_INST_IRQN(n)); \
229+
}
230+
231+
DT_INST_FOREACH_STATUS_OKAY(SAR_ADC_MCUX_INIT)

dts/bindings/adc/nxp,sar-adc.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2025 NXP
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: NXP SAR ADC controller
5+
6+
compatible: "nxp,sar-adc"
7+
8+
include: [adc-controller.yaml]
9+
10+
properties:
11+
reg:
12+
required: true
13+
14+
interrupts:
15+
required: true
16+
17+
"#io-channel-cells":
18+
const: 1
19+
20+
io-channel-cells:
21+
- input

modules/hal_nxp/mcux/mcux-sdk-ng/drivers/drivers.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ endif()
1515
set_variable_ifdef(CONFIG_HWINFO_MCUX_SRC_V2 CONFIG_MCUX_COMPONENT_driver.src_2)
1616
set_variable_ifdef(CONFIG_GPIO_MCUX_IGPIO CONFIG_MCUX_COMPONENT_driver.igpio)
1717
set_variable_ifdef(CONFIG_ADC_MCUX_LPADC CONFIG_MCUX_COMPONENT_driver.lpadc)
18+
set_variable_ifdef(CONFIG_ADC_MCUX_SAR_ADC CONFIG_MCUX_COMPONENT_driver.sar_adc)
1819
set_variable_ifdef(CONFIG_COUNTER_MCUX_CTIMER CONFIG_MCUX_COMPONENT_driver.ctimer)
1920
set_variable_ifdef(CONFIG_COUNTER_MCUX_LPC_RTC CONFIG_MCUX_COMPONENT_driver.lpc_rtc)
2021
set_variable_ifdef(CONFIG_GLIKEY_MCUX_GLIKEY CONFIG_MCUX_COMPONENT_driver.glikey)

0 commit comments

Comments
 (0)