drivers: eeprom: Unified simulator and native_posix

EEPROM simulator and native_posix have been unified to one solution,
the old eeprom,native_posix is removed.

Signed-off-by: Laczen JMS <laczenjms@gmail.com>
This commit is contained in:
Laczen JMS 2019-11-10 12:09:44 +01:00 committed by Johan Hedberg
commit ab3ed439c8
7 changed files with 115 additions and 247 deletions

View file

@ -104,7 +104,7 @@ endif # USB
if EEPROM if EEPROM
config EEPROM_NATIVE_POSIX config EEPROM_SIMULATOR
default y default y
endif # EEPROM endif # EEPROM

View file

@ -70,7 +70,7 @@
eeprom0: eeprom { eeprom0: eeprom {
status = "okay"; status = "okay";
compatible = "zephyr,native-posix-eeprom"; compatible = "zephyr,sim-eeprom";
label = "EEPROM_0"; label = "EEPROM_0";
size = <DT_SIZE_K(32)>; size = <DT_SIZE_K(32)>;
}; };

View file

@ -5,7 +5,6 @@ zephyr_library()
zephyr_library_sources_ifdef(CONFIG_USERSPACE eeprom_handlers.c) zephyr_library_sources_ifdef(CONFIG_USERSPACE eeprom_handlers.c)
zephyr_library_sources_ifdef(CONFIG_EEPROM_SHELL eeprom_shell.c) zephyr_library_sources_ifdef(CONFIG_EEPROM_SHELL eeprom_shell.c)
zephyr_library_sources_ifdef(CONFIG_EEPROM_NATIVE_POSIX eeprom_native_posix.c)
zephyr_library_sources_ifdef(CONFIG_EEPROM_AT2X eeprom_at2x.c) zephyr_library_sources_ifdef(CONFIG_EEPROM_AT2X eeprom_at2x.c)
zephyr_library_sources_ifdef(CONFIG_EEPROM_STM32 eeprom_stm32.c) zephyr_library_sources_ifdef(CONFIG_EEPROM_STM32 eeprom_stm32.c)
zephyr_library_sources_ifdef(CONFIG_EEPROM_SIMULATOR eeprom_simulator.c) zephyr_library_sources_ifdef(CONFIG_EEPROM_SIMULATOR eeprom_simulator.c)

View file

@ -28,12 +28,6 @@ config EEPROM_SHELL_BUFFER_SIZE
Size of the buffer used for EEPROM read/write commands in Size of the buffer used for EEPROM read/write commands in
the EEPROM shell. the EEPROM shell.
config EEPROM_NATIVE_POSIX
bool "Native POSIX EEPROM driver"
depends on BOARD_NATIVE_POSIX
help
Enable Native POSIX EEPROM driver.
config EEPROM_AT2X config EEPROM_AT2X
bool bool
help help

View file

@ -1,209 +0,0 @@
/*
* Copyright (c) 2019 Vestas Wind Systems A/S
*
* Heavily based on flash_native_posix.c, which is:
* Copyright (c) 2019 Jan Van Winkel <jan.van_winkel@dxplore.eu>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <device.h>
#include <drivers/eeprom.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include "cmdline.h"
#include "soc.h"
#define LOG_LEVEL CONFIG_EEPROM_LOG_LEVEL
#include <logging/log.h>
LOG_MODULE_REGISTER(eeprom_native_posix);
static const char default_eeprom_path[] = "eeprom.bin";
struct eeprom_native_posix_data {
const char *path;
int fd;
u8_t *eeprom;
bool init_called;
};
struct eeprom_native_posix_config {
size_t size;
bool readonly;
};
#define DEV_NAME(dev) ((dev)->config->name)
#define DEV_CONFIG(dev) ((dev)->config->config_info)
#define DEV_DATA(dev) \
((struct eeprom_native_posix_data *const)(dev)->driver_data)
static int eeprom_native_posix_read(struct device *dev, off_t offset,
void *buf, size_t len)
{
struct eeprom_native_posix_data *const data = DEV_DATA(dev);
const struct eeprom_native_posix_config *config = DEV_CONFIG(dev);
if (!len) {
return 0;
}
if ((offset + len) > config->size) {
LOG_WRN("attempt to read past device boundary");
return -EINVAL;
}
if (data->eeprom == MAP_FAILED) {
LOG_ERR("no EEPROM device mapped");
return -EIO;
}
memcpy(buf, data->eeprom + offset, len);
return 0;
}
static int eeprom_native_posix_write(struct device *dev, off_t offset,
const void *buf, size_t len)
{
struct eeprom_native_posix_data *const data = DEV_DATA(dev);
const struct eeprom_native_posix_config *config = DEV_CONFIG(dev);
if (config->readonly) {
LOG_WRN("attempt to write to read-only device");
return -EACCES;
}
if (!len) {
return 0;
}
if ((offset + len) > config->size) {
LOG_WRN("attempt to write past device boundary");
return -EINVAL;
}
if (data->eeprom == MAP_FAILED) {
LOG_ERR("no EEPROM device mapped");
return -EIO;
}
memcpy(data->eeprom + offset, buf, len);
return 0;
}
static size_t eeprom_native_posix_size(struct device *dev)
{
const struct eeprom_native_posix_config *config = DEV_CONFIG(dev);
return config->size;
}
static int eeprom_native_posix_init(struct device *dev)
{
struct eeprom_native_posix_data *const data = DEV_DATA(dev);
const struct eeprom_native_posix_config *config = DEV_CONFIG(dev);
data->init_called = true;
if (data->path == NULL) {
data->path = default_eeprom_path;
}
data->fd = open(data->path, O_RDWR | O_CREAT, (mode_t)0600);
if (data->fd == -1) {
posix_print_warning("failed to open EEPROM device file "
"%s: %s\n",
data->path, strerror(errno));
return -EIO;
}
if (ftruncate(data->fd, config->size) == -1) {
posix_print_warning("failed to resize EEPROM device file "
"%s: %s\n",
data->path, strerror(errno));
return -EIO;
}
data->eeprom = mmap(NULL, config->size, PROT_WRITE | PROT_READ,
MAP_SHARED, data->fd, 0);
if (data->eeprom == MAP_FAILED) {
posix_print_warning("failed to mmap EEPROM device file "
"%s: %s\n",
data->path, strerror(errno));
return -EIO;
}
return 0;
}
static const struct eeprom_driver_api eeprom_native_posix_driver_api = {
.read = eeprom_native_posix_read,
.write = eeprom_native_posix_write,
.size = eeprom_native_posix_size,
};
static const struct eeprom_native_posix_config eeprom_native_posix_config_0 = {
.size = DT_INST_0_ZEPHYR_NATIVE_POSIX_EEPROM_SIZE,
.readonly = DT_INST_0_ZEPHYR_NATIVE_POSIX_EEPROM_READ_ONLY,
};
static struct eeprom_native_posix_data eeprom_native_posix_data_0;
DEVICE_AND_API_INIT(eeprom_native_posix_0,
DT_INST_0_ZEPHYR_NATIVE_POSIX_EEPROM_LABEL,
&eeprom_native_posix_init, &eeprom_native_posix_data_0,
&eeprom_native_posix_config_0, POST_KERNEL,
CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
&eeprom_native_posix_driver_api);
static void eeprom_native_posix_cleanup_0(void)
{
struct eeprom_native_posix_data *const data =
&eeprom_native_posix_data_0;
const struct eeprom_native_posix_config *config =
&eeprom_native_posix_config_0;
if (!data->init_called) {
return;
}
if (data->eeprom != MAP_FAILED) {
munmap(data->eeprom, config->size);
}
if (data->fd != -1) {
close(data->fd);
}
}
void eeprom_native_posix_options_0(void)
{
static struct args_struct_t eeprom_options[] = {
{
.manual = false,
.is_mandatory = false,
.is_switch = false,
.option = "eeprom",
.name = "path",
.type = 's',
.dest = (void *)&eeprom_native_posix_data_0.path,
.call_when_found = NULL,
.descript = "Path to binary file to be used as EEPROM",
},
ARG_TABLE_ENDMARKER
};
native_add_command_line_opts(eeprom_options);
}
NATIVE_TASK(eeprom_native_posix_options_0, PRE_BOOT_1, 1);
NATIVE_TASK(eeprom_native_posix_cleanup_0, ON_EXIT, 1);

View file

@ -1,4 +1,5 @@
/* /*
* Copyright (c) 2019 Laczen
* Copyright (c) 2018 Nordic Semiconductor ASA * Copyright (c) 2018 Nordic Semiconductor ASA
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
@ -6,11 +7,22 @@
#include <device.h> #include <device.h>
#include <drivers/eeprom.h> #include <drivers/eeprom.h>
#include <init.h> #include <init.h>
#include <kernel.h> #include <kernel.h>
#include <sys/util.h> #include <sys/util.h>
#include <stats/stats.h> #include <stats/stats.h>
#include <string.h> #include <string.h>
#include <errno.h>
#ifdef CONFIG_ARCH_POSIX
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include "cmdline.h"
#include "soc.h"
#endif
#define LOG_LEVEL CONFIG_EEPROM_LOG_LEVEL #define LOG_LEVEL CONFIG_EEPROM_LOG_LEVEL
#include <logging/log.h> #include <logging/log.h>
@ -70,7 +82,14 @@ STATS_NAME(eeprom_sim_thresholds, max_write_calls)
STATS_NAME(eeprom_sim_thresholds, max_len) STATS_NAME(eeprom_sim_thresholds, max_len)
STATS_NAME_END(eeprom_sim_thresholds); STATS_NAME_END(eeprom_sim_thresholds);
#ifdef CONFIG_ARCH_POSIX
static u8_t *mock_eeprom;
static int eeprom_fd = -1;
static const char *eeprom_file_path;
static const char default_eeprom_file_path[] = "eeprom.bin";
#else
static u8_t mock_eeprom[DT_INST_0_ZEPHYR_SIM_EEPROM_SIZE]; static u8_t mock_eeprom[DT_INST_0_ZEPHYR_SIM_EEPROM_SIZE];
#endif /* CONFIG_ARCH_POSIX */
static int eeprom_range_is_valid(struct device *dev, off_t offset, size_t len) static int eeprom_range_is_valid(struct device *dev, off_t offset, size_t len)
{ {
@ -83,8 +102,8 @@ static int eeprom_range_is_valid(struct device *dev, off_t offset, size_t len)
return 0; return 0;
} }
static int eeprom_sim_read(struct device *dev, const off_t offset, void *data, static int eeprom_sim_read(struct device *dev, off_t offset, void *data,
const size_t len) size_t len)
{ {
if (!len) { if (!len) {
return 0; return 0;
@ -112,8 +131,8 @@ static int eeprom_sim_read(struct device *dev, const off_t offset, void *data,
return 0; return 0;
} }
static int eeprom_sim_write(struct device *dev, const off_t offset, static int eeprom_sim_write(struct device *dev, off_t offset, const void *data,
const void *data, const size_t len) size_t len)
{ {
const struct eeprom_sim_config *config = DEV_CONFIG(dev); const struct eeprom_sim_config *config = DEV_CONFIG(dev);
@ -140,27 +159,23 @@ static int eeprom_sim_write(struct device *dev, const off_t offset,
if (eeprom_sim_thresholds.max_write_calls != 0) { if (eeprom_sim_thresholds.max_write_calls != 0) {
if (eeprom_sim_stats.eeprom_write_calls > if (eeprom_sim_stats.eeprom_write_calls >
eeprom_sim_thresholds.max_write_calls) { eeprom_sim_thresholds.max_write_calls) {
return 0; goto end;
} else if (eeprom_sim_stats.eeprom_write_calls == } else if (eeprom_sim_stats.eeprom_write_calls ==
eeprom_sim_thresholds.max_write_calls) { eeprom_sim_thresholds.max_write_calls) {
if (eeprom_sim_thresholds.max_len == 0) { if (eeprom_sim_thresholds.max_len == 0) {
return 0; goto end;
} }
data_part_ignored = true; data_part_ignored = true;
} }
} }
for (u32_t i = 0; i < len; i++) { if ((data_part_ignored) && (len > eeprom_sim_thresholds.max_len)) {
if (data_part_ignored) { len = eeprom_sim_thresholds.max_len;
if (i >= eeprom_sim_thresholds.max_len) {
return 0;
}
}
*(EEPROM(offset + i)) = *((u8_t *)data + i);
} }
memcpy(EEPROM(offset), data, len);
STATS_INCN(eeprom_sim_stats, bytes_written, len); STATS_INCN(eeprom_sim_stats, bytes_written, len);
#ifdef CONFIG_EEPROM_SIMULATOR_SIMULATE_TIMING #ifdef CONFIG_EEPROM_SIMULATOR_SIMULATE_TIMING
@ -170,8 +185,8 @@ static int eeprom_sim_write(struct device *dev, const off_t offset,
CONFIG_EEPROM_SIMULATOR_MIN_WRITE_TIME_US); CONFIG_EEPROM_SIMULATOR_MIN_WRITE_TIME_US);
#endif #endif
end:
SYNC_UNLOCK(); SYNC_UNLOCK();
return 0; return 0;
} }
@ -193,17 +208,98 @@ static const struct eeprom_sim_config eeprom_sim_config_0 = {
.readonly = DT_INST_0_ZEPHYR_SIM_EEPROM_READ_ONLY, .readonly = DT_INST_0_ZEPHYR_SIM_EEPROM_READ_ONLY,
}; };
#ifdef CONFIG_ARCH_POSIX
static int eeprom_mock_init(struct device *dev)
{
if (eeprom_file_path == NULL) {
eeprom_file_path = default_eeprom_file_path;
}
eeprom_fd = open(eeprom_file_path, O_RDWR | O_CREAT, (mode_t)0600);
if (eeprom_fd == -1) {
posix_print_warning("Failed to open eeprom device file ",
"%s: %s\n",
eeprom_file_path, strerror(errno));
return -EIO;
}
if (ftruncate(eeprom_fd, DT_INST_0_ZEPHYR_SIM_EEPROM_SIZE) == -1) {
posix_print_warning("Failed to resize eeprom device file ",
"%s: %s\n",
eeprom_file_path, strerror(errno));
return -EIO;
}
mock_eeprom = mmap(NULL, DT_INST_0_ZEPHYR_SIM_EEPROM_SIZE,
PROT_WRITE | PROT_READ, MAP_SHARED, eeprom_fd, 0);
if (mock_eeprom == MAP_FAILED) {
posix_print_warning("Failed to mmap eeprom device file "
"%s: %s\n",
eeprom_file_path, strerror(errno));
return -EIO;
}
return 0;
}
#else
static int eeprom_mock_init(struct device *dev)
{
memset(mock_eeprom, 0xFF, ARRAY_SIZE(mock_eeprom));
return 0;
}
#endif /* CONFIG_ARCH_POSIX */
static int eeprom_sim_init(struct device *dev) static int eeprom_sim_init(struct device *dev)
{ {
SYNC_INIT(); SYNC_INIT();
STATS_INIT_AND_REG(eeprom_sim_stats, STATS_SIZE_32, "eeprom_sim_stats"); STATS_INIT_AND_REG(eeprom_sim_stats, STATS_SIZE_32, "eeprom_sim_stats");
STATS_INIT_AND_REG(eeprom_sim_thresholds, STATS_SIZE_32, STATS_INIT_AND_REG(eeprom_sim_thresholds, STATS_SIZE_32,
"eeprom_sim_thresholds"); "eeprom_sim_thresholds");
memset(mock_eeprom, 0xFF, ARRAY_SIZE(mock_eeprom));
return 0; return eeprom_mock_init(dev);
} }
DEVICE_AND_API_INIT(eeprom_sim_0, DT_INST_0_ZEPHYR_SIM_EEPROM_LABEL, DEVICE_AND_API_INIT(eeprom_sim_0, DT_INST_0_ZEPHYR_SIM_EEPROM_LABEL,
&eeprom_sim_init, NULL, &eeprom_sim_config_0, POST_KERNEL, &eeprom_sim_init, NULL, &eeprom_sim_config_0, POST_KERNEL,
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &eeprom_sim_api); CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &eeprom_sim_api);
#ifdef CONFIG_ARCH_POSIX
static void eeprom_native_posix_cleanup(void)
{
if ((mock_eeprom != MAP_FAILED) && (mock_eeprom != NULL)) {
munmap(mock_eeprom, DT_INST_0_ZEPHYR_SIM_EEPROM_SIZE);
}
if (eeprom_fd != -1) {
close(eeprom_fd);
}
}
static void eeprom_native_posix_options(void)
{
static struct args_struct_t eeprom_options[] = {
{ .manual = false,
.is_mandatory = false,
.is_switch = false,
.option = "eeprom",
.name = "path",
.type = 's',
.dest = (void *)&eeprom_file_path,
.call_when_found = NULL,
.descript = "Path to binary file to be used as eeprom" },
ARG_TABLE_ENDMARKER
};
native_add_command_line_opts(eeprom_options);
}
NATIVE_TASK(eeprom_native_posix_options, PRE_BOOT_1, 1);
NATIVE_TASK(eeprom_native_posix_cleanup, ON_EXIT, 1);
#endif /* CONFIG_ARCH_POSIX */

View file

@ -1,12 +0,0 @@
# Copyright (c) 2019 Vestas Wind Systems A/S
# SPDX-License-Identifier: Apache-2.0
description: Zephyr Native POSIX EEPROM device
compatible: "zephyr,native-posix-eeprom"
include: eeprom-base.yaml
properties:
size:
required: true