File: hiqsdr.c

package info (click to toggle)
hamlib 4.6.5-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,984 kB
  • sloc: ansic: 262,996; sh: 6,135; cpp: 1,578; perl: 876; makefile: 855; python: 148; awk: 58; xml: 26
file content (571 lines) | stat: -rw-r--r-- 14,412 bytes parent folder | download | duplicates (2)
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*
 *  Hamlib HiQSDR backend
 *  Copyright (c) 20012 by Stephane Fillod
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library 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
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */

#include "hamlib/rig.h"
#include "iofunc.h"
#include "token.h"

/*
 *  http://www.hiqsdr.org
 */

/* HiQSDR constants */

#define REFCLOCK 122880000
#define DEFAULT_SAMPLE_RATE 48000

/* V1.1 */
#define CTRL_FRAME_LEN 22

struct hiqsdr_priv_data
{
    split_t split;
    int sample_rate;
    double ref_clock;
    unsigned char control_frame[CTRL_FRAME_LEN];
};

static int hiqsdr_init(RIG *rig);
static int hiqsdr_cleanup(RIG *rig);
static int hiqsdr_open(RIG *rig);
static int hiqsdr_close(RIG *rig);

static int hiqsdr_set_freq(RIG *rig, vfo_t vfo, freq_t freq);
static int hiqsdr_get_freq(RIG *rig, vfo_t vfo, freq_t *freq);
static int hiqsdr_set_split_vfo(RIG *rig, vfo_t vfo, split_t split,
                                vfo_t tx_vfo);
static int hiqsdr_set_split_freq(RIG *rig, vfo_t vfo, freq_t tx_freq);
static int hiqsdr_set_mode(RIG *rig, vfo_t vfo, rmode_t mode, pbwidth_t width);
static int hiqsdr_set_ptt(RIG *rig, vfo_t vfo, ptt_t ptt);
static int hiqsdr_set_ant(RIG *rig, vfo_t vfo, ant_t ant, value_t option);

static int hiqsdr_set_conf(RIG *rig, hamlib_token_t token, const char *val);
static int hiqsdr_get_conf(RIG *rig, hamlib_token_t token, char *val);
static int hiqsdr_set_level(RIG *rig, vfo_t vfo, setting_t level, value_t val);
static int hiqsdr_get_level(RIG *rig, vfo_t vfo, setting_t level, value_t *val);


#define TOK_OSCFREQ TOKEN_BACKEND(1)
#define TOK_SAMPLE_RATE TOKEN_BACKEND(2)

const struct confparams hiqsdr_cfg_params[] =
{
    {
        TOK_OSCFREQ, "osc_freq", "Oscillator freq", "Oscillator frequency of reference clock in Hz",
        "122880000", RIG_CONF_NUMERIC, { .n = { 0, MHz(256), 1 } }
    },
    {
        TOK_SAMPLE_RATE, "sample_rate", "Sample rate", "Sample rate",
        "48000", RIG_CONF_NUMERIC, { /* .n = */ { 48000, 1920000, 1 } }
    },
    { RIG_CONF_END, NULL, }
};


/*
 * HiQSDR rig capabilities.
 */

#define HIQSDR_FUNC  RIG_FUNC_NONE
#define HIQSDR_LEVEL (RIG_LEVEL_RFPOWER|RIG_LEVEL_PREAMP|RIG_LEVEL_ATT)
#define HIQSDR_PARM   RIG_PARM_NONE
#define HIQSDR_VFO_OP RIG_OP_NONE
#define HIQSDR_SCAN   RIG_SCAN_NONE

#define HIQSDR_VFO (RIG_VFO_A)
#define HIQSDR_ANT (RIG_ANT_1|RIG_ANT_2)

#define HIQSDR_MODES (RIG_MODE_CW|RIG_MODE_DSB)

struct rig_caps hiqsdr_caps =
{
    RIG_MODEL(RIG_MODEL_HIQSDR),
    .model_name =     "HiQSDR",
    .mfg_name =       "N2ADR",
    .version =        "20200323.0",
    .copyright =      "LGPL",
    .status =         RIG_STATUS_BETA,
    .rig_type =       RIG_TYPE_TUNER,
    .targetable_vfo =  RIG_TARGETABLE_NONE,
    .ptt_type =       RIG_PTT_RIG,
    .dcd_type =       RIG_DCD_NONE,
    .port_type =      RIG_PORT_UDP_NETWORK,
    .timeout =        500,
    .has_get_func =   HIQSDR_FUNC,
    .has_set_func =   HIQSDR_FUNC,
    .has_get_level =  HIQSDR_LEVEL,
    .has_set_level =  RIG_LEVEL_SET(HIQSDR_LEVEL),
    .has_get_parm =    HIQSDR_PARM,
    .has_set_parm =    RIG_PARM_SET(HIQSDR_PARM),
    .ctcss_list =      NULL,
    .dcs_list =        NULL,
    .chan_list =   { RIG_CHAN_END, },
    .scan_ops =    HIQSDR_SCAN,
    .vfo_ops =     HIQSDR_VFO_OP,
    .transceive =     RIG_TRN_OFF,
    .attenuator =     { 2, 4, 6, 10, 20, 30, 44, RIG_DBLST_END }, // -2dB steps in fact
    .preamp =      { 10, RIG_DBLST_END, },    // TODO

    .rx_range_list1 =  { {
            .startf = kHz(100), .endf = MHz(66), .modes = HIQSDR_MODES,
            .low_power = -1, .high_power = -1, HIQSDR_VFO, HIQSDR_ANT
        },
        RIG_FRNG_END,
    },
    .tx_range_list1 =  { {
            .startf = kHz(100), .endf = MHz(66), .modes = HIQSDR_MODES,
            .low_power = mW(1), .high_power = mW(50), HIQSDR_VFO, HIQSDR_ANT
        },
        RIG_FRNG_END,
    },
    .rx_range_list2 =  { {
            .startf = kHz(100), .endf = MHz(66), .modes = HIQSDR_MODES,
            .low_power = -1, .high_power = -1, HIQSDR_VFO, HIQSDR_ANT
        },
        RIG_FRNG_END,
    },
    .tx_range_list2 =  { {
            .startf = kHz(100), .endf = MHz(66), .modes = HIQSDR_MODES,
            .low_power = mW(1), .high_power = mW(50), HIQSDR_VFO, HIQSDR_ANT
        },
        RIG_FRNG_END,
    },
    .tuning_steps =  { {HIQSDR_MODES, 1}, RIG_TS_END, },
    .filters =      {
        {RIG_MODE_CW, kHz(2.4)},
        {HIQSDR_MODES, RIG_FLT_ANY},
        RIG_FLT_END,
    },

    .priv =  NULL,

    .rig_init =     hiqsdr_init,
    .rig_cleanup =  hiqsdr_cleanup,
    .rig_open =     hiqsdr_open,
    .rig_close =    hiqsdr_close,

    .cfgparams =    hiqsdr_cfg_params,
    .set_conf =     hiqsdr_set_conf,
    .get_conf =     hiqsdr_get_conf,

    .set_freq =     hiqsdr_set_freq,
    .get_freq =     hiqsdr_get_freq,

    .set_split_freq = hiqsdr_set_split_freq,
    .set_split_vfo = hiqsdr_set_split_vfo,

    .set_mode =     hiqsdr_set_mode,

    .set_ptt =      hiqsdr_set_ptt,
    .set_ant =      hiqsdr_set_ant,

    .set_level =    hiqsdr_set_level,
    .get_level =    hiqsdr_get_level,
    .hamlib_check_rig_caps = HAMLIB_CHECK_RIG_CAPS
};


static int send_command(RIG *rig)
{
    const struct hiqsdr_priv_data *priv = (struct hiqsdr_priv_data *)
                                          STATE(rig)->priv;
    int ret;

    ret = write_block(RIGPORT(rig), (unsigned char *) priv->control_frame,
                      CTRL_FRAME_LEN);
#if 0
    ret = read_block(RIGPORT(rig), (unsigned char *) priv->control_frame,
                     CTRL_FRAME_LEN);

    if (ret != CTRL_FRAME_LEN)
    {
        ret = ret < 0 ? ret : -RIG_EPROTO;
    }

#endif

    return ret;
}

static unsigned compute_sample_rate(const struct hiqsdr_priv_data *priv)
{
    unsigned rx_control;

    rx_control = (unsigned)(priv->ref_clock / (8. * 8. * priv->sample_rate)) - 1;

    if (rx_control > 39)
    {
        rx_control = 39;
    }

    return rx_control;
}

/*
 * Assumes rig!=NULL, STATE(rig)->priv!=NULL
 */
int hiqsdr_set_conf(RIG *rig, hamlib_token_t token, const char *val)
{
    struct hiqsdr_priv_data *priv;
    struct rig_state *rs;

    rs = STATE(rig);
    priv = (struct hiqsdr_priv_data *)rs->priv;

    switch (token)
    {
    case TOK_OSCFREQ:
        priv->ref_clock = atof(val);
        priv->control_frame[12] = compute_sample_rate(priv);
        break;

    case TOK_SAMPLE_RATE:
        priv->sample_rate = atoi(val);
        priv->control_frame[12] = compute_sample_rate(priv);
        break;

    default:
        return -RIG_EINVAL;
    }

    return RIG_OK;
}

/*
 * assumes rig!=NULL,
 * Assumes rig!=NULL, STATE(rig)->priv!=NULL
 *  and val points to a buffer big enough to hold the conf value.
 */
int hiqsdr_get_conf2(RIG *rig, hamlib_token_t token, char *val, int val_len)
{
    struct hiqsdr_priv_data *priv;
    struct rig_state *rs;

    rs = STATE(rig);
    priv = (struct hiqsdr_priv_data *)rs->priv;

    switch (token)
    {
    case TOK_OSCFREQ:
        SNPRINTF(val, val_len, "%f", priv->ref_clock);
        break;

    case TOK_SAMPLE_RATE:
        SNPRINTF(val, val_len, "%d", priv->sample_rate);
        break;

    default:
        return -RIG_EINVAL;
    }

    return RIG_OK;
}

int hiqsdr_get_conf(RIG *rig, hamlib_token_t token, char *val)
{
    return hiqsdr_get_conf2(rig, token, val, 128);
}

int hiqsdr_init(RIG *rig)
{
    struct hiqsdr_priv_data *priv;

    rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);

    STATE(rig)->priv = (struct hiqsdr_priv_data *)calloc(1, sizeof(
                           struct hiqsdr_priv_data));

    if (!STATE(rig)->priv)
    {
        return -RIG_ENOMEM;
    }

    priv = STATE(rig)->priv;

    priv->split = RIG_SPLIT_OFF;
    priv->ref_clock = REFCLOCK;
    priv->sample_rate = DEFAULT_SAMPLE_RATE;
    strncpy(RIGPORT(rig)->pathname, "192.168.2.196:48248",
            HAMLIB_FILPATHLEN - 1);

    return RIG_OK;
}

int hiqsdr_open(RIG *rig)
{
    struct hiqsdr_priv_data *priv = (struct hiqsdr_priv_data *)STATE(rig)->priv;
#if 0
    const char buf_send_to_me[] = { 0x72, 0x72 };
    int ret;
#endif

    rig_debug(RIG_DEBUG_TRACE, "%s called\n", __func__);

    /* magic value */
    priv->control_frame[0] = 'S';
    priv->control_frame[1] = 't';
    /* zero tune phase */
    memset(priv->control_frame + 2, 0, 8);
    /* TX output level */
    priv->control_frame[10] = 120;
    /* Tx control: non-CW */
    priv->control_frame[11] = 0x02;
    /* decimation: 48 kSpls */
    priv->control_frame[12] = compute_sample_rate(priv);

    /* firmware version */
    priv->control_frame[13] = 0x00;
    /* X1 connector */
    priv->control_frame[14] = 0x00;
    /* Attenuator */
    priv->control_frame[15] = 0x00;
    /* AntSwitch */
    priv->control_frame[16] = 0x00;
    /* RFU */
    memset(priv->control_frame + 17, 0, 5);

#if 0
    /* Send the samples to me. FIXME: send to port 48247 */
    ret = write_block(RIGPORT(rig), buf_send_to_me, sizeof(buf_send_to_me));

    if (ret != RIG_OK)
    {
        return RIG_OK;
    }

#endif

    return RIG_OK;
}


int hiqsdr_close(RIG *rig)
{
    rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);

    return RIG_OK;
}

int hiqsdr_cleanup(RIG *rig)
{
    rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);

    if (STATE(rig)->priv)
    {
        free(STATE(rig)->priv);
    }

    STATE(rig)->priv = NULL;

    return RIG_OK;
}


/*
 */
int hiqsdr_set_freq(RIG *rig, vfo_t vfo, freq_t freq)
{
    struct hiqsdr_priv_data *priv = (struct hiqsdr_priv_data *)STATE(rig)->priv;
    int ret;
    double rxphase;
    uint32_t rxphase32;

    rig_debug(RIG_DEBUG_TRACE, "%s called\n", __func__);

    rxphase = (freq / priv->ref_clock) * (1ULL << 32) + 0.5;
    rxphase32 = (uint32_t)rxphase;

    priv->control_frame[2] = rxphase32 & 0xff;
    priv->control_frame[3] = (rxphase32 >> 8) & 0xff;
    priv->control_frame[4] = (rxphase32 >> 16) & 0xff;
    priv->control_frame[5] = (rxphase32 >> 24) & 0xff;

    if (priv->split == RIG_SPLIT_OFF)
    {
        priv->control_frame[6] = priv->control_frame[2];
        priv->control_frame[7] = priv->control_frame[3];
        priv->control_frame[8] = priv->control_frame[4];
        priv->control_frame[9] = priv->control_frame[5];
    }

    ret = send_command(rig);

    return ret;
}

static int hiqsdr_set_split_vfo(RIG *rig, vfo_t vfo, split_t split,
                                vfo_t tx_vfo)
{
    struct hiqsdr_priv_data *priv = (struct hiqsdr_priv_data *)STATE(rig)->priv;

    priv->split = split;

    return RIG_OK;
}

int hiqsdr_set_split_freq(RIG *rig, vfo_t vfo, freq_t tx_freq)
{
    struct hiqsdr_priv_data *priv = (struct hiqsdr_priv_data *)STATE(rig)->priv;
    int ret;
    double rxphase;
    uint32_t rxphase32;

    rig_debug(RIG_DEBUG_TRACE, "%s called\n", __func__);

    rxphase = (tx_freq / priv->ref_clock) * (1ULL << 32) + 0.5;
    rxphase32 = (uint32_t)rxphase;

    priv->control_frame[6] = rxphase32 & 0xff;
    priv->control_frame[7] = (rxphase32 >> 8) & 0xff;
    priv->control_frame[8] = (rxphase32 >> 16) & 0xff;
    priv->control_frame[9] = (rxphase32 >> 24) & 0xff;

    ret = send_command(rig);

    return ret;
}


int hiqsdr_get_freq(RIG *rig, vfo_t vfo, freq_t *freq)
{
    return -RIG_ENIMPL;
}

int hiqsdr_set_mode(RIG *rig, vfo_t vfo, rmode_t mode, pbwidth_t width)
{
    struct hiqsdr_priv_data *priv = (struct hiqsdr_priv_data *)STATE(rig)->priv;
    int ret = RIG_OK;

    rig_debug(RIG_DEBUG_VERBOSE, "%s called: %s\n",
              __func__, rig_strrmode(mode));


    if (mode == RIG_MODE_CW)
    {
        priv->control_frame[11] = 0x01;
    }
    else
    {
        priv->control_frame[11] = 0x02;
    }

    ret = send_command(rig);

    return ret;
}

int hiqsdr_set_ptt(RIG *rig, vfo_t vfo, ptt_t ptt)
{
    struct hiqsdr_priv_data *priv = (struct hiqsdr_priv_data *)STATE(rig)->priv;
    int ret = RIG_OK;

    rig_debug(RIG_DEBUG_VERBOSE, "%s called: %d\n",
              __func__, ptt);

    /* not allowed in CW mode */
    if (priv->control_frame[11] & 0x01)
    {
        return -RIG_ERJCTED;
    }

    if (ptt == RIG_PTT_ON)
    {
        priv->control_frame[11] |= 0x08;
    }
    else
    {
        priv->control_frame[11] &= ~0x08;
    }

    ret = send_command(rig);

    return ret;
}

int hiqsdr_set_ant(RIG *rig, vfo_t vfo, ant_t ant, value_t option)
{
    struct hiqsdr_priv_data *priv = (struct hiqsdr_priv_data *)STATE(rig)->priv;
    int ret = RIG_OK;

    rig_debug(RIG_DEBUG_VERBOSE, "%s called: %u\n",
              __func__, ant);

    if (ant == RIG_ANT_2)
    {
        priv->control_frame[16] |= 0x01;
    }
    else
    {
        priv->control_frame[16] &= ~0x01;
    }

    ret = send_command(rig);

    return ret;
}


int hiqsdr_set_level(RIG *rig, vfo_t vfo, setting_t level, value_t val)
{
    struct hiqsdr_priv_data *priv = (struct hiqsdr_priv_data *)STATE(rig)->priv;
    int ret = RIG_OK;

    switch (level)
    {
    case RIG_LEVEL_PREAMP:
        if (val.i)
        {
            priv->control_frame[14] |= 0x02;
        }
        else
        {
            priv->control_frame[14] &= ~0x02;
        }

        break;

    case RIG_LEVEL_ATT:
        /* FIXME: val->i should be looked up from the att list */
        priv->control_frame[14] = val.i & 0x1f;
        break;

    case RIG_LEVEL_RFPOWER:
        /* TX output level */
        priv->control_frame[10] = 0xff & (unsigned)(255 * val.f);
        break;

    default:
        return -RIG_EINVAL;
    }

    ret = send_command(rig);

    return ret;
}

static int hiqsdr_get_level(RIG *rig, vfo_t vfo, setting_t level, value_t *val)
{
    return -RIG_ENIMPL;
}