PX4 Firmware
PX4 Autopilot Software http://px4.io
SPI.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2012-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.cpp
36  *
37  * Base class for devices connected via SPI.
38  *
39  * @todo Work out if caching the mode/frequency would save any time.
40  *
41  * @todo A separate bus/device abstraction would allow for mixed interrupt-mode
42  * and non-interrupt-mode clients to arbitrate for the bus. As things stand,
43  * a bus shared between clients of both kinds is vulnerable to races between
44  * the two, where an interrupt-mode client will ignore the lock held by the
45  * non-interrupt-mode client.
46  */
47 
48 #include "SPI.hpp"
49 
50 #include <px4_platform_common/px4_config.h>
51 #include <nuttx/arch.h>
52 
53 #ifndef CONFIG_SPI_EXCHANGE
54 # error This driver requires CONFIG_SPI_EXCHANGE
55 #endif
56 
57 namespace device
58 {
59 
60 SPI::SPI(const char *name, const char *devname, int bus, uint32_t device, enum spi_mode_e mode, uint32_t frequency) :
61  CDev(name, devname),
62  _device(device),
63  _mode(mode),
64  _frequency(frequency),
65  _dev(nullptr)
66 {
67  // fill in _device_id fields for a SPI device
68  _device_id.devid_s.bus_type = DeviceBusType_SPI;
69  _device_id.devid_s.bus = bus;
70  _device_id.devid_s.address = (uint8_t)device;
71  // devtype needs to be filled in by the driver
72  _device_id.devid_s.devtype = 0;
73 }
74 
76 {
77  // XXX no way to let go of the bus...
78 }
79 
80 int
82 {
83  /* attach to the spi bus */
84  if (_dev == nullptr) {
85  int bus = get_device_bus();
86 
87  if (!board_has_bus(BOARD_SPI_BUS, bus)) {
88  return -ENOENT;
89  }
90 
91  _dev = px4_spibus_initialize(bus);
92  }
93 
94  if (_dev == nullptr) {
95  DEVICE_DEBUG("failed to init SPI");
96  return -ENOENT;
97  }
98 
99  /* deselect device to ensure high to low transition of pin select */
100  SPI_SELECT(_dev, _device, false);
101 
102  /* call the probe function to check whether the device is present */
103  int ret = probe();
104 
105  if (ret != OK) {
106  DEVICE_DEBUG("probe failed");
107  return ret;
108  }
109 
110  /* do base class init, which will create the device node, etc. */
111  ret = CDev::init();
112 
113  if (ret != OK) {
114  DEVICE_DEBUG("cdev init failed");
115  return ret;
116  }
117 
118  /* tell the workd where we are */
119  DEVICE_LOG("on SPI bus %d at %d (%u KHz)", get_device_bus(), PX4_SPI_DEV_ID(_device), _frequency / 1000);
120 
121  return PX4_OK;
122 }
123 
124 int
125 SPI::transfer(uint8_t *send, uint8_t *recv, unsigned len)
126 {
127  int result;
128 
129  if ((send == nullptr) && (recv == nullptr)) {
130  return -EINVAL;
131  }
132 
133  LockMode mode = up_interrupt_context() ? LOCK_NONE : _locking_mode;
134 
135  /* lock the bus as required */
136  switch (mode) {
137  default:
138  case LOCK_PREEMPTION: {
139  irqstate_t state = px4_enter_critical_section();
140  result = _transfer(send, recv, len);
141  px4_leave_critical_section(state);
142  }
143  break;
144 
145  case LOCK_THREADS:
146  SPI_LOCK(_dev, true);
147  result = _transfer(send, recv, len);
148  SPI_LOCK(_dev, false);
149  break;
150 
151  case LOCK_NONE:
152  result = _transfer(send, recv, len);
153  break;
154  }
155 
156  return result;
157 }
158 
159 int
160 SPI::_transfer(uint8_t *send, uint8_t *recv, unsigned len)
161 {
162  SPI_SETFREQUENCY(_dev, _frequency);
163  SPI_SETMODE(_dev, _mode);
164  SPI_SETBITS(_dev, 8);
165  SPI_SELECT(_dev, _device, true);
166 
167  /* do the transfer */
168  SPI_EXCHANGE(_dev, send, recv, len);
169 
170  /* and clean up */
171  SPI_SELECT(_dev, _device, false);
172 
173  return PX4_OK;
174 }
175 
176 int
177 SPI::transferhword(uint16_t *send, uint16_t *recv, unsigned len)
178 {
179  int result;
180 
181  if ((send == nullptr) && (recv == nullptr)) {
182  return -EINVAL;
183  }
184 
185  LockMode mode = up_interrupt_context() ? LOCK_NONE : _locking_mode;
186 
187  /* lock the bus as required */
188  switch (mode) {
189  default:
190  case LOCK_PREEMPTION: {
191  irqstate_t state = px4_enter_critical_section();
192  result = _transferhword(send, recv, len);
193  px4_leave_critical_section(state);
194  }
195  break;
196 
197  case LOCK_THREADS:
198  SPI_LOCK(_dev, true);
199  result = _transferhword(send, recv, len);
200  SPI_LOCK(_dev, false);
201  break;
202 
203  case LOCK_NONE:
204  result = _transferhword(send, recv, len);
205  break;
206  }
207 
208  return result;
209 }
210 
211 int
212 SPI::_transferhword(uint16_t *send, uint16_t *recv, unsigned len)
213 {
214  SPI_SETFREQUENCY(_dev, _frequency);
215  SPI_SETMODE(_dev, _mode);
216  SPI_SETBITS(_dev, 16); /* 16 bit transfer */
217  SPI_SELECT(_dev, _device, true);
218 
219  /* do the transfer */
220  SPI_EXCHANGE(_dev, send, recv, len);
221 
222  /* and clean up */
223  SPI_SELECT(_dev, _device, false);
224 
225  return PX4_OK;
226 }
227 
228 } // namespace device
int transfer(uint8_t *send, uint8_t *recv, unsigned len)
Perform a SPI transfer.
struct spi_dev_s * _dev
Definition: SPI.hpp:158
spi_mode_e
Definition: SPI.hpp:46
static enum @74 state
void * send(void *data)
static Mode _mode
Definition: motor_ramp.cpp:81
perform no locking, only safe if the bus is entirely private
Definition: SPI.hpp:80
enum spi_mode_e _mode
Definition: SPI.hpp:156
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
virtual int init() override
uint32_t _device
Definition: SPI.hpp:155
virtual int probe()
Check for the presence of the device on the bus.
Definition: SPI.hpp:88
void init()
Activates/configures the hardware registers.
virtual ~SPI()
#define DEVICE_LOG(FMT,...)
Definition: Device.hpp:51
int transferhword(uint16_t *send, uint16_t *recv, unsigned len)
Perform a SPI 16 bit transfer.
LockMode _locking_mode
selected locking mode
Definition: SPI.hpp:160
static char _device[64]
uint32_t _frequency
Definition: SPI.hpp:157
int _transfer(uint8_t *send, uint8_t *recv, unsigned len)
Definition: SPI.cpp:160
const char * name
Definition: tests_main.c:58
int _transferhword(uint16_t *send, uint16_t *recv, unsigned len)
Definition: SPI.cpp:212
lock only against other threads, using SPI_LOCK
Definition: SPI.hpp:79
#define OK
Definition: uavcan_main.cpp:71
mode
Definition: vtol_type.h:76
LockMode
Locking modes supported by the driver.
Definition: SPI.hpp:77
#define DEVICE_DEBUG(FMT,...)
Definition: Device.hpp:52