Line data Source code
1 : /* 2 : * Copyright (C) 2015-2016 Intel Corporation. All rights reserved. 3 : * 4 : * This file is free software: you can redistribute it and/or modify it 5 : * under the terms of the GNU General Public License as published by the 6 : * Free Software Foundation, either version 3 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * This file is distributed in the hope that it will be useful, but 10 : * WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 : * See the GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License along 15 : * with this program. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : #pragma once 18 : 19 : #include <inttypes.h> 20 : #include <vector> 21 : 22 : #include <AP_HAL/HAL.h> 23 : #include <AP_HAL/I2CDevice.h> 24 : 25 : #include "Semaphores.h" 26 : 27 : namespace Linux { 28 : 29 : class I2CBus; 30 : 31 : class I2CDevice : public AP_HAL::I2CDevice { 32 : public: 33 : static I2CDevice *from(AP_HAL::I2CDevice *dev) 34 : { 35 : return static_cast<I2CDevice*>(dev); 36 : } 37 : 38 : /* AP_HAL::I2CDevice implementation */ 39 : 40 : I2CDevice(I2CBus &bus, uint8_t address); 41 : 42 : ~I2CDevice(); 43 : 44 : /* See AP_HAL::I2CDevice::set_address() */ 45 0 : void set_address(uint8_t address) override { _address = address; } 46 : 47 : /* See AP_HAL::I2CDevice::set_retries() */ 48 0 : void set_retries(uint8_t retries) override { _retries = retries; } 49 : 50 : /* AP_HAL::Device implementation */ 51 : 52 : /* See AP_HAL::Device::set_speed(): Empty implementation, not supported. */ 53 0 : bool set_speed(enum Device::Speed speed) override { return true; } 54 : 55 : /* See AP_HAL::Device::transfer() */ 56 : bool transfer(const uint8_t *send, uint32_t send_len, 57 : uint8_t *recv, uint32_t recv_len) override; 58 : 59 : bool read_registers_multiple(uint8_t first_reg, uint8_t *recv, 60 : uint32_t recv_len, uint8_t times) override; 61 : 62 : /* See AP_HAL::Device::get_semaphore() */ 63 : AP_HAL::Semaphore *get_semaphore() override; 64 : 65 : /* See AP_HAL::Device::register_periodic_callback() */ 66 : AP_HAL::Device::PeriodicHandle register_periodic_callback( 67 : uint32_t period_usec, AP_HAL::Device::PeriodicCb) override; 68 : 69 : /* See AP_HAL::Device::adjust_periodic_callback() */ 70 : bool adjust_periodic_callback( 71 : AP_HAL::Device::PeriodicHandle h, uint32_t period_usec) override; 72 : 73 : /* set split transfers flag */ 74 0 : void set_split_transfers(bool set) override { 75 0 : _split_transfers = set; 76 0 : } 77 : 78 : protected: 79 : I2CBus &_bus; 80 : uint8_t _address; 81 : uint8_t _retries = 0; 82 : bool _split_transfers = false; 83 : }; 84 : 85 : class I2CDeviceManager : public AP_HAL::I2CDeviceManager { 86 : public: 87 : friend class I2CDevice; 88 : 89 0 : static I2CDeviceManager *from(AP_HAL::I2CDeviceManager *i2c_mgr) 90 : { 91 0 : return static_cast<I2CDeviceManager*>(i2c_mgr); 92 : } 93 : 94 : I2CDeviceManager(); 95 : 96 : /* AP_HAL::I2CDeviceManager implementation */ 97 : AP_HAL::I2CDevice *get_device_ptr(uint8_t bus, uint8_t address, 98 : uint32_t bus_clock=400000, 99 : bool use_smbus = false, 100 : uint32_t timeout_ms=4) override; 101 : 102 : /* 103 : * Stop all I2C threads and block until they are finalized. This doesn't 104 : * free memory because they can still be used by devices, however device 105 : * drivers won't receive any new event 106 : */ 107 : void teardown(); 108 : 109 : /* 110 : get mask of bus numbers for all configured I2C buses 111 : */ 112 : uint32_t get_bus_mask(void) const override; 113 : 114 : /* 115 : get mask of bus numbers for all configured external I2C buses 116 : */ 117 : uint32_t get_bus_mask_external(void) const override; 118 : 119 : /* 120 : get mask of bus numbers for all configured internal I2C buses 121 : */ 122 : uint32_t get_bus_mask_internal(void) const override; 123 : 124 : protected: 125 : void _unregister(I2CBus &b); 126 : AP_HAL::I2CDevice *_create_device(I2CBus &b, uint8_t address) const; 127 : 128 : std::vector<I2CBus*> _buses; 129 : }; 130 : 131 : }