Skip to content

Commit

Permalink
Add TKEYCTRL USB debug support
Browse files Browse the repository at this point in the history
  • Loading branch information
jthornblad committed Jan 21, 2025
1 parent eaa117a commit 382f3ea
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 56 deletions.
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ CC = clang

INCLUDE=include

# Note that QEMU_DEBUG needs to be defined!
# If you want debug_puts() etcetera to output something on our QEMU
# debug port, use -DQEMU_DEBUG below, or -DTKEY_DEBUG to use Tkeys USB debug pipe
CFLAGS = -target riscv32-unknown-none-elf -march=rv32iczmmul -mabi=ilp32 \
-mcmodel=medany -static -std=gnu99 -O2 -ffast-math -fno-common \
-fno-builtin-printf -fno-builtin-putchar -nostdlib -mno-relax -flto \
-Wall -Werror=implicit-function-declaration \
-I $(INCLUDE) -I . -D QEMU_DEBUG
-I $(INCLUDE) -I .
# -DQEMU_DEBUG -DTKEY_DEBUG

AS = clang
AR = llvm-ar
Expand Down Expand Up @@ -38,12 +40,13 @@ libcrt0.a: libcrt0/crt0.o

# Common C functions
LIBOBJS=libcommon/assert.o libcommon/blake2s.o libcommon/led.o libcommon/lib.o \
libcommon/proto.o libcommon/touch.o libcommon/qemu_debug.o
libcommon/proto.o libcommon/touch.o libcommon/qemu_debug.o \
libcommon/tkey_debug.o
libcommon.a: $(LIBOBJS)
$(AR) -qc $@ $(LIBOBJS)
$(LIBOBJS): include/tkey/assert.h include/tkey/blake2s.h include/tkey/led.h \
include/tkey/lib.h include/tkey/proto.h include/tkey/tk1_mem.h \
include/tkey/touch.h include/tkey/qemu_debug.h
include/tkey/touch.h include/tkey/debug.h

# Monocypher
MONOOBJS=monocypher/monocypher.o monocypher/monocypher-ed25519.o
Expand Down
7 changes: 5 additions & 2 deletions example-app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ P := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
LIBDIR ?= $(P)/../
OBJCOPY ?= llvm-objcopy
CC = clang

# If you want debug_puts() etcetera to output something on our QEMU
# debug port, use -DQEMU_DEBUG below, or -DTKEY_DEBUG to use Tkeys USB debug pipe
CFLAGS = -g -target riscv32-unknown-none-elf -march=rv32iczmmul -mabi=ilp32 -mcmodel=medany \
-static -std=gnu99 -O2 -ffast-math -fno-common -fno-builtin-printf \
-fno-builtin-putchar -nostdlib -mno-relax -flto \
-Wall -Werror=implicit-function-declaration \
-I $(LIBDIR)/include -I $(LIBDIR) \
# -D QEMU_DEBUG
-I $(LIBDIR)/include -I $(LIBDIR)
# -DQEMU_DEBUG -DTKEY_DEBUG

INCLUDE=$(LIBDIR)/include

Expand Down
10 changes: 5 additions & 5 deletions example-app/blue.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <stdint.h>
#include <tkey/led.h>
#include <tkey/tk1_mem.h>
#include <tkey/qemu_debug.h>
#include <tkey/debug.h>

#define SLEEPTIME 100000

Expand All @@ -15,10 +15,10 @@ void sleep(uint32_t n)

int main(void)
{
qemu_puts("Hello, world!\n");
qemu_puts("Going to sleep between blinks: ");
qemu_putinthex(SLEEPTIME);
qemu_lf();
debug_puts("Hello, world!\n");
debug_puts("Going to sleep between blinks: ");
debug_putinthex(SLEEPTIME);
debug_lf();

for (;;) {
led_set(LED_RED);
Expand Down
52 changes: 52 additions & 0 deletions include/tkey/debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-FileCopyrightText: 2023 Tillitis AB <tillitis.se>
// SPDX-License-Identifier: BSD-2-Clause

#ifndef TKEY_DEBUG_H
#define TKEY_DEBUG_H

#include <stdint.h>

#if defined(QEMU_DEBUG)

void qemu_putchar(const uint8_t ch);
void qemu_lf();
void qemu_putinthex(const uint32_t n);
void qemu_puts(const char *s);
void qemu_puthex(const uint8_t ch);
void qemu_hexdump(const uint8_t *buf, int len);

#define debug_putchar qemu_putchar
#define debug_lf qemu_lf
#define debug_putinthex qemu_putinthex
#define debug_puts qemu_puts
#define debug_puthex qemu_puthex
#define debug_hexdump qemu_hexdump

#elif defined(TKEY_DEBUG)

void tkey_putchar(const uint8_t ch);
void tkey_lf();
void tkey_putinthex(const uint32_t n);
void tkey_puts(const char *s);
void tkey_puthex(const uint8_t ch);
void tkey_hexdump(const uint8_t *buf, int len);

#define debug_putchar tkey_putchar
#define debug_lf tkey_lf
#define debug_putinthex tkey_putinthex
#define debug_puts tkey_puts
#define debug_puthex tkey_puthex
#define debug_hexdump tkey_hexdump

#else

#define debug_putchar(ch)
#define debug_lf()
#define debug_putinthex(n)
#define debug_puts(s)
#define debug_puthex(ch)
#define debug_hexdump(buf, len)

#endif

#endif
1 change: 1 addition & 0 deletions include/tkey/proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define TKEY_PROTO_H

enum mode {
MODE_TKEYCTRL = 0x20,
MODE_CDC = 0x40,
MODE_HID = 0x80,
};
Expand Down
25 changes: 0 additions & 25 deletions include/tkey/qemu_debug.h

This file was deleted.

20 changes: 10 additions & 10 deletions libcommon/assert.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
// SPDX-License-Identifier: BSD-2-Clause

#include <tkey/assert.h>
#include <tkey/debug.h>
#include <tkey/lib.h>
#include <tkey/qemu_debug.h>

void assert_fail(const char *assertion, const char *file, unsigned int line,
const char *function)
{
qemu_puts("assert: ");
qemu_puts(assertion);
qemu_puts(" ");
qemu_puts(file);
qemu_puts(":");
qemu_putinthex(line);
qemu_puts(" ");
qemu_puts(function);
qemu_lf();
debug_puts("assert: ");
debug_puts(assertion);
debug_puts(" ");
debug_puts(file);
debug_puts(":");
debug_putinthex(line);
debug_puts(" ");
debug_puts(function);
debug_lf();

// Force illegal instruction to halt CPU
asm volatile("unimp");
Expand Down
10 changes: 6 additions & 4 deletions libcommon/proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include <stddef.h>
#include <stdint.h>
#include <tkey/debug.h>
#include <tkey/proto.h>
#include <tkey/qemu_debug.h>
#include <tkey/tk1_mem.h>

// clang-format off
Expand Down Expand Up @@ -86,10 +86,12 @@ uint8_t readbyte(uint8_t *mode, uint8_t *mode_bytes_left)
{
if (*mode_bytes_left == 0) {
*mode = readbyte_();
if (*mode != MODE_CDC) {
qemu_puts("We only support MODE_CDC\n");
} else {
if ((*mode == MODE_CDC) || (*mode == MODE_HID) ||
(*mode == MODE_TKEYCTRL)) {
*mode_bytes_left = readbyte_();
} else {
debug_puts("We only support MODE_CDC, MODE_HID and "
"MODE_TKEYCTRL\n");
}
}
uint8_t b = readbyte_();
Expand Down
13 changes: 8 additions & 5 deletions libcommon/qemu_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@

#include <stdint.h>
#include <tkey/lib.h>
#include <tkey/qemu_debug.h>
#include <tkey/tk1_mem.h>

#ifndef QEMU_DEBUG
#define QEMU_DEBUG
#endif

#include <tkey/debug.h>

// clang-format off
static volatile uint8_t* const debugtx = (volatile uint8_t *)TK1_MMIO_QEMU_DEBUG;
// clang-format on

int qemu_putchar(const uint8_t ch)
void qemu_putchar(const uint8_t ch)
{
*debugtx = ch;

return ch;
}

void qemu_lf()
Expand Down Expand Up @@ -92,7 +95,7 @@ void qemu_hexdump(const uint8_t *buf, int len)
for (int i = 0; i < len; i++) {
qemu_puthex(byte_buf[i]);
if (i % 2 == 1) {
(void)qemu_putchar(' ');
qemu_putchar(' ');
}

if ((i + 1) % 16 == 0) {
Expand Down
Loading

0 comments on commit 382f3ea

Please sign in to comment.