Skip to content

Commit

Permalink
Merge pull request #45 from ams-tech/master
Browse files Browse the repository at this point in the history
Add support for "SPI_NO_CS" flag
  • Loading branch information
doceme committed May 22, 2016
2 parents 700fc50 + 942e894 commit 511db0d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ spi.mode = 0b01

* `bits_per_word`
* `cshigh`
* `loop`
* `loop` - Set the "SPI_LOOP" flag to enable loopback mode
* `no_cs` - Set the "SPI_NO_CS" flag to disable use of the chip select (although the driver may still own the CS pin)
* `lsbfirst`
* `max_speed_hz`
* `mode` - SPI mode as two bit pattern of clock polarity and phase [CPOL|CPHA], min: 0b00 = 0, max: 0b11 = 3
Expand Down
44 changes: 44 additions & 0 deletions spidev_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,20 @@ SpiDev_get_loop(SpiDevObject *self, void *closure)
return result;
}

static PyObject *
SpiDev_get_no_cs(SpiDevObject *self, void *closure)
{
PyObject *result;

if (self->mode & SPI_NO_CS)
result = Py_True;
else
result = Py_False;

Py_INCREF(result);
return result;
}


static int
SpiDev_set_mode(SpiDevObject *self, PyObject *val, void *closure)
Expand Down Expand Up @@ -702,6 +716,34 @@ SpiDev_set_3wire(SpiDevObject *self, PyObject *val, void *closure)
return 0;
}

static int
SpiDev_set_no_cs(SpiDevObject *self, PyObject *val, void *closure)
{
uint8_t tmp;

if (val == NULL) {
PyErr_SetString(PyExc_TypeError,
"Cannot delete attribute");
return -1;
}
else if (!PyBool_Check(val)) {
PyErr_SetString(PyExc_TypeError,
"The no_cs attribute must be boolean");
return -1;
}

if (val == Py_True)
tmp = self->mode | SPI_NO_CS;
else
tmp = self->mode & ~SPI_NO_CS;

__spidev_set_mode(self->fd, tmp);

self->mode = tmp;
return 0;
}


static int
SpiDev_set_loop(SpiDevObject *self, PyObject *val, void *closure)
{
Expand Down Expand Up @@ -834,6 +876,8 @@ static PyGetSetDef SpiDev_getset[] = {
"LSB first\n"},
{"loop", (getter)SpiDev_get_loop, (setter)SpiDev_set_loop,
"loopback configuration\n"},
{"no_cs", (getter)SpiDev_get_no_cs, (setter)SpiDev_set_no_cs,
"disable chip select\n"},
{"bits_per_word", (getter)SpiDev_get_bits_per_word, (setter)SpiDev_set_bits_per_word,
"bits per word\n"},
{"max_speed_hz", (getter)SpiDev_get_max_speed_hz, (setter)SpiDev_set_max_speed_hz,
Expand Down

0 comments on commit 511db0d

Please sign in to comment.