I2CDevice.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include <inttypes.h>
  19. #include <vector>
  20. #include <AP_HAL/HAL.h>
  21. #include <AP_HAL/I2CDevice.h>
  22. #include <AP_HAL/utility/OwnPtr.h>
  23. #include "Semaphores.h"
  24. namespace Linux {
  25. class I2CBus;
  26. class I2CDevice : public AP_HAL::I2CDevice {
  27. public:
  28. static I2CDevice *from(AP_HAL::I2CDevice *dev)
  29. {
  30. return static_cast<I2CDevice*>(dev);
  31. }
  32. /* AP_HAL::I2CDevice implementation */
  33. I2CDevice(I2CBus &bus, uint8_t address);
  34. ~I2CDevice();
  35. /* See AP_HAL::I2CDevice::set_address() */
  36. void set_address(uint8_t address) override { _address = address; }
  37. /* See AP_HAL::I2CDevice::set_retries() */
  38. void set_retries(uint8_t retries) override { _retries = retries; }
  39. /* AP_HAL::Device implementation */
  40. /* See AP_HAL::Device::set_speed(): Empty implementation, not supported. */
  41. bool set_speed(enum Device::Speed speed) override { return true; }
  42. /* See AP_HAL::Device::transfer() */
  43. bool transfer(const uint8_t *send, uint32_t send_len,
  44. uint8_t *recv, uint32_t recv_len) override;
  45. bool read_registers_multiple(uint8_t first_reg, uint8_t *recv,
  46. uint32_t recv_len, uint8_t times) override;
  47. /* See AP_HAL::Device::get_semaphore() */
  48. AP_HAL::Semaphore *get_semaphore() override;
  49. /* See AP_HAL::Device::register_periodic_callback() */
  50. AP_HAL::Device::PeriodicHandle register_periodic_callback(
  51. uint32_t period_usec, AP_HAL::Device::PeriodicCb) override;
  52. /* See AP_HAL::Device::adjust_periodic_callback() */
  53. bool adjust_periodic_callback(
  54. AP_HAL::Device::PeriodicHandle h, uint32_t period_usec) override;
  55. /* set split transfers flag */
  56. void set_split_transfers(bool set) override {
  57. _split_transfers = set;
  58. }
  59. protected:
  60. I2CBus &_bus;
  61. uint8_t _address;
  62. uint8_t _retries = 0;
  63. bool _split_transfers = false;
  64. };
  65. class I2CDeviceManager : public AP_HAL::I2CDeviceManager {
  66. public:
  67. friend class I2CDevice;
  68. static I2CDeviceManager *from(AP_HAL::I2CDeviceManager *i2c_mgr)
  69. {
  70. return static_cast<I2CDeviceManager*>(i2c_mgr);
  71. }
  72. I2CDeviceManager();
  73. AP_HAL::OwnPtr<AP_HAL::I2CDevice> get_device(
  74. std::vector<const char *> devpaths, uint8_t address) override;
  75. /* AP_HAL::I2CDeviceManager implementation */
  76. AP_HAL::OwnPtr<AP_HAL::I2CDevice> get_device(uint8_t bus, uint8_t address,
  77. uint32_t bus_clock=400000,
  78. bool use_smbus = false,
  79. uint32_t timeout_ms=4) override;
  80. /*
  81. * Stop all I2C threads and block until they are finalized. This doesn't
  82. * free memory because they can still be used by devices, however device
  83. * drivers won't receive any new event
  84. */
  85. void teardown();
  86. /*
  87. get mask of bus numbers for all configured I2C buses
  88. */
  89. uint32_t get_bus_mask(void) const override;
  90. /*
  91. get mask of bus numbers for all configured external I2C buses
  92. */
  93. uint32_t get_bus_mask_external(void) const override;
  94. /*
  95. get mask of bus numbers for all configured internal I2C buses
  96. */
  97. uint32_t get_bus_mask_internal(void) const override;
  98. protected:
  99. void _unregister(I2CBus &b);
  100. AP_HAL::OwnPtr<AP_HAL::I2CDevice> _create_device(I2CBus &b, uint8_t address) const;
  101. std::vector<I2CBus*> _buses;
  102. };
  103. }