Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers/mtd: use XFA for pointers to defined MTDs #19465

Merged
merged 9 commits into from
Oct 20, 2023
2 changes: 1 addition & 1 deletion boards/adafruit-grand-central-m4-express/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static mtd_spi_nor_t samd51_nor_dev = {
.params = &_samd51_nor_params,
};

mtd_dev_t *mtd0 = (mtd_dev_t *)&samd51_nor_dev;
MTD_XFA_ADD(samd51_nor_dev, 0);

#ifdef MODULE_VFS_DEFAULT
VFS_AUTO_MOUNT(littlefs2, VFS_MTD(samd51_nor_dev), VFS_DEFAULT_NVM(0), 0);
Expand Down
2 changes: 1 addition & 1 deletion boards/adafruit-itsybitsy-m4/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static mtd_spi_nor_t samd51_nor_dev = {
.params = &_samd51_nor_params,
};

mtd_dev_t *mtd0 = (mtd_dev_t *)&samd51_nor_dev;
MTD_XFA_ADD(samd51_nor_dev, 0);

#ifdef MODULE_VFS_DEFAULT
VFS_AUTO_MOUNT(littlefs2, VFS_MTD(samd51_nor_dev), VFS_DEFAULT_NVM(0), 0);
Expand Down
2 changes: 1 addition & 1 deletion boards/adafruit-pybadge/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static mtd_spi_nor_t samd51_nor_dev = {
.params = &_samd51_nor_params,
};

mtd_dev_t *mtd0 = (mtd_dev_t *)&samd51_nor_dev;
MTD_XFA_ADD(samd51_nor_dev, 0);
#endif /* MODULE_MTD */

void board_init(void)
Expand Down
2 changes: 1 addition & 1 deletion boards/common/weact-f4x1cx/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static mtd_spi_nor_t weact_nor_dev = {
.params = &_weact_nor_params,
};

mtd_dev_t *mtd0 = (mtd_dev_t *)&weact_nor_dev;
MTD_XFA_ADD(weact_nor_dev, 0);

#ifdef MODULE_VFS_DEFAULT
#include "vfs_default.h"
Expand Down
2 changes: 1 addition & 1 deletion boards/ikea-tradfri/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static mtd_spi_nor_t ikea_tradfri_nor_dev = {
.params = &_ikea_tradfri_nor_params,
};

mtd_dev_t *mtd0 = (mtd_dev_t *)&ikea_tradfri_nor_dev;
MTD_XFA_ADD(ikea_tradfri_nor_dev, 0);
#endif /* MODULE_MTD */

void board_init(void)
Expand Down
2 changes: 1 addition & 1 deletion boards/iotlab-m3/mtd.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static mtd_spi_nor_t mtd_nor_dev = {
.params = &_mtd_nor_params,
};

mtd_dev_t *mtd0 = (mtd_dev_t *)&mtd_nor_dev;
MTD_XFA_ADD(mtd_nor_dev, 0);

#ifdef MODULE_VFS_DEFAULT
#include "vfs_default.h"
Expand Down
2 changes: 1 addition & 1 deletion boards/mcb2388/board_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#ifdef MODULE_MTD_MCI
extern const mtd_desc_t mtd_mci_driver;
static mtd_dev_t _mtd_mci = { .driver = &mtd_mci_driver };
mtd_dev_t *mtd0 = &_mtd_mci;
MTD_XFA_ADD(_mtd_mci, 0);
#endif

#ifdef MODULE_VFS_DEFAULT
Expand Down
2 changes: 1 addition & 1 deletion boards/msba2/board_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#ifdef MODULE_MTD_MCI
extern const mtd_desc_t mtd_mci_driver;
static mtd_dev_t _mtd_mci = { .driver = &mtd_mci_driver };
mtd_dev_t *mtd0 = &_mtd_mci;
MTD_XFA_ADD(_mtd_mci, 0);
#endif

#ifdef MODULE_VFS_DEFAULT
Expand Down
2 changes: 1 addition & 1 deletion boards/mulle/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static mtd_spi_nor_t mulle_nor_dev = {
.params = &mulle_nor_params,
};

mtd_dev_t *mtd0 = (mtd_dev_t *)&mulle_nor_dev;
MTD_XFA_ADD(mulle_nor_dev, 0);

static devfs_t mulle_nor_devfs = {
.path = "/mtd0",
Expand Down
2 changes: 1 addition & 1 deletion boards/native/board_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mtd_native_dev_t mtd0_dev = {
.fname = MTD_NATIVE_FILENAME,
};

mtd_dev_t *mtd0 = &mtd0_dev.base;
MTD_XFA_ADD(mtd0_dev.base, 0);
#endif

#ifdef MODULE_VFS_DEFAULT
Expand Down
2 changes: 1 addition & 1 deletion boards/nrf52840dk/mtd.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static mtd_spi_nor_t nrf52840dk_nor_dev = {
.params = &_nrf52840dk_nor_params,
};

mtd_dev_t *mtd0 = (mtd_dev_t *)&nrf52840dk_nor_dev;
MTD_XFA_ADD(nrf52840dk_nor_dev, 0);

#ifdef MODULE_VFS_DEFAULT
#include "vfs_default.h"
Expand Down
2 changes: 1 addition & 1 deletion boards/nrf5340dk-app/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static mtd_spi_nor_t nrf5340_nor_dev = {
},
.params = &_nrf5340_nor_params,
};
mtd_dev_t *mtd0 = (mtd_dev_t *)&nrf5340_nor_dev;
MTD_XFA_ADD(nrf5340_nor_dev, 0);

#ifdef MODULE_VFS_DEFAULT
VFS_AUTO_MOUNT(littlefs2, VFS_MTD(nrf5340_nor_dev), VFS_DEFAULT_NVM(0), 0);
Expand Down
1 change: 0 additions & 1 deletion boards/nrf5340dk-app/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ extern "C" {
*/
extern mtd_dev_t *mtd0;
#define MTD_0 mtd0
#define MTD_NUMOF 1

#define BOARD_QSPI_PIN_CS GPIO_PIN(0, 18) /**< SPI Flash Chip Select */
#define BOARD_QSPI_PIN_WP GPIO_PIN(0, 15) /**< SPI Flash Write Protect */
Expand Down
2 changes: 1 addition & 1 deletion boards/pinetime/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static mtd_spi_nor_t pinetime_nor_dev = {
.params = &_pinetime_nor_params,
};

mtd_dev_t *mtd0 = (mtd_dev_t *)&pinetime_nor_dev;
MTD_XFA_ADD(pinetime_nor_dev, 0);
#endif /* MODULE_MTD */

void board_init(void)
Expand Down
2 changes: 1 addition & 1 deletion boards/qn9080dk/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ static mtd_spi_nor_t mtd_nor_dev = {
.params = &_mtd_nor_params,
};

mtd_dev_t *mtd0 = (mtd_dev_t *)&mtd_nor_dev;
MTD_XFA_ADD(mtd_nor_dev, 0);
#endif /* MODULE_MTD */
7 changes: 4 additions & 3 deletions boards/same54-xpro/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static mtd_spi_nor_t same54_nor_dev = {
},
.params = &_same54_nor_params,
};
mtd_dev_t *mtd0 = (mtd_dev_t *)&same54_nor_dev;
MTD_XFA_ADD(same54_nor_dev, 0);

#ifdef MODULE_VFS_DEFAULT
VFS_AUTO_MOUNT(littlefs2, VFS_MTD(same54_nor_dev), VFS_DEFAULT_NVM(0), 0);
Expand All @@ -69,7 +69,8 @@ static mtd_at24cxxx_t at24mac_dev = {
.at24cxxx_eeprom = &at24cxxx_dev,
.params = at24cxxx_params,
};
mtd_dev_t *mtd1 = (mtd_dev_t *)&at24mac_dev;
MTD_XFA_ADD(at24mac_dev, 1);

#endif /* MODULE_MTD_AT24CXXX */

#ifdef MODULE_SAM0_SDHC
Expand All @@ -84,7 +85,7 @@ static mtd_sam0_sdhc_t sdhc_dev = {
.wp = GPIO_PIN(PD, 21),
},
};
mtd_dev_t *mtd2 = (mtd_dev_t *)&sdhc_dev;
MTD_XFA_ADD(sdhc_dev, 2);

#ifdef MODULE_VFS_DEFAULT
/* default to FAT */
Expand Down
1 change: 0 additions & 1 deletion boards/same54-xpro/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ extern mtd_dev_t *mtd0, *mtd1, *mtd2;
#define MTD_0 mtd0
#define MTD_1 mtd1
#define MTD_2 mtd2
#define MTD_NUMOF 3

#define CONFIG_SDMMC_GENERIC_MTD_OFFSET 2 /**< mtd2 is used for SD Card */
/** @} */
Expand Down
2 changes: 1 addition & 1 deletion boards/samr34-xpro/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static mtd_spi_nor_t _nor_dev = {
},
.params = &_mtd_nor_params,
};
mtd_dev_t *mtd0 = (mtd_dev_t *)&_nor_dev;
MTD_XFA_ADD(_nor_dev, 0);

#ifdef MODULE_VFS_DEFAULT
#include "vfs_default.h"
Expand Down
2 changes: 1 addition & 1 deletion boards/serpente/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ static mtd_spi_nor_t serpente_nor_dev = {
.params = &_serpente_nor_params,
};

mtd_dev_t *mtd0 = (mtd_dev_t *)&serpente_nor_dev;
MTD_XFA_ADD(serpente_nor_dev, 0);
#endif /* MODULE_MTD */
8 changes: 3 additions & 5 deletions cpu/esp_common/periph/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@
#define ESP_PART_ENTRY_SIZE 0x20
#define ESP_PART_ENTRY_MAGIC ESP_PARTITION_MAGIC

/* the external pointer to the system MTD device */
mtd_dev_t* mtd0 = 0;

static mtd_dev_t _flash_dev;
static mtd_desc_t _flash_driver;

/* the external pointer to the system MTD device */
MTD_XFA_ADD(_flash_dev, 0);

#ifdef MODULE_VFS_DEFAULT
#include "vfs_default.h"
VFS_AUTO_MOUNT(littlefs2, { .dev = &_flash_dev }, VFS_DEFAULT_NVM(0), 0);
Expand Down Expand Up @@ -205,8 +205,6 @@ void spi_flash_drive_init(void)
_flash_dev.driver = &_flash_driver;
_flash_dev.sector_count = _flash_size / _flashchip->sector_size;

mtd0 = &_flash_dev;

_flash_dev.pages_per_sector = _flashchip->sector_size / _flashchip->page_size;
_flash_dev.page_size = _flashchip->page_size;
/* Emulation for smaller / unaligned writes is present, but at reduced
Expand Down
52 changes: 52 additions & 0 deletions drivers/include/mtd.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@

#include <stdint.h>

#include "xfa.h"

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -116,6 +118,56 @@ typedef struct {
#endif
} mtd_dev_t;

/**
* @brief MTD device array as XFA
*
* The array contains the addresses of all MTD devices that are defined using
* the @ref MTD_XFA_ADD macro, for example:
* ```
* MTD_XFA_ADD(my_dev, 0);
* ```
* The MTD devices in this array can be used for automatic functions such as
* with the `mtd_default` module. The i-th device in this array can then be
* accessed with `mtd_dev_xfa[i]`. The number of MTDs defined in this array
* is `XFA_LEN(mtd_dev_xfa)`.
*/
#if !DOXYGEN
XFA_USE_CONST(mtd_dev_t *, mtd_dev_xfa);
#else
mtd_dev_t * const mtd_dev_xfa[];
#endif

/**
* @brief Define MTD device pointer variable `mtd<idx>`
*
* The macro defines the MTD device pointer variable `mtd<idx>`, sets it to
* the address of the MTD device specified by the @p dev parameter, and adds
* it to the XFA of MTD device pointers @ref mtd_dev_xfa. For example
* ```
* MTD_XFA_ADD(my_dev, 1);
* ```
* defines the variable `mtd1` pointing to the device `my_dev`.
*
* The parameter @p idx is used as priority of the MTD device pointer within
* the XFA. That means it determines the order of the MTD device pointers
* within @ref mtd_dev_xfa.
*
* @note Only if each MTD device is added with a unique priority and only if the
* priorities start at 0 and are used in consecutive order, the parameter
* @p idx corresponds to the position of the MTD device pointer within
* the @ref mtd_dev_xfa XFA and `mtd_dev_xfa[i]` points to the i-th MTD
* device.
*
* @param dev MTD device
* @param idx Priority of the MTD device pointer within the XFA
*/
#define MTD_XFA_ADD(dev, idx) XFA_CONST(mtd_dev_xfa, 0) mtd_dev_t *mtd ## idx = (mtd_dev_t *)&(dev)

/**
* @brief Number of MTDs defined in the MTD device array in XFA
*/
#define MTD_NUMOF XFA_LEN(mtd_dev_t *, mtd_dev_xfa)

/**
* @brief MTD driver can write any data to the storage without erasing it first.
*
Expand Down
74 changes: 14 additions & 60 deletions drivers/include/mtd_default.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,54 +33,28 @@ extern "C" {
#include "mtd_emulated.h"
#endif

#if !defined(MTD_NUMOF) && !DOXYGEN

#if defined(MTD_3)
#define MTD_BOARD_NUMOF 4
#elif defined(MTD_2)
#define MTD_BOARD_NUMOF 3
#elif defined(MTD_1)
#define MTD_BOARD_NUMOF 2
#elif defined(MTD_0)
#define MTD_BOARD_NUMOF 1
#else
#define MTD_BOARD_NUMOF 0
#endif

#define MTD_SDCARD_NUMOF IS_USED(MODULE_MTD_SDCARD_DEFAULT)
#define MTD_EMULATED_NUMOF IS_USED(MODULE_MTD_EMULATED)

/**
* @brief Number of MTD devices
*/
#define MTD_NUMOF (MTD_BOARD_NUMOF + MTD_SDCARD_NUMOF + MTD_EMULATED_NUMOF)

#else
#define MTD_BOARD_NUMOF MTD_NUMOF
#endif /* !defined(MTD_NUMOF) && !DOXYGEN */

#if !DOXYGEN

/**
* @brief Declare `mtd*` according to the number of MTD devices
* @brief Declare `mtd*` according to the `MTD_*` symbols defined by the board
*/
#if MTD_NUMOF > 0
extern mtd_dev_t *mtd0;
#ifdef MTD_0
extern mtd_dev_t *MTD_0;
#endif
#if MTD_NUMOF > 1
extern mtd_dev_t *mtd1;
#ifdef MTD_1
extern mtd_dev_t *MTD_1;
#endif
#if MTD_NUMOF > 2
extern mtd_dev_t *mtd2;
#ifdef MTD_2
extern mtd_dev_t *MTD_2;
#endif
#if MTD_NUMOF > 3
extern mtd_dev_t *mtd3;
#ifdef MTD_3
extern mtd_dev_t *MTD_3;
#endif
#if MTD_NUMOF > 4
extern mtd_dev_t *mtd4;
#ifdef MTD_4
extern mtd_dev_t *MTD_4;
#endif
#if MTD_NUMOF > 5
extern mtd_dev_t *mtd5;
#ifdef MTD_5
extern mtd_dev_t *MTD_5;
#endif
#endif /* !DOXYGEN */

Expand All @@ -102,27 +76,7 @@ extern mtd_emulated_t mtd_emulated_dev0;
*/
static inline mtd_dev_t *mtd_default_get_dev(unsigned idx)
{
switch (idx) {
#if MTD_BOARD_NUMOF > 0
case 0: return MTD_0;
#endif
#if MTD_BOARD_NUMOF > 1
case 1: return MTD_1;
#endif
#if MTD_BOARD_NUMOF > 2
case 2: return MTD_2;
#endif
#if MTD_BOARD_NUMOF > 3
case 3: return MTD_3;
#endif
#if MTD_SDCARD_NUMOF > 0
case MTD_BOARD_NUMOF: return (mtd_dev_t *)&mtd_sdcard_dev0;
#endif
#if MTD_EMULATED_NUMOF > 0
case MTD_BOARD_NUMOF + MTD_SDCARD_NUMOF: return (mtd_dev_t *)&mtd_emulated_dev0;
#endif
}
return NULL;
return ((MTD_NUMOF != 0) && (idx < MTD_NUMOF)) ? mtd_dev_xfa[idx] : NULL;
}

#ifdef __cplusplus
Expand Down
Loading