OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [orpmon/] [common/] [common.c] - Blame information for rev 820

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 809 simons
#include "common.h"
2
#include "uart.h"
3
#include "screen.h"
4
#include "support.h"
5
 
6
#define MAX_COMMANDS  100
7
 
8 817 simons
extern unsigned long src_addr;
9
 
10 809 simons
bd_t bd;
11
 
12
int num_commands = 0;
13
 
14
struct command_struct {
15
  const char *name;
16
  const char *params;
17
  const char *help;
18
  int (*func)(int argc, char *argv[]);
19
} command[MAX_COMMANDS];
20
 
21
void putc (const char c)
22
{
23
  debug ("getc %i, %i = %c\n", bd.bi_console_type, c, c);
24
  switch (bd.bi_console_type) {
25
  case CT_NONE:
26
    break;
27
  case CT_UART:
28
    uart_putc (c);
29
    break;
30
  case CT_CRT:
31
    screen_putc (c);
32
    break;
33
  case CT_SIM:
34
    __printf ("%c", c);
35
    break;
36
  }
37
}
38
 
39
int getc ()
40
{
41
  debug ("getc %i\n", bd.bi_console_type);
42
  switch (bd.bi_console_type) {
43
  case CT_NONE:
44
  case CT_CRT:
45
  case CT_SIM:
46
    return -1;
47
  case CT_UART:
48
    return uart_getc ();
49
  }
50
  return -1;
51
}
52
 
53
int testc ()
54
{
55
  debug ("testc %i\n", bd.bi_console_type);
56
  switch (bd.bi_console_type) {
57
  case CT_NONE:
58
  case CT_CRT:
59
  case CT_SIM:
60
    return -1;
61
  case CT_UART:
62
    return uart_testc ();
63
  }
64
  return -1;
65
}
66
 
67
int ctrlc ()
68
{
69
  if (testc ()) {
70
    switch (getc ()) {
71
      case 0x03:    /* ^C - Control C */
72
        return 1;
73
      default:
74
        break;
75
    }
76
  }
77
  return 0;
78
}
79
 
80 812 markom
unsigned long parse_ip (char *ip)
81
{
82
  unsigned long num;
83
  num = strtoul (ip, &ip, 10) & 0xff;
84
  if (*ip++ != '.') return 0;
85
  num = (num << 8) | (strtoul (ip, &ip, 10) & 0xff);
86
  if (*ip++ != '.') return 0;
87
  num = (num << 8) | (strtoul (ip, &ip, 10) & 0xff);
88
  if (*ip++ != '.') return 0;
89
  num = (num << 8) | (strtoul (ip, &ip, 10) & 0xff);
90
  return num;
91
}
92
 
93 809 simons
void change_console_type (enum bi_console_type_t con_type)
94
{
95
  debug ("Console change %i -> %i\n", bd.bi_console_type, con_type);
96
  /* Close previous */
97
  switch (bd.bi_console_type) {
98
  case CT_NONE:
99
  case CT_UART:
100
  case CT_CRT:
101
  case CT_SIM:
102
    break;
103
  }
104
  bd.bi_console_type = con_type;
105
  /* Initialize new */
106
  switch (bd.bi_console_type) {
107
  case CT_NONE:
108
    break;
109
  case CT_UART:
110
    uart_init ();
111
    break;
112
  case CT_CRT:
113
    screen_init ();
114
    break;
115
  case CT_SIM:
116
    break;
117
  }
118
}
119
 
120
void register_command_func (const char *name, const char *params, const char *help, int (*func)(int argc, char *argv[]))
121
{
122
  debug ("register_command '%s'\n", name);
123
  if (num_commands < MAX_COMMANDS) {
124
    command[num_commands].name = name;
125
    command[num_commands].params = params;
126
    command[num_commands].help = help;
127
    command[num_commands].func = func;
128
    num_commands++;
129
  } else printf ("Command '%s' ignored; MAX_COMMANDS limit reached\n", name);
130
}
131
 
132
/* Process command and arguments by executing
133
   specific function. */
134
void mon_command(void)
135
{
136
  char c = '\0';
137
  char str[1000];
138
  char *pstr = str;
139
  char *command_str;
140
  char *argv[20];
141
  int argc = 0;
142
 
143
  /* Show prompt */
144
#ifdef XESS
145
  printf ("\norp-xsv> ");
146
#else
147
  printf ("\nbender> ");
148
#endif
149
 
150
  /* Get characters from UART */
151
  c = getc();
152
  while (c != '\r' && c != '\f' && c != '\n')
153
  {
154
    if (c == '\b')
155
      pstr--;
156
    else
157
      *pstr++ = c;
158
    putc(c);
159
    c = getc();
160
  }
161
  *pstr = '\0';
162
  printf ("\n");
163
 
164
  /* Skip leading blanks */
165
  pstr = str;
166
  while (*pstr == ' ' && *pstr != '\0') pstr++;
167
 
168
  /* Get command from the string */
169
  command_str = pstr;
170
 
171 817 simons
  while (1) {
172 809 simons
    /* Go to next argument */
173 817 simons
    while (*pstr != ' ' && *pstr != '\0') pstr++;
174
    if (*pstr) {
175
      *pstr++ = '\0';
176
      while (*pstr == ' ') pstr++;
177
      argv[argc++] = pstr;
178
    }
179
    else
180
      break;
181 809 simons
  }
182
 
183
  {
184
    int i, found = 0;
185
    for (i = 0; i < num_commands; i++)
186
      if (strcmp (command_str, command[i].name) == 0) {
187
        switch (command[i].func (argc, &argv[0])) {
188
        case -1:
189
          printf ("Missing/wrong parameters, usage: %s %s\n", command[i].name, command[i].params);
190
          break;
191
        }
192
        found = 1;
193
        break;
194
      }
195
    if (!found) printf ("Unknown command. Type 'help' for help.\n");
196
  }
197
}
198
 
199
#if HELP_ENABLED
200
/* Displays help screen */
201
int help_cmd (int argc, char *argv[])
202
{
203
  int i;
204
  for (i = 0; i < num_commands; i++)
205
    printf ("%-10s %-20s - %s\n", command[i].name, command[i].params, command[i].help);
206
  return 0;
207
}
208
#endif /* HELP_ENABLED */
209
 
210
void module_cpu_init (void);
211
void module_memory_init (void);
212
void module_eth_init (void);
213 816 markom
void module_dhry_init (void);
214 809 simons
void module_camera_init (void);
215 816 markom
void module_load_init (void);
216 809 simons
void tick_init(void);
217
 
218
/* List of all initializations */
219
void mon_init (void)
220
{
221 816 markom
  /* Set defaults */
222
  global.erase_method = 2; /* as needed */
223 817 simons
  global.src_addr = src_addr;
224 816 markom
  global.dst_addr = FLASH_BASE_ADDR;
225 817 simons
  global.ip = BOARD_DEF_IP;
226 816 markom
 
227
  /* Init modules */
228 809 simons
  module_cpu_init ();
229
  module_memory_init ();
230
  module_eth_init ();
231 816 markom
  module_dhry_init ();
232 809 simons
  module_camera_init ();
233 816 markom
  module_load_init ();
234 817 simons
 
235 809 simons
//  tick_init();
236
}
237
 
238
/* Main shell loop */
239
int main(int argc, char **argv)
240
{
241 820 markom
  extern unsigned long calc_mycrc32 ();
242 809 simons
  /* Initialize controller */
243
  change_console_type (CT_UART);
244 820 markom
  printf ("Self check... ");
245
  if (calc_mycrc32 ()) printf ("FAILED!!!\n");
246
  else printf ("OK\n");
247 809 simons
  mon_init ();
248
 
249
  if (HELP_ENABLED) register_command ("help", "", "shows this help", help_cmd);
250
 
251
#ifdef XESS
252
  printf ("\nORP-XSV Monitor (type 'help' for help)\n");
253
#else
254
  printf ("\nBender Monitor (type 'help' for help)\n");
255
#endif
256
 
257
  while(1) mon_command();
258
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.