PX4 Firmware
PX4 Autopilot Software http://px4.io
SPI.cpp
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.cpp
36  *
37  * Base class for devices connected via SPI.
38  *
39  */
40 
41 #include "SPI.hpp"
42 
43 #ifdef __PX4_LINUX
44 
45 #include <fcntl.h>
46 #include <unistd.h>
47 #include <sys/ioctl.h>
48 #include <linux/types.h>
49 #include <linux/spi/spidev.h>
50 
51 #include <px4_platform_common/px4_config.h>
52 
53 namespace device
54 {
55 
56 SPI::SPI(const char *name, const char *devname, int bus, uint32_t device, enum spi_mode_e mode, uint32_t frequency) :
57  CDev(name, devname),
58  _device(device),
59  _mode(mode),
60  _frequency(frequency)
61 {
62  DEVICE_DEBUG("SPI::SPI name = %s devname = %s", name, devname);
63 
64  // fill in _device_id fields for a SPI device
65  _device_id.devid_s.bus_type = DeviceBusType_SPI;
66  _device_id.devid_s.bus = bus;
67  _device_id.devid_s.address = (uint8_t)device;
68  // devtype needs to be filled in by the driver
69  _device_id.devid_s.devtype = 0;
70 }
71 
72 SPI::~SPI()
73 {
74  if (_fd >= 0) {
75  ::close(_fd);
76  _fd = -1;
77  }
78 }
79 
80 int
81 SPI::init()
82 {
83  // Open the actual SPI device
84  char dev_path[16];
85  snprintf(dev_path, sizeof(dev_path), "/dev/spidev%i.%i", get_device_bus(), PX4_SPI_DEV_ID(_device));
86  DEVICE_DEBUG("%s", dev_path);
87  _fd = ::open(dev_path, O_RDWR);
88 
89  if (_fd < 0) {
90  PX4_ERR("could not open %s", dev_path);
91  return PX4_ERROR;
92  }
93 
94  /* call the probe function to check whether the device is present */
95  int ret = probe();
96 
97  if (ret != OK) {
98  DEVICE_DEBUG("probe failed");
99  return ret;
100  }
101 
102  /* do base class init, which will create the device node, etc. */
103  ret = CDev::init();
104 
105  if (ret != OK) {
106  DEVICE_DEBUG("cdev init failed");
107  return ret;
108  }
109 
110  /* tell the workd where we are */
111  DEVICE_LOG("on SPI bus %d at %d (%u KHz)", get_device_bus(), PX4_SPI_DEV_ID(_device), _frequency / 1000);
112 
113  return PX4_OK;
114 }
115 
116 int
117 SPI::transfer(uint8_t *send, uint8_t *recv, unsigned len)
118 {
119  if ((send == nullptr) && (recv == nullptr)) {
120  return -EINVAL;
121  }
122 
123  // set write mode of SPI
124  int result = ::ioctl(_fd, SPI_IOC_WR_MODE, &_mode);
125 
126  if (result == -1) {
127  PX4_ERR("can’t set spi mode");
128  return PX4_ERROR;
129  }
130 
131  spi_ioc_transfer spi_transfer[1] {}; // datastructures for linux spi interface
132 
133  spi_transfer[0].tx_buf = (uint64_t)send;
134  spi_transfer[0].rx_buf = (uint64_t)recv;
135  spi_transfer[0].len = len;
136  spi_transfer[0].speed_hz = _frequency;
137  spi_transfer[0].bits_per_word = 8;
138  //spi_transfer[0].delay_usecs = 10;
139  spi_transfer[0].cs_change = true;
140 
141  result = ::ioctl(_fd, SPI_IOC_MESSAGE(1), &spi_transfer);
142 
143  if (result != (int)len) {
144  PX4_ERR("write failed. Reported %d bytes written (%s)", result, strerror(errno));
145  return -1;
146  }
147 
148  return 0;
149 }
150 
151 int
152 SPI::transferhword(uint16_t *send, uint16_t *recv, unsigned len)
153 {
154  if ((send == nullptr) && (recv == nullptr)) {
155  return -EINVAL;
156  }
157 
158  // set write mode of SPI
159  int result = ::ioctl(_fd, SPI_IOC_WR_MODE, &_mode);
160 
161  if (result == -1) {
162  PX4_ERR("can’t set spi mode");
163  return PX4_ERROR;
164  }
165 
166  int bits = 16;
167  result = ::ioctl(_fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
168 
169  if (result == -1) {
170  PX4_ERR("can’t set 16 bit spi mode");
171  return PX4_ERROR;
172  }
173 
174  spi_ioc_transfer spi_transfer[1] {}; // datastructures for linux spi interface
175 
176  spi_transfer[0].tx_buf = (uint64_t)send;
177  spi_transfer[0].rx_buf = (uint64_t)recv;
178  spi_transfer[0].len = len * 2;
179  spi_transfer[0].speed_hz = _frequency;
180  //spi_transfer[0].bits_per_word = 8;
181  //spi_transfer[0].delay_usecs = 10;
182  spi_transfer[0].cs_change = true;
183 
184  result = ::ioctl(_fd, SPI_IOC_MESSAGE(1), &spi_transfer);
185 
186  if (result != (int)(len * 2)) {
187  PX4_ERR("write failed. Reported %d bytes written (%s)", result, strerror(errno));
188  return -1;
189  }
190 
191  return 0;
192 }
193 
194 } // namespace device
195 
196 #endif // __PX4_LINUX
int transfer(uint8_t *send, uint8_t *recv, unsigned len)
Perform a SPI transfer.
spi_mode_e
Definition: SPI.hpp:46
void * send(void *data)
static Mode _mode
Definition: motor_ramp.cpp:81
enum spi_mode_e _mode
Definition: SPI.hpp:156
Namespace encapsulating all device framework classes, functions and data.
Definition: CDev.cpp:47
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.
static char _device[64]
uint32_t _frequency
Definition: SPI.hpp:157
const char * name
Definition: tests_main.c:58
#define OK
Definition: uavcan_main.cpp:71
mode
Definition: vtol_type.h:76
#define DEVICE_DEBUG(FMT,...)
Definition: Device.hpp:52