2013-09-22 03:01:24 -03:00
|
|
|
|
|
|
|
#include <AP_HAL.h>
|
2013-09-28 03:28:36 -03:00
|
|
|
|
2014-07-06 23:03:26 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
|
2013-09-22 03:01:24 -03:00
|
|
|
#include "I2CDriver.h"
|
|
|
|
|
2013-09-27 21:00:49 -03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <linux/i2c-dev.h>
|
2013-09-30 23:23:57 -03:00
|
|
|
#ifndef I2C_SMBUS_BLOCK_MAX
|
|
|
|
#include <linux/i2c.h>
|
2014-07-06 23:03:26 -03:00
|
|
|
#endif
|
|
|
|
|
2013-09-22 03:01:24 -03:00
|
|
|
using namespace Linux;
|
|
|
|
|
2013-09-27 21:00:49 -03:00
|
|
|
/*
|
|
|
|
constructor
|
|
|
|
*/
|
|
|
|
LinuxI2CDriver::LinuxI2CDriver(AP_HAL::Semaphore* semaphore, const char *device) :
|
|
|
|
_semaphore(semaphore),
|
|
|
|
_fd(-1),
|
|
|
|
_device(device)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
called from HAL class init()
|
|
|
|
*/
|
|
|
|
void LinuxI2CDriver::begin()
|
|
|
|
{
|
|
|
|
if (_fd != -1) {
|
|
|
|
close(_fd);
|
|
|
|
}
|
|
|
|
_fd = open(_device, O_RDWR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxI2CDriver::end()
|
|
|
|
{
|
|
|
|
if (_fd != -1) {
|
|
|
|
::close(_fd);
|
|
|
|
_fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
tell the I2C library what device we want to talk to
|
|
|
|
*/
|
|
|
|
bool LinuxI2CDriver::set_address(uint8_t addr)
|
|
|
|
{
|
|
|
|
if (_fd == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (_addr != addr) {
|
|
|
|
ioctl(_fd, I2C_SLAVE, addr);
|
|
|
|
_addr = addr;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxI2CDriver::setTimeout(uint16_t ms)
|
|
|
|
{
|
|
|
|
// unimplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxI2CDriver::setHighSpeed(bool active)
|
|
|
|
{
|
|
|
|
// unimplemented
|
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
|
|
|
|
uint8_t LinuxI2CDriver::write(uint8_t addr, uint8_t len, uint8_t* data)
|
2013-09-27 21:00:49 -03:00
|
|
|
{
|
|
|
|
if (!set_address(addr)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (::write(_fd, data, len) != len) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0; // success
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-22 03:01:24 -03:00
|
|
|
uint8_t LinuxI2CDriver::writeRegisters(uint8_t addr, uint8_t reg,
|
2013-09-27 21:00:49 -03:00
|
|
|
uint8_t len, uint8_t* data)
|
|
|
|
{
|
|
|
|
uint8_t buf[len+1];
|
|
|
|
buf[0] = reg;
|
|
|
|
if (len != 0) {
|
|
|
|
memcpy(&buf[1], data, len);
|
|
|
|
}
|
|
|
|
return write(addr, len+1, buf);
|
|
|
|
}
|
|
|
|
|
2013-09-30 23:23:57 -03:00
|
|
|
/*
|
|
|
|
this is a copy of i2c_smbus_access() from i2c-dev.h. We need it for
|
|
|
|
platforms with older headers
|
|
|
|
*/
|
|
|
|
static inline __s32 _i2c_smbus_access(int file, char read_write, __u8 command,
|
|
|
|
int size, union i2c_smbus_data *data)
|
|
|
|
{
|
|
|
|
struct i2c_smbus_ioctl_data args;
|
|
|
|
args.read_write = read_write;
|
|
|
|
args.command = command;
|
|
|
|
args.size = size;
|
|
|
|
args.data = data;
|
|
|
|
return ioctl(file,I2C_SMBUS,&args);
|
|
|
|
}
|
|
|
|
|
2013-09-27 21:00:49 -03:00
|
|
|
uint8_t LinuxI2CDriver::writeRegister(uint8_t addr, uint8_t reg, uint8_t val)
|
|
|
|
{
|
|
|
|
if (!set_address(addr)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2013-09-30 23:23:57 -03:00
|
|
|
union i2c_smbus_data data;
|
|
|
|
data.byte = val;
|
|
|
|
if (_i2c_smbus_access(_fd,I2C_SMBUS_WRITE, reg,
|
|
|
|
I2C_SMBUS_BYTE_DATA, &data) == -1) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2013-09-27 21:00:49 -03:00
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
|
|
|
|
uint8_t LinuxI2CDriver::read(uint8_t addr, uint8_t len, uint8_t* data)
|
2013-09-27 21:00:49 -03:00
|
|
|
{
|
|
|
|
if (!set_address(addr)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (::read(_fd, data, len) != len) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-22 03:01:24 -03:00
|
|
|
uint8_t LinuxI2CDriver::readRegisters(uint8_t addr, uint8_t reg,
|
2013-09-27 21:00:49 -03:00
|
|
|
uint8_t len, uint8_t* data)
|
|
|
|
{
|
2014-07-13 19:44:59 -03:00
|
|
|
if (_fd == -1) {
|
|
|
|
return 1;
|
|
|
|
}
|
2014-05-21 07:09:57 -03:00
|
|
|
struct i2c_msg msgs[] = {
|
|
|
|
{
|
|
|
|
addr : addr,
|
|
|
|
flags : 0,
|
|
|
|
len : 1,
|
2014-07-15 00:50:47 -03:00
|
|
|
buf : (typeof(msgs->buf))®
|
2014-05-21 07:09:57 -03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
addr : addr,
|
|
|
|
flags : I2C_M_RD,
|
|
|
|
len : len,
|
2014-07-15 00:50:47 -03:00
|
|
|
buf : (typeof(msgs->buf))data,
|
2014-05-21 07:09:57 -03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
struct i2c_rdwr_ioctl_data i2c_data = {
|
|
|
|
msgs : msgs,
|
|
|
|
nmsgs : 2
|
|
|
|
};
|
|
|
|
|
2014-07-07 00:36:20 -03:00
|
|
|
// prevent valgrind error
|
|
|
|
memset(data, 0, len);
|
|
|
|
|
2014-05-21 07:09:57 -03:00
|
|
|
if (ioctl(_fd, I2C_RDWR, &i2c_data) == -1) {
|
2013-09-27 21:00:49 -03:00
|
|
|
return 1;
|
|
|
|
}
|
2014-05-21 07:09:57 -03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t LinuxI2CDriver::readRegistersMultiple(uint8_t addr, uint8_t reg,
|
|
|
|
uint8_t len,
|
|
|
|
uint8_t count, uint8_t* data)
|
|
|
|
{
|
2014-07-13 19:44:59 -03:00
|
|
|
if (_fd == -1) {
|
|
|
|
return 1;
|
|
|
|
}
|
2014-05-21 07:09:57 -03:00
|
|
|
while (count > 0) {
|
|
|
|
uint8_t n = count>8?8:count;
|
|
|
|
struct i2c_msg msgs[2*n];
|
|
|
|
struct i2c_rdwr_ioctl_data i2c_data = {
|
|
|
|
msgs : msgs,
|
2014-07-15 00:50:47 -03:00
|
|
|
nmsgs : (typeof(i2c_data.nmsgs))(2*n)
|
2014-05-21 07:09:57 -03:00
|
|
|
};
|
|
|
|
for (uint8_t i=0; i<n; i++) {
|
|
|
|
msgs[i*2].addr = addr;
|
|
|
|
msgs[i*2].flags = 0;
|
|
|
|
msgs[i*2].len = 1;
|
2014-07-15 00:50:47 -03:00
|
|
|
msgs[i*2].buf = (typeof(msgs->buf))®
|
2014-05-21 07:09:57 -03:00
|
|
|
msgs[i*2+1].addr = addr;
|
|
|
|
msgs[i*2+1].flags = I2C_M_RD;
|
|
|
|
msgs[i*2+1].len = len;
|
2014-07-15 00:50:47 -03:00
|
|
|
msgs[i*2+1].buf = (typeof(msgs->buf))data;
|
2014-05-21 07:09:57 -03:00
|
|
|
data += len;
|
|
|
|
};
|
|
|
|
if (ioctl(_fd, I2C_RDWR, &i2c_data) == -1) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
count -= n;
|
2013-09-27 21:00:49 -03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t LinuxI2CDriver::readRegister(uint8_t addr, uint8_t reg, uint8_t* data)
|
|
|
|
{
|
|
|
|
if (!set_address(addr)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2013-09-30 23:23:57 -03:00
|
|
|
union i2c_smbus_data v;
|
2014-07-07 00:36:20 -03:00
|
|
|
memset(&v, 0, sizeof(v));
|
2013-09-30 23:23:57 -03:00
|
|
|
if (_i2c_smbus_access(_fd,I2C_SMBUS_READ, reg,
|
|
|
|
I2C_SMBUS_BYTE_DATA, &v)) {
|
2013-09-27 21:00:49 -03:00
|
|
|
return 1;
|
|
|
|
}
|
2013-09-30 23:23:57 -03:00
|
|
|
*data = v.byte;
|
2013-09-27 21:00:49 -03:00
|
|
|
return 0;
|
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2013-09-27 21:00:49 -03:00
|
|
|
uint8_t LinuxI2CDriver::lockup_count()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2013-09-28 03:28:36 -03:00
|
|
|
#endif // CONFIG_HAL_BOARD
|