WPILibC++  2020.3.2
SPI.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <stdint.h>
11 
12 #include <memory>
13 
14 #include <hal/SPITypes.h>
15 #include <units/units.h>
16 #include <wpi/ArrayRef.h>
17 #include <wpi/deprecated.h>
18 
19 #include "frc/ErrorBase.h"
20 
21 namespace frc {
22 
23 class DigitalSource;
24 
32 class SPI : public ErrorBase {
33  public:
34  enum Port { kOnboardCS0 = 0, kOnboardCS1, kOnboardCS2, kOnboardCS3, kMXP };
35 
41  explicit SPI(Port port);
42 
43  ~SPI() override;
44 
45  SPI(SPI&&) = default;
46  SPI& operator=(SPI&&) = default;
47 
56  void SetClockRate(int hz);
57 
62  void SetMSBFirst();
63 
68  void SetLSBFirst();
69 
75 
81 
86  WPI_DEPRECATED("Use SetSampleDataOnTrailingEdge in most cases.")
88 
93  WPI_DEPRECATED("Use SetSampleDataOnLeadingEdge in most cases")
94  void SetSampleDataOnRising();
95 
100  void SetClockActiveLow();
101 
106  void SetClockActiveHigh();
107 
112 
116  void SetChipSelectActiveLow();
117 
125  virtual int Write(uint8_t* data, int size);
126 
140  virtual int Read(bool initiate, uint8_t* dataReceived, int size);
141 
149  virtual int Transaction(uint8_t* dataToSend, uint8_t* dataReceived, int size);
150 
159  void InitAuto(int bufferSize);
160 
164  void FreeAuto();
165 
175  void SetAutoTransmitData(wpi::ArrayRef<uint8_t> dataToSend, int zeroSize);
176 
185  void StartAutoRate(units::second_t period);
186 
195  WPI_DEPRECATED("Use StartAutoRate with unit-safety instead")
196  void StartAutoRate(double period);
197 
208  void StartAutoTrigger(DigitalSource& source, bool rising, bool falling);
209 
213  void StopAuto();
214 
218  void ForceAutoRead();
219 
239  int ReadAutoReceivedData(uint32_t* buffer, int numToRead,
240  units::second_t timeout);
241 
261  WPI_DEPRECATED("Use ReadAutoReceivedData with unit-safety instead")
262  int ReadAutoReceivedData(uint32_t* buffer, int numToRead, double timeout);
263 
270  int GetAutoDroppedCount();
271 
282  void ConfigureAutoStall(HAL_SPIPort port, int csToSclkTicks, int stallTicks,
283  int pow2BytesPerRead);
284 
300  void InitAccumulator(units::second_t period, int cmd, int xferSize,
301  int validMask, int validValue, int dataShift,
302  int dataSize, bool isSigned, bool bigEndian);
303 
319  WPI_DEPRECATED("Use InitAccumulator with unit-safety instead")
320  void InitAccumulator(double period, int cmd, int xferSize, int validMask,
321  int validValue, int dataShift, int dataSize,
322  bool isSigned, bool bigEndian);
323 
327  void FreeAccumulator();
328 
332  void ResetAccumulator();
333 
342  void SetAccumulatorCenter(int center);
343 
347  void SetAccumulatorDeadband(int deadband);
348 
352  int GetAccumulatorLastValue() const;
353 
359  int64_t GetAccumulatorValue() const;
360 
369  int64_t GetAccumulatorCount() const;
370 
376  double GetAccumulatorAverage() const;
377 
387  void GetAccumulatorOutput(int64_t& value, int64_t& count) const;
388 
396  void SetAccumulatorIntegratedCenter(double center);
397 
404  double GetAccumulatorIntegratedValue() const;
405 
413  double GetAccumulatorIntegratedAverage() const;
414 
415  protected:
416  hal::SPIPort m_port;
417  bool m_msbFirst = false; // Default little-endian
418  bool m_sampleOnTrailing = false; // Default data updated on falling edge
419  bool m_clockIdleHigh = false; // Default clock active high
420 
421  private:
422  void Init();
423 
424  class Accumulator;
425  std::unique_ptr<Accumulator> m_accum;
426 };
427 
428 } // namespace frc
frc::SPI::SetLSBFirst
void SetLSBFirst()
Configure the order that bits are sent and received on the wire to be least significant bit first.
frc::SPI::ForceAutoRead
void ForceAutoRead()
Force the engine to make a single transfer.
frc::SPI::SetSampleDataOnTrailingEdge
void SetSampleDataOnTrailingEdge()
Configure that the data is stable on the trailing edge and the data changes on the leading edge.
frc::SPI::SetAccumulatorIntegratedCenter
void SetAccumulatorIntegratedCenter(double center)
Set the center value of the accumulator integrator.
wpi::ArrayRef< uint8_t >
frc::SPI::SetSampleDataOnRising
void SetSampleDataOnRising()
Configure that the data is stable on the rising edge and the data changes on the falling edge.
frc::SPI::SetClockRate
void SetClockRate(int hz)
Configure the rate of the generated clock signal.
frc::SPI::FreeAccumulator
void FreeAccumulator()
Frees the accumulator.
frc::SPI::SetChipSelectActiveHigh
void SetChipSelectActiveHigh()
Configure the chip select line to be active high.
frc::SPI::GetAccumulatorValue
int64_t GetAccumulatorValue() const
Read the accumulated value.
frc::SPI::SetAccumulatorCenter
void SetAccumulatorCenter(int center)
Set the center value of the accumulator.
frc::SPI::SetSampleDataOnLeadingEdge
void SetSampleDataOnLeadingEdge()
Configure that the data is stable on the leading edge and the data changes on the trailing edge.
frc::SPI::SetAutoTransmitData
void SetAutoTransmitData(wpi::ArrayRef< uint8_t > dataToSend, int zeroSize)
Set the data to be transmitted by the engine.
frc::SPI::SetClockActiveLow
void SetClockActiveLow()
Configure the clock output line to be active low.
frc::SPI::StartAutoTrigger
void StartAutoTrigger(DigitalSource &source, bool rising, bool falling)
Start running the automatic SPI transfer engine when a trigger occurs.
frc::SPI::InitAuto
void InitAuto(int bufferSize)
Initialize automatic SPI transfer engine.
frc::SPI::Write
virtual int Write(uint8_t *data, int size)
Write data to the slave device.
frc::SPI::ResetAccumulator
void ResetAccumulator()
Resets the accumulator to zero.
frc::SPI::StartAutoRate
void StartAutoRate(units::second_t period)
Start running the automatic SPI transfer engine at a periodic rate.
frc::SPI::SetClockActiveHigh
void SetClockActiveHigh()
Configure the clock output line to be active high.
frc::SPI
SPI bus interface class.
Definition: SPI.h:32
frc::SPI::InitAccumulator
void InitAccumulator(units::second_t period, int cmd, int xferSize, int validMask, int validValue, int dataShift, int dataSize, bool isSigned, bool bigEndian)
Initialize the accumulator.
frc::ErrorBase
Base class for most objects.
Definition: ErrorBase.h:104
frc::SPI::Read
virtual int Read(bool initiate, uint8_t *dataReceived, int size)
Read a word from the receive FIFO.
frc::SPI::SetChipSelectActiveLow
void SetChipSelectActiveLow()
Configure the chip select line to be active low.
frc::SPI::SPI
SPI(Port port)
Constructor.
frc::SPI::GetAccumulatorCount
int64_t GetAccumulatorCount() const
Read the number of accumulated values.
frc::SPI::GetAccumulatorIntegratedAverage
double GetAccumulatorIntegratedAverage() const
Read the average of the integrated value.
frc::SPI::SetSampleDataOnFalling
void SetSampleDataOnFalling()
Configure that the data is stable on the falling edge and the data changes on the rising edge.
frc::SPI::StopAuto
void StopAuto()
Stop running the automatic SPI transfer engine.
frc::SPI::GetAccumulatorOutput
void GetAccumulatorOutput(int64_t &value, int64_t &count) const
Read the accumulated value and the number of accumulated values atomically.
frc::SPI::GetAccumulatorAverage
double GetAccumulatorAverage() const
Read the average of the accumulated value.
frc::SPI::ConfigureAutoStall
void ConfigureAutoStall(HAL_SPIPort port, int csToSclkTicks, int stallTicks, int pow2BytesPerRead)
Configure the Auto SPI Stall time between reads.
frc
A class that enforces constraints on the differential drive kinematics.
Definition: SPIAccelerometerSim.h:16
frc::SPI::FreeAuto
void FreeAuto()
Frees the automatic SPI transfer engine.
frc::SPI::Transaction
virtual int Transaction(uint8_t *dataToSend, uint8_t *dataReceived, int size)
Perform a simultaneous read/write transaction with the device.
frc::SPI::ReadAutoReceivedData
int ReadAutoReceivedData(uint32_t *buffer, int numToRead, units::second_t timeout)
Read data that has been transferred by the automatic SPI transfer engine.
frc::SPI::GetAccumulatorIntegratedValue
double GetAccumulatorIntegratedValue() const
Read the integrated value.
frc::SPI::GetAccumulatorLastValue
int GetAccumulatorLastValue() const
Read the last value read by the accumulator engine.
frc::DigitalSource
DigitalSource Interface.
Definition: DigitalSource.h:25
frc::SPI::SetMSBFirst
void SetMSBFirst()
Configure the order that bits are sent and received on the wire to be most significant bit first.
frc::SPI::GetAutoDroppedCount
int GetAutoDroppedCount()
Get the number of bytes dropped by the automatic SPI transfer engine due to the receive buffer being ...
frc::SPI::SetAccumulatorDeadband
void SetAccumulatorDeadband(int deadband)
Set the accumulator's deadband.