APM:Libraries
SPIDevice.h
Go to the documentation of this file.
1 /*
2  * This file is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published
4  * by the Free Software Foundation, either version 3 of the License,
5  * or (at your option) any later version.
6  *
7  * This file is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 #pragma once
16 
17 #include <inttypes.h>
18 #include <AP_HAL/HAL.h>
19 #include <AP_HAL/SPIDevice.h>
20 #include <drivers/device/spi.h>
21 #include "Semaphores.h"
22 #include "Device.h"
23 #include "Scheduler.h"
24 
25 namespace VRBRAIN {
26 
27 class SPIDesc;
28 
29 class SPIBus : public DeviceBus {
30 public:
31  SPIBus(void) :
33  struct spi_dev_s *dev;
34  uint8_t bus;
35 };
36 
37 struct SPIDesc {
38  SPIDesc(const char *_name, uint8_t _bus,
39  enum spi_dev_e _device, enum spi_mode_e _mode,
40  uint32_t _lowspeed, uint32_t _highspeed)
41  : name(_name), bus(_bus), device(_device), mode(_mode),
42  lowspeed(_lowspeed), highspeed(_highspeed)
43  {
44  }
45 
46  const char *name;
47  uint8_t bus;
48  enum spi_dev_e device;
49  enum spi_mode_e mode;
50  uint32_t lowspeed;
51  uint32_t highspeed;
52 };
53 
54 
55 class SPIDevice : public AP_HAL::SPIDevice {
56 public:
57  SPIDevice(SPIBus &_bus, SPIDesc &_device_desc);
58 
59  virtual ~SPIDevice();
60 
61  /* See AP_HAL::Device::set_speed() */
62  bool set_speed(AP_HAL::Device::Speed speed) override;
63 
64  // low level transfer function
65  void do_transfer(const uint8_t *send, uint8_t *recv, uint32_t len);
66 
67  /* See AP_HAL::Device::transfer() */
68  bool transfer(const uint8_t *send, uint32_t send_len,
69  uint8_t *recv, uint32_t recv_len) override;
70 
71  /* See AP_HAL::SPIDevice::transfer_fullduplex() */
72  bool transfer_fullduplex(const uint8_t *send, uint8_t *recv,
73  uint32_t len) override;
74 
75  /* See AP_HAL::Device::get_semaphore() */
76  AP_HAL::Semaphore *get_semaphore() override;
77 
78  /* See AP_HAL::Device::register_periodic_callback() */
80  uint32_t period_usec, AP_HAL::Device::PeriodicCb) override;
81 
82  /* See AP_HAL::Device::adjust_periodic_callback() */
83  bool adjust_periodic_callback(AP_HAL::Device::PeriodicHandle h, uint32_t period_usec) override;
84 
85  bool set_chip_select(bool set) override;
86 
87 private:
90  uint32_t frequency;
91  perf_counter_t perf;
92  char *pname;
93  bool cs_forced;
94  static void *spi_thread(void *arg);
95 };
96 
98 public:
99  friend class SPIDevice;
100 
102  {
103  return static_cast<SPIDeviceManager*>(spi_mgr);
104  }
105 
107 
108 private:
109  static SPIDesc device_table[];
111 };
112 
113 }
struct F4Light::SPIDESC SPIDesc
#define APM_SPI_PRIORITY
Definition: Scheduler.h:44
struct spi_dev_s * dev
Definition: SPIDevice.h:33
AP_HAL::OwnPtr< AP_HAL::Device > get_device(const char *name)
Definition: BusTest.cpp:22
SPIDesc & device_desc
Definition: SPIDevice.h:89
const char * name
Definition: BusTest.cpp:11
uint32_t lowspeed
Definition: SPIDevice.h:50
perf_counter_t perf
Definition: SPIDevice.h:91
void * PeriodicHandle
Definition: Device.h:42
AP_HAL::Device::PeriodicHandle register_periodic_callback(uint32_t period_usec, AP_HAL::Device::PeriodicCb, AP_HAL::Device *hal_device)
Definition: Device.cpp:102
uint32_t highspeed
Definition: SPIDevice.h:51
const char * name
Definition: SPIDevice.h:46
uint32_t frequency
Definition: SPIDevice.h:90
static SPIDeviceManager * from(AP_HAL::SPIDeviceManager *spi_mgr)
Definition: SPIDevice.h:101
SPIDesc(const char *_name, uint8_t _bus, enum spi_dev_e _device, enum spi_mode_e _mode, uint32_t _lowspeed, uint32_t _highspeed)
Definition: SPIDevice.h:38
uint8_t bus
Definition: SPIDevice.h:34