Skip to content

Commit 0f0757c

Browse files
committed
[nrf fromlist] drivers: adc: nrfx: enable negative values for single-ended ADC readings
Add custom nordic-specific ADC API to enable to read negative values from a single-ended ADC channels. Upstream PR #: 91704 Signed-off-by: Jakub Zymelka <[email protected]>
1 parent 0736723 commit 0f0757c

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

drivers/adc/adc_nrfx_saadc.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ struct driver_data {
110110
bool enable_internal_timer;
111111
#endif
112112
bool internal_timer_enabled;
113+
bool correct_single_ended;
113114
};
114115

115116
static struct driver_data m_data = {
@@ -121,6 +122,7 @@ static struct driver_data m_data = {
121122
.enable_internal_timer = DT_INST_PROP(0, enable_internal_timer),
122123
#endif
123124
.internal_timer_enabled = false,
125+
.correct_single_ended = true,
124126
};
125127

126128
/* Maximum value of the internal timer interval in microseconds. */
@@ -749,6 +751,20 @@ static int adc_nrfx_read(const struct device *dev,
749751
int error;
750752

751753
adc_context_lock(&m_data.ctx, false, NULL);
754+
m_data.correct_single_ended = true;
755+
error = start_read(dev, sequence);
756+
adc_context_release(&m_data.ctx, error);
757+
758+
return error;
759+
}
760+
761+
int adc_nrf_read_single_ended_negative_results(const struct device *dev,
762+
const struct adc_sequence *sequence)
763+
{
764+
int error;
765+
766+
adc_context_lock(&m_data.ctx, false, NULL);
767+
m_data.correct_single_ended = false;
752768
error = start_read(dev, sequence);
753769
adc_context_release(&m_data.ctx, error);
754770

@@ -764,6 +780,7 @@ static int adc_nrfx_read_async(const struct device *dev,
764780
int error;
765781

766782
adc_context_lock(&m_data.ctx, true, async);
783+
m_data.correct_single_ended = true;
767784
error = start_read(dev, sequence);
768785
adc_context_release(&m_data.ctx, error);
769786

@@ -786,9 +803,11 @@ static void event_handler(const nrfx_saadc_evt_t *event)
786803
m_data.active_channel_cnt)),
787804
event->data.done.p_buffer);
788805

789-
if (has_single_ended(&m_data.ctx.sequence)) {
790-
correct_single_ended(&m_data.ctx.sequence, m_data.user_buffer,
791-
event->data.done.size);
806+
if (m_data.correct_single_ended) {
807+
if (has_single_ended(&m_data.ctx.sequence)) {
808+
correct_single_ended(&m_data.ctx.sequence, m_data.user_buffer,
809+
event->data.done.size);
810+
}
792811
}
793812
adc_context_on_sampling_done(&m_data.ctx, DEVICE_DT_INST_GET(0));
794813
} else if (event->type == NRFX_SAADC_EVT_CALIBRATEDONE) {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_INCLUDE_DRIVERS_ADC_ADC_NRFX_H_
8+
#define ZEPHYR_INCLUDE_DRIVERS_ADC_ADC_NRFX_H_
9+
10+
#include <zephyr/types.h>
11+
#include <zephyr/drivers/adc.h>
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
/**
18+
* @brief Set a read request.
19+
*
20+
* @param dev Pointer to the device structure for the driver instance.
21+
* @param sequence Structure specifying requested sequence of samplings.
22+
*
23+
* The function works identically to adc_read(), but does not correct
24+
* negative measurement results in single-ended mode.
25+
*
26+
* If invoked from user mode, any sequence struct options for callback must
27+
* be NULL.
28+
*
29+
* @retval 0 On success.
30+
* @retval -EINVAL If a parameter with an invalid value has been provided.
31+
* @retval -ENOMEM If the provided buffer is too small to hold the results
32+
* of all requested samplings.
33+
* @retval -ENOTSUP If the requested mode of operation is not supported.
34+
* @retval -EBUSY If another sampling was triggered while the previous one
35+
* was still in progress. This may occur only when samplings
36+
* are done with intervals, and it indicates that the selected
37+
* interval was too small. All requested samples are written
38+
* in the buffer, but at least some of them were taken with
39+
* an extra delay compared to what was scheduled.
40+
*/
41+
int adc_nrf_read_single_ended_negative_results(const struct device *dev,
42+
const struct adc_sequence *sequence);
43+
44+
#ifdef __cplusplus
45+
}
46+
#endif
47+
48+
#endif /* ZEPHYR_INCLUDE_DRIVERS_ADC_ADC_NRFX_H_ */

0 commit comments

Comments
 (0)