2016-07-14 07:07:24 -03:00
|
|
|
/*
|
|
|
|
* This file is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include "I2CDevice.h"
|
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
|
|
|
#include "Util.h"
|
2016-10-31 04:57:44 -03:00
|
|
|
#include "Scheduler.h"
|
|
|
|
|
2016-07-14 07:07:24 -03:00
|
|
|
namespace PX4 {
|
|
|
|
|
|
|
|
uint8_t PX4::PX4_I2C::instance;
|
2017-10-26 01:08:29 -03:00
|
|
|
pthread_mutex_t PX4::PX4_I2C::instance_lock;
|
2016-07-14 07:07:24 -03:00
|
|
|
|
2016-11-27 18:53:19 -04:00
|
|
|
DeviceBus I2CDevice::businfo[I2CDevice::num_buses];
|
|
|
|
|
2016-10-31 04:57:44 -03:00
|
|
|
/*
|
|
|
|
constructor for I2C wrapper class
|
|
|
|
*/
|
|
|
|
PX4_I2C::PX4_I2C(uint8_t bus) :
|
2016-11-19 21:18:24 -04:00
|
|
|
I2C(devname, devpath, map_bus_number(bus), 0, 100000UL)
|
2016-10-31 04:57:44 -03:00
|
|
|
{}
|
|
|
|
|
|
|
|
/*
|
|
|
|
map ArduPilot bus numbers to PX4 bus numbers
|
|
|
|
*/
|
|
|
|
uint8_t PX4_I2C::map_bus_number(uint8_t bus) const
|
|
|
|
{
|
|
|
|
switch (bus) {
|
|
|
|
case 0:
|
|
|
|
// map to internal bus
|
|
|
|
#ifdef PX4_I2C_BUS_ONBOARD
|
|
|
|
return PX4_I2C_BUS_ONBOARD;
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
// map to expansion bus
|
|
|
|
#ifdef PX4_I2C_BUS_EXPANSION
|
|
|
|
return PX4_I2C_BUS_EXPANSION;
|
|
|
|
#else
|
|
|
|
return 1;
|
|
|
|
#endif
|
2017-04-19 17:13:39 -03:00
|
|
|
case 2:
|
|
|
|
// map to expansion bus 2
|
|
|
|
#ifdef PX4_I2C_BUS_EXPANSION1
|
|
|
|
return PX4_I2C_BUS_EXPANSION1;
|
|
|
|
#else
|
|
|
|
return 2;
|
|
|
|
#endif
|
2016-10-31 04:57:44 -03:00
|
|
|
}
|
|
|
|
// default to bus 1
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-07-14 07:07:24 -03:00
|
|
|
/*
|
|
|
|
implement wrapper for PX4 I2C driver
|
|
|
|
*/
|
2017-04-17 23:28:47 -03:00
|
|
|
bool PX4_I2C::do_transfer(uint8_t address, const uint8_t *send, uint32_t send_len, uint8_t *recv, uint32_t recv_len, bool split_transfers)
|
2016-07-14 07:07:24 -03:00
|
|
|
{
|
2016-10-31 04:57:44 -03:00
|
|
|
set_address(address);
|
2016-07-14 07:07:24 -03:00
|
|
|
if (!init_done) {
|
2017-10-26 01:08:29 -03:00
|
|
|
if (pthread_mutex_lock(&instance_lock) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-14 07:07:24 -03:00
|
|
|
init_done = true;
|
|
|
|
// we do late init() so we can setup the device paths
|
2017-10-26 01:08:29 -03:00
|
|
|
|
2016-07-14 07:07:24 -03:00
|
|
|
snprintf(devname, sizeof(devname), "AP_I2C_%u", instance);
|
|
|
|
snprintf(devpath, sizeof(devpath), "/dev/api2c%u", instance);
|
|
|
|
init_ok = (init() == OK);
|
|
|
|
if (init_ok) {
|
|
|
|
instance++;
|
|
|
|
}
|
2017-10-26 01:08:29 -03:00
|
|
|
pthread_mutex_unlock(&instance_lock);
|
2016-07-14 07:07:24 -03:00
|
|
|
}
|
|
|
|
if (!init_ok) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-17 23:28:47 -03:00
|
|
|
|
|
|
|
if (split_transfers) {
|
|
|
|
/*
|
|
|
|
splitting the transfer() into two pieces avoids a stop condition
|
|
|
|
with SCL low which is not supported on some devices (such as
|
|
|
|
LidarLite blue label)
|
|
|
|
*/
|
|
|
|
if (send && send_len) {
|
|
|
|
if (transfer(send, send_len, nullptr, 0) != OK) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-11 17:59:16 -04:00
|
|
|
}
|
2017-04-17 23:28:47 -03:00
|
|
|
if (recv && recv_len) {
|
|
|
|
if (transfer(nullptr, 0, recv, recv_len) != OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// combined transfer
|
|
|
|
if (transfer(send, send_len, recv, recv_len) != OK) {
|
2016-11-11 17:59:16 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2016-07-14 07:07:24 -03:00
|
|
|
}
|
2016-10-31 04:57:44 -03:00
|
|
|
|
2016-07-14 07:07:24 -03:00
|
|
|
I2CDevice::I2CDevice(uint8_t bus, uint8_t address) :
|
2016-10-31 04:57:44 -03:00
|
|
|
_busnum(bus),
|
|
|
|
_px4dev(_busnum),
|
2016-07-14 07:07:24 -03:00
|
|
|
_address(address)
|
|
|
|
{
|
2016-12-01 05:58:40 -04:00
|
|
|
set_device_bus(bus);
|
2016-11-04 06:23:20 -03:00
|
|
|
set_device_address(address);
|
2016-11-08 00:18:57 -04:00
|
|
|
asprintf(&pname, "I2C:%u:%02x",
|
|
|
|
(unsigned)bus, (unsigned)address);
|
|
|
|
perf = perf_alloc(PC_ELAPSED, pname);
|
2016-07-14 07:07:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
I2CDevice::~I2CDevice()
|
|
|
|
{
|
2016-11-06 21:41:49 -04:00
|
|
|
printf("I2C device bus %u address 0x%02x closed\n",
|
|
|
|
(unsigned)_busnum, (unsigned)_address);
|
2016-11-08 00:18:57 -04:00
|
|
|
perf_free(perf);
|
|
|
|
free(pname);
|
2016-07-14 07:07:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool I2CDevice::transfer(const uint8_t *send, uint32_t send_len,
|
|
|
|
uint8_t *recv, uint32_t recv_len)
|
|
|
|
{
|
2016-11-08 00:18:57 -04:00
|
|
|
perf_begin(perf);
|
2017-04-17 23:28:47 -03:00
|
|
|
bool ret = _px4dev.do_transfer(_address, send, send_len, recv, recv_len, _split_transfers);
|
2016-11-08 00:18:57 -04:00
|
|
|
perf_end(perf);
|
|
|
|
return ret;
|
2016-07-14 07:07:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool I2CDevice::read_registers_multiple(uint8_t first_reg, uint8_t *recv,
|
|
|
|
uint32_t recv_len, uint8_t times)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-31 04:57:44 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
register a periodic callback
|
|
|
|
*/
|
|
|
|
AP_HAL::Device::PeriodicHandle I2CDevice::register_periodic_callback(uint32_t period_usec, AP_HAL::Device::PeriodicCb cb)
|
|
|
|
{
|
2016-11-03 19:48:36 -03:00
|
|
|
if (_busnum >= num_buses) {
|
2016-10-31 04:57:44 -03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-11-03 19:48:36 -03:00
|
|
|
struct DeviceBus &binfo = businfo[_busnum];
|
2016-11-27 19:18:52 -04:00
|
|
|
return binfo.register_periodic_callback(period_usec, cb, this);
|
2016-10-31 04:57:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
adjust a periodic callback
|
|
|
|
*/
|
|
|
|
bool I2CDevice::adjust_periodic_callback(AP_HAL::Device::PeriodicHandle h, uint32_t period_usec)
|
|
|
|
{
|
2017-03-01 04:18:40 -04:00
|
|
|
if (_busnum >= num_buses) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DeviceBus &binfo = businfo[_busnum];
|
|
|
|
|
|
|
|
return binfo.adjust_timer(h, period_usec);
|
2016-10-31 04:57:44 -03:00
|
|
|
}
|
2017-03-01 04:18:40 -04:00
|
|
|
|
2016-07-14 07:07:24 -03:00
|
|
|
AP_HAL::OwnPtr<AP_HAL::I2CDevice>
|
2018-01-10 00:26:58 -04:00
|
|
|
I2CDeviceManager::get_device(uint8_t bus, uint8_t address,
|
|
|
|
uint32_t bus_clock,
|
|
|
|
bool use_smbus,
|
|
|
|
uint32_t timeout_ms)
|
2016-07-14 07:07:24 -03:00
|
|
|
{
|
|
|
|
auto dev = AP_HAL::OwnPtr<AP_HAL::I2CDevice>(new I2CDevice(bus, address));
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|