WPILibC++  2019.1.1-2-g444b899
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
SPI.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2018 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 <wpi/ArrayRef.h>
16 #include <wpi/deprecated.h>
17 
18 #include "frc/ErrorBase.h"
19 
20 namespace frc {
21 
22 class DigitalSource;
23 
31 class SPI : public ErrorBase {
32  public:
33  enum Port { kOnboardCS0 = 0, kOnboardCS1, kOnboardCS2, kOnboardCS3, kMXP };
34 
40  explicit SPI(Port port);
41 
42  ~SPI() override;
43 
44  SPI(SPI&& rhs);
45  SPI& operator=(SPI&& rhs);
46 
55  void SetClockRate(double hz);
56 
61  void SetMSBFirst();
62 
67  void SetLSBFirst();
68 
74 
80 
85  WPI_DEPRECATED("Use SetSampleDataOnTrailingEdge in most cases.")
87 
92  WPI_DEPRECATED("Use SetSampleDataOnLeadingEdge in most cases")
93  void SetSampleDataOnRising();
94 
99  void SetClockActiveLow();
100 
105  void SetClockActiveHigh();
106 
111 
115  void SetChipSelectActiveLow();
116 
124  virtual int Write(uint8_t* data, int size);
125 
139  virtual int Read(bool initiate, uint8_t* dataReceived, int size);
140 
148  virtual int Transaction(uint8_t* dataToSend, uint8_t* dataReceived, int size);
149 
158  void InitAuto(int bufferSize);
159 
163  void FreeAuto();
164 
174  void SetAutoTransmitData(wpi::ArrayRef<uint8_t> dataToSend, int zeroSize);
175 
184  void StartAutoRate(double period);
185 
196  void StartAutoTrigger(DigitalSource& source, bool rising, bool falling);
197 
201  void StopAuto();
202 
206  void ForceAutoRead();
207 
227  int ReadAutoReceivedData(uint32_t* buffer, int numToRead, double timeout);
228 
235  int GetAutoDroppedCount();
236 
252  void InitAccumulator(double period, int cmd, int xferSize, int validMask,
253  int validValue, int dataShift, int dataSize,
254  bool isSigned, bool bigEndian);
255 
259  void FreeAccumulator();
260 
264  void ResetAccumulator();
265 
274  void SetAccumulatorCenter(int center);
275 
279  void SetAccumulatorDeadband(int deadband);
280 
284  int GetAccumulatorLastValue() const;
285 
291  int64_t GetAccumulatorValue() const;
292 
301  int64_t GetAccumulatorCount() const;
302 
308  double GetAccumulatorAverage() const;
309 
319  void GetAccumulatorOutput(int64_t& value, int64_t& count) const;
320 
328  void SetAccumulatorIntegratedCenter(double center);
329 
336  double GetAccumulatorIntegratedValue() const;
337 
345  double GetAccumulatorIntegratedAverage() const;
346 
347  protected:
348  HAL_SPIPort m_port = HAL_SPI_kInvalid;
349  bool m_msbFirst = false; // Default little-endian
350  bool m_sampleOnTrailing = false; // Default data updated on falling edge
351  bool m_clockIdleHigh = false; // Default clock active high
352 
353  private:
354  void Init();
355 
356  class Accumulator;
357  std::unique_ptr<Accumulator> m_accum;
358 };
359 
360 } // namespace frc
WPILib FRC namespace.
Definition: SPIAccelerometerSim.h:18
void SetSampleDataOnLeadingEdge()
Configure that the data is stable on the leading edge and the data changes on the trailing edge...
double GetAccumulatorIntegratedValue() const
Read the integrated value.
void SetChipSelectActiveHigh()
Configure the chip select line to be active high.
void SetLSBFirst()
Configure the order that bits are sent and received on the wire to be least significant bit first...
void SetAccumulatorDeadband(int deadband)
Set the accumulator's deadband.
virtual int Read(bool initiate, uint8_t *dataReceived, int size)
Read a word from the receive FIFO.
void SetMSBFirst()
Configure the order that bits are sent and received on the wire to be most significant bit first...
int GetAutoDroppedCount()
Get the number of bytes dropped by the automatic SPI transfer engine due to the receive buffer being ...
double GetAccumulatorIntegratedAverage() const
Read the average of the integrated value.
void StartAutoRate(double period)
Start running the automatic SPI transfer engine at a periodic rate.
void ResetAccumulator()
Resets the accumulator to zero.
int64_t GetAccumulatorValue() const
Read the accumulated value.
void SetAccumulatorCenter(int center)
Set the center value of the accumulator.
void SetClockActiveLow()
Configure the clock output line to be active low.
void SetAutoTransmitData(wpi::ArrayRef< uint8_t > dataToSend, int zeroSize)
Set the data to be transmitted by the engine.
SPI bus interface class.
Definition: SPI.h:31
void StartAutoTrigger(DigitalSource &source, bool rising, bool falling)
Start running the automatic SPI transfer engine when a trigger occurs.
double GetAccumulatorAverage() const
Read the average of the accumulated value.
void SetChipSelectActiveLow()
Configure the chip select line to be active low.
void SetClockActiveHigh()
Configure the clock output line to be active high.
void InitAccumulator(double period, int cmd, int xferSize, int validMask, int validValue, int dataShift, int dataSize, bool isSigned, bool bigEndian)
Initialize the accumulator.
void SetSampleDataOnRising()
Configure that the data is stable on the rising edge and the data changes on the falling edge...
int64_t GetAccumulatorCount() const
Read the number of accumulated values.
virtual int Write(uint8_t *data, int size)
Write data to the slave device.
DigitalSource Interface.
Definition: DigitalSource.h:25
void InitAuto(int bufferSize)
Initialize automatic SPI transfer engine.
void FreeAccumulator()
Frees the accumulator.
void StopAuto()
Stop running the automatic SPI transfer engine.
SPI(Port port)
Constructor.
int ReadAutoReceivedData(uint32_t *buffer, int numToRead, double timeout)
Read data that has been transferred by the automatic SPI transfer engine.
Base class for most objects.
Definition: ErrorBase.h:74
void GetAccumulatorOutput(int64_t &value, int64_t &count) const
Read the accumulated value and the number of accumulated values atomically.
void FreeAuto()
Frees the automatic SPI transfer engine.
void SetSampleDataOnTrailingEdge()
Configure that the data is stable on the trailing edge and the data changes on the leading edge...
void SetAccumulatorIntegratedCenter(double center)
Set the center value of the accumulator integrator.
virtual int Transaction(uint8_t *dataToSend, uint8_t *dataReceived, int size)
Perform a simultaneous read/write transaction with the device.
void SetClockRate(double hz)
Configure the rate of the generated clock signal.
void SetSampleDataOnFalling()
Configure that the data is stable on the falling edge and the data changes on the rising edge...
int GetAccumulatorLastValue() const
Read the last value read by the accumulator engine.
void ForceAutoRead()
Force the engine to make a single transfer.