WPILibC++  unspecified
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 <wpi/ArrayRef.h>
15 #include <wpi/deprecated.h>
16 
17 #include "ErrorBase.h"
18 
19 enum HAL_SPIPort : int32_t;
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(const SPI&) = delete;
46  SPI& operator=(const SPI&) = delete;
47 
56  void SetClockRate(double 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(double period);
186 
197  void StartAutoTrigger(DigitalSource& source, bool rising, bool falling);
198 
202  void StopAuto();
203 
207  void ForceAutoRead();
208 
223  int ReadAutoReceivedData(uint8_t* buffer, int numToRead, double timeout);
224 
231  int GetAutoDroppedCount();
232 
248  void InitAccumulator(double period, int cmd, int xferSize, int validMask,
249  int validValue, int dataShift, int dataSize,
250  bool isSigned, bool bigEndian);
251 
255  void FreeAccumulator();
256 
260  void ResetAccumulator();
261 
270  void SetAccumulatorCenter(int center);
271 
275  void SetAccumulatorDeadband(int deadband);
276 
280  int GetAccumulatorLastValue() const;
281 
287  int64_t GetAccumulatorValue() const;
288 
297  int64_t GetAccumulatorCount() const;
298 
304  double GetAccumulatorAverage() const;
305 
315  void GetAccumulatorOutput(int64_t& value, int64_t& count) const;
316 
317  protected:
318  HAL_SPIPort m_port;
319  bool m_msbFirst = false; // Default little-endian
320  bool m_sampleOnTrailing = false; // Default data updated on falling edge
321  bool m_clk_idle_high = false; // Default clock active high
322 
323  private:
324  void Init();
325 
326  class Accumulator;
327  std::unique_ptr<Accumulator> m_accum;
328 };
329 
330 } // namespace frc
Definition: Utility.cpp:119
void SetAccumulatorCenter(int center)
Set the center value of the accumulator.
Definition: SPI.cpp:320
void SetAccumulatorDeadband(int deadband)
Set the accumulator&#39;s deadband.
Definition: SPI.cpp:326
void GetAccumulatorOutput(int64_t &value, int64_t &count) const
Read the accumulated value and the number of accumulated values atomically.
Definition: SPI.cpp:361
double GetAccumulatorAverage() const
Read the average of the accumulated value.
Definition: SPI.cpp:353
void SetMSBFirst()
Configure the order that bits are sent and received on the wire to be most significant bit first...
Definition: SPI.cpp:143
void SetSampleDataOnLeadingEdge()
Configure that the data is stable on the leading edge and the data changes on the trailing edge...
Definition: SPI.cpp:153
void FreeAccumulator()
Frees the accumulator.
Definition: SPI.cpp:307
virtual int Write(uint8_t *data, int size)
Write data to the slave device.
Definition: SPI.cpp:195
int64_t GetAccumulatorValue() const
Read the accumulated value.
Definition: SPI.cpp:339
void StartAutoRate(double period)
Start running the automatic SPI transfer engine at a periodic rate.
Definition: SPI.cpp:238
void SetChipSelectActiveLow()
Configure the chip select line to be active low.
Definition: SPI.cpp:189
void SetChipSelectActiveHigh()
Configure the chip select line to be active high.
Definition: SPI.cpp:183
void StartAutoTrigger(DigitalSource &source, bool rising, bool falling)
Start running the automatic SPI transfer engine when a trigger occurs.
Definition: SPI.cpp:244
SPI bus interface class.
Definition: SPI.h:32
void StopAuto()
Stop running the automatic SPI transfer engine.
Definition: SPI.cpp:253
Definition: SPI.cpp:25
int ReadAutoReceivedData(uint8_t *buffer, int numToRead, double timeout)
Read data that has been transferred by the automatic SPI transfer engine.
Definition: SPI.cpp:265
void SetAutoTransmitData(wpi::ArrayRef< uint8_t > dataToSend, int zeroSize)
Set the data to be transmitted by the engine.
Definition: SPI.cpp:231
void SetSampleDataOnTrailingEdge()
Configure that the data is stable on the trailing edge and the data changes on the leading edge...
Definition: SPI.cpp:158
void FreeAuto()
Frees the automatic SPI transfer engine.
Definition: SPI.cpp:225
void ResetAccumulator()
Resets the accumulator to zero.
Definition: SPI.cpp:312
void ForceAutoRead()
Force the engine to make a single transfer.
Definition: SPI.cpp:259
DigitalSource Interface.
Definition: DigitalSource.h:25
void SetSampleDataOnRising()
Configure that the data is stable on the rising edge and the data changes on the falling edge...
Definition: SPI.cpp:168
virtual int Read(bool initiate, uint8_t *dataReceived, int size)
Read a word from the receive FIFO.
Definition: SPI.cpp:201
Base class for most objects.
Definition: ErrorBase.h:74
void SetClockActiveHigh()
Configure the clock output line to be active high.
Definition: SPI.cpp:178
int GetAutoDroppedCount()
Get the number of bytes dropped by the automatic SPI transfer engine due to the receive buffer being ...
Definition: SPI.cpp:273
void SetLSBFirst()
Configure the order that bits are sent and received on the wire to be least significant bit first...
Definition: SPI.cpp:148
void InitAccumulator(double period, int cmd, int xferSize, int validMask, int validValue, int dataShift, int dataSize, bool isSigned, bool bigEndian)
Initialize the accumulator.
Definition: SPI.cpp:280
virtual int Transaction(uint8_t *dataToSend, uint8_t *dataReceived, int size)
Perform a simultaneous read/write transaction with the device.
Definition: SPI.cpp:213
void SetClockRate(double hz)
Configure the rate of the generated clock signal.
Definition: SPI.cpp:141
void InitAuto(int bufferSize)
Initialize automatic SPI transfer engine.
Definition: SPI.cpp:219
int GetAccumulatorLastValue() const
Read the last value read by the accumulator engine.
Definition: SPI.cpp:332
int64_t GetAccumulatorCount() const
Read the number of accumulated values.
Definition: SPI.cpp:346
void SetClockActiveLow()
Configure the clock output line to be active low.
Definition: SPI.cpp:173
SPI(Port port)
Constructor.
Definition: SPI.cpp:129
void SetSampleDataOnFalling()
Configure that the data is stable on the falling edge and the data changes on the rising edge...
Definition: SPI.cpp:163