Skip to content

Commit

Permalink
Reorganized ttwrplus nonvolatile memory devices
Browse files Browse the repository at this point in the history
  • Loading branch information
silseva committed May 12, 2024
1 parent 854197d commit 24a296c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 44 deletions.
39 changes: 17 additions & 22 deletions platform/drivers/NVM/flash_zephyr.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,51 +22,46 @@
#include <zephyr/drivers/flash.h>
#include "flash_zephyr.h"

#define TO_DEV_HANDLE(x) ((const struct zephyrFlashDevice *) x)


static int nvm_api_read(const struct nvmDevice *dev, uint32_t offset,
void *data, size_t len)
{
const struct device *fDev = (const struct device *)(dev->config);

return flash_read(fDev, offset, data, len);
return flash_read(TO_DEV_HANDLE(dev)->device, offset, data, len);
}

static int nvm_api_write(const struct nvmDevice *dev, uint32_t offset,
const void *data, size_t len)
{
const struct device *fDev = (const struct device *)(dev->config);

return flash_write(fDev, offset, data, len);
return flash_write(TO_DEV_HANDLE(dev)->device, offset, data, len);
}

static int nvm_api_erase(const struct nvmDevice *dev, uint32_t offset, size_t size)
{
const struct device *fDev = (const struct device *)(dev->config);

return flash_erase(fDev, offset, size);
return flash_erase(TO_DEV_HANDLE(dev)->device, offset, size);
}

static const struct nvmParams *nvm_api_params(const struct nvmDevice *dev)
{
struct nvmParams *params = (struct nvmParams *)(dev->priv);
const struct device *fDev = (const struct device *)(dev->config);

int zephirFlash_init(const struct zephyrFlashDevice* dev)
{
// Retrieve write size
const struct flash_parameters *info = flash_get_parameters(fDev);
params->write_size = info->write_block_size;
const struct flash_parameters *info = flash_get_parameters(TO_DEV_HANDLE(dev)->device);
struct nvmInfo *pInfo = (struct nvmInfo *)(TO_DEV_HANDLE(dev)->info);

// TODO: erase size and erase cycles to be retrieved from the real device.
params->erase_size = 4096;
params->erase_cycles = 100000;
params->type = NVM_FLASH;
pInfo->write_size = info->write_block_size;
pInfo->erase_size = 4096;
pInfo->erase_cycles = 100000;
pInfo->device_info = NVM_FLASH | NVM_WRITE | NVM_BITWRITE | NVM_ERASE;

return params;
return 0;
}

const struct nvmApi zephyr_flash_api =
const struct nvmOps zephyr_flash_ops =
{
.read = nvm_api_read,
.write = nvm_api_write,
.erase = nvm_api_erase,
.sync = NULL,
.params = nvm_api_params
.sync = NULL
};
40 changes: 31 additions & 9 deletions platform/drivers/NVM/flash_zephyr.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,48 @@
* Wrapper interface for the Zephyr RTOS flash memory device driver.
*/


/**
* Device driver API for Zephyr RTOS flash memory.
* Driver data structure for Zephyr RTOS flash memory device.
*/
extern const struct nvmApi zephyr_flash_api;
struct zephyrFlashDevice
{
const struct nvmOps *ops; ///< Device operations
const struct nvmInfo *info; ///< Device info
const size_t size; ///< Device size
const struct device *device; ///< Underlying Zephyr RTOS device driver
};

/**
* Device driver API for Zephyr RTOS flash memory.
*/
extern const struct nvmOps zephyr_flash_ops;

/**
* Instantiate a nonvolatile memory device based on Zephyr RTOS flash device
* driver.
*
* @param name: device name.
* @param alias: devicetree alias of the flash device.
* @param sz: memory size, in bytes.
*/
#define ZEPHYR_FLASH_DEVICE_DEFINE(name, alias) \
static struct nvmParams flash_params_##name; \
static const struct nvmDevice name = \
{ \
.config = DEVICE_DT_GET(DT_ALIAS(alias)), \
.priv = &flash_params_##name, \
.api = &zephyr_flash_api \
#define ZEPHYR_FLASH_DEVICE_DEFINE(name, alias, sz) \
static struct nvmInfo nvm_devInfo_##name; \
static const struct zephyrFlashDevice name = \
{ \
.ops = &zephyr_flash_ops, \
.info = &nvm_devInfo_##name, \
.size = sz, \
.device = DEVICE_DT_GET(DT_ALIAS(alias)) \
};


/**
* Initialize a Zephyr RTOS flash device driver instance.
*
* @param dev: device handle.
* @return zero on success, a negative error code otherwise.
*/
int zephirFlash_init(const struct zephyrFlashDevice* dev);

#endif /* FLASH_ZEPHYR_H */
24 changes: 11 additions & 13 deletions platform/drivers/NVM/nvmem_ttwrplus.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,33 @@
#include <interfaces/nvmem.h>
#include "flash_zephyr.h"

ZEPHYR_FLASH_DEVICE_DEFINE(extFlash, flash);
ZEPHYR_FLASH_DEVICE_DEFINE(eflash, flash, FIXED_PARTITION_SIZE(storage_partition));

static const struct nvmArea areas[] =
static const struct nvmDescriptor nvMemory =
{
{
.name = "External flash",
.dev = &extFlash,
.startAddr = FIXED_PARTITION_OFFSET(storage_partition),
.size = FIXED_PARTITION_SIZE(storage_partition),
.partitions = NULL
}
.name = "External flash",
.dev = (const struct nvmDevice *) &eflash,
.partNum = 0,
.partitions = NULL
};


void nvm_init()
{

zephirFlash_init(&eflash);
}

void nvm_terminate()
{

}

size_t nvm_getMemoryAreas(const struct nvmArea **list)
const struct nvmDescriptor *nvm_getDesc(const size_t index)
{
*list = &areas[0];
if(index > 0)
return NULL;

return (sizeof(areas) / sizeof(struct nvmArea));
return &nvMemory;
}

void nvm_readCalibData(void *buf)
Expand Down

0 comments on commit 24a296c

Please sign in to comment.