1 /* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
4 * 2019/8/14 - ROBIN KRENS
8 * Small terminal with some built-in debug commands
16 #include <sys/robsys.h>
19 #include <lib/stdio.h>
20 #include <lib/string.h>
21 #include <lib/regfunc.h>
22 #include <lib/tinyprintf.h>
26 #include <drivers/led.h>
31 #define WHITESPACE "\t\r\n "
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
41 * switchmode -- switch to unprivileged mode TODO
44 static char buf[BUFSIZE];
48 int (*function)(int argc, char ** argsv);
51 struct cmd builtincmds[BUILTINCMDS];
53 int info(int argc, char ** argsv) {
58 int uptime(int arg, char ** argsv) {
59 printf("CURRENT UPTIME: %d seconds \n", *RTC_CNTL);
62 int led(int argc, char ** argsv) {
64 if (argsv[1] != NULL) {
65 if (strcmp(argsv[1], "on")) {
69 else if (strcmp(argsv[1], "off")) {
77 int showmem(int argc, char ** argsv) {
79 if ((argsv[1] != NULL) && (strlen(argsv[1]) == 8)) {
81 uint32_t * check = (uint32_t *) hextoreg(argsv[1]);
82 printf("LOCATION 0x%s, VALUE: %#x\n", argsv[1], *check);
87 extern mem_pool_t kheap_pool;
88 int * a = kalloc(&kheap_pool);
96 int exec_cmd(char * buf) {
102 // Parse the command buffer into whitespace-separated arguments
107 while (*buf && strchr(WHITESPACE, *buf))
112 // save and scan past next arg
113 if (argc == MAXARGS-1) {
114 printf("Too many arguments\n");
118 while (*buf && !strchr(WHITESPACE, *buf))
123 // Lookup and invoke the command
126 for (i = 0; i < BUILTINCMDS; i++) {
127 if (strcmp(argv[0], builtincmds[i].name))
128 return builtincmds[i].function(argc, argv);
130 printf("Unknown command\n");
137 builtincmds[0].name = "info";
138 builtincmds[0].function = info;
140 builtincmds[1].name = "led";
141 builtincmds[1].function = led;
143 builtincmds[2].name = "showmem";
144 builtincmds[2].function = showmem;
146 builtincmds[3].name = "uptime";
147 builtincmds[3].function = uptime;
153 buf = readline("root# ");
155 if (exec_cmd(buf) < 0)