From 32144f978e39e14e2e3cb1d0a5bd6fdbfbfd9242 Mon Sep 17 00:00:00 2001
From: Rahix <rahix@rahix.de>
Date: Sun, 11 Aug 2019 22:37:17 +0200
Subject: [PATCH] feat(epicardium): Add switch for core 1 debugging

Signed-off-by: Rahix <rahix@rahix.de>
---
 Documentation/how-to-build.rst |  2 ++
 epicardium/main.c              | 20 +++++++++++++++++++-
 meson.build                    |  7 +++++++
 meson_options.txt              |  8 ++++++++
 4 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/Documentation/how-to-build.rst b/Documentation/how-to-build.rst
index da17ca982..144c2e1f7 100644
--- a/Documentation/how-to-build.rst
+++ b/Documentation/how-to-build.rst
@@ -68,6 +68,8 @@ firmware features:
 - ``-Ddebug_prints=true``: Print more verbose debugging log messages
 - ``-Dble_trace=true``: Enable BLE tracing.  This will output lots of status
   info related to BLE.
+- ``-Ddebug_core1=true``: Enable the core 1 SWD lines which are exposed on the
+  SAO connector.  Only use this if you have a debugger which is modified for core 1.
 
 .. warning::
 
diff --git a/epicardium/main.c b/epicardium/main.c
index 56f532a67..dc3dfd259 100644
--- a/epicardium/main.c
+++ b/epicardium/main.c
@@ -6,6 +6,7 @@
 #include "max32665.h"
 #include "uart.h"
 #include "cdcacm.h"
+#include "gpio.h"
 
 #include "card10.h"
 #include "pmic.h"
@@ -51,7 +52,24 @@ int main(void)
 	LOG_INFO("startup", "Version " CARD10_VERSION);
 
 	card10_init();
-	card10_diag();
+#ifdef CARD10_DEBUG_CORE1
+	LOG_WARN("startup", "Core 1 Debugger Mode");
+	static const gpio_cfg_t swclk = {
+		PORT_0,
+		PIN_7,
+		GPIO_FUNC_ALT3,
+		GPIO_PAD_NONE,
+	};
+	static const gpio_cfg_t swdio = {
+		PORT_0,
+		PIN_6,
+		GPIO_FUNC_ALT3,
+		GPIO_PAD_NONE,
+	};
+
+	GPIO_Config(&swclk);
+	GPIO_Config(&swdio);
+#endif /* CARD10_DEBUG_CORE1 */
 
 	gfx_copy_region_raw(
 		&display_screen, 0, 0, 160, 80, 2, (const void *)(Heart)
diff --git a/meson.build b/meson.build
index 3dd0578c2..f1586ec93 100644
--- a/meson.build
+++ b/meson.build
@@ -28,6 +28,13 @@ if get_option('debug_prints')
   )
 endif
 
+if get_option('debug_core1')
+  add_global_arguments(
+    ['-DCARD10_DEBUG_CORE1=1'],
+    language: 'c',
+  )
+endif
+
 add_global_link_arguments(
   '-Wl,--gc-sections',
   '-lm',
diff --git a/meson_options.txt b/meson_options.txt
index bb96608f5..0bff7f441 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -6,6 +6,14 @@ option(
   description: 'Whether to print debug messages on the serial console'
 )
 
+option(
+  'debug_core1',
+  type: 'boolean',
+  value: false,
+
+  description: 'Enable core 1 debugging interface'
+)
+
 option(
   'ble_trace',
   type: 'boolean',
-- 
GitLab