i2c: emul: Add get_config function

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>
This commit is contained in:
Dawid Niedzwiecki 2021-10-12 10:27:09 +02:00 committed by Carles Cufí
parent 51c3e0de79
commit 65277d4770
4 changed files with 52 additions and 22 deletions

View file

@ -28,13 +28,6 @@ struct i2c_emul_data {
uint32_t config;
};
uint32_t i2c_emul_get_config(const struct device *dev)
{
struct i2c_emul_data *data = dev->data;
return data->config;
}
/**
* Find an emulator by its I2C address
*
@ -69,6 +62,19 @@ static int i2c_emul_configure(const struct device *dev, uint32_t dev_config)
return 0;
}
static int i2c_emul_get_config(const struct device *dev, uint32_t *dev_config)
{
struct i2c_emul_data *data = dev->data;
if (data->config == -1) {
return -EIO;
}
*dev_config = data->config;
return 0;
}
static int i2c_emul_transfer(const struct device *dev, struct i2c_msg *msgs,
uint8_t num_msgs, uint16_t addr)
{
@ -108,6 +114,9 @@ static int i2c_emul_init(const struct device *dev)
rc = emul_init_for_bus_from_list(dev, list);
/* Set config to an uninitialized state */
data->config = -1;
return rc;
}
@ -127,6 +136,7 @@ int i2c_emul_register(const struct device *dev, const char *name,
static struct i2c_driver_api i2c_emul_api = {
.configure = i2c_emul_configure,
.get_config = i2c_emul_get_config,
.transfer = i2c_emul_transfer,
};

View file

@ -77,14 +77,6 @@ struct i2c_emul_api {
i2c_emul_transfer_t transfer;
};
/**
* Back door to allow an emulator to retrieve the host configuration.
*
* @param dev I2C device associated with the emulator
* @return Bit-packed 32-bit value containing the device's runtime configuration
*/
uint32_t i2c_emul_get_config(const struct device *dev);
#ifdef __cplusplus
}
#endif

View file

@ -61,6 +61,7 @@ static int at24_emul_transfer(struct i2c_emul *emul, struct i2c_msg *msgs,
const struct at24_emul_cfg *cfg;
unsigned int len;
bool too_fast;
uint32_t i2c_cfg;
data = CONTAINER_OF(emul, struct at24_emul_data, emul);
cfg = emul->parent->cfg;
@ -71,9 +72,12 @@ static int at24_emul_transfer(struct i2c_emul *emul, struct i2c_msg *msgs,
return -EIO;
}
if (i2c_get_config(data->i2c, &i2c_cfg)) {
LOG_ERR("i2c_get_config failed");
return -EIO;
}
/* For testing purposes, fail if the bus speed is above standard */
too_fast = (I2C_SPEED_GET(i2c_emul_get_config(data->i2c)) >
I2C_SPEED_STANDARD);
too_fast = (I2C_SPEED_GET(i2c_cfg) > I2C_SPEED_STANDARD);
if (too_fast) {
LOG_ERR("Speed too high");
return -EIO;

View file

@ -7,6 +7,7 @@
#include <ztest.h>
#include <ztest_test.h>
#include <drivers/eeprom.h>
#include <drivers/i2c.h>
#include <device.h>
/* There is no obvious way to pass this to tests, so use a global */
@ -182,13 +183,24 @@ static void test_zero_length_write(void)
}
/* Run all of our tests on EEPROM device with the given label */
static void run_tests_on_eeprom(const char *label)
static void run_tests_on_eeprom(const char *label_eeprom, const char *label_i2c)
{
const struct device *eeprom = device_get_binding(label);
const struct device *eeprom = device_get_binding(label_eeprom);
const struct device *i2c;
zassert_not_null(eeprom, "Unable to get EEPROM device");
k_object_access_grant(eeprom, k_current_get());
eeprom_label = label;
eeprom_label = label_eeprom;
if (label_i2c != NULL) {
i2c = device_get_binding(label_i2c);
if (i2c != NULL) {
/* If the test is using I2C, configure it */
k_object_access_grant(i2c, k_current_get());
i2c_configure(i2c, I2C_MODE_MASTER |
I2C_SPEED_SET(I2C_SPEED_STANDARD));
}
}
ztest_test_suite(eeprom_api,
ztest_user_unit_test(test_size),
ztest_user_unit_test(test_out_of_bounds),
@ -202,10 +214,22 @@ static void run_tests_on_eeprom(const char *label)
void test_main(void)
{
run_tests_on_eeprom(DT_LABEL(DT_ALIAS(eeprom_0)));
const char *i2c_eeprom_0;
#ifdef DT_N_ALIAS_eeprom_1
const char *i2c_eeprom_1;
#endif
i2c_eeprom_0 = COND_CODE_1(DT_NODE_EXISTS(DT_BUS(DT_ALIAS(eeprom_0))),
(DT_BUS_LABEL(DT_ALIAS(eeprom_0))), (NULL));
#ifdef DT_N_ALIAS_eeprom_1
i2c_eeprom_1 = COND_CODE_1(DT_NODE_EXISTS(DT_BUS(DT_ALIAS(eeprom_1))),
(DT_BUS_LABEL(DT_ALIAS(eeprom_1))), (NULL));
#endif
run_tests_on_eeprom(DT_LABEL(DT_ALIAS(eeprom_0)), i2c_eeprom_0);
#ifdef DT_N_ALIAS_eeprom_1
run_tests_on_eeprom(DT_LABEL(DT_ALIAS(eeprom_1)));
run_tests_on_eeprom(DT_LABEL(DT_ALIAS(eeprom_1)), i2c_eeprom_1);
#endif
}