Skip to content

Commit 59c7d57

Browse files
yishai1999kartben
authored andcommitted
shell: add function to get device by name or label
Added shell_device_get_binding() that wraps device_get_binding() plus device_get_by_dt_nodelabel() so that a shell can easily get a device by its full name or label. Signed-off-by: Yishai Jaffe <[email protected]>
1 parent d5ae99a commit 59c7d57

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

include/zephyr/shell/shell.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,28 @@ typedef bool (*shell_device_filter_t)(const struct device *dev);
159159
const struct device *shell_device_filter(size_t idx,
160160
shell_device_filter_t filter);
161161

162+
/**
163+
* @brief Get a @ref device reference from its @ref device.name field or label.
164+
*
165+
* This function iterates through the devices on the system. If a device with
166+
* the given @p name field is found, and that device initialized successfully at
167+
* boot time, this function returns a pointer to the device.
168+
*
169+
* If no device has the given @p name, this function returns `NULL`.
170+
*
171+
* This function also returns NULL when a device is found, but it failed to
172+
* initialize successfully at boot time. (To troubleshoot this case, set a
173+
* breakpoint on your device driver's initialization function.)
174+
*
175+
* @param name device name to search for. A null pointer, or a pointer to an
176+
* empty string, will cause NULL to be returned.
177+
*
178+
* @return pointer to device structure with the given name; `NULL` if the device
179+
* is not found or if the device with that name's initialization function
180+
* failed.
181+
*/
182+
const struct device *shell_device_get_binding(const char *name);
183+
162184
/**
163185
* @brief Shell command handler prototype.
164186
*

subsys/shell/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ config SHELL_MINIMAL
2525
defaults which favor reduced flash or memory requirements over extra
2626
features.
2727

28+
config SHELL_DEVICE_HELPERS
29+
bool "Shell device helpers"
30+
imply DEVICE_DT_METADATA
31+
default y if !SHELL_MINIMAL
32+
2833
config SHELL_THREAD_PRIORITY_OVERRIDE
2934
bool "Override default shell thread priority"
3035
help

subsys/shell/shell_utils.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,17 @@ const struct device *shell_device_lookup(size_t idx,
536536
return shell_device_internal(idx, prefix, NULL);
537537
}
538538

539+
const struct device *shell_device_get_binding(const char *name)
540+
{
541+
const struct device *dev = device_get_binding(name);
542+
543+
if (IS_ENABLED(CONFIG_DEVICE_DT_METADATA) && dev == NULL) {
544+
dev = device_get_by_dt_nodelabel(name);
545+
}
546+
547+
return dev;
548+
}
549+
539550
long shell_strtol(const char *str, int base, int *err)
540551
{
541552
long val;

0 commit comments

Comments
 (0)