Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 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 "HAL/SPI.h" |
| 11 | |
| 12 | namespace frc { |
| 13 | |
| 14 | class DigitalOutput; |
| 15 | class 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 | */ |
| 24 | class SPI { |
| 25 | public: |
| 26 | enum Port : int32_t { |
| 27 | kOnboardCS0 = 0, |
| 28 | kOnboardCS1, |
| 29 | kOnboardCS2, |
| 30 | kOnboardCS3, |
| 31 | kMXP |
| 32 | }; |
| 33 | explicit SPI(Port SPIport); |
| 34 | virtual ~SPI(); |
| 35 | |
| 36 | SPI(const SPI &) = delete; |
| 37 | SPI &operator=(const SPI &) = delete; |
| 38 | |
| 39 | void SetClockRate(double hz); |
| 40 | |
| 41 | void SetMSBFirst(); |
| 42 | void SetLSBFirst(); |
| 43 | |
| 44 | void SetSampleDataOnFalling(); |
| 45 | void SetSampleDataOnRising(); |
| 46 | |
| 47 | void SetClockActiveLow(); |
| 48 | void SetClockActiveHigh(); |
| 49 | |
| 50 | void SetChipSelectActiveHigh(); |
| 51 | void SetChipSelectActiveLow(); |
| 52 | |
| 53 | virtual int Write(uint8_t *data, int size); |
| 54 | virtual int Read(bool initiate, uint8_t *dataReceived, int size); |
| 55 | virtual int Transaction(uint8_t *dataToSend, uint8_t *dataReceived, int size); |
| 56 | |
| 57 | protected: |
| 58 | #ifdef WPILIB2017 |
| 59 | int m_port; |
| 60 | #else |
| 61 | HAL_SPIPort m_port; |
| 62 | #endif |
| 63 | bool m_msbFirst = false; // default little-endian |
| 64 | bool m_sampleOnTrailing = false; // default data updated on falling edge |
| 65 | bool m_clk_idle_high = false; // default clock active high |
| 66 | |
| 67 | private: |
| 68 | void Init(); |
| 69 | }; |
| 70 | |
| 71 | } // namespace frc |