-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCC2500.cpp
executable file
·177 lines (141 loc) · 4 KB
/
CC2500.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
/*!
* \file CC2500.cpp
* \version 1.1
* \date 29-01-2009
* \author George Mathijssen, [email protected]
*
* Copyright (c) 2008, 2009 George Mathijssen
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* \internal
* Created: 02-10-2008
*/
#ifndef ESP8266
#include "CC2500.h"
#include "Arduino.h"
CC2500::CC2500(unsigned char pinCS, unsigned char pinSCK, unsigned char pinMOSI,
unsigned char pinMISO)
: m_pinCS(pinCS)
, m_pinSCK(pinSCK)
, m_pinMOSI(pinMOSI)
, m_pinMISO(pinMISO)
{
}
CC2500::~CC2500()
{
}
void CC2500::init()
{
// setup pin mode
pinMode(m_pinMOSI, OUTPUT);
pinMode(m_pinMISO, INPUT);
pinMode(m_pinSCK, OUTPUT);
pinMode(m_pinCS, OUTPUT);
// disable device
digitalWrite(m_pinCS, HIGH);
// setup SPI control register: SPCR = 01010000
// interrupt disabled, spi enabled, msb 1st, master, clk low when idle,
// sample on rising edge of clk, system clock rate fastest
SPCR = (1<<SPE) | (1<<MSTR);
// clear data registers
byte clr = SPSR;
clr = SPDR;
}
void CC2500::reset()
{
// enable device
digitalWrite(m_pinCS, LOW);
delayMicroseconds(1);
// disable device and wait at least 40 microseconds
digitalWrite(m_pinCS, HIGH);
delayMicroseconds(41);
// enable device
digitalWrite(m_pinCS, LOW);
// wait for device
while (digitalRead(m_pinMISO) == HIGH) {
};
// send reset command (SRES)
spiTransfer(0x30);
// disable device
digitalWrite(m_pinCS, HIGH);
}
unsigned char CC2500::sendByte(unsigned char data)
{
// enable device
digitalWrite(m_pinCS, LOW);
// wait for device
while (digitalRead(m_pinMISO) == HIGH) {
};
// send byte
unsigned char result = spiTransfer(data);
// disable device
digitalWrite(m_pinCS, HIGH);
// return result
return result;
}
unsigned char CC2500::sendCommand(unsigned char command, unsigned char data)
{
// enable device
digitalWrite(m_pinCS, LOW);
// wait for device
while (digitalRead(m_pinMISO) == HIGH) {
};
// send command byte
spiTransfer(command);
// send data byte
unsigned char result = spiTransfer(data);
// disable device
digitalWrite(m_pinCS, HIGH);
// return result
return result;
}
unsigned char CC2500::sendStrobeCommand(unsigned char command)
{
// send command
return sendByte(command);
}
unsigned char CC2500::sendBurstCommand(unsigned char command, unsigned char* data,
unsigned char length)
{
// enable device
digitalWrite(m_pinCS, LOW);
// wait for device
while (digitalRead(m_pinMISO) == HIGH) {
};
// send command byte
spiTransfer(command);
unsigned char result = 0;
// send data bytes
for (int i=0; i<length; ++i) {
result = spiTransfer(data[i]);
data[i] = result;
}
// disable device
digitalWrite(m_pinCS, HIGH);
// return result
return result;
}
unsigned char CC2500::spiTransfer(volatile unsigned char data)
{
// start transmission
SPDR = data;
// wait for end of transmission
while (!(SPSR & (1<<SPIF))) {
};
// return received byte
return SPDR;
}
#endif