Skip to content

Commit

Permalink
[hal] SPI: Remove byte limit on size in Java API (#7774)
Browse files Browse the repository at this point in the history
The underlying Linux spidev supports up to page size length.
  • Loading branch information
PeterJohnson authored Feb 10, 2025
1 parent b60b2b6 commit d2611d4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
20 changes: 10 additions & 10 deletions hal/src/main/java/edu/wpi/first/hal/SPIJNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
24 changes: 12 additions & 12 deletions hal/src/main/native/cpp/jni/SPIJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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");
Expand All @@ -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) {
Expand All @@ -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<HAL_SPIPort>(port),
reinterpret_cast<const uint8_t*>(
Expand All @@ -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");
Expand All @@ -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");
Expand Down
12 changes: 6 additions & 6 deletions wpilibj/src/main/java/edu/wpi/first/wpilibj/SPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit d2611d4

Please sign in to comment.