APM:Libraries
SPIDevice.h
Go to the documentation of this file.
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 
19 #include <inttypes.h>
20 #include <vector>
21 
22 #include <AP_HAL/HAL.h>
23 #include <AP_HAL/SPIDevice.h>
24 
25 namespace Linux {
26 
27 class SPIBus;
28 class SPIDesc;
29 
30 class SPIDevice : public AP_HAL::SPIDevice {
31 public:
32  SPIDevice(SPIBus &bus, SPIDesc &device_desc);
33 
34  virtual ~SPIDevice();
35 
36  /* AP_HAL::SPIDevice implementation */
37 
38  /* See AP_HAL::Device::set_speed() */
39  bool set_speed(AP_HAL::Device::Speed speed) override;
40 
41  /* See AP_HAL::Device::transfer() */
42  bool transfer(const uint8_t *send, uint32_t send_len,
43  uint8_t *recv, uint32_t recv_len) override;
44 
45  /* See AP_HAL::SPIDevice::transfer_fullduplex() */
46  bool transfer_fullduplex(const uint8_t *send, uint8_t *recv,
47  uint32_t len) override;
48 
49  /* See AP_HAL::Device::get_semaphore() */
50  AP_HAL::Semaphore *get_semaphore() override;
51 
52  /* See AP_HAL::Device::register_periodic_callback() */
54  uint32_t period_usec, AP_HAL::Device::PeriodicCb) override;
55 
56  /* See AP_HAL::Device::adjust_periodic_callback() */
58  AP_HAL::Device::PeriodicHandle h, uint32_t period_usec) override;
59 
60 protected:
64  uint32_t _speed;
65 
66  /*
67  * Select device if using userspace CS
68  */
69  void _cs_assert();
70 
71  /*
72  * Deselect device if using userspace CS
73  */
74  void _cs_release();
75 };
76 
78 public:
79  friend class SPIDevice;
80 
82  {
83  return static_cast<SPIDeviceManager*>(spi_mgr);
84  }
85 
87  {
88  /* Reserve space up-front for 3 buses */
89  _buses.reserve(3);
90  }
91 
92  /* AP_HAL::SPIDeviceManager implementation */
94 
95  /*
96  * Stop all SPI threads and block until they are finalized. This doesn't
97  * free memory because they can still be used by devices, however device
98  * drivers won't receive any new event
99  */
100  void teardown();
101 
102  /* See AP_HAL::SPIDeviceManager::get_count() */
103  uint8_t get_count();
104 
105  /* See AP_HAL::SPIDeviceManager::get_device_name() */
106  const char *get_device_name(uint8_t idx);
107 
108 protected:
109  void _unregister(SPIBus &b);
110  AP_HAL::OwnPtr<AP_HAL::SPIDevice> _create_device(SPIBus &b, SPIDesc &device_desc) const;
111 
112  std::vector<SPIBus*> _buses;
113 
114  static const uint8_t _n_device_desc;
115  static SPIDesc _device[];
116 };
117 
118 }
static SPIDeviceManager * from(AP_HAL::SPIDeviceManager *spi_mgr)
Definition: SPIDevice.h:81
AP_HAL::Semaphore * get_semaphore() override
Definition: SPIDevice.cpp:399
struct F4Light::SPIDESC SPIDesc
static const uint8_t _n_device_desc
Definition: SPIDevice.h:114
SPIBus & _bus
Definition: SPIDevice.h:61
bool set_speed(AP_HAL::Device::Speed speed) override
Definition: SPIDevice.cpp:249
bool transfer_fullduplex(const uint8_t *send, uint8_t *recv, uint32_t len) override
Definition: SPIDevice.cpp:341
AP_HAL::OwnPtr< AP_HAL::Device > get_device(const char *name)
Definition: BusTest.cpp:22
const char * name
Definition: BusTest.cpp:11
SPIDesc & _desc
Definition: SPIDevice.h:62
bool adjust_periodic_callback(AP_HAL::Device::PeriodicHandle h, uint32_t period_usec) override
Definition: SPIDevice.cpp:424
bool transfer(const uint8_t *send, uint32_t send_len, uint8_t *recv, uint32_t recv_len) override
Definition: SPIDevice.cpp:263
void * PeriodicHandle
Definition: Device.h:42
AP_HAL::Device::PeriodicHandle register_periodic_callback(uint32_t period_usec, AP_HAL::Device::PeriodicCb) override
Definition: SPIDevice.cpp:404
uint32_t _speed
Definition: SPIDevice.h:64
AP_HAL::DigitalSource * _cs
Definition: SPIDevice.h:63
std::vector< SPIBus * > _buses
Definition: SPIDevice.h:112
virtual ~SPIDevice()
Definition: SPIDevice.cpp:243