From fa874db8a5892b5e8fc034de8f1b5745affc671d Mon Sep 17 00:00:00 2001 From: Gerard Marull-Paretas Date: Wed, 23 Mar 2022 14:52:50 +0100 Subject: [PATCH] drivers: flash: shell: improve flash device choice Store an optional reference to the zephyr,flash-controller choice. If available and no user input is provided, it will be used as the default flash device. If not available, error message will be more explicit. Signed-off-by: Gerard Marull-Paretas --- drivers/flash/flash_shell.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/drivers/flash/flash_shell.c b/drivers/flash/flash_shell.c index 6c79d78a0e9..922c7d07103 100644 --- a/drivers/flash/flash_shell.c +++ b/drivers/flash/flash_shell.c @@ -6,6 +6,7 @@ */ #include +#include #include #include @@ -18,11 +19,8 @@ #define BUF_ARRAY_CNT 16 #define TEST_ARR_SIZE 0x1000 -#ifdef DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL -#define FLASH_DEV_NAME DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL -#else -#define FLASH_DEV_NAME "" -#endif +static const struct device *zephyr_flash_controller = + DEVICE_DT_GET_OR_NULL(DT_CHOSEN(zephyr_flash_controller)); static uint8_t __aligned(4) test_arr[TEST_ARR_SIZE]; @@ -33,12 +31,27 @@ static int parse_helper(const struct shell *shell, size_t *argc, char *endptr; *addr = strtoul((*argv)[1], &endptr, 16); - *flash_dev = device_get_binding((*endptr != '\0') ? (*argv)[1] : - FLASH_DEV_NAME); - if (!*flash_dev) { - shell_error(shell, "Flash driver was not found!"); + + if (*endptr != '\0') { + /* flash controller from user input */ + *flash_dev = device_get_binding((*argv)[1]); + if (!*flash_dev) { + shell_error(shell, "Given flash device was not found"); + return -ENODEV; + } + } else if (zephyr_flash_controller != NULL) { + /* default to zephyr,flash-controller */ + if (!device_is_ready(zephyr_flash_controller)) { + shell_error(shell, "Default flash driver not ready"); + return -ENODEV; + } + *flash_dev = zephyr_flash_controller; + } else { + /* no flash controller given, no default available */ + shell_error(shell, "No flash device specified (required)"); return -ENODEV; } + if (*endptr == '\0') { return 0; }