V148
Fork of RadioHead-148 by
RHGenericSPI.h@1:b7641da2b203, 2017-10-25 (annotated)
- Committer:
- ilkaykozak
- Date:
- Wed Oct 25 05:14:09 2017 +0000
- Revision:
- 1:b7641da2b203
- Parent:
- 0:ab4e012489ef
V148
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
davidr99 | 0:ab4e012489ef | 1 | // RHGenericSPI.h |
davidr99 | 0:ab4e012489ef | 2 | // Author: Mike McCauley (mikem@airspayce.com) |
davidr99 | 0:ab4e012489ef | 3 | // Copyright (C) 2011 Mike McCauley |
davidr99 | 0:ab4e012489ef | 4 | // Contributed by Joanna Rutkowska |
davidr99 | 0:ab4e012489ef | 5 | // $Id: RHGenericSPI.h,v 1.7 2014/04/14 08:37:11 mikem Exp $ |
davidr99 | 0:ab4e012489ef | 6 | |
davidr99 | 0:ab4e012489ef | 7 | #ifndef RHGenericSPI_h |
davidr99 | 0:ab4e012489ef | 8 | #define RHGenericSPI_h |
davidr99 | 0:ab4e012489ef | 9 | |
davidr99 | 0:ab4e012489ef | 10 | #include <RadioHead.h> |
davidr99 | 0:ab4e012489ef | 11 | |
davidr99 | 0:ab4e012489ef | 12 | ///////////////////////////////////////////////////////////////////// |
davidr99 | 0:ab4e012489ef | 13 | /// \class RHGenericSPI RHGenericSPI.h <RHGenericSPI.h> |
davidr99 | 0:ab4e012489ef | 14 | /// \brief Base class for SPI interfaces |
davidr99 | 0:ab4e012489ef | 15 | /// |
davidr99 | 0:ab4e012489ef | 16 | /// This generic abstract class is used to encapsulate hardware or software SPI interfaces for |
davidr99 | 0:ab4e012489ef | 17 | /// a variety of platforms. |
davidr99 | 0:ab4e012489ef | 18 | /// The intention is so that driver classes can be configured to use hardware or software SPI |
davidr99 | 0:ab4e012489ef | 19 | /// without changing the main code. |
davidr99 | 0:ab4e012489ef | 20 | /// |
davidr99 | 0:ab4e012489ef | 21 | /// You must provide a subclass of this class to driver constructors that require SPI. |
davidr99 | 0:ab4e012489ef | 22 | /// A concrete subclass that encapsualates the standard Arduino hardware SPI and a bit-banged |
davidr99 | 0:ab4e012489ef | 23 | /// software implementation is included. |
davidr99 | 0:ab4e012489ef | 24 | /// |
davidr99 | 0:ab4e012489ef | 25 | /// Do not directly use this class: it must be subclassed and the following abstract functions at least |
davidr99 | 0:ab4e012489ef | 26 | /// must be implmented: |
davidr99 | 0:ab4e012489ef | 27 | /// - begin() |
davidr99 | 0:ab4e012489ef | 28 | /// - end() |
davidr99 | 0:ab4e012489ef | 29 | /// - transfer() |
davidr99 | 0:ab4e012489ef | 30 | class RHGenericSPI |
davidr99 | 0:ab4e012489ef | 31 | { |
davidr99 | 0:ab4e012489ef | 32 | public: |
davidr99 | 0:ab4e012489ef | 33 | |
davidr99 | 0:ab4e012489ef | 34 | /// \brief Defines constants for different SPI modes |
davidr99 | 0:ab4e012489ef | 35 | /// |
davidr99 | 0:ab4e012489ef | 36 | /// Defines constants for different SPI modes |
davidr99 | 0:ab4e012489ef | 37 | /// that can be passed to the constructor or setMode() |
davidr99 | 0:ab4e012489ef | 38 | /// We need to define these in a device and platform independent way, because the |
davidr99 | 0:ab4e012489ef | 39 | /// SPI implementation is different on each platform. |
davidr99 | 0:ab4e012489ef | 40 | typedef enum |
davidr99 | 0:ab4e012489ef | 41 | { |
davidr99 | 0:ab4e012489ef | 42 | DataMode0 = 0, ///< SPI Mode 0: CPOL = 0, CPHA = 0 |
davidr99 | 0:ab4e012489ef | 43 | DataMode1, ///< SPI Mode 1: CPOL = 0, CPHA = 1 |
davidr99 | 0:ab4e012489ef | 44 | DataMode2, ///< SPI Mode 2: CPOL = 1, CPHA = 0 |
davidr99 | 0:ab4e012489ef | 45 | DataMode3, ///< SPI Mode 3: CPOL = 1, CPHA = 1 |
davidr99 | 0:ab4e012489ef | 46 | } DataMode; |
davidr99 | 0:ab4e012489ef | 47 | |
davidr99 | 0:ab4e012489ef | 48 | /// \brief Defines constants for different SPI bus frequencies |
davidr99 | 0:ab4e012489ef | 49 | /// |
davidr99 | 0:ab4e012489ef | 50 | /// Defines constants for different SPI bus frequencies |
davidr99 | 0:ab4e012489ef | 51 | /// that can be passed to setFrequency(). |
davidr99 | 0:ab4e012489ef | 52 | /// The frequency you get may not be exactly the one according to the name. |
davidr99 | 0:ab4e012489ef | 53 | /// We need to define these in a device and platform independent way, because the |
davidr99 | 0:ab4e012489ef | 54 | /// SPI implementation is different on each platform. |
davidr99 | 0:ab4e012489ef | 55 | typedef enum |
davidr99 | 0:ab4e012489ef | 56 | { |
davidr99 | 0:ab4e012489ef | 57 | Frequency1MHz = 0, ///< SPI bus frequency close to 1MHz |
davidr99 | 0:ab4e012489ef | 58 | Frequency2MHz, ///< SPI bus frequency close to 2MHz |
davidr99 | 0:ab4e012489ef | 59 | Frequency4MHz, ///< SPI bus frequency close to 4MHz |
davidr99 | 0:ab4e012489ef | 60 | Frequency8MHz, ///< SPI bus frequency close to 8MHz |
davidr99 | 0:ab4e012489ef | 61 | Frequency16MHz ///< SPI bus frequency close to 16MHz |
davidr99 | 0:ab4e012489ef | 62 | } Frequency; |
davidr99 | 0:ab4e012489ef | 63 | |
davidr99 | 0:ab4e012489ef | 64 | /// \brief Defines constants for different SPI endianness |
davidr99 | 0:ab4e012489ef | 65 | /// |
davidr99 | 0:ab4e012489ef | 66 | /// Defines constants for different SPI endianness |
davidr99 | 0:ab4e012489ef | 67 | /// that can be passed to setBitOrder() |
davidr99 | 0:ab4e012489ef | 68 | /// We need to define these in a device and platform independent way, because the |
davidr99 | 0:ab4e012489ef | 69 | /// SPI implementation is different on each platform. |
davidr99 | 0:ab4e012489ef | 70 | typedef enum |
davidr99 | 0:ab4e012489ef | 71 | { |
davidr99 | 0:ab4e012489ef | 72 | BitOrderMSBFirst = 0, ///< SPI MSB first |
davidr99 | 0:ab4e012489ef | 73 | BitOrderLSBFirst, ///< SPI LSB first |
davidr99 | 0:ab4e012489ef | 74 | } BitOrder; |
davidr99 | 0:ab4e012489ef | 75 | |
davidr99 | 0:ab4e012489ef | 76 | /// Constructor |
davidr99 | 0:ab4e012489ef | 77 | /// Creates an instance of an abstract SPI interface. |
davidr99 | 0:ab4e012489ef | 78 | /// Do not use this contructor directly: you must instead use on of the concrete subclasses provided |
davidr99 | 0:ab4e012489ef | 79 | /// such as RHHardwareSPI or RHSoftwareSPI |
davidr99 | 0:ab4e012489ef | 80 | /// \param[in] frequency One of RHGenericSPI::Frequency to select the SPI bus frequency. The frequency |
davidr99 | 0:ab4e012489ef | 81 | /// is mapped to the closest available bus frequency on the platform. |
davidr99 | 0:ab4e012489ef | 82 | /// \param[in] bitOrder Select the SPI bus bit order, one of RHGenericSPI::BitOrderMSBFirst or |
davidr99 | 0:ab4e012489ef | 83 | /// RHGenericSPI::BitOrderLSBFirst. |
davidr99 | 0:ab4e012489ef | 84 | /// \param[in] dataMode Selects the SPI bus data mode. One of RHGenericSPI::DataMode |
davidr99 | 0:ab4e012489ef | 85 | RHGenericSPI(Frequency frequency = Frequency1MHz, BitOrder bitOrder = BitOrderMSBFirst, DataMode dataMode = DataMode0); |
davidr99 | 0:ab4e012489ef | 86 | |
davidr99 | 0:ab4e012489ef | 87 | /// Transfer a single octet to and from the SPI interface |
davidr99 | 0:ab4e012489ef | 88 | /// \param[in] data The octet to send |
davidr99 | 0:ab4e012489ef | 89 | /// \return The octet read from SPI while the data octet was sent |
davidr99 | 0:ab4e012489ef | 90 | virtual uint8_t transfer(uint8_t data) = 0; |
davidr99 | 0:ab4e012489ef | 91 | |
davidr99 | 0:ab4e012489ef | 92 | /// SPI Configuration methods |
davidr99 | 0:ab4e012489ef | 93 | /// Enable SPI interrupts (if supported) |
davidr99 | 0:ab4e012489ef | 94 | /// This can be used in an SPI slave to indicate when an SPI message has been received |
davidr99 | 0:ab4e012489ef | 95 | virtual void attachInterrupt() {}; |
davidr99 | 0:ab4e012489ef | 96 | |
davidr99 | 0:ab4e012489ef | 97 | /// Disable SPI interrupts (if supported) |
davidr99 | 0:ab4e012489ef | 98 | /// This can be used to diable the SPI interrupt in slaves where that is supported. |
davidr99 | 0:ab4e012489ef | 99 | virtual void detachInterrupt() {}; |
davidr99 | 0:ab4e012489ef | 100 | |
davidr99 | 0:ab4e012489ef | 101 | /// Initialise the SPI library. |
davidr99 | 0:ab4e012489ef | 102 | /// Call this after configuring and before using the SPI library |
davidr99 | 0:ab4e012489ef | 103 | virtual void begin() = 0; |
davidr99 | 0:ab4e012489ef | 104 | |
davidr99 | 0:ab4e012489ef | 105 | /// Disables the SPI bus (leaving pin modes unchanged). |
davidr99 | 0:ab4e012489ef | 106 | /// Call this after you have finished using the SPI interface |
davidr99 | 0:ab4e012489ef | 107 | virtual void end() = 0; |
davidr99 | 0:ab4e012489ef | 108 | |
davidr99 | 0:ab4e012489ef | 109 | /// Sets the bit order the SPI interface will use |
davidr99 | 0:ab4e012489ef | 110 | /// Sets the order of the bits shifted out of and into the SPI bus, either |
davidr99 | 0:ab4e012489ef | 111 | /// LSBFIRST (least-significant bit first) or MSBFIRST (most-significant bit first). |
davidr99 | 0:ab4e012489ef | 112 | /// \param[in] bitOrder Bit order to be used: one of RHGenericSPI::BitOrder |
davidr99 | 0:ab4e012489ef | 113 | virtual void setBitOrder(BitOrder bitOrder); |
davidr99 | 0:ab4e012489ef | 114 | |
davidr99 | 0:ab4e012489ef | 115 | /// Sets the SPI data mode: that is, clock polarity and phase. |
davidr99 | 0:ab4e012489ef | 116 | /// See the Wikipedia article on SPI for details. |
davidr99 | 0:ab4e012489ef | 117 | /// \param[in] dataMode The mode to use: one of RHGenericSPI::DataMode |
davidr99 | 0:ab4e012489ef | 118 | virtual void setDataMode(DataMode dataMode); |
davidr99 | 0:ab4e012489ef | 119 | |
davidr99 | 0:ab4e012489ef | 120 | /// Sets the SPI clock divider relative to the system clock. |
davidr99 | 0:ab4e012489ef | 121 | /// On AVR based boards, the dividers available are 2, 4, 8, 16, 32, 64 or 128. |
davidr99 | 0:ab4e012489ef | 122 | /// The default setting is SPI_CLOCK_DIV4, which sets the SPI clock to one-quarter |
davidr99 | 0:ab4e012489ef | 123 | /// the frequency of the system clock (4 Mhz for the boards at 16 MHz). |
davidr99 | 0:ab4e012489ef | 124 | /// \param[in] frequency The data rate to use: one of RHGenericSPI::Frequency |
davidr99 | 0:ab4e012489ef | 125 | virtual void setFrequency(Frequency frequency); |
davidr99 | 0:ab4e012489ef | 126 | |
davidr99 | 0:ab4e012489ef | 127 | protected: |
davidr99 | 0:ab4e012489ef | 128 | /// The configure SPI Bus frequency, one of RHGenericSPI::Frequency |
davidr99 | 0:ab4e012489ef | 129 | Frequency _frequency; // Bus frequency, one of RHGenericSPI::Frequency |
davidr99 | 0:ab4e012489ef | 130 | |
davidr99 | 0:ab4e012489ef | 131 | /// Bit order, one of RHGenericSPI::BitOrder |
davidr99 | 0:ab4e012489ef | 132 | BitOrder _bitOrder; |
davidr99 | 0:ab4e012489ef | 133 | |
davidr99 | 0:ab4e012489ef | 134 | /// SPI bus mode, one of RHGenericSPI::DataMode |
davidr99 | 0:ab4e012489ef | 135 | DataMode _dataMode; |
davidr99 | 0:ab4e012489ef | 136 | }; |
davidr99 | 0:ab4e012489ef | 137 | #endif |