From dd93608234524d07b1a301d13bb517a967e1ffcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Martin=C4=8Dek?= Date: Thu, 20 Jan 2022 11:33:22 +0100 Subject: [PATCH] Added the source code of the project --- README.md | 1 + ds1307fixed/__init__.py | 26 ++++ ds1307fixed/pd.py | 307 +++++++++++++++++++++++++++++++++++++ extractor_i2c/__init__.py | 24 +++ extractor_i2c/pd.py | 103 +++++++++++++ extractor_spi/__init__.py | 24 +++ extractor_spi/pd.py | 78 ++++++++++ extractor_uart/__init__.py | 24 +++ extractor_uart/pd.py | 83 ++++++++++ packeter/__init__.py | 26 ++++ packeter/pd.py | 205 +++++++++++++++++++++++++ 11 files changed, 901 insertions(+) create mode 100644 ds1307fixed/__init__.py create mode 100644 ds1307fixed/pd.py create mode 100644 extractor_i2c/__init__.py create mode 100644 extractor_i2c/pd.py create mode 100644 extractor_spi/__init__.py create mode 100644 extractor_spi/pd.py create mode 100644 extractor_uart/__init__.py create mode 100644 extractor_uart/pd.py create mode 100644 packeter/__init__.py create mode 100644 packeter/pd.py diff --git a/README.md b/README.md index e69de29..065b252 100644 --- a/README.md +++ b/README.md @@ -0,0 +1 @@ +Prázdny súbor \ No newline at end of file diff --git a/ds1307fixed/__init__.py b/ds1307fixed/__init__.py new file mode 100644 index 0000000..d722a49 --- /dev/null +++ b/ds1307fixed/__init__.py @@ -0,0 +1,26 @@ +## +## This file is part of the libsigrokdecode project. +## +## Copyright (C) 2013 Matt Ranostay +## Copyright (C) 2022 Martinček Matej +## +## 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 . +## + +''' +This decoder stacks on top of the 'i2c' PD and decodes the Dallas DS1307 +real-time clock (RTC) specific registers and commands. +''' + +from .pd import Decoder diff --git a/ds1307fixed/pd.py b/ds1307fixed/pd.py new file mode 100644 index 0000000..8556d67 --- /dev/null +++ b/ds1307fixed/pd.py @@ -0,0 +1,307 @@ +## +## This file is part of the libsigrokdecode project. +## +## Copyright (C) 2012-2020 Uwe Hermann +## Copyright (C) 2013 Matt Ranostay +## Copyright (C) 2022 Martinček Matej +## +## 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 . +## + +import re +import sigrokdecode as srd +from common.srdhelper import bcd2int, SrdIntEnum + +days_of_week = ( + 'Sunday', 'Monday', 'Tuesday', 'Wednesday', + 'Thursday', 'Friday', 'Saturday', +) + +regs = ( + 'Seconds', 'Minutes', 'Hours', 'Day', 'Date', 'Month', 'Year', + 'Control', 'RAM', +) + +bits = ( + 'Clock halt', 'Seconds', 'Reserved', 'Minutes', '12/24 hours', 'AM/PM', + 'Hours', 'Day', 'Date', 'Month', 'Year', 'OUT', 'SQWE', 'RS', 'RAM', +) + +rates = { + 0b00: '1Hz', + 0b01: '4096Hz', + 0b10: '8192Hz', + 0b11: '32768Hz', +} + +DS1307_I2C_ADDRESS = 0x68 + +def regs_and_bits(): + l = [('reg_' + r.lower(), r + ' register') for r in regs] + l += [('bit_' + re.sub('\/| ', '_', b).lower(), b + ' bit') for b in bits] + return tuple(l) + +a = ['REG_' + r.upper() for r in regs] + \ + ['BIT_' + re.sub('\/| ', '_', b).upper() for b in bits] + \ + ['READ_DATE_TIME', 'WRITE_DATE_TIME', 'READ_REG', 'WRITE_REG', 'WARNING'] +Ann = SrdIntEnum.from_list('Ann', a) + + +class Decoder(srd.Decoder): + api_version = 3 + id = 'ds1307fixed' + name = 'DS1307 fixed' + longname = 'Dallas DS1307 Fixed Version' + desc = 'Fixed Dallas DS1307 realtime clock module protocol.' + license = 'gplv2+' + inputs = ['i2c'] + outputs = [] + tags = ['Clock/timing', 'IC'] + annotations = regs_and_bits() + ( + ('read_date_time', 'Read date/time'), + ('write_date_time', 'Write date/time'), + ('read_reg', 'Register read'), + ('write_reg', 'Register write'), + ('warning', 'Warning'), + ) + annotation_rows = ( + ('bits', 'Bits', Ann.prefixes('BIT_')), + ('regs', 'Registers', Ann.prefixes('REG_')), + ('date_time', 'Date/time', Ann.prefixes('READ_ WRITE_')), + ('warnings', 'Warnings', (Ann.WARNING,)), + ) + + def __init__(self): + self.reset() + + def reset(self): + self.state = 'IDLE' + self.hours = -1 + self.minutes = -1 + self.seconds = -1 + self.days = -1 + self.date = -1 + self.months = -1 + self.years = -1 + self.bits = [] + + def start(self): + self.out_ann = self.register(srd.OUTPUT_ANN) + + def putx(self, data): + self.put(self.ss, self.es, self.out_ann, data) + + def putd(self, bit1, bit2, data): + self.put(self.bits[bit1][1], self.bits[bit2][2], self.out_ann, data) + + def putr(self, bit): + self.put(self.bits[bit][1], self.bits[bit][2], self.out_ann, + [Ann.BIT_RESERVED, ['Reserved bit', 'Reserved', 'Rsvd', 'R']]) + + def handle_reg_0x00(self, b): # Seconds (0-59) / Clock halt bit + self.putd(7, 0, [Ann.REG_SECONDS, ['Seconds', 'Sec', 'S']]) + ch = 1 if (b & (1 << 7)) else 0 + self.putd(7, 7, [Ann.BIT_CLOCK_HALT, ['Clock halt: %d' % ch, + 'Clk hlt: %d' % ch, 'CH: %d' % ch, 'CH']]) + s = self.seconds = bcd2int(b & 0x7f) + self.putd(6, 0, [Ann.BIT_SECONDS, ['Second: %d' % s, 'Sec: %d' % s, + 'S: %d' % s, 'S']]) + + def handle_reg_0x01(self, b): # Minutes (0-59) + self.putd(7, 0, [Ann.REG_MINUTES, ['Minutes', 'Min', 'M']]) + self.putr(7) + m = self.minutes = bcd2int(b & 0x7f) + self.putd(6, 0, [Ann.BIT_MINUTES, ['Minute: %d' % m, 'Min: %d' % m, 'M: %d' % m, 'M']]) + + def handle_reg_0x02(self, b): # Hours (1-12+AM/PM or 0-23) + self.putd(7, 0, [Ann.REG_HOURS, ['Hours', 'H']]) + self.putr(7) + ampm_mode = True if (b & (1 << 6)) else False + if ampm_mode: + self.putd(6, 6, [Ann.BIT_12_24_HOURS, ['12-hour mode', '12h mode', '12h']]) + a = 'PM' if (b & (1 << 5)) else 'AM' + self.putd(5, 5, [Ann.BIT_AM_PM, [a, a[0]]]) + h = self.hours = bcd2int(b & 0x1f) + self.putd(4, 0, [Ann.BIT_HOURS, ['Hour: %d' % h, 'H: %d' % h, 'H']]) + else: + self.putd(6, 6, [Ann.BIT_12_24_HOURS, ['24-hour mode', '24h mode', '24h']]) + h = self.hours = bcd2int(b & 0x3f) + self.putd(5, 0, [Ann.BIT_HOURS, ['Hour: %d' % h, 'H: %d' % h, 'H']]) + + def handle_reg_0x03(self, b): # Day / day of week (1-7) + self.putd(7, 0, [Ann.REG_DAY, ['Day of week', 'Day', 'D']]) + for i in (7, 6, 5, 4, 3): + self.putr(i) + w = self.days = bcd2int(b & 0x07) + ws = days_of_week[self.days - 1] + self.putd(2, 0, [Ann.BIT_DAY, ['Weekday: %s' % ws, 'WD: %s' % ws, 'WD', 'W']]) + + def handle_reg_0x04(self, b): # Date (1-31) + self.putd(7, 0, [Ann.REG_DATE, ['Date', 'D']]) + for i in (7, 6): + self.putr(i) + d = self.date = bcd2int(b & 0x3f) + self.putd(5, 0, [Ann.BIT_DATE, ['Date: %d' % d, 'D: %d' % d, 'D']]) + + def handle_reg_0x05(self, b): # Month (1-12) + self.putd(7, 0, [Ann.REG_MONTH, ['Month', 'Mon', 'M']]) + for i in (7, 6, 5): + self.putr(i) + m = self.months = bcd2int(b & 0x1f) + self.putd(4, 0, [Ann.BIT_MONTH, ['Month: %d' % m, 'Mon: %d' % m, 'M: %d' % m, 'M']]) + + def handle_reg_0x06(self, b): # Year (0-99) + self.putd(7, 0, [Ann.REG_YEAR, ['Year', 'Y']]) + y = self.years = bcd2int(b & 0xff) + self.years += 2000 + self.putd(7, 0, [Ann.BIT_YEAR, ['Year: %d' % y, 'Y: %d' % y, 'Y']]) + + def handle_reg_0x07(self, b): # Control Register + self.putd(7, 0, [Ann.REG_CONTROL, ['Control', 'Ctrl', 'C']]) + for i in (6, 5, 3, 2): + self.putr(i) + o = 1 if (b & (1 << 7)) else 0 + s = 1 if (b & (1 << 4)) else 0 + s2 = 'en' if (b & (1 << 4)) else 'dis' + r = rates[b & 0x03] + self.putd(7, 7, [Ann.BIT_OUT, ['Output control: %d' % o, + 'OUT: %d' % o, 'O: %d' % o, 'O']]) + self.putd(4, 4, [Ann.BIT_SQWE, ['Square wave output: %sabled' % s2, + 'SQWE: %sabled' % s2, 'SQWE: %d' % s, 'S: %d' % s, 'S']]) + self.putd(1, 0, [Ann.BIT_RS, ['Square wave output rate: %s' % r, + 'Square wave rate: %s' % r, 'SQW rate: %s' % r, 'Rate: %s' % r, + 'RS: %s' % s, 'RS', 'R']]) + + def handle_reg_0x3f(self, b): # RAM (bytes 0x08-0x3f) + self.putd(7, 0, [Ann.REG_RAM, ['RAM', 'R']]) + self.putd(7, 0, [Ann.BIT_RAM, ['SRAM: 0x%02X' % b, '0x%02X' % b]]) + + def get_stored_values(self): + output = "" + if self.seconds != -1: + output = output + "Seconds: " + str(self.seconds) + " " + if self.minutes != -1: + output = output + "Minutes: " + str(self.minutes) + " " + if self.hours != -1: + output = output + "Hours: " + str(self.hours) + " " + if self.days != -1: + output = output + "Day of week: " + str(days_of_week[self.days-1]) + " " + if self.date != -1: + output = output + "Date: " + str(self.date) + " " + if self.months != -1: + output = output + "Months: " + str(self.months) + " " + if self.years != -1: + output = output + "Years: " + str(self.years) + return output + + def reset_stored_values(self): + self.hours = -1 + self.minutes = -1 + self.seconds = -1 + self.days = -1 + self.date = -1 + self.months = -1 + self.years = -1 + + def output_datetime(self, cls, rw): + if (self.days == -1 or self.date == -1 or self.months == -1 or self.years == -1 + or self.hours == -1 or self.minutes == -1 or self.seconds == -1): + d = self.get_stored_values() + else: + d = '%s, %02d.%02d.%4d %02d:%02d:%02d' % ( + days_of_week[self.days], self.date, self.months, + self.years, self.hours, self.minutes, self.seconds) + + self.put(self.ss_block, self.es, self.out_ann, + [cls, ['%s date/time: %s' % (rw, d)]]) + self.reset_stored_values() + + def handle_reg(self, b): + r = self.reg if self.reg < 8 else 0x3f + fn = getattr(self, 'handle_reg_0x%02x' % r) + fn(b) + # Honor address auto-increment feature of the DS1307. When the + # address reaches 0x3f, it will wrap around to address 0. + self.reg += 1 + if self.reg > 0x3f: + self.reg = 0 + + def is_correct_chip(self, addr): + if addr == DS1307_I2C_ADDRESS: + return True + self.put(self.ss_block, self.es, self.out_ann, + [Ann.WARNING, ['Ignoring non-DS1307 data (slave 0x%02X)' % addr]]) + return False + + def decode(self, ss, es, data): + cmd, databyte = data + + # Collect the 'BITS' packet, then return. The next packet is + # guaranteed to belong to these bits we just stored. + if cmd == 'BITS': + self.bits = databyte + return + + # Store the start/end samples of this I²C packet. + self.ss, self.es = ss, es + + # State machine. + if self.state == 'IDLE': + # Wait for an I²C START condition. + if cmd != 'START': + return + self.state = 'GET SLAVE ADDR' + self.ss_block = ss + elif self.state == 'GET SLAVE ADDR': + # Wait for an address read/write operation. + if cmd != 'ADDRESS WRITE' and cmd != 'ADDRESS READ': + return + if not self.is_correct_chip(databyte): + self.state = 'IDLE' + return + if cmd == 'ADDRESS WRITE': + self.state = 'GET REG ADDR' + elif cmd == 'ADDRESS READ': + self.state = 'READ RTC REGS' + elif self.state == 'GET REG ADDR': + # Wait for a data write (master selects the slave register). + if cmd != 'DATA WRITE': + return + self.reg = databyte + self.state = 'WRITE RTC REGS' + elif self.state == 'WRITE RTC REGS': + # If we see a Repeated Start here, it's an RTC read. + if cmd == 'START REPEAT': + self.state = 'GET SLAVE READ ADDR' + return + # Otherwise: Get data bytes until a STOP condition occurs. + if cmd == 'DATA WRITE': + self.handle_reg(databyte) + elif cmd == 'STOP': + self.output_datetime(Ann.WRITE_DATE_TIME, 'Written') + self.state = 'IDLE' + elif self.state == 'GET SLAVE READ ADDR': + # Wait for an address read operation. + if cmd != 'ADDRESS READ': + return + if not self.is_correct_chip(databyte): + self.state = 'IDLE' + return + self.state = 'READ RTC REGS' + elif self.state == 'READ RTC REGS': + if cmd == 'DATA READ': + self.handle_reg(databyte) + elif cmd == 'STOP': + self.output_datetime(Ann.READ_DATE_TIME, 'Read') + self.state = 'IDLE' diff --git a/extractor_i2c/__init__.py b/extractor_i2c/__init__.py new file mode 100644 index 0000000..41f31e1 --- /dev/null +++ b/extractor_i2c/__init__.py @@ -0,0 +1,24 @@ +## +## This file is part of the libsigrokdecode project. +## +## Copyright (C) 2022 Matej Martinček +## +## 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 . +## + +''' +This decoder stacks on top of the 'I2C' PD and outputs individual address and data bytes values decoded by 'I2C' PD. +''' + +from .pd import Decoder diff --git a/extractor_i2c/pd.py b/extractor_i2c/pd.py new file mode 100644 index 0000000..c3387cb --- /dev/null +++ b/extractor_i2c/pd.py @@ -0,0 +1,103 @@ +## +## This file is part of the libsigrokdecode project. +## +## Copyright (C) 2022 Matej Martinček +## +## 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 . +## + +''' +OUTPUT_PYTHON format: + +Packet: +[, ] + +: + - 'ADDRESS + - 'DATA' + - 'PACKET END' (: 0) + + is the data or address byte associated with the 'ADDRESS' or 'DATA' +command. +''' + +import re +import sigrokdecode as srd +from common.srdhelper import bcd2int, SrdIntEnum + +class Decoder(srd.Decoder): + api_version = 3 + id = 'i2c_extractor' + name = 'I2C bytes extractor' + longname = 'The I2C bytes extractor' + desc = 'Extracts data and address bytes from I2C communication and potentially sends them to other stack-decoders' + license = 'gplv2+' + inputs = ['i2c'] + outputs = ['dataBytes'] + tags = ['Embedded/industrial'] + annotations = () + annotation_rows = () + + def __init__(self): + self.reset() + + def reset(self): + self.state = 'INACTIVE' + self.bits = [] + + def start(self): + self.out_python = self.register(srd.OUTPUT_PYTHON) + + + def putp(self, data): + self.put(self.ss, self.es, self.out_python, data) + + def send_address_byte(self, b): + # send address byte of type ADDRESS + self.putp(['ADDRESS', b]) + + def send_data_byte(self, b): + # send data byte of type DATA + self.putp(['DATA', b]) + + def decode(self, ss, es, data): + cmd, data_byte = data + + # Collect the 'BITS' packet, then return. The next packet is + # guaranteed to belong to these bits we just stored. + if cmd == 'BITS': + self.bits = data_byte + return + + # Store the start/end samples of this I²C packet. + self.ss, self.es = ss, es + + # State machine. + if cmd == 'START' or cmd == 'START REPEAT': + self.state = 'ACTIVE' + # START REPEAT means an end of a packet + if cmd == 'START REPEAT': + self.putp(['PACKET END', 0]) + return + elif cmd == 'STOP': + self.state = 'INACTIVE' + # STOP means an end of a packet + self.putp(['PACKET END', 0]) + return + + if self.state == 'ACTIVE': + if cmd == 'ADDRESS WRITE' or cmd == 'ADDRESS READ': + self.send_address_byte(data_byte) + elif cmd == 'DATA WRITE' or cmd == 'DATA READ': + self.send_data_byte(data_byte) diff --git a/extractor_spi/__init__.py b/extractor_spi/__init__.py new file mode 100644 index 0000000..8d86321 --- /dev/null +++ b/extractor_spi/__init__.py @@ -0,0 +1,24 @@ +## +## This file is part of the libsigrokdecode project. +## +## Copyright (C) 2022 Matej Martinček +## +## 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 . +## + +''' +This decoder stacks on top of the 'SPI' PD and outputs the individual MOSI/MISO data values decoded by 'SPI' PD. +''' + +from .pd import Decoder diff --git a/extractor_spi/pd.py b/extractor_spi/pd.py new file mode 100644 index 0000000..c380ba4 --- /dev/null +++ b/extractor_spi/pd.py @@ -0,0 +1,78 @@ +## +## This file is part of the libsigrokdecode project. +## +## Copyright (C) 2022 Matej Martinček +## +## 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 . +## + +''' +OUTPUT_PYTHON format: + +Packet: +[, ] + +: + - 'DATA' + - 'PACKET END' (: 0) + + is the data byte associated with the 'DATA' +command. +''' + +import re +import sigrokdecode as srd +from common.srdhelper import bcd2int, SrdIntEnum + +class Decoder(srd.Decoder): + api_version = 3 + id = 'spi_extractor' + name = 'SPI bytes extractor' + longname = 'The SPI bytes extractor' + desc = 'Extracts MOSI/MISO data values from SPI communication and potentially sends them to other stack-decoders' + license = 'gplv2+' + inputs = ['spi'] + outputs = ['dataBytes'] + tags = ['Embedded/industrial'] + annotations = () + annotation_rows = () + + def __init__(self): + self.reset() + + def reset(self): + self.state = 'INACTIVE' + + def start(self): + self.out_python = self.register(srd.OUTPUT_PYTHON) + + def putp(self, data): + self.put(self.ss, self.es, self.out_python, data) + + def send_data(self, b): + # Send received data packet of type DATA + self.putp(['DATA', b]) + + def decode(self, ss, es, data): + cmd, mosi, miso = data + + self.ss, self.es = ss, es + + if cmd == 'DATA': + # If MOSI is sending a packet, pass it on as a single packet + if mosi is not None: + self.send_data(mosi) + # If MISO is sending a packet, pass it on as a single packet + if miso is not None: + self.send_data(miso) diff --git a/extractor_uart/__init__.py b/extractor_uart/__init__.py new file mode 100644 index 0000000..e028cf2 --- /dev/null +++ b/extractor_uart/__init__.py @@ -0,0 +1,24 @@ +## +## This file is part of the libsigrokdecode project. +## +## Copyright (C) 2022 Matej Martinček +## +## 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 . +## + +''' +This decoder stacks on top of the 'UART' PD and outputs the values of individual packets decoded by 'UART' PD. +''' + +from .pd import Decoder diff --git a/extractor_uart/pd.py b/extractor_uart/pd.py new file mode 100644 index 0000000..4b0425c --- /dev/null +++ b/extractor_uart/pd.py @@ -0,0 +1,83 @@ +## +## This file is part of the libsigrokdecode project. +## +## Copyright (C) 2022 Matej Martinček +## +## 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 . +## + +''' +OUTPUT_PYTHON format: + +Packet: +[, ] + +: + - 'ADDRESS + - 'DATA' + - 'PACKET END' (: 0) + + is the data value associated with the 'DATA' +command. +''' + +import re +import sigrokdecode as srd +from common.srdhelper import bcd2int, SrdIntEnum + +class Decoder(srd.Decoder): + api_version = 3 + id = 'uart_extractor' + name = 'UART bytes extractor' + longname = 'The UART bytes extractor' + desc = 'Extracts data packets values from UART communication and potentially sends them to other stack-decoders' + license = 'gplv2+' + inputs = ['uart'] + outputs = ['dataBytes'] + tags = ['Embedded/industrial'] + annotations = () + annotation_rows = () + + def __init__(self): + self.reset() + + def reset(self): + self.state = 'INACTIVE' + + def start(self): + self.out_python = self.register(srd.OUTPUT_PYTHON) + + def putp(self, data): + self.put(self.ss, self.es, self.out_python, data) + + def send_data_value(self, b): + # Send value of received data of type DATA + self.putp(['DATA', b]) + + def decode(self, ss, es, data): + cmd, rxtx, data_value_and_bits = data + + # Store the start/end samples of this packet. + self.ss, self.es = ss, es + + # State machine. + if cmd == 'STARTBIT': + self.state = 'ACTIVE' + elif cmd == 'STOPBIT': + self.state = 'INACTIVE' + # STOPBIT means end of a packet + self.putp(['PACKET END', 0]) + elif cmd == 'DATA': + # Send just value of received data packet without individual bits + self.send_data_value(data_value_and_bits[0]) diff --git a/packeter/__init__.py b/packeter/__init__.py new file mode 100644 index 0000000..c3b1f21 --- /dev/null +++ b/packeter/__init__.py @@ -0,0 +1,26 @@ +## +## This file is part of the libsigrokdecode project. +## +## Copyright (C) 2022 Matej Martinček +## +## 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 . +## + +''' +This decoder stacks on top of one of the 'extractor' decoders (or other stack-decoders having dataBytes output). +It allows for a more complex manipulation with received data packets by combining them into potentially larger data +packets according to given options. +''' + +from .pd import Decoder diff --git a/packeter/pd.py b/packeter/pd.py new file mode 100644 index 0000000..e3152c4 --- /dev/null +++ b/packeter/pd.py @@ -0,0 +1,205 @@ +## +## This file is part of the libsigrokdecode project. +## +## Copyright (C) 2022 Matej Martinček +## +## 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 . +## + + +import re +import sigrokdecode as srd +from common.srdhelper import bcd2int, SrdIntEnum +import math + +a = ['ADDRESS', 'DATA', ] + +Ann = SrdIntEnum.from_list('Ann', a) + + +class Decoder(srd.Decoder): + api_version = 3 + id = 'packeter' + name = 'Packeter' + longname = 'The Packeter' + desc = 'Alters input according to options.' + license = 'gplv2+' + inputs = ['dataBytes'] + options = ( + {'id': 'output-format', 'desc': 'Format for displaying packets values', + 'default': 'dec', 'values': ('dec', 'ASCII', 'bin', 'hex')}, + {'id': 'input-binary-format', 'desc': 'Binary format in which the input is encoded', + 'default': 'bin', 'values': ('bin', 'BCD')}, + {'id': 'max-packet-length', 'desc': 'Maximal length of a packet', + 'default': 4}, + {'id': 'packet-separator-sequence', + 'desc': 'Sequence of characters to separate packets on (hexadecimal values without 0x separated by ,)', + 'default': 'none'}, + {'id': 'use-separator-sequence', 'desc': 'Separate packets on sequence of characters', + 'default': 'no', 'values': ('yes', 'no')}, + {'id': 'display-separator-sequence', 'desc': 'Display separation sequence characters', + 'default': 'yes', 'values': ('yes', 'no')}, + ) + outputs = [] + tags = ['Embedded/industrial'] + annotations = ( + ('adr', 'Adrs'), + ('dt', 'Dat'), + ) + annotation_rows = ( + ('main', 'MAIN', (Ann.ADDRESS, Ann.DATA,)), + ) + + def __init__(self): + self.reset() + + def reset(self): + self.state = 'NEUTRAL' + self.stored_values = [] + self.separation_sequence_pointer = 0 + + def start(self): + self.out_ann = self.register(srd.OUTPUT_ANN) + if self.options['use-separator-sequence'] == 'yes': + self.separation_sequence = str(self.options['packet-separator-sequence']).split(',') + else: + self.separation_sequence = [] + + def manage_stored_values(self, t): + if self.have_to_output(): + self.output_stored_values(t) + + def output_stored_values(self, t): + # Outputs all values stored until now with the correct annotation type + if len(self.stored_values) == 0: + return + if t == 'DATA': + self.put(self.ss, self.es, self.out_ann, [Ann.DATA, ['Data: ' + self.get_output(), 'Da', 'D']]) + elif t == 'ADDR': + self.put(self.ss, self.es, self.out_ann, [Ann.ADDRESS, ['Address: ' + self.get_output(), 'Add', 'A']]) + + # Resets the current state, stored values and separation sequence pointer + self.reset() + + def have_to_output(self): + # Returns True if the options indicate that a new packet should be started and the previous one finished + return self.options['max-packet-length'] == len(self.stored_values) or self.is_separated_by_sequence() + + def is_separated_by_sequence(self): + # Returns True if the sequence separation is active and the pointer indicates the need of separation + return (self.options['use-separator-sequence'] == 'yes' and + 0 < len(self.separation_sequence) <= self.separation_sequence_pointer) + + def move_separation_sequence_pointer(self, value): + # If the sequence separation is active + if len(self.separation_sequence) == 0 or self.options['use-separator-sequence'] == 'no': + return + if self.options['input-binary-format'] == 'BCD': + value = bcd2int(value) + + # ...and this value is starting a separation sequence or following an already + # started separation sequence, move the separation sequence pointer potentially causing separation + if str(hex(value).replace("0x", "")) == self.separation_sequence[self.separation_sequence_pointer]: + self.separation_sequence_pointer += 1 + # ...otherwise reset separation sequence pointer + else: + self.separation_sequence_pointer = 0 + + def get_output(self): + # If the options say so, the separation sequence characters should be excluded from the output + if self.is_separated_by_sequence() and self.options['display-separator-sequence'] == 'no': + for _ in self.separation_sequence: + self.stored_values.pop() + + # Get the output from the stored values according to output and input binary format options + output = '' + if self.options['output-format'] == 'dec': + for b in self.stored_values: + if self.options['input-binary-format'] == 'BCD': + b = bcd2int(b) + output = output + ' ' + str(b) + elif self.options['output-format'] == 'bin': + for b in self.stored_values: + if self.options['input-binary-format'] == 'BCD': + b = bcd2int(b) + output = output + ' ' + str(bin(b).replace("0b", "")) + elif self.options['output-format'] == 'ASCII': + for b in self.stored_values: + if self.options['input-binary-format'] == 'BCD': + b = bcd2int(b) + + # If the output format is ASCII, visualize only characters with codes from 32 up to 126 + # and use special symbols for the other codes + if b == 10: + b = "\u240a" + elif b == 13: + b = "\u240d" + elif b == 32: + b = "\u2423" + elif b == 9: + b = "\u21e5" + elif 126 >= b >= 32: + b = chr(b) + else: + b = "\ufffd" + output = output + b + elif self.options['output-format'] == 'hex': + for b in self.stored_values: + if self.options['input-binary-format'] == 'BCD': + b = bcd2int(b) + output = output + ' ' + hex(b) + return output + + # Any addition to stored values should be done via this method + def add_to_stored_values(self, value): + self.stored_values.append(value) + self.move_separation_sequence_pointer(value) + + def decode(self, ss, es, data): + cmd, data_value = data + + # State machine. + if self.state == 'NEUTRAL': + self.ss = ss + + if cmd == 'DATA': + # If has not finished collecting an ADDRESS packet yet, finish it now + if self.state == 'RECEIVING ADDRESS': + self.output_stored_values('ADDR') + self.ss = ss + + # We are receiving DATA packets now + self.state = 'RECEIVING DATA' + # Set the end of the current packet for now + self.es = es + + self.add_to_stored_values(data_value) + + # Send all data values stored until now if it is suitable and clear the buffer + self.manage_stored_values('DATA') + elif cmd == 'ADDRESS': + # If has not finished collecting a DATA packet yet, finish it now + if self.state == 'RECEIVING DATA': + self.output_stored_values('DATA') + self.ss = ss + + # We are receiving ADDRESS packets now + self.state = 'RECEIVING ADDRESS' + # Set the end of the current packet for now + self.es = es + + self.add_to_stored_values(data_value) + + # Send all address values stored until now if it is suitable and clear the buffer + self.manage_stored_values('ADDR')