Debian Codegraph @

hdl/fpga/platforms/bladerf-micro/software/bladeRF_nios/src/bladeRF_nios.c

raw symbols SCIP
1 /* This file is part of the bladeRF project:
2 * http://www.github.com/nuand/bladeRF
3 *
4 * Copyright (c) 2015 Nuand LLC
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 /* Define this to run this code on a PC instead of the NIOS II */
26 //#define BLADERF_NULL_HARDWARE
27
28 /* Will send debug alt_printf info to JTAG console while running on NIOS.
29 * This can slow performance and cause timing issues... be careful. */
30 //#define BLADERF_NIOS_DEBUG
31
32 #include <stdlib.h>
33 #include <stdint.h>
34 #include <stdbool.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 #include "devices.h"
39 #include "pkt_handler.h"
40 #include "pkt_8x8.h"
41 #include "pkt_8x16.h"
42 #include "pkt_8x32.h"
43 #include "pkt_8x64.h"
44 #include "pkt_16x64.h"
45 #include "pkt_32x32.h"
46 #include "pkt_retune2.h"
47 #include "pkt_legacy.h"
48 #include "debug.h"
49
50 #define BLADERF_DEVICE_NAME "Nuand bladeRF 2.0 Micro"
51
52 #ifdef BLADERF_NIOS_PC_SIMULATION
53 extern bool run_nios;
54 # define HAVE_REQUEST() ({ \
55 have_request = !have_request; \
56 if (have_request) { \
57 command_uart_read_request( (uint8_t*) pkt.req); \
58 } \
59 have_request; \
60 })
61
62 /* We need to reset the response buffer to known values so we can
63 * compare against expected test case responses */
64 # define RESET_RESPONSE_BUF
65
66 #else
67 # define run_nios true
68 # define HAVE_REQUEST() (pkt.ready == true)
69 #endif
70
71 #ifdef RESET_RESPONSE_BUF
72 # undef RESET_RESPONSE_BUF
73 # define RESET_RESPONSE_BUF() do { \
74 memset(pkt.resp, 0xff, NIOS_PKT_LEN); \
75 } while (0)
76
77 #else
78 # define RESET_RESPONSE_BUF() do {} while (0)
79 #endif
80
81 /* When adding packet handlers here, you must also ensure that you update
82 * the bladeRF/hdl/fpga/ip/nuand/command_uart/vhdl/command_uart.vhd
83 * to include the magic header byte value in the `magics` array.
84 */
85 static const struct pkt_handler pkt_handlers[] = {
86 PKT_RETUNE2,
87 PKT_8x8,
88 PKT_8x16,
89 PKT_8x32,
90 PKT_8x64,
91 PKT_16x64,
92 PKT_32x32,
93 PKT_LEGACY,
94 };
95
96 /* A structure that represents a point on a line. Used for calibrating
97 * the VCTCXO */
98 typedef struct point {
99 int32_t x; // Error counts
100 uint16_t y; // DAC count
101 } point_t;
102
103 typedef struct line {
104 point_t point[2];
105 int32_t slope;
106 uint16_t y_intercept; // in DAC counts
107 } line_t;
108
109 /* State machine for VCTCXO tuning */
110 typedef enum state {
111 COARSE_TUNE_MIN,
112 COARSE_TUNE_MAX,
113 COARSE_TUNE_DONE,
114 FINE_TUNE,
115 DO_NOTHING
116 } state_t;
117
118 int main(void)
119 {
120 DBG(BLADERF_DEVICE_NAME " FPGA v%x.%x.%x\n",
121 FPGA_VERSION_MAJOR, FPGA_VERSION_MINOR, FPGA_VERSION_PATCH);
122 DBG("Built " __DATE__ " " __TIME__ " with love <3\n");
123 #ifdef BLADERF_NIOS_LIBAD936X
124 DBG("libad936x found: This FPGA image has magic transgirl powers\n");
125 #endif // BLADERF_NIOS_LIBAD936X
126
127 uint8_t i;
128
129 /* Pointer to currently active packet handler */
130 const struct pkt_handler *handler;
131
132 struct pkt_buf pkt;
133 struct vctcxo_tamer_pkt_buf vctcxo_tamer_pkt;
134
135 /* Marked volatile to ensure we actually read the byte populated by
136 * the UART ISR */
137 const volatile uint8_t *magic = &pkt.req[PKT_MAGIC_IDX];
138
139 volatile bool have_request = false;
140
141 #ifdef BLADERF_NIOS_DEBUG
142 // Twiddler: gratuitous screen placebo
143 size_t const TWIDDLE_DELAY_CONSTANT = 100000;
144 size_t twiddle_count = 0;
145 enum {
146 TWIDDLE_STATE_PIPE = '|',
147 TWIDDLE_STATE_FWD_SLASH = '/',
148 TWIDDLE_STATE_HYPHEN = '-',
149 TWIDDLE_STATE_BACKSLASH = '\\',
150 } twiddle_state = TWIDDLE_STATE_PIPE;
151 #endif
152
153 // Trim DAC constants
154 const uint16_t trimdac_min = 0x28F5;
155 const uint16_t trimdac_max = 0xF5C3;
156
157 // Trim DAC calibration line
158 line_t trimdac_cal_line;
159
160 // VCTCXO Tune State machine
161 state_t tune_state = COARSE_TUNE_MIN;
162
163 // Set the known/default values of the trim DAC cal line
164 trimdac_cal_line.point[0].x = 0;
165 trimdac_cal_line.point[0].y = trimdac_min;
166 trimdac_cal_line.point[1].x = 0;
167 trimdac_cal_line.point[1].y = trimdac_max;
168 trimdac_cal_line.slope = 0;
169 trimdac_cal_line.y_intercept = 0;
170
171 /* Sanity check */
172 ASSERT(PKT_MAGIC_IDX == 0);
173
174 memset(&pkt, 0, sizeof(pkt));
175 pkt.ready = false;
176 bladerf_nios_init(&pkt, &vctcxo_tamer_pkt);
177
178 /* Initialize packet handlers */
179 for (i = 0; i < ARRAY_SIZE(pkt_handlers); i++) {
180 if (pkt_handlers[i].init != NULL) {
181 pkt_handlers[i].init();
182 }
183 }
184
185 /* ====================
186 * AD9361 SPI TESTS
187 * ==================== */
188 #ifdef BLADERF_NIOS_AD9361_SPI_TESTS
189 uint16_t adi_spi_addr;
190 uint64_t adi_spi_data;
191
192 while ( 1 ) {
193 // W/Rb | NB2:0 | A[9:0]
194 adi_spi_addr = (0x0 << 15) | (0x0 << 12) | (0x000 << 0);
195 adi_spi_data = UINT64_C(0x0);
196
197 // Read AD9361 registers 0x045-0x04c
198 for( i=0x45; i < 0x4d; i++ ) {
199 adi_spi_addr = (adi_spi_addr & 0xfc00) | i;
200 adi_spi_data = adi_spi_read(adi_spi_addr);
201 }
202
203 // Read 0x028
204 adi_spi_addr = (adi_spi_addr & 0xfc00) | 0x28;
205 adi_spi_data = adi_spi_read(adi_spi_addr);
206
207 // Write 0x5a to 0x028
208 adi_spi_addr = (adi_spi_addr & 0xfc00) | 0x28;
209 adi_spi_data = (UINT64_C(0x5a) << (64-8));
210 adi_spi_write((adi_spi_addr | 0x8000), adi_spi_data);
211
212 // Read 0x028
213 adi_spi_addr = (adi_spi_addr & 0xfc00) | 0x28;
214 adi_spi_data = adi_spi_read(adi_spi_addr);
215
216 // Write 0x5a to 0x028
217 adi_spi_addr = (adi_spi_addr & 0xfc00) | 0x28;
218 adi_spi_data = (UINT64_C(0x0) << (64-8));
219 adi_spi_write((adi_spi_addr | 0x8000), adi_spi_data);
220
221 usleep(100);
222 }
223 #endif
224
225
226 /* ====================
227 * AD5621 SPI TESTS
228 * ==================== */
229 #ifdef BLADERF_NIOS_AD5621_SPI_TESTS
230 uint16_t dac_val;
231
232 // Disable the ADF400x
233 control_reg_write( 0x0 );
234 usleep(2000000);
235 while( 1 ) {
236 for( i = 0; i < 6; i++ ) {
237 // Calculate DAC value
238 dac_val = ((((uint16_t)i) * 0x333) << 2) & 0x3fff;
239 // Write DAC value
240 ad56x1_vctcxo_trim_dac_write(dac_val);
241 usleep(2000000);
242 // Tristate the DAC, keeping same DAC value in register
243 ad56x1_vctcxo_trim_dac_write(dac_val | 0xc000 );
244 usleep(2000000);
245 }
246 }
247 #endif
248
249
250 /* ====================
251 * ADF4001 SPI TESTS
252 * ==================== */
253 #ifdef BLADERF_NIOS_ADF4001_SPI_TESTS
254 // Tristate the DAC
255 ad56x1_vctcxo_trim_dac_write( 0xc000 );
256 while( 1 ) {
257
258 // Enable the ADF400x
259 control_reg_write( 0x1 << 11 );
260 usleep(2000000);
261
262 // 0x000003 -- MUXOUT = 'Z'; CP = Normal
263 adf400x_spi_write(0x3);
264 usleep(2000000);
265
266 // 0x000137 -- MUXOUT = AVDD (3.3 V); CP = 'Z'
267 adf400x_spi_write(0x137);
268 usleep(2000000);
269
270 // 0x000177 -- MUXOUT = DGND; CP = 'Z'
271 adf400x_spi_write(0x177);
272 usleep(2000000);
273
274 // 0x000167 -- MUXOUT = SDO; CP = 'Z'
275 //adf400x_spi_write(0x167);
276 //usleep(2000000);
277
278 for( i = 0; i < 4; i++ ) {
279 // Read all the registers
280 adf400x_spi_read((uint32_t)i);
281 usleep(100);
282 }
283
284 // Disable the ADF400x
285 control_reg_write( 0x0 );
286 usleep(2000000);
287 }
288 #endif
289
290 DBG("=== System Ready ===\n");
291
292 while (run_nios) {
293 have_request = HAVE_REQUEST();
294
295 #ifdef BLADERF_NIOS_DEBUG
296 if (have_request) {
297 twiddle_count = 0;
298 } else if (TWIDDLE_DELAY_CONSTANT == ++twiddle_count) {
299 DBG("%c\b", twiddle_state);
300
301 switch (twiddle_state) {
302 case TWIDDLE_STATE_PIPE:
303 twiddle_state = TWIDDLE_STATE_FWD_SLASH;
304 break;
305 case TWIDDLE_STATE_FWD_SLASH:
306 twiddle_state = TWIDDLE_STATE_HYPHEN;
307 break;
308 case TWIDDLE_STATE_HYPHEN:
309 twiddle_state = TWIDDLE_STATE_BACKSLASH;
310 break;
311 case TWIDDLE_STATE_BACKSLASH:
312 twiddle_state = TWIDDLE_STATE_PIPE;
313 break;
314 }
315
316 twiddle_count = 0;
317 }
318 #endif
319
320 /* We have a command in the UART */
321 if (have_request) {
322 pkt.ready = false;
323 handler = NULL;
324
325 /* Determine which packet handler should receive this message */
326 for (i = 0; i < ARRAY_SIZE(pkt_handlers); i++) {
327 if (pkt_handlers[i].magic == *magic) {
328 handler = &pkt_handlers[i];
329 }
330 }
331
332 if (handler == NULL) {
333 /* We somehow got out of sync. Throw away request data until
334 * we hit a magic value */
335 DBG("Got invalid magic value: 0x%x\n", pkt.req[PKT_MAGIC_IDX]);
336 continue;
337 }
338
339 print_bytes("Request data:", pkt.req, NIOS_PKT_LEN);
340
341 /* If building with RESET_RESPONSE_BUF defined, reset response buffer
342 * contents to ensure unused values are known values. */
343 RESET_RESPONSE_BUF();
344
345 /* Process data and execute requested actions */
346 handler->exec(&pkt);
347
348 /* Write response to host */
349 command_uart_write_response(pkt.resp);
350 } else {
351
352 /* Temporarily putting the VCTCXO Calibration stuff here. */
353 if( vctcxo_tamer_pkt.ready ) {
354
355 vctcxo_tamer_pkt.ready = false;
356
357 switch(tune_state) {
358
359 case COARSE_TUNE_MIN:
360
361 /* Tune to the minimum DAC value */
362 vctcxo_trim_dac_write( 0x08, trimdac_min );
363
364 /* State to enter upon the next interrupt */
365 tune_state = COARSE_TUNE_MAX;
366
367 break;
368
369 case COARSE_TUNE_MAX:
370
371 /* We have the error from the minimum DAC setting, store it
372 * as the 'x' coordinate for the first point */
373 trimdac_cal_line.point[0].x = vctcxo_tamer_pkt.pps_1s_error;
374
375 /* Tune to the maximum DAC value */
376 vctcxo_trim_dac_write( 0x08, trimdac_max );
377
378 /* State to enter upon the next interrupt */
379 tune_state = COARSE_TUNE_DONE;
380
381 break;
382
383 case COARSE_TUNE_DONE:
384
385 /* We have the error from the maximum DAC setting, store it
386 * as the 'x' coordinate for the second point */
387 trimdac_cal_line.point[1].x = vctcxo_tamer_pkt.pps_1s_error;
388
389 /* We now have two points, so we can calculate the equation
390 * for a line plotted with DAC counts on the Y axis and
391 * error on the X axis. We want a PPM of zero, which ideally
392 * corresponds to the y-intercept of the line. */
393 trimdac_cal_line.slope = ( (trimdac_cal_line.point[1].y - trimdac_cal_line.point[0].y) /
394 (trimdac_cal_line.point[1].x - trimdac_cal_line.point[0].x) );
395 trimdac_cal_line.y_intercept = ( trimdac_cal_line.point[0].y -
396 (trimdac_cal_line.slope * trimdac_cal_line.point[0].x) );
397
398 /* Set the trim DAC count to the y-intercept */
399 vctcxo_trim_dac_write( 0x08, trimdac_cal_line.y_intercept );
400
401 /* State to enter upon the next interrupt */
402 tune_state = FINE_TUNE;
403
404 break;
405
406 case FINE_TUNE:
407
408 /* We should be extremely close to a perfectly tuned
409 * VCTCXO, but some minor adjustments need to be made */
410
411 /* Check the magnitude of the errors starting with the
412 * one second count. If an error is greater than the maxium
413 * tolerated error, adjust the trim DAC by the error (Hz)
414 * multiplied by the slope (in counts/Hz) and scale the
415 * result by the precision interval (e.g. 1s, 10s, 100s). */
416 if( vctcxo_tamer_pkt.pps_1s_error_flag ) {
417 vctcxo_trim_dac_write( 0x08, (vctcxo_trim_dac_value -
418 ((vctcxo_tamer_pkt.pps_1s_error * trimdac_cal_line.slope)/1)) );
419 } else if( vctcxo_tamer_pkt.pps_10s_error_flag ) {
420 vctcxo_trim_dac_write( 0x08, (vctcxo_trim_dac_value -
421 ((vctcxo_tamer_pkt.pps_10s_error * trimdac_cal_line.slope)/10)) );
422 } else if( vctcxo_tamer_pkt.pps_100s_error_flag ) {
423 vctcxo_trim_dac_write( 0x08, (vctcxo_trim_dac_value -
424 ((vctcxo_tamer_pkt.pps_100s_error * trimdac_cal_line.slope)/100)) );
425 }
426
427 break;
428
429 default:
430 break;
431
432 } /* switch */
433
434 /* Take PPS counters out of reset */
435 vctcxo_tamer_reset_counters( false );
436
437 /* Enable interrupts */
438 vctcxo_tamer_enable_isr( true );
439
440 } /* VCTCXO Tamer interrupt */
441
442 for (i = 0; i < ARRAY_SIZE(pkt_handlers); i++) {
443 if (pkt_handlers[i].do_work != NULL) {
444 pkt_handlers[i].do_work();
445 }
446 }
447 }
448 }
449
450 return 0;
451 }