-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathspi_linux.go
More file actions
123 lines (104 loc) · 3.31 KB
/
spi_linux.go
File metadata and controls
123 lines (104 loc) · 3.31 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
package spidev
/*
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#define SPI_SPEED 4000000
uint8_t mode=0;
uint8_t bits=8;
uint32_t speed=SPI_SPEED;
uint16_t delay=5;
int spi_open(const char *device) {
int fd = open(device, O_RDWR);
int ret;
if (fd < 0) {
printf("can't open device");
return -1;
}
ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
if (ret == -1) {
printf("can't set spi mode");
return -1;
}
ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
if (ret == -1) {
printf("can't get spi mode");
return -1;
}
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -1) {
printf("can't set bits per word");
return -1;
}
ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
if (ret == -1) {
printf("can't get bits per word");
return -1;
}
ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
if (ret == -1) {
printf("can't set max speed hz");
return -1;
}
ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
if (ret == -1) {
printf("can't get max speed hz");
return -1;
}
return fd;
}
int spi_xfer(int fd, char* tx, char* rx, int length) {
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx,
.rx_buf = (unsigned long)rx,
.len = length,
.delay_usecs = delay,
.speed_hz = speed,
.bits_per_word = bits,
};
int ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1)
return -1;
return 0;
}
*/
import "C"
import "unsafe"
import "errors"
// SPIDevice device
type SPIDevice struct {
fd C.int
}
// NewSPIDevice opens the device
func NewSPIDevice(devPath string) (*SPIDevice, error) {
name := C.CString(devPath)
defer C.free(unsafe.Pointer(name))
i := C.spi_open(name)
if i < 0 {
return nil, errors.New("could not open")
}
return &SPIDevice{i}, nil
}
// Xfer cross transfer
func (d *SPIDevice) Xfer(tx []byte) ([]byte, error) {
length := len(tx)
rx := make([]byte, length)
//log.Print("sending", tx)
ret := C.spi_xfer(d.fd, (*C.char)(unsafe.Pointer(&tx[0])), (*C.char)(unsafe.Pointer(&rx[0])), C.int(length))
//log.Print("got", rx)
if ret < 0 {
return nil, errors.New("could not xfer")
}
return rx, nil
}
// Close closes the fd
func (d *SPIDevice) Close() {
C.close(d.fd)
}