summaryrefslogtreecommitdiff
path: root/Radio/HW/BladeRF/fpga_common/src/ad936x_helpers.c
blob: 15982faa561ff4508aa7cf68374941683ebffa98 (plain) (blame)
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
/*
 * This file is part of the bladeRF project:
 *   http://www.github.com/nuand/bladeRF
 *
 * Copyright (C) 2018 Nuand LLC
 *
 * 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
 */

#ifdef BLADERF_NIOS_BUILD
#include "devices.h"
#endif  // BLADERF_NIOS_BUILD

/* Avoid building this in low-memory situations */
#if !defined(BLADERF_NIOS_BUILD) || defined(BLADERF_NIOS_LIBAD936X)

#if !defined(BLADERF_NIOS_BUILD) && !defined(BLADERF_NIOS_PC_SIMULATION)
#include "log.h"
#endif

#include "ad936x_helpers.h"
#include "bladerf2_common.h"

static bool tx_mute_state[2] = { false };

uint32_t txmute_get_cached(struct ad9361_rf_phy *phy, bladerf_channel ch)
{
    switch (ch) {
        case BLADERF_CHANNEL_TX(0):
            return phy->tx1_atten_cached;
        case BLADERF_CHANNEL_TX(1):
            return phy->tx2_atten_cached;
        default:
            return 0;
    }
}

int txmute_set_cached(struct ad9361_rf_phy *phy,
                      bladerf_channel ch,
                      uint32_t atten)
{
    switch (ch) {
        case BLADERF_CHANNEL_TX(0):
            phy->tx1_atten_cached = atten;
            return 0;
        case BLADERF_CHANNEL_TX(1):
            phy->tx2_atten_cached = atten;
            return 0;
        default:
            return BLADERF_ERR_INVAL;
    }
}

int txmute_get(struct ad9361_rf_phy *phy, bladerf_channel ch, bool *state)
{
    int rfic_ch = (ch >> 1);

    *state = tx_mute_state[rfic_ch];

    return 0;
}

int txmute_set(struct ad9361_rf_phy *phy, bladerf_channel ch, bool state)
{
    int rfic_ch                = (ch >> 1);
    uint32_t const MUTED_ATTEN = 89750;
    uint32_t atten, cached;
    int status;

    if (tx_mute_state[rfic_ch] == state) {
        // short circuit if there's no change
        return 0;
    }

    if (state) {
        // mute: save the existing value before muting
        uint32_t readval;

        status = ad9361_get_tx_attenuation(phy, rfic_ch, &readval);
        if (status < 0) {
            return errno_ad9361_to_bladerf(status);
        }

        cached = readval;
        atten  = MUTED_ATTEN;
    } else {
        // unmute: restore the saved value
        cached = txmute_get_cached(phy, ch);
        atten  = cached;
    }

    status = ad9361_set_tx_attenuation(phy, rfic_ch, atten);
    if (status < 0) {
        return errno_ad9361_to_bladerf(status);
    }

    status = txmute_set_cached(phy, ch, cached);
    if (status < 0) {
        return status;
    }

    tx_mute_state[rfic_ch] = state;

    return 0;
}

int set_ad9361_port_by_freq(struct ad9361_rf_phy *phy,
                            bladerf_channel ch,
                            bool enabled,
                            bladerf_frequency freq)
{
    struct band_port_map const *port_map = NULL;
    int status;

    /* Look up the port configuration for this frequency */
    port_map = _get_band_port_map_by_freq(ch, enabled ? freq : 0);

    if (NULL == port_map) {
        return BLADERF_ERR_INVAL;
    }

    /* Set the AD9361 port accordingly */
    if (BLADERF_CHANNEL_IS_TX(ch)) {
        status = ad9361_set_tx_rf_port_output(phy, port_map->rfic_port);
    } else {
        status = ad9361_set_rx_rf_port_input(phy, port_map->rfic_port);
    }

    return errno_ad9361_to_bladerf(status);
}

enum rf_gain_ctrl_mode gainmode_bladerf_to_ad9361(bladerf_gain_mode gainmode,
                                                  bool *ok)
{
    struct bladerf_rfic_gain_mode_map const *mode_map;
    size_t mode_map_len;
    size_t i;

    mode_map     = bladerf2_rx_gain_mode_map;
    mode_map_len = ARRAY_SIZE(bladerf2_rx_gain_mode_map);

    if (NULL != ok) {
        *ok = false;
    }

    for (i = 0; i < mode_map_len; ++i) {
        if (mode_map[i].brf_mode == gainmode) {
            if (NULL != ok) {
                *ok = true;
            }
            return mode_map[i].rfic_mode;
        }
    }

    return 0;
};

bladerf_gain_mode gainmode_ad9361_to_bladerf(enum rf_gain_ctrl_mode gainmode,
                                             bool *ok)
{
    struct bladerf_rfic_gain_mode_map const *mode_map;
    size_t mode_map_len;
    size_t i;

    mode_map     = bladerf2_rx_gain_mode_map;
    mode_map_len = ARRAY_SIZE(bladerf2_rx_gain_mode_map);

    if (NULL != ok) {
        *ok = false;
    }

    for (i = 0; i < mode_map_len; ++i) {
        if (mode_map[i].rfic_mode == gainmode) {
            if (NULL != ok) {
                *ok = true;
            }
            return mode_map[i].brf_mode;
        }
    }

    return 0;
}

#endif  // !defined(BLADERF_NIOS_BUILD) || defined(BLADERF_NIOS_LIBAD936X)