Skip to content

Commit 53c07be

Browse files
committed
[nrf fromlist] drivers: adc: nrfx: enable negative values for single-ended ADC readings
The ADC driver API already supports ADC readings which can return signed values, these are differential readings. In Nordic's datasheet, we have a mode called "single ended", but its just a name. "Single ended" is a differential reading, with the negative channel tied to GND. This is not compatible with zephyrs definition of a single ended reading. To support Nordic's "single ended" mode, the user must configure a differential reading, with the negative input tied to ground, which the saadc driver can then use to configure the reading as Nordic SAADC "single ended", and return negative values as expected. Upstream PR #: 94069 Signed-off-by: Jakub Zymelka <[email protected]>
1 parent 64848a4 commit 53c07be

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

drivers/adc/adc_nrfx_saadc.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,21 +201,24 @@ static int input_assign(nrf_saadc_input_t *pin_p,
201201

202202
if (channel_cfg->differential) {
203203
if (channel_cfg->input_negative > ARRAY_SIZE(saadc_psels) ||
204-
channel_cfg->input_negative < NRF_SAADC_AIN0 ||
205204
(IS_ENABLED(CONFIG_NRF_PLATFORM_HALTIUM) &&
206205
(channel_cfg->input_positive > NRF_SAADC_AIN7) !=
207206
(channel_cfg->input_negative > NRF_SAADC_AIN7))) {
208207
LOG_ERR("Invalid analog negative input number: %d",
209208
channel_cfg->input_negative);
210209
return -EINVAL;
211210
}
212-
*pin_n = saadc_psels[channel_cfg->input_negative];
211+
*pin_n = channel_cfg->input_negative == NRF_SAADC_GND ?
212+
NRF_SAADC_INPUT_DISABLED :
213+
saadc_psels[channel_cfg->input_negative];
213214
} else {
214215
*pin_n = NRF_SAADC_INPUT_DISABLED;
215216
}
216217
#else
217218
*pin_p = channel_cfg->input_positive;
218-
*pin_n = channel_cfg->differential ? channel_cfg->input_negative
219+
*pin_n = channel_cfg->differential ? (channel_cfg->input_negative == NRF_SAADC_GND
220+
? NRF_SAADC_INPUT_DISABLED
221+
: channel_cfg->input_negative)
219222
: NRF_SAADC_INPUT_DISABLED;
220223
#endif
221224
LOG_DBG("ADC positive input: %d", *pin_p);
@@ -366,8 +369,14 @@ static int adc_nrfx_channel_setup(const struct device *dev,
366369
* after ADC sequence ends.
367370
*/
368371
if (channel_cfg->differential) {
369-
ch_cfg->mode = NRF_SAADC_MODE_DIFFERENTIAL;
370-
m_data.single_ended_channels &= ~BIT(channel_cfg->channel_id);
372+
if (channel_cfg->input_negative == NRF_SAADC_GND) {
373+
ch_cfg->mode = NRF_SAADC_MODE_SINGLE_ENDED;
374+
/* Do not mark as single-ended to not correct negative values. */
375+
m_data.single_ended_channels &= ~BIT(channel_cfg->channel_id);
376+
} else {
377+
ch_cfg->mode = NRF_SAADC_MODE_DIFFERENTIAL;
378+
m_data.single_ended_channels &= ~BIT(channel_cfg->channel_id);
379+
}
371380
} else {
372381
ch_cfg->mode = NRF_SAADC_MODE_SINGLE_ENDED;
373382
m_data.single_ended_channels |= BIT(channel_cfg->channel_id);

include/zephyr/dt-bindings/adc/nrf-saadc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#ifndef ZEPHYR_INCLUDE_DT_BINDINGS_ADC_NRF_SAADC_H_
88
#define ZEPHYR_INCLUDE_DT_BINDINGS_ADC_NRF_SAADC_H_
99

10+
#define NRF_SAADC_GND 0
1011
#define NRF_SAADC_AIN0 1
1112
#define NRF_SAADC_AIN1 2
1213
#define NRF_SAADC_AIN2 3

0 commit comments

Comments
 (0)