i2c_shell: add command to configure bus speed

Possibility to configure i2c speed through I2C_SHELL
command `i2c speed`.

Signed-off-by: Andrzej Kuros <andrzej.kuros@nordicsemi.no>
Signed-off-by: Jędrzej Ciupis <jedrzej.ciupis@nordicsemi.no>
This commit is contained in:
Andrzej Kuros 2022-10-03 12:55:33 +02:00 committed by Carles Cufí
commit bc78a03ca0

View file

@ -270,6 +270,43 @@ static int cmd_i2c_read(const struct shell *shell_ctx, size_t argc, char **argv)
return ret;
}
/* i2c speed <device> <speed>
* For: speed see constants like I2C_SPEED_STANDARD
*/
static int cmd_i2c_speed(const struct shell *shell_ctx, size_t argc, char **argv)
{
char *s_dev_name = argv[ARGV_DEV];
const struct device *dev;
uint32_t dev_config = 0;
uint32_t speed;
int ret;
dev = device_get_binding(s_dev_name);
if (!dev) {
shell_error(shell_ctx, "I2C: Device driver %s not found.",
s_dev_name);
return -ENODEV;
}
speed = strtol(argv[ARGV_DEV + 1], NULL, 10);
ret = i2c_get_config(dev, &dev_config);
if (ret == 0) {
dev_config &= ~I2C_SPEED_MASK;
dev_config |= I2C_SPEED_SET(speed);
} else {
/* Can't get current config. Fallback to something reasonable */
dev_config = I2C_MODE_CONTROLLER | I2C_SPEED_SET(speed);
}
ret = i2c_configure(dev, dev_config);
if (ret < 0) {
shell_error(shell_ctx, "I2C: Failed to configure device: %s",
s_dev_name);
return -EIO;
}
return 0;
}
static void device_name_get(size_t idx, struct shell_static_entry *entry)
{
const struct device *dev = shell_device_lookup(idx, NULL);
@ -307,6 +344,10 @@ SHELL_STATIC_SUBCMD_SET_CREATE(sub_i2c_cmds,
"Write a byte to an I2C device\n"
"Usage: write_byte <device> <addr> <reg> <value>",
cmd_i2c_write_byte, 5, 0),
SHELL_CMD_ARG(speed, &dsub_device_name,
"Configure I2C bus speed\n"
"Usage: speed <device> <speed>",
cmd_i2c_speed, 3, 0),
SHELL_SUBCMD_SET_END /* Array terminated. */
);