Debian Codegraph @

host/common/test/dc_calibration/src/main.c

raw symbols SCIP
1 /*
2 * This file is part of the bladeRF project:
3 * http://www.github.com/nuand/bladeRF
4 *
5 * Copyright (c) 2015 Nuand LLC
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <limits.h>
30 #include <inttypes.h>
31
32 #include <libbladeRF.h>
33 #include "dc_calibration.h"
34 #include "conversions.h"
35
36 const struct numeric_suffix freq_suffixes[] = {
37 { "G", 1000 * 1000 * 1000 },
38 { "GHz", 1000 * 1000 * 1000 },
39 { "M", 1000 * 1000 },
40 { "MHz", 1000 * 1000 },
41 { "k", 1000 } ,
42 { "kHz", 1000 }
43 };
44 #define NUM_FREQ_SUFFIXES (sizeof(freq_suffixes) / sizeof(freq_suffixes[0]))
45
46 void usage(const char *argv0) {
47 printf("Usage: %s <LMS6 cal>\n", argv0);
48 printf("Usage: %s <rx|tx> <frequency_low> [<step> <count>]\n", argv0);
49 printf("\n");
50 printf("To perform LMS6 calibrations, provide a single argument that is\n");
51 printf("one of the following:\n");
52 printf(" lpf_tuning, tx_lpf, rx_lpf, rx_vga2, all\n");
53 printf("\n");
54 printf("Otherwise, specify either 'rx' or 'tx' and the desired frequency.\n");
55 printf("A step size and count may be specified to calibrate over a range.\n");
56 printf("The results will be printed to stdout.\n");
57 printf("\n");
58 }
59
60 int main(int argc, char *argv[])
61 {
62 int status = 0;
63 struct bladerf *dev = NULL;
64 bladerf_module module;
65
66 if (argc < 2 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
67 usage(argv[0]);
68 return 0;
69 }
70
71 status = bladerf_open(&dev, NULL);
72 if (status != 0) {
73 fprintf(stderr, "Unable to open device: %s\n", bladerf_strerror(status));
74 return EXIT_FAILURE;
75 }
76
77 if (argc == 2) {
78 struct bladerf_lms_dc_cals cals;
79
80 status = dc_calibration_lms6(dev, argv[1]);
81 if (status == BLADERF_ERR_INVAL) {
82 fprintf(stderr, "Invalid LMS6 module: %s\n", argv[1]);
83 goto out;
84 } else if (status != 0) {
85 fprintf(stderr, "Calibration failed: %s\n", bladerf_strerror(status));
86 goto out;
87 }
88
89 status = bladerf_lms_get_dc_cals(dev, &cals);
90 if (status != 0) {
91 fprintf(stderr, "Failed to read LMS6 DC cals: %s\n",
92 bladerf_strerror(status));
93 goto out;
94 }
95
96 printf("LPF Tuning: %d\n", cals.lpf_tuning);
97 printf("TX LPF I: %d\n", cals.tx_lpf_i);
98 printf("TX LPF Q: %d\n", cals.tx_lpf_q);
99 printf("RX LPF I: %d\n", cals.rx_lpf_i);
100 printf("RX LPF Q: %d\n", cals.rx_lpf_q);
101 printf("RXVGA2 DC REF: %d\n", cals.dc_ref);
102 printf("RXVGA2 AI: %d\n", cals.rxvga2a_i);
103 printf("RXVGA2 AQ: %d\n", cals.rxvga2a_q);
104 printf("RXVGA2 BI: %d\n", cals.rxvga2b_i);
105 printf("RXVGA2 BQ: %d\n", cals.rxvga2b_q);
106
107 } else if (argc == 3 || argc == 5) {
108 bool ok;
109 unsigned int f_start;
110
111 module = str2module(argv[1]);
112 if (module == BLADERF_MODULE_INVALID) {
113 fprintf(stderr, "Invalid module: %s\n", argv[1]);
114 status = EXIT_FAILURE;
115 goto out;
116 }
117
118 f_start = str2uint_suffix(argv[2],
119 BLADERF_FREQUENCY_MIN, BLADERF_FREQUENCY_MAX,
120 freq_suffixes, NUM_FREQ_SUFFIXES,
121 &ok);
122
123 if (!ok) {
124 fprintf(stderr, "Invalid frequency: %s\n", argv[2]);
125 status = EXIT_FAILURE;
126 goto out;
127 }
128
129 if (argc == 3) {
130 struct dc_calibration_params p;
131 p.frequency = f_start;
132 p.corr_i = p.corr_q = 0;
133 p.error_i = p.error_q = 0;
134
135 status = dc_calibration(dev, module, &p, 1, false);
136 if (status == 0) {
137 printf("F=%10" PRIu64 ", Corr_I=%4d, Corr_Q=%4d, Error_I=%4.2f, Error_Q=%4.2f\n",
138 p.frequency, p.corr_i, p.corr_q, p.error_i, p.error_q);
139 } else {
140 fprintf(stderr, "Calibration failed: %s\n",
141 bladerf_strerror(status));
142 }
143 } else {
144 unsigned int f_inc, count, i;
145 struct dc_calibration_params *p;
146
147 f_inc = str2uint_suffix(argv[3], 1, UINT_MAX,
148 freq_suffixes, NUM_FREQ_SUFFIXES, &ok);
149 if (!ok) {
150 fprintf(stderr, "Invalid frequency increment: %s\n", argv[3]);
151 status = EXIT_FAILURE;
152 goto out;
153 }
154
155 count = str2uint(argv[4], 1, UINT_MAX, &ok);
156 if (!ok) {
157 fprintf(stderr, "Invalid count: %s\n", argv[4]);
158 status = EXIT_FAILURE;
159 goto out;
160 }
161
162 p = calloc(count, sizeof(p[0]));
163 for (i = 0; i < count; i++) {
164 p[i].frequency = f_start + i * f_inc;
165 }
166
167 if (p[count - 1].frequency <= BLADERF_FREQUENCY_MAX) {
168 status = dc_calibration(dev, module, p, count, true);
169 if (status == 0) {
170 for (i = 0; i < count; i++) {
171 printf("F=%10" PRIu64 ", Corr_I=%4d, Corr_Q=%4d, Error_I=%4.2f, Error_Q=%4.2f\n",
172 p[i].frequency, p[i].corr_i, p[i].corr_q,
173 p[i].error_i, p[i].error_q);
174 }
175 } else {
176 fprintf(stderr, "Calibration failed: %s\n",
177 bladerf_strerror(status));
178 }
179 } else {
180 fprintf(stderr,
181 "Error: Provided parameters yield out of range frequency.\n");
182 status = EXIT_FAILURE;
183 }
184
185 free(p);
186 }
187
188 } else {
189 fprintf(stderr, "Error: Invalid usage. Run with --help for more information.\n");
190 status = EXIT_FAILURE;
191 }
192
193 out:
194 bladerf_close(dev);
195
196 if (status != 0) {
197 status = EXIT_FAILURE;
198 }
199
200 return status;
201 }