drivers: eeprom: add fake EEPROM driver

Add a FFF-based fake EEPROM driver which can be used either as a stub or a
mock for testing.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
This commit is contained in:
Henrik Brix Andersen 2022-11-03 13:08:50 +01:00 committed by Maureen Helm
commit 1885bee7c3
5 changed files with 128 additions and 0 deletions

View file

@ -12,3 +12,4 @@ zephyr_library_sources_ifdef(CONFIG_EEPROM_SIMULATOR eeprom_simulator.c)
zephyr_library_sources_ifdef(CONFIG_EEPROM_EMULATOR eeprom_emulator.c)
zephyr_library_sources_ifdef(CONFIG_EEPROM_TMP116 eeprom_tmp116.c)
zephyr_library_sources_ifdef(CONFIG_EEPROM_XEC eeprom_mchp_xec.c)
zephyr_library_sources_ifdef(CONFIG_EEPROM_FAKE eeprom_fake.c)

View file

@ -96,4 +96,11 @@ config EEPROM_SIMULATOR_MIN_WRITE_TIME_US
endif # EEPROM_SIMULATOR_SIMULATE_TIMING
config EEPROM_FAKE
bool "Fake EEPROM driver"
default y
depends on DT_HAS_ZEPHYR_FAKE_EEPROM_ENABLED
help
Enable support for the FFF-based fake EEPROM driver.
endif # EEPROM

View file

@ -0,0 +1,78 @@
/*
* Copyright (c) 2022 Vestas Wind Systems A/S
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/device.h>
#include <zephyr/drivers/eeprom.h>
#include <zephyr/drivers/eeprom/eeprom_fake.h>
#include <zephyr/fff.h>
#ifdef CONFIG_ZTEST_NEW_API
#include <zephyr/ztest.h>
#endif /* CONFIG_ZTEST_NEW_API */
#define DT_DRV_COMPAT zephyr_fake_eeprom
struct fake_eeprom_config {
size_t size;
};
DEFINE_FAKE_VALUE_FUNC(int, fake_eeprom_read, const struct device *, off_t, void *, size_t);
DEFINE_FAKE_VALUE_FUNC(int, fake_eeprom_write, const struct device *, off_t, const void *, size_t);
DEFINE_FAKE_VALUE_FUNC(size_t, fake_eeprom_size, const struct device *);
size_t fake_eeprom_size_delegate(const struct device *dev)
{
const struct fake_eeprom_config *config = dev->config;
return config->size;
}
#ifdef CONFIG_ZTEST_NEW_API
static void fake_eeprom_reset_rule_before(const struct ztest_unit_test *test, void *fixture)
{
ARG_UNUSED(test);
ARG_UNUSED(fixture);
RESET_FAKE(fake_eeprom_read);
RESET_FAKE(fake_eeprom_write);
RESET_FAKE(fake_eeprom_size);
/* Re-install default delegate for reporting the EEPROM size */
fake_eeprom_size_fake.custom_fake = fake_eeprom_size_delegate;
}
ZTEST_RULE(fake_eeprom_reset_rule, fake_eeprom_reset_rule_before, NULL);
#endif /* CONFIG_ZTEST_NEW_API */
static const struct eeprom_driver_api fake_eeprom_driver_api = {
.read = fake_eeprom_read,
.write = fake_eeprom_write,
.size = fake_eeprom_size,
};
static int fake_eeprom_init(const struct device *dev)
{
ARG_UNUSED(dev);
/* Install default delegate for reporting the EEPROM size */
fake_eeprom_size_fake.custom_fake = fake_eeprom_size_delegate;
return 0;
}
#define FAKE_EEPROM_INIT(inst) \
static const struct fake_eeprom_config fake_eeprom_config_##inst = { \
.size = DT_INST_PROP(inst, size), \
}; \
\
DEVICE_DT_INST_DEFINE(inst, &fake_eeprom_init, NULL, NULL, \
&fake_eeprom_config_##inst, \
POST_KERNEL, CONFIG_EEPROM_INIT_PRIORITY, \
&fake_eeprom_driver_api);
DT_INST_FOREACH_STATUS_OKAY(FAKE_EEPROM_INIT)

View file

@ -0,0 +1,13 @@
# Copyright (c) 2022 Vestas Wind Systems A/S
# SPDX-License-Identifier: Apache-2.0
description: |
This binding provides a fake EEPROM for use as either a stub or a mock in Zephyr testing.
compatible: "zephyr,fake-eeprom"
include: eeprom-base.yaml
properties:
size:
required: true

View file

@ -0,0 +1,29 @@
/*
* Copyright (c) 2022 Vestas Wind Systems A/S
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_DRIVERS_EEPROM_FAKE_EEPROM_H_
#define ZEPHYR_INCLUDE_DRIVERS_EEPROM_FAKE_EEPROM_H_
#include <zephyr/drivers/eeprom.h>
#include <zephyr/fff.h>
#ifdef __cplusplus
extern "C" {
#endif
DECLARE_FAKE_VALUE_FUNC(int, fake_eeprom_read, const struct device *, off_t, void *, size_t);
DECLARE_FAKE_VALUE_FUNC(int, fake_eeprom_write, const struct device *, off_t, const void *, size_t);
DECLARE_FAKE_VALUE_FUNC(size_t, fake_eeprom_size, const struct device *);
size_t fake_eeprom_size_delegate(const struct device *dev);
#ifdef __cplusplus
}
#endif
#endif /* ZEPHYR_INCLUDE_DRIVERS_EEPROM_FAKE_EEPROM_H_ */