Reorganized ttwrplus nonvolatile memory devices

Silvano Seva 2024-04-05 21:20:50 +02:00
rodzic 65afc9bc4c
commit 358c66a2a6
3 zmienionych plików z 59 dodań i 44 usunięć

Wyświetl plik

@ -22,51 +22,46 @@
#include <zephyr/drivers/flash.h> #include <zephyr/drivers/flash.h>
#include "flash_zephyr.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, static int nvm_api_read(const struct nvmDevice *dev, uint32_t offset,
void *data, size_t len) void *data, size_t len)
{ {
const struct device *fDev = (const struct device *)(dev->config); return flash_read(TO_DEV_HANDLE(dev)->device, offset, data, len);
return flash_read(fDev, offset, data, len);
} }
static int nvm_api_write(const struct nvmDevice *dev, uint32_t offset, static int nvm_api_write(const struct nvmDevice *dev, uint32_t offset,
const void *data, size_t len) const void *data, size_t len)
{ {
const struct device *fDev = (const struct device *)(dev->config); return flash_write(TO_DEV_HANDLE(dev)->device, offset, data, len);
return flash_write(fDev, offset, data, len);
} }
static int nvm_api_erase(const struct nvmDevice *dev, uint32_t offset, size_t size) 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(TO_DEV_HANDLE(dev)->device, offset, size);
return flash_erase(fDev, 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 // Retrieve write size
const struct flash_parameters *info = flash_get_parameters(fDev); const struct flash_parameters *info = flash_get_parameters(TO_DEV_HANDLE(dev)->device);
params->write_size = info->write_block_size; struct nvmInfo *pInfo = (struct nvmInfo *)(TO_DEV_HANDLE(dev)->info);
// TODO: erase size and erase cycles to be retrieved from the real device. // TODO: erase size and erase cycles to be retrieved from the real device.
params->erase_size = 4096; pInfo->write_size = info->write_block_size;
params->erase_cycles = 100000; pInfo->erase_size = 4096;
params->type = NVM_FLASH; 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, .read = nvm_api_read,
.write = nvm_api_write, .write = nvm_api_write,
.erase = nvm_api_erase, .erase = nvm_api_erase,
.sync = NULL, .sync = NULL
.params = nvm_api_params
}; };

Wyświetl plik

@ -27,11 +27,22 @@
* Wrapper interface for the Zephyr RTOS flash memory device driver. * Wrapper interface for the Zephyr RTOS flash memory device driver.
*/ */
/**
* Driver data structure for Zephyr RTOS flash memory device.
*/
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. * Device driver API for Zephyr RTOS flash memory.
*/ */
extern const struct nvmApi zephyr_flash_api; extern const struct nvmOps zephyr_flash_ops;
/** /**
* Instantiate a nonvolatile memory device based on Zephyr RTOS flash device * Instantiate a nonvolatile memory device based on Zephyr RTOS flash device
@ -39,14 +50,25 @@ extern const struct nvmApi zephyr_flash_api;
* *
* @param name: device name. * @param name: device name.
* @param alias: devicetree alias of the flash device. * @param alias: devicetree alias of the flash device.
* @param sz: memory size, in bytes.
*/ */
#define ZEPHYR_FLASH_DEVICE_DEFINE(name, alias) \ #define ZEPHYR_FLASH_DEVICE_DEFINE(name, alias, sz) \
static struct nvmParams flash_params_##name; \ static struct nvmInfo nvm_devInfo_##name; \
static const struct nvmDevice name = \ static const struct zephyrFlashDevice name = \
{ \ { \
.config = DEVICE_DT_GET(DT_ALIAS(alias)), \ .ops = &zephyr_flash_ops, \
.priv = &flash_params_##name, \ .info = &nvm_devInfo_##name, \
.api = &zephyr_flash_api \ .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 */ #endif /* FLASH_ZEPHYR_H */

Wyświetl plik

@ -23,23 +23,20 @@
#include <interfaces/nvmem.h> #include <interfaces/nvmem.h>
#include "flash_zephyr.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",
.name = "External flash", .dev = (const struct nvmDevice *) &eflash,
.dev = &extFlash, .partNum = 0,
.startAddr = FIXED_PARTITION_OFFSET(storage_partition), .partitions = NULL
.size = FIXED_PARTITION_SIZE(storage_partition),
.partitions = NULL
}
}; };
void nvm_init() void nvm_init()
{ {
zephirFlash_init(&eflash);
} }
void nvm_terminate() void nvm_terminate()
@ -47,11 +44,12 @@ 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) void nvm_readCalibData(void *buf)