forked from dragino/RadioHead
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RHSoftwareSPI.cpp
166 lines (142 loc) · 3.26 KB
/
RHSoftwareSPI.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
// SoftwareSPI.cpp
// Author: Chris Lapa (chris@lapa.com.au)
// Copyright (C) 2014 Chris Lapa
// Contributed by Chris Lapa
#include <RHSoftwareSPI.h>
RHSoftwareSPI::RHSoftwareSPI(Frequency frequency, BitOrder bitOrder, DataMode dataMode)
:
RHGenericSPI(frequency, bitOrder, dataMode)
{
setPins(12, 11, 13);
}
// Caution: on Arduino Uno and many other CPUs, digitalWrite is quite slow, taking about 4us
// digitalWrite is also slow, taking about 3.5us
// resulting in very slow SPI bus speeds using this technique, up to about 120us per octet of transfer
uint8_t RHSoftwareSPI::transfer(uint8_t data)
{
uint8_t readData;
uint8_t writeData;
uint8_t builtReturn;
uint8_t mask;
if (_bitOrder == BitOrderMSBFirst)
{
mask = 0x80;
}
else
{
mask = 0x01;
}
builtReturn = 0;
readData = 0;
for (uint8_t count=0; count<8; count++)
{
if (data & mask)
{
writeData = HIGH;
}
else
{
writeData = LOW;
}
if (_clockPhase == 1)
{
// CPHA=1, miso/mosi changing state now
digitalWrite(_mosi, writeData);
digitalWrite(_sck, ~_clockPolarity);
delayPeriod();
// CPHA=1, miso/mosi stable now
readData = digitalRead(_miso);
digitalWrite(_sck, _clockPolarity);
delayPeriod();
}
else
{
// CPHA=0, miso/mosi changing state now
digitalWrite(_mosi, writeData);
digitalWrite(_sck, _clockPolarity);
delayPeriod();
// CPHA=0, miso/mosi stable now
readData = digitalRead(_miso);
digitalWrite(_sck, ~_clockPolarity);
delayPeriod();
}
if (_bitOrder == BitOrderMSBFirst)
{
mask >>= 1;
builtReturn |= (readData << (7 - count));
}
else
{
mask <<= 1;
builtReturn |= (readData << count);
}
}
digitalWrite(_sck, _clockPolarity);
return builtReturn;
}
/// Initialise the SPI library
void RHSoftwareSPI::begin()
{
if (_dataMode == DataMode0 ||
_dataMode == DataMode1)
{
_clockPolarity = LOW;
}
else
{
_clockPolarity = HIGH;
}
if (_dataMode == DataMode0 ||
_dataMode == DataMode2)
{
_clockPhase = 0;
}
else
{
_clockPhase = 1;
}
digitalWrite(_sck, _clockPolarity);
// Caution: these counts assume that digitalWrite is very fast, which is usually not true
switch (_frequency)
{
case Frequency1MHz:
_delayCounts = 8;
break;
case Frequency2MHz:
_delayCounts = 4;
break;
case Frequency4MHz:
_delayCounts = 2;
break;
case Frequency8MHz:
_delayCounts = 1;
break;
case Frequency16MHz:
_delayCounts = 0;
break;
}
}
/// Disables the SPI bus usually, in this case
/// there is no hardware controller to disable.
void RHSoftwareSPI::end() { }
/// Sets the pins used by this SoftwareSPIClass instance.
/// \param[in] miso master in slave out pin used
/// \param[in] mosi master out slave in pin used
/// \param[in] sck clock pin used
void RHSoftwareSPI::setPins(uint8_t miso, uint8_t mosi, uint8_t sck)
{
_miso = miso;
_mosi = mosi;
_sck = sck;
pinMode(_miso, INPUT);
pinMode(_mosi, OUTPUT);
pinMode(_sck, OUTPUT);
digitalWrite(_sck, _clockPolarity);
}
void RHSoftwareSPI::delayPeriod()
{
for (uint8_t count = 0; count < _delayCounts; count++)
{
__asm__ __volatile__ ("nop");
}
}