4a2a88bf549c99765f739cedf17bcd6d91a14cbc
[cortex-from-scratch] / term.c
1 /* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
2  * 
3  * $LOG$
4  * 2019/8/14 - ROBIN KRENS      
5  * Initial version 
6  * 
7  * $DESCRIPTION$
8  * Small terminal with some built-in debug commands
9  * 
10  * */
11
12 #include <stdbool.h>
13 #include <stddef.h>
14 #include <stdint.h>
15
16 #include <sys/robsys.h>
17 #include <sys/mmap.h>
18
19 #include <lib/stdio.h>
20 #include <lib/string.h>
21 #include <lib/regfunc.h>
22 #include <lib/tinyprintf.h>
23
24 #include <lib/pool.h>
25
26 #include <drivers/led.h>
27
28 #define SERIAL 1
29 #define BUFSIZE 200
30 #define MAXARGS 5
31 #define WHITESPACE "\t\r\n "
32 #define BUILTINCMDS 4
33
34 /* 
35  * Built in commands
36  *      info -- shows basic info of system
37  *      uptime -- uptime; read from the RTC register
38  *      reset -- software reset TODO
39  *      showmem xxxxxxxx -- shows address value
40  *      led -- led on/off
41  *      switchmode -- switch to unprivileged mode TODO
42  * */
43
44 static char buf[BUFSIZE];
45
46 struct cmd {
47         char * name;
48         int (*function)(int argc, char ** argsv);
49 };
50
51 struct cmd builtincmds[BUILTINCMDS];
52
53 int info(int argc, char ** argsv) {
54         extern mem_pool_t kheap_pool;
55         kheap_info(&kheap_pool);
56         return 0;
57 }
58
59 int uptime(int arg, char ** argsv) {
60         printf("CURRENT UPTIME: %d seconds \n", *RTC_CNTL);
61 }
62
63 int led(int argc, char ** argsv) {
64
65         if (argsv[1] != NULL) {
66                 if (strcmp(argsv[1], "on")) {
67                         printf("LED ON\n");
68                         led_on();
69                         }
70                 else if (strcmp(argsv[1], "off")) {
71                         printf("LED OFF\n");
72                         led_off();
73                         }
74         }
75         return 0;
76 }
77
78 int showmem(int argc, char ** argsv) {
79
80         if ((argsv[1] != NULL) && (strlen(argsv[1]) == 8)) {
81         
82                 uint32_t * check = (uint32_t *) hextoreg(argsv[1]);
83                 printf("LOCATION 0x%s, VALUE: %#x\n", argsv[1], *check);
84                 return 1;
85         
86         }
87
88         return 0;
89 }
90
91 int exec_cmd(char * buf) {
92
93         int argc;
94         char *argv[MAXARGS];
95         int i;
96
97         // Parse the command buffer into whitespace-separated arguments
98         argc = 0;
99         argv[argc] = 0;
100         while (1) {
101                 // gobble whitespace
102                 while (*buf && strchr(WHITESPACE, *buf))
103                         *buf++ = 0;
104                 if (*buf == 0)
105                         break;
106
107                 // save and scan past next arg
108                 if (argc == MAXARGS-1) {
109                         printf("Too many arguments\n");
110                         return 0;
111                 }
112                 argv[argc++] = buf;
113                 while (*buf && !strchr(WHITESPACE, *buf))
114                         buf++;
115         }
116         argv[argc] = 0;
117
118         // Lookup and invoke the command
119         if (argc == 0)
120                 return 0;
121         for (i = 0; i < BUILTINCMDS; i++) {
122                 if (strcmp(argv[0], builtincmds[i].name))
123                         return builtincmds[i].function(argc, argv);
124         }
125         printf("Unknown command\n");
126         return 0;
127
128
129
130 void terminal() {
131
132         builtincmds[0].name = "info";
133         builtincmds[0].function = info;
134
135         builtincmds[1].name = "led";
136         builtincmds[1].function = led;
137
138         builtincmds[2].name = "showmem";
139         builtincmds[2].function = showmem;
140
141         builtincmds[3].name = "uptime";
142         builtincmds[3].function = uptime;
143
144
145         char *buf;
146  
147          while (1) {
148                  buf = readline("root# ");
149                         if (buf != NULL)
150                          if (exec_cmd(buf) < 0)
151                                  break;
152          }
153 }
154