From cd0e1dff825adc848fa0f985d3188568376c53de Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Sun, 22 Nov 2015 21:57:03 -0200 Subject: [PATCH] AP_HAL_Linux: Add skeleton for I2CDevice This adds the logic to maintain the I2CDevice's managed by I2CManager. --- .../AP_HAL_Linux/AP_HAL_Linux_Namespace.h | 2 + libraries/AP_HAL_Linux/AP_HAL_Linux_Private.h | 1 + libraries/AP_HAL_Linux/I2CDevice.cpp | 165 ++++++++++++++++++ libraries/AP_HAL_Linux/I2CDevice.h | 116 ++++++++++++ 4 files changed, 284 insertions(+) create mode 100644 libraries/AP_HAL_Linux/I2CDevice.cpp create mode 100644 libraries/AP_HAL_Linux/I2CDevice.h diff --git a/libraries/AP_HAL_Linux/AP_HAL_Linux_Namespace.h b/libraries/AP_HAL_Linux/AP_HAL_Linux_Namespace.h index b6863556bd..7c99b706e9 100644 --- a/libraries/AP_HAL_Linux/AP_HAL_Linux_Namespace.h +++ b/libraries/AP_HAL_Linux/AP_HAL_Linux_Namespace.h @@ -6,6 +6,8 @@ namespace Linux { class UARTDriver; class SPIUARTDriver; class RPIOUARTDriver; + class I2CDevice; + class I2CDeviceManager; class I2CDriver; class SPIDeviceManager; class SPIDeviceDriver; diff --git a/libraries/AP_HAL_Linux/AP_HAL_Linux_Private.h b/libraries/AP_HAL_Linux/AP_HAL_Linux_Private.h index d989f3f04d..b046841ee7 100644 --- a/libraries/AP_HAL_Linux/AP_HAL_Linux_Private.h +++ b/libraries/AP_HAL_Linux/AP_HAL_Linux_Private.h @@ -9,6 +9,7 @@ #include "UARTDriver.h" #include "SPIUARTDriver.h" #include "RPIOUARTDriver.h" +#include "I2CDevice.h" #include "I2CDriver.h" #include "SPIDriver.h" #include "AnalogIn.h" diff --git a/libraries/AP_HAL_Linux/I2CDevice.cpp b/libraries/AP_HAL_Linux/I2CDevice.cpp new file mode 100644 index 0000000000..09380c071b --- /dev/null +++ b/libraries/AP_HAL_Linux/I2CDevice.cpp @@ -0,0 +1,165 @@ +/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- +/* + * Copyright (C) 2015-2016 Intel Corporation. All rights reserved. + * + * 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 . + */ +#include "I2CDevice.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "Util.h" + +namespace Linux { + +static const AP_HAL::HAL &hal = AP_HAL::get_HAL(); + +/* Private struct to maintain for each bus */ +class I2CBus { +public: + ~I2CBus() + { + if (fd >= 0) { + ::close(fd); + } + } + + int open(uint8_t n) + { + char path[sizeof("/dev/i2c-XXX")]; + int r; + + if (fd >= 0) { + return -EBUSY; + } + + // TODO: open the bus + bus = n; + + return fd; + } + + Semaphore sem; + int fd = -1; + uint8_t bus; + + uint8_t ref; +}; + +I2CDevice::~I2CDevice() +{ + // Unregister itself from the I2CDeviceManager + I2CDeviceManager::from(hal.i2c_mgr)->_unregister(_bus); +} + +bool I2CDevice::transfer(const uint8_t *send, uint32_t send_len, + uint8_t *recv, uint32_t recv_len) +{ + // TODO: implement I2C transfer + return false; +} + +AP_HAL::Semaphore *I2CDevice::get_semaphore() +{ + return &_bus.sem; +} + +int I2CDevice::get_fd() +{ + return _bus.fd; +} + +I2CDeviceManager::I2CDeviceManager() +{ + /* Reserve space up-front for 4 buses */ + _buses.reserve(4); +} + +AP_HAL::OwnPtr +I2CDeviceManager::get_device(std::vector devpaths, uint8_t address) +{ + // implement device search on sysfs + return nullptr; +} + +AP_HAL::OwnPtr +I2CDeviceManager::get_device(uint8_t bus, uint8_t address) +{ + for (uint8_t i = 0, n = _buses.size(); i < n; i++) { + if (_buses[i]->bus == bus) { + return _create_device(*_buses[i], address); + } + } + + /* Bus not found for this device, create a new one */ + AP_HAL::OwnPtr b{new I2CBus()}; + if (!b) { + return nullptr; + } + + if (b->open(bus) < 0) { + return nullptr; + } + + auto dev = _create_device(*b, address); + if (!dev) { + return nullptr; + } + + _buses.push_back(b.leak()); + + return dev; +} + +/* Create a new device increasing the bus reference */ +AP_HAL::OwnPtr +I2CDeviceManager::_create_device(I2CBus &b, uint8_t address) const +{ + auto dev = AP_HAL::OwnPtr(new I2CDevice(b, address)); + if (!dev) { + return nullptr; + } + b.ref++; + return dev; +} + +void I2CDeviceManager::_unregister(I2CBus &b) +{ + assert(b.ref > 0); + + if (--b.ref > 0) { + return; + } + + for (auto it = _buses.begin(); it != _buses.end(); it++) { + if ((*it)->bus == b.bus) { + _buses.erase(it); + delete &b; + break; + } + } +} + +} diff --git a/libraries/AP_HAL_Linux/I2CDevice.h b/libraries/AP_HAL_Linux/I2CDevice.h new file mode 100644 index 0000000000..359bf35f3e --- /dev/null +++ b/libraries/AP_HAL_Linux/I2CDevice.h @@ -0,0 +1,116 @@ +/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- +/* + * Copyright (C) 2015-2016 Intel Corporation. All rights reserved. + * + * 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 . + */ +#pragma once + +#include +#include + +#include +#include +#include + +#include "Semaphores.h" + +namespace Linux { + +class I2CBus; + +class I2CDevice : public AP_HAL::I2CDevice { +public: + static I2CDevice *from(AP_HAL::I2CDevice *dev) + { + return static_cast(dev); + } + + /* AP_HAL::I2CDevice implementation */ + + I2CDevice(I2CBus &bus, uint8_t address) + : _bus(bus) + , _address(address) + { + } + + ~I2CDevice(); + + /* See AP_HAL::I2CDevice::set_address() */ + void set_address(uint8_t address) override { _address = address; } + + /* See AP_HAL::I2CDevice::set_retries() */ + void set_retries(uint8_t retries) override { _retries = retries; } + + /* AP_HAL::Device implementation */ + + /* See AP_HAL::Device::set_speed(): Empty implementation, not supported. */ + bool set_speed(enum Device::Speed speed) override { return true; } + + /* See AP_HAL::Device::transfer() */ + bool transfer(const uint8_t *send, uint32_t send_len, + uint8_t *recv, uint32_t recv_len) override; + + /* See AP_HAL::Device::get_semaphore() */ + AP_HAL::Semaphore *get_semaphore() override; + + /* See AP_HAL::Device::register_periodic_callback() */ + AP_HAL::Device::PeriodicHandle *register_periodic_callback( + uint32_t period_usec, AP_HAL::MemberProc) override + { + /* Not implemented yet */ + return nullptr; + }; + + /* See AP_HAL::Device::get_fd() */ + int get_fd() override; + +protected: + I2CBus &_bus; + uint8_t _address; + uint8_t _retries = 0; +}; + +class I2CDeviceManager : public AP_HAL::I2CDeviceManager { +public: + friend class I2CDevice; + + static I2CDeviceManager *from(AP_HAL::I2CDeviceManager *i2c_mgr) + { + return static_cast(i2c_mgr); + } + + I2CDeviceManager(); + + /* + * Get device by looking up the I2C bus on the buses from @devpaths. + * + * Each string in @devpaths are possible locations for the bus as + * returned by 'udevadm info -q path /dev/i2c-X'. The first I2C bus + * matching a prefix in @devpaths is returned. + */ + AP_HAL::OwnPtr get_device( + std::vector devpaths, uint8_t address); + + /* AP_HAL::I2CDeviceManager implementation */ + AP_HAL::OwnPtr get_device(uint8_t bus, uint8_t address) override; + +protected: + void _unregister(I2CBus &b); + AP_HAL::OwnPtr _create_device(I2CBus &b, uint8_t address) const; + + std::vector _buses; +}; + +}