From b496ead7b30b1025595f624c71b5d59756ea9fb4 Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Tue, 16 May 2023 22:44:25 +0100 Subject: util/spkmodem_recv: Import from coreboot Imported from util/spkmodem_recv at coreboot revision: e70bc423f9a2e1d13827f2703efe1f9c72549f20 This is a client for spkmodem, to allow serial console via PC speaker. I've decided to import it in lbmk, because I heavily modified it. The patches will be applied next. Signed-off-by: Leah Rowe --- util/spkmodem_recv/.gitignore | 1 + util/spkmodem_recv/Makefile | 9 ++++ util/spkmodem_recv/description.md | 1 + util/spkmodem_recv/spkmodem-recv.c | 100 +++++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 util/spkmodem_recv/.gitignore create mode 100644 util/spkmodem_recv/Makefile create mode 100644 util/spkmodem_recv/description.md create mode 100644 util/spkmodem_recv/spkmodem-recv.c (limited to 'util/spkmodem_recv') diff --git a/util/spkmodem_recv/.gitignore b/util/spkmodem_recv/.gitignore new file mode 100644 index 00000000..2f5c946c --- /dev/null +++ b/util/spkmodem_recv/.gitignore @@ -0,0 +1 @@ +spkmodem-recv diff --git a/util/spkmodem_recv/Makefile b/util/spkmodem_recv/Makefile new file mode 100644 index 00000000..92a3bfe9 --- /dev/null +++ b/util/spkmodem_recv/Makefile @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +PREFIX ?= /usr/local +INSTALL ?= install + +spkmodem-recv: + $(CC) -o $@ $@.c +install: spkmodem-recv + $(INSTALL) -d $(DESTDIR)$(PREFIX)/bin/ + $(INSTALL) $< -t $(DESTDIR)$(PREFIX)/bin/ diff --git a/util/spkmodem_recv/description.md b/util/spkmodem_recv/description.md new file mode 100644 index 00000000..fe62f169 --- /dev/null +++ b/util/spkmodem_recv/description.md @@ -0,0 +1 @@ +Decode spkmodem signals `C` diff --git a/util/spkmodem_recv/spkmodem-recv.c b/util/spkmodem_recv/spkmodem-recv.c new file mode 100644 index 00000000..cd7bd483 --- /dev/null +++ b/util/spkmodem_recv/spkmodem-recv.c @@ -0,0 +1,100 @@ +/* spkmodem-recv.c - decode spkmodem signals */ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include +#include +#include + +/* Compilation: gcc -o spkmodem-recv spkmodem-recv */ +/* Usage: parec --channels=1 --rate=48000 --format=s16le | ./spkmodem-recv */ + +#define SAMPLES_PER_TRAME 240 +#define FREQ_SEP_MIN 5 +#define FREQ_SEP_MAX 15 +#define FREQ_DATA_MIN 15 +#define FREQ_DATA_THRESHOLD 25 +#define FREQ_DATA_MAX 60 +#define THRESHOLD 500 + +#define DEBUG 0 +#define FLUSH_TIMEOUT 1 + +static signed short trame[2 * SAMPLES_PER_TRAME]; +static signed short pulse[2 * SAMPLES_PER_TRAME]; +static int ringpos = 0; +static int pos, f1, f2; +static int amplitude = 0; +static int lp = 0; + +static void +read_sample (void) +{ + amplitude -= abs (trame[ringpos]); + f1 -= pulse[ringpos]; + f1 += pulse[(ringpos + SAMPLES_PER_TRAME) % (2 * SAMPLES_PER_TRAME)]; + f2 -= pulse[(ringpos + SAMPLES_PER_TRAME) % (2 * SAMPLES_PER_TRAME)]; + fread (trame + ringpos, 1, sizeof (trame[0]), stdin); + amplitude += abs (trame[ringpos]); + + if (pos ? (trame[ringpos] < -THRESHOLD) + : (trame[ringpos] > +THRESHOLD)) + { + pulse[ringpos] = 1; + pos = !pos; + f2++; + } + else + pulse[ringpos] = 0; + ringpos++; + ringpos %= 2 * SAMPLES_PER_TRAME; + lp++; +} + +int +main () +{ + int bitn = 7; + char c = 0; + int i; + int llp = 0; + while (!feof (stdin)) + { + if (lp > 3 * SAMPLES_PER_TRAME) + { + bitn = 7; + c = 0; + lp = 0; + llp++; + } + if (llp == FLUSH_TIMEOUT) + fflush (stdout); + if (f2 > FREQ_SEP_MIN && f2 < FREQ_SEP_MAX + && f1 > FREQ_DATA_MIN && f1 < FREQ_DATA_MAX) + { +#if DEBUG + printf ("%d %d %d @%d\n", f1, f2, FREQ_DATA_THRESHOLD, + ftell (stdin) - sizeof (trame)); +#endif + if (f1 < FREQ_DATA_THRESHOLD) + c |= (1 << bitn); + bitn--; + if (bitn < 0) + { +#if DEBUG + printf ("<%c, %x>", c, c); +#else + printf ("%c", c); +#endif + bitn = 7; + c = 0; + } + lp = 0; + llp = 0; + for (i = 0; i < SAMPLES_PER_TRAME; i++) + read_sample (); + continue; + } + read_sample (); + } + return 0; +} -- cgit v1.2.1