]> sigrok.org Git - libsigrokdecode.git/blame - decoders/edid/pd.py
Small cosmetic/consistency fixes in the PDs.
[libsigrokdecode.git] / decoders / edid / pd.py
CommitLineData
91b2e171
BV
1##
2## This file is part of the sigrok project.
3##
4## Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
5##
6## This program is free software; you can redistribute it and/or modify
7## it under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 3 of the License, or
9## (at your option) any later version.
10##
11## This program is distributed in the hope that it will be useful,
12## but WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14## GNU General Public License for more details.
15##
16## You should have received a copy of the GNU General Public License
17## along with this program; if not, see <http://www.gnu.org/licenses/>.
18##
19
156509ca
UH
20# EDID protocol decoder
21
91b2e171
BV
22# TODO:
23# - EDID < 1.3
1063646c 24# - add short annotations
91b2e171
BV
25# - Signal level standard field in basic display parameters block
26# - Additional color point descriptors
27# - Additional standard timing descriptors
28# - Extensions
29
30import sigrokdecode as srd
31import os
32
33EDID_HEADER = [0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00]
34OFF_VENDOR = 8
35OFF_VERSION = 18
36OFF_BASIC = 20
37OFF_CHROM = 25
38OFF_EST_TIMING = 35
39OFF_STD_TIMING = 38
40OFF_DET_TIMING = 54
41OFF_NUM_EXT = 126
42OFF_CHECKSUM = 127
43
91b2e171
BV
44# Pre-EDID established timing modes
45est_modes = [
09059016
UH
46 '720x400@70Hz',
47 '720x400@88Hz',
48 '640x480@60Hz',
49 '640x480@67Hz',
50 '640x480@72Hz',
51 '640x480@75Hz',
52 '800x600@56Hz',
53 '800x600@60Hz',
54 '800x600@72Hz',
55 '800x600@75Hz',
56 '832x624@75Hz',
57 '1024x768@87Hz(i)',
58 '1024x768@60Hz',
59 '1024x768@70Hz',
60 '1024x768@75Hz',
61 '1280x1024@75Hz',
62 '1152x870@75Hz',
91b2e171
BV
63]
64
65# X:Y display aspect ratios, as used in standard timing modes
66xy_ratio = [
67 (16, 10),
68 (4, 3),
69 (5, 4),
e4f82268 70 (16, 9),
91b2e171
BV
71]
72
73# Annotation types
74ANN_FIELDS = 0
75ANN_SECTIONS = 1
76
77class Decoder(srd.Decoder):
78 api_version = 1
79 id = 'edid'
80 name = 'EDID'
b7a7e6f5 81 longname = 'Extended Display Identification Data'
a465436e 82 desc = 'Data structure describing display device capabilities.'
91b2e171 83 license = 'gplv3+'
9e1437a0 84 inputs = ['i2c']
91b2e171 85 outputs = ['edid']
9e1437a0
UH
86 probes = []
87 optional_probes = []
91b2e171
BV
88 options = {}
89 annotations = [
90 ['EDID fields', 'EDID structure fields'],
91 ['EDID sections', 'EDID structure sections'],
92 ]
93
94 def __init__(self, **kwargs):
95 self.state = None
96 # Received data items, used as an index into samplenum/data
97 self.cnt = 0
98 # Start/end sample numbers per data item
99 self.sn = []
100 # Received data
101 self.cache = []
102
103 def start(self, metadata):
104 self.out_ann = self.add(srd.OUTPUT_ANN, 'edid')
105
363252b4
UH
106 def report(self):
107 pass
108
91b2e171 109 def decode(self, ss, es, data):
48eee789
UH
110 cmd, data = data
111
112 # We only care about actual data bytes that are read (for now).
113 if cmd != 'DATA READ':
114 return
115
91b2e171 116 self.cnt += 1
e4f82268 117 self.sn.append([ss, es])
91b2e171 118 self.cache.append(data)
2a706c20 119 # debug
09059016 120# self.put(ss, es, self.out_ann, [0, ['%d: [%.2x]' % (self.cnt, data)]])
91b2e171
BV
121
122 if self.state is None:
123 # Wait for the EDID header
124 if self.cnt >= OFF_VENDOR:
125 if self.cache[-8:] == EDID_HEADER:
126 # Throw away any garbage before the header
127 self.sn = self.sn[-8:]
128 self.cache = self.cache[-8:]
2a706c20 129 self.cnt = 8
91b2e171 130 self.state = 'edid'
09059016 131 self.put(ss, es, self.out_ann, [0, ['EDID header']])
91b2e171
BV
132 elif self.state == 'edid':
133 if self.cnt == OFF_VERSION:
134 self.decode_vid(-10)
135 self.decode_pid(-8)
136 self.decode_serial(-6)
137 self.decode_mfrdate(-2)
138 elif self.cnt == OFF_BASIC:
09059016 139 version = 'EDID version: %d.%d' % (self.cache[-2], self.cache[-1])
91b2e171
BV
140 self.put(ss, es, self.out_ann, [0, [version]])
141 elif self.cnt == OFF_CHROM:
142 self.decode_basicdisplay(-5)
143 elif self.cnt == OFF_EST_TIMING:
144 self.decode_chromaticity(-10)
145 elif self.cnt == OFF_STD_TIMING:
146 self.decode_est_timing(-3)
147 elif self.cnt == OFF_DET_TIMING:
148 self.decode_std_timing(-16)
149 elif self.cnt == OFF_NUM_EXT:
150 self.decode_descriptors(-72)
151 elif self.cnt == OFF_CHECKSUM:
09059016
UH
152 self.put(ss, es, self.out_ann,
153 [0, ['Extensions present: %d' % self.cache[self.cnt-1]]])
91b2e171
BV
154 elif self.cnt == OFF_CHECKSUM+1:
155 checksum = 0
156 for i in range(128):
157 checksum += self.cache[i]
158 if checksum % 256 == 0:
09059016 159 csstr = 'OK'
91b2e171 160 else:
09059016
UH
161 csstr = 'WRONG!'
162 self.put(ss, es, self.out_ann, [0, ['Checksum: %d (%s)' % (
163 self.cache[self.cnt-1], csstr)]])
91b2e171
BV
164 self.state = 'extensions'
165 elif self.state == 'extensions':
166 pass
167
168 def ann_field(self, start, end, annotation):
09059016
UH
169 self.put(self.sn[start][0], self.sn[end][1],
170 self.out_ann, [ANN_FIELDS, [annotation]])
91b2e171
BV
171
172 def lookup_pnpid(self, pnpid):
1063646c 173 pnpid_file = os.path.join(os.path.dirname(__file__), 'pnpids.txt')
91b2e171
BV
174 if os.path.exists(pnpid_file):
175 for line in open(pnpid_file).readlines():
176 if line.find(pnpid + ';') == 0:
177 return line[4:].strip()
178 return ''
179
180 def decode_vid(self, offset):
181 pnpid = chr(64 + ((self.cache[offset] & 0x7c) >> 2))
182 pnpid += chr(64 + (((self.cache[offset] & 0x03) << 3)
183 | ((self.cache[offset+1] & 0xe0) >> 5)))
184 pnpid += chr(64 + (self.cache[offset+1] & 0x1f))
185 vendor = self.lookup_pnpid(pnpid)
186 if vendor:
09059016 187 pnpid += ' (%s)' % vendor
91b2e171
BV
188 self.ann_field(offset, offset+1, pnpid)
189
190 def decode_pid(self, offset):
09059016 191 pidstr = 'Product 0x%.2x%.2x' % (self.cache[offset+1], self.cache[offset])
91b2e171
BV
192 self.ann_field(offset, offset+1, pidstr)
193
194 def decode_serial(self, offset):
195 serialnum = (self.cache[offset+3] << 24) \
196 + (self.cache[offset+2] << 16) \
197 + (self.cache[offset+1] << 8) \
198 + self.cache[offset]
199 serialstr = ''
200 is_alnum = True
201 for i in range(4):
202 if not chr(self.cache[offset+3-i]).isalnum():
203 is_alnum = False
204 break
205 serialstr += chr(self.cache[offset+3-i])
09059016
UH
206 serial = serialstr if is_alnum else str(serialnum)
207 self.ann_field(offset, offset+3, 'Serial ' + serial)
91b2e171
BV
208
209 def decode_mfrdate(self, offset):
210 datestr = ''
211 if self.cache[offset]:
09059016 212 datestr += 'week %d, ' % self.cache[offset]
91b2e171
BV
213 datestr += str(1990 + self.cache[offset+1])
214 if datestr:
09059016 215 self.ann_field(offset, offset+1, 'Manufactured ' + datestr)
91b2e171
BV
216
217 def decode_basicdisplay(self, offset):
218 # Video input definition
219 vid = self.cache[offset]
220 if vid & 0x80:
221 # Digital
09059016 222 self.ann_field(offset, offset, 'Video input: VESA DFP 1.')
91b2e171
BV
223 else:
224 # Analog
225 sls = (vid & 60) >> 5
09059016 226 self.ann_field(offset, offset, 'Signal level standard: %.2x' % sls)
91b2e171 227 if vid & 0x10:
09059016 228 self.ann_field(offset, offset, 'Blank-to-black setup expected')
91b2e171
BV
229 syncs = ''
230 if vid & 0x08:
231 syncs += 'separate syncs, '
232 if vid & 0x04:
233 syncs += 'composite syncs, '
234 if vid & 0x02:
235 syncs += 'sync on green, '
236 if vid & 0x01:
237 syncs += 'Vsync serration required, '
238 if syncs:
09059016 239 self.ann_field(offset, offset, 'Supported syncs: %s' % syncs[:-2])
91b2e171
BV
240 # Max horizontal/vertical image size
241 if self.cache[offset+1] != 0 and self.cache[offset+2] != 0:
242 # Projectors have this set to 0
09059016
UH
243 sizestr = '%dx%dcm' % (self.cache[offset+1], self.cache[offset+2])
244 self.ann_field(offset+1, offset+2, 'Physical size: ' + sizestr)
91b2e171
BV
245 # Display transfer characteristic (gamma)
246 if self.cache[offset+3] != 0xff:
247 gamma = (self.cache[offset+3] + 100) / 100
09059016 248 self.ann_field(offset+3, offset+3, 'Gamma: %1.2f' % gamma)
91b2e171
BV
249 # Feature support
250 fs = self.cache[offset+4]
251 dpms = ''
252 if fs & 0x80:
253 dpms += 'standby, '
254 if fs & 0x40:
255 dpms += 'suspend, '
256 if fs & 0x20:
257 dpms += 'active off, '
258 if dpms:
09059016 259 self.ann_field(offset+4, offset+4, 'DPMS support: %s' % dpms[:-2])
91b2e171
BV
260 dt = (fs & 0x18) >> 3
261 dtstr = ''
262 if dt == 0:
263 dtstr = 'Monochrome'
264 elif dt == 1:
265 dtstr = 'RGB color'
266 elif dt == 2:
e4f82268 267 dtstr = 'non-RGB multicolor'
91b2e171 268 if dtstr:
09059016 269 self.ann_field(offset+4, offset+4, 'Display type: %s' % dtstr)
91b2e171 270 if fs & 0x04:
09059016 271 self.ann_field(offset+4, offset+4, 'Color space: standard sRGB')
91b2e171
BV
272 # Save this for when we decode the first detailed timing descriptor
273 self.have_preferred_timing = (fs & 0x02) == 0x02
274 if fs & 0x01:
275 gft = ''
276 else:
277 gft = 'not '
09059016
UH
278 self.ann_field(offset+4, offset+4,
279 'Generalized timing formula: %ssupported' % gft)
91b2e171
BV
280
281 def convert_color(self, value):
282 # Convert from 10-bit packet format to float
283 outval = 0.0
284 for i in range(10):
285 if value & 0x01:
286 outval += 2 ** -(10-i)
287 value >>= 1
288 return outval
289
290 def decode_chromaticity(self, offset):
291 redx = (self.cache[offset+2] << 2) + ((self.cache[offset] & 0xc0) >> 6)
292 redy = (self.cache[offset+3] << 2) + ((self.cache[offset] & 0x30) >> 4)
09059016
UH
293 self.ann_field(offset, offset+9, 'Chromacity red: X %1.3f, Y %1.3f' % (
294 self.convert_color(redx), self.convert_color(redy)))
91b2e171
BV
295
296 greenx = (self.cache[offset+4] << 2) + ((self.cache[offset] & 0x0c) >> 6)
297 greeny = (self.cache[offset+5] << 2) + ((self.cache[offset] & 0x03) >> 4)
09059016
UH
298 self.ann_field(offset, offset+9, 'Chromacity green: X %1.3f, Y %1.3f' % (
299 self.convert_color(greenx), self.convert_color(greeny)))
91b2e171
BV
300
301 bluex = (self.cache[offset+6] << 2) + ((self.cache[offset+1] & 0xc0) >> 6)
302 bluey = (self.cache[offset+7] << 2) + ((self.cache[offset+1] & 0x30) >> 4)
09059016
UH
303 self.ann_field(offset, offset+9, 'Chromacity blue: X %1.3f, Y %1.3f' % (
304 self.convert_color(bluex), self.convert_color(bluey)))
91b2e171
BV
305
306 whitex = (self.cache[offset+8] << 2) + ((self.cache[offset+1] & 0x0c) >> 6)
307 whitey = (self.cache[offset+9] << 2) + ((self.cache[offset+1] & 0x03) >> 4)
09059016
UH
308 self.ann_field(offset, offset+9, 'Chromacity white: X %1.3f, Y %1.3f' % (
309 self.convert_color(whitex), self.convert_color(whitey)))
91b2e171
BV
310
311 def decode_est_timing(self, offset):
312 # Pre-EDID modes
313 bitmap = (self.cache[offset] << 9) \
314 + (self.cache[offset+1] << 1) \
315 + ((self.cache[offset+2] & 0x80) >> 7)
316 modestr = ''
317 for i in range(17):
318 if bitmap & (1 << (16-i)):
319 modestr += est_modes[i] + ', '
320 if modestr:
09059016
UH
321 self.ann_field(offset, offset+2,
322 'Supported establised modes: %s' % modestr[:-2])
91b2e171
BV
323
324 def decode_std_timing(self, offset):
325 modestr = ''
326 for i in range(0, 16, 2):
327 if self.cache[offset+i] == 0x01 and self.cache[offset+i+1] == 0x01:
328 # Unused field
329 continue
330 x = (self.cache[offset+i] + 31) * 8
331 ratio = (self.cache[offset+i+1] & 0xc0) >> 6
332 ratio_x, ratio_y = xy_ratio[ratio]
333 y = x / ratio_x * ratio_y
334 refresh = (self.cache[offset+i+1] & 0x3f) + 60
09059016 335 modestr += '%dx%d@%dHz, ' % (x, y, refresh)
91b2e171 336 if modestr:
09059016
UH
337 self.ann_field(offset, offset+2,
338 'Supported standard modes: %s' % modestr[:-2])
91b2e171
BV
339
340 def decode_detailed_timing(self, offset):
341 if offset == -72 and self.have_preferred_timing:
342 # Only on first detailed timing descriptor
343 section = 'Preferred'
344 else:
345 section = 'Detailed'
346 section += ' timing descriptor'
347 self.put(self.sn[offset][0], self.sn[offset+18][1],
348 self.out_ann, [ANN_SECTIONS, [section]])
349
350 pixclock = float((self.cache[offset+1] << 8) + self.cache[offset]) / 100
09059016 351 self.ann_field(offset, offset+1, 'Pixel clock: %.2f MHz' % pixclock)
91b2e171
BV
352
353 horiz_active = ((self.cache[offset+4] & 0xf0) << 4) + self.cache[offset+2]
09059016 354 self.ann_field(offset+2, offset+4, 'Horizontal active: %d' % horiz_active)
91b2e171
BV
355
356 horiz_blank = ((self.cache[offset+4] & 0x0f) << 8) + self.cache[offset+3]
09059016 357 self.ann_field(offset+3, offset+4, 'Horizontal blanking: %d' % horiz_blank)
91b2e171
BV
358
359 vert_active = ((self.cache[offset+7] & 0xf0) << 4) + self.cache[offset+5]
09059016 360 self.ann_field(offset+5, offset+7, 'Vertical active: %d' % vert_active)
91b2e171
BV
361
362 vert_blank = ((self.cache[offset+7] & 0x0f) << 8) + self.cache[offset+6]
09059016 363 self.ann_field(offset+6, offset+7, 'Vertical blanking: %d' % vert_blank)
91b2e171
BV
364
365 horiz_sync_off = ((self.cache[offset+11] & 0xc0) << 2) + self.cache[offset+8]
09059016 366 self.ann_field(offset+8, offset+11, 'Horizontal sync offset: %d' % horiz_sync_off)
91b2e171
BV
367
368 horiz_sync_pw = ((self.cache[offset+11] & 0x30) << 4) + self.cache[offset+9]
09059016 369 self.ann_field(offset+9, offset+11, 'Horizontal sync pulse width: %d' % horiz_sync_pw)
91b2e171
BV
370
371 vert_sync_off = ((self.cache[offset+11] & 0x0c) << 2) \
372 + ((self.cache[offset+10] & 0xf0) >> 4)
09059016 373 self.ann_field(offset+10, offset+11, 'Vertical sync offset: %d' % vert_sync_off)
91b2e171
BV
374
375 vert_sync_pw = ((self.cache[offset+11] & 0x03) << 4) \
376 + (self.cache[offset+10] & 0x0f)
09059016 377 self.ann_field(offset+10, offset+11, 'Vertical sync pulse width: %d' % vert_sync_pw)
91b2e171
BV
378
379 horiz_size = ((self.cache[offset+14] & 0xf0) << 4) + self.cache[offset+12]
380 vert_size = ((self.cache[offset+14] & 0x0f) << 8) + self.cache[offset+13]
09059016 381 self.ann_field(offset+12, offset+14, 'Physical size: %dx%dmm' % (horiz_size, vert_size))
91b2e171
BV
382
383 horiz_border = self.cache[offset+15]
384 if horiz_border:
09059016 385 self.ann_field(offset+15, offset+15, 'Horizontal border: %d pixels' % horiz_border)
91b2e171
BV
386 vert_border = self.cache[offset+16]
387 if vert_border:
09059016 388 self.ann_field(offset+16, offset+16, 'Vertical border: %d lines' % vert_border)
91b2e171
BV
389
390 features = 'Flags: '
391 if self.cache[offset+17] & 0x80:
392 features += 'interlaced, '
393 stereo = (self.cache[offset+17] & 0x60) >> 5
394 if stereo:
395 if self.cache[offset+17] & 0x01:
396 features += '2-way interleaved stereo ('
09059016
UH
397 features += ['right image on even lines',
398 'left image on even lines',
91b2e171
BV
399 'side-by-side'][stereo-1]
400 features += '), '
401 else:
402 features += 'field sequential stereo ('
403 features += ['right image on sync=1', 'left image on sync=1',
404 '4-way interleaved'][stereo-1]
405 features += '), '
406 sync = (self.cache[offset+17] & 0x18) >> 3
407 sync2 = (self.cache[offset+17] & 0x06) >> 1
408 posneg = ['negative', 'positive']
409 features += 'sync type '
410 if sync == 0x00:
411 features += 'analog composite (serrate on RGB)'
412 elif sync == 0x01:
413 features += 'bipolar analog composite (serrate on RGB)'
414 elif sync == 0x02:
415 features += 'digital composite (serrate on composite polarity ' \
09059016 416 + (posneg[sync2 & 0x01]) + ')'
91b2e171
BV
417 elif sync == 0x03:
418 features += 'digital separate ('
2a706c20 419 features += 'Vsync polarity ' + (posneg[(sync2 & 0x02) >> 1])
91b2e171
BV
420 features += ', Hsync polarity ' + (posneg[sync2 & 0x01])
421 features += ')'
422 features += ', '
423 self.ann_field(offset+17, offset+17, features[:-2])
424
425 def decode_descriptor(self, offset):
426 tag = self.cache[offset+3]
427 if tag == 0xff:
428 # Monitor serial number
429 text = bytes(self.cache[offset+5:][:13]).decode(encoding='cp437', errors='replace')
09059016 430 self.ann_field(offset, offset+17, 'Serial number: %s' % text.strip())
91b2e171
BV
431 elif tag == 0xfe:
432 # Text
433 text = bytes(self.cache[offset+5:][:13]).decode(encoding='cp437', errors='replace')
09059016 434 self.ann_field(offset, offset+17, 'Info: %s' % text.strip())
91b2e171
BV
435 elif tag == 0xfc:
436 # Monitor name
437 text = bytes(self.cache[offset+5:][:13]).decode(encoding='cp437', errors='replace')
09059016 438 self.ann_field(offset, offset+17, 'Model name: %s' % text.strip())
91b2e171
BV
439 elif tag == 0xfd:
440 # Monitor range limits
441 self.put(self.sn[offset][0], self.sn[offset+17][1], self.out_ann,
09059016
UH
442 [ANN_SECTIONS, ['Monitor range limits']])
443 self.ann_field(offset+5, offset+5, 'Minimum vertical rate: %dHz' %
91b2e171 444 self.cache[offset+5])
09059016 445 self.ann_field(offset+6, offset+6, 'Maximum vertical rate: %dHz' %
91b2e171 446 self.cache[offset+6])
09059016 447 self.ann_field(offset+7, offset+7, 'Minimum horizontal rate: %dkHz' %
91b2e171 448 self.cache[offset+7])
09059016 449 self.ann_field(offset+8, offset+8, 'Maximum horizontal rate: %dkHz' %
91b2e171 450 self.cache[offset+8])
09059016 451 self.ann_field(offset+9, offset+9, 'Maximum pixel clock: %dMHz' %
91b2e171
BV
452 (self.cache[offset+9] * 10))
453 if self.cache[offset+10] == 0x02:
454 # Secondary GTF curve supported
09059016 455 self.ann_field(offset+10, offset+17, 'Secondary timing formula supported')
91b2e171
BV
456 elif tag == 0xfb:
457 # Additional color point data
458 self.put(self.sn[offset][0], self.sn[offset+17][1], self.out_ann,
09059016 459 [ANN_SECTIONS, ['Additional color point data']])
91b2e171
BV
460 elif tag == 0xfa:
461 # Additional standard timing definitions
462 self.put(self.sn[offset][0], self.sn[offset+17][1], self.out_ann,
09059016 463 [ANN_SECTIONS, ['Additional standard timing definitions']])
91b2e171
BV
464 else:
465 self.put(self.sn[offset][0], self.sn[offset+17][1], self.out_ann,
09059016 466 [ANN_SECTIONS, ['Unknown descriptor']])
91b2e171
BV
467
468 def decode_descriptors(self, offset):
469 # 4 consecutive 18-byte descriptor blocks
470 for i in range(offset, 0, 18):
471 if self.cache[i] != 0 and self.cache[i+1] != 0:
472 self.decode_detailed_timing(i)
473 else:
1063646c 474 if self.cache[i+2] == 0 or self.cache[i+4] == 0:
91b2e171
BV
475 self.decode_descriptor(i)
476