2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2016-01-02 03:02:34 -08:00
|
|
|
/* Copyright (c) FIRST 2008-2016. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2016-01-02 03:02:34 -08:00
|
|
|
|
2014-05-02 17:54:01 -04:00
|
|
|
#pragma once
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
#include "SensorBase.h"
|
|
|
|
|
|
|
|
|
|
class DigitalOutput;
|
|
|
|
|
class DigitalInput;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* SPI bus interface class.
|
|
|
|
|
*
|
|
|
|
|
* This class is intended to be used by sensor (and other SPI device) drivers.
|
|
|
|
|
* It probably should not be used directly.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
class SPI : public SensorBase {
|
|
|
|
|
public:
|
|
|
|
|
enum Port { kOnboardCS0, kOnboardCS1, kOnboardCS2, kOnboardCS3, kMXP };
|
2016-07-10 17:47:44 -07:00
|
|
|
explicit SPI(Port SPIport);
|
2015-06-25 15:07:55 -04:00
|
|
|
virtual ~SPI();
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-07-21 01:23:34 -07:00
|
|
|
SPI(const SPI&) = delete;
|
|
|
|
|
SPI& operator=(const SPI&) = delete;
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void SetClockRate(double hz);
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void SetMSBFirst();
|
|
|
|
|
void SetLSBFirst();
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void SetSampleDataOnFalling();
|
|
|
|
|
void SetSampleDataOnRising();
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void SetClockActiveLow();
|
|
|
|
|
void SetClockActiveHigh();
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void SetChipSelectActiveHigh();
|
|
|
|
|
void SetChipSelectActiveLow();
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
virtual int Write(uint8_t* data, int size);
|
|
|
|
|
virtual int Read(bool initiate, uint8_t* dataReceived, int size);
|
|
|
|
|
virtual int Transaction(uint8_t* dataToSend, uint8_t* dataReceived, int size);
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
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);
|
2015-11-22 11:50:49 -08:00
|
|
|
void FreeAccumulator();
|
|
|
|
|
void ResetAccumulator();
|
2016-09-06 00:01:45 -07:00
|
|
|
void SetAccumulatorCenter(int center);
|
|
|
|
|
void SetAccumulatorDeadband(int deadband);
|
|
|
|
|
int GetAccumulatorLastValue() const;
|
2015-11-22 11:50:49 -08:00
|
|
|
int64_t GetAccumulatorValue() const;
|
2016-07-12 10:45:14 -07:00
|
|
|
int64_t GetAccumulatorCount() const;
|
2015-11-22 11:50:49 -08:00
|
|
|
double GetAccumulatorAverage() const;
|
2016-07-12 10:45:14 -07:00
|
|
|
void GetAccumulatorOutput(int64_t& value, int64_t& count) const;
|
2015-11-22 11:50:49 -08:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
protected:
|
2016-09-06 00:01:45 -07:00
|
|
|
int m_port;
|
2016-05-20 17:30:37 -07:00
|
|
|
bool m_msbFirst = false; // default little-endian
|
|
|
|
|
bool m_sampleOnTrailing = false; // default data updated on falling edge
|
|
|
|
|
bool m_clk_idle_high = false; // default clock active high
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
private:
|
|
|
|
|
void Init();
|
2013-12-15 18:30:16 -05:00
|
|
|
};
|