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

Utilize SPI Buffer size for large writes #384

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Arrays;
import java.util.Objects;
import java.util.Scanner;

import static com.pi4j.library.pigpio.PiGpioConst.PI_IF_DISABLE_FIFO;
import static com.pi4j.library.pigpio.PiGpioConst.PI_IF_DISABLE_SOCK;
Expand All @@ -49,6 +51,8 @@
public class PiGpioNativeImpl extends PiGpioBase implements PiGpio {
protected Logger logger = LoggerFactory.getLogger(this.getClass());

private int SPI_BUFFSIZ = 4096;

private static final PiGpioNativeImpl instance;
static {
instance = new PiGpioNativeImpl();
Expand Down Expand Up @@ -105,6 +109,16 @@ public int gpioInitialise() {
result = PIGPIO.gpioInitialise();
validateResult(result);

try {
Scanner scanner = new Scanner(new File("/sys/module/spidev/parameters/bufsiz"));
if (scanner.hasNextInt()) {
SPI_BUFFSIZ = scanner.nextInt();
}
logger.trace("[INITIALIZE] -> SPI_BUFFSIZ={}", SPI_BUFFSIZ);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}

// initialization successful
this.initialized = true;
logger.debug("[INITIALIZE] -- INITIALIZED SUCCESSFULLY");
Expand Down Expand Up @@ -1454,8 +1468,16 @@ public int spiWrite(int handle, byte[] data, int offset, int length) {
Objects.checkFromIndexSize(offset, length, data.length);
validateHandle(handle);
// write data array to SPI bus/channel
int result = PIGPIO.spiWrite(handle, data, offset, length);
logger.trace("[SPI::WRITE] <- HANDLE={}; SUCCESS={}", handle, (result>=0));
int result = 0;
byte[] someData = Arrays.copyOfRange(data, offset, length);
int start = 0;
while (start < someData.length) {
int end = Math.min(someData.length, start + SPI_BUFFSIZ);
byte[] chunk = Arrays.copyOfRange(someData, start, end);
result += PIGPIO.spiWrite(handle, chunk, 0, chunk.length );
logger.trace("[SPI::WRITE] <- HANDLE={}; SUCCESS={}", handle, (result>=0));
start += SPI_BUFFSIZ;
}
validateResult(result, false);
return result;
}
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,10 @@
<maven-antrun-plugin.version>3.1.0</maven-antrun-plugin.version>
<maven-assembly-plugin.version>3.3.0</maven-assembly-plugin.version>
<maven-bundle-plugin.version>5.1.2</maven-bundle-plugin.version>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<maven-compiler-plugin.version>3.13.0</maven-compiler-plugin.version>
<maven-dependency-plugin.version>3.1.2</maven-dependency-plugin.version>
<maven-gpg-plugin.version>3.2.1</maven-gpg-plugin.version>
<maven-jar-plugin.version>3.2.0</maven-jar-plugin.version>
<maven-jar-plugin.version>3.4.2</maven-jar-plugin.version>
<maven-javadoc-plugin.version>3.2.0</maven-javadoc-plugin.version>
<maven-release-plugin.version>3.0.0-M4</maven-release-plugin.version>
<maven-resource-plugin.version>3.2.0</maven-resource-plugin.version>
Expand Down Expand Up @@ -668,7 +668,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M2</version>
<version>3.5.0</version>
</plugin>

<!-- Deploy to Maven plugin -->
Expand Down