WPILib 2012
WPILibRoboticsLibraryforFRC
|
00001 /*----------------------------------------------------------------------------*/ 00002 /* Copyright (c) FIRST 2008. All Rights Reserved. */ 00003 /* Open Source Software - may be modified and shared by FRC teams. The code */ 00004 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ 00005 /*----------------------------------------------------------------------------*/ 00006 00007 #ifndef __SPI_H__ 00008 #define __SPI_H__ 00009 00010 #include "ChipObject.h" 00011 #include "SensorBase.h" 00012 00013 class DigitalOutput; 00014 class DigitalInput; 00015 00024 class SPI : public SensorBase 00025 { 00026 public: 00027 enum tFrameMode {kChipSelect, kPreLatchPulse, kPostLatchPulse, kPreAndPostLatchPulse}; 00028 enum tSPIConstants {kReceiveFIFODepth=512, kTransmitFIFODepth=512}; 00029 00030 SPI(DigitalOutput &clk, DigitalOutput &mosi, DigitalInput &miso); 00031 SPI(DigitalOutput *clk, DigitalOutput *mosi, DigitalInput *miso); 00032 SPI(DigitalOutput &clk, DigitalOutput &mosi); 00033 SPI(DigitalOutput *clk, DigitalOutput *mosi); 00034 SPI(DigitalOutput &clk, DigitalInput &miso); 00035 SPI(DigitalOutput *clk, DigitalInput *miso); 00036 virtual ~SPI(); 00037 00038 void SetBitsPerWord(UINT32 bits); 00039 UINT32 GetBitsPerWord(); 00040 void SetClockRate(double hz); 00041 00042 void SetMSBFirst(); 00043 void SetLSBFirst(); 00044 00045 void SetSampleDataOnFalling(); 00046 void SetSampleDataOnRising(); 00047 00048 void SetSlaveSelect(DigitalOutput *ss, tFrameMode mode=kChipSelect, bool activeLow=false); 00049 void SetSlaveSelect(DigitalOutput &ss, tFrameMode mode=kChipSelect, bool activeLow=false); 00050 DigitalOutput *GetSlaveSelect(tFrameMode *mode=NULL, bool *activeLow=NULL); 00051 00052 void SetClockActiveLow(); 00053 void SetClockActiveHigh(); 00054 00055 virtual void ApplyConfig(); 00056 00057 virtual UINT16 GetOutputFIFOAvailable(); 00058 virtual UINT16 GetNumReceived(); 00059 00060 virtual bool IsDone(); 00061 bool HadReceiveOverflow(); 00062 00063 virtual void Write(UINT32 data); 00064 virtual UINT32 Read(bool initiate = false); 00065 00066 virtual void Reset(); 00067 virtual void ClearReceivedData(); 00068 00069 protected: 00070 static SEM_ID m_semaphore; 00071 00072 tSPI* m_spi; 00073 tSPI::tConfig m_config; 00074 tSPI::tChannels m_channels; 00075 DigitalOutput *m_ss; 00076 00077 private: 00078 void Init(DigitalOutput *clk, DigitalOutput *mosi, DigitalInput *miso); 00079 00080 DISALLOW_COPY_AND_ASSIGN(SPI); 00081 }; 00082 00083 #endif