SPI answer repeats sent bytes

32 views
Skip to first unread message

Peter Jeschke

unread,
Jun 26, 2017, 8:06:32 AM6/26/17
to Pi4J
I'm using Pi4J to write to a SPI interface. This issue occurs in a big application, but I managed to reproduce it with a small example:

public class Main {
public static void main(String[] args) throws IOException {
SpiDevice spi = SpiFactory.getInstance(SpiChannel.CS0);

try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
String line;
while ((line = reader.readLine()) != null) {
String[] split = line.split(" ");
ByteBuffer buf = ByteBuffer.allocate(split.length);
Stream.of(split).map(Byte::parseByte).forEach(buf::put);
ByteBuffer result = spi.write(buf);
System.out.println(Arrays.toString(result.array()));
}
}

}
}

This code reads bytes from stdin, wraps them in a ByteBuffer and sends it over SPI. The result is then written to stdout. I'm noticing some strange behaviour here: Everything I enter is repeated, with an offset of one byte:

pi@raspberrypi:~ $ java -jar spi-test.jar
80 80
[0, 80]
60 60
[0, 60]
43 42
[0, 43]
43 42 41
[0, 43, 42]

This is not the expected behaviour. It should always return [0, 0, 1]. Trying to transmit the same with the spincl utility, I get the expected result: 

pi@raspberrypi:~/spincl $ sudo ./spincl -m0 3 43 42 41
0x00 0x00 0x01

Is there some error in my java code or is there some known issue with SPI?

I tried to compile the code against both v1.1 and v1.2-SNAPSHOT. This happens with both versions.
Reply all
Reply to author
Forward
0 new messages