I2CDevice.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. * Modified for use in AP_HAL_ChibiOS by Andrew Tridgell and Siddharth Bharat Purohit
  18. */
  19. #pragma once
  20. #include <inttypes.h>
  21. #include <AP_HAL/HAL.h>
  22. #include <AP_HAL/I2CDevice.h>
  23. #include <AP_HAL/utility/OwnPtr.h>
  24. #include "AP_HAL_ChibiOS.h"
  25. #if HAL_USE_I2C == TRUE
  26. #include "Semaphores.h"
  27. #include "Device.h"
  28. #include "shared_dma.h"
  29. namespace ChibiOS {
  30. class I2CBus : public DeviceBus {
  31. public:
  32. I2CConfig i2ccfg;
  33. uint8_t busnum;
  34. uint32_t busclock;
  35. // we need an additional lock in the dma_allocate and
  36. // dma_deallocate functions to cope with 3-way contention as we
  37. // have two DMA channels that we are handling with the shared_dma
  38. // code
  39. mutex_t dma_lock;
  40. void dma_allocate(Shared_DMA *);
  41. void dma_deallocate(Shared_DMA *);
  42. void dma_init(void);
  43. static void clear_all(void);
  44. static void clear_bus(uint8_t busidx);
  45. static uint8_t read_sda(uint8_t busidx);
  46. };
  47. class I2CDevice : public AP_HAL::I2CDevice {
  48. public:
  49. static I2CDevice *from(AP_HAL::I2CDevice *dev)
  50. {
  51. return static_cast<I2CDevice*>(dev);
  52. }
  53. I2CDevice(uint8_t bus, uint8_t address, uint32_t bus_clock, bool use_smbus, uint32_t timeout_ms);
  54. ~I2CDevice();
  55. /* See AP_HAL::I2CDevice::set_address() */
  56. void set_address(uint8_t address) override { _address = address; }
  57. /* See AP_HAL::I2CDevice::set_retries() */
  58. void set_retries(uint8_t retries) override { _retries = retries; }
  59. /* See AP_HAL::Device::set_speed(): Empty implementation, not supported. */
  60. bool set_speed(enum Device::Speed speed) override { return true; }
  61. /* See AP_HAL::Device::transfer() */
  62. bool transfer(const uint8_t *send, uint32_t send_len,
  63. uint8_t *recv, uint32_t recv_len) override;
  64. bool read_registers_multiple(uint8_t first_reg, uint8_t *recv,
  65. uint32_t recv_len, uint8_t times) override;
  66. /* See AP_HAL::Device::register_periodic_callback() */
  67. AP_HAL::Device::PeriodicHandle register_periodic_callback(
  68. uint32_t period_usec, AP_HAL::Device::PeriodicCb) override;
  69. /* See AP_HAL::Device::adjust_periodic_callback() */
  70. bool adjust_periodic_callback(AP_HAL::Device::PeriodicHandle h, uint32_t period_usec) override;
  71. AP_HAL::Semaphore* get_semaphore() override {
  72. // if asking for invalid bus number use bus 0 semaphore
  73. return &bus.semaphore;
  74. }
  75. void set_split_transfers(bool set) override {
  76. _split_transfers = set;
  77. }
  78. private:
  79. I2CBus &bus;
  80. bool _transfer(const uint8_t *send, uint32_t send_len,
  81. uint8_t *recv, uint32_t recv_len);
  82. /* I2C interface #2 */
  83. uint8_t _retries;
  84. uint8_t _address;
  85. char *pname;
  86. bool _split_transfers;
  87. bool _use_smbus;
  88. uint32_t _timeout_ms;
  89. };
  90. class I2CDeviceManager : public AP_HAL::I2CDeviceManager {
  91. public:
  92. friend class I2CDevice;
  93. static I2CBus businfo[];
  94. // constructor
  95. I2CDeviceManager();
  96. static I2CDeviceManager *from(AP_HAL::I2CDeviceManager *i2c_mgr)
  97. {
  98. return static_cast<I2CDeviceManager*>(i2c_mgr);
  99. }
  100. AP_HAL::OwnPtr<AP_HAL::I2CDevice> get_device(uint8_t bus, uint8_t address,
  101. uint32_t bus_clock=400000,
  102. bool use_smbus = false,
  103. uint32_t timeout_ms=4) override;
  104. /*
  105. get mask of bus numbers for all configured I2C buses
  106. */
  107. uint32_t get_bus_mask(void) const override;
  108. /*
  109. get mask of bus numbers for all configured external I2C buses
  110. */
  111. uint32_t get_bus_mask_external(void) const override;
  112. /*
  113. get mask of bus numbers for all configured internal I2C buses
  114. */
  115. uint32_t get_bus_mask_internal(void) const override;
  116. };
  117. }
  118. #endif // HAL_USE_I2C