blob: 7b33dd7e6baa51ddd2ea61867d3cb81d212e7426 [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008. All Rights Reserved.
3 */
4/* Open Source Software - may be modified and shared by FRC teams. The code */
5/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
6/*----------------------------------------------------------------------------*/
7#pragma once
8
9#include "HAL/HAL.hpp"
10#include "SensorBase.h"
11
12class DigitalOutput;
13class DigitalInput;
14
15/**
16 * SPI bus interface class.
17 *
18 * This class is intended to be used by sensor (and other SPI device) drivers.
19 * It probably should not be used directly.
20 *
21 */
22class SPI : public SensorBase {
23 public:
24 enum Port { kOnboardCS0, kOnboardCS1, kOnboardCS2, kOnboardCS3, kMXP };
25 SPI(Port SPIport);
26 virtual ~SPI();
27
28 SPI(const SPI&) = delete;
29 SPI& operator=(const SPI&) = delete;
30
31 void SetClockRate(double hz);
32
33 void SetMSBFirst();
34 void SetLSBFirst();
35
36 void SetSampleDataOnFalling();
37 void SetSampleDataOnRising();
38
39 void SetClockActiveLow();
40 void SetClockActiveHigh();
41
42 void SetChipSelectActiveHigh();
43 void SetChipSelectActiveLow();
44
45 virtual int32_t Write(uint8_t* data, uint8_t size);
46 virtual int32_t Read(bool initiate, uint8_t* dataReceived, uint8_t size);
47 virtual int32_t Transaction(uint8_t* dataToSend, uint8_t* dataReceived,
48 uint8_t size);
49
50 void InitAccumulator(double period, uint32_t cmd, uint8_t xfer_size,
51 uint32_t valid_mask, uint32_t valid_value,
52 uint8_t data_shift, uint8_t data_size, bool is_signed,
53 bool big_endian);
54 void FreeAccumulator();
55 void ResetAccumulator();
56 void SetAccumulatorCenter(int32_t center);
57 void SetAccumulatorDeadband(int32_t deadband);
58 int32_t GetAccumulatorLastValue() const;
59 int64_t GetAccumulatorValue() const;
60 uint32_t GetAccumulatorCount() const;
61 double GetAccumulatorAverage() const;
62 void GetAccumulatorOutput(int64_t &value, uint32_t &count) const;
63
64 protected:
65 uint8_t 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};