From 6f7228cd00d3fc61f84d407e4baf1db5c4a4e606 Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Wed, 14 Oct 2020 00:22:56 +0200
Subject: [PATCH] change(repl): Reset the hardware when entering REPL

This will make it easier for casual users to take first steps with the
card10.

The downside is that it might hinder debugging scripts which were
running, as for example all files of core 1 have been closed and
potentially all references to other APIs are lost.
---
 epicardium/epicardium.h        | 8 ++++++++
 epicardium/modules/hardware.c  | 6 +++---
 epicardium/modules/lifecycle.c | 2 +-
 epicardium/modules/modules.h   | 3 +--
 pycardium/main.c               | 1 +
 5 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h
index 842714028..cf803cdf7 100644
--- a/epicardium/epicardium.h
+++ b/epicardium/epicardium.h
@@ -33,6 +33,7 @@ typedef _Bool bool;
 #define API_SYSTEM_EXEC             0x2
 #define API_SYSTEM_RESET            0x3
 #define API_BATTERY_VOLTAGE         0x4
+#define API_HARDWARE_RESET          0x5
 
 #define API_INTERRUPT_ENABLE        0xA
 #define API_INTERRUPT_DISABLE       0xB
@@ -300,6 +301,13 @@ API(API_SYSTEM_EXEC, int __epic_exec(char *name));
  */
 API(API_SYSTEM_RESET, void epic_system_reset(void));
 
+/**
+ * Brings peripherals and hardware back into a sane state (LEDs off,
+ * display on, sensors off, vibration motor off, BLE not bonding,
+ * BLE not scanning, files of core 1 closed).
+ */
+API(API_HARDWARE_RESET, int epic_hardware_reset(void));
+
 /**
  * PMIC API
  * ===============
diff --git a/epicardium/modules/hardware.c b/epicardium/modules/hardware.c
index 055ae486b..6f30993ac 100644
--- a/epicardium/modules/hardware.c
+++ b/epicardium/modules/hardware.c
@@ -218,13 +218,13 @@ int hardware_init(void)
 }
 
 /*
- * hardware_reset() is called whenever a new l0dable is started.  hardware_reset()
+ * epic_hardware_reset() is called whenever a new l0dable is started. epic_hardware_reset()
  * should bring all peripherals back into a known initial state.  This does not
- * necessarily mean resetting the peripheral entirely but hardware_reset()
+ * necessarily mean resetting the peripheral entirely but epic_hardware_reset()
  * should at least bring the API facing part of a peripheral back into the state
  * a fresh booted l0dable expects.
  */
-int hardware_reset(void)
+int epic_hardware_reset(void)
 {
 	/*
 	 * API Dispatcher & API Interrupts
diff --git a/epicardium/modules/lifecycle.c b/epicardium/modules/lifecycle.c
index 76c5648cc..9fe6b7ae6 100644
--- a/epicardium/modules/lifecycle.c
+++ b/epicardium/modules/lifecycle.c
@@ -113,7 +113,7 @@ static int do_load(struct load_info *info)
 	/*
 	 * Reinitialize Hardware & Drivers
 	 */
-	res = hardware_reset();
+	res = epic_hardware_reset();
 	if (res < 0) {
 		goto out_free_api;
 	}
diff --git a/epicardium/modules/modules.h b/epicardium/modules/modules.h
index 14184bd9c..8ac21b880 100644
--- a/epicardium/modules/modules.h
+++ b/epicardium/modules/modules.h
@@ -19,10 +19,9 @@ void dispatcher_mutex_init(void);
 extern struct mutex api_mutex;
 extern TaskHandle_t dispatcher_task_id;
 
-/* ---------- Hardware Init & Reset ---------------------------------------- */
+/* ---------- Hardware Init ------------------------------------------------ */
 int hardware_early_init(void);
 int hardware_init(void);
-int hardware_reset(void);
 
 /* ---------- Lifecycle ---------------------------------------------------- */
 void vLifecycleTask(void *pvParameters);
diff --git a/pycardium/main.c b/pycardium/main.c
index 3aec04919..ce5ae4b4c 100644
--- a/pycardium/main.c
+++ b/pycardium/main.c
@@ -65,6 +65,7 @@ int main(void)
 		}
 
 		epic_uart_write_str(header, sizeof(header));
+		epic_hardware_reset();
 
 		for (;;) {
 			if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
-- 
GitLab