File: revcodes.py

package info (click to toggle)
python-adafruit-platformdetect 3.87.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 368 kB
  • sloc: python: 2,607; sh: 6; makefile: 2
file content (306 lines) | stat: -rw-r--r-- 8,313 bytes parent folder | download
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
# SPDX-FileCopyrightText: 2023 Melissa LeBlanc-Williams for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
`adafruit_platformdetect.revcodes`
================================================================================

Class to help with Raspberry Pi Rev Codes

* Author(s): Melissa LeBlanc-Williams

Implementation Notes
--------------------

**Software and Dependencies:**

* Linux and Python 3.7 or Higher

Data values from https://github.com/raspberrypi/documentation/blob/develop/
documentation/asciidoc/computers/raspberry-pi/revision-codes.adoc#new-style-revision-codes

"""

NEW_OVERVOLTAGE = (
    "Overvoltage allowed",
    "Overvoltage disallowed",
)

NEW_OTP_PROGRAM = (
    "OTP programming is allowed",
    "OTP programming is disallowed",
)

NEW_OTP_READ = (
    "OTP reading is allowed",
    "OTP reading is disallowed",
)

NEW_WARRANTY_BIT = (
    "Warranty is intact",
    "Warranty has been voided by overclocking",
)

NEW_REV_STYLE = (
    "Old-style revision",
    "New-style revision",
)

NEW_MEMORY_SIZE = (
    "256MB",
    "512MB",
    "1GB",
    "2GB",
    "4GB",
    "8GB",
)

NEW_MANUFACTURER = (
    "Sony UK",
    "Egoman",
    "Embest",
    "Sony Japan",
    "Embest",
    "Stadium",
)

NEW_PROCESSOR = (
    "BCM2835",
    "BCM2836",
    "BCM2837",
    "BCM2711",
    "BCM2712",
)

PI_TYPE = {
    0x00: "A",
    0x01: "B",
    0x02: "A+",
    0x03: "B+",
    0x04: "2B",
    0x05: "Alpha (early prototype)",
    0x06: "CM1",
    0x08: "3B",
    0x09: "Zero",
    0x0A: "CM3",
    0x0B: "Custom",
    0x0C: "Zero W",
    0x0D: "3B+",
    0x0E: "3A+",
    0x0F: "Internal use only",
    0x10: "CM3+",
    0x11: "4B",
    0x12: "Zero 2 W",
    0x13: "400",
    0x14: "CM4",
    0x15: "CM4S",
    0x17: "5",
    0x18: "CM5",
    0x19: "500",
    0x1A: "CM5 Lite",
}

OLD_MANUFACTURER = (
    "Sony UK",
    "Egoman",
    "Embest",
    "Qisda",
)

OLD_MEMORY_SIZE = ("256MB", "512MB", "256MB/512MB")

NEW_REV_STRUCTURE = {
    "overvoltage": (31, 1, NEW_OVERVOLTAGE),
    "otp_program": (30, 1, NEW_OTP_PROGRAM),
    "otp_read": (29, 1, NEW_OTP_READ),
    "warranty": (25, 1, NEW_WARRANTY_BIT),
    "rev_style": (23, 1, NEW_REV_STYLE),
    "memory_size": (20, 3, NEW_MEMORY_SIZE),
    "manufacturer": (16, 4, NEW_MANUFACTURER),
    "processor": (12, 4, NEW_PROCESSOR),
    "type": (4, 8, PI_TYPE),
    "revision": (0, 4, int),
}

OLD_REV_STRUCTURE = {
    "type": (0, PI_TYPE),
    "revision": (1, float),
    "memory_size": (2, OLD_MEMORY_SIZE),
    "manufacturer": (3, OLD_MANUFACTURER),
}

OLD_REV_EXTRA_PROPS = {
    "warranty": (24, 1, NEW_WARRANTY_BIT),
}

OLD_REV_LUT = {
    0x02: (1, 1.0, 0, 1),
    0x03: (1, 1.0, 0, 1),
    0x04: (1, 2.0, 0, 0),
    0x05: (1, 2.0, 0, 3),
    0x06: (1, 2.0, 0, 1),
    0x07: (0, 2.0, 0, 1),
    0x08: (0, 2.0, 0, 0),
    0x09: (0, 2.0, 0, 3),
    0x0D: (1, 2.0, 1, 1),
    0x0E: (1, 2.0, 1, 0),
    0x0F: (1, 2.0, 1, 1),
    0x10: (3, 1.2, 1, 0),
    0x11: (6, 1.0, 1, 0),
    0x12: (2, 1.1, 0, 0),
    0x13: (3, 1.2, 1, 2),
    0x14: (6, 1.0, 1, 2),
    0x15: (2, 1.1, 2, 2),
}


class PiDecoder:
    """Raspberry Pi Revision Code Decoder"""

    def __init__(self, rev_code):
        try:
            self.rev_code = int(rev_code, 16) & 0xFFFFFFFF
        except ValueError:
            print("Invalid revision code. It should be a hexadecimal value.")

    def is_valid_code(self):
        """Quickly check the validity of a code"""
        if self.is_new_format():
            for code_format in NEW_REV_STRUCTURE.values():
                lower_bit, bit_size, values = code_format
                prop_value = (self.rev_code >> lower_bit) & ((1 << bit_size) - 1)
                if not self._valid_value(prop_value, values):
                    return False
        else:
            if (
                self.rev_code & 0xFFFF
            ) not in OLD_REV_LUT.keys():  # pylint: disable=consider-iterating-dictionary
                return False
            for code_format in OLD_REV_STRUCTURE.values():
                index, values = code_format
                code_format = OLD_REV_LUT[self.rev_code & 0xFFFF]
                if index >= len(code_format):
                    return False
                if not self._valid_value(code_format[index], values):
                    return False
        return True

    def _get_rev_prop_value(self, name, structure=None, raw=False):
        if structure is None:
            structure = NEW_REV_STRUCTURE
        if name not in structure.keys():
            raise ValueError(f"Unknown property {name}")
        lower_bit, bit_size, values = structure[name]
        prop_value = self._get_bits_value(lower_bit, bit_size)
        if not self._valid_value(prop_value, values):
            raise ValueError(f"Invalid value {prop_value} for property {name}")
        if raw:
            return prop_value
        return self._format_value(prop_value, values)

    def _get_bits_value(self, lower_bit, bit_size):
        return (self.rev_code >> lower_bit) & ((1 << bit_size) - 1)

    def _get_old_rev_prop_value(self, name, raw=False):
        if (
            name
            not in OLD_REV_STRUCTURE.keys()  # pylint: disable=consider-iterating-dictionary
        ):
            raise ValueError(f"Unknown property {name}")
        index, values = OLD_REV_STRUCTURE[name]
        data = OLD_REV_LUT[self.rev_code & 0xFFFF]
        if index >= len(data):
            raise IndexError(f"Index {index} out of range for property {name}")
        if not self._valid_value(data[index], values):
            raise ValueError(f"Invalid value {data[index]} for property {name}")
        if raw:
            return data[index]
        return self._format_value(data[index], values)

    @staticmethod
    def _format_value(value, valid_values):
        if valid_values is float or valid_values is int:
            return valid_values(value)
        return valid_values[value]

    @staticmethod
    def _valid_value(value, valid_values):
        if valid_values is float or valid_values is int:
            return isinstance(value, valid_values)
        if isinstance(valid_values, (tuple, list)) and 0 <= value < len(valid_values):
            return True
        if isinstance(valid_values, dict) and value in valid_values.keys():
            return True
        return False

    def _get_property(self, name, raw=False):
        if name not in NEW_REV_STRUCTURE:
            raise ValueError(f"Unknown property {name}")
        if self.is_new_format():
            return self._get_rev_prop_value(name, raw=raw)
        if name in OLD_REV_EXTRA_PROPS:
            return self._get_rev_prop_value(
                name, structure=OLD_REV_EXTRA_PROPS, raw=raw
            )
        return self._get_old_rev_prop_value(name, raw=raw)

    def is_new_format(self):
        """Check if the code is in the new format"""
        return self._get_rev_prop_value("rev_style", raw=True) == 1

    @property
    def overvoltage(self):
        """Overvoltage allowed/disallowed"""
        return self._get_property("overvoltage")

    @property
    def warranty_bit(self):
        """Warranty bit"""
        return self._get_property("warranty")

    @property
    def otp_program(self):
        """OTP programming allowed/disallowed"""
        return self._get_property("otp_program")

    @property
    def otp_read(self):
        """OTP reading allowed/disallowed"""
        return self._get_property("otp_read")

    @property
    def rev_style(self):
        """Revision Code style"""
        # Force new style for Rev Style
        return self._get_rev_prop_value("rev_style")

    @property
    def memory_size(self):
        """Memory size"""
        return self._get_property("memory_size")

    @property
    def manufacturer(self):
        """Manufacturer"""
        return self._get_property("manufacturer")

    @property
    def processor(self):
        """Processor"""
        return self._get_property("processor")

    @property
    def type(self):
        """Specific Model"""
        return self._get_property("type")

    @property
    def type_raw(self):
        """Raw Value of Specific Model"""
        return self._get_property("type", raw=True)

    @property
    def revision(self):
        """Revision Number"""
        return self._get_property("revision")