mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-11-02 06:01:35 +08:00
posix I2C::init() should also probe() (sync with NuttX)
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
*
|
*
|
||||||
* Copyright (c) 2012-2015 PX4 Development Team. All rights reserved.
|
* Copyright (c) 2012-2019 PX4 Development Team. All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@@ -73,26 +73,42 @@ I2C::~I2C()
|
|||||||
int
|
int
|
||||||
I2C::init()
|
I2C::init()
|
||||||
{
|
{
|
||||||
// Assume the driver set the desired bus frequency. There is no standard
|
int ret = PX4_ERROR;
|
||||||
// way to set it from user space.
|
|
||||||
|
|
||||||
// do base class init, which will create device node, etc
|
|
||||||
int ret = CDev::init();
|
|
||||||
|
|
||||||
if (ret != PX4_OK) {
|
|
||||||
DEVICE_DEBUG("CDev::init failed");
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open the actual I2C device
|
// Open the actual I2C device
|
||||||
char dev_path[16];
|
char dev_path[16] {};
|
||||||
snprintf(dev_path, sizeof(dev_path), "/dev/i2c-%i", get_device_bus());
|
snprintf(dev_path, sizeof(dev_path), "/dev/i2c-%i", get_device_bus());
|
||||||
_fd = ::open(dev_path, O_RDWR);
|
_fd = ::open(dev_path, O_RDWR);
|
||||||
|
|
||||||
if (_fd < 0) {
|
if (_fd < 0) {
|
||||||
PX4_ERR("could not open %s", dev_path);
|
DEVICE_DEBUG("failed to init I2C");
|
||||||
px4_errno = errno;
|
ret = -ENOENT;
|
||||||
return PX4_ERROR;
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
|
// call the probe function to check whether the device is present
|
||||||
|
ret = probe();
|
||||||
|
|
||||||
|
if (ret != OK) {
|
||||||
|
DEVICE_DEBUG("probe failed");
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
|
// do base class init, which will create device node, etc
|
||||||
|
ret = CDev::init();
|
||||||
|
|
||||||
|
if (ret != OK) {
|
||||||
|
DEVICE_DEBUG("cdev init failed");
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
|
// tell the world where we are
|
||||||
|
DEVICE_LOG("on I2C bus %d at 0x%02x", get_device_bus(), get_device_address());
|
||||||
|
|
||||||
|
out:
|
||||||
|
|
||||||
|
if ((ret != OK) && !(_fd < 0)) {
|
||||||
|
::close(_fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|||||||
Reference in New Issue
Block a user