blob: b6d68f38594e2aaf6633d16637d18537875b3e1f [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
Brian Silverman1a675112016-02-20 20:42:49 -05002/* Copyright (c) FIRST 2008-2016. All Rights Reserved. */
Brian Silverman26e4e522015-12-17 01:56:40 -05003/* Open Source Software - may be modified and shared by FRC teams. The code */
Brian Silverman1a675112016-02-20 20:42:49 -05004/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
Brian Silverman26e4e522015-12-17 01:56:40 -05006/*----------------------------------------------------------------------------*/
Brian Silverman1a675112016-02-20 20:42:49 -05007
Brian Silverman26e4e522015-12-17 01:56:40 -05008#pragma once
9
10#include "HAL/HAL.hpp"
11#include "SensorBase.h"
12
13class DigitalOutput;
14class DigitalInput;
15
16/**
17 * SPI bus interface class.
18 *
19 * This class is intended to be used by sensor (and other SPI device) drivers.
20 * It probably should not be used directly.
21 *
22 */
23class SPI : public SensorBase {
24 public:
25 enum Port { kOnboardCS0, kOnboardCS1, kOnboardCS2, kOnboardCS3, kMXP };
26 SPI(Port SPIport);
27 virtual ~SPI();
28
29 SPI(const SPI&) = delete;
30 SPI& operator=(const SPI&) = delete;
31
32 void SetClockRate(double hz);
33
34 void SetMSBFirst();
35 void SetLSBFirst();
36
37 void SetSampleDataOnFalling();
38 void SetSampleDataOnRising();
39
40 void SetClockActiveLow();
41 void SetClockActiveHigh();
42
43 void SetChipSelectActiveHigh();
44 void SetChipSelectActiveLow();
45
46 virtual int32_t Write(uint8_t* data, uint8_t size);
47 virtual int32_t Read(bool initiate, uint8_t* dataReceived, uint8_t size);
48 virtual int32_t Transaction(uint8_t* dataToSend, uint8_t* dataReceived,
49 uint8_t size);
50
51 void InitAccumulator(double period, uint32_t cmd, uint8_t xfer_size,
52 uint32_t valid_mask, uint32_t valid_value,
53 uint8_t data_shift, uint8_t data_size, bool is_signed,
54 bool big_endian);
55 void FreeAccumulator();
56 void ResetAccumulator();
57 void SetAccumulatorCenter(int32_t center);
58 void SetAccumulatorDeadband(int32_t deadband);
59 int32_t GetAccumulatorLastValue() const;
60 int64_t GetAccumulatorValue() const;
61 uint32_t GetAccumulatorCount() const;
62 double GetAccumulatorAverage() const;
63 void GetAccumulatorOutput(int64_t &value, uint32_t &count) const;
64
65 protected:
66 uint8_t m_port;
67 bool m_msbFirst = false; // default little-endian
68 bool m_sampleOnTrailing = false; // default data updated on falling edge
69 bool m_clk_idle_high = false; // default clock active high
70
71 private:
72 void Init();
73};