[hal,wpilib] Fix SPI Mode Setting (#4434)

SPI Mode setting was very broken. MSB and LSB sets did not work (MSB is the only one supported)
and if LSB was set (which was the default) the ioct to set clock phase would fail. This
deprecates all the individual functions, the LSB/MSB functions, and adds an SPI mode selection
function. This is usually more understandable, and shows up in a lot more documentation
This commit is contained in:
Thad House
2022-09-24 00:11:37 -07:00
committed by GitHub
parent 773198537c
commit 17f504f548
19 changed files with 243 additions and 95 deletions

View File

@@ -10,6 +10,7 @@
#include <hal/SPITypes.h>
#include <units/time.h>
#include <wpi/deprecated.h>
#include <wpi/span.h>
namespace frc {
@@ -26,6 +27,12 @@ class DigitalSource;
class SPI {
public:
enum Port { kOnboardCS0 = 0, kOnboardCS1, kOnboardCS2, kOnboardCS3, kMXP };
enum Mode {
kMode0 = HAL_SPI_kMode0,
kMode1 = HAL_SPI_kMode1,
kMode2 = HAL_SPI_kMode2,
kMode3 = HAL_SPI_kMode3
};
/**
* Constructor
@@ -54,39 +61,72 @@ class SPI {
/**
* Configure the order that bits are sent and received on the wire
* to be most significant bit first.
*
* @deprecated Does not work, will be removed.
*/
WPI_DEPRECATED("Not supported by roboRIO.")
void SetMSBFirst();
/**
* Configure the order that bits are sent and received on the wire
* to be least significant bit first.
*
* @deprecated Does not work, will be removed.
*/
WPI_DEPRECATED("Not supported by roboRIO.")
void SetLSBFirst();
/**
* Configure that the data is stable on the leading edge and the data
* changes on the trailing edge.
*
* @deprecated Use SetMode() instead.
*/
WPI_DEPRECATED("Use SetMode() instead")
void SetSampleDataOnLeadingEdge();
/**
* Configure that the data is stable on the trailing edge and the data
* changes on the leading edge.
*
* @deprecated Use SetMode() instead.
*/
WPI_DEPRECATED("Use SetMode() instead")
void SetSampleDataOnTrailingEdge();
/**
* Configure the clock output line to be active low.
* This is sometimes called clock polarity high or clock idle high.
*
* @deprecated Use SetMode() instead.
*/
WPI_DEPRECATED("Use SetMode() instead")
void SetClockActiveLow();
/**
* Configure the clock output line to be active high.
* This is sometimes called clock polarity low or clock idle low.
*
* @deprecated Use SetMode() instead.
*/
WPI_DEPRECATED("Use SetMode() instead")
void SetClockActiveHigh();
/**
* Sets the mode for the SPI device.
*
* <p>Mode 0 is Clock idle low, data sampled on rising edge
*
* <p>Mode 1 is Clock idle low, data sampled on falling edge
*
* <p>Mode 2 is Clock idle high, data sampled on falling edge
*
* <p>Mode 3 is Clock idle high, data sampled on rising edge
*
* @param mode The mode to set.
*/
void SetMode(Mode mode);
/**
* Configure the chip select line to be active high.
*/
@@ -345,9 +385,7 @@ class SPI {
protected:
hal::SPIPort m_port;
bool m_msbFirst = false; // Default little-endian
bool m_sampleOnTrailing = false; // Default data updated on falling edge
bool m_clockIdleHigh = false; // Default clock active high
HAL_SPIMode m_mode = HAL_SPIMode::HAL_SPI_kMode0;
private:
void Init();