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