Skip to content

Commit 0190ed2

Browse files
cyliangtwfabiobaltieri
authored andcommitted
drivers: adc: support Nuvoton numaker m2l31x
Update m2l31x.dtsi for adc support and update adc_numaker.c to support acquisition time in 0~255 ADC ticks. Signed-off-by: cyliang tw <cyliang@nuvoton.com>
1 parent 3216254 commit 0190ed2

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

drivers/adc/adc_numaker.c

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <NuMicro.h>
1818

1919
#define ADC_CONTEXT_USES_KERNEL_TIMER
20+
#define ADC_CONTEXT_ENABLE_ON_COMPLETE
2021
#include "adc_context.h"
2122

2223
LOG_MODULE_REGISTER(adc_numaker, CONFIG_ADC_LOG_LEVEL);
@@ -45,6 +46,7 @@ struct adc_numaker_data {
4546
uint16_t *repeat_buffer;
4647
bool is_differential;
4748
uint32_t channels;
49+
uint32_t acq_time;
4850
};
4951

5052
static int adc_numaker_channel_setup(const struct device *dev,
@@ -54,9 +56,13 @@ static int adc_numaker_channel_setup(const struct device *dev,
5456
struct adc_numaker_data *data = dev->data;
5557

5658
if (chan_cfg->acquisition_time != ADC_ACQ_TIME_DEFAULT) {
57-
LOG_ERR("Not support acquisition time");
58-
return -ENOTSUP;
59+
if ((ADC_ACQ_TIME_UNIT(chan_cfg->acquisition_time) != ADC_ACQ_TIME_TICKS) ||
60+
(ADC_ACQ_TIME_VALUE(chan_cfg->acquisition_time) > 255)) {
61+
LOG_ERR("Selected ADC acquisition time is not in 0~255 ticks");
62+
return -EINVAL;
63+
}
5964
}
65+
data->acq_time = ADC_ACQ_TIME_VALUE(chan_cfg->acquisition_time);
6066

6167
if (chan_cfg->gain != ADC_GAIN_1) {
6268
LOG_ERR("Not support channel gain");
@@ -115,9 +121,6 @@ static void adc_numaker_isr(const struct device *dev)
115121
uint16_t conv_data;
116122
uint32_t pend_flag;
117123

118-
/* Clear pending flag first */
119-
pend_flag = eadc->PENDSTS;
120-
eadc->PENDSTS = pend_flag;
121124
LOG_DBG("ADC ISR pend flag: 0x%X\n", pend_flag);
122125
LOG_DBG("ADC ISR STATUS2[0x%x] STATUS3[0x%x]", eadc->STATUS2, eadc->STATUS3);
123126
/* Complete the conversion of channels.
@@ -151,9 +154,6 @@ static void adc_numaker_isr(const struct device *dev)
151154
eadc->SCTL[module_id] = 0;
152155
}
153156

154-
/* Disable ADC */
155-
EADC_Close(eadc);
156-
157157
/* Inform sampling is done */
158158
adc_context_on_sampling_done(&data->ctx, data->dev);
159159
}
@@ -179,6 +179,8 @@ static void m_adc_numaker_start_scan(const struct device *dev)
179179
channel_mask &= ~BIT(channel_id);
180180
EADC_ConfigSampleModule(eadc, module_id,
181181
EADC_SOFTWARE_TRIGGER, channel_id);
182+
/* Set sample module external sampling time to 0 */
183+
EADC_SetExtendSampleTime(eadc, module_id, data->acq_time);
182184
}
183185

184186
/* Clear the A/D ADINT0 interrupt flag for safe */
@@ -221,6 +223,19 @@ static void adc_context_update_buffer_pointer(struct adc_context *ctx,
221223
}
222224
}
223225

226+
static void adc_context_on_complete(struct adc_context *ctx, int status)
227+
{
228+
struct adc_numaker_data *data =
229+
CONTAINER_OF(ctx, struct adc_numaker_data, ctx);
230+
const struct adc_numaker_config *cfg = data->dev->config;
231+
EADC_T *eadc = cfg->eadc_base;
232+
233+
ARG_UNUSED(status);
234+
235+
/* Disable ADC */
236+
EADC_Close(eadc);
237+
}
238+
224239
static int m_adc_numaker_start_read(const struct device *dev,
225240
const struct adc_sequence *sequence)
226241
{
@@ -243,14 +258,9 @@ static int m_adc_numaker_start_read(const struct device *dev,
243258

244259
/* Enable the A/D converter */
245260
if (data->is_differential) {
246-
err = EADC_Open(eadc, EADC_CTL_DIFFEN_DIFFERENTIAL);
261+
EADC_Open(eadc, EADC_CTL_DIFFEN_DIFFERENTIAL);
247262
} else {
248-
err = EADC_Open(eadc, EADC_CTL_DIFFEN_SINGLE_END);
249-
}
250-
251-
if (err) {
252-
LOG_ERR("ADC Open fail (%u)", err);
253-
return -ENODEV;
263+
EADC_Open(eadc, EADC_CTL_DIFFEN_SINGLE_END);
254264
}
255265

256266
data->buffer = sequence->buffer;

dts/arm/nuvoton/m2l31x.dtsi

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <zephyr/dt-bindings/reset/numaker_m2l31x_reset.h>
1212
#include <zephyr/dt-bindings/gpio/gpio.h>
1313
#include <zephyr/dt-bindings/i2c/i2c.h>
14+
#include <zephyr/dt-bindings/adc/adc.h>
1415

1516
/ {
1617
chosen {
@@ -306,6 +307,19 @@
306307
#address-cells = <1>;
307308
#size-cells = <0>;
308309
};
310+
311+
eadc0: eadc@40043000 {
312+
compatible = "nuvoton,numaker-adc";
313+
reg = <0x40043000 0xffc>;
314+
interrupts = <42 0>;
315+
resets = <&rst NUMAKER_EADC0_RST>;
316+
clocks = <&pcc NUMAKER_EADC0_MODULE
317+
NUMAKER_CLK_CLKSEL0_EADC0SEL_HCLK
318+
NUMAKER_CLK_CLKDIV0_EADC0(2)>;
319+
channels = <31>;
320+
status = "disabled";
321+
#io-channel-cells = <1>;
322+
};
309323
};
310324
};
311325

0 commit comments

Comments
 (0)