forked from gicking/stm8gal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spi_spidev_comm.c
287 lines (191 loc) · 6.09 KB
/
spi_spidev_comm.c
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
\file spi_spidev_comm.c
\author G. Icking-Konert
\date 2017-12-10
\version 0.1
\brief implementation of SPI comm routines via spidev library
implementation of routines for SPI communication using the spidev library.
*/
// include files
#include "spi_spidev_comm.h"
#include "main.h"
#include "misc.h"
/**
\fn HANDLE init_spi_spidev(const char *port, uint32_t baudrate)
\param[in] port name of port as string
\param[in] baudrate SPI port speed in Baud
\return handle to SPI port
open SPI port for communication, set properties (baudrate, polarity,...).
*/
HANDLE init_spi_spidev(const char *port, uint32_t baudrate) {
/////////
// Windows
/////////
#if defined(WIN32) || defined(WIN64)
#error Windows not yet supported (which API?)
#endif // WIN32 || WIN64
/////////
// Posix
/////////
#if defined(__APPLE__) || defined(__unix__)
HANDLE fp;
int ret;
uint32_t mode = 0;
uint8_t bits = 8;
// latch in on falling edge. Default is latch on rising edge (CPHA=0)
//mode |= SPI_CPHA;
// clock idle high. Default is idle clock low (CPOL=0)
//mode |= SPI_CPOL;
// LSB first. Default is MSB first
//mode |= SPI_LSB_FIRST;
// chip-select high active. Default is low active
//mode |= SPI_CS_HIGH;
// 3-wire / half-duplex SPI. Default is 4-wire / full-duplex SPI
//mode |= SPI_3WIRE;
// chip-select control in software. Default is hardware CS control
//mode |= SPI_NO_CS;
//////
// open device
//////
fp = open(port, O_RDWR);
if (fp < 0)
Error("in 'init_spi_spidev(%s)': open port failed", port);
//////
// change properties
//////
// set SPI mode
ret = ioctl(fp, SPI_IOC_WR_MODE32, &mode);
if (ret == -1)
Error("in 'init_spi_spidev(%s)': set mode %d failed", port, mode);
// set SPI speed
ret = ioctl(fp, SPI_IOC_WR_MAX_SPEED_HZ, &baudrate);
if (ret == -1)
Error("in 'init_spi_spidev(%s)': set speed %d failed", port, baudrate);
// set bits per word
ret = ioctl(fp, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -1)
Error("in 'init_spi_spidev(%s)': set frame width %d failed", port, bits);
// wait 10ms
usleep(10000);
// return comm port handle
return fp;
#endif // __APPLE__ || __unix__
} // init_spi_spidev
/**
\fn void close_spi_spidev(HANDLE *fp)
\param[in] fp handle to SPI port
close & release SPI port.
*/
void close_spi_spidev(HANDLE *fp) {
/////////
// Windows
/////////
#if defined(WIN32) || defined(WIN64)
#error Windows not yet supported (which API?)
#endif // WIN32 || WIN64
/////////
// Posix
/////////
#if defined(__APPLE__) || defined(__unix__)
// if open, close port
if (*fp != 0) {
if (close(*fp) != 0)
Error("in 'close_spi_spidev': close port failed");
}
*fp = 0;
#endif // __APPLE__ || __unix__
} // close_spi_spidev
/**
\fn uint32_t send_spi_spidev(HANDLE fp, uint32_t lenTx, char *Tx)
\param[in] fp handle to SPI port
\param[in] lenTx number of bytes to send
\param[in] Tx array of bytes to send
\return number of sent bytes
send data via SPI port. Use this function to facilitate SPI communication
on different platforms, e.g. Windows and Posix.
*/
uint32_t send_spi_spidev(HANDLE fp, uint32_t lenTx, char *Tx) {
/////////
// Windows
/////////
#if defined(WIN32) || defined(WIN64)
#error Windows not yet supported (which API?)
#endif // WIN32 || WIN64
/////////
// Posix
/////////
#if defined(__APPLE__) || defined(__unix__)
uint32_t numChars;
// send data & return number of sent bytes
numChars = write(fp, Tx, lenTx);
// just to be on the safe side...
//SLEEP(5);
#endif // __APPLE__ || __unix__
// return number of sent bytes
return((uint32_t) numChars);
} // send_spi_spidev
/**
\fn uint32_t receive_spi_spidev(HANDLE fp, uint32_t lenRx, char *Rx)
\param[in] fp handle to SPI port
\param[in] lenRx number of bytes to receive
\param[out] Rx array containing bytes received
\return number of received bytes
receive data via SPI port. Use this function to facilitate SPI communication
on different platforms, e.g. Windows and Posix
*/
uint32_t receive_spi_spidev(HANDLE fp, uint32_t lenRx, char *Rx) {
/////////
// Windows
/////////
#if defined(WIN32) || defined(WIN64)
#error Windows not yet supported (which API?)
#endif // WIN32 || WIN64
/////////
// Posix
/////////
#if defined(__APPLE__) || defined(__unix__)
// send dummy bytes and read MISO back
int len = read(fp, Rx, lenRx);
// just to be on the safe side...
//SLEEP(5);
// return number of received bytes
return(len);
#endif // __APPLE__ || __unix__
} // receive_spi_spidev
/**
\fn void spi_transfer(HANDLE fp, int len, uint8_t *Tx, uint8_t *Rx)
\param[in] fp handle to SPI port
\param[in] len number of bytes to send & receive
\param[in] Tx array containing bytes to send
\param[out] Rx array containing bytes received
\return number of received bytes
send & receive data via SPI port. Use this function to facilitate SPI communication
on different platforms, e.g. Windows and Posix
*/
void spi_transfer(HANDLE fp, int len, uint8_t *Tx, uint8_t *Rx) {
/////////
// Windows
/////////
#if defined(WIN32) || defined(WIN64)
#error Windows not yet supported (which API?)
#endif // WIN32 || WIN64
/////////
// Posix
/////////
#if defined(__APPLE__) || defined(__unix__)
// fill SPI data structure
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)Tx,
.rx_buf = (unsigned long)Rx,
.len = len,
.delay_usecs = 4,
.speed_hz = 0,
.bits_per_word = 0,
};
// send & receive data
int ret = ioctl(fp, SPI_IOC_MESSAGE(1), &tr);
if (ret != 0)
Error("in 'spi_transfer': cannot transfer data (code %d)", ret);
#endif // __APPLE__ || __unix__
} // receive_spi_spidev
// end of file