/
le_spiLibrary.c
337 lines (294 loc) · 9.15 KB
/
le_spiLibrary.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
*
*
* <hr>
*
* Copyright (C) Sierra Wireless Inc.
*/
#include "legato.h"
#include "interfaces.h"
#include "le_spiLibrary.h"
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
//--------------------------------------------------------------------------------------------------
/**
* Configures the SPI bus for use with a specific device.
*/
//--------------------------------------------------------------------------------------------------
void le_spiLib_Configure
(
int fd, ///< [in] name of device file. dont pass */dev* prefix
int mode, ///< [in] mode options for the bus as defined in spidev.h.
uint8_t bits, ///< [in] bits per word, typically 8 bits per word
uint32_t speed, ///< [in] max speed (Hz), this is slave dependant
int msb ///< [in] set as 0 for MSB as first bit or 1 for LSB as first bit
)
{
LE_DEBUG("Running the configure library call");
int ret;
LE_FATAL_IF(
((ret = ioctl(fd, SPI_IOC_WR_MODE, &mode)) < 0),
"SPI modeset failed with error %d: %d (%m)",
ret,
errno);
LE_FATAL_IF(
((ret = ioctl(fd, SPI_IOC_RD_MODE, &mode)) < 0),
"SPI modeget failed with error %d: %d (%m)",
ret,
errno);
LE_FATAL_IF(
((ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits)) < 0),
"SPI bitset failed with error %d : %d (%m)",
ret,
errno);
LE_FATAL_IF(
((ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits)) < 0),
"SPI bitget failed with error %d : %d (%m)",
ret,
errno);
LE_FATAL_IF(
((ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed)) < 0),
"SPI speedset failed with error %d : %d (%m)",
ret,
errno);
LE_FATAL_IF(
((ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed)) < 0),
"SPI speedget failed with error %d : %d (%m)",
ret,
errno);
LE_FATAL_IF(
((ret = ioctl(fd, SPI_IOC_WR_LSB_FIRST, &msb)) < 0),
"SPI MSB/LSB write failed with error %d : %d (%m)",
ret,
errno);
LE_FATAL_IF(
((ret = ioctl(fd, SPI_IOC_RD_LSB_FIRST, &msb)) < 0),
"SPI MSB/LSB read failed with error %d : %d (%m)",
ret,
errno);
LE_DEBUG("Mode = %d, Speed = %d, Bits = %d, MSB = %d", mode, speed, bits, msb);
}
/**-----------------------------------------------------------------------------------------------
* Performs SPI WriteRead Half Duplex. You can send send Read command/ address of data to read.
*
* @return
* - LE_OK
* - LE_FAULT
*
* @note
* Some devices do not support this mode. Check the data sheet of the device.
*/
//--------------------------------------------------------------------------------------------------
le_result_t le_spiLib_WriteReadHD
(
int fd, ///< [in] open file descriptor of SPI port
const uint8_t* writeData, ///< [in] tx command/address being sent to slave
size_t writeDataLength, ///< [in] number of bytes in tx message
uint8_t* readData, ///< [out] rx response from slave
size_t* readDataLength ///< [in/out] number of bytes in rx message
)
{
int transferResult;
le_result_t result;
struct spi_ioc_transfer tr[] =
{
{
.tx_buf = (unsigned long)writeData,
.rx_buf = (unsigned long)NULL,
.len = writeDataLength,
// .delay_usecs = delay_out,
.cs_change = 0
},
{
.tx_buf = (unsigned long)NULL,
.rx_buf = (unsigned long)readData,
.len = *readDataLength,
// .delay_usecs = delay_in,
.cs_change = 0
}
};
LE_DEBUG("Transmitting this message...len:%zu", writeDataLength);
for (size_t i = 0; i < writeDataLength; i++)
{
LE_DEBUG("%.2X ", writeData[i]);
}
transferResult = ioctl(fd, SPI_IOC_MESSAGE(2), tr);
if (transferResult < 1)
{
LE_ERROR("Transfer failed with error %d : %d (%m)", transferResult, errno);
LE_ERROR("can't send spi message");
result = LE_FAULT;
}
else
{
LE_DEBUG("Successful transmission with success %d", transferResult);
result = LE_OK;
}
LE_DEBUG("Received message...");
for (size_t i = 0; i < *readDataLength; i++)
{
LE_DEBUG("%.2X ", readData[i]);
}
return result;
}
//--------------------------------------------------------------------------------------------------
/**
* Performs SPI Write Half Duplex. You can send send Write command/ address of data to write/data
* to write.
*
* @return
* - LE_OK
* - LE_FAULT
*
* @note
* Some devices do not support this mode. Check the data sheet of the device.
*/
//--------------------------------------------------------------------------------------------------
le_result_t le_spiLib_WriteHD
(
int fd, ///< [in] open file descriptor of SPI port
const uint8_t* writeData, ///< [in] command/address being sent to slave
size_t writeDataLength ///< [in] number of bytes in tx message
)
{
int transferResult;
le_result_t result;
struct spi_ioc_transfer tr[] =
{
{
.tx_buf = (unsigned long)writeData,
.rx_buf = (unsigned long)NULL,
.len = writeDataLength,
// .delay_usecs = delay_out,
.cs_change = 0
}
};
LE_DEBUG("Transferring this message...len: %zu", writeDataLength);
for (size_t i = 0; i < writeDataLength; i++)
{
LE_DEBUG("%.2X ", writeData[i]);
}
transferResult = ioctl(fd, SPI_IOC_MESSAGE(1), tr);
if (transferResult < 1)
{
LE_ERROR("Transfer failed with error %d : %d (%m)", transferResult, errno);
LE_ERROR("can't send spi message");
result = LE_FAULT;
}
else
{
LE_DEBUG("%d", transferResult);
result = LE_OK;
}
return result;
}
/**-----------------------------------------------------------------------------------------------
* Performs SPI WriteRead Full Duplex. You can send send Read command/ address of data to read.
*
* @return
* - LE_OK
* - LE_FAULT
*
* @note
* Some devices do not support this mode. Check the data sheet of the device.
*/
//--------------------------------------------------------------------------------------------------
le_result_t le_spiLib_WriteReadFD
(
int fd, ///< [in] open file descriptor of SPI port
const uint8_t* writeData, ///< [in] tx command/address being sent to slave
uint8_t* readData, ///< [out] rx response from slave
size_t dataLength ///< [in/out] number of bytes in tx message
)
{
int transferResult;
le_result_t result;
struct spi_ioc_transfer tr[] =
{
{
.tx_buf = (unsigned long)writeData,
.rx_buf = (unsigned long)readData,
.len = dataLength,
// .delay_usecs = delay_out,
.cs_change = 0
},
};
LE_DEBUG("Transmitting this message...len:%zu", dataLength);
for (size_t i = 0; i < dataLength; i++)
{
LE_DEBUG("%.2X ", writeData[i]);
}
transferResult = ioctl(fd, SPI_IOC_MESSAGE(1), tr);
if (transferResult < 1)
{
LE_ERROR("Transfer failed with error %d : %d (%m)", transferResult, errno);
LE_ERROR("can't send spi message");
result = LE_FAULT;
}
else
{
LE_DEBUG("Successful transmission with success %d", transferResult);
result = LE_OK;
}
LE_DEBUG("Received message...");
for (size_t i = 0; i < dataLength; i++)
{
LE_DEBUG("%.2X ", readData[i]);
}
return result;
}
//--------------------------------------------------------------------------------------------------
/**
* Performs SPI Read. You can send send Write command/ address of data to write/data to write.
*
* @return
* - LE_OK
* - LE_FAULT
*
* @note
* Some devices do not support this mode. Check the data sheet of the device.
*/
//--------------------------------------------------------------------------------------------------
le_result_t le_spiLib_ReadHD
(
int fd, ///< [in] open file descriptor of SPI port
uint8_t* readData, ///< [out] data being sent by slave
size_t* readDataLength ///< [in/out] number of bytes in rx message
)
{
int transferResult;
le_result_t result;
struct spi_ioc_transfer tr[] =
{
{
.tx_buf = (unsigned long)NULL,
.rx_buf = (unsigned long)readData,
.len = *readDataLength,
// .delay_usecs = delay_out,
.cs_change = 0
}
};
transferResult = ioctl(fd, SPI_IOC_MESSAGE(1), tr);
if (transferResult < 1)
{
LE_ERROR("Transfer failed with error %d : %d (%m)", transferResult, errno);
LE_ERROR("can't receive spi message");
result = LE_FAULT;
}
else
{
LE_DEBUG("%d", transferResult);
result = LE_OK;
}
LE_DEBUG("Received message...");
for (size_t i = 0; i < *readDataLength; i++)
{
LE_DEBUG("%.2X ", readData[i]);
}
return result;
}
COMPONENT_INIT
{
LE_DEBUG("spiLibrary initializing");
}