spi.c
3.87 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
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
/* memory management */
#include <sys/mman.h>
#include "spi.h"
#include <string.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#undef DEBUG
//#define DEBUG
static uint8_t bits = 8;
//static uint32_t speed = 15000000; // 9600;
//static uint32_t speed = 20000000; // 9600;
static uint32_t speed = 10000000;
//static uint32_t speed = 100000; // 9600;
//static uint16_t delay;
static void
pabort (const char *s)
{
perror (s);
abort ();
}
uint8_t
spi_put_multiple (int fd, uint8_t * tx, uint16_t tx_size,
uint8_t * rx, uint16_t rx_size)
{
int ret;
int nb = 2;
if (rx_size == 0 || tx_size == 0)
nb = 1;
struct spi_ioc_transfer xfer[nb];
nb = 0;
memset (xfer, 0, sizeof (xfer));
if (tx_size > 0)
{
xfer[nb].tx_buf = (unsigned long) tx;
xfer[nb].len = tx_size;
nb++;
}
if (rx_size > 0)
{
xfer[nb].rx_buf = (unsigned long) rx;
xfer[nb].len = rx_size;
nb++;
}
ret = ioctl (fd, SPI_IOC_MESSAGE (nb), xfer);
return ret;
}
uint8_t
tr3 (int fd, uint8_t * val, uint8_t * rx, uint8_t size)
{
int ret;
struct spi_ioc_transfer xfer[2];
memset (xfer, 0, sizeof (xfer));
xfer[0].tx_buf = (unsigned long) val;
xfer[0].len = size;
xfer[1].rx_buf = (unsigned long) rx;
xfer[1].len = size;
ret = ioctl (fd, SPI_IOC_MESSAGE (2), xfer);
if (ret < 1)
{
pabort ("can't send spi message");
}
return ret;
}
void
setMode (int fd, uint8_t mode)
{
if (ioctl (fd, SPI_IOC_WR_MODE, &mode) < 0)
pabort ("can't set mode");
if (ioctl (fd, SPI_IOC_RD_MODE, &mode) < 0)
pabort ("can't get mode");
}
uint8_t
spi_put (int fd, uint8_t valeur)
{
int ret;
uint8_t rx_buf[1], tx_buf[1];
tx_buf[0] = valeur;
struct spi_ioc_transfer xfer[2];
memset (xfer, 0, sizeof (xfer));
xfer[0].tx_buf = (unsigned long) tx_buf;
xfer[0].rx_buf = (unsigned long) rx_buf;
xfer[0].len = 1;
ret = ioctl (fd, SPI_IOC_MESSAGE (1), xfer);
if (ret < 1)
{
pabort ("can't send spi message");
}
return rx_buf[0];
}
int
configureSpi (char *device)
{
printf("device(configureSpi)=%s\n",device);
int fd = open (device, O_RDWR);
if (fd < 0)
pabort ("can't open device");
setMode (fd, 0);
/* bits per word */
if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1)
pabort ("can't set bits per word");
if (ioctl (fd, SPI_IOC_RD_BITS_PER_WORD, &bits) == -1)
pabort ("can't get bits per word");
/* max speed hz */
if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1)
pabort ("can't set max speed hz");
if (ioctl (fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) == -1)
pabort ("can't get max speed hz");
//#ifdef DEBUG
//printf("spi mode: %d\n", mode);
//printf ("bits per word: %d\r\n", bits);
//printf ("max speed: %lu Hz (%lu kHz)\r\n",
// (unsigned long) speed, ((unsigned long) speed) / 1000);
//#endif /* DEBUG */
return fd;
}
int configureSpiFromFd (int fd)
{
setMode (fd, 0);
/* bits per word */
if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1)
pabort ("can't set bits per word");
if (ioctl (fd, SPI_IOC_RD_BITS_PER_WORD, &bits) == -1)
pabort ("can't get bits per word");
/* max speed hz */
if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1)
pabort ("can't set max speed hz");
if (ioctl (fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) == -1)
pabort ("can't get max speed hz");
#ifdef DEBUG
//printf("spi mode: %d\n", mode);
printf ("bits per word: %d\r\n", bits);
printf ("max speed: %lu Hz (%lu kHz)\r\n",
(unsigned long) speed, ((unsigned long) speed) / 1000);
#endif /* DEBUG */
return EXIT_SUCCESS;
}