PX4 Firmware
PX4 Autopilot Software http://px4.io
SPI.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2012 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file SPI.hpp
36  *
37  * Base class for devices connected via SPI.
38  */
39 
40 #ifndef _DEVICE_SPI_H
41 #define _DEVICE_SPI_H
42 
43 #include "../CDev.hpp"
44 
45 namespace device __EXPORT
46 {
47 
48 /**
49  * Abstract class for character device on SPI
50  */
51 class __EXPORT SPI : public CDev
52 {
53 public:
54  // no copy, assignment, move, move assignment
55  SPI(const SPI &) = delete;
56  SPI &operator=(const SPI &) = delete;
57  SPI(SPI &&) = delete;
58  SPI &operator=(SPI &&) = delete;
59 
60 protected:
61  /**
62  * Constructor
63  *
64  * @param name Driver name
65  * @param devname Device node name
66  * @param bus SPI bus on which the device lives
67  * @param device Device handle (used by SPI_SELECT)
68  * @param mode SPI clock/data mode
69  * @param frequency SPI clock frequency
70  */
71  SPI(const char *name, const char *devname, int bus, uint32_t device, enum spi_mode_e mode, uint32_t frequency);
72  virtual ~SPI();
73 
74  /**
75  * Locking modes supported by the driver.
76  */
77  enum LockMode {
78  LOCK_PREEMPTION, /**< the default; lock against all forms of preemption. */
79  LOCK_THREADS, /**< lock only against other threads, using SPI_LOCK */
80  LOCK_NONE /**< perform no locking, only safe if the bus is entirely private */
81  };
82 
83  virtual int init() override;
84 
85  /**
86  * Check for the presence of the device on the bus.
87  */
88  virtual int probe() { return PX4_OK; }
89 
90  /**
91  * Perform a SPI transfer.
92  *
93  * If called from interrupt context, this interface does not lock
94  * the bus and may interfere with non-interrupt-context callers.
95  *
96  * Clients in a mixed interrupt/non-interrupt configuration must
97  * ensure appropriate interlocking.
98  *
99  * At least one of send or recv must be non-null.
100  *
101  * @param send Bytes to send to the device, or nullptr if
102  * no data is to be sent.
103  * @param recv Buffer for receiving bytes from the device,
104  * or nullptr if no bytes are to be received.
105  * @param len Number of bytes to transfer.
106  * @return OK if the exchange was successful, -errno
107  * otherwise.
108  */
109  int transfer(uint8_t *send, uint8_t *recv, unsigned len);
110 
111  /**
112  * Perform a SPI 16 bit transfer.
113  *
114  * If called from interrupt context, this interface does not lock
115  * the bus and may interfere with non-interrupt-context callers.
116  *
117  * Clients in a mixed interrupt/non-interrupt configuration must
118  * ensure appropriate interlocking.
119  *
120  * At least one of send or recv must be non-null.
121  *
122  * @param send Words to send to the device, or nullptr if
123  * no data is to be sent.
124  * @param recv Words for receiving bytes from the device,
125  * or nullptr if no bytes are to be received.
126  * @param len Number of words to transfer.
127  * @return OK if the exchange was successful, -errno
128  * otherwise.
129  */
130  int transferhword(uint16_t *send, uint16_t *recv, unsigned len);
131 
132  /**
133  * Set the SPI bus frequency
134  * This is used to change frequency on the fly. Some sensors
135  * (such as the MPU6000) need a lower frequency for setup
136  * registers and can handle higher frequency for sensor
137  * value registers
138  *
139  * @param frequency Frequency to set (Hz)
140  */
141  void set_frequency(uint32_t frequency) { _frequency = frequency; }
142  uint32_t get_frequency() { return _frequency; }
143 
144  /**
145  * Set the SPI bus locking mode
146  *
147  * This set the SPI locking mode. For devices competing with NuttX SPI
148  * drivers on a bus the right lock mode is LOCK_THREADS.
149  *
150  * @param mode Locking mode
151  */
152  void set_lockmode(enum LockMode mode) { _locking_mode = mode; }
153 
154 private:
155  uint32_t _device;
157  uint32_t _frequency;
158  struct spi_dev_s *_dev;
159 
160  LockMode _locking_mode{LOCK_THREADS}; /**< selected locking mode */
161 
162 protected:
163  int _transfer(uint8_t *send, uint8_t *recv, unsigned len);
164 
165  int _transferhword(uint16_t *send, uint16_t *recv, unsigned len);
166 
167  virtual bool external() const override { return px4_spi_bus_external(get_device_bus()); }
168 
169 };
170 
171 } // namespace device
172 
173 #endif /* _DEVICE_SPI_H */
void set_frequency(uint32_t frequency)
Set the SPI bus frequency This is used to change frequency on the fly.
Definition: SPI.hpp:141
struct spi_dev_s * _dev
Definition: SPI.hpp:158
spi_mode_e
Definition: SPI.hpp:46
void * send(void *data)
Definition: I2C.hpp:51
static Mode _mode
Definition: motor_ramp.cpp:81
Abstract class for character device on SPI.
Definition: SPI.hpp:51
Namespace encapsulating all device framework classes, functions and data.
Definition: CDev.cpp:47
the default; lock against all forms of preemption.
Definition: SPI.hpp:78
uint32_t _device
Definition: SPI.hpp:155
uint32_t get_frequency()
Definition: SPI.hpp:142
virtual int probe()
Check for the presence of the device on the bus.
Definition: SPI.hpp:88
void init()
Activates/configures the hardware registers.
void set_lockmode(enum LockMode mode)
Set the SPI bus locking mode.
Definition: SPI.hpp:152
uint32_t _frequency
Definition: SPI.hpp:157
const char * name
Definition: tests_main.c:58
virtual bool external() const override
Definition: SPI.hpp:167
lock only against other threads, using SPI_LOCK
Definition: SPI.hpp:79
mode
Definition: vtol_type.h:76
LockMode
Locking modes supported by the driver.
Definition: SPI.hpp:77