blob: 6a69e61992147af513d914361ece79cfb1dc8afd [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008-2017. 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 "SensorBase.h"
11
12namespace frc {
13
14class DigitalOutput;
15class DigitalInput;
16
17/**
18 * SPI bus interface class.
19 *
20 * This class is intended to be used by sensor (and other SPI device) drivers.
21 * It probably should not be used directly.
22 *
23 */
24class SPI : public SensorBase {
25 public:
26 enum Port { kOnboardCS0, kOnboardCS1, kOnboardCS2, kOnboardCS3, kMXP };
27 explicit SPI(Port SPIport);
28 virtual ~SPI();
29
30 SPI(const SPI&) = delete;
31 SPI& operator=(const SPI&) = delete;
32
33 void SetClockRate(double hz);
34
35 void SetMSBFirst();
36 void SetLSBFirst();
37
38 void SetSampleDataOnFalling();
39 void SetSampleDataOnRising();
40
41 void SetClockActiveLow();
42 void SetClockActiveHigh();
43
44 void SetChipSelectActiveHigh();
45 void SetChipSelectActiveLow();
46
47 virtual int Write(uint8_t* data, int size);
48 virtual int Read(bool initiate, uint8_t* dataReceived, int size);
49 virtual int Transaction(uint8_t* dataToSend, uint8_t* dataReceived, int size);
50
51 void InitAccumulator(double period, int cmd, int xfer_size, int valid_mask,
52 int valid_value, int data_shift, int data_size,
53 bool is_signed, bool big_endian);
54 void FreeAccumulator();
55 void ResetAccumulator();
56 void SetAccumulatorCenter(int center);
57 void SetAccumulatorDeadband(int deadband);
58 int GetAccumulatorLastValue() const;
59 int64_t GetAccumulatorValue() const;
60 int64_t GetAccumulatorCount() const;
61 double GetAccumulatorAverage() const;
62 void GetAccumulatorOutput(int64_t& value, int64_t& count) const;
63
64 protected:
65 int m_port;
66 bool m_msbFirst = false; // default little-endian
67 bool m_sampleOnTrailing = false; // default data updated on falling edge
68 bool m_clk_idle_high = false; // default clock active high
69
70 private:
71 void Init();
72};
73
74} // namespace frc