RF69
HardwareSPI.h
1 // HardwareSPI.h
2 // Author: Mike McCauley (mikem@airspayce.com)
3 // Copyright (C) 2011 Mike McCauley
4 // Contributed by Joanna Rutkowska
5 // $Id: HardwareSPI.h,v 1.1 2014/03/31 21:37:42 mikem Exp mikem $
6 
7 #include "GenericSPI.h"
8 
9 // These defs cause trouble on some versions of Arduino
10 #undef round
11 #undef double
12 
13 #include <SPI.h>
14 
15 
16 /////////////////////////////////////////////////////////////////////
17 /// \class HardwareSPIClass HardwareSPI.h <HardwareSPI.h>
18 /// \brief Encapsulate the Arduino hardware SPI interface
19 ///
20 /// This concrete subclass of GenericSPIClass ncapsulates the standard Arduino hardware SPI interface
22 {
23 
24 public:
25 
26  /// Transfer a single octet to and from the SPI interface
27  /// \param[in] data The octet to send
28  /// \return The octet read from SPI while the data octet was sent
29  uint8_t transfer(uint8_t data)
30  {
31  return SPI.transfer(data);
32  }
33 
34  // SPI Configuration methods
35  /// Enable SPI interrupts
36  /// This can be used in an SPI slave to indicate when an SPI message has been received
37  /// It will cause the SPI_STC_vect interrupt vectr to be executed
39  {
40  return SPI.attachInterrupt();
41  }
42 
43  /// Disable SPI interrupts
44  /// This can be used to diable the SPI interrupt in slaves where that is supported.
46  {
47  return SPI.detachInterrupt();
48  }
49 
50  /// Initialise the SPI library
51  /// Initializes the SPI bus by setting SCK, MOSI, and SS to outputs, pulling SCK and MOSI low, and SS high.
52  void begin()
53  {
54  return SPI.begin();
55  }
56 
57  /// Disables the SPI bus (leaving pin modes unchanged).
58  /// Call this after you have finished using the SPI interface
59  void end()
60  {
61  return SPI.end();
62  }
63 
64  /// Sets the bit order the SPI interface will use
65  /// Sets the order of the bits shifted out of and into the SPI bus, either
66  /// LSBFIRST (least-significant bit first) or MSBFIRST (most-significant bit first).
67  /// \param[in] bitOrder Bit order to be used: LSBFIRST or MSBFIRST
68  void setBitOrder(uint8_t bitOrder)
69  {
70  SPI.setBitOrder (bitOrder);
71  }
72 
73  /// Sets the SPI data mode: that is, clock polarity and phase.
74  /// See the Wikipedia article on SPI for details.
75  /// \param[in] mode The mode to use: SPI_MODE0 SPI_MODE1 SPI_MODE2 SPI_MODE3
76  void setDataMode(uint8_t mode)
77  {
78  SPI.setDataMode (mode);
79  }
80 
81  /// Sets the SPI clock divider relative to the system clock.
82  /// On AVR based boards, the dividers available are 2, 4, 8, 16, 32, 64 or 128.
83  /// The default setting is SPI_CLOCK_DIV4, which sets the SPI clock to one-quarter
84  /// the frequency of the system clock (4 Mhz for the boards at 16 MHz).
85  /// \param[in] rate The data rate to use: one of SPI_CLOCK_
86  void setClockDivider(uint8_t rate)
87  {
88  SPI.setClockDivider (rate);
89  }
90 };
91 
92 extern HardwareSPIClass Hardware_spi;
void begin()
Definition: HardwareSPI.h:52
Base class for SPI interfaces.
Definition: GenericSPI.h:28
void setClockDivider(uint8_t rate)
Definition: HardwareSPI.h:86
Encapsulate the Arduino hardware SPI interface.
Definition: HardwareSPI.h:21
void detachInterrupt()
Definition: HardwareSPI.h:45
uint8_t transfer(uint8_t data)
Definition: HardwareSPI.h:29
void setDataMode(uint8_t mode)
Definition: HardwareSPI.h:76
void end()
Definition: HardwareSPI.h:59
void setBitOrder(uint8_t bitOrder)
Definition: HardwareSPI.h:68
void attachInterrupt()
Definition: HardwareSPI.h:38