Skip to content

Commit 3703027

Browse files
tbursztykahansbinderup
authored andcommitted
i2c: Add target related commands to the shell module
For testing/debugging purposes, it will be possible then to register or unregister an i2c target. Signed-off-by: Tomasz Bursztyka <tobu@bang-olufsen.dk> Co-authored-by: Hans Binderup <habi@bang-olufsen.dk>
1 parent a62f157 commit 3703027

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

drivers/i2c/i2c_shell.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,58 @@ static int cmd_i2c_speed(const struct shell *sh, size_t argc, char **argv)
319319
return 0;
320320
}
321321

322+
/* i2c target register <device> */
323+
static int cmd_i2c_target_register(const struct shell *sh,
324+
size_t argc, char **argv)
325+
{
326+
char *s_dev_name = argv[ARGV_DEV];
327+
const struct device *dev;
328+
int ret;
329+
330+
dev = shell_device_get_binding(s_dev_name);
331+
if (!dev) {
332+
shell_error(sh, "I2C: Device driver %s not found.", s_dev_name);
333+
return -ENODEV;
334+
}
335+
336+
ret = i2c_target_driver_register(dev);
337+
if (ret < 0) {
338+
shell_error(sh, "I2C: Failed to register %s with err=%d",
339+
s_dev_name, ret);
340+
return ret;
341+
}
342+
343+
shell_print(sh, "I2C: Successfully registered %s", s_dev_name);
344+
345+
return 0;
346+
}
347+
348+
/* i2c target unregister <device> */
349+
static int cmd_i2c_target_unregister(const struct shell *sh,
350+
size_t argc, char **argv)
351+
{
352+
char *s_dev_name = argv[ARGV_DEV];
353+
const struct device *dev;
354+
int ret;
355+
356+
dev = shell_device_get_binding(s_dev_name);
357+
if (!dev) {
358+
shell_error(sh, "I2C: Device driver %s not found.", s_dev_name);
359+
return -ENODEV;
360+
}
361+
362+
ret = i2c_target_driver_unregister(dev);
363+
if (ret < 0) {
364+
shell_error(sh, "I2C: Failed to unregister %s with err=%d",
365+
s_dev_name, ret);
366+
return ret;
367+
}
368+
369+
shell_print(sh, "I2C: Successfully unregistered %s", s_dev_name);
370+
371+
return 0;
372+
}
373+
322374
static bool device_is_i2c(const struct device *dev)
323375
{
324376
#ifdef CONFIG_I3C
@@ -340,6 +392,19 @@ static void device_name_get(size_t idx, struct shell_static_entry *entry)
340392

341393
SHELL_DYNAMIC_CMD_CREATE(dsub_device_name, device_name_get);
342394

395+
SHELL_STATIC_SUBCMD_SET_CREATE(
396+
sub_i2c_target,
397+
SHELL_CMD_ARG(register, &dsub_device_name,
398+
"Register an i2c-target on its respective bus.\n"
399+
"Usage: target register <device>",
400+
cmd_i2c_target_register, 2, 0),
401+
SHELL_CMD_ARG(unregister, &dsub_device_name,
402+
"Unegister an i2c-target from its respective bus.\n"
403+
"Usage: target unregister <device>",
404+
cmd_i2c_target_unregister, 2, 0),
405+
SHELL_SUBCMD_SET_END /* Array terminated. */
406+
);
407+
343408
SHELL_STATIC_SUBCMD_SET_CREATE(sub_i2c_cmds,
344409
SHELL_CMD_ARG(scan, &dsub_device_name,
345410
"Scan I2C devices\n"
@@ -374,6 +439,9 @@ SHELL_STATIC_SUBCMD_SET_CREATE(sub_i2c_cmds,
374439
"Configure I2C bus speed\n"
375440
"Usage: speed <device> <speed>",
376441
cmd_i2c_speed, 3, 0),
442+
SHELL_CMD_ARG(target, &sub_i2c_target,
443+
"Subcommands operating on i2c targets.",
444+
NULL, 3, 0),
377445
SHELL_SUBCMD_SET_END /* Array terminated. */
378446
);
379447

0 commit comments

Comments
 (0)