diff --git a/drivers/include/mtd.h b/drivers/include/mtd.h index 6a638be274..eda7ded808 100644 --- a/drivers/include/mtd.h +++ b/drivers/include/mtd.h @@ -73,6 +73,7 @@ #ifndef MTD_H #define MTD_H +#include #include #include "xfa.h" @@ -517,6 +518,19 @@ int mtd_erase_sector(mtd_dev_t *mtd, uint32_t sector, uint32_t num); */ int mtd_power(mtd_dev_t *mtd, enum mtd_power_state power); +/** + * @brief Get an MTD device by index + * + * @param[in] idx Index of the MTD device + * + * @return MTD_0 for @p idx 0 and so on + * NULL if no MTD device exists for the given index + */ +static inline mtd_dev_t *mtd_dev_get(unsigned idx) +{ + return ((MTD_NUMOF != 0) && (idx < MTD_NUMOF)) ? mtd_dev_xfa[idx] : NULL; +} + #ifdef __cplusplus } #endif diff --git a/drivers/include/mtd_default.h b/drivers/include/mtd_default.h index 671218635a..0c90a1d477 100644 --- a/drivers/include/mtd_default.h +++ b/drivers/include/mtd_default.h @@ -69,6 +69,8 @@ extern mtd_emulated_t mtd_emulated_dev0; /** * @brief Get the default MTD device by index * + * @deprecated Use @ref mtd_dev_get instead + * * @param[in] idx Index of the MTD device * * @return MTD_0 for @p idx 0 and so on diff --git a/sys/include/usb/usbus/msc.h b/sys/include/usb/usbus/msc.h index 58e2e57d2c..9670530831 100644 --- a/sys/include/usb/usbus/msc.h +++ b/sys/include/usb/usbus/msc.h @@ -25,7 +25,7 @@ #include #include "usb/usbus.h" #include "usb/usbus/msc/scsi.h" -#include "mtd_default.h" +#include "mtd.h" #ifdef __cplusplus extern "C" { diff --git a/sys/usb/usbus/msc/msc.c b/sys/usb/usbus/msc/msc.c index dec3581360..7a3fb84fc6 100644 --- a/sys/usb/usbus/msc/msc.c +++ b/sys/usb/usbus/msc/msc.c @@ -374,7 +374,7 @@ static void _init(usbus_t *usbus, usbus_handler_t *handler) /* Auto-configure all MTD devices */ if (CONFIG_USBUS_MSC_AUTO_MTD) { for (unsigned i = 0; i < MTD_NUMOF; i++) { - usbus_msc_add_lun(usbus, mtd_default_get_dev(i)); + usbus_msc_add_lun(usbus, mtd_dev_get(i)); } } } diff --git a/tests/drivers/mtd_raw/main.c b/tests/drivers/mtd_raw/main.c index 8f10f9f11c..1252f28e86 100644 --- a/tests/drivers/mtd_raw/main.c +++ b/tests/drivers/mtd_raw/main.c @@ -25,7 +25,7 @@ #include #include "od.h" -#include "mtd_default.h" +#include "mtd.h" #include "shell.h" #include "board.h" #include "macros/units.h" @@ -45,7 +45,7 @@ static mtd_dev_t *_get_dev(int argc, char **argv) return NULL; } - return mtd_default_get_dev(idx); + return mtd_dev_get(idx); } static uint64_t _get_size(mtd_dev_t *dev) @@ -283,7 +283,7 @@ static int cmd_info(int argc, char **argv) for (unsigned i = 0; i < MTD_NUMOF; ++i) { printf(" -=[ MTD_%d ]=-\n", i); - _print_info(mtd_default_get_dev(i)); + _print_info(mtd_dev_get(i)); } return 0; } @@ -453,7 +453,7 @@ int main(void) for (unsigned i = 0; i < MTD_NUMOF; ++i) { printf("init MTD_%d… ", i); - mtd_dev_t *dev = mtd_default_get_dev(i); + mtd_dev_t *dev = mtd_dev_get(i); int res = mtd_init(dev); if (res) { printf("error: %d\n", res); diff --git a/tests/sys/usbus_msc/main.c b/tests/sys/usbus_msc/main.c index 56020aff48..07c20d8d37 100644 --- a/tests/sys/usbus_msc/main.c +++ b/tests/sys/usbus_msc/main.c @@ -48,7 +48,7 @@ MTD_EMULATED_DEV(0, SECTOR_COUNT, PAGES_PER_SECTOR, PAGE_SIZE); #endif /* MODULE_MTD_EMULATED */ -#include "mtd_default.h" +#include "mtd.h" #include "shell.h" #include "usb/usbus.h" #include "usb/usbus/msc.h" @@ -76,7 +76,7 @@ static int _cmd_add_lun(int argc, char **argv) puts("error: invalid MTD device specified"); return -2; } - mtd_dev = mtd_default_get_dev(dev); + mtd_dev = mtd_dev_get(dev); ret = usbus_msc_add_lun(usbus, mtd_dev); if (ret != 0) { printf("Cannot add LUN device (error:%d %s)\n", ret, strerror(-ret)); @@ -104,7 +104,7 @@ static int _cmd_remove_lun(int argc, char **argv) puts("error: invalid MTD device specified"); return -2; } - mtd_dev = mtd_default_get_dev(dev); + mtd_dev = mtd_dev_get(dev); ret = usbus_msc_remove_lun(usbus, mtd_dev); if (ret == -EAGAIN) { printf("MTD device was not registered\n");