forked from kerrydwong/AD770X
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPIDevice.cpp
201 lines (170 loc) · 3.84 KB
/
SPIDevice.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* SPIDevice
*
* Copyright Runcible Software Pty Ltd 2015
* Created: 7/3/2015 7:51:53 PM
* Author: [email protected]
*/
#include <Arduino.h>
#include "SPIDevice.h"
#include <avr/io.h>
#include "stdio.h"
namespace
{
const int SCK_PIN = DDB5;
const int MISO_PIN = DDB4;
const int MOSI_PIN = DDB3;
const int SS_PIN = DDB2;
}
SPIDevice::SPIDevice(bool master, int selectPin)
: m_selectPin(selectPin),
m_bitOrder(MSB_FIRST),
m_clockPolarity(CLOCK_POLARITY_RISING_LEADS),
m_clockPhase(CLOCK_PHASE_SAMPLE_ON_LEADING),
m_clockRate(RATE_DIV_16),
m_interruptEnabled(false),
m_master(master),
m_doubleClockSpeed(false)
{
//
// If we are the master then we want to immediately set the select pin as an
// output and set it high. We also set the SS Pin as an output to stop spurious
// signals taking us out of master mode.
//
if ( m_master )
{
DDRB |= (1<<m_selectPin) | (1<<SS_PIN);
//
// Set MISO as an input
//
DDRB &= ~(1<<MISO_PIN);
//
// Set the select pin high
//
PORTB |= (1<<m_selectPin);
}
}
void SPIDevice::enableInterrup(bool enable)
{
m_interruptEnabled = enable;
}
bool SPIDevice::isInterruptsEnabled() const
{
return m_interruptEnabled;
}
void SPIDevice::setBitOrdering( const BitOrder order )
{
m_bitOrder = order;
}
const SPIDevice::BitOrder SPIDevice::getBitOrdering() const
{
return m_bitOrder;
}
void SPIDevice::setMaster( bool master )
{
m_master = master;
}
bool SPIDevice::isMaster() const
{
return m_master;
}
void SPIDevice::setClockRate( const ClockRate rate )
{
m_clockRate = rate;
}
const SPIDevice::ClockRate SPIDevice::getClockRate() const
{
return m_clockRate;
}
const SPIDevice::ClockPolarity SPIDevice::getClockPolarity() const
{
return m_clockPolarity;
}
void SPIDevice::setClockPolarity(const ClockPolarity polarity)
{
m_clockPolarity = polarity;
}
const SPIDevice::ClockPhase SPIDevice::getClockPhase() const
{
return m_clockPhase;
}
void SPIDevice::setClockPhase(const ClockPhase phase)
{
m_clockPhase = phase;
}
void SPIDevice::setDoubleSpeedModeEnabled( bool enable )
{
m_doubleClockSpeed = enable;
}
bool SPIDevice::isDoubleSpeedModeEnabled() const
{
return m_doubleClockSpeed;
}
void SPIDevice::setup() const
{
if ( m_master )
{
//
// Set MOSI, SCK and the select pin as outputs
//
DDRB |= (1<<MOSI_PIN) | (1<<SCK_PIN) | (1<<SS_PIN) | (1<<m_selectPin);
//
// Set MISO as an input
//
DDRB &= ~(1<<MISO_PIN);
PORTB &= ~(1<<MISO_PIN);
}
else
{
DDRB |= (1<<MISO_PIN);
}
int newSPCR = (1 << SPE ) |
((m_bitOrder)<<DORD) |
((m_master?1:0)<< MSTR) |
((m_interruptEnabled?1:0) << SPIE) |
((m_clockPolarity?1:0) << CPOL) |
((m_clockPhase?1:0) << CPHA) |
(m_clockRate & 0x3);
SPCR = newSPCR;
if ( m_doubleClockSpeed )
{
SPSR |= ((m_doubleClockSpeed?1:0) << SPI2X);
}
else
{
SPSR &= ~((m_doubleClockSpeed?1:0) << SPI2X);
}
}
bool SPIDevice::getWriteCollisionFlag() const
{
return (SPSR & (1<<WCOL)) != 0;
}
bool SPIDevice::getInterruptStatus() const
{
return (SPSR & (1<<SPIF)) != 0;
}
void SPIDevice::selectSlave() const
{
//
// Pull select low (as it is active low)
//
//PORTB &= ~(1 << m_selectPin);
digitalWrite(m_selectPin,LOW);
}
void SPIDevice::releaseSlave() const
{
//PORTB |= (1 << m_selectPin);
digitalWrite(m_selectPin,HIGH);
}
uint8_t SPIDevice::writeByte( uint8_t byte )
{
SPDR = byte;
while( !getInterruptStatus() );
return SPDR;
}
uint8_t SPIDevice::readByte() const
{
SPDR = 0;
while( !getInterruptStatus() );
return SPDR;
}