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 <llvm/ArrayRef.h>
15 
16 #include "ErrorBase.h"
17 
18 enum HAL_SPIPort : int32_t;
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 
35  explicit SPI(Port port);
36  ~SPI() override;
37 
38  SPI(const SPI&) = delete;
39  SPI& operator=(const SPI&) = delete;
40 
41  void SetClockRate(double hz);
42 
43  void SetMSBFirst();
44  void SetLSBFirst();
45 
47  void SetSampleDataOnRising();
48 
49  void SetClockActiveLow();
50  void SetClockActiveHigh();
51 
54 
55  virtual int Write(uint8_t* data, int size);
56  virtual int Read(bool initiate, uint8_t* dataReceived, int size);
57  virtual int Transaction(uint8_t* dataToSend, uint8_t* dataReceived, int size);
58 
59  void InitAuto(int bufferSize);
60  void FreeAuto();
61  void SetAutoTransmitData(llvm::ArrayRef<uint8_t> dataToSend, int zeroSize);
62  void StartAutoRate(double period);
63  void StartAutoTrigger(DigitalSource& source, bool rising, bool falling);
64  void StopAuto();
65  void ForceAutoRead();
66  int ReadAutoReceivedData(uint8_t* buffer, int numToRead, double timeout);
67  int GetAutoDroppedCount();
68 
69  void InitAccumulator(double period, int cmd, int xferSize, int validMask,
70  int validValue, int dataShift, int dataSize,
71  bool isSigned, bool bigEndian);
72  void FreeAccumulator();
73  void ResetAccumulator();
74  void SetAccumulatorCenter(int center);
75  void SetAccumulatorDeadband(int deadband);
76  int GetAccumulatorLastValue() const;
77  int64_t GetAccumulatorValue() const;
78  int64_t GetAccumulatorCount() const;
79  double GetAccumulatorAverage() const;
80  void GetAccumulatorOutput(int64_t& value, int64_t& count) const;
81 
82  protected:
83  HAL_SPIPort m_port;
84  bool m_msbFirst = false; // Default little-endian
85  bool m_sampleOnTrailing = false; // Default data updated on falling edge
86  bool m_clk_idle_high = false; // Default clock active high
87 
88  private:
89  void Init();
90 
91  class Accumulator;
92  std::unique_ptr<Accumulator> m_accum;
93 };
94 
95 } // namespace frc
Definition: RobotController.cpp:14
void SetAccumulatorCenter(int center)
Set the center value of the accumulator.
Definition: SPI.cpp:477
void SetAccumulatorDeadband(int deadband)
Set the accumulator&#39;s deadband.
Definition: SPI.cpp:486
void GetAccumulatorOutput(int64_t &value, int64_t &count) const
Read the accumulated value and the number of accumulated values atomically.
Definition: SPI.cpp:551
void SetAutoTransmitData(llvm::ArrayRef< uint8_t > dataToSend, int zeroSize)
Set the data to be transmitted by the engine.
Definition: SPI.cpp:314
double GetAccumulatorAverage() const
Read the average of the accumulated value.
Definition: SPI.cpp:534
void SetMSBFirst()
Configure the order that bits are sent and received on the wire to be most significant bit first...
Definition: SPI.cpp:163
void FreeAccumulator()
Frees the accumulator.
Definition: SPI.cpp:453
virtual int Write(uint8_t *data, int size)
Write data to the slave device.
Definition: SPI.cpp:238
int64_t GetAccumulatorValue() const
Read the accumulated value.
Definition: SPI.cpp:507
void StartAutoRate(double period)
Start running the automatic SPI transfer engine at a periodic rate.
Definition: SPI.cpp:330
void SetChipSelectActiveLow()
Configure the chip select line to be active low.
Definition: SPI.cpp:225
void SetChipSelectActiveHigh()
Configure the chip select line to be active high.
Definition: SPI.cpp:216
void StartAutoTrigger(DigitalSource &source, bool rising, bool falling)
Start running the automatic SPI transfer engine when a trigger occurs.
Definition: SPI.cpp:346
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:32
SPI bus interface class.
Definition: SPI.h:31
void StopAuto()
Stop running the automatic SPI transfer engine.
Definition: SPI.cpp:358
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:387
void FreeAuto()
Frees the automatic SPI transfer engine.
Definition: SPI.cpp:299
void ResetAccumulator()
Resets the accumulator to zero.
Definition: SPI.cpp:461
void ForceAutoRead()
Force the engine to make a single transfer.
Definition: SPI.cpp:367
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:190
virtual int Read(bool initiate, uint8_t *dataReceived, int size)
Read a word from the receive FIFO.
Definition: SPI.cpp:257
Base class for most objects.
Definition: ErrorBase.h:74
void SetClockActiveHigh()
Configure the clock output line to be active high.
Definition: SPI.cpp:208
int GetAutoDroppedCount()
Get the number of bytes dropped by the automatic SPI transfer engine due to the receive buffer being ...
Definition: SPI.cpp:401
void SetLSBFirst()
Configure the order that bits are sent and received on the wire to be least significant bit first...
Definition: SPI.cpp:172
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:423
virtual int Transaction(uint8_t *dataToSend, uint8_t *dataReceived, int size)
Perform a simultaneous read/write transaction with the device.
Definition: SPI.cpp:276
void SetClockRate(double hz)
Configure the rate of the generated clock signal.
Definition: SPI.cpp:157
void InitAuto(int bufferSize)
Initialize automatic SPI transfer engine.
Definition: SPI.cpp:290
int GetAccumulatorLastValue() const
Read the last value read by the accumulator engine.
Definition: SPI.cpp:495
int64_t GetAccumulatorCount() const
Read the number of accumulated values.
Definition: SPI.cpp:522
void SetClockActiveLow()
Configure the clock output line to be active low.
Definition: SPI.cpp:199
~SPI() override
Destructor.
Definition: SPI.cpp:147
SPI(Port port)
Constructor.
Definition: SPI.cpp:134
void SetSampleDataOnFalling()
Configure that the data is stable on the falling edge and the data changes on the rising edge...
Definition: SPI.cpp:181