From bdc1cab013fa5c4633c95fee9aaf6badbac8e4ac Mon Sep 17 00:00:00 2001 From: Thad House Date: Tue, 24 Dec 2019 10:52:58 -0800 Subject: [PATCH] Add support for configuring SPI Auto Stall Config (#2193) --- .../main/java/edu/wpi/first/hal/SPIJNI.java | 4 +++- hal/src/main/native/athena/SPI.cpp | 21 +++++++++++++------ hal/src/main/native/cpp/jni/SPIJNI.cpp | 16 ++++++++++++++ hal/src/main/native/include/hal/SPI.h | 15 ++++++++++++- hal/src/main/native/sim/SPI.cpp | 10 ++++++++- wpilibc/src/main/native/cpp/SPI.cpp | 8 +++++++ wpilibc/src/main/native/include/frc/SPI.h | 13 ++++++++++++ .../main/java/edu/wpi/first/wpilibj/SPI.java | 11 ++++++++++ 8 files changed, 89 insertions(+), 9 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 c473181ad7..c203213df5 100644 --- a/hal/src/main/java/edu/wpi/first/hal/SPIJNI.java +++ b/hal/src/main/java/edu/wpi/first/hal/SPIJNI.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2016-2018 FIRST. All Rights Reserved. */ +/* Copyright (c) 2016-2019 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -61,4 +61,6 @@ public class SPIJNI extends JNIWrapper { double timeout); public static native int spiGetAutoDroppedCount(int port); + + public static native void spiConfigureAutoStall(int port, int csToSclkTicks, int stallTicks, int pow2BytesPerRead); } diff --git a/hal/src/main/native/athena/SPI.cpp b/hal/src/main/native/athena/SPI.cpp index 80cbf09bbe..37c5f0ed08 100644 --- a/hal/src/main/native/athena/SPI.cpp +++ b/hal/src/main/native/athena/SPI.cpp @@ -631,12 +631,21 @@ int32_t HAL_GetSPIAutoDroppedCount(HAL_SPIPort port, int32_t* status) { return spiSystem->readTransferSkippedFullCount(status); } -// These 2 functions are so the new stall functionality -// can be tested. How they're used is not very clear -// but I want them to be testable so we can add an impl. -// We will not be including these in the headers -void* HAL_GetSPIDMAManager() { return spiAutoDMA.get(); } +void HAL_ConfigureSPIAutoStall(HAL_SPIPort port, int32_t csToSclkTicks, + int32_t stallTicks, int32_t pow2BytesPerRead, + int32_t* status) { + std::scoped_lock lock(spiAutoMutex); + // FPGA only has one auto SPI engine + if (port != spiAutoPort) { + *status = INCOMPATIBLE_STATE; + return; + } -void* HAL_GetSPISystem() { return spiSystem.get(); } + tSPI::tStallConfig stallConfig; + stallConfig.CsToSclkTicks = static_cast(csToSclkTicks); + stallConfig.StallTicks = static_cast(stallTicks); + stallConfig.Pow2BytesPerRead = static_cast(pow2BytesPerRead); + spiSystem->writeStallConfig(stallConfig, status); +} } // extern "C" diff --git a/hal/src/main/native/cpp/jni/SPIJNI.cpp b/hal/src/main/native/cpp/jni/SPIJNI.cpp index 27078fd855..7962e214e0 100644 --- a/hal/src/main/native/cpp/jni/SPIJNI.cpp +++ b/hal/src/main/native/cpp/jni/SPIJNI.cpp @@ -394,4 +394,20 @@ Java_edu_wpi_first_hal_SPIJNI_spiGetAutoDroppedCount return retval; } +/* + * Class: edu_wpi_first_hal_SPIJNI + * Method: spiConfigureAutoStall + * Signature: (IIII)V + */ +JNIEXPORT void JNICALL +Java_edu_wpi_first_hal_SPIJNI_spiConfigureAutoStall + (JNIEnv* env, jclass, jint port, jint csToSclkTicks, jint stallTicks, + jint pow2BytesPerRead) +{ + int32_t status = 0; + HAL_ConfigureSPIAutoStall(static_cast(port), csToSclkTicks, + stallTicks, pow2BytesPerRead, &status); + CheckStatus(env, status); +} + } // extern "C" diff --git a/hal/src/main/native/include/hal/SPI.h b/hal/src/main/native/include/hal/SPI.h index 4f1815f1bb..abee379ac9 100644 --- a/hal/src/main/native/include/hal/SPI.h +++ b/hal/src/main/native/include/hal/SPI.h @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2016-2018 FIRST. All Rights Reserved. */ +/* Copyright (c) 2016-2019 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -244,6 +244,19 @@ int32_t HAL_ReadSPIAutoReceivedData(HAL_SPIPort port, uint32_t* buffer, */ int32_t HAL_GetSPIAutoDroppedCount(HAL_SPIPort port, int32_t* status); +/** + * Configure the Auto SPI Stall time between reads. + * + * @param port The number of the port to use. 0-3 for Onboard CS0-CS2, 4 for + * MXP. + * @param csToSclkTicks the number of ticks to wait before asserting the cs pin + * @param stallTicks the number of ticks to stall for + * @param pow2BytesPerRead the number of bytes to read before stalling + */ +void HAL_ConfigureSPIAutoStall(HAL_SPIPort port, int32_t csToSclkTicks, + int32_t stallTicks, int32_t pow2BytesPerRead, + int32_t* status); + #ifdef __cplusplus } // extern "C" #endif diff --git a/hal/src/main/native/sim/SPI.cpp b/hal/src/main/native/sim/SPI.cpp index 8c539d3ec0..1c90a98389 100644 --- a/hal/src/main/native/sim/SPI.cpp +++ b/hal/src/main/native/sim/SPI.cpp @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */ +/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -18,6 +18,8 @@ void InitializeSPI() {} } // namespace init } // namespace hal +extern "C" { + void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) { hal::init::CheckInit(); SimSPIData[port].initialized = true; @@ -63,3 +65,9 @@ int32_t HAL_ReadSPIAutoReceivedData(HAL_SPIPort port, uint32_t* buffer, int32_t HAL_GetSPIAutoDroppedCount(HAL_SPIPort port, int32_t* status) { return 0; } + +void HAL_ConfigureSPIAutoStall(HAL_SPIPort port, int32_t csToSclkTicks, + int32_t stallTicks, int32_t pow2BytesPerRead, + int32_t* status) {} + +} // extern "C" diff --git a/wpilibc/src/main/native/cpp/SPI.cpp b/wpilibc/src/main/native/cpp/SPI.cpp index 074cc79432..d51fa3bc86 100644 --- a/wpilibc/src/main/native/cpp/SPI.cpp +++ b/wpilibc/src/main/native/cpp/SPI.cpp @@ -311,6 +311,14 @@ int SPI::GetAutoDroppedCount() { return val; } +void SPI::ConfigureAutoStall(HAL_SPIPort port, int csToSclkTicks, + int stallTicks, int pow2BytesPerRead) { + int32_t status = 0; + HAL_ConfigureSPIAutoStall(m_port, csToSclkTicks, stallTicks, pow2BytesPerRead, + &status); + wpi_setHALError(status); +} + void SPI::InitAccumulator(units::second_t period, int cmd, int xferSize, int validMask, int validValue, int dataShift, int dataSize, bool isSigned, bool bigEndian) { diff --git a/wpilibc/src/main/native/include/frc/SPI.h b/wpilibc/src/main/native/include/frc/SPI.h index fb4835e941..8e721bc7c5 100644 --- a/wpilibc/src/main/native/include/frc/SPI.h +++ b/wpilibc/src/main/native/include/frc/SPI.h @@ -269,6 +269,19 @@ class SPI : public ErrorBase { */ int GetAutoDroppedCount(); + /** + * Configure the Auto SPI Stall time between reads. + * + * @param port The number of the port to use. 0-3 for Onboard CS0-CS2, 4 for + * MXP. + * @param csToSclkTicks the number of ticks to wait before asserting the cs + * pin + * @param stallTicks the number of ticks to stall for + * @param pow2BytesPerRead the number of bytes to read before stalling + */ + void ConfigureAutoStall(HAL_SPIPort port, int csToSclkTicks, int stallTicks, + int pow2BytesPerRead); + /** * Initialize the accumulator. * 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 97d9b27fa2..aca2cbeacf 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/SPI.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/SPI.java @@ -424,6 +424,17 @@ public class SPI implements AutoCloseable { return SPIJNI.spiGetAutoDroppedCount(m_port); } + /** + * Configure the Auto SPI Stall time between reads. + * + * @param csToSclkTicks the number of ticks to wait before asserting the cs pin + * @param stallTicks the number of ticks to stall for + * @param pow2BytesPerRead the number of bytes to read before stalling + */ + public void configureAutoStall(int csToSclkTicks, int stallTicks, int pow2BytesPerRead) { + SPIJNI.spiConfigureAutoStall(m_port, csToSclkTicks, stallTicks, pow2BytesPerRead); + } + private static final int kAccumulateDepth = 2048; @SuppressWarnings("PMD.TooManyFields")