summaryrefslogtreecommitdiff
path: root/Radio/HW/BladeRF/src/expansion/xb300.c
blob: 15a099ddfd954494dd0e2e08b7c15c68c631efe9 (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
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
/*
 * This file is part of the bladeRF project:
 *   http://www.github.com/nuand/bladeRF
 *
 * Copyright (C) 2014 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
 */

#include "xb300.h"

#include "log.h"

#define BLADERF_XB_AUX_EN   0x000002
#define BLADERF_XB_TX_LED   0x000010
#define BLADERF_XB_RX_LED   0x000020
#define BLADERF_XB_TRX_TXn  0x000040
#define BLADERF_XB_TRX_RXn  0x000080
#define BLADERF_XB_TRX_MASK 0x0000c0
#define BLADERF_XB_PA_EN    0x000200
#define BLADERF_XB_LNA_ENn  0x000400
#define BLADERF_XB_CS       0x010000
#define BLADERF_XB_CSEL     0x040000
#define BLADERF_XB_DOUT     0x100000
#define BLADERF_XB_SCLK     0x400000

int xb300_attach(struct bladerf *dev) {
    int status;
    uint32_t val;

    val = BLADERF_XB_TX_LED | BLADERF_XB_RX_LED | BLADERF_XB_TRX_MASK;
    val |= BLADERF_XB_PA_EN | BLADERF_XB_LNA_ENn;
    val |= BLADERF_XB_CSEL  | BLADERF_XB_SCLK | BLADERF_XB_CS;

    if ((status = dev->backend->expansion_gpio_dir_write(dev, 0xffffffff, val)))
        return status;

    val = BLADERF_XB_CS | BLADERF_XB_LNA_ENn;
    if ((status = dev->backend->expansion_gpio_write(dev, 0xffffffff, val)))
        return status;

    return status;
}

void xb300_detach(struct bladerf *dev)
{
}

int xb300_enable(struct bladerf *dev, bool enable)
{
    int status;
    uint32_t val;
    float pwr;

    val = BLADERF_XB_CS | BLADERF_XB_CSEL | BLADERF_XB_LNA_ENn;
    if ((status = dev->backend->expansion_gpio_write(dev, 0xffffffff, val)))
        return status;

    status = xb300_get_output_power(dev, &pwr);

    return status;
}

int xb300_init(struct bladerf *dev)
{
    int status;

    log_verbose( "Setting TRX path to TX\n" );
    status = xb300_set_trx(dev, BLADERF_XB300_TRX_TX);
    if (status != 0) {
        return status;
    }

    return 0;
}

int xb300_set_trx(struct bladerf *dev, bladerf_xb300_trx trx)
{
    int status;
    uint32_t val;

    status = dev->backend->expansion_gpio_read(dev, &val);
    if (status != 0) {
        return status;
    }

    val &= ~(BLADERF_XB_TRX_MASK);

    switch (trx) {
        case BLADERF_XB300_TRX_RX:
            val |= BLADERF_XB_TRX_RXn;
            break;

        case BLADERF_XB300_TRX_TX:
            val |= BLADERF_XB_TRX_TXn;
            break;

        case BLADERF_XB300_TRX_UNSET:
            break;

        default:
            log_debug("Invalid TRX option: %d\n", trx);
            return BLADERF_ERR_INVAL;
    }

    status = dev->backend->expansion_gpio_write(dev, 0xffffffff, val);
    return status;
}

int xb300_get_trx(struct bladerf *dev, bladerf_xb300_trx *trx)
{
    int status;
    uint32_t val;

    *trx = BLADERF_XB300_TRX_INVAL;

    status = dev->backend->expansion_gpio_read(dev, &val);
    if (status != 0) {
        return status;
    }

    val &= BLADERF_XB_TRX_MASK;

    if (!val) {
        *trx = BLADERF_XB300_TRX_UNSET;
    } else {
        *trx = (val & BLADERF_XB_TRX_RXn) ? BLADERF_XB300_TRX_RX : BLADERF_XB300_TRX_TX;
    }

    /* Sanity check */
    switch (*trx) {
        case BLADERF_XB300_TRX_TX:
        case BLADERF_XB300_TRX_RX:
        case BLADERF_XB300_TRX_UNSET:
            break;

        default:
            log_debug("Read back invalid TRX setting value: %d\n", *trx);
            status = BLADERF_ERR_INVAL;
    }

    return status;
}

int xb300_set_amplifier_enable(struct bladerf *dev,
                               bladerf_xb300_amplifier amp, bool enable) {
    int status;
    uint32_t val;

    status = dev->backend->expansion_gpio_read(dev, &val);
    if (status != 0) {
        return status;
    }

    switch (amp) {
        case BLADERF_XB300_AMP_PA:
            if (enable) {
                val |= BLADERF_XB_TX_LED;
                val |= BLADERF_XB_PA_EN;
            } else {
                val &= ~BLADERF_XB_TX_LED;
                val &= ~BLADERF_XB_PA_EN;
            }
            break;

        case BLADERF_XB300_AMP_LNA:
            if (enable) {
                val |= BLADERF_XB_RX_LED;
                val &= ~BLADERF_XB_LNA_ENn;
            } else {
                val &= ~BLADERF_XB_RX_LED;
                val |= BLADERF_XB_LNA_ENn;
            }
            break;

        case BLADERF_XB300_AMP_PA_AUX:
            if (enable) {
                val |= BLADERF_XB_AUX_EN;
            } else {
                val &= ~BLADERF_XB_AUX_EN;
            }
            break;

        default:
            log_debug("Invalid amplifier selection: %d\n", amp);
            return BLADERF_ERR_INVAL;
    }

    status = dev->backend->expansion_gpio_write(dev, 0xffffffff, val);

    return status;
}

int xb300_get_amplifier_enable(struct bladerf *dev,
                               bladerf_xb300_amplifier amp, bool *enable)
{
    int status;
    uint32_t val;

    *enable = false;

    status = dev->backend->expansion_gpio_read(dev, &val);
    if (status != 0) {
        return status;
    }

    switch (amp) {
        case BLADERF_XB300_AMP_PA:
            *enable = (val & BLADERF_XB_PA_EN);
            break;

        case BLADERF_XB300_AMP_LNA:
            *enable = !(val & BLADERF_XB_LNA_ENn);
            break;

        case BLADERF_XB300_AMP_PA_AUX:
            *enable = (val & BLADERF_XB_AUX_EN);
            break;

        default:
            log_debug("Read back invalid amplifier setting: %d\n", amp);
            status = BLADERF_ERR_INVAL;
    }

    return status;
}

int xb300_get_output_power(struct bladerf *dev, float *pwr)
{
    uint32_t val, rval;
    int i;
    int ret = 0;
    int status;
    float volt, volt2, volt3, volt4;

    status = dev->backend->expansion_gpio_read(dev, &val);
    if (status) {
        return status;
    }

    val &= ~(BLADERF_XB_CS | BLADERF_XB_SCLK | BLADERF_XB_CSEL);

    status = dev->backend->expansion_gpio_write(dev, 0xffffffff, BLADERF_XB_SCLK | val);
    if (status) {
        return status;
    }

    status = dev->backend->expansion_gpio_write(dev, 0xffffffff, BLADERF_XB_CS | BLADERF_XB_SCLK | val);
    if (status) {
        return status;
    }

    for (i = 1; i <= 14; i++) {
        status = dev->backend->expansion_gpio_write(dev, 0xffffffff, val);
        if (status) {
            return status;
        }

        status = dev->backend->expansion_gpio_write(dev, 0xffffffff, BLADERF_XB_SCLK | val);
        if (status) {
            return status;
        }

        status = dev->backend->expansion_gpio_read(dev, &rval);
        if (status) {
            return status;
        }

        if (i >= 2 && i <= 11) {
            ret |= (!!(rval & BLADERF_XB_DOUT)) << (11 - i);
        }
    }

    volt = (1.8f/1024.0f) * ret;
    volt2 = volt  * volt;
    volt3 = volt2 * volt;
    volt4 = volt3 * volt;

    *pwr =  -503.933f  * volt4 +
            1409.489f  * volt3 -
            1487.84f   * volt2 +
             722.9793f * volt  -
             114.7529f;

    return 0;

}