mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-19 05:03:34 +08:00
deprecate df_bmp280_wrapper and replace with in tree bmp280
- deprecate DriverFramework bmp280 driver (df_bmp280_wrapper) - update beaglebone blue and snapdragon flight eagle boards to use in tree bmp280 - update posix (really just linux) and qurt I2C wrappers - tested on beaglebone blue
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2012-2015 PX4 Development Team. All rights reserved.
|
||||
* Copyright (c) 2012-2020 PX4 Development Team. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@@ -32,7 +32,7 @@
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file i2c.cpp
|
||||
* @file I2C.cpp
|
||||
*
|
||||
* Base class for devices attached via the I2C bus.
|
||||
*
|
||||
@@ -42,6 +42,8 @@
|
||||
|
||||
#include "I2C.hpp"
|
||||
|
||||
#include <nuttx/i2c/i2c_master.h>
|
||||
|
||||
namespace device
|
||||
{
|
||||
/*
|
||||
@@ -169,19 +171,19 @@ out:
|
||||
int
|
||||
I2C::transfer(const uint8_t *send, const unsigned send_len, uint8_t *recv, const unsigned recv_len)
|
||||
{
|
||||
px4_i2c_msg_t msgv[2];
|
||||
unsigned msgs;
|
||||
int ret = PX4_ERROR;
|
||||
unsigned retry_count = 0;
|
||||
|
||||
if (_dev == nullptr) {
|
||||
PX4_ERR("I2C device not opened");
|
||||
return 1;
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
do {
|
||||
DEVICE_DEBUG("transfer out %p/%u in %p/%u", send, send_len, recv, recv_len);
|
||||
msgs = 0;
|
||||
|
||||
px4_i2c_msg_t msgv[2] {};
|
||||
unsigned msgs = 0;
|
||||
|
||||
if (send_len > 0) {
|
||||
msgv[msgs].frequency = _bus_clocks[get_device_bus() - 1];
|
||||
@@ -205,10 +207,15 @@ I2C::transfer(const uint8_t *send, const unsigned send_len, uint8_t *recv, const
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
ret = I2C_TRANSFER(_dev, &msgv[0], msgs);
|
||||
int ret_transfer = I2C_TRANSFER(_dev, &msgv[0], msgs);
|
||||
|
||||
/* success */
|
||||
if (ret == PX4_OK) {
|
||||
if (ret_transfer != 0) {
|
||||
DEVICE_DEBUG("I2C transfer failed, result %d", ret_transfer);
|
||||
ret = PX4_ERROR;
|
||||
|
||||
} else {
|
||||
// success
|
||||
ret = PX4_OK;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2012-2019 PX4 Development Team. All rights reserved.
|
||||
* Copyright (c) 2016-2020 PX4 Development Team. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@@ -32,7 +32,7 @@
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file i2c.cpp
|
||||
* @file I2C.cpp
|
||||
*
|
||||
* Base class for devices attached via the I2C bus.
|
||||
*
|
||||
@@ -109,6 +109,7 @@ out:
|
||||
|
||||
if ((ret != OK) && !(_fd < 0)) {
|
||||
::close(_fd);
|
||||
_fd = -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
@@ -117,19 +118,19 @@ out:
|
||||
int
|
||||
I2C::transfer(const uint8_t *send, const unsigned send_len, uint8_t *recv, const unsigned recv_len)
|
||||
{
|
||||
struct i2c_msg msgv[2];
|
||||
unsigned msgs;
|
||||
int ret = PX4_ERROR;
|
||||
unsigned retry_count = 0;
|
||||
|
||||
if (_fd < 0) {
|
||||
PX4_ERR("I2C device not opened");
|
||||
return 1;
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
do {
|
||||
DEVICE_DEBUG("transfer out %p/%u in %p/%u", send, send_len, recv, recv_len);
|
||||
msgs = 0;
|
||||
|
||||
unsigned msgs = 0;
|
||||
struct i2c_msg msgv[2] {};
|
||||
|
||||
if (send_len > 0) {
|
||||
msgv[msgs].addr = get_device_address();
|
||||
@@ -151,24 +152,19 @@ I2C::transfer(const uint8_t *send, const unsigned send_len, uint8_t *recv, const
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
struct i2c_rdwr_ioctl_data packets;
|
||||
|
||||
i2c_rdwr_ioctl_data packets{};
|
||||
packets.msgs = msgv;
|
||||
|
||||
packets.nmsgs = msgs;
|
||||
|
||||
ret = ::ioctl(_fd, I2C_RDWR, (unsigned long)&packets);
|
||||
int ret_ioctl = ::ioctl(_fd, I2C_RDWR, (unsigned long)&packets);
|
||||
|
||||
if (ret == -1) {
|
||||
if (ret_ioctl == -1) {
|
||||
DEVICE_DEBUG("I2C transfer failed");
|
||||
ret = PX4_ERROR;
|
||||
|
||||
} else {
|
||||
// success
|
||||
ret = PX4_OK;
|
||||
}
|
||||
|
||||
/* success */
|
||||
if (ret == PX4_OK) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (C) 2012 PX4 Development Team. All rights reserved.
|
||||
* Copyright (C) 2016-2020 PX4 Development Team. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@@ -32,7 +32,7 @@
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file i2c.h
|
||||
* @file I2C.hpp
|
||||
*
|
||||
* Base class for devices connected via I2C.
|
||||
*/
|
||||
@@ -44,11 +44,6 @@
|
||||
|
||||
#include <px4_platform_common/i2c.h>
|
||||
|
||||
#ifdef __PX4_LINUX
|
||||
#include <linux/i2c.h>
|
||||
#include <linux/i2c-dev.h>
|
||||
#endif
|
||||
|
||||
namespace device __EXPORT
|
||||
{
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2012-2015 PX4 Development Team. All rights reserved.
|
||||
* Copyright (c) 2016-2020 PX4 Development Team. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@@ -32,7 +32,7 @@
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file i2c.cpp
|
||||
* @file I2C.cpp
|
||||
*
|
||||
* Base class for devices attached via the I2C bus.
|
||||
*
|
||||
@@ -42,13 +42,14 @@
|
||||
|
||||
#include "I2C.hpp"
|
||||
|
||||
#include "dev_fs_lib_i2c.h"
|
||||
#include <dev_fs_lib_i2c.h>
|
||||
|
||||
namespace device
|
||||
{
|
||||
|
||||
I2C::I2C(const char *name, const char *devname, const int bus, const uint16_t address, const uint32_t frequency) :
|
||||
CDev(name, devname)
|
||||
CDev(name, devname),
|
||||
_frequency(frequency)
|
||||
{
|
||||
DEVICE_DEBUG("I2C::I2C name = %s devname = %s", name, devname);
|
||||
// fill in _device_id fields for a I2C device
|
||||
@@ -70,26 +71,43 @@ I2C::~I2C()
|
||||
int
|
||||
I2C::init()
|
||||
{
|
||||
// Assume the driver set the desired bus frequency. There is no standard
|
||||
// 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;
|
||||
}
|
||||
int ret = PX4_ERROR;
|
||||
|
||||
// Open the actual I2C device
|
||||
char dev_path[16];
|
||||
char dev_path[16] {};
|
||||
snprintf(dev_path, sizeof(dev_path), "/dev/iic-%i", get_device_bus());
|
||||
_fd = ::open(dev_path, O_RDWR);
|
||||
|
||||
if (_fd < 0) {
|
||||
PX4_ERR("could not open %s", dev_path);
|
||||
px4_errno = errno;
|
||||
return PX4_ERROR;
|
||||
DEVICE_DEBUG("failed to init I2C");
|
||||
ret = -ENOENT;
|
||||
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);
|
||||
_fd = -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
@@ -98,21 +116,50 @@ I2C::init()
|
||||
int
|
||||
I2C::transfer(const uint8_t *send, const unsigned send_len, uint8_t *recv, const unsigned recv_len)
|
||||
{
|
||||
dspal_i2c_ioctl_combined_write_read ioctl_write_read{};
|
||||
int ret = PX4_ERROR;
|
||||
unsigned retry_count = 0;
|
||||
|
||||
ioctl_write_read.write_buf = (uint8_t *)send;
|
||||
ioctl_write_read.write_buf_len = send_len;
|
||||
ioctl_write_read.read_buf = recv;
|
||||
ioctl_write_read.read_buf_len = recv_len;
|
||||
|
||||
int bytes_read = ::ioctl(_fd, I2C_IOCTL_RDWR, &ioctl_write_read);
|
||||
|
||||
if (bytes_read != (ssize_t)recv_len) {
|
||||
PX4_ERR("read register reports a read of %d bytes, but attempted to read %d bytes", bytes_read, recv_len);
|
||||
return -1;
|
||||
if (_fd < 0) {
|
||||
PX4_ERR("I2C device not opened");
|
||||
return PX4_ERROR;
|
||||
}
|
||||
|
||||
return 0;
|
||||
do {
|
||||
DEVICE_DEBUG("transfer out %p/%u in %p/%u", send, send_len, recv, recv_len);
|
||||
|
||||
dspal_i2c_ioctl_slave_config slave_config{};
|
||||
slave_config.slave_address = get_device_address();
|
||||
slave_config.bus_frequency_in_khz = _frequency / 1000;
|
||||
slave_config.byte_transer_timeout_in_usecs = 10000; // 10 ms
|
||||
int ret_config = ::ioctl(_fd, I2C_IOCTL_SLAVE, &slave_config);
|
||||
|
||||
if (ret_config < 0) {
|
||||
DEVICE_DEBUG("Could not set slave config, result: %d", ret_config);
|
||||
}
|
||||
|
||||
|
||||
dspal_i2c_ioctl_combined_write_read ioctl_write_read{};
|
||||
|
||||
ioctl_write_read.write_buf = (uint8_t *)send;
|
||||
ioctl_write_read.write_buf_len = send_len;
|
||||
ioctl_write_read.read_buf = recv;
|
||||
ioctl_write_read.read_buf_len = recv_len;
|
||||
|
||||
int bytes_read = ::ioctl(_fd, I2C_IOCTL_RDWR, &ioctl_write_read);
|
||||
|
||||
if (bytes_read != (int)recv_len) {
|
||||
DEVICE_DEBUG("I2C transfer failed, bytes read %d", bytes_read);
|
||||
ret = PX4_ERROR;
|
||||
|
||||
} else {
|
||||
// success
|
||||
ret = PX4_OK;
|
||||
break;
|
||||
}
|
||||
|
||||
} while (retry_count++ < _retries);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
} // namespace device
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (C) 2012 PX4 Development Team. All rights reserved.
|
||||
* Copyright (C) 2016-2020 PX4 Development Team. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@@ -32,7 +32,7 @@
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file i2c.h
|
||||
* @file I2C.hpp
|
||||
*
|
||||
* Base class for devices connected via I2C.
|
||||
*/
|
||||
@@ -44,11 +44,6 @@
|
||||
|
||||
#include <px4_platform_common/i2c.h>
|
||||
|
||||
#ifdef __PX4_LINUX
|
||||
#include <linux/i2c.h>
|
||||
#include <linux/i2c-dev.h>
|
||||
#endif
|
||||
|
||||
namespace device __EXPORT
|
||||
{
|
||||
|
||||
@@ -109,6 +104,7 @@ protected:
|
||||
virtual bool external() const override { return px4_i2c_bus_external(_device_id.devid_s.bus); }
|
||||
|
||||
private:
|
||||
uint32_t _frequency{0};
|
||||
int _fd{-1};
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user