Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SPI mode setting #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ static PyObject* openSPI(PyObject *self, PyObject *args, PyObject *kwargs) {
uint8_t bits = 8;
uint32_t speed = 500000;
uint16_t delay=0;
uint8_t temp;

int ret = 0;
int fd;
Expand Down Expand Up @@ -93,14 +94,19 @@ static PyObject* openSPI(PyObject *self, PyObject *args, PyObject *kwargs) {
}

// Set mode of SPI device
ret = ioctl(fd, SPI_IOC_WR_MODE, &spi_mode);
ret = ioctl(fd, SPI_IOC_RD_MODE, &temp);
if (ret == -1) {
pabort("can't set spi mode");
pabort("can't get spi mode");
}

ret = ioctl(fd, SPI_IOC_RD_MODE, &spi_mode);
temp &= ~(SPI_CPHA | SPI_CPOL);
temp |= spi_mode;
ret = ioctl(fd, SPI_IOC_WR_MODE, &temp);
if (ret == -1) {
pabort("can't get spi mode");
pabort("can't set spi mode");
}
ret = ioctl(fd, SPI_IOC_RD_MODE, &temp);
if (ret == -1 || ((temp & (SPI_CPHA|SPI_CPOL)) != spi_mode)) {
pabort("can't modify spi mode");
}

/*
Expand Down