-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRFgenBoardInit.c
More file actions
388 lines (350 loc) · 7.93 KB
/
Copy pathRFgenBoardInit.c
File metadata and controls
388 lines (350 loc) · 7.93 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/**
*Epsilon r Labs RF Generator 30MHz 9.8GHz
*Copyright (C) 2017 Jean Wlodarski
* KaZjjW at gmailcom
*
*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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
/*
* RFgenBoardInit.c
*
* Created: 11/3/2016 7:15:43 PM
* Author: Jean
*/
#include <asf.h>
#include <string.h>
#include "RFgen.h"
#include "LMX2592.h"
#include "scpi-def.h"
extern ui_t ui;
extern rfgen_t rfgen;
extern LMX2592_t LMX2592;
void RfgenBoardInit(void)
{
//RF Attenuator Port
PORTA_DIRSET = PIN0_bm;
PORTA_DIRSET = PIN1_bm;
PORTA_DIRSET = PIN2_bm;
PORTA_DIRSET = PIN3_bm;
PORTA_DIRSET = PIN4_bm;
PORTA_DIRSET = PIN5_bm;
PORTA_OUTSET = PIN0_bm;
PORTA_OUTSET = PIN1_bm;
PORTA_OUTSET = PIN2_bm;
PORTA_OUTSET = PIN3_bm;
PORTA_OUTSET = PIN4_bm;
PORTA_OUTSET = PIN5_bm;
//Encoder A and B
PORTA_DIRCLR = PIN6_bm;
PORTA_DIRCLR = PIN7_bm;
//PLL Chip Enable CE
PORTB_DIRSET = PIN0_bm;
PORTB_OUTCLR = PIN0_bm;
//Encoder E
PORTB_DIRCLR = PIN1_bm;
//PLL SPI
PORTC_DIRSET = PIN4_bm; //SS
PORTC_OUTSET = PIN4_bm; //SS
PORTC_DIRSET = PIN5_bm; //MOSI
PORTC_DIRCLR = PIN6_bm; //MISO
PORTC_DIRSET = PIN7_bm; //SCK
//RF Relay
PORTD_DIRSET = PIN3_bm;
PORTD_OUTCLR = PIN3_bm;
//-5V DCDC Converter SHUTDOWN
PORTD_DIRSET = PIN0_bm;
PORTD_OUTSET = PIN0_bm;
//Jack Power Present
PORTC_DIRCLR = PIN1_bm;
//USB Power Present
PORTD_DIRCLR = PIN2_bm;
//Front Panel Detection
PORTB_DIRSET = PIN2_bm;
PORTD_OUTCLR = PIN3_bm;
//+4.5 V Enable
PORTC_DIRSET = PIN0_bm;
PORTC_OUTCLR = PIN0_bm;
}
void string_digits_commas(char *str1, char *str2)
{
uint8_t i;
i = strlen(str1);
//we are using a /10 frequency
switch(i)
{
case 7:
str2[0] = ' ';
str2[1] = ' ';
str2[2] = ' ';
str2[3] = ' ';
str2[4] = str1[0]; //x0 MHz
str2[5] = str1[1];
str2[6] = ',';
str2[7] = str1[2];
str2[8] = str1[3];
str2[9] = str1[4];
str2[10] = ',';
str2[11] = str1[5];
str2[12] = str1[6];
str2[13] = 0;
break;
case 8:
str2[0] = ' ';
str2[1] = ' ';
str2[2] = ' ';
str2[3] = str1[0]; //x00 MHz
str2[4] = str1[1];
str2[5] = str1[2];
str2[6] = ',';
str2[7] = str1[3];
str2[8] = str1[4];
str2[9] = str1[5];
str2[10] = ',';
str2[11] = str1[6];
str2[12] = str1[7];
str2[13] = 0;
break;
case 9:
str2[0] = ' ';
str2[1] = str1[0]; //x GHz
str2[2] = ',';
str2[3] = str1[1];
str2[4] = str1[2];
str2[5] = str1[3];
str2[6] = ',';
str2[7] = str1[4];
str2[8] = str1[5];
str2[9] = str1[6];
str2[10] = ',';
str2[11] = str1[7];
str2[12] = str1[8];
str2[13] = 0;
break;
case 10:
str2[0] = str1[0]; //x0 GHz
str2[1] = str1[1];
str2[2] = ',';
str2[3] = str1[2];
str2[4] = str1[3];
str2[5] = str1[4];
str2[6] = ',';
str2[7] = str1[5];
str2[8] = str1[6];
str2[9] = str1[7];
str2[10] = ',';
str2[11] = str1[8];
str2[12] = str1[9];
str2[13] = 0;
break;
}
}
void string_digits_commas_dbm(char *str1, char *str2)
{
uint8_t i;
i = strlen(str1);
if(str1[0] == '-')
{
switch(i)
{
case 2:
str2[0] = str1[0];
str2[1] = ' ';
str2[2] = '0';
str2[3] = '.';
str2[4] = str1[1];
str2[5]= 0;
break;
case 3:
str2[0] = str1[0];
str2[1] = ' ';
str2[2] = str1[1];
str2[3] = '.';
str2[4] = str1[2];
str2[5]= 0;
break;
case 4:
str2[0] = str1[0];
str2[1] = str1[1];
str2[2] = str1[2];
str2[3] = '.';
str2[4] = str1[3];
str2[5]= 0;
break;
}
}
else
{
switch(i)
{
case 1:
str2[0] = ' ';
str2[1] = ' ';
str2[2] = '0';
str2[3] = '.';
str2[4] = str1[0];
str2[5]= 0;
break;
case 2:
str2[0] = ' ';
str2[1] = ' ';
str2[2] = str1[0];
str2[3] = '.';
str2[4] = str1[1];
str2[5]= 0;
break;
case 3:
str2[0] = ' ';
str2[1] = str1[0];
str2[2] = str1[1];
str2[3] = '.';
str2[4] = str1[2];
str2[5]= 0;
break;
}
}
}
void string_digits_commas_dbm_scpi(char *str1, char *str2)
{
uint8_t i;
i = strlen(str1);
if(str1[0] == '-')
{
switch(i)
{
case 2:
str2[0] = str1[0];
str2[1] = '0';
str2[2] = '.';
str2[3] = str1[1];
str2[4]= 0;
break;
case 3:
str2[0] = str1[0];
str2[1] = str1[1];
str2[2] = '.';
str2[3] = str1[2];
str2[4]= 0;
break;
case 4:
str2[0] = str1[0];
str2[1] = str1[1];
str2[2] = str1[2];
str2[3] = '.';
str2[4] = str1[3];
str2[5]= 0;
break;
}
}
else
{
switch(i)
{
case 1:
str2[0] = '0';
str2[1] = '.';
str2[2] = str1[0];
str2[3]= 0;
break;
case 2:
str2[0] = str1[0];
str2[1] = '.';
str2[2] = str1[1];
str2[3]= 0;
break;
case 3:
str2[0] = str1[0];
str2[1] = str1[1];
str2[2] = '.';
str2[3] = str1[2];
str2[4]= 0;
break;
}
}
}
size_t SCPI_Write(scpi_t * context, const char * data, size_t len) {
(void) context;
//return fwrite(data, 1, len, stdout);
return udi_cdc_write_buf(data, len);
}
scpi_result_t SCPI_Flush(scpi_t * context) {
(void) context;
return SCPI_RES_OK;
}
int SCPI_Error(scpi_t * context, int_fast16_t err) {
char rx_buffer[50];
(void) context;
//fprintf(stderr, "**ERROR: %d, \"%s\"\r\n", (int16_t) err, SCPI_ErrorTranslate(err));
udi_cdc_write_buf("**ERROR: ", 9);
itoa((int16_t)err, rx_buffer, 10);
udi_cdc_write_buf(rx_buffer,strlen(rx_buffer));
udi_cdc_putc(' ');
udi_cdc_write_buf(SCPI_ErrorTranslate(err),strlen(SCPI_ErrorTranslate(err)));
udi_cdc_write_buf("\r\n", 2);
return 0;
}
scpi_result_t SCPI_Control(scpi_t * context, scpi_ctrl_name_t ctrl, scpi_reg_val_t val) {
char rx_buffer[50];
(void) context;
if (SCPI_CTRL_SRQ == ctrl) {
//fprintf(stderr, "**SRQ: 0x%X (%d)\r\n", val, val);
udi_cdc_write_buf("**SRQ: ", 7);
utoa(val, rx_buffer, 10);
udi_cdc_write_buf(rx_buffer,strlen(rx_buffer));
udi_cdc_write_buf("\r\n", 2);
} else {
udi_cdc_write_buf("**CTRL ", 7);
utoa(ctrl, rx_buffer, 10);
udi_cdc_write_buf(rx_buffer,strlen(rx_buffer));
udi_cdc_putc(' ');
utoa(val, rx_buffer, 10);
udi_cdc_write_buf(rx_buffer,strlen(rx_buffer));
udi_cdc_write_buf("\r\n", 2);
//fprintf(stderr, "**CTRL %02x: 0x%X (%d)\r\n", ctrl, val, val);
}
return SCPI_RES_OK;
}
scpi_result_t SCPI_SystemCommTcpipControlQ(scpi_t * context) {
(void) context;
return SCPI_RES_ERR;
}
void RFgenReset(LMX2592_t *LMX2592, ui_t *ui, rfgen_t *rfgen)
{
rfgen->Frequency_10 = 810000000; //100MHz
rfgen->power_dbm = -100; //-10.0 dBm
ltoa(rfgen->Frequency_10,rfgen->Freq_string,10);
string_digits_commas(rfgen->Freq_string,ui->Freq_string_print);
itoa(rfgen->power_dbm,rfgen->Power_string,10);
string_digits_commas_dbm(rfgen->Power_string, ui->Power_string_print);
ui->refresh = 1;
ui->cur_pos = CUR_POS_MAIN_10MHZ;
ui->cur_x = CUR_POS_MAIN_10MHZ_X;
ui->cur_y = CUR_POS_MAIN_FREQ_Y;
LMX2592_init_config(LMX2592);
LMX2592_reset();
delay_ms(100);
LMX2592_defaults();
LMX2592_set_PFD(LMX2592, 100000000);
LMX2592_set_out_freq(LMX2592,rfgen->Frequency_10, 0xFF);
LMX2592_RFonOff(LMX2592, 0);
LMX2592_configure(LMX2592);
//LMX2592_write_24_reg(0x002304 );
}
scpi_result_t SCPI_Reset(scpi_t * context)
{
(void) context;
udi_cdc_write_buf( "**Reset\r\n", 9);
RFgenReset(&LMX2592, &ui, &rfgen);
return SCPI_RES_OK;
}