Skip to content

Commit 87b982d

Browse files
gmarullcarlescufi
authored andcommitted
drivers: adc: shell: use DEVICE_DT_GET
ADC instances can be obtained at compile time, so use DEVICE_DT_GET. Signed-off-by: Gerard Marull-Paretas <[email protected]>
1 parent 26ff5a3 commit 87b982d

File tree

1 file changed

+32
-48
lines changed

1 file changed

+32
-48
lines changed

drivers/adc/adc_shell.c

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ LOG_MODULE_REGISTER(adc_shell);
7979
#define CMD_HELP_GAIN "Configure gain.\n"
8080
#define CMD_HELP_PRINT "Print current configuration"
8181

82-
#define NODE_LABELS(n) DT_INST_LABEL(n),
83-
#define ADC_HDL_LIST_ENTRY(label) \
82+
#define DEVICES(n) DEVICE_DT_INST_GET(n),
83+
#define ADC_HDL_LIST_ENTRY(dev_) \
8484
{ \
85-
.device_label = label, \
85+
.dev = dev_, \
8686
.channel_config = { \
8787
.gain = ADC_GAIN_1, \
8888
.reference = ADC_REF_INTERNAL, \
@@ -92,15 +92,15 @@ LOG_MODULE_REGISTER(adc_shell);
9292
.resolution = 0, \
9393
}
9494

95-
#define INIT_MACRO() DT_INST_FOREACH_STATUS_OKAY(NODE_LABELS) "NA"
95+
#define INIT_MACRO() DT_INST_FOREACH_STATUS_OKAY(DEVICES) NULL
9696

9797
#define CHOSEN_STR_LEN 20
9898
static char chosen_reference[CHOSEN_STR_LEN + 1] = "INTERNAL";
9999
static char chosen_gain[CHOSEN_STR_LEN + 1] = "1";
100100

101101
/* This table size is = ADC devices count + 1 (NA). */
102102
static struct adc_hdl {
103-
char *device_label;
103+
const struct device *dev;
104104
struct adc_channel_cfg channel_config;
105105
uint8_t resolution;
106106
} adc_list[] = {
@@ -110,7 +110,7 @@ static struct adc_hdl {
110110
static struct adc_hdl *get_adc(const char *device_label)
111111
{
112112
for (int i = 0; i < ARRAY_SIZE(adc_list); i++) {
113-
if (!strcmp(device_label, adc_list[i].device_label)) {
113+
if (!strcmp(device_label, adc_list[i].dev->name)) {
114114
return &adc_list[i];
115115
}
116116
}
@@ -124,12 +124,10 @@ static int cmd_adc_ch_id(const struct shell *shell, size_t argc, char **argv)
124124
{
125125
/* -2: index of ADC label name */
126126
struct adc_hdl *adc = get_adc(argv[-2]);
127-
const struct device *adc_dev;
128127
int retval = 0;
129128

130-
adc_dev = device_get_binding(adc->device_label);
131-
if (adc_dev == NULL) {
132-
shell_error(shell, "ADC device not found");
129+
if (!device_is_ready(adc->dev)) {
130+
shell_error(shell, "ADC device not ready");
133131
return -ENODEV;
134132
}
135133

@@ -139,7 +137,7 @@ static int cmd_adc_ch_id(const struct shell *shell, size_t argc, char **argv)
139137
}
140138

141139
adc->channel_config.channel_id = (uint8_t)strtol(argv[1], NULL, 10);
142-
retval = adc_channel_setup(adc_dev, &adc->channel_config);
140+
retval = adc_channel_setup(adc->dev, &adc->channel_config);
143141
LOG_DBG("Channel setup returned %i\n", retval);
144142

145143
return retval;
@@ -150,12 +148,10 @@ static int cmd_adc_ch_neg(const struct shell *shell, size_t argc, char **argv)
150148
#if CONFIG_ADC_CONFIGURABLE_INPUTS
151149
/* -2: index of ADC label name */
152150
struct adc_hdl *adc = get_adc(argv[-2]);
153-
const struct device *adc_dev;
154151
int retval = 0;
155152

156-
adc_dev = device_get_binding(adc->device_label);
157-
if (adc_dev == NULL) {
158-
shell_error(shell, "ADC device not found");
153+
if (!device_is_ready(adc->dev)) {
154+
shell_error(shell, "ADC device not ready");
159155
return -ENODEV;
160156
}
161157

@@ -165,7 +161,7 @@ static int cmd_adc_ch_neg(const struct shell *shell, size_t argc, char **argv)
165161
}
166162

167163
adc->channel_config.input_negative = (uint8_t)strtol(argv[1], NULL, 10);
168-
retval = adc_channel_setup(adc_dev, &adc->channel_config);
164+
retval = adc_channel_setup(adc->dev, &adc->channel_config);
169165
LOG_DBG("Channel setup returned %i\n", retval);
170166

171167
return retval;
@@ -179,12 +175,10 @@ static int cmd_adc_ch_pos(const struct shell *shell, size_t argc, char **argv)
179175
#if CONFIG_ADC_CONFIGURABLE_INPUTS
180176
/* -2: index of ADC label name */
181177
struct adc_hdl *adc = get_adc(argv[-2]);
182-
const struct device *adc_dev;
183178
int retval = 0;
184179

185-
adc_dev = device_get_binding(adc->device_label);
186-
if (adc_dev == NULL) {
187-
shell_error(shell, "ADC device not found");
180+
if (!device_is_ready(adc->dev)) {
181+
shell_error(shell, "ADC device not ready");
188182
return -ENODEV;
189183
}
190184

@@ -194,7 +188,7 @@ static int cmd_adc_ch_pos(const struct shell *shell, size_t argc, char **argv)
194188
}
195189

196190
adc->channel_config.input_positive = (uint8_t)strtol(argv[1], NULL, 10);
197-
retval = adc_channel_setup(adc_dev, &adc->channel_config);
191+
retval = adc_channel_setup(adc->dev, &adc->channel_config);
198192
LOG_DBG("Channel setup returned %i\n", retval);
199193

200194
return retval;
@@ -209,12 +203,10 @@ static int cmd_adc_gain(const struct shell *shell, size_t argc, char **argv,
209203
/* -2: index of ADC label name */
210204
struct adc_hdl *adc = get_adc(argv[-2]);
211205
enum adc_gain gain = (enum adc_gain)data;
212-
const struct device *adc_dev;
213206
int retval = -EINVAL;
214207

215-
adc_dev = device_get_binding(adc->device_label);
216-
if (adc_dev == NULL) {
217-
shell_error(shell, "ADC device not found");
208+
if (!device_is_ready(adc->dev)) {
209+
shell_error(shell, "ADC device not ready");
218210
return -ENODEV;
219211
}
220212

@@ -223,7 +215,7 @@ static int cmd_adc_gain(const struct shell *shell, size_t argc, char **argv,
223215
: strlen(argv[0]);
224216
memcpy(chosen_gain, argv[0], len);
225217
chosen_gain[len] = '\0';
226-
retval = adc_channel_setup(adc_dev, &adc->channel_config);
218+
retval = adc_channel_setup(adc->dev, &adc->channel_config);
227219
LOG_DBG("Channel setup returned %i\n", retval);
228220

229221
return retval;
@@ -233,13 +225,11 @@ static int cmd_adc_acq(const struct shell *shell, size_t argc, char **argv)
233225
{
234226
/* -1 index of ADC label name */
235227
struct adc_hdl *adc = get_adc(argv[-1]);
236-
const struct device *adc_dev;
237228
uint16_t acq_time;
238229
int retval;
239230

240-
adc_dev = device_get_binding(adc->device_label);
241-
if (adc_dev == NULL) {
242-
shell_error(shell, "ADC device not found");
231+
if (!device_is_ready(adc->dev)) {
232+
shell_error(shell, "ADC device not ready");
243233
return -ENODEV;
244234
}
245235

@@ -262,7 +252,7 @@ static int cmd_adc_acq(const struct shell *shell, size_t argc, char **argv)
262252
adc->channel_config.acquisition_time =
263253
ADC_ACQ_TIME_DEFAULT;
264254
}
265-
retval = adc_channel_setup(adc_dev, &adc->channel_config);
255+
retval = adc_channel_setup(adc->dev, &adc->channel_config);
266256
LOG_DBG("Channel setup returned %i\n", retval);
267257

268258
return retval;
@@ -271,12 +261,10 @@ static int cmd_adc_reso(const struct shell *shell, size_t argc, char **argv)
271261
{
272262
/* -1 index of ADC label name */
273263
struct adc_hdl *adc = get_adc(argv[-1]);
274-
const struct device *adc_dev;
275264
int retval;
276265

277-
adc_dev = device_get_binding(adc->device_label);
278-
if (adc_dev == NULL) {
279-
shell_error(shell, "ADC device not found");
266+
if (!device_is_ready(adc->dev)) {
267+
shell_error(shell, "ADC device not ready");
280268
return -ENODEV;
281269
}
282270

@@ -286,7 +274,7 @@ static int cmd_adc_reso(const struct shell *shell, size_t argc, char **argv)
286274
}
287275

288276
adc->resolution = (uint8_t)strtol(argv[1], NULL, 10);
289-
retval = adc_channel_setup(adc_dev, &adc->channel_config);
277+
retval = adc_channel_setup(adc->dev, &adc->channel_config);
290278

291279
return retval;
292280
}
@@ -297,12 +285,10 @@ static int cmd_adc_ref(const struct shell *shell, size_t argc, char **argv,
297285
/* -2 index of ADC label name */
298286
struct adc_hdl *adc = get_adc(argv[-2]);
299287
enum adc_reference reference = (enum adc_reference)data;
300-
const struct device *adc_dev;
301288
int retval = -EINVAL;
302289

303-
adc_dev = device_get_binding(adc->device_label);
304-
if (adc_dev == NULL) {
305-
shell_error(shell, "ADC device not found");
290+
if (!device_is_ready(adc->dev)) {
291+
shell_error(shell, "ADC device not ready");
306292
return -ENODEV;
307293
}
308294

@@ -312,7 +298,7 @@ static int cmd_adc_ref(const struct shell *shell, size_t argc, char **argv,
312298
chosen_reference[len] = '\0';
313299

314300
adc->channel_config.reference = reference;
315-
retval = adc_channel_setup(adc_dev, &adc->channel_config);
301+
retval = adc_channel_setup(adc->dev, &adc->channel_config);
316302
LOG_DBG("Channel setup returned %i\n", retval);
317303

318304
return retval;
@@ -325,12 +311,10 @@ static int cmd_adc_read(const struct shell *shell, size_t argc, char **argv)
325311
/* -1 index of adc label name */
326312
struct adc_hdl *adc = get_adc(argv[-1]);
327313
uint16_t m_sample_buffer[BUFFER_SIZE];
328-
const struct device *adc_dev;
329314
int retval;
330315

331-
adc_dev = device_get_binding(adc->device_label);
332-
if (adc_dev == NULL) {
333-
shell_error(shell, "adc device not found");
316+
if (!device_is_ready(adc->dev)) {
317+
shell_error(shell, "ADC device not ready");
334318
return -ENODEV;
335319
}
336320

@@ -342,7 +326,7 @@ static int cmd_adc_read(const struct shell *shell, size_t argc, char **argv)
342326
.resolution = adc->resolution,
343327
};
344328

345-
retval = adc_read(adc_dev, &sequence);
329+
retval = adc_read(adc->dev, &sequence);
346330
if (retval >= 0) {
347331
shell_print(shell, "read: %i", m_sample_buffer[0]);
348332
}
@@ -361,7 +345,7 @@ static int cmd_adc_print(const struct shell *shell, size_t argc, char **argv)
361345
"Acquisition Time: %u\n"
362346
"Channel ID: %u\n"
363347
"Resolution: %u",
364-
adc->device_label,
348+
adc->dev->name,
365349
chosen_gain,
366350
chosen_reference,
367351
adc->channel_config.acquisition_time,
@@ -422,7 +406,7 @@ static void cmd_adc_dev_get(size_t idx, struct shell_static_entry *entry)
422406
{
423407
/* -1 because the last element in the list is a "list terminator" */
424408
if (idx < ARRAY_SIZE(adc_list) - 1) {
425-
entry->syntax = adc_list[idx].device_label;
409+
entry->syntax = adc_list[idx].dev->name;
426410
entry->handler = NULL;
427411
entry->subcmd = &sub_adc_cmds;
428412
entry->help = "Select subcommand for ADC property label.\n";

0 commit comments

Comments
 (0)