Skip to content

Commit b9d9b60

Browse files
committed
[nrf fromtree] drivers: adc: Update adc driver to support nRF54H20 device
Expands driver to cover nRF54H20 features like 8bit sample width. Signed-off-by: Karol Lasończyk <[email protected]> (cherry picked from commit 13196ec)
1 parent f4c1869 commit b9d9b60

File tree

1 file changed

+66
-14
lines changed

1 file changed

+66
-14
lines changed

drivers/adc/adc_nrfx_saadc.c

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "adc_context.h"
99
#include <haly/nrfy_saadc.h>
1010
#include <zephyr/dt-bindings/adc/nrf-adc.h>
11+
#include <zephyr/linker/devicetree_regions.h>
1112

1213
#define LOG_LEVEL CONFIG_ADC_LOG_LEVEL
1314
#include <zephyr/logging/log.h>
@@ -18,6 +19,18 @@ LOG_MODULE_REGISTER(adc_nrfx_saadc);
1819

1920
#if (NRF_SAADC_HAS_AIN_AS_PIN)
2021

22+
#if defined(CONFIG_SOC_NRF54H20)
23+
static const uint8_t saadc_psels[NRF_SAADC_AIN7 + 1] = {
24+
[NRF_SAADC_AIN0] = NRF_PIN_PORT_TO_PIN_NUMBER(0U, 1),
25+
[NRF_SAADC_AIN1] = NRF_PIN_PORT_TO_PIN_NUMBER(1U, 1),
26+
[NRF_SAADC_AIN2] = NRF_PIN_PORT_TO_PIN_NUMBER(2U, 1),
27+
[NRF_SAADC_AIN3] = NRF_PIN_PORT_TO_PIN_NUMBER(3U, 1),
28+
[NRF_SAADC_AIN4] = NRF_PIN_PORT_TO_PIN_NUMBER(4U, 1),
29+
[NRF_SAADC_AIN5] = NRF_PIN_PORT_TO_PIN_NUMBER(5U, 1),
30+
[NRF_SAADC_AIN6] = NRF_PIN_PORT_TO_PIN_NUMBER(6U, 1),
31+
[NRF_SAADC_AIN7] = NRF_PIN_PORT_TO_PIN_NUMBER(7U, 1),
32+
};
33+
#elif defined(CONFIG_SOC_NRF54L15)
2134
static const uint8_t saadc_psels[NRF_SAADC_AIN7 + 1] = {
2235
[NRF_SAADC_AIN0] = NRF_PIN_PORT_TO_PIN_NUMBER(4U, 1),
2336
[NRF_SAADC_AIN1] = NRF_PIN_PORT_TO_PIN_NUMBER(5U, 1),
@@ -28,6 +41,7 @@ static const uint8_t saadc_psels[NRF_SAADC_AIN7 + 1] = {
2841
[NRF_SAADC_AIN6] = NRF_PIN_PORT_TO_PIN_NUMBER(13U, 1),
2942
[NRF_SAADC_AIN7] = NRF_PIN_PORT_TO_PIN_NUMBER(14U, 1),
3043
};
44+
#endif
3145

3246
#else
3347
BUILD_ASSERT((NRF_SAADC_AIN0 == NRF_SAADC_INPUT_AIN0) &&
@@ -38,7 +52,6 @@ BUILD_ASSERT((NRF_SAADC_AIN0 == NRF_SAADC_INPUT_AIN0) &&
3852
(NRF_SAADC_AIN5 == NRF_SAADC_INPUT_AIN5) &&
3953
(NRF_SAADC_AIN6 == NRF_SAADC_INPUT_AIN6) &&
4054
(NRF_SAADC_AIN7 == NRF_SAADC_INPUT_AIN7) &&
41-
(NRF_SAADC_AIN7 == NRF_SAADC_INPUT_AIN7) &&
4255
#if defined(SAADC_CH_PSELP_PSELP_VDDHDIV5)
4356
(NRF_SAADC_VDDHDIV5 == NRF_SAADC_INPUT_VDDHDIV5) &&
4457
#endif
@@ -49,16 +62,43 @@ BUILD_ASSERT((NRF_SAADC_AIN0 == NRF_SAADC_INPUT_AIN0) &&
4962
"Definitions from nrf-adc.h do not match those from nrf_saadc.h");
5063
#endif
5164

65+
#ifdef CONFIG_SOC_NRF54H20
66+
67+
/* nRF54H20 always uses bounce buffers in RAM */
68+
69+
#define SAADC_MEMORY_SECTION \
70+
COND_CODE_1(DT_NODE_HAS_PROP(DT_NODELABEL(adc), memory_regions), \
71+
(__attribute__((__section__(LINKER_DT_NODE_REGION_NAME( \
72+
DT_PHANDLE(DT_NODELABEL(adc), memory_regions)))))), \
73+
())
74+
75+
static uint16_t adc_samples_buffer[SAADC_CH_NUM] SAADC_MEMORY_SECTION;
76+
77+
#define ADC_BUFFER_IN_RAM
78+
79+
#endif /* CONFIG_SOC_NRF54H20 */
80+
5281
struct driver_data {
5382
struct adc_context ctx;
5483

5584
uint8_t positive_inputs[SAADC_CH_NUM];
85+
86+
#if defined(ADC_BUFFER_IN_RAM)
87+
void *samples_buffer;
88+
void *user_buffer;
89+
uint8_t active_channels;
90+
#endif
5691
};
5792

5893
static struct driver_data m_data = {
5994
ADC_CONTEXT_INIT_TIMER(m_data, ctx),
6095
ADC_CONTEXT_INIT_LOCK(m_data, ctx),
6196
ADC_CONTEXT_INIT_SYNC(m_data, ctx),
97+
#if defined(ADC_BUFFER_IN_RAM)
98+
.samples_buffer = adc_samples_buffer,
99+
.user_buffer = NULL,
100+
.active_channels = 0,
101+
#endif
62102
};
63103

64104
/* Helper function to convert number of samples to the byte representation. */
@@ -279,20 +319,15 @@ static void adc_context_update_buffer_pointer(struct adc_context *ctx,
279319
ARG_UNUSED(ctx);
280320

281321
if (!repeat) {
282-
nrf_saadc_value_t *buffer;
283-
284-
#if (NRF_SAADC_8BIT_SAMPLE_WIDTH == 8)
285-
if (nrfy_saadc_resolution_get(NRF_SAADC) == NRF_SAADC_RESOLUTION_8BIT) {
286-
buffer = (uint8_t *)nrfy_saadc_buffer_pointer_get(NRF_SAADC) +
287-
nrfy_saadc_amount_get(NRF_SAADC);
288-
} else
289-
#endif
290-
{
291-
buffer = (uint16_t *)nrfy_saadc_buffer_pointer_get(NRF_SAADC) +
292-
nrfy_saadc_amount_get(NRF_SAADC);
293-
}
294-
322+
#if defined(ADC_BUFFER_IN_RAM)
323+
m_data.user_buffer = (uint8_t *)m_data.user_buffer +
324+
samples_to_bytes(&ctx->sequence, nrfy_saadc_amount_get(NRF_SAADC));
325+
#else
326+
nrf_saadc_value_t *buffer =
327+
(uint8_t *)nrf_saadc_buffer_pointer_get(NRF_SAADC) +
328+
samples_to_bytes(&ctx->sequence, nrfy_saadc_amount_get(NRF_SAADC));
295329
nrfy_saadc_buffer_pointer_set(NRF_SAADC, buffer);
330+
#endif
296331
}
297332
}
298333

@@ -466,13 +501,23 @@ static int start_read(const struct device *dev,
466501
return error;
467502
}
468503

504+
#if defined(ADC_BUFFER_IN_RAM)
505+
m_data.user_buffer = sequence->buffer;
506+
m_data.active_channels = active_channels;
507+
508+
nrf_saadc_buffer_init(NRF_SAADC,
509+
(nrf_saadc_value_t *)m_data.samples_buffer,
510+
active_channels);
511+
#else
469512
nrf_saadc_buffer_init(NRF_SAADC,
470513
(nrf_saadc_value_t *)sequence->buffer,
471514
active_channels);
515+
#endif
472516

473517
adc_context_start_read(&m_data.ctx, sequence);
474518

475519
error = adc_context_wait_for_completion(&m_data.ctx);
520+
476521
return error;
477522
}
478523

@@ -513,6 +558,11 @@ static void saadc_irq_handler(const struct device *dev)
513558
nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_STOP);
514559
nrf_saadc_disable(NRF_SAADC);
515560

561+
#if defined(ADC_BUFFER_IN_RAM)
562+
memcpy(m_data.user_buffer, m_data.samples_buffer,
563+
samples_to_bytes(&m_data.ctx.sequence, m_data.active_channels));
564+
#endif
565+
516566
adc_context_on_sampling_done(&m_data.ctx, dev);
517567
} else if (nrf_saadc_event_check(NRF_SAADC,
518568
NRF_SAADC_EVENT_CALIBRATEDONE)) {
@@ -553,6 +603,8 @@ static const struct adc_driver_api adc_nrfx_driver_api = {
553603
#endif
554604
#if defined(CONFIG_SOC_NRF54L15)
555605
.ref_internal = 900,
606+
#elif defined(CONFIG_SOC_NRF54H20)
607+
.ref_internal = 1024,
556608
#else
557609
.ref_internal = 600,
558610
#endif

0 commit comments

Comments
 (0)