SPIDevice.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (C) 2015 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/SPIDevice.h>
  22. namespace Linux {
  23. class SPIBus;
  24. class SPIDesc;
  25. class SPIDevice : public AP_HAL::SPIDevice {
  26. public:
  27. SPIDevice(SPIBus &bus, SPIDesc &device_desc);
  28. virtual ~SPIDevice();
  29. /* AP_HAL::SPIDevice implementation */
  30. /* See AP_HAL::Device::set_speed() */
  31. bool set_speed(AP_HAL::Device::Speed speed) override;
  32. /* See AP_HAL::Device::transfer() */
  33. bool transfer(const uint8_t *send, uint32_t send_len,
  34. uint8_t *recv, uint32_t recv_len) override;
  35. /* See AP_HAL::SPIDevice::transfer_fullduplex() */
  36. bool transfer_fullduplex(const uint8_t *send, uint8_t *recv,
  37. uint32_t len) override;
  38. /* See AP_HAL::Device::get_semaphore() */
  39. AP_HAL::Semaphore *get_semaphore() override;
  40. /* See AP_HAL::Device::register_periodic_callback() */
  41. AP_HAL::Device::PeriodicHandle register_periodic_callback(
  42. uint32_t period_usec, AP_HAL::Device::PeriodicCb) override;
  43. /* See AP_HAL::Device::adjust_periodic_callback() */
  44. bool adjust_periodic_callback(
  45. AP_HAL::Device::PeriodicHandle h, uint32_t period_usec) override;
  46. protected:
  47. SPIBus &_bus;
  48. SPIDesc &_desc;
  49. AP_HAL::DigitalSource *_cs;
  50. uint32_t _speed;
  51. /*
  52. * Select device if using userspace CS
  53. */
  54. void _cs_assert();
  55. /*
  56. * Deselect device if using userspace CS
  57. */
  58. void _cs_release();
  59. };
  60. class SPIDeviceManager : public AP_HAL::SPIDeviceManager {
  61. public:
  62. friend class SPIDevice;
  63. static SPIDeviceManager *from(AP_HAL::SPIDeviceManager *spi_mgr)
  64. {
  65. return static_cast<SPIDeviceManager*>(spi_mgr);
  66. }
  67. SPIDeviceManager()
  68. {
  69. /* Reserve space up-front for 3 buses */
  70. _buses.reserve(3);
  71. }
  72. /* AP_HAL::SPIDeviceManager implementation */
  73. AP_HAL::OwnPtr<AP_HAL::SPIDevice> get_device(const char *name) override;
  74. /*
  75. * Stop all SPI threads and block until they are finalized. This doesn't
  76. * free memory because they can still be used by devices, however device
  77. * drivers won't receive any new event
  78. */
  79. void teardown();
  80. /* See AP_HAL::SPIDeviceManager::get_count() */
  81. uint8_t get_count() override;
  82. /* See AP_HAL::SPIDeviceManager::get_device_name() */
  83. const char *get_device_name(uint8_t idx) override;
  84. protected:
  85. void _unregister(SPIBus &b);
  86. AP_HAL::OwnPtr<AP_HAL::SPIDevice> _create_device(SPIBus &b, SPIDesc &device_desc) const;
  87. std::vector<SPIBus*> _buses;
  88. static const uint8_t _n_device_desc;
  89. static SPIDesc _device[];
  90. };
  91. }