-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
416 lines (326 loc) · 11.4 KB
/
main.c
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/****************************************************************************
*
* Copyright 2021 Lee Mitchell <lee@indigopepper.com>
* This file is part of ADF435xCFG (ADF435x Configurator)
*
* ADF435xCFG (ADF435x Configurator) 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.
*
* ADF435xCFG (ADF435x Configurator) 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 ADF435xCFG (ADF435x Configurator). If not,
* see <http://www.gnu.org/licenses/>.
*
****************************************************************************/
/****************************************************************************/
/*** Include files ***/
/****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>
#include "common.h"
#ifdef _WIN32
#include <windows.h>
#endif
#include "ch341.h"
#include "adf435x.h"
/****************************************************************************/
/*** Macro Definitions ***/
/****************************************************************************/
/****************************************************************************/
/*** Type Definitions ***/
/****************************************************************************/
typedef enum
{
E_STATUS_OK,
E_STATUS_AGAIN,
E_STATUS_ERROR_TIMEOUT,
E_STATUS_ERROR_WRITING,
E_STATUS_OUT_OF_RANGE,
E_STATUS_NULL_PARAMETER,
E_STATUS_FAIL
} teStatus;
typedef enum{
E_VERBOSITY_LOW,
E_VERBOSITY_MEDIUM,
E_VERBOSITY_HIGH
} teVerbosity;
typedef struct
{
volatile bool_t bExitRequest;
uint64_t u64Frequency;
bool bSweepMode;
uint64_t u64FreqLow;
uint64_t u64FreqHigh;
uint64_t u64FreqStep;
teVerbosity eVerbosity;
int iDelay;
} tsInstance;
/****************************************************************************/
/*** Local Function Prototypes ***/
/****************************************************************************/
static void vParseCommandLineOptions(tsInstance *psInstance, int argc, char *argv[]);
#ifdef _WIN32
static BOOL WINAPI bCtrlHandler(DWORD dwCtrlType);
#endif
bool bConfigureADF435x(ADF435x_tsOptions *psOptions, uint64_t u64FrequencyHz);
/****************************************************************************/
/*** Exported Variables ***/
/****************************************************************************/
/****************************************************************************/
/*** Local Variables ***/
/****************************************************************************/
static tsInstance sInstance;
/****************************************************************************/
/*** Exported Functions ***/
/****************************************************************************/
/****************************************************************************
*
* NAME: main
*
* DESCRIPTION:
* Application entry point
*
* RETURNS:
* int
*
****************************************************************************/
int main(int argc, char *argv[])
{
int n;
bool_t bOk = TRUE;
char acOut[] = {0xaa, 0x55, 0x01, 0x02, 0x03, 0x04};
char acWord[4] = {0};
/* Initialise application state and set some defaults */
sInstance.bExitRequest = FALSE;
sInstance.eVerbosity = E_VERBOSITY_MEDIUM;
sInstance.u64Frequency = 50000000;
sInstance.bSweepMode = false;
sInstance.u64FreqLow = 50000000;
sInstance.u64FreqHigh = 100000000;
sInstance.u64FreqStep = 100000;
sInstance.iDelay = 1;
ADF435x_tsOptions sOptions;
printf("+----------------------------------------------------------------------+\n" \
"| ADF435xCFG (ADF435x Configurator) |\n" \
"| Copyright (C) 2021 Lee Mitchell <lee@indigopepper.com> |\n" \
"| |\n" \
"| This program comes with ABSOLUTELY NO WARRANTY. |\n" \
"| This is free software, and you are welcome to redistribute it |\n" \
"| under certain conditions; See the GNU General Public License |\n" \
"| version 3 or later for more details. You should have received a copy |\n" \
"| of the GNU General Public License along with this software), |\n" \
"| If not, see <http://www.gnu.org/licenses/>. |\n" \
"+----------------------------------------------------------------------+\n\n");
SetConsoleCtrlHandler(bCtrlHandler, TRUE);
// Initialise options struct for ADF435x device
ADF435x_vGetOptions(&sOptions);
// Parse the command line options
vParseCommandLineOptions(&sInstance, argc, argv);
// Initialise the CH4351A UAB to SPI adapter
if(!CH341DeviceInit())
{
printf("Error at line %d\n", __LINE__);
}
// Disable chip select
if(!CH341ChipSelect(0, FALSE))
{
printf("Error at line %d\n", __LINE__);
}
// Initialise ADF435x functions
ADF435x_vInit(E_ADF435X_VERBOSITY_LOW);
if(sInstance.bSweepMode)
{
/* Main program loop, execute until we get a signal requesting to exit */
while(!sInstance.bExitRequest)
{
for(uint64_t f = sInstance.u64FreqLow; (f <= sInstance.u64FreqHigh) && !sInstance.bExitRequest; f += sInstance.u64FreqStep)
{
printf("\r %d.%06dMHz ", f / 1000000, f % 1000000);
bConfigureADF435x(&sOptions, f);
Sleep(sInstance.iDelay);
}
}
// Switch the output off before we exit
sOptions.bOutputEnable = false;
bConfigureADF435x(&sOptions, 35000000);
}
else
{
bConfigureADF435x(&sOptions, sInstance.u64Frequency);
}
CH341DeviceRelease();
printf("\nDone!\n");
return EXIT_SUCCESS;
}
/****************************************************************************/
/*** Local Functions ***/
/****************************************************************************/
/****************************************************************************
*
* NAME: vParseCommandLineOptions
*
* DESCRIPTION:
* Parse command line options
*
* RETURNS:
* void
*
****************************************************************************/
static void vParseCommandLineOptions(tsInstance *psInstance, int argc, char *argv[])
{
int n;
int c;
int index, value, from, to, port;
static const struct option lopts[] = {
{ "freq", required_argument, 0, 'f' },
{ "sweep", no_argument, 0, 's' },
{ "low", required_argument, 0, 'l' },
{ "high", required_argument, 0, 'h' },
{ "resolution", required_argument, 0, 'r' },
{ "delay", required_argument, 0, 'd' },
{ "verbosity", required_argument, 0, 'v' },
{ "help", required_argument, 0, '?' },
{ NULL, 0, 0, 0 },
};
while(1)
{
c = getopt_long(argc, argv, "f:sl:h:r:d:v:?:h:", lopts, NULL);
if (c == -1)
break;
switch(c)
{
case 'f':
psInstance->u64Frequency = _atoi64(optarg);
printf("Frequency = %d.%dMHz\n", psInstance->u64Frequency / 1000000, psInstance->u64Frequency % 1000000);
break;
case 's':
psInstance->bSweepMode = true;
printf("Sweep mode enabled\n");
break;
case 'l':
psInstance->u64FreqLow = _atoi64(optarg);
printf("Low Frequency = %d.%dMHz\n", psInstance->u64FreqLow / 1000000, psInstance->u64FreqLow % 1000000);
break;
case 'h':
psInstance->u64FreqHigh = _atoi64(optarg);
printf("High Frequency = %d.%dMHz\n", psInstance->u64FreqHigh / 1000000, psInstance->u64FreqHigh % 1000000);
break;
case 'r':
psInstance->u64FreqStep = _atoi64(optarg);
printf("Step Frequency = %d.%dMHz\n", psInstance->u64FreqStep / 1000000, psInstance->u64FreqStep % 1000000);
break;
case 'd':
psInstance->iDelay = atoi(optarg);
printf("Step Delay = %dms\n", psInstance->iDelay);
break;
case 'v':
switch(atoi(optarg))
{
case 0:
psInstance->eVerbosity = E_VERBOSITY_LOW;
break;
case 1:
psInstance->eVerbosity = E_VERBOSITY_MEDIUM;
break;
case 2:
psInstance->eVerbosity = E_VERBOSITY_HIGH;
break;
default:
psInstance->eVerbosity = E_VERBOSITY_HIGH;
break;
}
break;
case '?':
default:
printf("\nUsage: %s <options>\n\n", argv[0]);
puts(" -f --frequency <freq> Set the output frequency to <freq> Hz\n\n"
" -s --sweep Enable sweep mode\n\n"
" -l --low <freq> Set the sweep lower frequency to <freq> Hz\n\n"
" -h --high <freq> Set the sweep upper frequency to <freq> Hz\n\n"
" -r --resolution <freq> Set the sweep step frequency to <freq> Hz\n\n"
" -d --delay <delay> Set the sweep mode step delay to <delay> milliseconds\n\n"
// " -v --verbosity <level> Set verbosity level 0, 1 & 2 are valid\n\n"
" -? --help Display help\n\n"
);
exit(EXIT_FAILURE);
break;
}
}
}
/****************************************************************************
*
* NAME: bCtrlHandler
*
* DESCRIPTION:
* Handles Ctrl+C events
*
* RETURNS:
* BOOL
*
****************************************************************************/
#ifdef _WIN32
static BOOL WINAPI bCtrlHandler(DWORD dwCtrlType)
{
switch (dwCtrlType)
{
case CTRL_C_EVENT:
case CTRL_SHUTDOWN_EVENT:
case CTRL_BREAK_EVENT:
printf("\nExit requested\n");
sInstance.bExitRequest = TRUE;
return TRUE;
default:
return FALSE;
}
}
#endif
bool bConfigureADF435x(ADF435x_tsOptions *psOptions, uint64_t u64FrequencyHz)
{
char acWord[4] = {0};
ADF435X_tsSettings sSettings;
ADF435X_tuRegisters uRegisters;
// Generate calculated settings, exit if there is a problem
if(!ADF435x_bCalculateSettings(u64FrequencyHz, psOptions, &sSettings))
{
return false;
}
// Calculate the register values, exit if there is a problem
if(!ADF435x_bGenerateRegisters(psOptions, &sSettings, &uRegisters))
{
return false;
}
// All good if here so write the 6 registers in order R5, R4, R3, R2, R1 and R0
for(int n = 6; n > 0; n--)
{
// printf("Sending %d - %08x\n", n-1, uRegisters.au32[n-1]);
acWord[0] = (uRegisters.au32[n-1] >> 24) & 0xff;
acWord[1] = (uRegisters.au32[n-1] >> 16) & 0xff;
acWord[2] = (uRegisters.au32[n-1] >> 8) & 0xff;
acWord[3] = (uRegisters.au32[n-1] >> 0) & 0xff;
if(!CH341ChipSelect(0, TRUE))
{
printf("Error at line %d\n", __LINE__);
}
if(CH341WriteSPI(acWord, 4) <= 0)
{
printf("Error at line %d\n", __LINE__);
}
if(!CH341ChipSelect(0, FALSE))
{
printf("Error at line %d\n", __LINE__);
}
}
return true;
}
/****************************************************************************/
/*** END OF FILE ***/
/****************************************************************************/