Skip to content

Commit 9360150

Browse files
sjg20carlescufi
authored andcommitted
i2c_shell: Add write/read multiple-byte commands
Allow writing multiple registers (or a single 16-bit register) and reading multiple registers. i2c read I2C_2 36 40 00000000: 12 34 00 00 0F 3D 80 80 AF 9C 17 00 90 01 00 00 i2c write I2C_2 36 40 12 34 56 78 90 12 34 56 78 90 i2c read I2C_2 36 40 00000000: 12 34 00 00 90 12 80 80 AF 9C 17 00 90 01 00 00 Signed-off-by: Simon Glass <[email protected]>
1 parent ffd568d commit 9360150

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

drivers/i2c/i2c_shell.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ LOG_MODULE_REGISTER(i2c_shell, CONFIG_LOG_DEFAULT_LEVEL);
1919
extern struct device __device_start[];
2020
extern struct device __device_end[];
2121

22+
/* Maximum bytes we can write or read at once */
23+
#define MAX_I2C_BYTES 16
24+
2225
static int cmd_i2c_scan(const struct shell *shell,
2326
size_t argc, char **argv)
2427
{
@@ -88,6 +91,42 @@ static int cmd_i2c_recover(const struct shell *shell,
8891
return 0;
8992
}
9093

94+
/* i2c write <device> <dev_addr> [<byte1>, ...] */
95+
static int cmd_i2c_write(const struct shell *shell, size_t argc, char **argv)
96+
{
97+
u8_t buf[MAX_I2C_BYTES];
98+
struct device *dev;
99+
int num_bytes;
100+
int reg_addr;
101+
int dev_addr;
102+
int i;
103+
104+
dev = device_get_binding(argv[1]);
105+
if (!dev) {
106+
shell_error(shell, "I2C: Device driver %s not found.", argv[1]);
107+
return -ENODEV;
108+
}
109+
110+
dev_addr = strtol(argv[2], NULL, 16);
111+
reg_addr = strtol(argv[3], NULL, 16);
112+
num_bytes = argc - 4;
113+
if (num_bytes < 0)
114+
return 0;
115+
if (num_bytes > MAX_I2C_BYTES) {
116+
num_bytes = MAX_I2C_BYTES;
117+
}
118+
for (i = 0; i < num_bytes; i++) {
119+
buf[i] = (uint8_t)strtol(argv[4 + i], NULL, 16);
120+
}
121+
122+
if (i2c_burst_write(dev, dev_addr, reg_addr, buf, num_bytes) < 0) {
123+
shell_error(shell, "Failed to write to device: %s", argv[1]);
124+
return -EIO;
125+
}
126+
127+
return 0;
128+
}
129+
91130
static int cmd_i2c_write_byte(const struct shell *shell,
92131
size_t argc, char **argv)
93132
{
@@ -143,6 +182,41 @@ static int cmd_i2c_read_byte(const struct shell *shell,
143182
return 0;
144183
}
145184

185+
/* i2c read <device> <dev_addr> [<numbytes>] */
186+
static int cmd_i2c_read(const struct shell *shell, size_t argc, char **argv)
187+
{
188+
u8_t buf[MAX_I2C_BYTES];
189+
struct device *dev;
190+
int num_bytes;
191+
int reg_addr;
192+
int dev_addr;
193+
194+
dev = device_get_binding(argv[1]);
195+
if (!dev) {
196+
shell_error(shell, "I2C: Device driver %s not found.", argv[1]);
197+
return -ENODEV;
198+
}
199+
200+
dev_addr = strtol(argv[2], NULL, 16);
201+
reg_addr = strtol(argv[3], NULL, 16);
202+
if (argc > 4) {
203+
num_bytes = strtol(argv[4], NULL, 16);
204+
if (num_bytes > MAX_I2C_BYTES)
205+
num_bytes = MAX_I2C_BYTES;
206+
} else {
207+
num_bytes = MAX_I2C_BYTES;
208+
}
209+
210+
if (i2c_burst_read(dev, dev_addr, reg_addr, buf, num_bytes) < 0) {
211+
shell_error(shell, "Failed to read from device: %s", argv[1]);
212+
return -EIO;
213+
}
214+
215+
shell_hexdump(shell, buf, num_bytes);
216+
217+
return 0;
218+
}
219+
146220
static void device_name_get(size_t idx, struct shell_static_entry *entry);
147221

148222
SHELL_DYNAMIC_CMD_CREATE(dsub_device_name, device_name_get);
@@ -175,9 +249,15 @@ SHELL_STATIC_SUBCMD_SET_CREATE(sub_i2c_cmds,
175249
"Scan I2C devices", cmd_i2c_scan),
176250
SHELL_CMD(recover, &dsub_device_name,
177251
"Recover I2C bus", cmd_i2c_recover),
252+
SHELL_CMD_ARG(read, &dsub_device_name,
253+
"Read bytes from an I2C device",
254+
cmd_i2c_read, 3, MAX_I2C_BYTES),
178255
SHELL_CMD_ARG(read_byte, &dsub_device_name,
179256
"Read a byte from an I2C device",
180257
cmd_i2c_read_byte, 3, 1),
258+
SHELL_CMD_ARG(write, &dsub_device_name,
259+
"Write bytes to an I2C device",
260+
cmd_i2c_write, 3, MAX_I2C_BYTES),
181261
SHELL_CMD_ARG(write_byte, &dsub_device_name,
182262
"Write a byte to an I2C device",
183263
cmd_i2c_write_byte, 4, 1),

0 commit comments

Comments
 (0)