-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.c
More file actions
132 lines (122 loc) · 3.68 KB
/
Copy pathkernel.c
File metadata and controls
132 lines (122 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <stdint.h>
#include <string.h>
void uart_init();
void uart_send_string(const char *str);
char uart_recv();
void uart_send(char c);
void get_board_revision();
void get_arm_memory();
void uart_send_hex(uint32_t value);
/**
* @brief Prints the core ID of the current CPU core.
*
* This function retrieves the core ID from the MPIDR_EL1 register,
* masks the relevant bits to extract the core ID, and then sends
* the core ID as a hexadecimal string via UART.
*
* The core ID is extracted from bits 0:1 of the MPIDR_EL1 register.
*
*/
void print_core_id()
{
uint64_t core_id;
asm volatile("mrs %0, mpidr_el1" : "=r"(core_id));
core_id &= 0xFF; // 取出核心 ID (bits 0:1)
uart_send_string("Core ID: 0x");
uart_send_hex((uint32_t)core_id);
uart_send_string("\r\n");
}
void shell()
{
char buffer[256];
int index = 0;
uart_send_string("Welcome to the simple shell!\r\n");
uart_send_string("> ");
while (1)
{
char c = uart_recv();
uart_send(c); // Echo the received character
if (c == '\r' || c == '\n')
{
uart_send_string("\r\n");
buffer[index] = '\0'; // Null-terminate the string
// Prevent the empty input
if (index == 0)
{
uart_send_string("> ");
continue;
}
if (strcmp(buffer, "help") == 0)
{
uart_send_string("Available commands:\r\n");
uart_send_string("help - print all available commands\r\n");
uart_send_string("hello - print Hello World!\r\n");
uart_send_string("modinfo - get board revision & ARM memory information\r\n");
uart_send_string("boardrev - get board revision\r\n");
uart_send_string("armmem - get ARM memory information\r\n");
uart_send_string("coreid - print current core ID\r\n");
}
else if (strcmp(buffer, "hello") == 0)
{
uart_send_string("Hello World!\r\n");
}
else if (strcmp(buffer, "modinfo") == 0)
{
get_board_revision();
get_arm_memory();
}
else if (strcmp(buffer, "boardrev") == 0)
{
get_board_revision();
}
else if (strcmp(buffer, "armmem") == 0)
{
get_arm_memory();
}
else if (strcmp(buffer, "coreid") == 0)
{
print_core_id();
}
else
{
uart_send_string("Unknown command: ");
uart_send_string(buffer);
uart_send_string("\r\n");
}
index = 0;
uart_send_string("> ");
}
/**
* Handles backspace or delete character input.
* If the character `c` is a backspace (`\b`) or delete (ASCII 127),
* and there are characters to delete (index > 0), it decrements the index
* and sends the backspace sequence to the UART to remove the character
* from the display.
*
* @param c The character input to handle.
* @param index The current position in the input buffer.
*/
else if (c == '\b' || c == 127)
{
if (index > 0)
{
index--;
uart_send_string("\b \b"); // Handle backspace
}
}
else
{
buffer[index++] = c;
}
}
}
void kernel_main()
{
uart_init();
print_core_id(); // 在 shell 啟動前打印核心 ID
shell();
while (1)
{
// Loop indefinitely
}
}