[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[RFC PATCH v2 13/23] test/unit: add i2c-tester
|
From: |
Octavian Purdila |
|
Subject: |
[RFC PATCH v2 13/23] test/unit: add i2c-tester |
|
Date: |
Sat, 17 Aug 2024 03:25:56 -0700 |
Add a simple i2c peripheral to be used for testing I2C device
models. The peripheral has a fixed number of registers that can be
read and written.
Signed-off-by: Octavian Purdila <tavip@google.com>
---
tests/unit/i2c_tester.h | 30 +++++++++++
tests/unit/i2c_tester.c | 108 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 138 insertions(+)
create mode 100644 tests/unit/i2c_tester.h
create mode 100644 tests/unit/i2c_tester.c
diff --git a/tests/unit/i2c_tester.h b/tests/unit/i2c_tester.h
new file mode 100644
index 0000000000..14cf2ead36
--- /dev/null
+++ b/tests/unit/i2c_tester.h
@@ -0,0 +1,30 @@
+/*
+ *
+ * Copyright (c) 2024 Google LLC
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef TESTS_UNIT_I2C_TESTER_H
+#define TESTS_UNIT_I2C_TESTER_H
+
+#include "qemu/osdep.h"
+#include "hw/i2c/i2c.h"
+#include "hw/irq.h"
+
+#define I2C_TESTER_NUM_REGS 0x31
+
+#define TYPE_I2C_TESTER "i2c_tester"
+#define I2C_TESTER(obj) OBJECT_CHECK(I2cTesterState, (obj), TYPE_I2C_TESTER)
+
+typedef struct {
+ I2CSlave i2c;
+ bool set_reg_idx;
+ uint8_t reg_idx;
+ uint8_t regs[I2C_TESTER_NUM_REGS];
+} I2cTesterState;
+
+#endif /* TESTS_UNIT_I2C_TESTER_H */
diff --git a/tests/unit/i2c_tester.c b/tests/unit/i2c_tester.c
new file mode 100644
index 0000000000..2e4314fe6e
--- /dev/null
+++ b/tests/unit/i2c_tester.c
@@ -0,0 +1,108 @@
+/*
+ * Simple I2C peripheral for testing I2C device models.
+ *
+ * Copyright (c) 2024 Google LLC
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "i2c_tester.h"
+#include "qemu/log.h"
+#include "qemu/module.h"
+
+static void i2c_tester_reset(DeviceState *ds)
+{
+ I2cTesterState *s = I2C_TESTER(ds);
+
+ s->set_reg_idx = false;
+ s->reg_idx = 0;
+ memset(s->regs, 0, I2C_TESTER_NUM_REGS);
+}
+
+static int i2c_tester_event(I2CSlave *i2c, enum i2c_event event)
+{
+ I2cTesterState *s = I2C_TESTER(i2c);
+
+ if (event == I2C_START_SEND) {
+ s->set_reg_idx = true;
+ }
+
+ return 0;
+}
+
+static uint8_t i2c_tester_rx(I2CSlave *i2c)
+{
+ I2cTesterState *s = I2C_TESTER(i2c);
+
+ if (s->reg_idx >= I2C_TESTER_NUM_REGS) {
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid reg 0x%02x\n", __func__,
+ s->reg_idx);
+ return I2C_NACK;
+ }
+
+ return s->regs[s->reg_idx];
+}
+
+static int i2c_tester_tx(I2CSlave *i2c, uint8_t data)
+{
+ I2cTesterState *s = I2C_TESTER(i2c);
+
+ if (s->set_reg_idx) {
+ /* Setting the register in which the operation will be done. */
+ s->reg_idx = data;
+ s->set_reg_idx = false;
+ return 0;
+ }
+
+ if (s->reg_idx >= I2C_TESTER_NUM_REGS) {
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid reg 0x%02x\n", __func__,
+ s->reg_idx);
+ return I2C_NACK;
+ }
+
+ /* Write reg data. */
+ s->regs[s->reg_idx] = data;
+
+ return 0;
+}
+
+static void i2c_tester_init(Object *obj)
+{
+}
+
+static void i2c_tester_realize(DeviceState *ds, Error **errp)
+{
+}
+
+static void i2c_tester_unrealize(DeviceState *dev)
+{
+}
+
+static void i2c_tester_class_init(ObjectClass *oc, void *data)
+{
+ DeviceClass *dc = DEVICE_CLASS(oc);
+ I2CSlaveClass *isc = I2C_SLAVE_CLASS(oc);
+
+ dc->reset = i2c_tester_reset;
+ dc->realize = i2c_tester_realize;
+ dc->unrealize = i2c_tester_unrealize;
+
+ isc->event = i2c_tester_event;
+ isc->recv = i2c_tester_rx;
+ isc->send = i2c_tester_tx;
+}
+
+static const TypeInfo i2c_tester_types[] = {
+ {
+ .name = TYPE_I2C_TESTER,
+ .parent = TYPE_I2C_SLAVE,
+ .instance_size = sizeof(I2cTesterState),
+ .instance_init = i2c_tester_init,
+ .class_init = i2c_tester_class_init
+ },
+};
+
+DEFINE_TYPES(i2c_tester_types);
--
2.46.0.184.g6999bdac58-goog
- [RFC PATCH v2 00/23] NXP i.MX RT595, ARM SVD and device model unit tests, Octavian Purdila, 2024/08/17
- [RFC PATCH v2 02/23] tests/unit: add fifo test, Octavian Purdila, 2024/08/17
- [RFC PATCH v2 01/23] fifo32: add peek function, Octavian Purdila, 2024/08/17
- [RFC PATCH v2 05/23] hw: add register access utility functions, Octavian Purdila, 2024/08/17
- [RFC PATCH v2 03/23] scripts: add script to generate C header files from SVD XML files, Octavian Purdila, 2024/08/17
- [RFC PATCH v2 10/23] hw/char: add support for flexcomm usart, Octavian Purdila, 2024/08/17
- [RFC PATCH v2 04/23] Add mcux-soc-svd subproject, Octavian Purdila, 2024/08/17
- [RFC PATCH v2 13/23] test/unit: add i2c-tester,
Octavian Purdila <=
- [RFC PATCH v2 09/23] test/unit: add flexcomm unit test, Octavian Purdila, 2024/08/17
- [RFC PATCH v2 11/23] test/unit: add flexcomm usart unit test, Octavian Purdila, 2024/08/17
- [RFC PATCH v2 06/23] hw/misc: add basic flexcomm device model, Octavian Purdila, 2024/08/17
- [RFC PATCH v2 08/23] test/unit: add register access macros and functions, Octavian Purdila, 2024/08/17
- [RFC PATCH v2 07/23] tests/unit: add system bus mock, Octavian Purdila, 2024/08/17
- [RFC PATCH v2 16/23] test/unit: add spi-tester, Octavian Purdila, 2024/08/17
- [RFC PATCH v2 12/23] hw/i2c: add support for flexcomm i2c, Octavian Purdila, 2024/08/17
- [RFC PATCH v2 20/23] hw/ssi: add support for flexspi, Octavian Purdila, 2024/08/17
- [RFC PATCH v2 17/23] test/unit: add unit tests for flexcomm spi, Octavian Purdila, 2024/08/17
- [RFC PATCH v2 14/23] test/unit: add unit tests for flexcomm i2c, Octavian Purdila, 2024/08/17