|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Prevas A/S |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <shell/shell.h> |
| 8 | +#include <stdlib.h> |
| 9 | +#include <i2c.h> |
| 10 | +#include <string.h> |
| 11 | +#include <sys/util.h> |
| 12 | + |
| 13 | +#include <logging/log.h> |
| 14 | +LOG_MODULE_REGISTER(i2c_shell, CONFIG_LOG_DEFAULT_LEVEL); |
| 15 | + |
| 16 | +#define I2C_DEVICE_PREFIX "I2C_" |
| 17 | + |
| 18 | +extern struct device __device_init_start[]; |
| 19 | +extern struct device __device_init_end[]; |
| 20 | + |
| 21 | +static int cmd_i2c_scan(const struct shell *shell, |
| 22 | + size_t argc, char **argv) |
| 23 | +{ |
| 24 | + struct device *dev; |
| 25 | + u8_t cnt = 0, first = 0x04, last = 0x77; |
| 26 | + |
| 27 | + dev = device_get_binding(argv[1]); |
| 28 | + |
| 29 | + if (!dev) { |
| 30 | + shell_error(shell, "I2C: Device driver %s not found.", |
| 31 | + argv[1]); |
| 32 | + return -ENODEV; |
| 33 | + } |
| 34 | + |
| 35 | + shell_print(shell, |
| 36 | + " 0 1 2 3 4 5 6 7 8 9 a b c d e f"); |
| 37 | + for (u8_t i = 0; i <= last; i += 16) { |
| 38 | + shell_fprintf(shell, SHELL_NORMAL, "%02x: ", i); |
| 39 | + for (u8_t j = 0; j < 16; j++) { |
| 40 | + if (i + j < first || i + j > last) { |
| 41 | + shell_fprintf(shell, SHELL_NORMAL, " "); |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + struct i2c_msg msgs[1]; |
| 46 | + u8_t dst; |
| 47 | + |
| 48 | + /* Send the address to read from */ |
| 49 | + msgs[0].buf = &dst; |
| 50 | + msgs[0].len = 0U; |
| 51 | + msgs[0].flags = I2C_MSG_WRITE | I2C_MSG_STOP; |
| 52 | + if (i2c_transfer(dev, &msgs[0], 1, i + j) == 0) { |
| 53 | + shell_fprintf(shell, SHELL_NORMAL, |
| 54 | + "%02x ", i + j); |
| 55 | + ++cnt; |
| 56 | + } else { |
| 57 | + shell_fprintf(shell, SHELL_NORMAL, "-- "); |
| 58 | + } |
| 59 | + } |
| 60 | + shell_print(shell, ""); |
| 61 | + } |
| 62 | + |
| 63 | + shell_print(shell, "%u devices found on %s", |
| 64 | + cnt, argv[1]); |
| 65 | + |
| 66 | + return 0; |
| 67 | +} |
| 68 | + |
| 69 | +static void device_name_get(size_t idx, struct shell_static_entry *entry); |
| 70 | + |
| 71 | +SHELL_DYNAMIC_CMD_CREATE(dsub_device_name, device_name_get); |
| 72 | + |
| 73 | +static void device_name_get(size_t idx, struct shell_static_entry *entry) |
| 74 | +{ |
| 75 | + int device_idx = 0; |
| 76 | + struct device *dev; |
| 77 | + |
| 78 | + entry->syntax = NULL; |
| 79 | + entry->handler = NULL; |
| 80 | + entry->help = NULL; |
| 81 | + entry->subcmd = &dsub_device_name; |
| 82 | + |
| 83 | + for (dev = __device_init_start; dev != __device_init_end; dev++) { |
| 84 | + if ((dev->driver_api != NULL) && |
| 85 | + strstr(dev->config->name, I2C_DEVICE_PREFIX) != NULL && |
| 86 | + strcmp(dev->config->name, "") && (dev->config->name != NULL)) { |
| 87 | + if (idx == device_idx) { |
| 88 | + entry->syntax = dev->config->name; |
| 89 | + break; |
| 90 | + } |
| 91 | + device_idx++; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +SHELL_STATIC_SUBCMD_SET_CREATE(sub_i2c_cmds, |
| 97 | + SHELL_CMD(scan, &dsub_device_name, |
| 98 | + "Scan I2C devices", cmd_i2c_scan), |
| 99 | + SHELL_SUBCMD_SET_END /* Array terminated. */ |
| 100 | + ); |
| 101 | + |
| 102 | +SHELL_CMD_REGISTER(i2c, &sub_i2c_cmds, "I2C commands", NULL); |
0 commit comments