Add an argument to i2c_dump_msgs() to log the data from reads too. And
then rename the function to i2c_dump_msgs_rw() so the API doesn't
change. If the dump is done after a transaction is processed, as
opposed to before, then the read data is valid and can be very useful.
Signed-off-by: Trent Piepho <trent.piepho@igorinstitute.com>
The emulator structure currently initializes a bus-level API that is
used to communicated register accesses to the emulator from the parent
bus.
Add support for declaring a backend API for emulators. This provides
tests a common way to interact with emulators of the same device type.
Signed-off-by: Keith Short <keithshort@google.com>
Update the SBS Gauge driver that implements the fuel_gauge API to implement
a set_property function allowing the writing of an SBS word to the
manufacturer access register per the SBS spec.
Includes an update to the SBS Gauge emulator and SBS fuel gauge tests to
weakly verify the code runs.
Signed-off-by: Aaron Massey <aaronmassey@google.com>
Fix SBS I2C transfer emulation to accept reads and writes of 16 bit words
as defined by the SBS spec. This change is tested by a following commit
that implements writing and subsequently reading a written SBS property.
Signed-off-by: Aaron Massey <aaronmassey@google.com>
Implement and weakly test fetching the manufacturer access word from an SBS
compatible fuel gauge.
Signed-off-by: Aaron Massey <aaronmassey@google.com>
Add a test that validates fetching every property from the sbs gauge driver
results in no driver error codes being returned.
Signed-off-by: Aaron Massey <aaronmassey@google.com>
Add a sample sbs gauge driver with feature parity and basic tests
comparison to its sensor counter-part. Includes a simple stub test that is
extended upon.
Signed-off-by: Aaron Massey <aaronmassey@google.com>
Rename the reg_read and reg_write functions in the emul_sbs_gauge emulator
to be more unique symbols so as to support overriding this specific
function in future tests that make use of FFF. See #46817 which includes a
test for error reads as an example.
Signed-off-by: Aaron Massey <aaronmassey@google.com>
The SBS Gauge emulator would set the I2C buffer message with a value from
the emulator's custom read function even on a read error instead of just
returning. This behavior may be improperly relied upon by future tests.
Return early on register read before setting the i2c message buffer value
in the emulator.
Signed-off-by: Aaron Massey <aaronmassey@google.com>
To facilitate testing improper reads and writes on various registers,
enable the emul_sbs_gauge reg_read/write handlers to return custom exit
codes.
Signed-off-by: Aaron Massey <aaronmassey@google.com>
Add an initial bare-bones functionality sbs_gauge emulator that only
supports register reads tested by the current sbs_gauge driver tests and
what the sbs_driver currently supports.
Signed-off-by: Aaron Massey <aaronmassey@google.com>
Those files do not contain anything of relevance since
fa90b5c243 [emul: spi: bmi160: Move to
top-level directory].
Signed-off-by: Reto Schneider <reto.schneider@husqvarnagroup.com>
The data_host variable in the emul_espi_host_send_cw function should be
assigned the espi host emulator data and not the espi_emul data.
Assign the variable to correct reference.
Signed-off-by: Aaron Massey <aaronmassey@google.com>
Rework the <BUS>_emul_register calls to not pass the name param. The
name param is only used for logging and we can get it from the
struct <BUS>_emul instead.
Signed-off-by: Kumar Gala <galak@kernel.org>
The bus_label is not used and is using DT_INST_BUS_LABEL which is
now deprecated and thus causes an error when running CI.
Signed-off-by: Kumar Gala <galak@kernel.org>
DT nodes aren't guaranteed to define a label property. But emulated bus
controllers currently make use of this property to dispatch to the
associated emulator.
Have emulated bus controllers use DEVICE_DT_GET(node_id) to dispatch to
right target peripheral emulator. This also change makes emul_get_binding
and device_get_binding synonymous in behavior with respect to their
parameters.
This also strictly enforces a 1:1 correspondence between invocations of
DEVICE_DT_DEFINE and EMUL_DEFINE.
Signed-off-by: Aaron Massey <aaronmassey@google.com>
Some paramaters in prior emulators have historically referred to instances
of struct emul as "emulator" or "emul". The proper parameter name for this
type is "target" as it designates the struct emul instance is the emul
bus (e.g. emul_i2c) controller's target peripheral for dispatch.
Do a small refactor renaming some of these parameters to "target".
TEST=twister on accel, espi, and eeprom drivers tests
Signed-off-by: Aaron Massey <aaronmassey@google.com>
In several locations of the emulator code there are unused function
arguments that were never caught.
Declare these as unused or remove the unused function parameters entirely.
Signed-off-by: Aaron Massey <aaronmassey@google.com>
Allow emulator creators to write an init function that can be used
across multiple busses so as to reduce the boilerplate and cognitive
load in creating an emulator.
Part of this change includes allowing access to the emul struct from a
field in a {bus}_struct api (e.g. i2c_struct), which removes the need for
sporadic usages of CONTAINER_OF to access the emul struct.
Overall, this change simplifies and reduces the amount of boilerplate
code to get a device emulator up and running, thus reducing excise work
to writing tests.
TEST=twister on accel,espi, and eeprom drivers tests
Signed-off-by: Aaron Massey <aaronmassey@google.com>
Run clang-format on all files touched by improved emulator API pull-request
that allowed access to the target device emulator from its bus api without
CONTAINER_OF usage.
drivers/i2c/i2c_emul.c
drivers/spi/spi_emul.c
include/zephyr/drivers/emul.h
include/zephyr/drivers/espi_emul.h
include/zephyr/drivers/i2c_emul.h
include/zephyr/drivers/spi_emul.h
subsys/emul/emul.c
subsys/emul/emul_bmi160.c
subsys/emul/espi/emul_espi_host.c
subsys/emul/i2c/emul_atmel_at24.c
TEST=twister on accel,espi, and eeprom drivers tests
Signed-off-by: Aaron Massey <aaronmassey@google.com>
The bus_label field of the struct was not used, so remove it. This
lets us remove one of the last users of DT_INST_BUS_LABEL.
Signed-off-by: Kumar Gala <galak@kernel.org>
In order to bring consistency in-tree, migrate all subsystems code to
the new prefix <zephyr/...>. Note that the conversion has been scripted,
refer to zephyrproject-rtos#45388 for more details.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
Add get_config function to I2C emulator.
Also update tests using I2C emulator to use i2c_get_config.
Signed-off-by: Dawid Niedzwiecki <dn@semihalf.com>
Emulators are difficult to work with, they generally require maintaining
some state in a mutable data struct. Since the emulator struct doesn't
support a data field like devices do, the pattern seems to be to add it
to the configuration. This makes following the logic of where things are
difficult.
1. Add a `struct emul *parent` structure to the espi/i2c/spi emulator
structs to make it easier when casting up.
2. Add a `void *data` field to `struct emul` to hold the data for
emulators.
Signed-off-by: Yuval Peress <peress@chromium.org>
Issue #38271
Implement a getter for emulators similar to device_get_binding. This
function can be used to get the emulator instance during tests to call
emulator specific functions.
Example: The current BMI160 emulator pre-defines a finite set of data
samples that will be returned. If a test was to be written for logic
that uses that data, then the emulator would become completely useless
without the ability for the test to define what data should be returned.
This will also help in exercising error conditions in tests.
Signed-off-by: Yuval Peress <peress@chromium.org>
Move emul.h out of the top level include/ dir into
include/drivers/emul.h and deprecated the old location.
Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
This emulator pretends a generic eSPI Host. It supports basic virtual
wires and port80 operations.
There are functions to trigger actions on the host side e.g. for
setting a virtual wire from the host to the eSPI slave, use
emul_espi_host_send_vw. It will prepare data and set a proper event
on the slave side which will trigger callback (if there is any).
Signed-off-by: Dawid Niedzwiecki <dn@semihalf.com>
When testing the bmi160 I've come across an issue where the readings
didn't make sense to me. The issue comes from reading the
BMI160_SAMPLE_BURST_READ_ADDR which is 0x0C assuming both accelerometer
and gyroscope. At this point we would normally read 12 bytes
(2 bytes per sample * 3 axes * 2 sensors). This reading takes place in
bmi160_sample_fetch and begins writing to data->sample.raw
Without this change, the first byte written is actually to the dummy
byte which effectively gets tossed. The issue is that this is the
GYR_X<7:0>(LSB) according to the BMI160 data sheet. When we later call
either bmi160_gyr_channel_get or bmi160_acc_channel_get we're looking
at sample.gyr and sample.acc (which is effectively shiften by 1 byte).
This change gets rid of the dummy byte which re-alignes gyr with the
start of the raw buffer.
Signed-off-by: Yuval Peress <peress@chromium.org>
If a read is requested without a read buffer, the emulator currently
crashes. Fix this by adding a check.
Fixes: #29703Fixes: #29702Fixes: #29017Fixes: #29016
Signed-off-by: Simon Glass <sjg@chromium.org>
At present this driver only supports SPI. Refactor it so that SPI is
just one of the options. This does not change any functionality.
Signed-off-by: Simon Glass <sjg@chromium.org>
This emulator currently only supports SPI. Before making it also
support I2C, move it up a directory to avoid I2C uses missing it.
Signed-off-by: Simon Glass <sjg@chromium.org>
Move the logging for transactions to the functions that actually access
registers and sample data. This avoids needed to repeat this code when
I2C starts calling these functions.
Signed-off-by: Simon Glass <sjg@chromium.org>
Update the language to refer to a bus instead of SPI. This will make it
more applicable with I2C support is added.
Signed-off-by: Simon Glass <sjg@chromium.org>
This emulator supports enable functionality to start up the device and
read a few samples. It connects itself to any BMI160 device it finds in
the device tree. The SPI emulation controller driver is used to direct
SPI messages from the BMI160 driver to the BMI160 emulator.
Add a few more definitions to the header file, as needed.
Signed-off-by: Simon Glass <sjg@chromium.org>
Add a log message to indicate that a new emulator is attached. Also add
a message in the assert to make it clearer what has gone wrong.
Signed-off-by: Simon Glass <sjg@chromium.org>
Now that device_api attribute is unmodified at runtime, as well as all
the other attributes, it is possible to switch all device driver
instance to be constant.
A coccinelle rule is used for this:
@r_const_dev_1
disable optional_qualifier
@
@@
-struct device *
+const struct device *
@r_const_dev_2
disable optional_qualifier
@
@@
-struct device * const
+const struct device *
Fixes#27399
Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
This emulator supports basic reads and writes with 8-bit addressing.
It connects itself to any AT24 devices it finds in the device tree. The
I2C emulation controller driver is used to direct I2C messages from the
AT24 driver to the AT24 emulator.
Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
Create a header file and implementation for emulators. Set up a linker
list so that emulators can be found and initialised at start-up.
Emulators are used to emulate hardware devices, to support testing of
various subsystems. For example, it is possible to write an emulator
for an I2C compass such that it appears on the I2C bus and can be used
just like a real hardware device.
Emulators often implement special features for testing. For example a
compass may support returning bogus data if the I2C bus speed is too
high, or may return invalid measurements if calibration has not yet
been completed. This allows for testing that high-level code can
handle these situations correctly. Test coverage can therefore
approach 100% if all failure conditions are emulated.
Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
Signed-off-by: Simon Glass <sjg@chromium.org>