fw: simplify how to enable QEMU debug in firmware.

- Remove the define `NOCONSOLE`, add define `QEMU_CONSOLE`
- Inverse the use of it, add the define to have QEMU debug output in fw.
- Add a make target `qemu_firmware.elf` which builds the firmware with
  QEMU console enabled.

Co-authored-by: Mikael Ågren <mikael@tillitis.se>
This commit is contained in:
Daniel Jobson 2024-09-06 12:46:18 +02:00 committed by Michael Cardell Widerkrantz
parent 35052e50cb
commit 613316f53e
No known key found for this signature in database
GPG Key ID: D3DB3DDF57E704E5
4 changed files with 24 additions and 18 deletions

View File

@ -44,7 +44,7 @@ CC = clang
CFLAGS = -target riscv32-unknown-none-elf -march=rv32iczmmul -mabi=ilp32 \ CFLAGS = -target riscv32-unknown-none-elf -march=rv32iczmmul -mabi=ilp32 \
-static -std=gnu99 -O2 -ffast-math -fno-common -fno-builtin-printf \ -static -std=gnu99 -O2 -ffast-math -fno-common -fno-builtin-printf \
-fno-builtin-putchar -fno-builtin-memcpy -nostdlib -mno-relax -Wall \ -fno-builtin-putchar -fno-builtin-memcpy -nostdlib -mno-relax -Wall \
-Wpedantic -Wno-language-extension-token -flto -g -DNOCONSOLE -Wpedantic -Wno-language-extension-token -flto -g
AS = clang AS = clang
ASFLAGS = -target riscv32-unknown-none-elf -march=rv32iczmmul -mabi=ilp32 -mno-relax ASFLAGS = -target riscv32-unknown-none-elf -march=rv32iczmmul -mabi=ilp32 -mno-relax
@ -151,6 +151,10 @@ $(TESTFW_OBJS): $(FIRMWARE_DEPS)
firmware.elf: $(FIRMWARE_OBJS) $(P)/fw/tk1/firmware.lds firmware.elf: $(FIRMWARE_OBJS) $(P)/fw/tk1/firmware.lds
$(CC) $(CFLAGS) $(FIRMWARE_OBJS) $(LDFLAGS) -o $@ $(CC) $(CFLAGS) $(FIRMWARE_OBJS) $(LDFLAGS) -o $@
qemu_firmware.elf: CFLAGS += -DQEMU_CONSOLE
qemu_firmware.elf: firmware.elf
mv firmware.elf qemu_firmware.elf
.PHONY: check .PHONY: check
check: check:
clang-tidy -header-filter=.* -checks=cert-* $(FIRMWARE_SOURCES) -- $(CFLAGS) clang-tidy -header-filter=.* -checks=cert-* $(FIRMWARE_SOURCES) -- $(CFLAGS)
@ -359,6 +363,7 @@ clean_fw:
rm -f $(FIRMWARE_OBJS) rm -f $(FIRMWARE_OBJS)
rm -f testfw.{elf,elf.map,bin,hex} rm -f testfw.{elf,elf.map,bin,hex}
rm -f $(TESTFW_OBJS) rm -f $(TESTFW_OBJS)
rm -f qemu_firmware.elf
.PHONY: clean_fw .PHONY: clean_fw
#------------------------------------------------------------------- #-------------------------------------------------------------------

View File

@ -286,11 +286,12 @@ for the tools you need. The easiest is probably to use your OCI image,
also useful for debugging the firmware. You can attach GDB, use also useful for debugging the firmware. You can attach GDB, use
breakpoints, et cetera. breakpoints, et cetera.
If you want to use plain debug prints you can remove `-DNOCONSOLE` There is a special make target for QEMU: `qemu_firmware.elf`, which
from the `CFLAGS` in the Makefile and using the helper functions in sets `-DQEMU_CONSOLE`, so you can use plain debug prints using the
`lib.c` like `htif_puts()` `htif_putinthex()` `htif_hexdump()` and helper functions in `lib.c` like `htif_puts()` `htif_putinthex()`
friends for printf-like debugging. Note that these functions are only `htif_hexdump()` and friends. Note that these functions are only
usable in qemu. usable in qemu and that you might need to `make clean` before
building, if you have already built before.
### Test firmware ### Test firmware

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2022 - Tillitis AB * Copyright (C) 2022-2024 - Tillitis AB
* SPDX-License-Identifier: GPL-2.0-only * SPDX-License-Identifier: GPL-2.0-only
*/ */
@ -7,7 +7,7 @@
#include "assert.h" #include "assert.h"
#include "types.h" #include "types.h"
#ifndef NOCONSOLE #ifdef QEMU_CONSOLE
struct { struct {
uint32_t arr[2]; uint32_t arr[2];
} static volatile tohost __attribute__((section(".htif"))); } static volatile tohost __attribute__((section(".htif")));

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2022 - Tillitis AB * Copyright (C) 2022-2024 - Tillitis AB
* SPDX-License-Identifier: GPL-2.0-only * SPDX-License-Identifier: GPL-2.0-only
*/ */
@ -8,21 +8,21 @@
#include "types.h" #include "types.h"
#ifdef NOCONSOLE #ifdef QEMU_CONSOLE
#define htif_putc(ch)
#define htif_lf()
#define htif_puthex(c)
#define htif_putinthex(n)
#define htif_puts(s)
#define htif_hexdump(buf, len)
#else
void htif_putc(char ch); void htif_putc(char ch);
void htif_lf(); void htif_lf();
void htif_puthex(uint8_t c); void htif_puthex(uint8_t c);
void htif_putinthex(const uint32_t n); void htif_putinthex(const uint32_t n);
void htif_puts(const char *s); void htif_puts(const char *s);
void htif_hexdump(void *buf, int len); void htif_hexdump(void *buf, int len);
#endif #else
#define htif_putc(ch)
#define htif_lf()
#define htif_puthex(c)
#define htif_putinthex(n)
#define htif_puts(s)
#define htif_hexdump(buf, len)
#endif /* QEMU_CONSOLE */
void *memset(void *dest, int c, unsigned n); void *memset(void *dest, int c, unsigned n);
void memcpy_s(void *dest, size_t destsize, const void *src, size_t n); void memcpy_s(void *dest, size_t destsize, const void *src, size_t n);