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 #include <fcntl.h>
44 #include <unistd.h>
45 #include <sys/ioctl.h>
46 #include "dev_fs_lib_spi.h"
47 
48 #include <px4_platform_common/px4_config.h>
49 
50 namespace device
51 {
52 
53 SPI::SPI(const char *name, const char *devname, int bus, uint32_t device, enum spi_mode_e mode, uint32_t frequency) :
54  CDev(name, devname),
55  _device(device),
56  _mode(mode),
57  _frequency(frequency)
58 {
59  DEVICE_DEBUG("SPI::SPI name = %s devname = %s", name, devname);
60 
61  // fill in _device_id fields for a SPI device
62  _device_id.devid_s.bus_type = DeviceBusType_SPI;
63  _device_id.devid_s.bus = bus;
64  _device_id.devid_s.address = (uint8_t)device;
65  // devtype needs to be filled in by the driver
66  _device_id.devid_s.devtype = 0;
67 }
68 
69 SPI::~SPI()
70 {
71  if (_fd >= 0) {
72  ::close(_fd);
73  _fd = -1;
74  }
75 }
76 
77 int
78 SPI::init()
79 {
80  // Open the actual SPI device
81  char dev_path[16];
82  snprintf(dev_path, sizeof(dev_path), "dev/spi-%lu", PX4_SPI_DEV_ID(_device));
83  DEVICE_DEBUG("%s", dev_path);
84  _fd = ::open(dev_path, O_RDWR);
85 
86  if (_fd < 0) {
87  PX4_ERR("could not open %s", dev_path);
88  return PX4_ERROR;
89  }
90 
91  /* call the probe function to check whether the device is present */
92  int ret = probe();
93 
94  if (ret != OK) {
95  DEVICE_DEBUG("probe failed");
96  return ret;
97  }
98 
99  /* do base class init, which will create the device node, etc. */
100  ret = CDev::init();
101 
102  if (ret != OK) {
103  DEVICE_DEBUG("cdev init failed");
104  return ret;
105  }
106 
107  /* tell the workd where we are */
108  DEVICE_LOG("on SPI bus %d at %d (%u KHz)", get_device_bus(), PX4_SPI_DEV_ID(_device), _frequency / 1000);
109 
110  return PX4_OK;
111 }
112 
113 int
114 SPI::transfer(uint8_t *send, uint8_t *recv, unsigned len)
115 {
116  if ((send == nullptr) && (recv == nullptr)) {
117  return -EINVAL;
118  }
119 
120  dspal_spi_ioctl_read_write ioctl_write_read{};
121  ioctl_write_read.read_buffer = send;
122  ioctl_write_read.read_buffer_length = len;
123  ioctl_write_read.write_buffer = recv;
124  ioctl_write_read.write_buffer_length = len;
125 
126  int result = ::ioctl(_fd, SPI_IOCTL_RDWR, &ioctl_write_read);
127 
128  if (result < 0) {
129  PX4_ERR("transfer error %d", result);
130  return result;
131  }
132 
133  return result;
134 }
135 
136 int
137 SPI::transferhword(uint16_t *send, uint16_t *recv, unsigned len)
138 {
139  if ((send == nullptr) && (recv == nullptr)) {
140  return -EINVAL;
141  }
142 
143  // int bits = 16;
144  // result = ::ioctl(_fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
145 
146  // if (result == -1) {
147  // PX4_ERR("can’t set 16 bit spi mode");
148  // return PX4_ERROR;
149  // }
150 
151  dspal_spi_ioctl_read_write ioctl_write_read{};
152  ioctl_write_read.read_buffer = send;
153  ioctl_write_read.read_buffer_length = len * 2;
154  ioctl_write_read.write_buffer = recv;
155  ioctl_write_read.write_buffer_length = len * 2;
156 
157  int result = ::ioctl(_fd, SPI_IOCTL_RDWR, &ioctl_write_read);
158 
159  if (result < 0) {
160  PX4_ERR("transfer error %d", result);
161  return result;
162  }
163 
164  return result;
165 }
166 
167 } // namespace device
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
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