This repository has been archived by the owner on May 24, 2020. It is now read-only.
forked from PaulStoffregen/RadioHead
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RHHardwareSP12.cpp
167 lines (144 loc) · 4.31 KB
/
RHHardwareSP12.cpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// RHHardwareSPI2.h
// Author: Mike McCauley (mikem@airspayce.com)
// Copyright (C) 2011 Mike McCauley
// Contributed by Joanna Rutkowska
// $Id: RHHardwareSPI2.cpp,v 1.16 2016/07/07 00:02:53 mikem Exp mikem $
// This is a copy of the standard SPI node, that is hopefully setup to work on those processors
// who have SPI2. Currently I only have it setup for Teensy 3.5/3.6
#if defined(__arm__) && defined(TEENSYDUINO) && (defined(__MK64FX512__) || defined(__MK66FX1M0__) )
#include <RHHardwareSPI2.h>
// Declare a single default instance of the hardware SPI interface class
RHHardwareSPI2 hardware_spi2;
#ifdef RH_HAVE_HARDWARE_SPI
RHHardwareSPI2::RHHardwareSPI2(Frequency frequency, BitOrder bitOrder, DataMode dataMode)
:
RHGenericSPI(frequency, bitOrder, dataMode)
{
}
uint8_t RHHardwareSPI2::transfer(uint8_t data)
{
return SPI2.transfer(data);
}
void RHHardwareSPI2::attachInterrupt()
{
#if (RH_PLATFORM == RH_PLATFORM_ARDUINO)
SPI2.attachInterrupt();
#endif
}
void RHHardwareSPI2::detachInterrupt()
{
#if (RH_PLATFORM == RH_PLATFORM_ARDUINO)
SPI2.detachInterrupt();
#endif
}
void RHHardwareSPI2::begin()
{
// Sigh: there are no common symbols for some of these SPI options across all platforms
#if (RH_PLATFORM == RH_PLATFORM_ARDUINO) || (RH_PLATFORM == RH_PLATFORM_UNO32) || (RH_PLATFORM == RH_PLATFORM_CHIPKIT_CORE)
uint8_t dataMode;
if (_dataMode == DataMode0)
dataMode = SPI_MODE0;
else if (_dataMode == DataMode1)
dataMode = SPI_MODE1;
else if (_dataMode == DataMode2)
dataMode = SPI_MODE2;
else if (_dataMode == DataMode3)
dataMode = SPI_MODE3;
else
dataMode = SPI_MODE0;
#if (RH_PLATFORM == RH_PLATFORM_ARDUINO) && defined(__arm__) && defined(CORE_TEENSY)
// Temporary work-around due to problem where avr_emulation.h does not work properly for the setDataMode() cal
SPCR &= ~SPI_MODE_MASK;
#else
#if (RH_PLATFORM == RH_PLATFORM_ARDUINO) && defined (__arm__) && defined(ARDUINO_ARCH_SAMD)
// Zero requires begin() before anything else :-)
SPI2.begin();
#endif
SPI2.setDataMode(dataMode);
#endif
#if (RH_PLATFORM == RH_PLATFORM_ARDUINO) && defined(SPI_HAS_TRANSACTION)
uint32_t frequency32;
if (_frequency == Frequency16MHz) {
frequency32 = 16000000;
} else if (_frequency == Frequency8MHz) {
frequency32 = 8000000;
} else if (_frequency == Frequency4MHz) {
frequency32 = 4000000;
} else if (_frequency == Frequency2MHz) {
frequency32 = 2000000;
} else {
frequency32 = 1000000;
}
_settings = SPISettings(frequency32,
(_bitOrder == BitOrderLSBFirst) ? LSBFIRST : MSBFIRST,
dataMode);
#endif
#if (RH_PLATFORM == RH_PLATFORM_ARDUINO) && defined (__arm__) && (defined(ARDUINO_SAM_DUE) || defined(ARDUINO_ARCH_SAMD))
// Arduino Due in 1.5.5 has its own BitOrder :-(
// So too does Arduino Zero
::BitOrder bitOrder;
#else
uint8_t bitOrder;
#endif
if (_bitOrder == BitOrderLSBFirst)
bitOrder = LSBFIRST;
else
bitOrder = MSBFIRST;
SPI2.setBitOrder(bitOrder);
uint8_t divider;
switch (_frequency)
{
case Frequency1MHz:
default:
#if F_CPU == 8000000
divider = SPI_CLOCK_DIV8;
#else
divider = SPI_CLOCK_DIV16;
#endif
break;
case Frequency2MHz:
#if F_CPU == 8000000
divider = SPI_CLOCK_DIV4;
#else
divider = SPI_CLOCK_DIV8;
#endif
break;
case Frequency4MHz:
#if F_CPU == 8000000
divider = SPI_CLOCK_DIV2;
#else
divider = SPI_CLOCK_DIV4;
#endif
break;
case Frequency8MHz:
divider = SPI_CLOCK_DIV2; // 4MHz on an 8MHz Arduino
break;
case Frequency16MHz:
divider = SPI_CLOCK_DIV2; // Not really 16MHz, only 8MHz. 4MHz on an 8MHz Arduino
break;
}
SPI2.setClockDivider(divider);
SPI2.begin();
// Teensy requires it to be set _after_ begin()
SPI2.setClockDivider(divider);
#else
#warning RHHardwareSPI does not support this platform yet. Consider adding it and contributing a patch.
#endif
}
void RHHardwareSPI2::end()
{
return SPI2.end();
}
// If our platform is arduino and we support transactions then lets use the begin/end transaction
#if (RH_PLATFORM == RH_PLATFORM_ARDUINO) && defined(SPI_HAS_TRANSACTION)
void RHHardwareSPI2::beginTransaction()
{
SPI2.beginTransaction(_settings);
}
void RHHardwareSPI2::endTransaction()
{
SPI2.endTransaction();
}
#endif
#endif
#endif