aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/fx3/b200/common/common_helpers.c
blob: 5a21a210690d431babfedb59ec3f67b71a3be5bd (plain)
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
//
// Copyright 2019 Ettus Research, a National Instruments Brand
//
// SPDX-License-Identifier: GPL-3.0-or-later
//

#include "common_helpers.h"
#include "common_const.h"
#include "common_descriptors.h"

#define UNREAD 0xFFFF

/* Check the rev, magic value, and eeprom compatibility field to determine if
   our eeprom map is up-to-date with what is written on the device */
int eeprom_is_readable(eeprom_read_t read_fn) {
    /* Only check this once, then store that info */
    static int is_readable = UNREAD;

    if (is_readable != UNREAD) {
        return is_readable;
    }

    int rev = get_rev(read_fn);

    if (rev == EEPROM_REV_UNRECOGNIZED)
    {
        is_readable = 0;
        return is_readable;
    }


    /* If rev is 0, there's no further checks available */
    if (rev == 0)
    {
        is_readable = 1;
        return is_readable;
    }

    /* For rev 1, check the EEPROM magic value and compat number */

    if (rev == 1) {
        {
            uint8_t buffer[EEPROM_MAGIC_LENGTH];
            read_fn(EEPROM_REV1_MAGIC_ADDR, buffer, EEPROM_MAGIC_LENGTH);
            if ((buffer[1] << 8 | buffer[0]) != EEPROM_EXPECTED_MAGIC) {
                is_readable = 0;
                return is_readable;
            }
        }
        {
            uint8_t buffer[EEPROM_COMPAT_LENGTH];
            read_fn(EEPROM_REV1_COMPAT_ADDR, buffer, EEPROM_COMPAT_LENGTH);
            if ((buffer[1] << 8 | buffer[0]) > EEPROM_EXPECTED_COMPAT) {
                is_readable = 0;
                return is_readable;
            }
        }
        is_readable = 1;
        return is_readable;
    }

    /* some other unrecognized rev */
    is_readable = 0;
    return is_readable;
}

/* Read the EEPROM layout revision number from EEPROM using the function
   specified */
int get_rev(eeprom_read_t read_fn)
{
    /* Only check the rev once, then store that info */
    static int rev = UNREAD;

    if (rev != UNREAD) {
        return rev;
    }

    uint8_t buffer[EEPROM_SIGNATURE_LENGTH];
    read_fn(EEPROM_SIGNATURE_ADDR, buffer, EEPROM_SIGNATURE_LENGTH);

    const uint32_t signature = buffer[3] << 24 | buffer[2] << 16 | buffer[1] << 8
                               | buffer[0];
    if (signature == EEPROM_REV0_SIGNATURE) {
        rev = 0;
    } else if (signature == EEPROM_REV1_OR_GREATER_SIGNATURE) {
        rev = 1;
    } else {
        rev = EEPROM_REV_UNRECOGNIZED;
    }
    return rev;
}



/* Read the vendor ID from EEPROM using the function specified*/
uint16_t get_vid(eeprom_read_t read_fn)
{
    static uint16_t vid = UNREAD;

    if (vid != UNREAD) {
        return vid;
    }

    if (!eeprom_is_readable(read_fn)) {
        vid = VID_CYPRESS;
        return vid;
    }

    // eeprom_is_readable guarantees rev is valid
    int rev = get_rev(read_fn);

    const uint16_t addr = (rev == 0) ? EEPROM_REV0_VID_ADDR : EEPROM_REV1_VID_ADDR;

    uint8_t buffer[EEPROM_VID_LENGTH];
    read_fn(addr, buffer, EEPROM_VID_LENGTH);
    vid = buffer[1] << 8 | buffer[0];
    return vid;
}

/* Read the product ID from EEPROM using the function specified*/
uint16_t get_pid(eeprom_read_t read_fn)
{
    static uint16_t pid = UNREAD;

    if (pid != UNREAD) {
        return pid;
    }

    if (!eeprom_is_readable(read_fn)) {
        pid = PID_CYPRESS_DEFAULT;
        return pid;
    }

    // eeprom_is_readable guarantees rev is valid
    int rev = get_rev(read_fn);

    const uint16_t addr = (rev == 0) ? EEPROM_REV0_PID_ADDR : EEPROM_REV1_PID_ADDR;

    uint8_t buffer[EEPROM_PID_LENGTH];
    read_fn(addr, buffer, EEPROM_PID_LENGTH);
    pid = buffer[1] << 8 | buffer[0];
    return pid;
}

/* Read the vendor ID from EEPROM using the function specified */
const uint8_t* get_serial_string_descriptor(eeprom_read_t read_fn)
{
    static uint8_t* serial_string_descriptor = 0;
    if (serial_string_descriptor) {
        return serial_string_descriptor;
    }

    /* All code paths will eventually return this value, but some will modify
       it beforehand */
    serial_string_descriptor = common_dev_serial_desc;

    if (!eeprom_is_readable(read_fn)) {
        return serial_string_descriptor;
    }

    // eeprom_is_readable guarantees rev is valid
    int rev = get_rev(read_fn);

    const uint16_t addr = (rev == 0) ? EEPROM_REV0_SERIAL_ADDR : EEPROM_REV1_SERIAL_ADDR;

    uint8_t buffer[EEPROM_SERIAL_LENGTH];
    read_fn(addr, buffer, EEPROM_SERIAL_LENGTH);
    int i;
    for (i = 0; i < EEPROM_SERIAL_LENGTH; ++i) {
        common_dev_serial_desc[2 + i * 2] = buffer[i];
    }
    return serial_string_descriptor;
}

/* Return the string descriptor based on the VID given */
const uint8_t* get_manufacturer_string_descriptor(uint16_t vid)
{
    if (vid == VID_ETTUS_RESEARCH) {
        return common_ettus_manufacturer_desc;
    } else if (vid == VID_NATIONAL_INSTRUMENTS) {
        return common_ni_manufacturer_desc;
    } else {
        return common_unknown_desc;
    }
}

/* Return the string descriptor based on the PID given */
const uint8_t* get_product_string_descriptor(uint16_t pid)
{
    if (pid == PID_ETTUS_B200_B210) {
        return common_b200_product_desc;
    } else if (pid == PID_NI_USRP_2900) {
        return common_niusrp_2900_product_desc;
    } else if (pid == PID_NI_USRP_2901) {
        return common_niusrp_2901_product_desc;
    } else {
        return common_unknown_desc;
    }
}