forked from PX4/DriverFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPIDevObj.cpp
More file actions
468 lines (367 loc) · 12.8 KB
/
SPIDevObj.cpp
File metadata and controls
468 lines (367 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/****************************************************************************
*
* Copyright (C) 2016 Julian Oes. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#include "SPIDevObj.hpp"
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include "OSConfig.h"
#include "DevIOCTL.h"
#if defined(__DF_RPI) || defined(__DF_EDISON) || defined(__DF_BEBOP) || defined(__DF_OCPOC)
#include <sys/ioctl.h>
#include <linux/types.h>
#include <alloca.h>
#include <linux/spi/spidev.h>
#include <cstdlib>
#include <cstring>
#elif defined(__DF_QURT)
#include "dev_fs_lib_spi.h"
#endif
using namespace DriverFramework;
#define DIR_READ 0x80
#define DIR_WRITE 0x00
SPIDevObj::~SPIDevObj() = default;
int SPIDevObj::start()
{
m_fd = ::open(m_dev_path, 0);
if (m_fd < 0) {
DF_LOG_ERR("SPIDevObj start failed");
return m_fd;
}
return 0;
}
int SPIDevObj::stop()
{
if (m_fd >= 0) {
int ret = ::close(m_fd);
m_fd = -1;
if (ret < 0) {
DF_LOG_ERR("Error: SPIDevObj::stop failed on ::close()");
return ret;
}
}
return 0;
}
int SPIDevObj::readReg(DevHandle &h, uint8_t address, uint8_t &val)
{
SPIDevObj *obj = DevMgr::getDevObjByHandle<SPIDevObj>(h);
if (obj) {
return obj->_readReg(address, val);
}
return -1;
}
int SPIDevObj::_readReg(uint8_t address, uint8_t &val)
{
#if defined(__DF_RPI) || defined(__DF_EDISON) || defined(__DF_BEBOP) || defined(__DF_OCPOC)
/* implement sensor interface via rpi2 spi */
// constexpr int transfer_bytes = 1 + 1; // first byte is address
uint8_t write_buffer[2] = {0}; // automatic write buffer
uint8_t read_buffer[2] = {0}; // automatic read buffer
write_buffer[0] = address | DIR_READ; // read mode
write_buffer[1] = 0; // write data
struct spi_ioc_transfer spi_transfer; // datastructures for linux spi interface
memset(&spi_transfer, 0, sizeof(spi_ioc_transfer));
spi_transfer.tx_buf = (unsigned long)write_buffer;
spi_transfer.rx_buf = (unsigned long)read_buffer;
spi_transfer.len = 2;
// spi_transfer.speed_hz = SPI_FREQUENCY_1MHZ; // temporarily override spi speed
spi_transfer.bits_per_word = 8;
spi_transfer.delay_usecs = 0;
int result = 0;
result = ::ioctl(m_fd, SPI_IOC_MESSAGE(1), &spi_transfer);
if (result != 2) {
DF_LOG_ERR("error: SPI combined read write failed: %d", result);
return -1;
}
val = read_buffer[1];
return 0;
#elif defined(__DF_QURT)
/* Save the address of the register to read from in the write buffer for the combined write. */
struct dspal_spi_ioctl_read_write ioctl_write_read;
uint8_t write_buffer[2];
uint8_t read_buffer[2];
write_buffer[0] = address | DIR_READ;
write_buffer[1] = 0;
ioctl_write_read.write_buffer = write_buffer;
ioctl_write_read.write_buffer_length = 2;
ioctl_write_read.read_buffer = read_buffer;
ioctl_write_read.read_buffer_length = 2;
int result = ::ioctl(m_fd, SPI_IOCTL_RDWR, &ioctl_write_read);
if (result < 0) {
DF_LOG_ERR("error: SPI combined read write failed: %d", result);
return -1;
}
val = read_buffer[1];
return 0;
#else
return -1;
#endif
}
int SPIDevObj::writeReg(DevHandle &h, uint8_t address, uint8_t val)
{
SPIDevObj *obj = DevMgr::getDevObjByHandle<SPIDevObj>(h);
if (obj) {
return obj->_writeReg(address, val);
}
return -1;
}
int SPIDevObj::writeRegVerified(DevHandle &h, uint8_t address, uint8_t val)
{
SPIDevObj *obj = DevMgr::getDevObjByHandle<SPIDevObj>(h);
if (obj) {
int result;
uint8_t read_val = ~val;
int retries = 5;
while (retries) {
result = obj->_writeReg(address, val);
if (result < 0) {
--retries;
continue;
}
result = obj->_readReg(address, read_val);
if (result < 0 || read_val != val) {
--retries;
continue;
}
}
if (val == read_val) {
return 0;
} else {
DF_LOG_ERR("error: SPI write verify failed: %d", errno);
}
}
return -1;
}
int SPIDevObj::_writeReg(uint8_t address, uint8_t val)
{
return _writeReg(address, &val, 1);
}
int SPIDevObj::_writeReg(uint8_t address, uint8_t *in_buffer, uint16_t length)
{
#if defined(__DF_RPI) || defined(__DF_EDISON) || defined(__DF_BEBOP) || defined(__DF_OCPOC)
/* implement sensor interface via rpi2 spi */
uint8_t write_buffer[length + 1];// automatic write buffer: first byte is address
memset(&write_buffer, 0, length + 1);
uint8_t read_buffer[2] = {0}; // automatic read buffer
struct spi_ioc_transfer spi_transfer; // datastructures for linux spi interface
memset(&spi_transfer, 0, sizeof(spi_ioc_transfer));
write_buffer[0] = address | DIR_WRITE; // write mode
if (in_buffer) {
memcpy(&write_buffer[1], in_buffer, length);
}
spi_transfer.rx_buf = (unsigned long)read_buffer;
spi_transfer.len = length + 1;
spi_transfer.tx_buf = (unsigned long)write_buffer;
// spi_transfer.speed_hz = SPI_FREQUENCY_1MHZ; // temporarily override spi speed
spi_transfer.bits_per_word = 8;
spi_transfer.delay_usecs = 0;
int result = 0;
result = ::ioctl(m_fd, SPI_IOC_MESSAGE(1), &spi_transfer);
if (result != length + 1) {
DF_LOG_ERR("Error: SPI write failed. Reported %d bytes written (%s)", result, strerror(errno));
return -1;
}
return 0;
#else
uint8_t write_buffer[length + 1];
write_buffer[0] = address | DIR_WRITE;
if (in_buffer) {
memcpy(&write_buffer[1], in_buffer, length);
}
/* Save the address of the register to read from in the write buffer for the combined write. */
int bytes_written = ::write(m_fd, (char *) write_buffer, 2);
if (bytes_written != 2) {
DF_LOG_ERR("Error: SPI write failed. Reported %d bytes written (%d)", bytes_written, errno);
return -1;
}
return 0;
#endif
}
int SPIDevObj::_modifyReg(uint8_t address, uint8_t clearbits, uint8_t setbits)
{
int ret;
uint8_t val;
ret = _readReg(address, val);
if (ret != 0) {
return ret;
}
val &= ~clearbits;
val |= setbits;
return _writeReg(address, val);
}
int SPIDevObj::_transfer(uint8_t *write_buffer, uint8_t *read_buffer, uint8_t len)
{
#if defined(__DF_OCPOC)
struct spi_ioc_transfer spi_transfer; // datastructures for linux spi interface
memset(&spi_transfer, 0, sizeof(spi_ioc_transfer));
write_buffer[0] |= DIR_WRITE; // write mode
spi_transfer.rx_buf = (unsigned long)read_buffer;
spi_transfer.len = len;
spi_transfer.tx_buf = (unsigned long)write_buffer;
// spi_transfer.speed_hz = SPI_FREQUENCY_1MHZ; // temporarily override spi speed
spi_transfer.bits_per_word = 8;
spi_transfer.delay_usecs = 0;
int result = 0;
result = ::ioctl(m_fd, SPI_IOC_MESSAGE(1), &spi_transfer);
if (result != len) {
DF_LOG_ERR("Error: SPI write failed. Reported %d bytes written (%d)", result, errno);
return -1;
}
return 0;
#else
write_buffer[0] |= DIR_WRITE;
/* Save the address of the register to read from in the write buffer for the combined write. */
int bytes_written = ::write(m_fd, (char *) write_buffer, len);
if (bytes_written != len) {
DF_LOG_ERR("Error: SPI write failed. Reported %d bytes written (%d)", bytes_written, errno);
return -1;
}
return 0;
#endif
}
int SPIDevObj::bulkRead(DevHandle &h, uint8_t address, uint8_t *out_buffer, int length)
{
#if defined(__DF_RPI) || defined(__DF_EDISON) || defined(__DF_BEBOP) || defined(__DF_OCPOC)
/* implement sensor interface via rpi2 spi */
SPIDevObj *obj = DevMgr::getDevObjByHandle<SPIDevObj>(h);
if (obj) {
return obj->_bulkRead(address, out_buffer, length);
}
return -1;
#elif defined(__DF_QURT)
int result = 0;
int transfer_bytes = 1 + length; // first byte is address
struct dspal_spi_ioctl_read_write ioctl_write_read;
uint8_t write_buffer[transfer_bytes];
uint8_t read_buffer[transfer_bytes];
write_buffer[0] = address | DIR_READ;
ioctl_write_read.read_buffer = out_buffer;
ioctl_write_read.read_buffer_length = transfer_bytes;
ioctl_write_read.write_buffer = write_buffer;
ioctl_write_read.write_buffer_length = transfer_bytes;
result = h.ioctl(SPI_IOCTL_RDWR, (unsigned long)&ioctl_write_read);
if (result != transfer_bytes) {
DF_LOG_ERR("bulkRead error %d (%d)", result, h.getError());
return result;
}
memcpy(out_buffer, &read_buffer[1], transfer_bytes - 1);
return 0;
#else
return -1;
#endif
}
int SPIDevObj::_bulkRead(uint8_t address, uint8_t *out_buffer, int length)
{
#if defined(__DF_RPI) || defined(__DF_EDISON) || defined(__DF_BEBOP) || defined(__DF_OCPOC)
DF_LOG_DEBUG("_bulkRead: length = %d", length);
/* implement sensor interface via rpi spi */
int transfer_bytes = 1 + length; // first byte is address
// automatic write buffer
uint8_t *write_buffer = (uint8_t *)alloca(transfer_bytes);
memset(write_buffer, 0, transfer_bytes);
// automatic read buffer
uint8_t *read_buffer = (uint8_t *)alloca(transfer_bytes);
memset(read_buffer, 0, transfer_bytes);
write_buffer[0] = address | DIR_READ; // read mode
struct spi_ioc_transfer spi_transfer; // datastructure for linux spi interface
memset(&spi_transfer, 0, sizeof(spi_ioc_transfer));
spi_transfer.rx_buf = (unsigned long)read_buffer;
spi_transfer.len = transfer_bytes;
spi_transfer.tx_buf = (unsigned long)write_buffer;
// spi_transfer.speed_hz = SPI_FREQUENCY_1MHZ;
spi_transfer.bits_per_word = 8;
spi_transfer.delay_usecs = 0;
int result = 0;
result = ::ioctl(m_fd, SPI_IOC_MESSAGE(1), &spi_transfer);
if (result != transfer_bytes) {
DF_LOG_ERR("bulkRead error %d", result);
return result;
}
DF_LOG_DEBUG("_bulkRead: read_buffer = %u, %u, %u, %u, %u, %u, %u, %u, %u",
read_buffer[1], read_buffer[2], read_buffer[3],
read_buffer[4], read_buffer[5], read_buffer[6],
read_buffer[7], read_buffer[8], read_buffer[9]);
memcpy(out_buffer, &read_buffer[1], transfer_bytes - 1);
return 0;
#elif defined(__DF_QURT)
int result = 0;
int transfer_bytes = 1 + length; // first byte is address
// XXX NOTE: The write and read buffer need to be of the same length.
// If the write buffer is chosen with only 1 byte length,
// we see the ioctl randomly getting stuck.
struct dspal_spi_ioctl_read_write ioctl_write_read;
uint8_t write_buffer[transfer_bytes];
uint8_t read_buffer[transfer_bytes];
::memset(write_buffer, 0, sizeof(write_buffer));
write_buffer[0] = address | DIR_READ;
ioctl_write_read.read_buffer = read_buffer;
ioctl_write_read.read_buffer_length = transfer_bytes;
ioctl_write_read.write_buffer = write_buffer;
ioctl_write_read.write_buffer_length = transfer_bytes;
result = ::ioctl(m_fd, SPI_IOCTL_RDWR, &ioctl_write_read);
if (result != transfer_bytes) {
DF_LOG_ERR("bulkRead error %d", result);
return result;
}
memcpy(out_buffer, &read_buffer[1], transfer_bytes - 1);
return 0;
#else
return -1;
#endif
}
int SPIDevObj::setLoopbackMode(DevHandle &h, bool enable)
{
#if defined(__DF_RPI) || defined(__DF_EDISON) || defined(__DF_BEBOP) || defined(__DF_OCPOC)
/* implement sensor interface via rpi2 spi */
DF_LOG_ERR("ERROR: attempt to set loopback mode in software fails.");
return -1;
#elif defined(__DF_QURT)
struct dspal_spi_ioctl_loopback loopback;
loopback.state = enable ? SPI_LOOPBACK_STATE_ENABLED : SPI_LOOPBACK_STATE_DISABLED;
return h.ioctl(SPI_IOCTL_LOOPBACK_TEST, (unsigned long)&loopback);
#else
return -1;
#endif
}
int SPIDevObj::_setBusFrequency(uint32_t freq_hz)
{
#if defined(__DF_RPI) || defined(__DF_BEBOP) || defined(__DF_OCPOC) || defined(__DF_EDISON)
return ::ioctl(m_fd, SPI_IOC_WR_MAX_SPEED_HZ, &freq_hz);
#elif defined(__DF_QURT)
struct dspal_spi_ioctl_set_bus_frequency bus_freq;
bus_freq.bus_frequency_in_hz = freq_hz;
return ::ioctl(m_fd, SPI_IOCTL_SET_BUS_FREQUENCY_IN_HZ, &bus_freq);
#else
return -1;
#endif
}