From 944578ca6fd116ff98b71de80cef699e63d62fc2 Mon Sep 17 00:00:00 2001
From: Rahix <rahix@rahix.de>
Date: Tue, 20 Aug 2019 15:15:09 +0200
Subject: [PATCH] refactor(lifecycle): Split reset into trigger & wait

This change allows cleaner loading of payloads and is a preparation for
the proper implementation/use of hardware_reset().

Signed-off-by: Rahix <rahix@rahix.de>
---
 epicardium/api/control.c       |  5 ++++-
 epicardium/api/dispatcher.h    |  5 ++++-
 epicardium/modules/lifecycle.c | 27 +++++++++++++++++----------
 3 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/epicardium/api/control.c b/epicardium/api/control.c
index 9302eb5e..e11037b4 100644
--- a/epicardium/api/control.c
+++ b/epicardium/api/control.c
@@ -199,11 +199,14 @@ void core1_boot(void)
 	core1_start(&core1_initial_ivt);
 }
 
-void core1_reset(void)
+void core1_trigger_reset(void)
 {
 	/* Signal core 1 that we intend to load a new payload. */
 	api_interrupt_trigger(EPIC_INT_RESET);
+}
 
+void core1_wait_ready(void)
+{
 	/* Wait for the core to accept */
 	while (1) {
 		while (SEMA_GetSema(_CONTROL_SEMAPHORE) == E_BUSY) {
diff --git a/epicardium/api/dispatcher.h b/epicardium/api/dispatcher.h
index 2299f54d..4b79095e 100644
--- a/epicardium/api/dispatcher.h
+++ b/epicardium/api/dispatcher.h
@@ -37,7 +37,10 @@ void api_prepare_args(char *args);
 void core1_boot(void);
 
 /* Reset core 1 into a state where it can accept a new payload */
-void core1_reset(void);
+void core1_trigger_reset(void);
+
+/* Wait for core 1 to respond that it is ready for a new payload */
+void core1_wait_ready(void);
 
 /* Load a payload into core 1 */
 void core1_load(void *ivt, char *args);
diff --git a/epicardium/modules/lifecycle.c b/epicardium/modules/lifecycle.c
index 85968154..b3ce83c1 100644
--- a/epicardium/modules/lifecycle.c
+++ b/epicardium/modules/lifecycle.c
@@ -2,6 +2,7 @@
 #include "modules/log.h"
 #include "modules/modules.h"
 #include "api/dispatcher.h"
+#include "api/interrupt-sender.h"
 #include "l0der/l0der.h"
 
 #include "card10.h"
@@ -87,6 +88,9 @@ static int load_stat(char *name)
  */
 static int do_load(struct load_info *info)
 {
+	struct l0dable_info l0dable;
+	int res;
+
 	if (*info->name == '\0') {
 		LOG_INFO("lifecycle", "Loading Python interpreter ...");
 	} else {
@@ -100,11 +104,21 @@ static int do_load(struct load_info *info)
 
 	if (info->do_reset) {
 		LOG_DEBUG("lifecycle", "Triggering core 1 reset.");
-		core1_reset();
 
-		api_dispatcher_init();
+		core1_trigger_reset();
 	}
 
+	/*
+	 * Wait for the core to become ready to accept a new payload.
+	 */
+	core1_wait_ready();
+
+	/*
+	 * Reinitialize API
+	 */
+	api_dispatcher_init();
+	api_interrupt_init();
+
 	switch (info->type) {
 	case PL_PYTHON_SCRIPT:
 	case PL_PYTHON_DIR:
@@ -112,14 +126,7 @@ static int do_load(struct load_info *info)
 		core1_load(PYCARDIUM_IVT, info->name);
 		break;
 	case PL_L0DABLE:
-		/*
-		 * Always reset when loading a l0dable to make sure the memory
-		 * space is absolutely free.
-		 */
-		core1_reset();
-
-		struct l0dable_info l0dable;
-		int res = l0der_load_path(info->name, &l0dable);
+		res = l0der_load_path(info->name, &l0dable);
 		if (res != 0) {
 			LOG_ERR("lifecycle", "l0der failed: %d\n", res);
 			xSemaphoreGive(api_mutex);
-- 
GitLab