-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlime_beidou.c
351 lines (293 loc) · 9.43 KB
/
lime_beidou.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
#include "lime_beidou.h"
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <lime/LimeSuite.h>
void init_sim(sim_t *s)
{
pthread_mutex_init(&(s->tx.lock), NULL);
//s->tx.error = 0;
pthread_mutex_init(&(s->beidou.lock), NULL);
s->beidou.ready = 0;
pthread_cond_init(&(s->beidou.initialization_done), NULL);
s->status = 0;
s->head = 0;
s->tail = 0;
s->sample_length = 0;
pthread_cond_init(&(s->fifo_write_ready), NULL);
pthread_cond_init(&(s->fifo_read_ready), NULL);
s->time = 0.0;
}
size_t get_sample_length(sim_t *s)
{
long length;
length = s->head - s->tail;
if (length < 0)
length += FIFO_LENGTH;
return((size_t)length);
}
size_t fifo_read(int16_t *buffer, size_t samples, sim_t *s)
{
size_t length;
size_t samples_remaining;
int16_t *buffer_current = buffer;
length = get_sample_length(s);
if (length < samples)
samples = length;
length = samples; // return value
samples_remaining = FIFO_LENGTH - s->tail;
if (samples > samples_remaining) {
memcpy(buffer_current, &(s->fifo[s->tail * 2]), samples_remaining * sizeof(int16_t) * 2);
s->tail = 0;
buffer_current += samples_remaining * 2;
samples -= samples_remaining;
}
memcpy(buffer_current, &(s->fifo[s->tail * 2]), samples * sizeof(int16_t) * 2);
s->tail += (long)samples;
if (s->tail >= FIFO_LENGTH)
s->tail -= FIFO_LENGTH;
return(length);
}
bool is_finished_generation(sim_t *s)
{
return s->finished;
}
int is_fifo_write_ready(sim_t *s)
{
int status = 0;
s->sample_length = get_sample_length(s);
if (s->sample_length < NUM_IQ_SAMPLES)
status = 1;
return(status);
}
void *tx_task(void *arg)
{
sim_t *s = (sim_t *)arg;
size_t samples_populated;
while (1) {
int16_t *tx_buffer_current = s->tx.buffer;
unsigned int buffer_samples_remaining = SAMPLES_PER_BUFFER;
while (buffer_samples_remaining > 0) {
pthread_mutex_lock(&(s->beidou.lock));
while (get_sample_length(s) == 0)
{
pthread_cond_wait(&(s->fifo_read_ready), &(s->beidou.lock));
}
samples_populated = fifo_read(tx_buffer_current,
buffer_samples_remaining,
s);
pthread_mutex_unlock(&(s->beidou.lock));
pthread_cond_signal(&(s->fifo_write_ready));
#if 0
if (is_finished_generation(s)
goto out;
#endif
// Advance the buffer pointer.
buffer_samples_remaining -= (unsigned int)samples_populated;
tx_buffer_current += (2 * samples_populated);
}
LMS_SendStream(&s->tx.stream, s->tx.buffer, SAMPLES_PER_BUFFER, NULL, 1000);
if (is_finished_generation(s))
goto out;
}
out:
return NULL;
}
int start_tx_task(sim_t *s)
{
int status;
status = pthread_create(&(s->tx.thread), NULL, tx_task, s);
return(status);
}
int start_beidou_task(sim_t *s)
{
int status;
status = pthread_create(&(s->beidou.thread), NULL, beidou_task, s);
return(status);
}
int main(){
sim_t s;
s.finished = false;
// 查找设备
int device_count = LMS_GetDeviceList(NULL);
// 增益
double gain = 0.1;
if (device_count < 1)
{
printf("ERROR: No device was found.\n");
exit(1);
}
else if (device_count > 1)
{
printf("ERROR: Found more than one device.\n");
exit(1);
}
// 查找设备
lms_info_str_t *device_list = malloc(sizeof(lms_info_str_t) * device_count);
device_count = LMS_GetDeviceList(device_list);
// Initialize simulator
init_sim(&s);
// 分配I/Q缓存
s.tx.buffer = (int16_t *)malloc(SAMPLES_PER_BUFFER * sizeof(int16_t) * 2);
if (s.tx.buffer == NULL)
{
printf("ERROR: 分配tx buffer失败.\n");
goto out;
}
// 分配fifo缓存
s.fifo = (int16_t *)malloc(FIFO_LENGTH * sizeof(int16_t) * 2);
if (s.fifo == NULL)
{
printf("ERROR: Failed to allocate I/Q sample buffer.\n");
goto out;
}
lms_device_t *device = NULL;
printf("Opening and initializing device...\n");
// 打开设备
if(LMS_Open(&device, device_list[0], NULL)){
printf("ERROR: Failed to open device: %s\n", device_list[0]);
goto out;
}
// 获取设备信息
const lms_dev_info_t *devinfo = LMS_GetDeviceInfo(device);
if (devinfo == NULL)
{
printf("ERROR: Failed to read device info: %s\n", LMS_GetLastErrorMessage());
goto out;
}
// 打印设备信息
printf("deviceName: %s\n", devinfo->deviceName);
printf("expansionName: %s\n", devinfo->expansionName);
printf("firmwareVersion: %s\n", devinfo->firmwareVersion);
printf("hardwareVersion: %s\n", devinfo->hardwareVersion);
printf("protocolVersion: %s\n", devinfo->protocolVersion);
printf("gatewareVersion: %s\n", devinfo->gatewareVersion);
printf("gatewareTargetBoard: %s\n", devinfo->gatewareTargetBoard);
int limeOversample = 1;
// 重置设备
int lmsReset = LMS_Reset(device);
if (lmsReset)
{
printf("ERROR: Failed to reset device: %s\n", LMS_GetLastErrorMessage());
goto out;
}
// 初始化设备
int lmsInit = LMS_Init(device);
if (lmsInit)
{
printf("ERROR: Failed to linitialize device: %s\n", LMS_GetLastErrorMessage());
goto out;
}
// 选择天线频道
int32_t channel = 0;
// 可用天线数量
int antenna_count = LMS_GetAntennaList(device, LMS_CH_TX, channel, NULL);
lms_name_t *antenna_name = malloc(sizeof(lms_name_t) * antenna_count);
// 获取发送端口的带宽
if (antenna_count > 0)
{
int i = 0;
lms_range_t *antenna_bw = malloc(sizeof(lms_range_t) * antenna_count);
LMS_GetAntennaList(device, LMS_CH_TX, channel, antenna_name);
for (i = 0; i < antenna_count; i++)
{
LMS_GetAntennaBW(device, LMS_CH_TX, channel, i, antenna_bw + i);
//printf("Channel %d, antenna [%s] has BW [%lf .. %lf] (step %lf)" "\n", channel, antenna_name[i], antenna_bw[i].min, antenna_bw[i].max, antenna_bw[i].step);
}
}
// 设置增益
LMS_SetNormalizedGain(device, LMS_CH_TX, channel, gain);
// 禁用其它频道
LMS_EnableChannel(device, LMS_CH_TX, 1 - channel, false);
LMS_EnableChannel(device, LMS_CH_RX, 0, false);
LMS_EnableChannel(device, LMS_CH_RX, 1, false);
// 开启要使用的Tx0频道
LMS_EnableChannel(device, LMS_CH_TX, channel, true);
int setLOFrequency = LMS_SetLOFrequency(device, LMS_CH_TX, channel, (double)TX_FREQUENCY);
if (setLOFrequency)
{
printf("ERROR: Failed to set TX frequency: %s\n", LMS_GetLastErrorMessage());
goto out;
}
// 设置采样率
lms_range_t sampleRateRange;
// 采样率范围
int getSampleRateRange = LMS_GetSampleRateRange(device, LMS_CH_TX, &sampleRateRange);
if (getSampleRateRange)
printf("Warning: Failed to get sample rate range: %s\n", LMS_GetLastErrorMessage());
int setSampleRate = LMS_SetSampleRate(device, (double)TX_SAMPLERATE, limeOversample);
if (setSampleRate)
{
printf("ERROR: Failed to set sample rate: %s\n", LMS_GetLastErrorMessage());
goto out;
}
double actualHostSampleRate = 0.0;
double actualRFSampleRate = 0.0;
// 获得采样速率
int getSampleRate = LMS_GetSampleRate(device, LMS_CH_TX, channel, &actualHostSampleRate, &actualRFSampleRate);
if (getSampleRate)
printf("Warnig: Failed to get sample rate: %s\n", LMS_GetLastErrorMessage());
else
printf("Sample rate: %.1lf Hz (Host) / %.1lf Hz (RF)" "\n", actualHostSampleRate, actualRFSampleRate);
// 自动校准
printf("Calibrating...\n");
int calibrate = LMS_Calibrate(device, LMS_CH_TX, channel, (double)TX_BANDWIDTH, 0);
if (calibrate)
printf("Warning: Failed to calibrate device: %s\n", LMS_GetLastErrorMessage());
// 设置Tx发送流
printf("Setup TX stream...\n");
s.tx.stream.channel = channel;
s.tx.stream.fifoSize = 1024 * 1024;
s.tx.stream.throughputVsLatency = 0.5;
s.tx.stream.isTx = true;
s.tx.stream.dataFmt = LMS_FMT_I12;
int setupStream = LMS_SetupStream(device, &s.tx.stream);
if (setupStream)
{
printf("ERROR: Failed to setup TX stream: %s\n", LMS_GetLastErrorMessage());
goto out;
}
// 启动Tx流
LMS_StartStream(&s.tx.stream);
// 发送北斗数据
s.status = start_beidou_task(&s);
if (s.status < 0) {
fprintf(stderr, "Failed to start BEIDOU task.\n");
goto out;
}
else
printf("Creating BEIDOU task...\n");
// Wait until beidou task is initialized
pthread_mutex_lock(&(s.tx.lock));
while (!s.beidou.ready)
pthread_cond_wait(&(s.beidou.initialization_done), &(s.tx.lock));
pthread_mutex_unlock(&(s.tx.lock));
// Fillfull the FIFO.
if (is_fifo_write_ready(&s))
pthread_cond_signal(&(s.fifo_write_ready));
// Start TX task
s.status = start_tx_task(&s);
if (s.status < 0) {
fprintf(stderr, "Failed to start TX task.\n");
goto out;
}
else
printf("Creating TX task...\n");
printf("Running...\n" "Press Ctrl+C to abort.\n");
// Wainting for TX task to complete.
pthread_join(s.tx.thread, NULL);
printf("\nDone!\n");
out:
// 关闭Tx流
LMS_StopStream(&s.tx.stream);
LMS_DestroyStream(device, &s.tx.stream);
if (s.tx.buffer != NULL)
free(s.tx.buffer);
if (s.fifo != NULL)
free(s.fifo);
// 关闭设备
printf("\nClosing device...\n");
LMS_EnableChannel(device, LMS_CH_TX, channel, false);
LMS_Close(device);
return 0;
}