|
9 | 9 | #include <drivers/i2c.h>
|
10 | 10 | #include <string.h>
|
11 | 11 | #include <sys/util.h>
|
| 12 | +#include <stdlib.h> |
12 | 13 |
|
13 | 14 | #include <logging/log.h>
|
14 | 15 | LOG_MODULE_REGISTER(i2c_shell, CONFIG_LOG_DEFAULT_LEVEL);
|
@@ -87,6 +88,61 @@ static int cmd_i2c_recover(const struct shell *shell,
|
87 | 88 | return 0;
|
88 | 89 | }
|
89 | 90 |
|
| 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 | + |
90 | 146 | static void device_name_get(size_t idx, struct shell_static_entry *entry);
|
91 | 147 |
|
92 | 148 | SHELL_DYNAMIC_CMD_CREATE(dsub_device_name, device_name_get);
|
@@ -119,6 +175,12 @@ SHELL_STATIC_SUBCMD_SET_CREATE(sub_i2c_cmds,
|
119 | 175 | "Scan I2C devices", cmd_i2c_scan),
|
120 | 176 | SHELL_CMD(recover, &dsub_device_name,
|
121 | 177 | "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), |
122 | 184 | SHELL_SUBCMD_SET_END /* Array terminated. */
|
123 | 185 | );
|
124 | 186 |
|
|
0 commit comments