-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathairspy_calibrate.c
161 lines (142 loc) · 4.45 KB
/
airspy_calibrate.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
/*
* Copyright 2024 Benjamin Vernoux <bvernoux@gmail.com>
*
* This file is part of AirSpy.
*
* 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, 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; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include <airspy.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/types.h>
#include <time.h>
#ifndef bool
typedef int bool;
#define true 1
#define false 0
#endif
#define AIRSPY_FLASH_CALIB_OFFSET (0x20000) /* After 128KB (Reserved for Firmware + 64KB Spare) */
#define AIRSPY_FLASH_CALIB_HEADER (0xCA1B0001)
typedef struct
{
uint32_t header; /* Shall be equal to AIRSPY_FLASH_CALIB_HEADER */
uint32_t timestamp; /* Epoch Unix Time Stamp */
int32_t correction_ppb;
} airspy_calib_t;
static void usage()
{
printf("Usage:\n");
printf("\t-r: Read and display calibration data.\n");
printf("\t-w <calibration in ppb>: Erase and Write calibration in ppb.\n");
}
int main(int argc, char **argv)
{
int opt;
struct airspy_device* device = NULL;
int result;
bool read = false;
bool write = false;
int32_t calibration_ppb = 0;
airspy_calib_t calib;
while ((opt = getopt(argc, argv, "rw:")) != EOF)
{
switch (opt) {
case 'r':
read = true;
break;
case 'w':
write = true;
calibration_ppb = atoi(optarg);
break;
default:
fprintf(stderr, "opt error: %d\n", opt);
usage();
return EXIT_FAILURE;
}
}
if (write == read) {
if (write == true) {
fprintf(stderr, "Read and write options are mutually exclusive.\n");
} else {
fprintf(stderr, "Specify either read or write option.\n");
}
usage();
return EXIT_FAILURE;
}
result = airspy_init();
if (result != AIRSPY_SUCCESS) {
fprintf(stderr, "airspy_init() failed: %s (%d)\n", airspy_error_name(result), result);
return EXIT_FAILURE;
}
result = airspy_open(&device);
if (result != AIRSPY_SUCCESS) {
fprintf(stderr, "Failed to open airspy device.\n");
return 1;
}
if (read) {
printf("Reading %d bytes from 0x%06x.\n", (int)sizeof(calib), AIRSPY_FLASH_CALIB_OFFSET);
result = airspy_spiflash_read(device, AIRSPY_FLASH_CALIB_OFFSET, (int)sizeof(calib), (uint8_t *)&calib);
if (result != AIRSPY_SUCCESS) {
fprintf(stderr, "airspy_spiflash_read() failed: %s (%d)\n", airspy_error_name(result), result);
return EXIT_FAILURE;
}
time_t epoch_time = calib.timestamp;
struct tm *local_time = localtime(&epoch_time);
printf("Calibration timestamp: %04d/%02d/%02d %02d:%02d:%02d\nCalibration correction in ppb: %d\n",
local_time->tm_year + 1900,
local_time->tm_mon + 1,
local_time->tm_mday,
local_time->tm_hour,
local_time->tm_min,
local_time->tm_sec,
calib.correction_ppb);
}
if(write) {
printf("Erasing sector 2 (calibration) in SPI flash.\n");
result = airspy_spiflash_erase_sector(device, 2);
if (result != AIRSPY_SUCCESS) {
fprintf(stderr, "Failed to erase sector 2.\n");
return 1;
}
calib.header = AIRSPY_FLASH_CALIB_HEADER;
calib.timestamp = (uint32_t)time(NULL);
calib.correction_ppb = calibration_ppb;
printf("Writing calibration %d bytes at 0x%06x.\n", (int)sizeof(calib), AIRSPY_FLASH_CALIB_OFFSET);
time_t epoch_time = calib.timestamp;
struct tm *local_time = localtime(&epoch_time);
printf("Calibration timestamp: %04d/%02d/%02d %02d:%02d:%02d\nCalibration correction in ppb: %d\n",
local_time->tm_year + 1900,
local_time->tm_mon + 1,
local_time->tm_mday,
local_time->tm_hour,
local_time->tm_min,
local_time->tm_sec,
calib.correction_ppb);
result = airspy_spiflash_write(device, AIRSPY_FLASH_CALIB_OFFSET, sizeof(calib), (uint8_t *)&calib);
if (result != AIRSPY_SUCCESS) {
fprintf(stderr, "Failed to write calibration data.\n");
return 1;
}
}
result = airspy_close(device);
if (result != AIRSPY_SUCCESS) {
fprintf(stderr, "airspy_close() failed: %s (%d)\n", airspy_error_name(result), result);
return EXIT_FAILURE;
}
airspy_exit();
return EXIT_SUCCESS;
}