From 9efa67a81539c2785e33985fc420e98c35359a36 Mon Sep 17 00:00:00 2001
From: iliya <iliya.saroukha@hes-so.ch>
Date: Wed, 10 Jan 2024 16:08:13 +0100
Subject: [PATCH] feat: finishing UART

---
 mpu_user_console_etu.c | 22 ++++++++++++++--------
 user_cmd.c             | 39 +++++++++++++++++++++++++++++++--------
 user_cmd.h             | 20 +++++++++++++++++---
 3 files changed, 62 insertions(+), 19 deletions(-)

diff --git a/mpu_user_console_etu.c b/mpu_user_console_etu.c
index ca3af7c..52a3be4 100644
--- a/mpu_user_console_etu.c
+++ b/mpu_user_console_etu.c
@@ -62,14 +62,16 @@ void test_supervisor_mode() {
 }
 
 void ask_user_input(void) {
-	char buff[64] = { 0 };
+	char buff[USER_INPUT_SIZE] = { 0 };
 	char switch_cmd[] = "switch";
 	char status_cmd[] = "status";
 
-	printf("(%s) Would you like to `switch` or display `status` errors : ",
-			is_supervisor_mode ? "Supervisor" : "User");
-	fflush(stdin);
-	fscanf(stdin, "%s", buff);
+	PRINT("(%s) Would you like to `switch` or display `status` errors : ",
+			is_supervisor_mode ? "Supervisor" : "User")
+	;
+//	fflush(stdin);
+//	fscanf(stdin, "%s", buff);
+	uart_scanf(buff);
 
 	if (strncmp(buff, switch_cmd, strlen(switch_cmd)) == 0) {
 		// permute through svc depending on flag
@@ -79,14 +81,16 @@ void ask_user_input(void) {
 
 	if (strncmp(buff, status_cmd, strlen(status_cmd)) == 0) {
 		if (idx_error == 0) {
-			printf("No errors have occurred yet\n");
+			PRINT("No errors have occurred yet\n")
+			;
 			return;
 		}
 
 		for (int i = 0; i < idx_error; i++) {
 			if (i < BUF_SIZE) {
-				printf("Address that shat the bed : 0x%x\tError code : 0x%x\n",
-						arr_addr[i], arr_err_code[i]);
+				PRINT("Address that shat the bed : 0x%x\tError code : 0x%x\n",
+						arr_addr[i], arr_err_code[i])
+				;
 			}
 		}
 	}
@@ -105,6 +109,8 @@ void test_user_mode() {
 }
 
 int main(void) {
+	uart0_init_ref(9600, NULL, NULL);
+
 	LPC_GPIO2->FIODIR = 0xFF;
 	LPC_GPIO2->FIOPIN = 0;
 	// MPU configuration here...
diff --git a/user_cmd.c b/user_cmd.c
index 9fe526f..409f9e4 100644
--- a/user_cmd.c
+++ b/user_cmd.c
@@ -15,6 +15,7 @@
 
 #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);
@@ -28,17 +29,35 @@ void SVC_Handler() {
 	is_supervisor_mode = !is_supervisor_mode;
 }
 
+void uart_scanf(char *buff) {
+	char user_input[USER_INPUT_SIZE] = { 0 };
+	size_t idx = 1;
+
+	while (user_input[idx - 1] != '\n') {
+		idx--;
+		user_input[idx] = uart0_rec_byte_ref();
+		idx++;
+	}
+
+	user_input[idx - 1] = '\0';
+
+	memcpy(buff, user_input, USER_INPUT_SIZE);
+}
+
 void exec_user_read_write() {
-	char str[200];
+	char str[USER_INPUT_SIZE];
 	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):\n",
-			is_supervisor_mode ? "Supervisor" : "User");
+			is_supervisor_mode ? "Supervisor" : "User")
+	;
+
+//	fflush(stdin);
+//	fscanf(stdin, "%s", str);
 
-	fflush(stdin);
-	fscanf(stdin, "%s", str);
+	uart_scanf(str);
 
 	for (i = 0; i < strlen(str); i++) {
 		if (str[i] == ',') {
@@ -55,17 +74,21 @@ void exec_user_read_write() {
 	}
 	if ((coma_nb & (i < 2)) || i > 17) {
 		PRINT_SYNTAX_ERROR();
-		PRINT("(Bad length!)");
+		PRINT("(Bad length!)")
+		;
 		return;
 	}
 
 	sscanf(str, "%x", &addr);
 	if (!coma_nb) {					// if read
-		PRINT("reading address: 0x%08x\n", addr);
-		PRINT("value read: 0x%08x\n", *(unsigned*) addr);
+		PRINT("reading address: 0x%08x\n", addr)
+		;
+		PRINT("value read: 0x%08x\n", *(unsigned*) addr)
+		;
 	} else {						// write
 		sscanf(str + value_idx, "%x", &value);
-		PRINT("writing address: 0x%08x with 0x%08x\n", addr, value);
+		PRINT("writing address: 0x%08x with 0x%08x\n", addr, value)
+		;
 		*(unsigned*) addr = value;
 	}
 }
diff --git a/user_cmd.h b/user_cmd.h
index 75425c1..5554108 100644
--- a/user_cmd.h
+++ b/user_cmd.h
@@ -4,16 +4,30 @@
  *  Created on: 12 nov. 2023
  *  Author: VP
  */
-
 #ifndef USER_CMD_H_
 #define USER_CMD_H_
+#include <string.h>
+#include "uart.h"
+
+#define UART_STDOUT_SIZE 100
+#define USER_INPUT_SIZE 200
+
+static char uart_stdout[UART_STDOUT_SIZE] = { 0 };
 
-#define UART_ENABLE 0		// if 0, the console is MCUXpresso, otherwise the UART (use a terminal on PC side)
+#define UART_ENABLE 1		// 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);
 
 #if !UART_ENABLE
 #define PRINT printf
+#else
+#define PRINT uart_printf
 #endif
 
-void exec_user_read_write();
+void uart_scanf(char *buff);
+
+void exec_user_read_write(void);
 
 #endif /* USER_CMD_H_ */
-- 
GitLab