drivers: adc: Add ADC properties in Renesas RA ADC node

- Add "channel-available-mask" property in ADC node
to detect which channels are available to use

- Add "add-average-count" property in ADC node to chose
number of count of the addition or average mode

- Change the source code of ADC to match with 2 new properties.

Signed-off-by: Khoa Nguyen <khoa.nguyen.xh@renesas.com>
This commit is contained in:
Khoa Nguyen 2024-10-01 11:34:50 +07:00 committed by Benjamin Cabé
parent d54353bce8
commit e95d18587f
4 changed files with 42 additions and 19 deletions

View file

@ -238,6 +238,7 @@ mikrobus_serial: &uart3 {};
status = "okay";
pinctrl-0 = <&adc0_default>;
pinctrl-names = "default";
average-count = <4>;
};
&trng {

View file

@ -11,6 +11,7 @@
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/drivers/reset.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/util_internal.h>
#include <instances/r_adc.h>
#include <zephyr/irq.h>
@ -21,6 +22,11 @@ LOG_MODULE_REGISTER(adc_ra, CONFIG_ADC_LOG_LEVEL);
#include "adc_context.h"
#define ADC_RA_MAX_RESOLUTION 12
#define ADC_AVERAGE_1 ADC_ADD_OFF
#define ADC_AVERAGE_2 ADC_ADD_AVERAGE_TWO
#define ADC_AVERAGE_4 ADC_ADD_AVERAGE_FOUR
#define ADC_AVERAGE_8 ADC_ADD_AVERAGE_EIGHT
#define ADC_AVERAGE_16 ADC_ADD_AVERAGE_SIXTEEN
void adc_scan_end_isr(void);
@ -32,6 +38,8 @@ void adc_scan_end_isr(void);
struct adc_ra_config {
/** Number of supported channels */
uint8_t num_channels;
/** Mask for channels existed in each board */
uint32_t channel_available_mask;
/** pinctrl configs */
const struct pinctrl_dev_config *pcfg;
/** function pointer to irq setup */
@ -76,10 +84,9 @@ static int adc_ra_channel_setup(const struct device *dev, const struct adc_chann
{
fsp_err_t fsp_err = FSP_SUCCESS;
struct adc_ra_data *data = dev->data;
const struct adc_ra_config *config = dev->config;
if (!((channel_cfg->channel_id >= 0 && channel_cfg->channel_id <= 2) ||
(channel_cfg->channel_id >= 4 && channel_cfg->channel_id <= 8) ||
(channel_cfg->channel_id >= 16 && channel_cfg->channel_id <= 19))) {
if (!((config->channel_available_mask & (1 << channel_cfg->channel_id)) != 0)) {
LOG_ERR("unsupported channel id '%d'", channel_cfg->channel_id);
return -ENOTSUP;
}
@ -312,20 +319,6 @@ static int adc_ra_init(const struct device *dev)
return 0;
}
const adc_extended_cfg_t g_adc_cfg_extend = {
.add_average_count = ADC_ADD_OFF,
.clearing = ADC_CLEAR_AFTER_READ_ON,
.trigger_group_b = ADC_START_SOURCE_DISABLED,
.double_trigger_mode = ADC_DOUBLE_TRIGGER_DISABLED,
.adc_vref_control = ADC_VREF_CONTROL_VREFH,
.enable_adbuf = 0,
.window_a_irq = FSP_INVALID_VECTOR,
.window_a_ipl = (1),
.window_b_irq = FSP_INVALID_VECTOR,
.window_b_ipl = (BSP_IRQ_DISABLED),
.trigger = ADC_START_SOURCE_DISABLED, /* Use Software trigger */
};
#define IRQ_CONFIGURE_FUNC(idx) \
static void adc_ra_configure_func_##idx(void) \
{ \
@ -342,6 +335,19 @@ const adc_extended_cfg_t g_adc_cfg_extend = {
#define ADC_RA_INIT(idx) \
IRQ_CONFIGURE_FUNC(idx) \
PINCTRL_DT_INST_DEFINE(idx); \
static const adc_extended_cfg_t g_adc_cfg_extend_##idx = { \
.add_average_count = UTIL_CAT(ADC_AVERAGE_, DT_INST_PROP(idx, average_count)), \
.clearing = ADC_CLEAR_AFTER_READ_ON, \
.trigger_group_b = ADC_START_SOURCE_DISABLED, \
.double_trigger_mode = ADC_DOUBLE_TRIGGER_DISABLED, \
.adc_vref_control = ADC_VREF_CONTROL_VREFH, \
.enable_adbuf = 0, \
.window_a_irq = FSP_INVALID_VECTOR, \
.window_a_ipl = (1), \
.window_b_irq = FSP_INVALID_VECTOR, \
.window_b_ipl = (BSP_IRQ_DISABLED), \
.trigger = ADC_START_SOURCE_DISABLED, \
}; \
static DEVICE_API(adc, adc_ra_api_##idx) = { \
.channel_setup = adc_ra_channel_setup, \
.read = adc_ra_read, \
@ -349,6 +355,7 @@ const adc_extended_cfg_t g_adc_cfg_extend = {
IF_ENABLED(CONFIG_ADC_ASYNC, (.read_async = adc_ra_read_async))}; \
static const struct adc_ra_config adc_ra_config_##idx = { \
.num_channels = DT_INST_PROP(idx, channel_count), \
.channel_available_mask = DT_INST_PROP(idx, channel_available_mask), \
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(idx), \
IRQ_CONFIGURE_DEFINE(idx), \
}; \
@ -366,7 +373,7 @@ const adc_extended_cfg_t g_adc_cfg_extend = {
.trigger = 0, \
.p_callback = NULL, \
.p_context = NULL, \
.p_extend = &g_adc_cfg_extend, \
.p_extend = &g_adc_cfg_extend_##idx, \
.scan_end_irq = DT_INST_IRQ_BY_NAME(idx, scanend, irq), \
.scan_end_ipl = DT_INST_IRQ_BY_NAME(idx, scanend, priority), \
.scan_end_b_irq = FSP_INVALID_VECTOR, \
@ -377,7 +384,7 @@ const adc_extended_cfg_t g_adc_cfg_extend = {
.scan_mask = 0, \
.scan_mask_group_b = 0, \
.priority_group_a = ADC_GROUP_A_PRIORITY_OFF, \
.add_mask = 0, \
.add_mask = UINT16_MAX, \
.sample_hold_mask = 0, \
.sample_hold_states = 24, \
.p_window_cfg = NULL, \

View file

@ -289,6 +289,7 @@
#io-channel-cells = <1>;
vref-mv = <3300>;
channel-count = <12>;
channel-available-mask = <0xf01f7>;
status = "disabled";
};
@ -300,6 +301,7 @@
#io-channel-cells = <1>;
vref-mv = <3300>;
channel-count = <13>;
channel-available-mask = <0x7f0077>;
status = "disabled";
};

View file

@ -24,5 +24,18 @@ properties:
"#io-channel-cells":
const: 1
channel-available-mask:
type: int
required: true
description: Mask for ADC channels existed in each board
average-count:
type: int
default: 1
enum: [1, 2, 4, 8, 16]
description: The conversion count of the average mode
The ADC module will take multiple samples of an analog signal
and averages them to reduce noise and enhance accuracy
io-channel-cells:
- input