From 81abfd9f5cda20b080214ff09016694d27b2ec67 Mon Sep 17 00:00:00 2001
From: moon2 <moon2protonmail@protonmail.com>
Date: Thu, 14 Sep 2023 02:22:35 +0000
Subject: [PATCH] enable gdb build flag

how to:
(1) idf.py menuconfig -> Component config -> debug config -> usb gdb mode
(2) rm -r build ; idf.py app-flash
(3) OPENOCD_COMMANDS="-f board/esp32s3-builtin.cfg" idf.py openocd
(4) (in another terminal) idf.py gdb
---
 components/st3m/Kconfig    | 10 +++++++++
 components/st3m/st3m_usb.c | 46 +++++++++++++++++++++++++++++++++++++-
 components/st3m/st3m_usb.h |  3 +++
 main/main.c                | 16 +------------
 4 files changed, 59 insertions(+), 16 deletions(-)
 create mode 100644 components/st3m/Kconfig

diff --git a/components/st3m/Kconfig b/components/st3m/Kconfig
new file mode 100644
index 0000000000..65526b7af7
--- /dev/null
+++ b/components/st3m/Kconfig
@@ -0,0 +1,10 @@
+menu "debug config"
+    choice DEBUG_GDB
+        prompt "gdb or tinyusb/mass storage"
+        default DEBUG_GDB_DISABLED
+        config DEBUG_GDB_DISABLED
+            bool "tinyusb/mass storage mode, no gdb"
+        config DEBUG_GDB_ENABLED
+            bool "usb gdb mode, no tinyusb/mass storage"
+    endchoice
+endmenu
diff --git a/components/st3m/st3m_usb.c b/components/st3m/st3m_usb.c
index f3bd482758..283e2bb041 100644
--- a/components/st3m/st3m_usb.c
+++ b/components/st3m/st3m_usb.c
@@ -1,4 +1,14 @@
 #include "st3m_usb.h"
+#ifndef CONFIG_DEBUG_GDB_ENABLED
+#ifndef CONFIG_DEBUG_GDB_DISABLED
+#error "st3m: no debug flags"
+#endif
+#endif
+#ifdef CONFIG_DEBUG_GDB_ENABLED
+#ifdef CONFIG_DEBUG_GDB_DISABLED
+#error "st3m: invalid debug flags"
+#endif
+#endif
 
 static const char *TAG = "st3m-usb";
 
@@ -11,6 +21,8 @@ static const char *TAG = "st3m-usb";
 #include "esp_private/usb_phy.h"
 #include "tusb.h"
 
+#include "st3m_console.h"
+
 static SemaphoreHandle_t _mu = NULL;
 static st3m_usb_mode_kind_t _mode = st3m_usb_mode_kind_disabled;
 static usb_phy_handle_t phy_hdl;
@@ -45,12 +57,16 @@ static void _generate_serial(void) {
 }
 
 void st3m_usb_init(void) {
+#ifdef CONFIG_DEBUG_GDB_ENABLED
+    return;
+#endif
     assert(_mu == NULL);
     _mu = xSemaphoreCreateMutex();
     assert(_mu != NULL);
     _mode = st3m_usb_mode_kind_disabled;
 
     _generate_serial();
+#ifndef CONFIG_DEBUG_GDB_ENABLED
     st3m_usb_cdc_init();
     usb_phy_config_t phy_conf = {
         .controller = USB_PHY_CTRL_OTG,
@@ -68,9 +84,13 @@ void st3m_usb_init(void) {
 
     xTaskCreate(_usb_task, "usb", 4096, NULL, 5, NULL);
     ESP_LOGI(TAG, "USB stack started");
+#endif
 }
 
 void st3m_usb_mode_switch(st3m_usb_mode_t *mode) {
+#ifdef CONFIG_DEBUG_GDB_ENABLED
+    return;
+#endif
     xSemaphoreTake(_mu, portMAX_DELAY);
 
     bool running = false;
@@ -120,6 +140,9 @@ void st3m_usb_mode_switch(st3m_usb_mode_t *mode) {
 }
 
 bool st3m_usb_connected(void) {
+#ifdef CONFIG_DEBUG_GDB_ENABLED
+    return false;
+#endif
     xSemaphoreTake(_mu, portMAX_DELAY);
     bool res = _connected;
     xSemaphoreGive(_mu);
@@ -143,4 +166,25 @@ void tud_suspend_cb(bool remote_wakeup_en) {
     if (mode == st3m_usb_mode_kind_app) {
         st3m_usb_cdc_detached();
     }
-}
\ No newline at end of file
+}
+
+void st3m_usb_startup() {
+#ifdef CONFIG_DEBUG_GDB_ENABLED
+    return;
+#endif
+    st3m_usb_app_conf_t app = {
+        .fn_rx = st3m_console_cdc_on_rx,
+        .fn_txpoll = st3m_console_cdc_on_txpoll,
+        .fn_detach = st3m_console_cdc_on_detach,
+    };
+    st3m_usb_mode_t usb_mode = {
+        .kind = st3m_usb_mode_kind_app,
+        .app = &app,
+    };
+    st3m_usb_mode_switch(&usb_mode);
+    puts(" ___ _           ___     _         _");
+    puts("|  _| |___ _ _ _|_  |___| |_ ___ _| |___ ___");
+    puts("|  _| | . | | | |_  |  _| . | .'| . | . | -_|");
+    puts("|_| |_|___|_____|___|_| |___|__,|___|_  |___|");
+    puts("                                    |___|");
+}
diff --git a/components/st3m/st3m_usb.h b/components/st3m/st3m_usb.h
index 72e8f2e150..5aa726d77c 100644
--- a/components/st3m/st3m_usb.h
+++ b/components/st3m/st3m_usb.h
@@ -38,6 +38,7 @@
 #include <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>
+#include "sdkconfig.h"
 
 typedef enum {
     // Device should not enumerate.
@@ -106,6 +107,8 @@ void st3m_usb_mode_switch(st3m_usb_mode_t *target);
 // Initialize the subsystem. Must be called, bad things will happen otherwise.
 void st3m_usb_init();
 
+void st3m_usb_startup();
+
 // Return true if the badge is connected to a host.
 bool st3m_usb_connected(void);
 
diff --git a/main/main.c b/main/main.c
index ade4f7c08f..440f7bf91d 100644
--- a/main/main.c
+++ b/main/main.c
@@ -57,21 +57,7 @@ static QueueHandle_t _core1_init_done_q;
 void _init_core1(void *unused) {
     st3m_usb_init();
     st3m_console_init();
-    st3m_usb_app_conf_t app = {
-        .fn_rx = st3m_console_cdc_on_rx,
-        .fn_txpoll = st3m_console_cdc_on_txpoll,
-        .fn_detach = st3m_console_cdc_on_detach,
-    };
-    st3m_usb_mode_t usb_mode = {
-        .kind = st3m_usb_mode_kind_app,
-        .app = &app,
-    };
-    st3m_usb_mode_switch(&usb_mode);
-    puts(" ___ _           ___     _         _");
-    puts("|  _| |___ _ _ _|_  |___| |_ ___ _| |___ ___");
-    puts("|  _| | . | | | |_  |  _| . | .'| . | . | -_|");
-    puts("|_| |_|___|_____|___|_| |___|__,|___|_  |___|");
-    puts("                                    |___|");
+    st3m_usb_startup();
 
     // Load bearing delay. USB crashes otherwise?
     // TODO(q3k): debug this
-- 
GitLab