From 20c994eece8b3de0fe95e1913a7ee635adbf2a29 Mon Sep 17 00:00:00 2001 From: Piotr Esden-Tempski Date: Sat, 18 Jun 2016 01:26:56 -0700 Subject: [PATCH] Added black magic verbose/debug mode switch. --- src/Makefile | 7 +++++- src/adiv5_swdp.c | 1 + src/command.c | 21 +++++++++++++++++ src/platforms/native/platform.h | 12 +++++++++- src/platforms/stm32/jtagtap.c | 3 ++- src/platforms/stm32/usbuart.c | 41 +++++++++++++++++++++++++++++++++ 6 files changed, 82 insertions(+), 3 deletions(-) diff --git a/src/Makefile b/src/Makefile index 7661837..6d9a990 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,6 +1,7 @@ PROBE_HOST ?= native PLATFORM_DIR = platforms/$(PROBE_HOST) VPATH += platforms/common $(PLATFORM_DIR) +ENABLE_DEBUG ?= ifneq ($(V), 1) MAKEFLAGS += --no-print-dir @@ -11,7 +12,11 @@ OPT_FLAGS ?= -O2 CFLAGS += -Wall -Wextra -Werror -Wno-char-subscripts\ $(OPT_FLAGS) -std=gnu99 -g3 -MD \ - -I. -Iinclude -Iplatforms/common -I$(PLATFORM_DIR) \ + -I. -Iinclude -Iplatforms/common -I$(PLATFORM_DIR) + +ifeq ($(ENABLE_DEBUG), 1) +CFLAGS += -DENABLE_DEBUG +endif SRC = \ adiv5.c \ diff --git a/src/adiv5_swdp.c b/src/adiv5_swdp.c index 63e4eb4..5edf49e 100644 --- a/src/adiv5_swdp.c +++ b/src/adiv5_swdp.c @@ -29,6 +29,7 @@ #include "jtagtap.h" #include "command.h" #include "morse.h" +#include "gdb_packet.h" #define SWDP_ACK_OK 0x01 #define SWDP_ACK_WAIT 0x02 diff --git a/src/command.c b/src/command.c index 3de4ec0..beb3ede 100644 --- a/src/command.c +++ b/src/command.c @@ -50,6 +50,9 @@ static bool cmd_target_power(target *t, int argc, const char **argv); #ifdef PLATFORM_HAS_TRACESWO static bool cmd_traceswo(void); #endif +#ifdef PLATFORM_HAS_DEBUG +static bool cmd_debug_bmp(target *t, int argc, const char **argv); +#endif const struct command_s cmd_list[] = { {"version", (cmd_handler)cmd_version, "Display firmware version info"}, @@ -64,11 +67,17 @@ const struct command_s cmd_list[] = { #endif #ifdef PLATFORM_HAS_TRACESWO {"traceswo", (cmd_handler)cmd_traceswo, "Start trace capture" }, +#endif +#ifdef PLATFORM_HAS_DEBUG + {"debug_bmp", (cmd_handler)cmd_debug_bmp, "Output BMP \"debug\" strings to the second vcom: (enable|disable)"}, #endif {NULL, NULL, NULL} }; static bool connect_assert_srst; +#ifdef PLATFORM_HAS_DEBUG +bool debug_bmp; +#endif int command_process(target *t, char *cmd) { @@ -276,3 +285,15 @@ static bool cmd_traceswo(void) } #endif +#ifdef PLATFORM_HAS_DEBUG +static bool cmd_debug_bmp(target *t, int argc, const char **argv) +{ + (void)t; + if (argc > 1) { + debug_bmp = !strcmp(argv[1], "enable"); + } + gdb_outf("Debug mode is %s\n", + debug_bmp ? "enabled" : "disabled"); + return true; +} +#endif diff --git a/src/platforms/native/platform.h b/src/platforms/native/platform.h index 3797f11..5b19bdc 100644 --- a/src/platforms/native/platform.h +++ b/src/platforms/native/platform.h @@ -29,6 +29,10 @@ #define PLATFORM_HAS_TRACESWO #define PLATFORM_HAS_POWER_SWITCH +#ifdef ENABLE_DEBUG +#define PLATFORM_HAS_DEBUG +#define USBUART_DEBUG +#endif #define BOARD_IDENT "Black Magic Probe" #define BOARD_IDENT_DFU "Black Magic Probe (Upgrade)" #define BOARD_IDENT_UPD "Black Magic Probe (DFU Upgrade)" @@ -142,7 +146,14 @@ #define TRACE_IRQ NVIC_TIM3_IRQ #define TRACE_ISR tim3_isr +#ifdef ENABLE_DEBUG +extern bool debug_bmp; +void usbuart_debug_outf(const char *fmt, ...); + +#define DEBUG(...) if (debug_bmp) {usbuart_debug_outf("bmp: ");usbuart_debug_outf(__VA_ARGS__);} +#else #define DEBUG(...) +#endif #define SET_RUN_STATE(state) {running_status = (state);} #define SET_IDLE_STATE(state) {gpio_set_val(LED_PORT, LED_IDLE_RUN, state);} @@ -155,4 +166,3 @@ #define vasprintf vasiprintf #endif - diff --git a/src/platforms/stm32/jtagtap.c b/src/platforms/stm32/jtagtap.c index 4026fe3..baaa149 100644 --- a/src/platforms/stm32/jtagtap.c +++ b/src/platforms/stm32/jtagtap.c @@ -24,6 +24,7 @@ #include "general.h" #include "jtagtap.h" +#include "gdb_packet.h" int jtagtap_init(void) { @@ -60,7 +61,7 @@ inline uint8_t jtagtap_next(uint8_t dTMS, uint8_t dTDO) ret = gpio_get(TDO_PORT, TDO_PIN); gpio_clear(TCK_PORT, TCK_PIN); - DEBUG("jtagtap_next(TMS = %d, TDO = %d) = %d\n", dTMS, dTDO, ret); + //DEBUG("jtagtap_next(TMS = %d, TDO = %d) = %d\n", dTMS, dTDO, ret); return ret != 0; } diff --git a/src/platforms/stm32/usbuart.c b/src/platforms/stm32/usbuart.c index cce2904..7db5ea1 100644 --- a/src/platforms/stm32/usbuart.c +++ b/src/platforms/stm32/usbuart.c @@ -192,6 +192,47 @@ void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep) gpio_clear(LED_PORT_UART, LED_UART); } +#ifdef USBUART_DEBUG +#include + +/* Function to output debug data to usbuart port (ttyACM1 on linux) */ +void usbuart_debug_outf(const char *fmt, ...) +{ + va_list ap; + char *buf, *tmp; + + va_start(ap, fmt); + if (vasprintf(&buf, fmt, ap) < 0) + return; + tmp = buf; + while( *tmp != 0 ) + { + if( *tmp == '\n' && *(tmp-1) != '\r' ) + { + /* insert into FIFO */ + buf_rx[buf_rx_in++] = '\r'; + + /* wrap out pointer */ + if (buf_rx_in >= FIFO_SIZE) + { + buf_rx_in = 0; + } + } + /* insert into FIFO */ + buf_rx[buf_rx_in++] = *(tmp++); + + /* wrap out pointer */ + if (buf_rx_in >= FIFO_SIZE) + { + buf_rx_in = 0; + } + } + /* enable deferred processing if we put data in the FIFO */ + timer_enable_irq(USBUSART_TIM, TIM_DIER_UIE); + free(buf); + va_end(ap); +} +#endif void usbuart_usb_in_cb(usbd_device *dev, uint8_t ep) {