41 #include <px4_platform_common/px4_config.h> 42 #include <px4_platform_common/log.h> 43 #include <px4_platform_common/module.h> 44 #include <px4_platform_common/getopt.h> 45 #include <px4_platform_common/i2c.h> 52 printf(
"Scanning I2C bus: %d\n", bus);
57 struct i2c_master_s *i2c_dev = px4_i2cbus_initialize(bus);
59 if (i2c_dev ==
nullptr) {
60 PX4_ERR(
"invalid bus %d", bus);
64 printf(
" 0 1 2 3 4 5 6 7 8 9 a b c d e f\n");
66 for (
int i = 0; i < 128; i += 16) {
69 for (
int j = 0; j < 16; j++) {
75 unsigned retry_count = 0;
76 const unsigned retries = 5;
83 px4_i2c_msg_t msgv[2];
86 msgv[0].frequency = 100000;
93 msgv[1].frequency = 100000;
95 msgv[1].flags = I2C_M_READ;
99 ret = I2C_TRANSFER(i2c_dev, &msgv[0], 2);
108 if ((retry_count >= 1) || (retry_count >= retries)) {
112 }
while (retry_count++ < retries);
115 printf(
"%02x ", addr);
125 px4_i2cbus_uninitialize(i2c_dev);
130 int usage(
const char *reason =
nullptr)
133 PX4_ERR(
"%s", reason);
136 PRINT_MODULE_DESCRIPTION(
"Utility to scan for I2C devices on a particular bus.");
138 PRINT_MODULE_USAGE_NAME_SIMPLE(
"i2cdetect",
"command");
139 PRINT_MODULE_USAGE_PARAM_INT(
'b', 1, 4, PX4_I2C_BUS_EXPANSION,
"I2C bus",
true);
152 int i2c_bus = PX4_I2C_BUS_EXPANSION;
156 const char *myoptarg =
nullptr;
158 while ((ch = px4_getopt(argc, argv,
"b:", &myoptind, &myoptarg)) != EOF) {
162 i2c_bus = strtol(myoptarg,
nullptr, 0);
__EXPORT int i2cdetect_main(int argc, char *argv[])
static int send_data(int uart, uint8_t *buffer, size_t size)
int usage(const char *reason=nullptr)
Print the correct usage.
static int recv_data(int uart, uint8_t *buffer, size_t *size, uint8_t *id)