From f697102ece64f8e99bfd26bbc2dedadaa037a0e5 Mon Sep 17 00:00:00 2001
From: Gerhard Sittig <gerhard.sittig@gmx.net>
Date: Thu, 18 Mar 2021 19:14:54 +0100
Subject: ieee488: support optional parity for ATN commands (for HP gear)

HP gear is said to sometimes send commands (ATN asserted) with a parity.
Introduce an option to check and strip the MSB before interpretation.

Reported-By: Anders Gustafsson <Anders.Gustafsson@pedago.fi>
---
 decoders/ieee488/pd.py | 9 +++++++++
 1 file changed, 9 insertions(+)

(limited to 'decoders/ieee488')

diff --git a/decoders/ieee488/pd.py b/decoders/ieee488/pd.py
index 4531cb3..0451c14 100644
--- a/decoders/ieee488/pd.py
+++ b/decoders/ieee488/pd.py
@@ -288,6 +288,8 @@ class Decoder(srd.Decoder):
             'default': 'no', 'values': ('no', 'yes')},
         {'id': 'delim', 'desc': 'Payload data delimiter',
             'default': 'eol', 'values': ('none', 'eol')},
+        {'id': 'atn_parity', 'desc': 'ATN commands use parity',
+            'default': 'no', 'values': ('no', 'yes')},
     )
     annotations = (
         ('bit', 'IEC bit'),
@@ -486,6 +488,13 @@ class Decoder(srd.Decoder):
             upd_iec = False,
             py_type = None
             py_peers = False
+            if self.options['atn_parity'] == 'yes':
+                par = 1 if b & 0x80 else 0
+                b &= ~0x80
+                ones = bin(b).count('1') + par
+                if ones % 2:
+                    warn_texts = ['Command parity error', 'parity', 'PAR']
+                    self.emit_warn_ann(self.ss_raw, self.es_raw, warn_texts)
             is_cmd, is_unl, is_unt = _is_command(b)
             laddr = _is_listen_addr(b)
             taddr = _is_talk_addr(b)
-- 
cgit v1.2.3-70-g09d2