WPILibC++  unspecified
SPI.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2017 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 "SensorBase.h"
13 
14 enum HAL_SPIPort : int32_t;
15 
16 namespace frc {
17 
18 class DigitalOutput;
19 class DigitalInput;
20 
28 class SPI : public SensorBase {
29  public:
30  enum Port { kOnboardCS0 = 0, kOnboardCS1, kOnboardCS2, kOnboardCS3, kMXP };
31  explicit SPI(Port port);
32  virtual ~SPI();
33 
34  SPI(const SPI&) = delete;
35  SPI& operator=(const SPI&) = delete;
36 
37  void SetClockRate(double hz);
38 
39  void SetMSBFirst();
40  void SetLSBFirst();
41 
43  void SetSampleDataOnRising();
44 
45  void SetClockActiveLow();
46  void SetClockActiveHigh();
47 
50 
51  virtual int Write(uint8_t* data, int size);
52  virtual int Read(bool initiate, uint8_t* dataReceived, int size);
53  virtual int Transaction(uint8_t* dataToSend, uint8_t* dataReceived, int size);
54 
55  void InitAccumulator(double period, int cmd, int xfer_size, int valid_mask,
56  int valid_value, int data_shift, int data_size,
57  bool is_signed, bool big_endian);
58  void FreeAccumulator();
59  void ResetAccumulator();
60  void SetAccumulatorCenter(int center);
61  void SetAccumulatorDeadband(int deadband);
62  int GetAccumulatorLastValue() const;
63  int64_t GetAccumulatorValue() const;
64  int64_t GetAccumulatorCount() const;
65  double GetAccumulatorAverage() const;
66  void GetAccumulatorOutput(int64_t& value, int64_t& count) const;
67 
68  protected:
69  HAL_SPIPort m_port;
70  bool m_msbFirst = false; // default little-endian
71  bool m_sampleOnTrailing = false; // default data updated on falling edge
72  bool m_clk_idle_high = false; // default clock active high
73 
74  private:
75  void Init();
76 };
77 
78 } // namespace frc
Definition: Timer.cpp:18
void SetAccumulatorCenter(int center)
Set the center value of the accumulator.
Definition: SPI.cpp:224
void SetAccumulatorDeadband(int deadband)
Set the accumulator&#39;s deadband.
Definition: SPI.cpp:233
Base class for all sensors.
Definition: SensorBase.h:20
void GetAccumulatorOutput(int64_t &value, int64_t &count) const
Read the accumulated value and the number of accumulated values atomically.
Definition: SPI.cpp:297
void InitAccumulator(double period, int cmd, int xfer_size, int valid_mask, int valid_value, int data_shift, int data_size, bool is_signed, bool big_endian)
Initialize the accumulator.
Definition: SPI.cpp:188
double GetAccumulatorAverage() const
Read the average of the accumulated value.
Definition: SPI.cpp:281
void SetMSBFirst()
Configure the order that bits are sent and received on the wire to be most significant bit first...
Definition: SPI.cpp:54
void FreeAccumulator()
Frees the accumulator.
Definition: SPI.cpp:201
virtual int Write(uint8_t *data, int size)
Write data to the slave device.
Definition: SPI.cpp:129
int64_t GetAccumulatorValue() const
Read the accumulated value.
Definition: SPI.cpp:254
void SetChipSelectActiveLow()
Configure the chip select line to be active low.
Definition: SPI.cpp:116
void SetChipSelectActiveHigh()
Configure the chip select line to be active high.
Definition: SPI.cpp:107
virtual ~SPI()
Destructor.
Definition: SPI.cpp:38
SPI bus interface class.
Definition: SPI.h:28
void ResetAccumulator()
Resets the accumulator to zero.
Definition: SPI.cpp:210
void SetSampleDataOnRising()
Configure that the data is stable on the rising edge and the data changes on the falling edge...
Definition: SPI.cpp:81
virtual int Read(bool initiate, uint8_t *dataReceived, int size)
Read a word from the receive FIFO.
Definition: SPI.cpp:148
void SetClockActiveHigh()
Configure the clock output line to be active high.
Definition: SPI.cpp:99
void SetLSBFirst()
Configure the order that bits are sent and received on the wire to be least significant bit first...
Definition: SPI.cpp:63
Class to write to digital outputs.
Definition: DigitalOutput.h:27
virtual int Transaction(uint8_t *dataToSend, uint8_t *dataReceived, int size)
Perform a simultaneous read/write transaction with the device.
Definition: SPI.cpp:167
void SetClockRate(double hz)
Configure the rate of the generated clock signal.
Definition: SPI.cpp:48
Class to read a digital input.
Definition: DigitalInput.h:29
int GetAccumulatorLastValue() const
Read the last value read by the accumulator engine.
Definition: SPI.cpp:242
int64_t GetAccumulatorCount() const
Read the number of accumulated values.
Definition: SPI.cpp:269
void SetClockActiveLow()
Configure the clock output line to be active low.
Definition: SPI.cpp:90
SPI(Port port)
Constructor.
Definition: SPI.cpp:25
void SetSampleDataOnFalling()
Configure that the data is stable on the falling edge and the data changes on the rising edge...
Definition: SPI.cpp:72