From 5da5d72565fecb9b49712d4c0cd8b17b5cdc5ef7 Mon Sep 17 00:00:00 2001
From: iliya <iliya.saroukha@hes-so.ch>
Date: Wed, 10 Jan 2024 19:41:40 +0100
Subject: [PATCH] feat: conditional compilation based on UART_ENABLE macro

---
 mpu_user_console_etu.c | 18 +++++++++++++-----
 user_cmd.c             | 29 +++++++++++++++++------------
 user_cmd.h             |  8 ++++----
 3 files changed, 34 insertions(+), 21 deletions(-)

diff --git a/mpu_user_console_etu.c b/mpu_user_console_etu.c
index 03e0d5b..cbdc09b 100644
--- a/mpu_user_console_etu.c
+++ b/mpu_user_console_etu.c
@@ -67,11 +67,17 @@ void ask_user_input(void) {
 	char status_cmd[] = "status";
 
 	PRINT("\r\n(%s) Would you like to `switch` or display `status` errors : ",
-			is_supervisor_mode ? "Supervisor" : "User");
-//	fflush(stdin);
-//	fscanf(stdin, "%s", buff);
+			is_supervisor_mode ? "Supervisor" : "User")
+	;
+
+#if UART_ENABLE
 	uart_scanf(buff);
-	PRINT("\r\n");
+	PRINT("\r\n")
+	;
+#else
+	fflush(stdin);
+	fscanf(stdin, "%s", buff);
+#endif
 
 	if (strncmp(buff, switch_cmd, strlen(switch_cmd)) == 0) {
 		// permute through svc depending on flag
@@ -88,7 +94,7 @@ void ask_user_input(void) {
 
 		for (int i = 0; i < idx_error; i++) {
 			if (i < BUF_SIZE) {
-				PRINT("Address that shat the bed : 0x%x\tError code : 0x%x\r\n",
+				PRINT("Address where error occurred : 0x%x\tError code : 0x%x\r\n",
 						arr_addr[i], arr_err_code[i])
 				;
 			}
@@ -109,7 +115,9 @@ void test_user_mode() {
 }
 
 int main(void) {
+#if UART_ENABLE
 	uart0_init_ref(9600, NULL, NULL);
+#endif
 
 	LPC_GPIO2->FIODIR = 0xFF;
 	LPC_GPIO2->FIOPIN = 0;
diff --git a/user_cmd.c b/user_cmd.c
index cdb3b6a..18478b6 100644
--- a/user_cmd.c
+++ b/user_cmd.c
@@ -12,19 +12,21 @@
 #include <stdarg.h>
 #include "uart.h"
 #include "user_cmd.h"
+#include "macro.h"
 
 #define PRINT_SYNTAX_ERROR() PRINT("Syntax error! Expected syntax is either <8 characters hexadecimal address> for reading\n or " \
 									"<8 characters hexadecimal address>=<8 characters hexadecimal value> for writing\n");
 
 bool is_supervisor_mode = false;
-void switch_to_user_mode();
-void switch_to_supervisor_mode(void);
+//void switch_to_user_mode();
+void switch_mode(int control_value);
+//void switch_to_supervisor_mode(void);
 
 void SVC_Handler() {
-	if (!is_supervisor_mode) { 	// If user mode
-		switch_to_supervisor_mode();
-	} else {					// If supervisor
-		switch_to_user_mode();
+	if (is_supervisor_mode) {
+		switch_mode(USER_CONTROL_VALUE);
+	} else {
+		switch_mode(SUPERVISOR_CONTROL_VALUE);
 	}
 	is_supervisor_mode = !is_supervisor_mode;
 }
@@ -40,7 +42,7 @@ void uart_scanf(char *buff) {
 		idx++;
 	}
 
-	user_input[idx-1] = '\0';
+	user_input[idx - 1] = '\0';
 
 	memcpy(buff, user_input, USER_INPUT_SIZE);
 }
@@ -50,13 +52,16 @@ void exec_user_read_write() {
 	int i = 0, coma_nb = 0, value_idx;
 	unsigned addr, value;
 
-	PRINT("Write an hexadecimal address for reading <addr> or <addr>,<value> for writing (%s):\r\n",
-			is_supervisor_mode ? "Supervisor" : "User");
-
-//	fflush(stdin);
-//	fscanf(stdin, "%s", str);
+	PRINT("(%s) Write an hexadecimal address for reading <addr> or <addr>,<value> for writing :\r\n",
+			is_supervisor_mode ? "Supervisor" : "User")
+	;
 
+#if UART_ENABLE
 	uart_scanf(str);
+#else
+	fflush(stdin);
+	fscanf(stdin, "%s", str);
+#endif
 
 	for (i = 0; i < strlen(str); i++) {
 		if (str[i] == ',') {
diff --git a/user_cmd.h b/user_cmd.h
index 39aa642..3d4f370 100644
--- a/user_cmd.h
+++ b/user_cmd.h
@@ -14,17 +14,17 @@
 
 static char uart_stdout[UART_STDOUT_SIZE] = { 0 };
 
-#define UART_ENABLE 1		// if 0, the console is MCUXpresso, otherwise the UART (use a terminal on PC side)
+#define UART_ENABLE 0		// if 0, the console is MCUXpresso, otherwise the UART (use a terminal on PC side)
 
 #define uart_printf(...) \
 	snprintf(uart_stdout, UART_STDOUT_SIZE, __VA_ARGS__); \
 	uart0_send_ref((uint8_t *)uart_stdout, UART_STDOUT_SIZE); \
 	memset(uart_stdout, 0, UART_STDOUT_SIZE);
 
-#if !UART_ENABLE
-#define PRINT printf
-#else
+#if UART_ENABLE
 #define PRINT uart_printf
+#else
+#define PRINT printf
 #endif
 
 void uart_scanf(char *buff);
-- 
GitLab