-
Notifications
You must be signed in to change notification settings - Fork 0
/
radio_si446x.cpp
340 lines (238 loc) · 9.3 KB
/
radio_si446x.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* pecanpico2 Si446x Driver copyright (C) 2013 KT5TK
*
* 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.
*/
#include "config.h"
#include <math.h>
#include "radio_si446x.h"
#include <SPI.h>
#if defined(ARDUINO) && ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
unsigned int si446x_powerlevel = 0;
unsigned long active_freq = RADIO_FREQUENCY;
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(ADC2_PIN, ADC1_PIN); // RX, TX
void RadioSi446x::SendCmdReceiveAnswer(int byteCountTx, int byteCountRx, const char* pData)
{
/* There was a bug in A1 hardware that will not handle 1 byte commands.
It was supposedly fixed in B0 but the fix didn't make it at the last minute, so here we go again */
#ifdef DELETED_BY_W2CXM
if (byteCountTx == 1)
byteCountTx++;
#endif DELETED_BY_W2CXM
digitalWrite(SSpin,LOW);
char answer;
// Serial.print("sent: ");
// for (int j = 0; j < byteCountTx; j++) // Loop through the bytes of the pData
// {
// byte wordb = pData[j];
// Serial.print(wordb,HEX);
// Serial.print(" ");
// }
// Serial.println();
for (int j = 0; j < byteCountTx; j++) // Loop through the bytes of the pData
{
byte wordb = pData[j];
SPI.transfer(wordb);
}
digitalWrite(SSpin,HIGH);
delayMicroseconds(20);
digitalWrite(SSpin,LOW);
int reply = 0x00;
while (reply != 0xFF)
{
reply = SPI.transfer(0x44);
// Serial.print(reply,HEX);
// Serial.print(" ");
if (reply != 0xFF)
{
digitalWrite(SSpin,HIGH);
delayMicroseconds(20);
digitalWrite(SSpin,LOW);
}
}
// Serial.println();
// Serial.print("rx: ");
// for (int k = 1; k < byteCountRx; k++)
// {
// Serial.print(SPI.transfer(0x44), HEX);
// Serial.print(" ");
// }
digitalWrite(SSpin,HIGH);
// Serial.println();
delay(200); // Wait half a second to prevent Serial buffer overflow
}
// Actions ===============================================================
// Config reset ----------------------------------------------------------
void RadioSi446x::reset(void)
{
digitalWrite(VCXO_ENABLE_PIN,HIGH);
// Serial.println("VCXO is enabled");
delay(200);
digitalWrite(RADIO_SDN_PIN, HIGH); // active high shutdown = reset
delay(200);
digitalWrite(RADIO_SDN_PIN, LOW); // booting
// Serial.println("Radio is powered up");
// Start talking to the Si446X radio chip
const char PART_INFO_command[] = {0x01}; // Part Info
SendCmdReceiveAnswer(1, 9, PART_INFO_command);
// Serial.println("Part info was checked");
//W2CXM - Suggested that we add this delay by aadmson
delay(15);
//divide VCXO_FREQ into its bytes; MSB first
unsigned int x3 = VCXO_FREQ / 0x1000000;
unsigned int x2 = (VCXO_FREQ - x3 * 0x1000000) / 0x10000;
unsigned int x1 = (VCXO_FREQ - x3 * 0x1000000 - x2 * 0x10000) / 0x100;
unsigned int x0 = (VCXO_FREQ - x3 * 0x1000000 - x2 * 0x10000 - x1 * 0x100);
//POWER_UP
const char init_command[] = {0x02, 0x01, 0x01, x3, x2, x1, x0};// no patch, boot main app. img, FREQ_VCXO, return 1 byte
SendCmdReceiveAnswer(7, 1 ,init_command);
// Serial.println("Radio booted");
const char get_int_status_command[] = {0x20, 0x00, 0x00, 0x00}; // Clear all pending interrupts and get the interrupt status back
SendCmdReceiveAnswer(4, 9, get_int_status_command);
// Serial.println("Radio ready");
const char gpio_pin_cfg_command[] = {0x13, 0x02, 0x02, 0x02, 0x02, 0x08, 0x0b, 0x00}; // Set all GPIOs LOW; Link NIRQ to CTS; Link SDO to MISO; Max drive strength
SendCmdReceiveAnswer(8, 8, gpio_pin_cfg_command);
// W2CXM - Added two more initializations per aadamson
const char set_global_config1[] = {0x11, 0x00, 0x01, 0x03, 0x60};
SendCmdReceiveAnswer(5, 1, set_global_config1);
// cliPrint("Setting special global Config 1 changes (see WDS)\n");
const char set_global_xo_tune_command[] = {0x11, 0x00, 0x01, 0x00, 0x00};
SendCmdReceiveAnswer(5, 1, set_global_xo_tune_command);
// cliPrint("Setting no additional capacitance on VXCO\n");
// W2CXM ^^^^^^^^^
// Serial.println("LEDs should be switched off at this point");
setFrequency(active_freq);
// Serial.println("Frequency set");
setModem();
// Serial.println("CW mode set");
tune_tx();
// Serial.println("TX tune");
}
void RadioSi446x::start_tx()
{
// W2CXM Debug - overwrite power level
char set_pa_power_lvl_command[] = {0x11, 0x22, 0x01, 0x01, 0x7f};
SendCmdReceiveAnswer(5, 1, set_pa_power_lvl_command);
char change_state_command[] = {0x34, 0x07}; // Change to TX state
SendCmdReceiveAnswer(2, 1, change_state_command);
}
void RadioSi446x::stop_tx()
{
char change_state_command[] = {0x34, 0x03}; // Change to Ready state
SendCmdReceiveAnswer(2, 1, change_state_command);
}
void RadioSi446x::tune_tx()
{
char change_state_command[] = {0x34, 0x05}; // Change to TX tune state
SendCmdReceiveAnswer(2, 1, change_state_command);
}
// Configuration parameter functions ---------------------------------------
void RadioSi446x::setModem()
{
// Set to CW mode
// Serial.println("Setting modem into CW mode");
char set_modem_mod_type_command[] = {0x11, 0x20, 0x01, 0x00, 0x00};
SendCmdReceiveAnswer(5, 1, set_modem_mod_type_command);
}
void RadioSi446x::setFrequency(unsigned long freq)
{
// Set the output divider according to recommended ranges given in Si446x datasheet
int outdiv = 4;
int band = 0;
if (freq < 705000000UL) { outdiv = 6; band = 1;};
if (freq < 525000000UL) { outdiv = 8; band = 2;};
if (freq < 353000000UL) { outdiv = 12; band = 3;};
if (freq < 239000000UL) { outdiv = 16; band = 4;};
if (freq < 177000000UL) { outdiv = 24; band = 5;};
unsigned long f_pfd = 2 * VCXO_FREQ / outdiv;
unsigned int n = ((unsigned int)(freq / f_pfd)) - 1;
float ratio = (float)freq / (float)f_pfd;
float rest = ratio - (float)n;
unsigned long m = (unsigned long)(rest * 524288UL);
// set the band parameter
unsigned int sy_sel = 8; //
char set_band_property_command[] = {0x11, 0x20, 0x01, 0x51, (band + sy_sel)}; //
// send parameters
SendCmdReceiveAnswer(5, 1, set_band_property_command);
// Set the pll parameters
unsigned int m2 = m / 0x10000;
unsigned int m1 = (m - m2 * 0x10000) / 0x100;
unsigned int m0 = (m - m2 * 0x10000 - m1 * 0x100);
// assemble parameter string
//char set_frequency_property_command[] = {0x11, 0x40, 0x04, 0x00, n, m2, m1, m0};
// send parameters
//SendCmdReceiveAnswer(8, 1, set_frequency_property_command);
//W2CXM - suggestions from aadamson
const char set_freq_control_inte[] = {0x11, 0x40, 0x08, 0x00, n, m2, m1, m0, 0x0B, 0x61, 0x20, 0xFA};
SendCmdReceiveAnswer(12, 1, set_freq_control_inte);
//W2CXM ^^^^
}
// Public functions -----------------------------------------------------------
void RadioSi446x::setup()
{
// Not much radio configuration to do here
// because we initialize the transmitter each time right before we transmit a packet
// Just make sure that the VCXO is on and the transmitter chip is switched off at boot up
pinMode(VCXO_ENABLE_PIN, OUTPUT);
digitalWrite(VCXO_ENABLE_PIN,HIGH);
Serial.println("VCXO is enabled");
pinMode(RADIO_SDN_PIN, OUTPUT);
delay(300);
digitalWrite(RADIO_SDN_PIN, HIGH); // active high = shutdown
// Set up SPI
pinMode(SCKpin, OUTPUT);
pinMode(SSpin, OUTPUT);
pinMode(MOSIpin, OUTPUT);
digitalWrite(SSpin, HIGH); // ensure SS stays high for now
// initialize SPI:
SPI.begin();
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV8); // 8 MHz / 8 = 1 MHz
delay(200);
// Serial.println("SPI is initialized");
}
void RadioSi446x::ptt_on()
{
digitalWrite(VCXO_ENABLE_PIN, HIGH);
reset();
// turn on the blue LED (GPIO2) to indicate TX
char gpio_pin_cfg_command2[] = {0x13, 0x02, 0x02, 0x03, 0x02, 0x08, 0x0b, 0x00}; // Set GPIO2 HIGH; Link NIRQ to CTS; Link SDO to MISO; Max drive strength
SendCmdReceiveAnswer(8, 1, gpio_pin_cfg_command2);
start_tx();
si446x_powerlevel = 1023;
}
void RadioSi446x::ptt_off()
{
stop_tx();
si446x_powerlevel = 0;
// turn off the blue LED (GPIO2)
char gpio_pin_cfg_command0[] = {0x13, 0x02, 0x02, 0x02, 0x02, 0x08, 0x0b, 0x00}; // Set all GPIOs LOW; Link NIRQ to CTS; Link SDO to MISO; Max drive strength
SendCmdReceiveAnswer(8, 1, gpio_pin_cfg_command0);
digitalWrite(RADIO_SDN_PIN, HIGH); // active high = shutdown
//digitalWrite(VCXO_ENABLE_PIN, LOW); //keep enabled for now
}
void RadioSi446x::set_freq(unsigned long freq)
{
active_freq = freq;
}
int RadioSi446x::get_powerlevel()
{
return si446x_powerlevel;
}