Skip to content

Commit 602deab

Browse files
committed
#454 Changed TransferBuffer for SPI, minor changes
1 parent 42d547b commit 602deab

File tree

5 files changed

+93
-52
lines changed

5 files changed

+93
-52
lines changed

plugins/pi4j-plugin-ffm/src/main/java/com/pi4j/plugin/ffm/common/spi/SpiIocTransfer.java

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.pi4j.plugin.ffm.common.Pi4JLayout;
44

5-
import java.lang.foreign.Arena;
65
import java.lang.foreign.MemoryLayout;
76
import java.lang.foreign.MemorySegment;
87
import java.lang.foreign.ValueLayout;
@@ -14,13 +13,9 @@
1413
/**
1514
* Source: /usr/src/linux-headers-6.8.0-52-generic/include/linux/spi/spidev.h:70:0
1615
*/
17-
public record SpiIocTransfer(byte[] txBuf, byte[] rxBuf, int length, int speedHz, short delayUsecs, byte bitsPerWord,
18-
byte csChange, byte txNbits, byte rxNbits, byte wordDelayUsecs,
19-
byte pad) implements Pi4JLayout {
20-
21-
public SpiIocTransfer(byte[] txBuf, byte[] rxBuf, int length) {
22-
this(txBuf, rxBuf, length, 0, (short) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0);
23-
}
16+
record SpiIocTransfer(byte[] txBuf, byte[] rxBuf, int length, int speedHz, short delayUsecs, byte bitsPerWord,
17+
byte csChange, byte txNbits, byte rxNbits, byte wordDelayUsecs,
18+
byte pad) implements Pi4JLayout {
2419

2520
public static final MemoryLayout LAYOUT = MemoryLayout.structLayout(
2621
ValueLayout.JAVA_LONG.withName("tx_buf"),
@@ -50,18 +45,6 @@ public SpiIocTransfer(byte[] txBuf, byte[] rxBuf, int length) {
5045
private static final VarHandle VH_PAD = LAYOUT.varHandle(groupElement("pad"));
5146

5247

53-
public static SpiIocTransfer create(MemorySegment memorySegment) throws Throwable {
54-
var spiIocTransferInstance = SpiIocTransfer.createEmpty();
55-
if (!memorySegment.equals(MemorySegment.NULL)) {
56-
spiIocTransferInstance = spiIocTransferInstance.from(memorySegment);
57-
}
58-
return spiIocTransferInstance;
59-
}
60-
61-
public static SpiIocTransfer createEmpty() {
62-
return new SpiIocTransfer(new byte[]{}, new byte[]{}, 0, 0, (short) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0);
63-
}
64-
6548
@Override
6649
public MemoryLayout getMemoryLayout() {
6750
return LAYOUT;
@@ -70,9 +53,13 @@ public MemoryLayout getMemoryLayout() {
7053
@Override
7154
@SuppressWarnings("unchecked")
7255
public SpiIocTransfer from(MemorySegment buffer) throws Throwable {
56+
return from(buffer, null, null);
57+
}
58+
59+
public SpiIocTransfer from(MemorySegment buffer, MemorySegment txBuf, MemorySegment rxBuf) throws Throwable {
7360
return new SpiIocTransfer(
74-
txBuf1 != null ? txBuf1.toArray(ValueLayout.JAVA_BYTE) : new byte[]{},
75-
rxBuf1 != null ? rxBuf1.toArray(ValueLayout.JAVA_BYTE) : new byte[]{},
61+
txBuf != null ? txBuf.toArray(ValueLayout.JAVA_BYTE) : new byte[]{},
62+
rxBuf != null ? rxBuf.toArray(ValueLayout.JAVA_BYTE) : new byte[]{},
7663
(int) VH_LEN.get(buffer, 0L),
7764
(int) VH_SPEED_HZ.get(buffer, 0L),
7865
(short) VH_DELAY_USECS.get(buffer, 0L),
@@ -84,19 +71,8 @@ public SpiIocTransfer from(MemorySegment buffer) throws Throwable {
8471
(byte) VH_PAD.get(buffer, 0L));
8572
}
8673

87-
public static MemorySegment txBuf1;
88-
public static MemorySegment rxBuf1;
89-
9074
@Override
9175
public void to(MemorySegment buffer) throws Throwable {
92-
if (txBuf != null) {
93-
txBuf1 = Arena.global().allocateFrom(ValueLayout.JAVA_BYTE, txBuf);
94-
VH_TX_BUF.set(buffer, 0L, txBuf1.address());
95-
}
96-
if (rxBuf != null) {
97-
rxBuf1 = Arena.global().allocateFrom(ValueLayout.JAVA_BYTE, rxBuf);
98-
VH_RX_BUF.set(buffer, 0L, rxBuf1.address());
99-
}
10076
VH_LEN.set(buffer, 0L, length);
10177
VH_SPEED_HZ.set(buffer, 0L, speedHz);
10278
VH_DELAY_USECS.set(buffer, 0L, delayUsecs);
@@ -108,6 +84,16 @@ public void to(MemorySegment buffer) throws Throwable {
10884
VH_PAD.set(buffer, 0L, pad);
10985
}
11086

87+
public void to(MemorySegment buffer, long txAddress, long rxAddress) throws Throwable {
88+
if (txBuf != null) {
89+
VH_TX_BUF.set(buffer, 0L, txAddress);
90+
}
91+
if (rxBuf != null) {
92+
VH_RX_BUF.set(buffer, 0L, rxAddress);
93+
}
94+
to(buffer);
95+
}
96+
11197

11298
@Override
11399
public String toString() {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.pi4j.plugin.ffm.common.spi;
2+
3+
import com.pi4j.plugin.ffm.common.Pi4JLayout;
4+
5+
import java.lang.foreign.Arena;
6+
import java.lang.foreign.MemoryLayout;
7+
import java.lang.foreign.MemorySegment;
8+
import java.lang.foreign.ValueLayout;
9+
10+
public final class SpiTransferBuffer implements Pi4JLayout {
11+
private static final Arena ARENA = Arena.ofConfined();
12+
private SpiIocTransfer spiIocTransfer;
13+
private MemorySegment txBuf;
14+
private MemorySegment rxBuf;
15+
16+
public SpiTransferBuffer(byte[] txBuf, byte[] rxBuf, int length, int speedHz, short delayUsecs, byte bitsPerWord,
17+
byte csChange, byte txNbits, byte rxNbits, byte wordDelayUsecs,
18+
byte pad) {
19+
this.spiIocTransfer = new SpiIocTransfer(txBuf, rxBuf, length, speedHz, delayUsecs, bitsPerWord, csChange, txNbits, rxNbits, wordDelayUsecs, pad);
20+
if (txBuf != null) {
21+
this.txBuf = ARENA.allocateFrom(ValueLayout.JAVA_BYTE, txBuf);
22+
}
23+
if (rxBuf != null) {
24+
this.rxBuf = ARENA.allocateFrom(ValueLayout.JAVA_BYTE, rxBuf);
25+
}
26+
}
27+
28+
public SpiTransferBuffer(byte[] txBuf, byte[] rxBuf, int length) {
29+
this(txBuf, rxBuf, length, 0, (short) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0);
30+
}
31+
32+
33+
@Override
34+
public MemoryLayout getMemoryLayout() {
35+
return spiIocTransfer.getMemoryLayout();
36+
}
37+
38+
@Override
39+
@SuppressWarnings("unchecked")
40+
public SpiTransferBuffer from(MemorySegment buffer) throws Throwable {
41+
this.spiIocTransfer = spiIocTransfer.from(buffer, txBuf, rxBuf);
42+
return this;
43+
}
44+
45+
@Override
46+
public void to(MemorySegment buffer) throws Throwable {
47+
var txAddress = txBuf != null ? txBuf.address() : 0;
48+
var rxAddress = rxBuf != null ? rxBuf.address() : 0;
49+
spiIocTransfer.to(buffer, txAddress, rxAddress);
50+
}
51+
52+
public byte[] getTxBuffer() {
53+
return spiIocTransfer.txBuf();
54+
}
55+
56+
public byte[] getRxBuffer() {
57+
return spiIocTransfer.rxBuf();
58+
}
59+
}

plugins/pi4j-plugin-ffm/src/main/java/com/pi4j/plugin/ffm/providers/spi/SpiFFM.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import com.pi4j.plugin.ffm.common.file.FileFlag;
1111
import com.pi4j.plugin.ffm.common.ioctl.Command;
1212
import com.pi4j.plugin.ffm.common.ioctl.IoctlNative;
13-
import com.pi4j.plugin.ffm.common.spi.SpiIocTransfer;
13+
import com.pi4j.plugin.ffm.common.spi.SpiTransferBuffer;
1414
import org.slf4j.Logger;
1515
import org.slf4j.LoggerFactory;
1616

@@ -59,22 +59,23 @@ public Spi initialize(Context context) throws InitializeException {
5959
IOCTL.call(spiFileDescriptor, Command.getSpiIocRdLsbFirst(), config.getReadLsbFirst());
6060

6161
this.isOpen = true;
62+
logger.info("{} - SPI Bus configured.", path);
6263
return this;
6364
}
6465

65-
@Override
66-
public void close() {
67-
super.close();
68-
}
69-
7066
@Override
7167
public int transfer(byte[] write, int writeOffset, byte[] read, int readOffset, int numberOfBytes) {
72-
var spiTransfer = new SpiIocTransfer(write, read, numberOfBytes);
68+
checkClosed();
69+
logger.trace("{} - Transferring data (length '{}')", path, numberOfBytes);
70+
logger.trace("{} - Write buffer: {}", path, write);
71+
logger.trace("{} - Read buffer: {}", path, read);
72+
var spiTransfer = new SpiTransferBuffer(write, read, numberOfBytes);
7373
spiTransfer = IOCTL.call(spiFileDescriptor, Command.getSpiIocMessage(1), spiTransfer);
74+
var readBytes = spiTransfer.getRxBuffer();
7475
if (read != null) {
75-
ByteBuffer.wrap(read).put(spiTransfer.rxBuf());
76+
ByteBuffer.wrap(read).put(readBytes);
7677
}
77-
return spiTransfer.rxBuf().length;
78+
return readBytes.length;
7879
}
7980

8081
@Override
@@ -84,8 +85,6 @@ public int write(byte data) {
8485

8586
@Override
8687
public int write(byte[] data, int offset, int length) {
87-
checkClosed();
88-
logger.trace("{} - writing data {}.", path, data);
8988
return transfer(data, offset, null, 0, length);
9089
}
9190

@@ -96,8 +95,6 @@ public int read() {
9695

9796
@Override
9897
public int read(byte[] buffer, int offset, int length) {
99-
checkClosed();
100-
logger.trace("{} - reading data {}.", path, buffer);
10198
return transfer(null, 0, buffer, offset, length);
10299
}
103100

@@ -106,7 +103,7 @@ public int read(byte[] buffer, int offset, int length) {
106103
*/
107104
private void checkClosed() {
108105
if (!isOpen) {
109-
throw new RuntimeException("SPI bus " + path + " is closed");
106+
throw new RuntimeException("SPI bus '" + path + "' is closed");
110107
}
111108
}
112109
}

plugins/pi4j-plugin-ffm/src/main/java/com/pi4j/plugin/ffm/providers/spi/SpiFFMProviderImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
public class SpiFFMProviderImpl extends SpiProviderBase implements SpiProvider {
99

1010
public SpiFFMProviderImpl() {
11-
this.id = "FFMSpiProviderImpl";
12-
this.name = "FFMSpiProviderImpl";
11+
this.id = "SpiFFMProviderImpl";
12+
this.name = "SpiFFMProviderImpl";
1313
}
1414

1515
@Override
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#!/bin/sh
22

3-
pwd
43
modprobe spidev
5-
#insmod "$(pwd)"/src/test/resources/spi-mock.ko
6-
insmod spi-mock.ko
4+
insmod "$(pwd)"/src/test/resources/spi-mock.ko
5+
#insmod spi-mock.ko
76
echo 'spidev' | sudo tee /sys/bus/spi/devices/spi0.0/driver_override
87
echo 'spi0.0' | sudo tee /sys/bus/spi/drivers/spidev/bind
98
chmod 0666 /dev/spidev0.0

0 commit comments

Comments
 (0)