PX4 Firmware
PX4 Autopilot Software http://px4.io
SPI.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2019 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 #pragma once
41 
42 #include "../CDev.hpp"
43 
44 #include "dev_fs_lib_spi.h"
45 
46 enum spi_mode_e {
47  SPIDEV_MODE0 = 0, /* CPOL=0 CHPHA=0 */
48  SPIDEV_MODE1 = 1, /* CPOL=0 CHPHA=1 */
49  SPIDEV_MODE2 = 2, /* CPOL=1 CHPHA=0 */
50  SPIDEV_MODE3 = 3 /* CPOL=1 CHPHA=1 */
51 };
52 
53 namespace device __EXPORT
54 {
55 
56 /**
57  * Abstract class for character device on SPI
58  */
59 class __EXPORT SPI : public CDev
60 {
61 protected:
62  /**
63  * Constructor
64  *
65  * @param name Driver name
66  * @param devname Device node name
67  * @param bus SPI bus on which the device lives
68  * @param device Device handle (used by SPI_SELECT)
69  * @param mode SPI clock/data mode
70  * @param frequency SPI clock frequency
71  */
72  SPI(const char *name, const char *devname, int bus, uint32_t device, enum spi_mode_e mode, uint32_t frequency);
73  virtual ~SPI();
74 
75  /**
76  * Locking modes supported by the driver.
77  */
78  enum LockMode {
79  LOCK_PREEMPTION, /**< the default; lock against all forms of preemption. */
80  LOCK_THREADS, /**< lock only against other threads, using SPI_LOCK */
81  LOCK_NONE /**< perform no locking, only safe if the bus is entirely private */
82  };
83 
84  virtual int init();
85 
86  /**
87  * Check for the presence of the device on the bus.
88  */
89  virtual int probe() { return PX4_OK; }
90 
91  /**
92  * Perform a SPI transfer.
93  *
94  * If called from interrupt context, this interface does not lock
95  * the bus and may interfere with non-interrupt-context callers.
96  *
97  * Clients in a mixed interrupt/non-interrupt configuration must
98  * ensure appropriate interlocking.
99  *
100  * At least one of send or recv must be non-null.
101  *
102  * @param send Bytes to send to the device, or nullptr if
103  * no data is to be sent.
104  * @param recv Buffer for receiving bytes from the device,
105  * or nullptr if no bytes are to be received.
106  * @param len Number of bytes to transfer.
107  * @return OK if the exchange was successful, -errno
108  * otherwise.
109  */
110  int transfer(uint8_t *send, uint8_t *recv, unsigned len);
111 
112  /**
113  * Perform a SPI 16 bit transfer.
114  *
115  * If called from interrupt context, this interface does not lock
116  * the bus and may interfere with non-interrupt-context callers.
117  *
118  * Clients in a mixed interrupt/non-interrupt configuration must
119  * ensure appropriate interlocking.
120  *
121  * At least one of send or recv must be non-null.
122  *
123  * @param send Words to send to the device, or nullptr if
124  * no data is to be sent.
125  * @param recv Words for receiving bytes from the device,
126  * or nullptr if no bytes are to be received.
127  * @param len Number of words to transfer.
128  * @return OK if the exchange was successful, -errno
129  * otherwise.
130  */
131  int transferhword(uint16_t *send, uint16_t *recv, unsigned len);
132 
133  /**
134  * Set the SPI bus frequency
135  * This is used to change frequency on the fly. Some sensors
136  * (such as the MPU6000) need a lower frequency for setup
137  * registers and can handle higher frequency for sensor
138  * value registers
139  *
140  * @param frequency Frequency to set (Hz)
141  */
142  void set_frequency(uint32_t frequency) { _frequency = frequency; }
143  uint32_t get_frequency() { return _frequency; }
144 
145  /**
146  * Set the SPI bus locking mode
147  *
148  * This set the SPI locking mode. For devices competing with NuttX SPI
149  * drivers on a bus the right lock mode is LOCK_THREADS.
150  *
151  * @param mode Locking mode
152  */
153  void set_lockmode(enum LockMode mode) {}
154 
155 private:
156  int _fd{-1};
157 
158  uint32_t _device;
159  enum spi_mode_e _mode;
160  uint32_t _frequency;
161 
162  /* this class does not allow copying */
163  SPI(const SPI &);
164  SPI operator=(const SPI &);
165 
166 protected:
167 
168  bool external() { return px4_spi_bus_external(get_device_bus()); }
169 
170 };
171 
172 } // namespace device
void set_frequency(uint32_t frequency)
Set the SPI bus frequency This is used to change frequency on the fly.
Definition: SPI.hpp:142
spi_mode_e
Definition: SPI.hpp:46
void * send(void *data)
Definition: I2C.hpp:51
static Mode _mode
Definition: motor_ramp.cpp:81
Namespace encapsulating all device framework classes, functions and data.
Definition: CDev.cpp:47
uint32_t get_frequency()
Definition: SPI.hpp:143
virtual int probe()
Check for the presence of the device on the bus.
Definition: SPI.hpp:89
void init()
Activates/configures the hardware registers.
static char _device[64]
void set_lockmode(enum LockMode mode)
Set the SPI bus locking mode.
Definition: SPI.hpp:153
const char * name
Definition: tests_main.c:58
mode
Definition: vtol_type.h:76
LockMode
Locking modes supported by the driver.
Definition: SPI.hpp:77
bool external()
Definition: SPI.hpp:168