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