From d2611d4ad568ca5c56e6b8d35812c4793705d93d Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sun, 9 Feb 2025 23:01:01 -0800 Subject: [PATCH] [hal] SPI: Remove byte limit on size in Java API (#7774) The underlying Linux spidev supports up to page size length. --- .../main/java/edu/wpi/first/hal/SPIJNI.java | 20 ++++++++-------- hal/src/main/native/cpp/jni/SPIJNI.cpp | 24 +++++++++---------- .../main/java/edu/wpi/first/wpilibj/SPI.java | 12 +++++----- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/hal/src/main/java/edu/wpi/first/hal/SPIJNI.java b/hal/src/main/java/edu/wpi/first/hal/SPIJNI.java index 242053ec382..b164e993edc 100644 --- a/hal/src/main/java/edu/wpi/first/hal/SPIJNI.java +++ b/hal/src/main/java/edu/wpi/first/hal/SPIJNI.java @@ -61,12 +61,12 @@ public class SPIJNI extends JNIWrapper { * @param port The number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP * @param dataToSend Buffer of data to send as part of the transaction. * @param dataReceived Buffer to read data into. - * @param size Number of bytes to transfer. [0..7] + * @param size Number of bytes to transfer. * @return Number of bytes transferred, -1 for error * @see "HAL_TransactionSPI" */ public static native int spiTransaction( - int port, ByteBuffer dataToSend, ByteBuffer dataReceived, byte size); + int port, ByteBuffer dataToSend, ByteBuffer dataReceived, int size); /** * Performs an SPI send/receive transaction. @@ -77,12 +77,12 @@ public static native int spiTransaction( * @param port The number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP * @param dataToSend Buffer of data to send as part of the transaction. * @param dataReceived Buffer to read data into. - * @param size Number of bytes to transfer. [0..7] + * @param size Number of bytes to transfer. * @return Number of bytes transferred, -1 for error * @see "HAL_TransactionSPI" */ public static native int spiTransactionB( - int port, byte[] dataToSend, byte[] dataReceived, byte size); + int port, byte[] dataToSend, byte[] dataReceived, int size); /** * Executes a write transaction with the device. @@ -95,7 +95,7 @@ public static native int spiTransactionB( * @return The number of bytes written. -1 for an error * @see "HAL_WriteSPI" */ - public static native int spiWrite(int port, ByteBuffer dataToSend, byte sendSize); + public static native int spiWrite(int port, ByteBuffer dataToSend, int sendSize); /** * Executes a write transaction with the device. @@ -108,7 +108,7 @@ public static native int spiTransactionB( * @return The number of bytes written. -1 for an error * @see "HAL_WriteSPI" */ - public static native int spiWriteB(int port, byte[] dataToSend, byte sendSize); + public static native int spiWriteB(int port, byte[] dataToSend, int sendSize); /** * Executes a read from the device. @@ -121,11 +121,11 @@ public static native int spiTransactionB( * @param port The number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP * @param initiate initiates a transaction when true. Just reads when false. * @param dataReceived A pointer to the array of bytes to store the data read from the device. - * @param size The number of bytes to read in the transaction. [1..7] + * @param size The number of bytes to read in the transaction. * @return Number of bytes read. -1 for error. * @see "HAL_ReadSPI" */ - public static native int spiRead(int port, boolean initiate, ByteBuffer dataReceived, byte size); + public static native int spiRead(int port, boolean initiate, ByteBuffer dataReceived, int size); /** * Executes a read from the device. @@ -138,11 +138,11 @@ public static native int spiTransactionB( * @param port The number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP * @param initiate initiates a transaction when true. Just reads when false. * @param dataReceived A pointer to the array of bytes to store the data read from the device. - * @param size The number of bytes to read in the transaction. [1..7] + * @param size The number of bytes to read in the transaction. * @return Number of bytes read. -1 for error. * @see "HAL_ReadSPI" */ - public static native int spiReadB(int port, boolean initiate, byte[] dataReceived, byte size); + public static native int spiReadB(int port, boolean initiate, byte[] dataReceived, int size); /** * Closes the SPI port. diff --git a/hal/src/main/native/cpp/jni/SPIJNI.cpp b/hal/src/main/native/cpp/jni/SPIJNI.cpp index 0940de6da73..93e353250d4 100644 --- a/hal/src/main/native/cpp/jni/SPIJNI.cpp +++ b/hal/src/main/native/cpp/jni/SPIJNI.cpp @@ -55,12 +55,12 @@ Java_edu_wpi_first_hal_SPIJNI_spiInitialize /* * Class: edu_wpi_first_hal_SPIJNI * Method: spiTransaction - * Signature: (ILjava/lang/Object;Ljava/lang/Object;B)I + * Signature: (ILjava/lang/Object;Ljava/lang/Object;I)I */ JNIEXPORT jint JNICALL Java_edu_wpi_first_hal_SPIJNI_spiTransaction (JNIEnv* env, jclass, jint port, jobject dataToSend, jobject dataReceived, - jbyte size) + jint size) { uint8_t* dataToSendPtr = nullptr; if (dataToSend != nullptr) { @@ -77,12 +77,12 @@ Java_edu_wpi_first_hal_SPIJNI_spiTransaction /* * Class: edu_wpi_first_hal_SPIJNI * Method: spiTransactionB - * Signature: (I[B[BB)I + * Signature: (I[B[BI)I */ JNIEXPORT jint JNICALL Java_edu_wpi_first_hal_SPIJNI_spiTransactionB (JNIEnv* env, jclass, jint port, jbyteArray dataToSend, - jbyteArray dataReceived, jbyte size) + jbyteArray dataReceived, jint size) { if (size < 0) { ThrowIllegalArgumentException(env, "SPIJNI.spiTransactionB() size < 0"); @@ -104,11 +104,11 @@ Java_edu_wpi_first_hal_SPIJNI_spiTransactionB /* * Class: edu_wpi_first_hal_SPIJNI * Method: spiWrite - * Signature: (ILjava/lang/Object;B)I + * Signature: (ILjava/lang/Object;I)I */ JNIEXPORT jint JNICALL Java_edu_wpi_first_hal_SPIJNI_spiWrite - (JNIEnv* env, jclass, jint port, jobject dataToSend, jbyte size) + (JNIEnv* env, jclass, jint port, jobject dataToSend, jint size) { uint8_t* dataToSendPtr = nullptr; if (dataToSend != nullptr) { @@ -123,11 +123,11 @@ Java_edu_wpi_first_hal_SPIJNI_spiWrite /* * Class: edu_wpi_first_hal_SPIJNI * Method: spiWriteB - * Signature: (I[BB)I + * Signature: (I[BI)I */ JNIEXPORT jint JNICALL Java_edu_wpi_first_hal_SPIJNI_spiWriteB - (JNIEnv* env, jclass, jint port, jbyteArray dataToSend, jbyte size) + (JNIEnv* env, jclass, jint port, jbyteArray dataToSend, jint size) { jint retVal = HAL_WriteSPI(static_cast(port), reinterpret_cast( @@ -139,12 +139,12 @@ Java_edu_wpi_first_hal_SPIJNI_spiWriteB /* * Class: edu_wpi_first_hal_SPIJNI * Method: spiRead - * Signature: (IZLjava/lang/Object;B)I + * Signature: (IZLjava/lang/Object;I)I */ JNIEXPORT jint JNICALL Java_edu_wpi_first_hal_SPIJNI_spiRead (JNIEnv* env, jclass, jint port, jboolean initiate, jobject dataReceived, - jbyte size) + jint size) { if (size < 0) { ThrowIllegalArgumentException(env, "SPIJNI.spiRead() size < 0"); @@ -169,12 +169,12 @@ Java_edu_wpi_first_hal_SPIJNI_spiRead /* * Class: edu_wpi_first_hal_SPIJNI * Method: spiReadB - * Signature: (IZ[BB)I + * Signature: (IZ[BI)I */ JNIEXPORT jint JNICALL Java_edu_wpi_first_hal_SPIJNI_spiReadB (JNIEnv* env, jclass, jint port, jboolean initiate, jbyteArray dataReceived, - jbyte size) + jint size) { if (size < 0) { ThrowIllegalArgumentException(env, "SPIJNI.spiReadB() size < 0"); diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/SPI.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/SPI.java index 9278fd81703..dd5d6a34630 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/SPI.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/SPI.java @@ -140,7 +140,7 @@ public int write(byte[] dataToSend, int size) { if (dataToSend.length < size) { throw new IllegalArgumentException("buffer is too small, must be at least " + size); } - return SPIJNI.spiWriteB(m_port, dataToSend, (byte) size); + return SPIJNI.spiWriteB(m_port, dataToSend, size); } /** @@ -163,7 +163,7 @@ public int write(ByteBuffer dataToSend, int size) { if (dataToSend.capacity() < size) { throw new IllegalArgumentException("buffer is too small, must be at least " + size); } - return SPIJNI.spiWrite(m_port, dataToSend, (byte) size); + return SPIJNI.spiWrite(m_port, dataToSend, size); } /** @@ -184,7 +184,7 @@ public int read(boolean initiate, byte[] dataReceived, int size) { if (dataReceived.length < size) { throw new IllegalArgumentException("buffer is too small, must be at least " + size); } - return SPIJNI.spiReadB(m_port, initiate, dataReceived, (byte) size); + return SPIJNI.spiReadB(m_port, initiate, dataReceived, size); } /** @@ -211,7 +211,7 @@ public int read(boolean initiate, ByteBuffer dataReceived, int size) { if (dataReceived.capacity() < size) { throw new IllegalArgumentException("buffer is too small, must be at least " + size); } - return SPIJNI.spiRead(m_port, initiate, dataReceived, (byte) size); + return SPIJNI.spiRead(m_port, initiate, dataReceived, size); } /** @@ -229,7 +229,7 @@ public int transaction(byte[] dataToSend, byte[] dataReceived, int size) { if (dataReceived.length < size) { throw new IllegalArgumentException("dataReceived is too small, must be at least " + size); } - return SPIJNI.spiTransactionB(m_port, dataToSend, dataReceived, (byte) size); + return SPIJNI.spiTransactionB(m_port, dataToSend, dataReceived, size); } /** @@ -256,7 +256,7 @@ public int transaction(ByteBuffer dataToSend, ByteBuffer dataReceived, int size) if (dataReceived.capacity() < size) { throw new IllegalArgumentException("dataReceived is too small, must be at least " + size); } - return SPIJNI.spiTransaction(m_port, dataToSend, dataReceived, (byte) size); + return SPIJNI.spiTransaction(m_port, dataToSend, dataReceived, size); } /**