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 "Semaphores.h"
21 #include "Scheduler.h"
22 #include "Device.h"
23 
24 #if HAL_USE_SPI == TRUE
25 
26 namespace ChibiOS {
27 
28 class SPIBus : public DeviceBus {
29 public:
30  SPIBus(uint8_t bus);
31  struct spi_dev_s *dev;
32  uint8_t bus;
33  SPIConfig spicfg;
34  void dma_allocate(Shared_DMA *ctx);
35  void dma_deallocate(Shared_DMA *ctx);
37 };
38 
39 struct SPIDesc {
40  SPIDesc(const char *_name, uint8_t _bus,
41  uint8_t _device, ioline_t _pal_line,
42  uint16_t _mode, uint32_t _lowspeed, uint32_t _highspeed)
43  : name(_name), bus(_bus), device(_device),
44  pal_line(_pal_line), mode(_mode),
45  lowspeed(_lowspeed), highspeed(_highspeed)
46  {
47  }
48 
49  const char *name;
50  uint8_t bus;
51  uint8_t device;
52  ioline_t pal_line;
53  uint16_t mode;
54  uint32_t lowspeed;
55  uint32_t highspeed;
56 };
57 
58 
59 class SPIDevice : public AP_HAL::SPIDevice {
60 public:
61  SPIDevice(SPIBus &_bus, SPIDesc &_device_desc);
62 
63  virtual ~SPIDevice();
64 
65  /* See AP_HAL::Device::set_speed() */
66  bool set_speed(AP_HAL::Device::Speed speed) override;
67 
68  // low level transfer function
69  void do_transfer(const uint8_t *send, uint8_t *recv, uint32_t len);
70 
71  /* See AP_HAL::Device::transfer() */
72  bool transfer(const uint8_t *send, uint32_t send_len,
73  uint8_t *recv, uint32_t recv_len) override;
74 
75  /* See AP_HAL::SPIDevice::transfer_fullduplex() */
76  bool transfer_fullduplex(const uint8_t *send, uint8_t *recv,
77  uint32_t len) override;
78 
79  /*
80  * send N bytes of clock pulses without taking CS. This is used
81  * when initialising microSD interfaces over SPI
82  */
83  bool clock_pulse(uint32_t len) override;
84 
85  /* See AP_HAL::Device::get_semaphore() */
86  AP_HAL::Semaphore *get_semaphore() override;
87 
88  /* See AP_HAL::Device::register_periodic_callback() */
90  uint32_t period_usec, AP_HAL::Device::PeriodicCb) override;
91 
92  /* See AP_HAL::Device::adjust_periodic_callback() */
93  bool adjust_periodic_callback(AP_HAL::Device::PeriodicHandle h, uint32_t period_usec) override;
94 
95  bool set_chip_select(bool set) override;
96 
97  bool acquire_bus(bool acquire, bool skip_cs);
98 
99  SPIDriver * get_driver();
100 
101 #ifdef HAL_SPI_CHECK_CLOCK_FREQ
102  // used to measure clock frequencies
103  static void test_clock_freq(void);
104 #endif
105 
106 private:
109  uint32_t frequency;
110  uint16_t freq_flag;
111  uint16_t freq_flag_low;
112  uint16_t freq_flag_high;
113  char *pname;
114  bool cs_forced;
115  static void *spi_thread(void *arg);
116  uint16_t derive_freq_flag(uint32_t _frequency);
117 };
118 
120 public:
121  friend class SPIDevice;
122 
124  {
125  return static_cast<SPIDeviceManager*>(spi_mgr);
126  }
127 
129 
130 private:
131  static SPIDesc device_table[];
133 };
134 }
135 
136 #endif // HAL_USE_SPI
void dma_deallocate(Shared_DMA *ctx)
Definition: SPIDevice.cpp:80
uint16_t mode
Definition: SPIDevice.h:53
uint8_t device
Definition: SPIDevice.h:51
bool spi_started
Definition: SPIDevice.h:36
static SPIDeviceManager * from(AP_HAL::SPIDeviceManager *spi_mgr)
Definition: SPIDevice.h:123
uint8_t bus
Definition: SPIDevice.h:32
SPIBus(uint8_t bus)
Definition: SPIDevice.cpp:57
AP_HAL::OwnPtr< AP_HAL::Device > get_device(const char *name)
Definition: BusTest.cpp:22
const char * name
Definition: BusTest.cpp:11
SPIDesc & device_desc
Definition: SPIDevice.h:108
uint16_t freq_flag
Definition: SPIDevice.h:110
uint32_t frequency
Definition: SPIDevice.h:109
uint16_t freq_flag_low
Definition: SPIDevice.h:111
void dma_allocate(Shared_DMA *ctx)
Definition: SPIDevice.cpp:72
void * PeriodicHandle
Definition: Device.h:42
struct spi_dev_s * dev
Definition: SPIDevice.h:31
const char * name
Definition: SPIDevice.h:49
uint16_t freq_flag_high
Definition: SPIDevice.h:112
ioline_t pal_line
Definition: SPIDevice.h:52
SPIDesc(const char *_name, uint8_t _bus, uint8_t _device, ioline_t _pal_line, uint16_t _mode, uint32_t _lowspeed, uint32_t _highspeed)
Definition: SPIDevice.h:40
SPIConfig spicfg
Definition: SPIDevice.h:33
AP_HAL::Device::PeriodicHandle register_periodic_callback(uint32_t period_usec, AP_HAL::Device::PeriodicCb, AP_HAL::Device *hal_device)
Definition: Device.cpp:84
uint32_t highspeed
Definition: SPIDevice.h:55
uint32_t lowspeed
Definition: SPIDevice.h:54