summaryrefslogtreecommitdiff
path: root/Radio/HW/BladeRF/src/driver/fpga_trigger.c
blob: 67818a02c8c0fe73b438c1ba0306beaf3114536f (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) 2016 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 <libbladeRF.h>

#include "log.h"

#include "fpga_trigger.h"

static bool is_valid_signal(bladerf_trigger_signal signal)
{
    switch (signal) {
        case BLADERF_TRIGGER_J71_4:
        case BLADERF_TRIGGER_J51_1:
        case BLADERF_TRIGGER_MINI_EXP_1:

        case BLADERF_TRIGGER_USER_0:
        case BLADERF_TRIGGER_USER_1:
        case BLADERF_TRIGGER_USER_2:
        case BLADERF_TRIGGER_USER_3:
        case BLADERF_TRIGGER_USER_4:
        case BLADERF_TRIGGER_USER_5:
        case BLADERF_TRIGGER_USER_6:
        case BLADERF_TRIGGER_USER_7:
            return true;

        default:
            log_debug("Invalid trigger signal: %d\n", signal);
            return false;
    }
}

int fpga_trigger_read(struct bladerf *dev, bladerf_channel ch,
                      bladerf_trigger_signal signal, uint8_t *regval)
{
    if (ch != BLADERF_CHANNEL_RX(0) && ch != BLADERF_CHANNEL_TX(0))
        return BLADERF_ERR_INVAL;

    if (!is_valid_signal(signal))
        return BLADERF_ERR_INVAL;

    return dev->backend->read_trigger(dev, ch, signal, regval);
}

int fpga_trigger_write(struct bladerf *dev, bladerf_channel ch,
                       bladerf_trigger_signal signal, uint8_t regval)
{
    if (ch != BLADERF_CHANNEL_RX(0) && ch != BLADERF_CHANNEL_TX(0))
        return BLADERF_ERR_INVAL;

    if (!is_valid_signal(signal))
        return BLADERF_ERR_INVAL;

    return dev->backend->write_trigger(dev, ch, signal, regval);
}

int fpga_trigger_init(struct bladerf *dev, bladerf_channel ch,
                      bladerf_trigger_signal signal,
                 struct bladerf_trigger *trigger)

{
    int status;
    uint8_t regval;

    trigger->options = 0;

    status = fpga_trigger_read(dev, ch, signal, &regval);
    if (status != 0) {
        trigger->channel = BLADERF_CHANNEL_INVALID;
        trigger->role   = BLADERF_TRIGGER_ROLE_INVALID;
        trigger->signal = BLADERF_TRIGGER_INVALID;
        return status;
    }

    if ((regval & BLADERF_TRIGGER_REG_MASTER) != 0) {
        trigger->role = BLADERF_TRIGGER_ROLE_MASTER;
    } else {
        trigger->role = BLADERF_TRIGGER_ROLE_SLAVE;
    }

    trigger->channel = ch;
    trigger->signal = signal;

    return 0;
}

int fpga_trigger_arm(struct bladerf *dev,
                     const struct bladerf_trigger *trigger, bool arm)
{
    int status;
    uint8_t regval;

    status = fpga_trigger_read(dev, trigger->channel, trigger->signal, &regval);
    if (status != 0) {
        return status;
    }

    /* Reset any previous fire request */
    regval &= ~BLADERF_TRIGGER_REG_FIRE;

    if (arm) {
        regval |= BLADERF_TRIGGER_REG_ARM;
    } else {
        regval &= ~BLADERF_TRIGGER_REG_ARM;
    }

    switch (trigger->role) {
        case BLADERF_TRIGGER_ROLE_MASTER:
            regval |= BLADERF_TRIGGER_REG_MASTER;
            break;

        case BLADERF_TRIGGER_ROLE_SLAVE:
            regval &= ~BLADERF_TRIGGER_REG_MASTER;
            break;

        case BLADERF_TRIGGER_ROLE_DISABLED:
            regval = 0;
            break;

        default:
            log_debug("Invalid trigger role: %d\n", trigger->role);
            return BLADERF_ERR_INVAL;
    }

    status = fpga_trigger_write(dev, trigger->channel, trigger->signal, regval);

    return status;
}

int fpga_trigger_fire(struct bladerf *dev,
                      const struct bladerf_trigger *trigger)
{
    int status;
    uint8_t regval;

    status = fpga_trigger_read(dev, trigger->channel, trigger->signal, &regval);
    if (status != 0) {
        return status;
    }

    regval |= BLADERF_TRIGGER_REG_FIRE;
    status = fpga_trigger_write(dev, trigger->channel, trigger->signal, regval);

    return status;
}

int fpga_trigger_state(struct bladerf *dev, const struct bladerf_trigger *trigger,
                       bool *is_armed, bool *fired, bool *fire_requested)
{
    int status;
    uint8_t regval;

    status = fpga_trigger_read(dev, trigger->channel, trigger->signal, &regval);
    if (status != 0) {
        *fired = false;
        return status;
    }

    if (is_armed != NULL) {
        *is_armed = (regval & BLADERF_TRIGGER_REG_ARM) != 0;
    }

    if (fired != NULL) {
        /* Signal is active-low */
        *fired = (regval & BLADERF_TRIGGER_REG_LINE) == 0;
    }

    if (fire_requested != NULL) {
        if (trigger->role == BLADERF_TRIGGER_ROLE_MASTER) {
            *fire_requested = (regval & BLADERF_TRIGGER_REG_FIRE) != 0;
        } else {
            *fire_requested = false;
        }
    }

    return status;
}