-
Notifications
You must be signed in to change notification settings - Fork 1
/
CC1200Demo.cpp
240 lines (202 loc) · 6.63 KB
/
CC1200Demo.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//
// Test suite for the CC1200 radio driver.
// Provides a menu to configure radio options, and
// attempts to send and receive a signal using two radios connected
// to the processor.
//
#include <mbed.h>
#include "CC1200DemoPins.h"
#include <CC1200.h>
#include <cinttypes>
CC1200 txRadio(PIN_SPI_MOSI, PIN_SPI_MISO, PIN_SPI_SCLK, PIN_TX_CS, PIN_TX_RST, stdout);
CC1200 rxRadio(PIN_SPI_MOSI, PIN_SPI_MISO, PIN_SPI_SCLK, PIN_RX_CS, PIN_RX_RST, stdout);
void checkExistance()
{
printf("Checking TX radio.....\n");
bool txSuccess = txRadio.begin();
printf("TX radio initialized: %s\n", txSuccess ? "true" : "false");
printf("Checking RX radio.....\n");
bool rxSuccess = rxRadio.begin();
printf("RX radio initialized: %s\n", rxSuccess ? "true" : "false");
}
void checkSignalTransmit()
{
printf("Initializing CC1200s.....\n");
txRadio.begin();
rxRadio.begin();
printf("Configuring RF settings.....\n");
int config=-1;
//MENU. ADD AN OPTION FOR EACH TEST.
printf("Select a config: \n");
// This is similar to the SmartRF "100ksps ARIB Standard" configuration, except it doesn't use zero-if
printf("1. 915MHz 100ksps 2-GFSK DEV=50kHz CHF=208kHz\n");
// This should be identical to the SmartRF "500ksps 4-GFSK Max Throughput ARIB Standard" configuration
printf("2. 915MHz 500ksps 4-GFSK DEV=400kHz CHF=1666kHz Zero-IF\n");
printf("3. 915MHz 38.4ksps 2-GFSK DEV=20kHz CHF=833kHz\n");
printf("4. 915MHz 100ksps 2-GFSK DEV=50kHz CHF=208kHz Fixed Length\n");
scanf("%d", &config);
printf("Running test with config %d:\n\n", config);
CC1200::PacketMode mode = CC1200::PacketMode::VARIABLE_LENGTH;
CC1200::Band band = CC1200::Band::BAND_820_960MHz;
float frequency = 915e6;
const float txPower = 0;
float fskDeviation;
float symbolRate;
float rxFilterBW;
CC1200::ModFormat modFormat;
CC1200::IFCfg ifCfg = CC1200::IFCfg::POSITIVE_DIV_8;
bool imageCompEnabled = true;
bool dcOffsetCorrEnabled = true;
uint8_t agcRefLevel;
uint8_t dcFiltSettingCfg = 1; // default chip setting
uint8_t dcFiltCutoffCfg = 4; // default chip setting
uint8_t agcSettleWaitCfg = 2;
CC1200::SyncMode syncMode = CC1200::SyncMode::SYNC_32_BITS;
uint32_t syncWord = 0x930B51DE; // default sync word
uint8_t syncThreshold = 8; // TI seems to recommend this threshold for most configs above 100ksps
uint8_t preableLengthCfg = 5; // default chip setting
uint8_t preambleFormatCfg = 0; // default chip setting
if(config == 1)
{
fskDeviation = 49896;
symbolRate = 100000;
rxFilterBW = 208300;
modFormat = CC1200::ModFormat::GFSK_2;
agcRefLevel = 0x2A;
}
else if(config == 2)
{
// SmartRF "500ksps 4-GFSK ARIB standard" config
fskDeviation = 399169;
symbolRate = 500000;
rxFilterBW = 1666700;
ifCfg = CC1200::IFCfg::ZERO;
imageCompEnabled = false;
dcOffsetCorrEnabled = true;
modFormat = CC1200::ModFormat::GFSK_4;
agcRefLevel = 0x2F;
}
else if(config == 3)
{
fskDeviation = 19989;
symbolRate = 38400;
rxFilterBW = 104200;
modFormat = CC1200::ModFormat::GFSK_2;
agcRefLevel = 0x27;
agcSettleWaitCfg = 1;
}
else if(config == 4)
{
fskDeviation = 49896;
symbolRate = 100000;
rxFilterBW = 208300;
modFormat = CC1200::ModFormat::GFSK_2;
agcRefLevel = 0x2A;
mode = CC1200::PacketMode::FIXED_LENGTH;
}
else
{
printf("Invalid config number!");
return;
}
for(CC1200 & radio : {std::ref(txRadio), std::ref(rxRadio)})
{
radio.configureFIFOMode();
radio.setPacketMode(mode);
radio.setModulationFormat(modFormat);
radio.setFSKDeviation(fskDeviation);
radio.setSymbolRate(symbolRate);
radio.setOutputPower(txPower);
radio.setRadioFrequency(band, frequency);
radio.setIFCfg(ifCfg, imageCompEnabled);
radio.configureDCFilter(dcOffsetCorrEnabled, dcFiltSettingCfg, dcFiltCutoffCfg);
radio.setRXFilterBandwidth(rxFilterBW);
radio.configureSyncWord(syncWord, syncMode, syncThreshold);
radio.configurePreamble(preableLengthCfg, preambleFormatCfg);
// AGC configuration (straight from SmartRF)
radio.setAGCReferenceLevel(agcRefLevel);
radio.setAGCSyncBehavior(CC1200::SyncBehavior::FREEZE_NONE);
radio.setAGCSettleWait(agcSettleWaitCfg);
if(ifCfg == CC1200::IFCfg::ZERO)
{
radio.setAGCGainTable(CC1200::GainTable::ZERO_IF, 11, 0);
}
else
{
// enable all AGC steps for NORMAL mode
radio.setAGCGainTable(CC1200::GainTable::NORMAL, 17, 0);
}
radio.setAGCHysteresis(0b10);
radio.setAGCSlewRate(0);
}
// configure on-transmit actions
txRadio.setOnTransmitState(CC1200::State::TX);
rxRadio.setOnReceiveState(CC1200::State::RX, CC1200::State::RX);
printf("Starting transmission.....\n");
txRadio.startTX();
rxRadio.startRX();
const char message[] = "Hello world!";
char receiveBuffer[sizeof(message)];
if(mode == CC1200::PacketMode::FIXED_LENGTH)
{
txRadio.setPacketLength(sizeof(message) - 1);
rxRadio.setPacketLength(sizeof(message) - 1);
}
while(true)
{
ThisThread::sleep_for(1s);
printf("\n---------------------------------\n");
printf("<<SENDING: %s\n", message);
txRadio.enqueuePacket(message, sizeof(message) - 1);
// update TX radio
printf("TX radio: state = 0x%" PRIx8 ", TX FIFO len = %zu",
static_cast<uint8_t>(txRadio.getState()), txRadio.getTXFIFOLen());
// wait a while for the packet to come in.
ThisThread::sleep_for(10ms);
// wait the time we expect the message to take, with a safety factor of 2
auto timeoutTime = std::chrono::microseconds(static_cast<int64_t>((1/symbolRate) * 1e-6f * sizeof(message) * 2));
Timer packetReceiveTimeout;
packetReceiveTimeout.start();
bool hasPacket;
do
{
hasPacket = rxRadio.hasReceivedPacket();
}
while(packetReceiveTimeout.elapsed_time() < timeoutTime && !hasPacket);
printf("RX radio: state = 0x%" PRIx8 ", RX FIFO len = %zu\n",
static_cast<uint8_t>(rxRadio.getState()), rxRadio.getRXFIFOLen());
if(hasPacket)
{
rxRadio.receivePacket(receiveBuffer, sizeof(message) - 1);
receiveBuffer[sizeof(message)-1] = '\0';
printf(">>RECEIVED: %s\n", receiveBuffer);
}
else
{
printf(">>No packet received!\n");
}
}
}
int main()
{
printf("\nCC1200 Radio Demo:\n");
while(1){
int test=-1;
//MENU. ADD AN OPTION FOR EACH TEST.
printf("Select a test: \n");
printf("1. Exit Test Suite\n");
printf("2. Check Existence\n");
printf("3. Try Transmitting Data\n");
scanf("%d", &test);
printf("Running test %d:\n\n", test);
//SWITCH. ADD A CASE FOR EACH TEST.
switch(test) {
case 1: printf("Exiting test suite.\n"); return 0;
case 2: checkExistance(); break;
case 3: checkSignalTransmit(); break;
default: printf("Invalid test number. Please run again.\n"); continue;
}
printf("done.\r\n");
}
return 0;
}