mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-17 15:13:34 +08:00
board support add px4_i2c_bus_external/px4_spi_bus_external
This commit is contained in:
committed by
Lorenz Meier
parent
2aeb4aa55f
commit
53595bac0e
@@ -137,30 +137,32 @@ public:
|
||||
*
|
||||
* @return The bus ID
|
||||
*/
|
||||
virtual uint8_t get_device_bus() { return _device_id.devid_s.bus; }
|
||||
uint8_t get_device_bus() { return _device_id.devid_s.bus; }
|
||||
|
||||
/**
|
||||
* Return the bus type the device is connected to.
|
||||
*
|
||||
* @return The bus type
|
||||
*/
|
||||
virtual DeviceBusType get_device_bus_type() { return _device_id.devid_s.bus_type; }
|
||||
DeviceBusType get_device_bus_type() { return _device_id.devid_s.bus_type; }
|
||||
|
||||
/**
|
||||
* Return the bus address of the device.
|
||||
*
|
||||
* @return The bus address
|
||||
*/
|
||||
virtual uint8_t get_device_address() { return _device_id.devid_s.address; }
|
||||
uint8_t get_device_address() { return _device_id.devid_s.address; }
|
||||
|
||||
void set_device_address(int address) { _device_id.devid_s.address = address; }
|
||||
|
||||
/**
|
||||
* Set the device type
|
||||
*
|
||||
* @return The device type
|
||||
*/
|
||||
virtual void set_device_type(uint8_t devtype) { _device_id.devid_s.devtype = devtype; }
|
||||
void set_device_type(uint8_t devtype) { _device_id.devid_s.devtype = devtype; }
|
||||
|
||||
//virtual bool external() = 0;
|
||||
virtual bool external() { return false; }
|
||||
|
||||
/*
|
||||
broken out device elements. The bitfields are used to keep
|
||||
|
||||
@@ -3,9 +3,10 @@
|
||||
|
||||
#include <cinttypes>
|
||||
|
||||
#include <px4_micro_hal.h>
|
||||
#include <nuttx/arch.h>
|
||||
|
||||
#include <px4_micro_hal.h>
|
||||
|
||||
#define ATOMIC_ENTER irqstate_t flags = px4_enter_critical_section()
|
||||
#define ATOMIC_LEAVE px4_leave_critical_section(flags)
|
||||
|
||||
|
||||
@@ -61,9 +61,6 @@ I2C::I2C(const char *name,
|
||||
// public
|
||||
// protected
|
||||
_retries(0),
|
||||
// private
|
||||
_bus(bus),
|
||||
_address(address),
|
||||
_frequency(frequency),
|
||||
_dev(nullptr)
|
||||
{
|
||||
@@ -108,7 +105,7 @@ I2C::init()
|
||||
unsigned bus_index;
|
||||
|
||||
// attach to the i2c bus
|
||||
_dev = px4_i2cbus_initialize(_bus);
|
||||
_dev = px4_i2cbus_initialize(get_device_bus());
|
||||
|
||||
if (_dev == nullptr) {
|
||||
DEVICE_DEBUG("failed to init I2C");
|
||||
@@ -118,7 +115,7 @@ I2C::init()
|
||||
|
||||
// the above call fails for a non-existing bus index,
|
||||
// so the index math here is safe.
|
||||
bus_index = _bus - 1;
|
||||
bus_index = get_device_bus() - 1;
|
||||
|
||||
// abort if the max frequency we allow (the frequency we ask)
|
||||
// is smaller than the bus frequency
|
||||
@@ -126,7 +123,7 @@ I2C::init()
|
||||
(void)px4_i2cbus_uninitialize(_dev);
|
||||
_dev = nullptr;
|
||||
DEVICE_LOG("FAIL: too slow for bus #%u: %u KHz, device max: %u KHz)",
|
||||
_bus, _bus_clocks[bus_index] / 1000, _frequency / 1000);
|
||||
get_device_bus(), _bus_clocks[bus_index] / 1000, _frequency / 1000);
|
||||
ret = -EINVAL;
|
||||
goto out;
|
||||
}
|
||||
@@ -165,7 +162,7 @@ I2C::init()
|
||||
|
||||
// tell the world where we are
|
||||
DEVICE_LOG("on I2C bus %d at 0x%02x (bus: %u KHz, max: %u KHz)",
|
||||
_bus, _address, _bus_clocks[bus_index] / 1000, _frequency / 1000);
|
||||
get_device_bus(), get_device_address(), _bus_clocks[bus_index] / 1000, _frequency / 1000);
|
||||
|
||||
out:
|
||||
|
||||
@@ -198,8 +195,8 @@ I2C::transfer(const uint8_t *send, unsigned send_len, uint8_t *recv, unsigned re
|
||||
msgs = 0;
|
||||
|
||||
if (send_len > 0) {
|
||||
msgv[msgs].frequency = _bus_clocks[_bus - 1];
|
||||
msgv[msgs].addr = _address;
|
||||
msgv[msgs].frequency = _bus_clocks[get_device_bus() - 1];
|
||||
msgv[msgs].addr = get_device_address();
|
||||
msgv[msgs].flags = 0;
|
||||
msgv[msgs].buffer = const_cast<uint8_t *>(send);
|
||||
msgv[msgs].length = send_len;
|
||||
@@ -207,8 +204,8 @@ I2C::transfer(const uint8_t *send, unsigned send_len, uint8_t *recv, unsigned re
|
||||
}
|
||||
|
||||
if (recv_len > 0) {
|
||||
msgv[msgs].frequency = _bus_clocks[_bus - 1];
|
||||
msgv[msgs].addr = _address;
|
||||
msgv[msgs].frequency = _bus_clocks[get_device_bus() - 1];
|
||||
msgv[msgs].addr = get_device_address();
|
||||
msgv[msgs].flags = I2C_M_READ;
|
||||
msgv[msgs].buffer = recv;
|
||||
msgv[msgs].length = recv_len;
|
||||
@@ -245,8 +242,8 @@ I2C::transfer(i2c_msg_s *msgv, unsigned msgs)
|
||||
|
||||
/* force the device address and Frequency into the message vector */
|
||||
for (unsigned i = 0; i < msgs; i++) {
|
||||
msgv[i].frequency = _bus_clocks[_bus - 1];
|
||||
msgv[i].addr = _address;
|
||||
msgv[i].frequency = _bus_clocks[get_device_address() - 1];
|
||||
msgv[i].addr = get_device_address();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -55,11 +55,6 @@ class __EXPORT I2C : public CDev
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Get the address
|
||||
*/
|
||||
int16_t get_address() const { return _address; }
|
||||
|
||||
static int set_bus_clock(unsigned bus, unsigned clock_hz);
|
||||
|
||||
static unsigned int _bus_clocks[BOARD_NUMBER_I2C_BUSES];
|
||||
@@ -71,11 +66,6 @@ protected:
|
||||
*/
|
||||
unsigned _retries;
|
||||
|
||||
/**
|
||||
* The I2C bus number the device is attached to.
|
||||
*/
|
||||
int _bus;
|
||||
|
||||
/**
|
||||
* @ Constructor
|
||||
*
|
||||
@@ -111,8 +101,7 @@ protected:
|
||||
* @return OK if the transfer was successful, -errno
|
||||
* otherwise.
|
||||
*/
|
||||
int transfer(const uint8_t *send, unsigned send_len,
|
||||
uint8_t *recv, unsigned recv_len);
|
||||
int transfer(const uint8_t *send, unsigned send_len, uint8_t *recv, unsigned recv_len);
|
||||
|
||||
/**
|
||||
* Perform a multi-part I2C transaction to the device.
|
||||
@@ -124,22 +113,9 @@ protected:
|
||||
*/
|
||||
int transfer(px4_i2c_msg_t *msgv, unsigned msgs);
|
||||
|
||||
/**
|
||||
* Change the bus address.
|
||||
*
|
||||
* Most often useful during probe() when the driver is testing
|
||||
* several possible bus addresses.
|
||||
*
|
||||
* @param address The new bus address to set.
|
||||
*/
|
||||
void set_address(uint16_t address)
|
||||
{
|
||||
_address = address;
|
||||
_device_id.devid_s.address = _address;
|
||||
}
|
||||
bool external() { return px4_i2c_bus_external(_device_id.devid_s.bus); }
|
||||
|
||||
private:
|
||||
uint16_t _address;
|
||||
uint32_t _frequency;
|
||||
px4_i2c_dev_t *_dev;
|
||||
|
||||
|
||||
@@ -71,8 +71,7 @@ SPI::SPI(const char *name,
|
||||
_device(device),
|
||||
_mode(mode),
|
||||
_frequency(frequency),
|
||||
_dev(nullptr),
|
||||
_bus(bus)
|
||||
_dev(nullptr)
|
||||
{
|
||||
// fill in _device_id fields for a SPI device
|
||||
_device_id.devid_s.bus_type = DeviceBusType_SPI;
|
||||
@@ -94,7 +93,7 @@ SPI::init()
|
||||
|
||||
/* attach to the spi bus */
|
||||
if (_dev == nullptr) {
|
||||
_dev = px4_spibus_initialize(_bus);
|
||||
_dev = px4_spibus_initialize(get_device_bus());
|
||||
}
|
||||
|
||||
if (_dev == nullptr) {
|
||||
@@ -123,7 +122,7 @@ SPI::init()
|
||||
}
|
||||
|
||||
/* tell the workd where we are */
|
||||
DEVICE_LOG("on SPI bus %d at %d (%u KHz)", _bus, PX4_SPI_DEV_ID(_device), _frequency / 1000);
|
||||
DEVICE_LOG("on SPI bus %d at %d (%u KHz)", get_device_bus(), PX4_SPI_DEV_ID(_device), _frequency / 1000);
|
||||
|
||||
out:
|
||||
return ret;
|
||||
|
||||
@@ -106,7 +106,7 @@ extern "C" {
|
||||
return dev;
|
||||
}
|
||||
|
||||
int register_driver(const char *name, const device::px4_file_operations_t *fops, mode_t mode, void *data)
|
||||
int register_driver(const char *name, const device::px4_file_operations_t *fops, device::mode_t mode, void *data)
|
||||
{
|
||||
PX4_DEBUG("CDev::register_driver %s", name);
|
||||
int ret = 0;
|
||||
|
||||
@@ -40,7 +40,8 @@
|
||||
* that is supplied. Should we just depend on the bus knowing?
|
||||
*/
|
||||
|
||||
#include "i2c.h"
|
||||
#include "i2c_posix.h"
|
||||
|
||||
#ifdef __PX4_LINUX
|
||||
#include <linux/i2c.h>
|
||||
#include <linux/i2c-dev.h>
|
||||
@@ -72,8 +73,6 @@ I2C::I2C(const char *name,
|
||||
// protected
|
||||
_retries(0),
|
||||
// private
|
||||
_bus(bus),
|
||||
_address(address),
|
||||
_fd(-1)
|
||||
{
|
||||
DEVICE_DEBUG("I2C::I2C name = %s devname = %s", name, devname);
|
||||
@@ -119,7 +118,7 @@ I2C::init()
|
||||
|
||||
// Open the actual I2C device
|
||||
char dev_path[16];
|
||||
snprintf(dev_path, sizeof(dev_path), "/dev/i2c-%i", _bus);
|
||||
snprintf(dev_path, sizeof(dev_path), "/dev/i2c-%i", get_device_bus());
|
||||
_fd = ::open(dev_path, O_RDWR);
|
||||
|
||||
if (_fd < 0) {
|
||||
@@ -156,7 +155,7 @@ I2C::transfer(const uint8_t *send, unsigned send_len, uint8_t *recv, unsigned re
|
||||
msgs = 0;
|
||||
|
||||
if (send_len > 0) {
|
||||
msgv[msgs].addr = _address;
|
||||
msgv[msgs].addr = get_device_address();
|
||||
msgv[msgs].flags = 0;
|
||||
msgv[msgs].buf = const_cast<uint8_t *>(send);
|
||||
msgv[msgs].len = send_len;
|
||||
@@ -164,7 +163,7 @@ I2C::transfer(const uint8_t *send, unsigned send_len, uint8_t *recv, unsigned re
|
||||
}
|
||||
|
||||
if (recv_len > 0) {
|
||||
msgv[msgs].addr = _address;
|
||||
msgv[msgs].addr = get_device_address();
|
||||
msgv[msgs].flags = I2C_M_READ;
|
||||
msgv[msgs].buf = recv;
|
||||
msgv[msgs].len = recv_len;
|
||||
@@ -217,7 +216,7 @@ I2C::transfer(struct i2c_msg *msgv, unsigned msgs)
|
||||
|
||||
/* force the device address into the message vector */
|
||||
for (unsigned i = 0; i < msgs; i++) {
|
||||
msgv[i].addr = _address;
|
||||
msgv[i].addr = get_device_address();
|
||||
}
|
||||
|
||||
do {
|
||||
|
||||
@@ -60,11 +60,6 @@ class __EXPORT I2C : public CDev
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Get the address
|
||||
*/
|
||||
int16_t get_address() const { return _address; }
|
||||
|
||||
protected:
|
||||
/**
|
||||
* The number of times a read or write operation will be retried on
|
||||
@@ -72,11 +67,6 @@ protected:
|
||||
*/
|
||||
unsigned _retries;
|
||||
|
||||
/**
|
||||
* The I2C bus number the device is attached to.
|
||||
*/
|
||||
int _bus;
|
||||
|
||||
/**
|
||||
* @ Constructor
|
||||
*
|
||||
@@ -110,8 +100,7 @@ protected:
|
||||
* @return OK if the transfer was successful, -errno
|
||||
* otherwise.
|
||||
*/
|
||||
int transfer(const uint8_t *send, unsigned send_len,
|
||||
uint8_t *recv, unsigned recv_len);
|
||||
int transfer(const uint8_t *send, unsigned send_len, uint8_t *recv, unsigned recv_len);
|
||||
|
||||
/**
|
||||
* Perform a multi-part I2C transaction to the device.
|
||||
@@ -123,22 +112,9 @@ protected:
|
||||
*/
|
||||
int transfer(struct i2c_msg *msgv, unsigned msgs);
|
||||
|
||||
/**
|
||||
* Change the bus address.
|
||||
*
|
||||
* Most often useful during probe() when the driver is testing
|
||||
* several possible bus addresses.
|
||||
*
|
||||
* @param address The new bus address to set.
|
||||
*/
|
||||
void set_address(uint16_t address)
|
||||
{
|
||||
_address = address;
|
||||
_device_id.devid_s.address = _address;
|
||||
}
|
||||
bool external() { return px4_i2c_bus_external(_device_id.devid_s.bus); }
|
||||
|
||||
private:
|
||||
uint16_t _address;
|
||||
int _fd;
|
||||
std::string _dname;
|
||||
|
||||
|
||||
@@ -142,10 +142,10 @@ private:
|
||||
SPI operator=(const SPI &);
|
||||
|
||||
protected:
|
||||
int _bus;
|
||||
|
||||
int _transfer(uint8_t *send, uint8_t *recv, unsigned len);
|
||||
|
||||
bool external() { return px4_spi_bus_external(get_device_bus()); }
|
||||
|
||||
};
|
||||
|
||||
} // namespace device
|
||||
|
||||
Reference in New Issue
Block a user