Skip to content

SPI mode inconsistency #1094

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

Closed
kroggen opened this issue Feb 10, 2018 · 1 comment
Closed

SPI mode inconsistency #1094

kroggen opened this issue Feb 10, 2018 · 1 comment

Comments

@kroggen
Copy link
Contributor

kroggen commented Feb 10, 2018

Hi!

I guess there is an inconsistency in the SPI mode assignment.

In the function spiSetDataMode we have:

case SPI_MODE2:
spi->dev->pin.ck_idle_edge = 1;
spi->dev->user.ck_out_edge = 1;
break;
case SPI_MODE3:
spi->dev->pin.ck_idle_edge = 1;
spi->dev->user.ck_out_edge = 0;
break;

And in the function spiTransaction we have:

case SPI_MODE2:
spi->dev->pin.ck_idle_edge = 1;
spi->dev->user.ck_out_edge = 0;
break;
case SPI_MODE3:
spi->dev->pin.ck_idle_edge = 1;
spi->dev->user.ck_out_edge = 1;
break;

The user.ck_out_edge value is different.

Here is the spiGetDataMode function:

bool idleEdge = spi->dev->pin.ck_idle_edge;
bool outEdge = spi->dev->user.ck_out_edge;
if(idleEdge) {
if(outEdge) {
return SPI_MODE3;
}
return SPI_MODE2;
}
if(outEdge) {
return SPI_MODE1;
}
return SPI_MODE0;

@kroggen
Copy link
Contributor Author

kroggen commented Feb 10, 2018

In my tests with an AD7124-4 device, that uses SPI in mode 3, I got the following results:

1 - Without transactions, using the SPI.setDataMode:

It is working with modes 0 and 3.

2 - With transactions:

It is working with modes 0 and 2.

So I concluded that the spiSetDataMode function is right and the spiTransaction and the spiGetDataMode functions are wrong.

This property of working on 2 modes makes sense when we check the SPI modes operation:

SPI modes

Both modes 0 and 3 sample in the rising edge of the clock pulse.

Notice also that mode 2 shifts out the bit in the rising edge (1) and mode 3 shifts out in the falling edge (0).

*image taken from https://www.allaboutcircuits.com/technical-articles/spi-serial-peripheral-interface/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant