Hello everyone,

Proposed patch adds decoder of ST7735 TFT controller protocol. Currently
this decoder only allows to distinguish commands and data, and also shows
command names and their short descriptions.

Here is a screenshot:

https://afiskon.ru/s/43/ea50416b0f_temp.png

In this context I would like also to answer the question asked in IRC:

11:33 < uwe_> afiskon: sigrok-cli cannot throw exceptions, it's C code. do
you mean a Python exception or an error code or such? if yes, under which
circumstances? sounds like a bug

The circumstances are:

$ sigrok-cli -i ~/temp/st7735-minimal.sr \
  -P 'st7735:cs=CS:clk=SCLK:mosi=MOSI:dc=DC' \
  -A 'st7735=description'
...
st7735-1: CASET: Column address set
st7735-1: RASET: Row address set
st7735-1: RAMWR: Memory write

Note that the description of the last command RAMWR was successfully sent.
This is because unlike PulseView sigrok-cli makes 'self.wait()' to throw an
exception instead of hanging. The decoder interprets the exception as an
end of stream and decodes buffered data.

-- 
Best regards,
Aleksander Alekseev
From 4e9e31e490b9623eceb98371e6be128a4d394e58 Mon Sep 17 00:00:00 2001
From: Aleksander Alekseev <afis...@gmail.com>
Date: Sat, 5 May 2018 23:44:16 +0300
Subject: [PATCH] Simple decoder for ST7735 TFT controller

---
 decoders/st7735/__init__.py |  27 +++++
 decoders/st7735/pd.py       | 193 ++++++++++++++++++++++++++++++++++++
 2 files changed, 220 insertions(+)
 create mode 100644 decoders/st7735/__init__.py
 create mode 100644 decoders/st7735/pd.py

diff --git a/decoders/st7735/__init__.py b/decoders/st7735/__init__.py
new file mode 100644
index 0000000..e724af6
--- /dev/null
+++ b/decoders/st7735/__init__.py
@@ -0,0 +1,27 @@
+##
+## This file is part of the libsigrokdecode project.
+##
+## Copyright (C) 2018 Aleksander Alekseev <afis...@gmail.com>
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 2 of the License, or
+## (at your option) any later version.
+##
+## This program 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 General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program; if not, see <http://www.gnu.org/licenses/>.
+##
+
+'''
+This decoder decodes the ST7735 TFT controller protocol.
+
+Details:
+http://www.displayfuture.com/Display/datasheet/controller/ST7735.pdf
+'''
+
+from .pd import Decoder
diff --git a/decoders/st7735/pd.py b/decoders/st7735/pd.py
new file mode 100644
index 0000000..dc81307
--- /dev/null
+++ b/decoders/st7735/pd.py
@@ -0,0 +1,193 @@
+## vim: set ai et ts=4 sw=4:
+##
+## This file is part of the libsigrokdecode project.
+##
+## Copyright (C) 2018 Aleksander Alekseev <afis...@gmail.com>
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 2 of the License, or
+## (at your option) any later version.
+##
+## This program 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 General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program; if not, see <http://www.gnu.org/licenses/>.
+
+import sigrokdecode as srd
+
+MAX_DATA_LEN = 128
+
+# Command Id -> Name, Short Description
+META = {
+    0x00: {"name": 'NOP    ', "desc": 'No Operation'},
+    0x01: {"name": 'SWRESET', "desc": 'Software reset'},
+    0x04: {"name": 'RDDID  ', "desc": 'Read Display ID'},
+    0x09: {"name": 'RDDST  ', "desc": 'Read Display Status'},
+    0x10: {"name": 'SLPIN  ', "desc": 'Sleep in & booster off'},
+    0x11: {"name": 'SLPOUT ', "desc": 'Sleep out & booster on'},
+    0x12: {"name": 'PTLON  ', "desc": 'Partial mode on'},
+    0x13: {"name": 'NORON  ', "desc": 'Partial off (Normal)'},
+    0x20: {"name": 'INVOFF ', "desc": 'Display inversion off'},
+    0x21: {"name": 'INVON  ', "desc": 'Display inversion on'},
+    0x28: {"name": 'DISPOFF', "desc": 'Display off'},
+    0x29: {"name": 'DISPON ', "desc": 'Display on'},
+    0x2A: {"name": 'CASET  ', "desc": 'Column address set'},
+    0x2B: {"name": 'RASET  ', "desc": 'Row address set'},
+    0x2C: {"name": 'RAMWR  ', "desc": 'Memory write'},
+    0x2E: {"name": 'RAMRD  ', "desc": 'Memory read'},
+    0x30: {"name": 'PTLAR  ', "desc": 'Partial start/end address set'},
+    0x36: {"name": 'MADCTL ', "desc": 'Memory data address control'},
+    0x3A: {"name": 'COLMOD ', "desc": 'Interface pixel format'},
+    0xB1: {"name": 'FRMCTR1', "desc": 'Frame rate control (in normal mode / full colors)'},
+    0xB2: {"name": 'FRMCTR2', "desc": 'Frame rate control (in idle mode / 8-colors)'},
+    0xB3: {"name": 'FRMCTR3', "desc": 'Frame rate control (in partial mode / full colors) '},
+    0xB4: {"name": 'INVCTR ', "desc": 'Display inversion control'},
+    0xB6: {"name": 'DISSET5', "desc": 'Display Function set 5'},
+    0xC0: {"name": 'PWCTR1 ', "desc": 'Power control 1'},
+    0xC1: {"name": 'PWCTR2 ', "desc": 'Power control 2'},
+    0xC2: {"name": 'PWCTR3 ', "desc": 'Power control 3'},
+    0xC3: {"name": 'PWCTR4 ', "desc": 'Power control 4'},
+    0xC4: {"name": 'PWCTR5 ', "desc": 'Power control 5'},
+    0xC5: {"name": 'VMCTR1 ', "desc": 'VCOM Control 1'},
+    0xDA: {"name": 'RDID1  ', "desc": 'Read ID1'},
+    0xDB: {"name": 'RDID2  ', "desc": 'Read ID2'},
+    0xDC: {"name": 'RDID3  ', "desc": 'Read ID3'},
+    0xDD: {"name": 'RDID4  ', "desc": 'Read ID4'},
+    0xFC: {"name": 'PWCTR6 ', "desc": 'Power control 6'},
+    0xE0: {"name": 'GMCTRP1', "desc": 'Gamma \'+\'polarity correction characteristics setting'},
+    0xE1: {"name": 'GMCTRN1', "desc": 'Gamma \'-\'polarity correction characteristics setting'},
+}
+
+class Ann:
+    BITS, CMD, DATA, DESC = range(4)
+
+class Decoder(srd.Decoder):
+    api_version = 3
+    id = 'st7735' # this is shown in `sigrok-cli -L`
+    name = 'ST7735' # this is shown in PulseView
+    longname = 'ST7735 TFT controller' # this is shown in `sigrok-cli -L`
+    desc = 'Decoder of ST7735 TFT controller protocol'
+    license = 'gplv2+'
+    inputs = ['logic']
+    outputs = ['st7735']
+    channels = (
+        {'id': 'cs', 'name': 'CS#', 'desc': 'Chip-select'},
+        {'id': 'clk', 'name': 'CLK', 'desc': 'Clock'},
+        {'id': 'mosi', 'name': 'MOSI', 'desc': 'Master out, slave in'},
+        {'id': 'dc', 'name': 'DC', 'desc': 'Data or command'}
+    )
+    optional_channels = ( )
+    annotations = (
+        ('bits', 'Bits'),               # Ann.BITS
+        ('command', 'Command'),         # Ann.CMD
+        ('data', 'Data'),               # Ann.DATA
+        ('description', 'Description'), # Ann.DESC
+    )
+    annotation_rows = (
+        ('bits', 'Bits', (Ann.BITS,)),
+        ('fields', 'Fields', (Ann.CMD, Ann.DATA,)),
+        ('description', 'Description', (Ann.DESC,)),
+    )
+    options = ( )
+
+    def __init__(self):
+        self.reset()
+
+    def reset(self):
+        self.samplenum = 0
+        pass # do nothing, yet
+
+    # This function is called before the beginning of the decoding. This is the
+    # place to register() the output types, check the user-supplied PD options
+    # for validity, and so on
+    def start(self):
+        self.out_ann = self.register(srd.OUTPUT_ANN)
+
+    def reset_state(self):
+        self.accum_byte = 0
+        self.accum_bits_num = 0
+        self.bit_ss = -1
+        self.byte_ss = -1
+        self.current_bit = -1
+
+    def output_description(self, start, end, command, data):
+        if command == -1:
+            return
+
+        if META[command]:
+            self.put(start, end, self.out_ann,
+                [Ann.DESC,
+                    ["%s: %s" % (META[command]['name'].strip(), META[command]['desc'])]
+                ])
+        else:
+            # Default description:
+            dots = ""
+            if len(data) == MAX_DATA_LEN:
+                data = data[:-1]
+                dots = "..."
+
+            data_str = "(none)"
+            if len(data) > 0:
+                data_str = " ".join(["%02X" % b for b in data ])
+
+            self.put(start, end, self.out_ann,
+                [Ann.DESC,
+                    ["Unknown command: %02X. Data: %s%s" % (command, data_str, dots)]
+                ])
+
+    def decode(self):
+        current_command = -1
+        current_data = []
+        desc_ss = -1 # description begin
+        desc_es = -1 # description end
+        self.reset_state()
+        while(True):
+            # Check data on both CLK edges
+            try:
+                (cs, clk, mosi, dc) = self.wait( { 1:"e" } )
+            except:
+                break
+
+            if cs == 1: # wait for CS = LOW, ignore the rest
+                self.reset_state()
+                continue
+             
+            if (clk == 1):
+                # read one bit
+                self.bit_ss = self.samplenum
+                if self.accum_bits_num == 0:
+                    self.byte_ss = self.samplenum
+                self.current_bit = mosi
+
+            if (clk == 0) and (self.current_bit >= 0):
+                # process one bit
+                self.put(self.bit_ss, self.samplenum, self.out_ann, [Ann.BITS, [str(self.current_bit)]])
+                self.accum_byte = (self.accum_byte << 1) | self.current_bit # msb-first
+                self.accum_bits_num += 1
+                if self.accum_bits_num == 8:
+                    # process one byte
+                    ann = Ann.DATA if dc else Ann.CMD # DC = LOW for commands
+                    self.put(self.byte_ss, self.samplenum, self.out_ann, [ann, ["%02X" % self.accum_byte]])
+                    if ann == Ann.CMD:
+                        self.output_description(desc_ss, desc_es, current_command, current_data)
+                        desc_ss = self.byte_ss
+                        desc_es = self.samplenum # for commands without any additional data
+                        current_command = self.accum_byte
+                        current_data = []
+                    else:
+                        if len(current_data) < MAX_DATA_LEN:
+                            current_data += [ self.accum_byte ]
+                        desc_es = self.samplenum
+
+                    self.accum_bits_num = 0
+                    self.accum_byte = 0
+                    self.byte_ss = -1
+                self.current_bit = -1
+                self.bit_ss = -1
+
+        # for the rest of the accumulated data
+        self.output_description(desc_ss, desc_es, current_command, current_data)
-- 
2.17.0

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
sigrok-devel mailing list
sigrok-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sigrok-devel

Reply via email to