Skip to content
Snippets Groups Projects
Commit 9efa67a8 authored by iliya's avatar iliya
Browse files

feat: finishing UART

parent 314d9910
Branches
No related tags found
No related merge requests found
...@@ -62,14 +62,16 @@ void test_supervisor_mode() { ...@@ -62,14 +62,16 @@ void test_supervisor_mode() {
} }
void ask_user_input(void) { void ask_user_input(void) {
char buff[64] = { 0 }; char buff[USER_INPUT_SIZE] = { 0 };
char switch_cmd[] = "switch"; char switch_cmd[] = "switch";
char status_cmd[] = "status"; char status_cmd[] = "status";
printf("(%s) Would you like to `switch` or display `status` errors : ", PRINT("(%s) Would you like to `switch` or display `status` errors : ",
is_supervisor_mode ? "Supervisor" : "User"); is_supervisor_mode ? "Supervisor" : "User")
fflush(stdin); ;
fscanf(stdin, "%s", buff); // fflush(stdin);
// fscanf(stdin, "%s", buff);
uart_scanf(buff);
if (strncmp(buff, switch_cmd, strlen(switch_cmd)) == 0) { if (strncmp(buff, switch_cmd, strlen(switch_cmd)) == 0) {
// permute through svc depending on flag // permute through svc depending on flag
...@@ -79,14 +81,16 @@ void ask_user_input(void) { ...@@ -79,14 +81,16 @@ void ask_user_input(void) {
if (strncmp(buff, status_cmd, strlen(status_cmd)) == 0) { if (strncmp(buff, status_cmd, strlen(status_cmd)) == 0) {
if (idx_error == 0) { if (idx_error == 0) {
printf("No errors have occurred yet\n"); PRINT("No errors have occurred yet\n")
;
return; return;
} }
for (int i = 0; i < idx_error; i++) { for (int i = 0; i < idx_error; i++) {
if (i < BUF_SIZE) { if (i < BUF_SIZE) {
printf("Address that shat the bed : 0x%x\tError code : 0x%x\n", PRINT("Address that shat the bed : 0x%x\tError code : 0x%x\n",
arr_addr[i], arr_err_code[i]); arr_addr[i], arr_err_code[i])
;
} }
} }
} }
...@@ -105,6 +109,8 @@ void test_user_mode() { ...@@ -105,6 +109,8 @@ void test_user_mode() {
} }
int main(void) { int main(void) {
uart0_init_ref(9600, NULL, NULL);
LPC_GPIO2->FIODIR = 0xFF; LPC_GPIO2->FIODIR = 0xFF;
LPC_GPIO2->FIOPIN = 0; LPC_GPIO2->FIOPIN = 0;
// MPU configuration here... // MPU configuration here...
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#define PRINT_SYNTAX_ERROR() PRINT("Syntax error! Expected syntax is either <8 characters hexadecimal address> for reading\n or " \ #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"); "<8 characters hexadecimal address>=<8 characters hexadecimal value> for writing\n");
bool is_supervisor_mode = false; bool is_supervisor_mode = false;
void switch_to_user_mode(); void switch_to_user_mode();
void switch_to_supervisor_mode(void); void switch_to_supervisor_mode(void);
...@@ -28,17 +29,35 @@ void SVC_Handler() { ...@@ -28,17 +29,35 @@ void SVC_Handler() {
is_supervisor_mode = !is_supervisor_mode; 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() { void exec_user_read_write() {
char str[200]; char str[USER_INPUT_SIZE];
int i = 0, coma_nb = 0, value_idx; int i = 0, coma_nb = 0, value_idx;
unsigned addr, value; unsigned addr, value;
PRINT( PRINT(
"Write an hexadecimal address for reading <addr> or <addr>,<value> for writing (%s):\n", "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); uart_scanf(str);
fscanf(stdin, "%s", str);
for (i = 0; i < strlen(str); i++) { for (i = 0; i < strlen(str); i++) {
if (str[i] == ',') { if (str[i] == ',') {
...@@ -55,17 +74,21 @@ void exec_user_read_write() { ...@@ -55,17 +74,21 @@ void exec_user_read_write() {
} }
if ((coma_nb & (i < 2)) || i > 17) { if ((coma_nb & (i < 2)) || i > 17) {
PRINT_SYNTAX_ERROR(); PRINT_SYNTAX_ERROR();
PRINT("(Bad length!)"); PRINT("(Bad length!)")
;
return; return;
} }
sscanf(str, "%x", &addr); sscanf(str, "%x", &addr);
if (!coma_nb) { // if read if (!coma_nb) { // if read
PRINT("reading address: 0x%08x\n", addr); PRINT("reading address: 0x%08x\n", addr)
PRINT("value read: 0x%08x\n", *(unsigned*) addr); ;
PRINT("value read: 0x%08x\n", *(unsigned*) addr)
;
} else { // write } else { // write
sscanf(str + value_idx, "%x", &value); 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; *(unsigned*) addr = value;
} }
} }
......
...@@ -4,16 +4,30 @@ ...@@ -4,16 +4,30 @@
* Created on: 12 nov. 2023 * Created on: 12 nov. 2023
* Author: VP * Author: VP
*/ */
#ifndef USER_CMD_H_ #ifndef USER_CMD_H_
#define 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 #if !UART_ENABLE
#define PRINT printf #define PRINT printf
#else
#define PRINT uart_printf
#endif #endif
void exec_user_read_write(); void uart_scanf(char *buff);
void exec_user_read_write(void);
#endif /* USER_CMD_H_ */ #endif /* USER_CMD_H_ */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment