Skip to content

Commit

Permalink
Merge pull request #17627 from benpicco/mtd_flashpage_t
Browse files Browse the repository at this point in the history
drivers/mtd_flashpage: add mtd_flashpage_t type
  • Loading branch information
fjmolinas authored Mar 25, 2022
2 parents 844279f + fe4efca commit a10b157
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
17 changes: 13 additions & 4 deletions drivers/include/mtd_flashpage.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,26 @@ extern "C"
* @brief Macro helper to initialize a mtd_t with flash-age driver
*/
#define MTD_FLASHPAGE_INIT_VAL(_pages_per_sector) { \
.driver = &mtd_flashpage_driver, \
.sector_count = FLASHPAGE_NUMOF, \
.pages_per_sector = _pages_per_sector,\
.page_size = FLASHPAGE_SIZE / _pages_per_sector,\
.base = { \
.driver = &mtd_flashpage_driver, \
.sector_count = FLASHPAGE_NUMOF, \
.pages_per_sector = _pages_per_sector, \
.page_size = FLASHPAGE_SIZE / _pages_per_sector, \
}, \
}

/**
* @brief Flashpage MTD device operations table
*/
extern const mtd_desc_t mtd_flashpage_driver;

/**
* @brief MTD flashpage descriptor
*/
typedef struct {
mtd_dev_t base; /**< MTD generic device */
} mtd_flashpage_t;

#ifdef __cplusplus
}
#endif
Expand Down
13 changes: 7 additions & 6 deletions sys/fido2/ctap/ctap_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
/**
* @brief MTD device descriptor initialized with flash-page driver
*/
static mtd_dev_t _mtd_dev = MTD_FLASHPAGE_INIT_VAL(CTAP_FLASH_PAGES_PER_SECTOR);
static mtd_flashpage_t _mtd_flash_dev = MTD_FLASHPAGE_INIT_VAL(CTAP_FLASH_PAGES_PER_SECTOR);
static mtd_dev_t *_mtd_dev = &_mtd_flash_dev.base;

/**
* @brief Max amount of resident keys that can be stored
Expand All @@ -49,7 +50,7 @@ int fido2_ctap_mem_init(void)
{
int ret;

ret = mtd_init(&_mtd_dev);
ret = mtd_init(_mtd_dev);

if (ret < 0) {
return ret;
Expand All @@ -64,7 +65,7 @@ int fido2_ctap_mem_init(void)

static unsigned _amount_of_flashpages(void)
{
return _mtd_dev.sector_count * _mtd_dev.pages_per_sector;
return _mtd_dev->sector_count * _mtd_dev->pages_per_sector;
}

int fido2_ctap_mem_read(void *buf, uint32_t page, uint32_t offset, uint32_t len)
Expand All @@ -73,7 +74,7 @@ int fido2_ctap_mem_read(void *buf, uint32_t page, uint32_t offset, uint32_t len)

int ret;

ret = mtd_read_page(&_mtd_dev, buf, page, offset, len);
ret = mtd_read_page(_mtd_dev, buf, page, offset, len);

if (ret < 0) {
return CTAP1_ERR_OTHER;
Expand All @@ -89,14 +90,14 @@ int fido2_ctap_mem_write(const void *buf, uint32_t page, uint32_t offset, uint32
int ret;

if (!_flash_is_erased(page, offset, len)) {
ret = mtd_write_page(&_mtd_dev, buf, page, offset, len);
ret = mtd_write_page(_mtd_dev, buf, page, offset, len);

if (ret < 0) {
return CTAP1_ERR_OTHER;
}
}
else {
ret = mtd_write_page_raw(&_mtd_dev, buf, page, offset, len);
ret = mtd_write_page_raw(_mtd_dev, buf, page, offset, len);

if (ret < 0) {
return CTAP1_ERR_OTHER;
Expand Down
4 changes: 2 additions & 2 deletions tests/mtd_flashpage/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
#endif
#define TEST_ADDRESS0 (FLASHPAGE_NUMOF - 1)

static mtd_dev_t _dev = MTD_FLASHPAGE_INIT_VAL(8);
static mtd_dev_t *dev = &_dev;
static mtd_flashpage_t _dev = MTD_FLASHPAGE_INIT_VAL(8);
static mtd_dev_t *dev = &_dev.base;

static void setup(void)
{
Expand Down

0 comments on commit a10b157

Please sign in to comment.