sketchy slow test with LEDs
[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 <drivers/led.h>
25
26 #define SERIAL 1
27 #define BUFSIZE 200
28 #define MAXARGS 5
29 #define WHITESPACE "\t\r\n "
30 #define BUILTINCMDS 4
31
32 /* 
33  * Built in commands
34  *      info -- shows basic info of system
35  *      uptime -- uptime; read from the RTC register
36  *      reset -- software reset TODO
37  *      showmem xxxxxxxx -- shows address value
38  *      led -- led on/off
39  *      switchmode -- switch to unprivileged mode TODO
40  * */
41
42 static char buf[BUFSIZE];
43
44
45 struct cmd {
46         char * name;
47         int (*function)(int argc, char ** argsv);
48 };
49
50 struct cmd builtincmds[BUILTINCMDS];
51
52 int info(int argc, char ** argsv) {
53         sysinfo();
54         return 0;
55 }
56
57 int uptime(int arg, char ** argsv) {
58         printf("CURRENT UPTIME: %d seconds \n", *RTC_CNTL);
59 }
60
61 int led(int argc, char ** argsv) {
62
63         if (argsv[1] != NULL) {
64                 if (strcmp(argsv[1], "on")) {
65                         printf("LED ON\n");
66                         led_on();
67                         }
68                 else if (strcmp(argsv[1], "off")) {
69                         printf("LED OFF\n");
70                         led_off();
71                         }
72         }
73         return 0;
74 }
75
76 int showmem(int argc, char ** argsv) {
77
78         if ((argsv[1] != NULL) && (strlen(argsv[1]) == 8)) {
79         
80                 uint32_t * check = (uint32_t *) hextoreg(argsv[1]);
81                 printf("LOCATION 0x%s, VALUE: %#x\n", argsv[1], *check);
82                 return 1;
83         
84         }
85
86         return 0;
87 }
88
89 int exec_cmd(char * buf) {
90
91         int argc;
92         char *argv[MAXARGS];
93         int i;
94
95         // Parse the command buffer into whitespace-separated arguments
96         argc = 0;
97         argv[argc] = 0;
98         while (1) {
99                 // gobble whitespace
100                 while (*buf && strchr(WHITESPACE, *buf))
101                         *buf++ = 0;
102                 if (*buf == 0)
103                         break;
104
105                 // save and scan past next arg
106                 if (argc == MAXARGS-1) {
107                         printf("Too many arguments\n");
108                         return 0;
109                 }
110                 argv[argc++] = buf;
111                 while (*buf && !strchr(WHITESPACE, *buf))
112                         buf++;
113         }
114         argv[argc] = 0;
115
116         // Lookup and invoke the command
117         if (argc == 0)
118                 return 0;
119         for (i = 0; i < BUILTINCMDS; i++) {
120                 if (strcmp(argv[0], builtincmds[i].name))
121                         return builtincmds[i].function(argc, argv);
122         }
123         printf("Unknown command\n");
124         return 0;
125
126
127
128 void terminal() {
129
130         builtincmds[0].name = "info";
131         builtincmds[0].function = info;
132
133         builtincmds[1].name = "led";
134         builtincmds[1].function = led;
135
136         builtincmds[2].name = "showmem";
137         builtincmds[2].function = showmem;
138
139         builtincmds[3].name = "uptime";
140         builtincmds[3].function = uptime;
141
142
143         char *buf;
144  
145          while (1) {
146                  buf = readline("root# ");
147                         if (buf != NULL)
148                          if (exec_cmd(buf) < 0)
149                                  break;
150          }
151 }
152