From da676955b5bb57e4de2b5e77ca499f77d022c856 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Chru=C5=9Bci=C5=84ski?= Date: Thu, 20 Mar 2025 13:22:10 +0100 Subject: [PATCH 1/3] [nrf fromlist] drivers: adc: nrfx_saadc: Add validation of channel configuration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On nrf54h20 there are additional analog pins (AIN8+). When differential mode is used they must not be mixed with AIN0-AIN7. Add build time validation which detects if configuration is invalid. Upstream PR #: 87405 Signed-off-by: Krzysztof Chruściński --- drivers/adc/adc_nrfx_saadc.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/drivers/adc/adc_nrfx_saadc.c b/drivers/adc/adc_nrfx_saadc.c index 22a839a5f35..5692f910cbb 100644 --- a/drivers/adc/adc_nrfx_saadc.c +++ b/drivers/adc/adc_nrfx_saadc.c @@ -719,6 +719,29 @@ static DEVICE_API(adc, adc_nrfx_driver_api) = { #endif }; +#ifdef NRF_SAADC_AIN8 +/* AIN8-AIN14 inputs are on 3v3 GPIO port and they cannot be mixed with other + * analog inputs (from 1v8 ports) in differential mode. + */ +#define CH_IS_3V3(val) (val >= NRF_SAADC_AIN8) + +#define CH_IS_DIFF(node) DT_PROP(node, zephyr_differential) + +#define CH_MIX_3V3_1V8_CHECK(node) \ + (!CH_IS_DIFF(node) || \ + !(CH_IS_3V3(DT_PROP_OR(node, zephyr_input_positive, 0)) ^ \ + CH_IS_3V3(DT_PROP_OR(node, zephyr_input_negative, 0)))) +#else +#define CH_MIX_3V3_1V8_CHECK(node) true +#endif + +#define VALIDATE_CHANNEL_CONFIG(node) \ + BUILD_ASSERT(CH_MIX_3V3_1V8_CHECK(node) == true, \ + "1v8 inputs cannot be mixed with 3v3 inputs"); + +/* Validate configuration of all channels. */ +#define VALIDATE_CHANNELS_CONFIG(inst) DT_FOREACH_CHILD(DT_DRV_INST(inst), VALIDATE_CHANNEL_CONFIG) + /* * There is only one instance on supported SoCs, so inst is guaranteed * to be 0 if any instance is okay. (We use adc_0 above, so the driver @@ -731,6 +754,7 @@ static DEVICE_API(adc, adc_nrfx_driver_api) = { #define SAADC_INIT(inst) \ BUILD_ASSERT((inst) == 0, \ "multiple instances not supported"); \ + VALIDATE_CHANNELS_CONFIG(inst) \ PM_DEVICE_DT_INST_DEFINE(0, saadc_pm_hook, 1); \ DEVICE_DT_INST_DEFINE(0, \ init_saadc, \ From a717bc0aebe5bbecffc2348e413144dbc3217ab3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Chru=C5=9Bci=C5=84ski?= Date: Thu, 20 Mar 2025 13:49:57 +0100 Subject: [PATCH 2/3] [nrf fromlist] samples: drivers: adc: adc_sequence: Do not overwrite vref_mv MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sample is reading vref-mv property to get reference voltage and then if reference source is set to ADC_REF_INTERNAL this value is overwritten. If vref-mv property is provided then it should not be overwritten. Upstream PR #: 87405 Signed-off-by: Krzysztof Chruściński --- samples/drivers/adc/adc_sequence/src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/drivers/adc/adc_sequence/src/main.c b/samples/drivers/adc/adc_sequence/src/main.c index 60bde60650d..084dbf428f3 100644 --- a/samples/drivers/adc/adc_sequence/src/main.c +++ b/samples/drivers/adc/adc_sequence/src/main.c @@ -60,7 +60,7 @@ int main(void) printf("Could not setup channel #%d (%d)\n", i, err); return 0; } - if (channel_cfgs[i].reference == ADC_REF_INTERNAL) { + if ((vrefs_mv[i] == 0) && (channel_cfgs[i].reference == ADC_REF_INTERNAL)) { vrefs_mv[i] = adc_ref_internal(adc); } } From 26bcb5956c1ddbce8423d2716e9fee9d8cfd8aa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Chru=C5=9Bci=C5=84ski?= Date: Thu, 20 Mar 2025 13:52:43 +0100 Subject: [PATCH 3/3] [nrf fromlist] samples: drivers: adc: adc_sequence: Fix nrf54h20dk configuration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AIN9 source needs special handling (all from AIN8-AIN14). Conversion result needs to be scaled up by 3.6 additionally to gain and reference voltage. In order to achieve correct value in the sample vref-mv is used as reference voltage instead of predefined internal reference source. Upstream PR #: 87405 Signed-off-by: Krzysztof Chruściński --- .../adc/adc_sequence/boards/nrf54h20dk_nrf54h20_cpuapp.overlay | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/drivers/adc/adc_sequence/boards/nrf54h20dk_nrf54h20_cpuapp.overlay b/samples/drivers/adc/adc_sequence/boards/nrf54h20dk_nrf54h20_cpuapp.overlay index bf53616d928..24cce679375 100644 --- a/samples/drivers/adc/adc_sequence/boards/nrf54h20dk_nrf54h20_cpuapp.overlay +++ b/samples/drivers/adc/adc_sequence/boards/nrf54h20dk_nrf54h20_cpuapp.overlay @@ -45,6 +45,7 @@ zephyr,reference = "ADC_REF_INTERNAL"; zephyr,acquisition-time = ; zephyr,input-positive = ; /* P9.01 */ + zephyr,vref-mv = <3686>; /* 3.6V * 1024 */ zephyr,resolution = <12>; zephyr,oversampling = <8>; };