Skip to content

Commit 7a9e005

Browse files
Hugo Lindströmfschrempf
authored andcommitted
spi_blk_maxsize
1 parent 60375bd commit 7a9e005

File tree

2 files changed

+40
-36
lines changed

2 files changed

+40
-36
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Performs an SPI transaction. Chip-select should be held active between blocks.
9292

9393
Similar to `xfer2` but accepts arbitrary large lists. If list size exceeds
9494
buffer size (which is read from `/sys/module/spidev/parameters/bufsiz`), data
95-
will be split into smaller chunks and sent in multiple operations.
95+
will be split into smaller 4096 byte chunks and sent in multiple operations.
9696

9797
close()
9898

spidev_module.c

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@
3939
#define SPIDEV_MAXPATH 4096
4040

4141
#define BLOCK_SIZE_CONTROL_FILE "/sys/module/spidev/parameters/bufsiz"
42-
// The xfwr3 function attempts to use large blocks if /sys/module/spidev/parameters/bufsiz setting allows it.
42+
// The xfwr function attempts to use large blocks if /sys/module/spidev/parameters/bufsiz setting allows it.
4343
// However where we cannot get a value from that file, we fall back to this safe default.
44-
#define XFER3_DEFAULT_BLOCK_SIZE SPIDEV_MAXPATH
45-
// Largest block size for xfer3 - even if /sys/module/spidev/parameters/bufsiz allows bigger
44+
#define XFER_DEFAULT_BLOCK_SIZE SPIDEV_MAXPATH
45+
// Largest block size for xfer - even if /sys/module/spidev/parameters/bufsiz allows bigger
4646
// blocks, we won't go above this value. As I understand, DMA is not used for anything bigger so why bother.
47-
#define XFER3_MAX_BLOCK_SIZE 65535
47+
#define XFER_MAX_BLOCK_SIZE 65535
4848

4949

5050
#if PY_MAJOR_VERSION < 3
@@ -60,38 +60,38 @@
6060
#define PyInt_Type PyLong_Type
6161
#endif
6262

63-
// Maximum block size for xfer3
64-
// Initialised once by get_xfer3_block_size
65-
uint32_t xfer3_block_size = 0;
63+
// Maximum block size for xfer
64+
// Initialised once by get_xfer_block_size
65+
uint32_t xfer_block_size = 0;
6666

6767
// Read maximum block size from the /sys/module/spidev/parameters/bufsiz
68-
// In case of any problems reading the number, we fall back to XFER3_DEFAULT_BLOCK_SIZE.
69-
// If number is read ok but it exceeds the XFER3_MAX_BLOCK_SIZE, it will be capped to that value.
68+
// In case of any problems reading the number, we fall back to XFER_DEFAULT_BLOCK_SIZE.
69+
// If number is read ok but it exceeds the XFER_MAX_BLOCK_SIZE, it will be capped to that value.
7070
// The value is read and cached on the first invocation. Following invocations just return the cached one.
71-
uint32_t get_xfer3_block_size(void) {
71+
uint32_t get_xfer_block_size(void) {
7272
int value;
7373

7474
// If value was already initialised, just use it
75-
if (xfer3_block_size != 0) {
76-
return xfer3_block_size;
75+
if (xfer_block_size != 0) {
76+
return xfer_block_size;
7777
}
7878

7979
// Start with the default
80-
xfer3_block_size = XFER3_DEFAULT_BLOCK_SIZE;
80+
xfer_block_size = XFER_DEFAULT_BLOCK_SIZE;
8181

8282
FILE *file = fopen(BLOCK_SIZE_CONTROL_FILE,"r");
8383
if (file != NULL) {
8484
if (fscanf(file, "%d", &value) == 1 && value > 0) {
85-
if (value <= XFER3_MAX_BLOCK_SIZE) {
86-
xfer3_block_size = value;
85+
if (value <= XFER_MAX_BLOCK_SIZE) {
86+
xfer_block_size = value;
8787
} else {
88-
xfer3_block_size = XFER3_MAX_BLOCK_SIZE;
88+
xfer_block_size = XFER_MAX_BLOCK_SIZE;
8989
}
9090
}
9191
fclose(file);
9292
}
9393

94-
return xfer3_block_size;
94+
return xfer_block_size;
9595
}
9696

9797
PyDoc_STRVAR(SpiDev_module_doc,
@@ -173,12 +173,13 @@ PyDoc_STRVAR(SpiDev_write_doc,
173173
static PyObject *
174174
SpiDev_writebytes(SpiDevObject *self, PyObject *args)
175175
{
176-
int status;
177-
uint16_t ii, len;
178-
uint8_t buf[SPIDEV_MAXPATH];
179-
PyObject *obj;
180-
PyObject *seq;
181-
char wrmsg_text[4096];
176+
int status;
177+
uint16_t ii, len;
178+
PyObject *obj;
179+
PyObject *seq;
180+
const uint32_t spi_blk_maxsize = get_xfer_block_size();
181+
uint8_t buf[spi_blk_maxsize];
182+
char wrmsg_text[spi_blk_maxsize];
182183

183184
if (!PyArg_ParseTuple(args, "O:write", &obj))
184185
return NULL;
@@ -193,8 +194,8 @@ SpiDev_writebytes(SpiDevObject *self, PyObject *args)
193194
return NULL;
194195
}
195196

196-
if (len > SPIDEV_MAXPATH) {
197-
snprintf(wrmsg_text, sizeof (wrmsg_text) - 1, wrmsg_listmax, SPIDEV_MAXPATH);
197+
if (len > spi_blk_maxsize) {
198+
snprintf(wrmsg_text, sizeof (wrmsg_text) - 1, wrmsg_listmax, spi_blk_maxsize);
198199
PyErr_SetString(PyExc_OverflowError, wrmsg_text);
199200
return NULL;
200201
}
@@ -242,7 +243,8 @@ PyDoc_STRVAR(SpiDev_read_doc,
242243
static PyObject *
243244
SpiDev_readbytes(SpiDevObject *self, PyObject *args)
244245
{
245-
uint8_t rxbuf[SPIDEV_MAXPATH];
246+
const uint32_t spi_blk_maxsize = get_xfer_block_size();
247+
uint8_t rxbuf[spi_blk_maxsize];
246248
int status, len, ii;
247249
PyObject *list;
248250

@@ -284,7 +286,7 @@ SpiDev_writebytes2_buffer(SpiDevObject *self, Py_buffer *buffer)
284286
int status;
285287
Py_ssize_t remain, block_size, block_start, spi_max_block;
286288

287-
spi_max_block = get_xfer3_block_size();
289+
spi_max_block = get_xfer_block_size();
288290

289291
block_start = 0;
290292
remain = buffer->len;
@@ -380,7 +382,7 @@ SpiDev_writebytes2_seq(SpiDevObject *self, PyObject *seq)
380382
return NULL;
381383
}
382384

383-
spi_max_block = get_xfer3_block_size();
385+
spi_max_block = get_xfer_block_size();
384386

385387
bufsize = (len < spi_max_block) ? len : spi_max_block;
386388

@@ -475,7 +477,8 @@ SpiDev_xfer(SpiDevObject *self, PyObject *args)
475477
memset(&xfer, 0, sizeof(xfer));
476478
#endif
477479
uint8_t *txbuf, *rxbuf;
478-
char wrmsg_text[4096];
480+
const uint32_t spi_blk_maxsize = get_xfer_block_size();
481+
char wrmsg_text[spi_blk_maxsize];
479482

480483
if (!PyArg_ParseTuple(args, "O|IHB:xfer", &obj, &speed_hz, &delay_usecs, &bits_per_word))
481484
return NULL;
@@ -492,8 +495,8 @@ SpiDev_xfer(SpiDevObject *self, PyObject *args)
492495
return NULL;
493496
}
494497

495-
if (len > SPIDEV_MAXPATH) {
496-
snprintf(wrmsg_text, sizeof(wrmsg_text) - 1, wrmsg_listmax, SPIDEV_MAXPATH);
498+
if (len > spi_blk_maxsize) {
499+
snprintf(wrmsg_text, sizeof(wrmsg_text) - 1, wrmsg_listmax, spi_blk_maxsize);
497500
PyErr_SetString(PyExc_OverflowError, wrmsg_text);
498501
Py_DECREF(seq);
499502
return NULL;
@@ -644,7 +647,8 @@ SpiDev_xfer2(SpiDevObject *self, PyObject *args)
644647
memset(&xfer, 0, sizeof(xfer));
645648
Py_END_ALLOW_THREADS
646649
uint8_t *txbuf, *rxbuf;
647-
char wrmsg_text[4096];
650+
const uint32_t spi_blk_maxsize = get_xfer_block_size();
651+
char wrmsg_text[spi_blk_maxsize];
648652

649653
if (!PyArg_ParseTuple(args, "O|IHB:xfer2", &obj, &speed_hz, &delay_usecs, &bits_per_word))
650654
return NULL;
@@ -661,8 +665,8 @@ SpiDev_xfer2(SpiDevObject *self, PyObject *args)
661665
return NULL;
662666
}
663667

664-
if (len > SPIDEV_MAXPATH) {
665-
snprintf(wrmsg_text, sizeof(wrmsg_text) - 1, wrmsg_listmax, SPIDEV_MAXPATH);
668+
if (len > spi_blk_maxsize) {
669+
snprintf(wrmsg_text, sizeof(wrmsg_text) - 1, wrmsg_listmax, spi_blk_maxsize);
666670
PyErr_SetString(PyExc_OverflowError, wrmsg_text);
667671
Py_DECREF(seq);
668672
return NULL;
@@ -783,7 +787,7 @@ SpiDev_xfer3(SpiDevObject *self, PyObject *args)
783787
return NULL;
784788
}
785789

786-
bufsize = get_xfer3_block_size();
790+
bufsize = get_xfer_block_size();
787791
if (bufsize > len) {
788792
bufsize = len;
789793
}

0 commit comments

Comments
 (0)