Skip to content

Commit ffd568d

Browse files
nashifcarlescufi
authored andcommitted
i2c_shell: Add write_byte/read_byte commands
Add simple commands to read or write a single byte from a device register. i2c write_byte I2C_2 36 b0 12 i2c read_byte I2C_2 36 0 Output: 0x82 I modified Anas' version to put args in variables first so that the code is self-documenting. Signed-off-by: Anas Nashif <[email protected]> Signed-off-by: Simon Glass <[email protected]>
1 parent 0ecc71c commit ffd568d

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

drivers/i2c/i2c_shell.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <drivers/i2c.h>
1010
#include <string.h>
1111
#include <sys/util.h>
12+
#include <stdlib.h>
1213

1314
#include <logging/log.h>
1415
LOG_MODULE_REGISTER(i2c_shell, CONFIG_LOG_DEFAULT_LEVEL);
@@ -87,6 +88,61 @@ static int cmd_i2c_recover(const struct shell *shell,
8788
return 0;
8889
}
8990

91+
static int cmd_i2c_write_byte(const struct shell *shell,
92+
size_t argc, char **argv)
93+
{
94+
struct device *dev;
95+
int reg_addr;
96+
int dev_addr;
97+
int out_byte;
98+
99+
dev = device_get_binding(argv[1]);
100+
if (!dev) {
101+
shell_error(shell, "I2C: Device driver %s not found.",
102+
argv[1]);
103+
return -ENODEV;
104+
}
105+
106+
dev_addr = strtol(argv[2], NULL, 16);
107+
reg_addr = strtol(argv[3], NULL, 16);
108+
out_byte = strtol(argv[4], NULL, 16);
109+
110+
if (i2c_reg_write_byte(dev, dev_addr, reg_addr, out_byte) < 0) {
111+
shell_error(shell, "Failed to write to device: %s", argv[1]);
112+
return -EIO;
113+
}
114+
115+
return 0;
116+
}
117+
118+
static int cmd_i2c_read_byte(const struct shell *shell,
119+
size_t argc, char **argv)
120+
{
121+
struct device *dev;
122+
int reg_addr;
123+
int dev_addr;
124+
u8_t out;
125+
126+
dev = device_get_binding(argv[1]);
127+
if (!dev) {
128+
shell_error(shell, "I2C: Device driver %s not found.",
129+
argv[1]);
130+
return -ENODEV;
131+
}
132+
133+
dev_addr = strtol(argv[2], NULL, 16);
134+
reg_addr = strtol(argv[3], NULL, 16);
135+
136+
if (i2c_reg_read_byte(dev, dev_addr, reg_addr, &out) < 0) {
137+
shell_error(shell, "Failed to read from device: %s", argv[1]);
138+
return -EIO;
139+
}
140+
141+
shell_print(shell, "Output: 0x%x", out);
142+
143+
return 0;
144+
}
145+
90146
static void device_name_get(size_t idx, struct shell_static_entry *entry);
91147

92148
SHELL_DYNAMIC_CMD_CREATE(dsub_device_name, device_name_get);
@@ -119,6 +175,12 @@ SHELL_STATIC_SUBCMD_SET_CREATE(sub_i2c_cmds,
119175
"Scan I2C devices", cmd_i2c_scan),
120176
SHELL_CMD(recover, &dsub_device_name,
121177
"Recover I2C bus", cmd_i2c_recover),
178+
SHELL_CMD_ARG(read_byte, &dsub_device_name,
179+
"Read a byte from an I2C device",
180+
cmd_i2c_read_byte, 3, 1),
181+
SHELL_CMD_ARG(write_byte, &dsub_device_name,
182+
"Write a byte to an I2C device",
183+
cmd_i2c_write_byte, 4, 1),
122184
SHELL_SUBCMD_SET_END /* Array terminated. */
123185
);
124186

0 commit comments

Comments
 (0)