Skip to content

Commit a0cc2e9

Browse files
committed
[nrf fromlist] drivers: adc: nrf: add support for nrf54l internal SAADC inputs
SAADC peripheral for nRF54L Series allows to choose internal voltages as positive inputs. Upstream PR: zephyrproject-rtos/zephyr#79330 Signed-off-by: Nikodem Kastelik <[email protected]> (cherry picked from commit b74efe4cf82efbb3a518e69792d4d3e7c5a76415)
1 parent 09b9e02 commit a0cc2e9

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

drivers/adc/adc_nrfx_saadc.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
#define ADC_CONTEXT_USES_KERNEL_TIMER
88
#include "adc_context.h"
99
#include <haly/nrfy_saadc.h>
10-
#include <zephyr/dt-bindings/adc/nrf-adc.h>
10+
#include <zephyr/dt-bindings/adc/nrf-saadc-v3.h>
11+
#include <zephyr/dt-bindings/adc/nrf-saadc-nrf54l.h>
1112
#include <zephyr/linker/devicetree_regions.h>
1213

1314
#define LOG_LEVEL CONFIG_ADC_LOG_LEVEL
@@ -17,6 +18,8 @@ LOG_MODULE_REGISTER(adc_nrfx_saadc);
1718

1819
#define DT_DRV_COMPAT nordic_nrf_saadc
1920

21+
#define SAADC_INPUT_INVALID 0xFEU
22+
2023
#if (NRF_SAADC_HAS_AIN_AS_PIN)
2124

2225
#if defined(CONFIG_SOC_NRF54H20) || defined(CONFIG_SOC_NRF9280)
@@ -31,7 +34,7 @@ static const uint8_t saadc_psels[NRF_SAADC_AIN7 + 1] = {
3134
[NRF_SAADC_AIN7] = NRF_PIN_PORT_TO_PIN_NUMBER(7U, 1),
3235
};
3336
#elif defined(CONFIG_SOC_NRF54L15)
34-
static const uint8_t saadc_psels[NRF_SAADC_AIN7 + 1] = {
37+
static const uint32_t saadc_psels[NRF_SAADC_DVDD + 1] = {
3538
[NRF_SAADC_AIN0] = NRF_PIN_PORT_TO_PIN_NUMBER(4U, 1),
3639
[NRF_SAADC_AIN1] = NRF_PIN_PORT_TO_PIN_NUMBER(5U, 1),
3740
[NRF_SAADC_AIN2] = NRF_PIN_PORT_TO_PIN_NUMBER(6U, 1),
@@ -40,6 +43,9 @@ static const uint8_t saadc_psels[NRF_SAADC_AIN7 + 1] = {
4043
[NRF_SAADC_AIN5] = NRF_PIN_PORT_TO_PIN_NUMBER(12U, 1),
4144
[NRF_SAADC_AIN6] = NRF_PIN_PORT_TO_PIN_NUMBER(13U, 1),
4245
[NRF_SAADC_AIN7] = NRF_PIN_PORT_TO_PIN_NUMBER(14U, 1),
46+
[NRF_SAADC_VDD] = NRF_SAADC_INPUT_VDD,
47+
[NRF_SAADC_AVDD] = NRF_SAADC_INPUT_AVDD,
48+
[NRF_SAADC_DVDD] = NRF_SAADC_INPUT_DVDD,
4349
};
4450
#endif
4551

@@ -273,13 +279,8 @@ static int adc_nrfx_channel_setup(const struct device *dev,
273279
m_data.single_ended_channels |= BIT(channel_cfg->channel_id);
274280
}
275281

276-
/* Keep the channel disabled in hardware (set positive input to
277-
* NRF_SAADC_INPUT_DISABLED) until it is selected to be included
278-
* in a sampling sequence.
279-
*/
280-
281282
#if (NRF_SAADC_HAS_AIN_AS_PIN)
282-
if ((channel_cfg->input_positive > NRF_SAADC_AIN7) ||
283+
if ((channel_cfg->input_positive >= ARRAY_SIZE(saadc_psels)) ||
283284
(channel_cfg->input_positive < NRF_SAADC_AIN0)) {
284285
return -EINVAL;
285286
}
@@ -294,17 +295,18 @@ static int adc_nrfx_channel_setup(const struct device *dev,
294295
} else {
295296
input_negative = NRF_SAADC_INPUT_DISABLED;
296297
}
297-
298+
#endif
298299
/* Store the positive input selection in a dedicated array,
299300
* to get it later when the channel is selected for a sampling
300301
* and to mark the channel as configured (ready to be selected).
301302
*/
302-
m_data.positive_inputs[channel_id] = saadc_psels[channel_cfg->input_positive];
303-
#else
304303
m_data.positive_inputs[channel_id] = channel_cfg->input_positive;
305-
#endif
306304

307305
nrf_saadc_channel_init(NRF_SAADC, channel_id, &config);
306+
/* Keep the channel disabled in hardware (set positive input to
307+
* NRF_SAADC_INPUT_DISABLED) until it is selected to be included
308+
* in a sampling sequence.
309+
*/
308310
nrf_saadc_channel_input_set(NRF_SAADC,
309311
channel_id,
310312
NRF_SAADC_INPUT_DISABLED,
@@ -494,7 +496,7 @@ static int start_read(const struct device *dev,
494496
/* Signal an error if a selected channel has not been
495497
* configured yet.
496498
*/
497-
if (m_data.positive_inputs[channel_id] == 0U) {
499+
if (m_data.positive_inputs[channel_id] == SAADC_INPUT_INVALID) {
498500
LOG_ERR("Channel %u not configured",
499501
channel_id);
500502
return -EINVAL;
@@ -531,7 +533,12 @@ static int start_read(const struct device *dev,
531533
nrf_saadc_channel_pos_input_set(
532534
NRF_SAADC,
533535
channel_id,
534-
m_data.positive_inputs[channel_id]);
536+
#if NRF_SAADC_HAS_AIN_AS_PIN
537+
saadc_psels[m_data.positive_inputs[channel_id]]
538+
#else
539+
m_data.positive_inputs[channel_id]
540+
#endif
541+
);
535542
++active_channels;
536543
} else {
537544
nrf_saadc_burst_set(
@@ -642,6 +649,9 @@ static void saadc_irq_handler(const struct device *dev)
642649

643650
static int init_saadc(const struct device *dev)
644651
{
652+
for (uint8_t ch = 0; ch < ARRAY_SIZE(m_data.positive_inputs); ch++) {
653+
m_data.positive_inputs[ch] = SAADC_INPUT_INVALID;
654+
}
645655
nrf_saadc_event_clear(NRF_SAADC, NRF_SAADC_EVENT_END);
646656
nrf_saadc_event_clear(NRF_SAADC, NRF_SAADC_EVENT_CALIBRATEDONE);
647657
nrf_saadc_int_enable(NRF_SAADC,

0 commit comments

Comments
 (0)