1
|
#include <stdint.h>
|
2
|
#include <unistd.h>
|
3
|
#include <stdio.h>
|
4
|
#include <stdlib.h>
|
5
|
#include <getopt.h>
|
6
|
#include <fcntl.h>
|
7
|
#include <sys/ioctl.h>
|
8
|
#include <linux/types.h>
|
9
|
#include <linux/spi/spidev.h>
|
10
|
//#include <iostream>
|
11
|
#include <string.h>
|
12
|
//#include <chrono>
|
13
|
|
14
|
// ... test framework, etc.
|
15
|
|
16
|
const size_t SPI_BUFFER_SIZE = 4;
|
17
|
uint16_t spi_tx_buffer[SPI_BUFFER_SIZE];
|
18
|
uint16_t spi_rx_buffer[SPI_BUFFER_SIZE];
|
19
|
|
20
|
struct spi_ioc_transfer xfer[1];
|
21
|
|
22
|
int spi_init()
|
23
|
{
|
24
|
int file;
|
25
|
uint8_t mode, lsb, bits;
|
26
|
uint32_t speed;
|
27
|
|
28
|
if ((file = open("/dev/spidev32766.0", O_RDWR)) < 0)
|
29
|
{
|
30
|
printf("Failed to open the bus.\n");
|
31
|
return -1;
|
32
|
}
|
33
|
|
34
|
// ... mode reading and writing
|
35
|
|
36
|
printf("file: spi mode %d, %d bits %sper word, %d Hz max\n", mode, bits, lsb ? "(lsb first) " : "", speed);
|
37
|
|
38
|
memset(xfer, 0, sizeof(spi_ioc_transfer));
|
39
|
|
40
|
xfer[0].tx_buf = (unsigned long)spi_tx_buffer;
|
41
|
xfer[0].len = 20; // Length of command to write
|
42
|
xfer[0].cs_change = 1; // Do NOT Keep CS activated
|
43
|
xfer[0].delay_usecs = 0;
|
44
|
xfer[0].speed_hz = 100000;
|
45
|
xfer[0].bits_per_word = 16;
|
46
|
return file;
|
47
|
}
|
48
|
|
49
|
uint16_t* spi_read(int file)
|
50
|
{
|
51
|
int status;
|
52
|
|
53
|
memset(spi_tx_buffer, 0, sizeof(spi_tx_buffer));
|
54
|
memset(spi_rx_buffer, 0, sizeof(spi_rx_buffer));
|
55
|
|
56
|
spi_tx_buffer[0] = 0x8330;
|
57
|
|
58
|
xfer[0].tx_buf = (unsigned long)spi_tx_buffer;
|
59
|
xfer[0].len = 2; // Length of command to write/read
|
60
|
xfer[0].rx_buf = (unsigned long)spi_rx_buffer;
|
61
|
|
62
|
status = ioctl(file, SPI_IOC_MESSAGE(1), xfer);
|
63
|
if (status < 0)
|
64
|
{
|
65
|
perror("SPI_IOC_MESSAGE");
|
66
|
}
|
67
|
|
68
|
printf("rx: ");
|
69
|
for (uint16_t i = 0; i < SPI_BUFFER_SIZE; i++)
|
70
|
{
|
71
|
printf("%04X ", spi_rx_buffer[i]);
|
72
|
}
|
73
|
printf("\n");
|
74
|
|
75
|
return spi_rx_buffer;
|
76
|
}
|
77
|
|
78
|
|
79
|
int main()
|
80
|
{
|
81
|
int spidev_fd = spi_init();
|
82
|
if (spidev_fd < 0)
|
83
|
{
|
84
|
printf("Failed to open!\n");
|
85
|
return -1;
|
86
|
}
|
87
|
|
88
|
spi_read(spidev_fd); //reading the address 0xE60E
|
89
|
|
90
|
close(spidev_fd);
|
91
|
return 0;
|
92
|
}
|