forked from adafruit/RadioHead
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RHHardwareSPI.h
93 lines (77 loc) · 3.83 KB
/
RHHardwareSPI.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// RHHardwareSPI.h
// Author: Mike McCauley ([email protected])
// Copyright (C) 2011 Mike McCauley
// Contributed by Joanna Rutkowska
// $Id: RHHardwareSPI.h,v 1.11 2017/11/06 00:04:08 mikem Exp $
#ifndef RHHardwareSPI_h
#define RHHardwareSPI_h
#include <RHGenericSPI.h>
/////////////////////////////////////////////////////////////////////
/// \class RHHardwareSPI RHHardwareSPI.h <RHHardwareSPI.h>
/// \brief Encapsulate a hardware SPI bus interface
///
/// This concrete subclass of GenericSPIClass encapsulates the standard Arduino hardware and other
/// hardware SPI interfaces.
///
/// SPI transactions are supported in development environments that support it with SPI_HAS_TRANSACTION.
class RHHardwareSPI : public RHGenericSPI
{
#ifdef RH_HAVE_HARDWARE_SPI
public:
/// Constructor
/// Creates an instance of a hardware SPI interface, using whatever SPI hardware is available on
/// your processor platform. On Arduino and Uno32, uses SPI. On Maple, uses HardwareSPI.
/// \param[in] frequency One of RHGenericSPI::Frequency to select the SPI bus frequency. The frequency
/// is mapped to the closest available bus frequency on the platform.
/// \param[in] bitOrder Select the SPI bus bit order, one of RHGenericSPI::BitOrderMSBFirst or
/// RHGenericSPI::BitOrderLSBFirst.
/// \param[in] dataMode Selects the SPI bus data mode. One of RHGenericSPI::DataMode
RHHardwareSPI(Frequency frequency = Frequency1MHz, BitOrder bitOrder = BitOrderMSBFirst, DataMode dataMode = DataMode0);
/// Transfer a single octet to and from the SPI interface
/// \param[in] data The octet to send
/// \return The octet read from SPI while the data octet was sent
uint8_t transfer(uint8_t data);
// SPI Configuration methods
/// Enable SPI interrupts
/// This can be used in an SPI slave to indicate when an SPI message has been received
/// It will cause the SPI_STC_vect interrupt vectr to be executed
void attachInterrupt();
/// Disable SPI interrupts
/// This can be used to diable the SPI interrupt in slaves where that is supported.
void detachInterrupt();
/// Initialise the SPI library
/// Call this after configuring the SPI interface and before using it to transfer data.
/// Initializes the SPI bus by setting SCK, MOSI, and SS to outputs, pulling SCK and MOSI low, and SS high.
void begin();
/// Disables the SPI bus (leaving pin modes unchanged).
/// Call this after you have finished using the SPI interface.
void end();
#else
// not supported on ATTiny etc
uint8_t transfer(uint8_t /*data*/) {return 0;}
void begin(){}
void end(){}
#endif
/// Signal the start of an SPI transaction that must not be interrupted by other SPI actions
/// In subclasses that support transactions this will ensure that other SPI transactions
/// are blocked until this one is completed by endTransaction().
/// Uses the underlying SPI transaction support if available as specified by SPI_HAS_TRANSACTION.
virtual void beginTransaction();
/// Signal the end of an SPI transaction
/// Uses the underlying SPI transaction support if available as specified by SPI_HAS_TRANSACTION.
virtual void endTransaction();
/// Specify the interrupt number of the interrupt that will use SPI transactions
/// Tells the SPI support software that SPI transactions will occur with the interrupt
/// handler assocated with interruptNumber
/// Uses the underlying SPI transaction support if available as specified by SPI_HAS_TRANSACTION.
/// \param[in] interruptNumber The number of the interrupt
virtual void usingInterrupt(uint8_t interruptNumber);
protected:
#if defined(SPI_HAS_TRANSACTION)
// Storage for SPI settings used in SPI transactions
SPISettings _settings;
#endif
};
// Built in default instance
extern RHHardwareSPI hardware_spi;
#endif