-
Notifications
You must be signed in to change notification settings - Fork 150
/
spi.c
368 lines (308 loc) · 11.5 KB
/
spi.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* SPI testing utility (see copyright beow)
* adapted for use in Python
* by Louis Thiery
* Lots more flexibility and cleanup by Connor Wolf (imaginaryindustries.com)
*
* compile for Python using: "python setup.py build"
* compiled module will be in "./build/lib.linux-armv6l-2.7/spi.so"
*
* SPI testing utility (using spidev driver)
*
* Copyright (c) 2007 MontaVista Software, Inc.
* Copyright (c) 2007 Anton Vorontsov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
*/
#include <Python.h>
#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>
static void pabort(const char *s)
{
perror(s);
abort();
}
static PyObject* openSPI(PyObject *self, PyObject *args, PyObject *kwargs) {
// Default parameters
char *device = "/dev/spidev0.0";
uint8_t mode=0;
uint8_t bits = 8;
uint32_t speed = 500000;
uint16_t delay=0;
int ret = 0;
int fd;
static char* kwlist[] = {"device", "mode", "bits", "speed", "delay", NULL};
// Adding some sort of mode parsing would probably be a nice idea for the future, so you don't have to specify it as a bitfield
// stuffed into an int.
// For the moment the default mode ("0"), will probably work for 99% of people who need a SPI interface, so I'm not working on that
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|siiii:keywords", kwlist, &device, &mode, &bits, &speed, &delay)) {
return NULL;
}
//
// Parse mode of SPI device, option 0, 1, 2, 3
//
uint8_t spi_mode;
switch(mode) {
case 0:
spi_mode = SPI_MODE_0;
break;
case 1:
spi_mode = SPI_MODE_1;
break;
case 2:
spi_mode = SPI_MODE_2;
break;
case 3:
spi_mode = SPI_MODE_3;
break;
default:
spi_mode = SPI_MODE_0;
}
// It's not clearly documented, but it seems that PyArg_ParseTupleAndKeywords basically only modifies the values passed to it if the
// keyword pertaining to that value is passed to the function. As such, the defaults specified by the variable definition are used
// unless you pass a kwd argument.
// Note that there isn't any proper bounds-checking, so if you pass a value that exceeds the variable size, it's just truncated before
// being stuffed into the avasilable space. For example, passing a bits-per-word of 500 gets truncated to 244. Unfortunately, the
// PyArg_ParseTupleAndKeywords function only seems to support ints of 32 bits.
PyErr_Clear();
// printf("Mode: %i, Bits: %i, Speed: %i, Delay: %i\n", mode, bits, speed, delay);
fd = open(device, O_RDWR);
if (fd < 0) {
pabort("can't open device");
}
// Set mode of SPI device
ret = ioctl(fd, SPI_IOC_WR_MODE, &spi_mode);
if (ret == -1) {
pabort("can't set spi mode");
}
ret = ioctl(fd, SPI_IOC_RD_MODE, &spi_mode);
if (ret == -1) {
pabort("can't get spi mode");
}
/*
* bits per word
*/
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -1)
pabort("can't set bits per word");
ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
if (ret == -1)
pabort("can't get bits per word");
/*
* max speed hz
*/
ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
if (ret == -1)
pabort("can't set max speed hz");
ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
if (ret == -1)
pabort("can't get max speed hz");
// Stuff the various initilization parameters into a dict, and return that.
// Note that the returned values may not be completely real. It seems that, at least for the speed value,
// the hardware only has several possible settings (250000, 500000, 1000000, etc...) Strangely enough, the
// ioctl for setting the speed *returns the speed you specify*. However, the hardware seems to default to the
// closest avalable value *below* the specified rate. (i.e. you will never get a speed faster then you spec),
// but you may get a slower value.
//It would probably be a good idea to bin-down the passed arguement to the available values, and return
// that.
#if PY_MAJOR_VERSION >= 3
PyObject* key_mode = PyBytes_FromString("mode");
PyObject* key_bits = PyBytes_FromString("bits");
PyObject* key_speed = PyBytes_FromString("speed");
PyObject* key_delay = PyBytes_FromString("delay");
PyObject* key_fd = PyBytes_FromString("fd");
PyObject* val_mode = PyLong_FromLong((long)spi_mode);
PyObject* val_bits = PyLong_FromLong((long)bits);
PyObject* val_speed = PyLong_FromLong((long)speed);
PyObject* val_delay = PyLong_FromLong((long)delay);
PyObject* val_fd = PyLong_FromLong((long)fd);
#else
PyObject* key_mode = PyString_FromString("mode");
PyObject* key_bits = PyString_FromString("bits");
PyObject* key_speed = PyString_FromString("speed");
PyObject* key_delay = PyString_FromString("delay");
PyObject* key_fd = PyString_FromString("fd");
PyObject* val_mode = PyInt_FromLong((long)spi_mode);
PyObject* val_bits = PyInt_FromLong((long)bits);
PyObject* val_speed = PyInt_FromLong((long)speed);
PyObject* val_delay = PyInt_FromLong((long)delay);
PyObject* val_fd = PyInt_FromLong((long)fd);
#endif
PyObject* retDict;
retDict = PyDict_New();
PyDict_SetItem(retDict, key_mode, val_mode);
PyDict_SetItem(retDict, key_bits, val_bits);
PyDict_SetItem(retDict, key_speed, val_speed);
PyDict_SetItem(retDict, key_delay, val_delay);
PyDict_SetItem(retDict, key_fd, val_fd);
Py_XDECREF(key_mode);Py_XDECREF(val_mode);
Py_XDECREF(key_bits);Py_XDECREF(val_bits);
Py_XDECREF(key_speed);Py_XDECREF(val_speed);
Py_XDECREF(key_delay);Py_XDECREF(val_delay);
Py_XDECREF(key_fd);Py_XDECREF(val_fd);
return retDict;
}
static PyObject* transfer(PyObject* self, PyObject* args) {
uint8_t bits = 8;
uint32_t speed = 500000;
uint16_t delay;
int ret = 0;
int fd;
PyObject* dict;
PyObject* transferTuple;
// "O" - Gets non-NULL borrowed reference to Python argument.
// As far as I can tell, it's mostly just copying arg[0] into transferTuple
// and making sure at least one arg has been passed (I think)
if(!PyArg_ParseTuple(args, "OO", &dict, &transferTuple)) {
return NULL;
}
// Check that dictionary was parsed correctly
if(!PyDict_Check(dict)) {
pabort("First argument must be a valid dictionary.");
}
// Check that transfer tuple was parsed correctly.
if(!PyTuple_Check(transferTuple)) {
pabort("Second argument must be a tuple of bytes.\n");
}
// Declare these variable separately so we can manually decrease reference
// counts when finished and free up memory
#if PY_MAJOR_VERSION >= 3
PyObject* p_bits = PyBytes_FromString("bits");
PyObject* p_speed = PyBytes_FromString("speed");
PyObject* p_delay = PyBytes_FromString("delay");
PyObject* p_fd = PyBytes_FromString("fd");
#else
PyObject* p_bits = PyString_FromString("bits");
PyObject* p_speed = PyString_FromString("speed");
PyObject* p_delay = PyString_FromString("delay");
PyObject* p_fd = PyString_FromString("fd");
#endif
// Get SPI device parameters to use
#if PY_MAJOR_VERSION >= 3
bits = (uint8_t) PyLong_AsUnsignedLong(PyDict_GetItem( dict, p_bits ));
speed = (uint32_t) PyLong_AsUnsignedLong(PyDict_GetItem( dict, p_speed ));
delay = (uint16_t) PyLong_AsUnsignedLong(PyDict_GetItem( dict, p_delay ));
fd = (int) PyLong_AsLong(PyDict_GetItem( dict, p_fd ));
#else
bits = (uint8_t) PyInt_AsUnsignedLongMask(PyDict_GetItem( dict, p_bits ));
speed = (uint32_t) PyInt_AsUnsignedLongMask(PyDict_GetItem( dict, p_speed ));
delay = (uint16_t) PyInt_AsUnsignedLongMask(PyDict_GetItem( dict, p_delay ));
fd = (int) PyInt_AsLong(PyDict_GetItem( dict, p_fd ));
#endif
// Decrease reference counts for tempoerary variables, freeing memory
Py_XDECREF(p_bits);
Py_XDECREF(p_speed);
Py_XDECREF(p_delay);
Py_XDECREF(p_fd);
// printf("Mode: %i, Bits: %i, Speed: %i, Delay: %i\n", mode, bits, speed, delay);
uint32_t tupleSize = PyTuple_Size(transferTuple);
uint8_t tx[tupleSize];
uint8_t rx[tupleSize];
PyObject* tempItem;
uint16_t i=0;
while(i < tupleSize) {
tempItem = PyTuple_GetItem(transferTuple, i); //
#if PY_MAJOR_VERSION >= 3
if(!PyLong_Check(tempItem)) {
pabort("non-integer contained in tuple\n");
}
#else
if(!PyInt_Check(tempItem)) {
pabort("non-integer contained in tuple\n");
}
#endif
#if PY_MAJOR_VERSION >= 3
tx[i] = (uint8_t)PyLong_AsSsize_t(tempItem);
#else
tx[i] = (uint8_t)PyInt_AsSsize_t(tempItem);
#endif
i++;
}
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx,
.rx_buf = (unsigned long)rx,
.len = tupleSize,
.delay_usecs = delay,
.speed_hz = speed,
.bits_per_word = bits,
.cs_change = 0,
};
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1) {
pabort("can't send spi message");
}
// This explains why the input data are not overwritten by the received data
// If you delete this line you could have the function overwrite the input
// data with the received data. (Works in Python 3, not Python 2)
transferTuple = PyTuple_New(tupleSize);
for(i=0;i<tupleSize;i++) {
if(PyTuple_SetItem(transferTuple, i, Py_BuildValue("i",rx[i]))) {
printf("spi: can't set value in data tuple.\n");
}
}
return transferTuple;
}
static PyObject* closeSPI(PyObject* self,PyObject* args) {
PyObject* dict;
if(!PyArg_ParseTuple(args, "O", &dict)) { // "O" - Gets non-NULL borrowed reference to Python argument.
return NULL; // As far as I can tell, it's mostly just copying arg[0] into transferTuple
}
#if PY_MAJOR_VERSION >= 3
PyObject* p_fd = PyBytes_FromString("fd");
#else
PyObject* p_fd = PyString_FromString("fd");
#endif
#if PY_MAJOR_VERSION >= 3
int fd = (int) PyLong_AsLong(PyDict_GetItem( dict, p_fd ));
#else
int fd = (int) PyInt_AsLong(PyDict_GetItem( dict, p_fd ));
#endif
close(fd);
Py_XDECREF(p_fd);
Py_RETURN_NONE;
}
static PyMethodDef SpiMethods[] =
{
{"openSPI", (PyCFunction)openSPI, METH_VARARGS | METH_KEYWORDS, "Open SPI Port."},
{"transfer", (PyCFunction)transfer, METH_VARARGS, "Transfer data."},
{"closeSPI", (PyCFunction)closeSPI, METH_VARARGS, "Close SPI port."},
{NULL, NULL, 0, NULL}
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"spi", /* m_name */
"spi library", /* m_doc */
-1, /* m_size */
SpiMethods, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL, /* m_free */
};
#endif
PyMODINIT_FUNC
#if PY_MAJOR_VERSION >= 3
PyInit_spi(void)
#else
initspi(void)
#endif
{
#if PY_MAJOR_VERSION >= 3
PyObject *module = PyModule_Create(&moduledef);
#else
(void) Py_InitModule("spi", SpiMethods);
#endif
#if PY_MAJOR_VERSION >= 3
return module;
#endif
}