From 57ddb6438e4c47756be5d49715c8b00f94e2fc96 Mon Sep 17 00:00:00 2001
From: Nikolay Amiantov <ab@fmap.me>
Date: Fri, 23 Aug 2019 17:59:19 +0300
Subject: [PATCH] Create libcard10-platform for shared constants

---
 bootloader/main.c                     | 6 ++----
 bootloader/meson.build                | 1 +
 epicardium/api/common.h               | 5 ++---
 epicardium/meson.build                | 8 ++++----
 epicardium/modules/lifecycle.c        | 4 ++--
 lib/card10-platform/card10-platform.h | 8 ++++++++
 lib/card10-platform/meson.build       | 8 ++++++++
 lib/meson.build                       | 1 +
 8 files changed, 28 insertions(+), 13 deletions(-)
 create mode 100644 lib/card10-platform/card10-platform.h
 create mode 100644 lib/card10-platform/meson.build

diff --git a/bootloader/main.c b/bootloader/main.c
index 85ad2edb..c1f0a87d 100644
--- a/bootloader/main.c
+++ b/bootloader/main.c
@@ -2,6 +2,7 @@
 #include "card10-version.h"
 
 #include "card10.h"
+#include "card10-platform.h"
 #include "led.h"
 #include "pb.h"
 #include "pmic.h"
@@ -30,9 +31,6 @@
 #define PARTITION_END (0x10000000 + 1024 * 1024 - 1)
 #define MSC_MAGIC 0x6E697807
 
-/* TODO: Make this address part of the linker script */
-void* API_CALL_MEM = (void*)0x20080000;
-
 DIR dir;
 FATFS FatFs;
 
@@ -75,7 +73,7 @@ int mount(void)
 int get_msc_flag(void)
 {
 	// We use the API call memory to store a magic value there.
-	uint32_t* flag = (uint32_t*)API_CALL_MEM;
+	uint32_t* flag = API_CALL_MEM_ADDRESS;
 	if (*flag == MSC_MAGIC) {
 		*flag = 0;
 		return 1;
diff --git a/bootloader/meson.build b/bootloader/meson.build
index 9a680aca..32780105 100644
--- a/bootloader/meson.build
+++ b/bootloader/meson.build
@@ -24,6 +24,7 @@ elf = executable(
   version_hdr,
   dependencies: [
     libcard10,
+    libcard10_platform,
     max32665_startup_boot,
     libff13,
     maxusb,
diff --git a/epicardium/api/common.h b/epicardium/api/common.h
index ff0c9f3a..5e8a1e0c 100644
--- a/epicardium/api/common.h
+++ b/epicardium/api/common.h
@@ -1,5 +1,6 @@
 #pragma once
 #include "epicardium.h"
+#include "card10-platform.h"
 
 #include <stdint.h>
 #include <stdbool.h>
@@ -49,6 +50,4 @@ struct api_call_mem {
 	uint8_t buffer[1];
 };
 
-/* TODO: Make this address part of the linker script */
-static __attribute__((unused)) struct api_call_mem* API_CALL_MEM =
-	(struct api_call_mem*)0x20080000;
+static __attribute__((unused)) struct api_call_mem* API_CALL_MEM = API_CALL_MEM_ADDRESS;
diff --git a/epicardium/meson.build b/epicardium/meson.build
index 1aa55560..f594b4ef 100644
--- a/epicardium/meson.build
+++ b/epicardium/meson.build
@@ -24,13 +24,13 @@ api_caller_lib = static_library(
   'api/caller.c',
   'api/interrupt-receiver.c',
   api[0], # Caller
-  dependencies: periphdriver,
+  dependencies: [periphdriver, libcard10_platform],
 )
 
 api_caller = declare_dependency(
   include_directories: include_directories('.'),
   link_with: api_caller_lib,
-  dependencies: periphdriver,
+  dependencies: [periphdriver, libcard10_platform],
 )
 
 api_dispatcher_lib = static_library(
@@ -39,7 +39,7 @@ api_dispatcher_lib = static_library(
   'api/interrupt-sender.c',
   'api/control.c',
   api[1], # Dispatcher
-  dependencies: [libcard10, periphdriver],
+  dependencies: [libcard10, libcard10_platform, periphdriver],
 )
 
 ##########################################################################
@@ -88,7 +88,7 @@ elf = executable(
   l0der_sources,
   ble_sources,
   version_hdr,
-  dependencies: [libcard10, max32665_startup_core0, maxusb, libff13, ble, bhy1],
+  dependencies: [libcard10, libcard10_platform, max32665_startup_core0, maxusb, libff13, ble, bhy1],
   link_with: [api_dispatcher_lib, freertos],
   link_whole: [max32665_startup_core0_lib, board_card10_lib, newlib_heap_lib],
   include_directories: [freertos_includes],
diff --git a/epicardium/modules/lifecycle.c b/epicardium/modules/lifecycle.c
index 214fe4a7..fcadfcc8 100644
--- a/epicardium/modules/lifecycle.c
+++ b/epicardium/modules/lifecycle.c
@@ -6,6 +6,7 @@
 #include "l0der/l0der.h"
 
 #include "card10.h"
+#include "card10-platform.h"
 
 #include "FreeRTOS.h"
 #include "task.h"
@@ -17,7 +18,6 @@
 
 #define PYCARDIUM_IVT (void *)0x10080000
 #define BLOCK_WAIT pdMS_TO_TICKS(1000)
-#define MSC_MAGIC 0x6E697807
 
 /*
  * Loading an empty filename into Pycardium will drop straight into the
@@ -287,7 +287,7 @@ static void load_menu(bool reset)
 void epic_system_reset(int to_bootloader)
 {
 	// Set a flag for the bootloader.
-	uint32_t* flag = (uint32_t*)API_CALL_MEM;
+	uint32_t* flag = API_CALL_MEM_ADDRESS;
 	*flag = to_bootloader ? MSC_MAGIC : 0;
 
 	card10_reset();
diff --git a/lib/card10-platform/card10-platform.h b/lib/card10-platform/card10-platform.h
new file mode 100644
index 00000000..138a7013
--- /dev/null
+++ b/lib/card10-platform/card10-platform.h
@@ -0,0 +1,8 @@
+#ifndef CARD10_PLATFORM_H
+
+#define MSC_MAGIC 0x6E697807
+
+/* TODO: Make this address part of the linker script */
+#define API_CALL_MEM_ADDRESS ((void*)0x20080000)
+
+#endif
diff --git a/lib/card10-platform/meson.build b/lib/card10-platform/meson.build
new file mode 100644
index 00000000..8cc8a03c
--- /dev/null
+++ b/lib/card10-platform/meson.build
@@ -0,0 +1,8 @@
+includes = include_directories(
+  './',
+)
+
+libcard10_platform = declare_dependency(
+  include_directories: includes,
+)
+
diff --git a/lib/meson.build b/lib/meson.build
index e43c62db..ddcb1d13 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -12,5 +12,6 @@ subdir('./FreeRTOS-Plus/')
 subdir('./micropython/')
 
 subdir('./card10/')
+subdir('./card10-platform/')
 subdir('./mx25lba/')
 subdir('./ff13/')
-- 
GitLab