WPILibC++
2019.1.1-beta-2-18-ga769d56
|
SPI bus interface class. More...
#include <SPI.h>
Public Types | |
enum | Port { kOnboardCS0 = 0, kOnboardCS1, kOnboardCS2, kOnboardCS3, kMXP } |
Public Member Functions | |
SPI (Port port) | |
Constructor. More... | |
SPI (SPI &&rhs) | |
SPI & | operator= (SPI &&rhs) |
void | SetClockRate (double hz) |
Configure the rate of the generated clock signal. More... | |
void | SetMSBFirst () |
Configure the order that bits are sent and received on the wire to be most significant bit first. | |
void | SetLSBFirst () |
Configure the order that bits are sent and received on the wire to be least significant bit first. | |
void | SetSampleDataOnLeadingEdge () |
Configure that the data is stable on the leading edge and the data changes on the trailing edge. | |
void | SetSampleDataOnTrailingEdge () |
Configure that the data is stable on the trailing edge and the data changes on the leading edge. | |
void | SetSampleDataOnFalling () |
Configure that the data is stable on the falling edge and the data changes on the rising edge. | |
void | SetSampleDataOnRising () |
Configure that the data is stable on the rising edge and the data changes on the falling edge. | |
void | SetClockActiveLow () |
Configure the clock output line to be active low. More... | |
void | SetClockActiveHigh () |
Configure the clock output line to be active high. More... | |
void | SetChipSelectActiveHigh () |
Configure the chip select line to be active high. | |
void | SetChipSelectActiveLow () |
Configure the chip select line to be active low. | |
virtual int | Write (uint8_t *data, int size) |
Write data to the slave device. More... | |
virtual int | Read (bool initiate, uint8_t *dataReceived, int size) |
Read a word from the receive FIFO. More... | |
virtual int | Transaction (uint8_t *dataToSend, uint8_t *dataReceived, int size) |
Perform a simultaneous read/write transaction with the device. More... | |
void | InitAuto (int bufferSize) |
Initialize automatic SPI transfer engine. More... | |
void | FreeAuto () |
Frees the automatic SPI transfer engine. | |
void | SetAutoTransmitData (wpi::ArrayRef< uint8_t > dataToSend, int zeroSize) |
Set the data to be transmitted by the engine. More... | |
void | StartAutoRate (double period) |
Start running the automatic SPI transfer engine at a periodic rate. More... | |
void | StartAutoTrigger (DigitalSource &source, bool rising, bool falling) |
Start running the automatic SPI transfer engine when a trigger occurs. More... | |
void | StopAuto () |
Stop running the automatic SPI transfer engine. | |
void | ForceAutoRead () |
Force the engine to make a single transfer. | |
int | ReadAutoReceivedData (uint8_t *buffer, int numToRead, double timeout) |
Read data that has been transferred by the automatic SPI transfer engine. More... | |
int | GetAutoDroppedCount () |
Get the number of bytes dropped by the automatic SPI transfer engine due to the receive buffer being full. More... | |
void | InitAccumulator (double period, int cmd, int xferSize, int validMask, int validValue, int dataShift, int dataSize, bool isSigned, bool bigEndian) |
Initialize the accumulator. More... | |
void | FreeAccumulator () |
Frees the accumulator. | |
void | ResetAccumulator () |
Resets the accumulator to zero. | |
void | SetAccumulatorCenter (int center) |
Set the center value of the accumulator. More... | |
void | SetAccumulatorDeadband (int deadband) |
Set the accumulator's deadband. | |
int | GetAccumulatorLastValue () const |
Read the last value read by the accumulator engine. | |
int64_t | GetAccumulatorValue () const |
Read the accumulated value. More... | |
int64_t | GetAccumulatorCount () const |
Read the number of accumulated values. More... | |
double | GetAccumulatorAverage () const |
Read the average of the accumulated value. More... | |
void | GetAccumulatorOutput (int64_t &value, int64_t &count) const |
Read the accumulated value and the number of accumulated values atomically. More... | |
![]() | |
ErrorBase (ErrorBase &&)=default | |
ErrorBase & | operator= (ErrorBase &&)=default |
virtual Error & | GetError () |
Retrieve the current error. More... | |
virtual const Error & | GetError () const |
Retrieve the current error. More... | |
virtual void | ClearError () const |
Clear the current error information associated with this sensor. | |
virtual void | SetErrnoError (const wpi::Twine &contextMessage, wpi::StringRef filename, wpi::StringRef function, int lineNumber) const |
Set error information associated with a C library call that set an error to the "errno" global variable. More... | |
virtual void | SetImaqError (int success, const wpi::Twine &contextMessage, wpi::StringRef filename, wpi::StringRef function, int lineNumber) const |
Set the current error information associated from the nivision Imaq API. More... | |
virtual void | SetError (Error::Code code, const wpi::Twine &contextMessage, wpi::StringRef filename, wpi::StringRef function, int lineNumber) const |
Set the current error information associated with this sensor. More... | |
virtual void | SetErrorRange (Error::Code code, int32_t minRange, int32_t maxRange, int32_t requestedValue, const wpi::Twine &contextMessage, wpi::StringRef filename, wpi::StringRef function, int lineNumber) const |
Set the current error information associated with this sensor. More... | |
virtual void | SetWPIError (const wpi::Twine &errorMessage, Error::Code code, const wpi::Twine &contextMessage, wpi::StringRef filename, wpi::StringRef function, int lineNumber) const |
Set the current error information associated with this sensor. More... | |
virtual void | CloneError (const ErrorBase &rhs) const |
virtual bool | StatusIsFatal () const |
Check if the current error code represents a fatal error. More... | |
Protected Attributes | |
HAL_SPIPort | m_port = HAL_SPI_kInvalid |
bool | m_msbFirst = false |
bool | m_sampleOnTrailing = false |
bool | m_clockIdleHigh = false |
![]() | |
Error | m_error |
Additional Inherited Members | |
![]() | |
static void | SetGlobalError (Error::Code code, const wpi::Twine &contextMessage, wpi::StringRef filename, wpi::StringRef function, int lineNumber) |
static void | SetGlobalWPIError (const wpi::Twine &errorMessage, const wpi::Twine &contextMessage, wpi::StringRef filename, wpi::StringRef function, int lineNumber) |
static const Error & | GetGlobalError () |
Retrieve the current global error. | |
SPI bus interface class.
This class is intended to be used by sensor (and other SPI device) drivers. It probably should not be used directly.
|
explicit |
Constructor.
port | the physical SPI port |
double frc::SPI::GetAccumulatorAverage | ( | ) | const |
Read the average of the accumulated value.
int64_t frc::SPI::GetAccumulatorCount | ( | ) | const |
Read the number of accumulated values.
Read the count of the accumulated values since the accumulator was last Reset().
void frc::SPI::GetAccumulatorOutput | ( | int64_t & | value, |
int64_t & | count | ||
) | const |
Read the accumulated value and the number of accumulated values atomically.
This function reads the value and count atomically. This can be used for averaging.
value | Pointer to the 64-bit accumulated output. |
count | Pointer to the number of accumulation cycles. |
int64_t frc::SPI::GetAccumulatorValue | ( | ) | const |
Read the accumulated value.
int frc::SPI::GetAutoDroppedCount | ( | ) |
Get the number of bytes dropped by the automatic SPI transfer engine due to the receive buffer being full.
void frc::SPI::InitAccumulator | ( | double | period, |
int | cmd, | ||
int | xferSize, | ||
int | validMask, | ||
int | validValue, | ||
int | dataShift, | ||
int | dataSize, | ||
bool | isSigned, | ||
bool | bigEndian | ||
) |
Initialize the accumulator.
period | Time between reads |
cmd | SPI command to send to request data |
xferSize | SPI transfer size, in bytes |
validMask | Mask to apply to received data for validity checking |
validData | After valid_mask is applied, required matching value for validity checking |
dataShift | Bit shift to apply to received data to get actual data value |
dataSize | Size (in bits) of data field |
isSigned | Is data field signed? |
bigEndian | Is device big endian? |
void frc::SPI::InitAuto | ( | int | bufferSize | ) |
|
virtual |
Read a word from the receive FIFO.
Waits for the current transfer to complete if the receive FIFO is empty.
If the receive FIFO is empty, there is no active transfer, and initiate is false, errors.
initiate | If true, this function pushes "0" into the transmit buffer and initiates a transfer. If false, this function assumes that data is already in the receive FIFO from a previous write. |
int frc::SPI::ReadAutoReceivedData | ( | uint8_t * | buffer, |
int | numToRead, | ||
double | timeout | ||
) |
Read data that has been transferred by the automatic SPI transfer engine.
Transfers may be made a byte at a time, so it's necessary for the caller to handle cases where an entire transfer has not been completed.
Blocks until numToRead bytes have been read or timeout expires. May be called with numToRead=0 to retrieve how many bytes are available.
buffer | buffer where read bytes are stored |
numToRead | number of bytes to read |
timeout | timeout in seconds (ms resolution) |
void frc::SPI::SetAccumulatorCenter | ( | int | center | ) |
Set the center value of the accumulator.
The center value is subtracted from each value before it is added to the accumulator. This is used for the center value of devices like gyros and accelerometers to make integration work and to take the device offset into account when integrating.
void frc::SPI::SetAutoTransmitData | ( | wpi::ArrayRef< uint8_t > | dataToSend, |
int | zeroSize | ||
) |
Set the data to be transmitted by the engine.
Up to 16 bytes are configurable, and may be followed by up to 127 zero bytes.
dataToSend | data to send (maximum 16 bytes) |
zeroSize | number of zeros to send after the data |
void frc::SPI::SetClockActiveHigh | ( | ) |
Configure the clock output line to be active high.
This is sometimes called clock polarity low or clock idle low.
void frc::SPI::SetClockActiveLow | ( | ) |
Configure the clock output line to be active low.
This is sometimes called clock polarity high or clock idle high.
void frc::SPI::SetClockRate | ( | double | hz | ) |
Configure the rate of the generated clock signal.
The default value is 500,000Hz. The maximum value is 4,000,000Hz.
hz | The clock rate in Hertz. |
void frc::SPI::StartAutoRate | ( | double | period | ) |
Start running the automatic SPI transfer engine at a periodic rate.
InitAuto() and SetAutoTransmitData() must be called before calling this function.
period | period between transfers, in seconds (us resolution) |
void frc::SPI::StartAutoTrigger | ( | DigitalSource & | source, |
bool | rising, | ||
bool | falling | ||
) |
Start running the automatic SPI transfer engine when a trigger occurs.
InitAuto() and SetAutoTransmitData() must be called before calling this function.
source | digital source for the trigger (may be an analog trigger) |
rising | trigger on the rising edge |
falling | trigger on the falling edge |
|
virtual |
Perform a simultaneous read/write transaction with the device.
dataToSend | The data to be written out to the device |
dataReceived | Buffer to receive data from the device |
size | The length of the transaction, in bytes |
|
virtual |
Write data to the slave device.
Blocks until there is space in the output FIFO.
If not running in output only mode, also saves the data received on the MISO input during the transfer into the receive FIFO.