blob: ac89bbc720cc9aa0ccbef2825c49fe07004dd5b9 [file] [log] [blame]
jerrymf1579332013-02-07 01:56:28 +00001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008. 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 $(WIND_BASE)/WPILib. */
5/*----------------------------------------------------------------------------*/
6
7#ifndef __SPI_H__
8#define __SPI_H__
9
10#include "ChipObject.h"
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 * The FPGA only supports a single SPI interface.
23 */
24class SPI : public SensorBase
25{
26public:
27 enum tFrameMode {kChipSelect, kPreLatchPulse, kPostLatchPulse, kPreAndPostLatchPulse};
28 enum tSPIConstants {kReceiveFIFODepth=512, kTransmitFIFODepth=512};
29
30 SPI(DigitalOutput &clk, DigitalOutput &mosi, DigitalInput &miso);
31 SPI(DigitalOutput *clk, DigitalOutput *mosi, DigitalInput *miso);
32 SPI(DigitalOutput &clk, DigitalOutput &mosi);
33 SPI(DigitalOutput *clk, DigitalOutput *mosi);
34 SPI(DigitalOutput &clk, DigitalInput &miso);
35 SPI(DigitalOutput *clk, DigitalInput *miso);
36 virtual ~SPI();
37
38 void SetBitsPerWord(UINT32 bits);
39 UINT32 GetBitsPerWord();
40 void SetClockRate(double hz);
41
42 void SetMSBFirst();
43 void SetLSBFirst();
44
45 void SetSampleDataOnFalling();
46 void SetSampleDataOnRising();
47
48 void SetSlaveSelect(DigitalOutput *ss, tFrameMode mode=kChipSelect, bool activeLow=false);
49 void SetSlaveSelect(DigitalOutput &ss, tFrameMode mode=kChipSelect, bool activeLow=false);
50 DigitalOutput *GetSlaveSelect(tFrameMode *mode=NULL, bool *activeLow=NULL);
51
52 void SetClockActiveLow();
53 void SetClockActiveHigh();
54
55 virtual void ApplyConfig();
56
57 virtual UINT16 GetOutputFIFOAvailable();
58 virtual UINT16 GetNumReceived();
59
60 virtual bool IsDone();
61 bool HadReceiveOverflow();
62
63 virtual void Write(UINT32 data);
64 virtual UINT32 Read(bool initiate = false);
65
66 virtual void Reset();
67 virtual void ClearReceivedData();
68
69protected:
70 static SEM_ID m_semaphore;
71
72 tSPI* m_spi;
73 tSPI::tConfig m_config;
74 tSPI::tChannels m_channels;
75 DigitalOutput *m_ss;
76
77private:
78 void Init(DigitalOutput *clk, DigitalOutput *mosi, DigitalInput *miso);
79
80 DISALLOW_COPY_AND_ASSIGN(SPI);
81};
82
83#endif