-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathspi_utils.cpp
More file actions
59 lines (47 loc) · 1.8 KB
/
spi_utils.cpp
File metadata and controls
59 lines (47 loc) · 1.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
#include "software/embedded/spi_utils.h"
#include <errno.h>
#include <linux/ioctl.h>
#include <linux/spi/spidev.h>
#include "software/logger/logger.h"
void spiTransfer(int fd, uint8_t const* tx, uint8_t const* rx, unsigned len,
uint32_t spi_speed)
{
int ret;
struct spi_ioc_transfer tr[1];
memset(tr, 0, sizeof(tr));
tr[0].tx_buf = (unsigned long)tx;
tr[0].rx_buf = (unsigned long)rx;
tr[0].len = len;
tr[0].delay_usecs = 0;
tr[0].speed_hz = spi_speed;
tr[0].bits_per_word = 8;
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
CHECK(ret >= 1) << "SPI Transfer to motor failed, not safe to proceed: errno "
<< strerror(errno);
}
void readThenWriteSpiTransfer(int fd, const uint8_t* read_tx, const uint8_t* write_tx,
const uint8_t* read_rx, const uint32_t read_len,
const uint32_t write_len, uint32_t spi_speed)
{
uint8_t write_rx[5] = {0};
struct spi_ioc_transfer tr[2];
memset(tr, 0, sizeof(tr));
tr[0].tx_buf = (unsigned long)read_tx;
tr[0].rx_buf = (unsigned long)read_rx;
tr[0].len = read_len;
tr[0].delay_usecs = 0;
tr[0].speed_hz = spi_speed;
tr[0].bits_per_word = 8;
tr[0].cs_change = 0;
tr[1].tx_buf = (unsigned long)write_tx;
tr[1].rx_buf = (unsigned long)write_rx;
tr[1].len = write_len;
tr[1].delay_usecs = 0;
tr[1].speed_hz = spi_speed;
tr[1].bits_per_word = 8;
tr[1].cs_change = 0;
int ret1 = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
int ret2 = ioctl(fd, SPI_IOC_MESSAGE(1), &tr[1]);
CHECK(ret1 >= 1 && ret2 >= 1)
<< "SPI Transfer to motor failed, not safe to proceed: errno " << strerror(errno);
}