-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspi_test.c
More file actions
81 lines (70 loc) · 1.95 KB
/
spi_test.c
File metadata and controls
81 lines (70 loc) · 1.95 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
// Usually /dev/spidev0.0 for CE0 and /dev/spidev0.1 for CE1
#define SPI_DEVICE "/dev/spidev0.0"
#define SPI_SPEED_HZ 1000000 // 1 MHz
int main(int argc, char **argv) {
int fd;
uint8_t buffer[] = {0x01, 0x02, 0x03, 0x04};
uint8_t read_buffer[sizeof(buffer)];
// Open SPI device
fd = open(SPI_DEVICE, O_RDWR);
if (fd < 0) {
perror("Can't open SPI device");
return 1;
}
// Configure SPI mode
uint8_t mode = SPI_MODE_0;
if (ioctl(fd, SPI_IOC_WR_MODE, &mode) < 0) {
perror("Can't set SPI mode");
close(fd);
return 1;
}
// Configure bits per word
uint8_t bits = 8;
if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) < 0) {
perror("Can't set bits per word");
close(fd);
return 1;
}
// Configure max speed
uint32_t speed = SPI_SPEED_HZ;
if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) {
perror("Can't set max speed");
close(fd);
return 1;
}
// Prepare the transfer
struct spi_ioc_transfer transfer = {
.tx_buf = (unsigned long)buffer,
.rx_buf = (unsigned long)read_buffer,
.len = sizeof(buffer),
.speed_hz = SPI_SPEED_HZ,
.bits_per_word = bits,
.delay_usecs = 0,
};
// Perform the transfer
if (ioctl(fd, SPI_IOC_MESSAGE(1), &transfer) < 0) {
perror("SPI transfer failed");
close(fd);
return 1;
}
// Print what we sent and received
printf("Sent: ");
for (size_t i = 0; i < sizeof(buffer); i++) {
printf("0x%02X ", buffer[i]);
}
printf("\n");
printf("Received: ");
for (size_t i = 0; i < sizeof(buffer); i++) {
printf("0x%02X ", read_buffer[i]);
}
printf("\n");
close(fd);
return 0;
}