Skip to content

Signal Capture for debug

hasu@tmk edited this page May 22, 2026 · 34 revisions

Signal Capture Firmware

WORK_IN_PROGRESS

This is not that useful if you have already logic analyzer or oscilloscope. This can be used by users who don't have such equipments to see signal of their keyboard.

'Signal Capture' firmwares code is available for converters listed below.

Prebuilt firmware

Download proper firmware *_capture_*.hex for your converter.

Build firmware

Build 'Signal Capture' firmware and flash it onto your converter with commands like below.

make -f Makefile.capture clean
make -f Makefile.capture
make -f Makefile.capture dfu

Capture record

Signal Capture prints signal data in text format on hid_listen like below.

0021 0001 0001 6401 D423 D5C2 D720 D962 DBD0 DE22 DF53 E091 E2E3 E402 

Convert into VCD format

Capture records need to be convertered into Value Change Dump(VCD) format using capture2vcd.py script to see signal. The script is located at tmk_core/tool/ in repository.

You need to install pyvcd before using the script.

$ pip3 install pyvcd

To convert into VCD format:

$ python3 capture2vcd.py <capture_file>

or

$ cat <capture_data> | python3 capture2vcd.py

View signals

You can see converted VCD data with tool like Sigrok PulsView.

Internals

Capture record format

Captured data are stored in ring buffer at AVR SRAM(0x100-2FF) temporarily and then, printed in format below.

# Capture record format:
#
#   P: pin state(4-bit)
#   TTT: timer[us](0-4095)
#   CC: timer overflow count(1-255)
#
#   1. Pin change:      TTTP
#   pin state should be changed from state of previous record.
#   TTT indicates time(12-bit):     0-FFFh(4095)
#
#   2. Timer overflow count:    CC0P
#   pin state should be same as state of previous record.
#   CC indicates time elapsed:      CC(1-255) * 1000h(4096)
#
#   3. Timer overflow count rollover:    000P
#   pin state should be same as state of previous record.
#   this indicates time elapsed:    256 * 1000h(4096), around 1sec

capture2vcd.py

tmk_core/tool/capture2vcd.py

#!/usr/bin/python3
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#
# This converts pin capture data into VCD format
#
# Capture record format:
#
#   P: pin state(4-bit)
#   TTT: timer[us](0-4095)
#   CC: timer overflow count(1-255)
#
#   1. Pin change:      TTTP
#   pin state should be changed from state of previous record.
#   TTT indicates time(12-bit):     0-FFFh(4095)
#
#   2. Timer overflow count:    CC0P
#   pin state should be same as state of previous record.
#   CC indicates time elapsed:      CC(1-255) * 1000h(4096)
#
#   3. Timer overflow count rollover:    000P
#   pin state should be same as state of previous record.
#   this indicates time elapsed:    256 * 1000h(4096), around 1sec
#

import sys
import re

# https://github.com/westerndigitalcorporation/pyvcd
from vcd import VCDWriter

if len(sys.argv) == 2:
    file = open(sys.argv[1])
else:
    file = sys.stdin
    print(f"Usage: {sys.argv[0]} [infile]", file=sys.stderr)
    print('or give data into stdin. Reading from stdin...', file=sys.stderr);

# write VCD
from datetime import datetime, timezone
with VCDWriter(sys.stdout, timescale='1 us', date=datetime.now(timezone.utc).ctime()) as writer:

    # prams: scope, name, var_type, size
    port = writer.register_var('tmk_capture', 'port', 'wire', size=4, init=0x0)

    # record: TTTP/CC0P/000P
    p = re.compile(r'([0-9A-Fa-f]{4})')

    ext_time = 0
    prv_time = 0
    prv_stat = -1
    for line in file:
        for record in line.strip().split():
            m = p.match(record)
            if m is not None:
                time = int(record[0:3], 16)
                stat = int(record[3:4], 16)

                if prv_stat == stat:
                    # no pin change: timer overflow count
                    if time == 0:
                        # overflow count rollover: 000P
                        ext_time += 0x100
                    else:
                        # overflow count: CC0P
                        ext_time += time >> 4

                    # no VCD record
                    #print('time overflow: ', time >> 4, hex(time), stat, file=sys.stderr);

                    prv_time = 0
                else:
                    # Pin change: TTTP
                    if prv_time >= time:
                        # time flipped
                        ext_time += 1
                        #print('time flipped: ', ext_time, ': ', hex(prv_time), '>', hex(time), file=sys.stderr);

                    # write VCD record
                    writer.change(port, ((ext_time * 0x1000) + time), stat)
                    #print(ext_time, time, hex(time), stat, file=sys.stderr)

                    prv_time = time
                    prv_stat = stat
            else:
                print('Invalid record: ', record, file=sys.stderr);
                #sys.exit(1)

Sigrok

PulseView

Decoder

Installation

Just place decoder holder in ~/.local/share/libsigrokdecode/decoders on Linux and macOS, or %ProgramData%\libsigrokdecode\decoders on Windows.

IBMPC AT/XT

ADB

Clone this wiki locally