lin
2025-04-29 67d159bd47b6cf87700657ef6308f0f69fb8a723
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
*********************************************************************************************************
*                                                AR100 System
*                                     AR100 Software System Develop Kits
*                                               Debugger Module
*
*                                    (c) Copyright 2012-2016, Sunny China
*                                             All Rights Reserved
*
* File    : debugger.c
* By      : Sunny
* Version : v1.0
* Date    : 2012-5-3
* Descript: debugger module.
* Update  : date                auther      ver     notes
*           2012-5-3 17:16:47   Sunny       1.0     Create this file.
*********************************************************************************************************
*/
 
#include "debugger_i.h"
 
u32 debug_level = 3;
volatile unsigned int print_timeflg = 1;
#define DEBUG_BUFFER_SIZE (128) /* debug buffer size */
/*
*********************************************************************************************************
*                                           INITIALIZE DEBUGGER
*
* Description:  initialize debugger.
*
* Arguments  :  none.
*
* Returns    :  OK if initialize debugger succeeded, others if failed.
*********************************************************************************************************
*/
s32 debugger_init(void)
{
   /* initialize serial module */
   //uart_init();
#ifdef CFG_SHELL_USED
   extern s32 getcmd(void *parg);
   install_isr(INTC_R_UART_IRQ, getcmd, NULL);
   interrupt_enable(INTC_R_UART_IRQ);
#endif
 
   return OK;
}
 
/*
*********************************************************************************************************
*                                           EXIT DEBUGGER
*
* Description:  exit debugger.
*
* Arguments  :  none.
*
* Returns    :  OK if exit debugger succeeded, others if failed.
*********************************************************************************************************
*/
s32 debugger_exit(void)
{
   return OK;
}
 
/*
*********************************************************************************************************
*                                           PUT A CHARSET
*
* Description:  put out a charset.
*
* Arguments  :  ch  : the charset which we want to put out.
*
* Returns    :  OK if put out charset succeeded, others if failed.
*********************************************************************************************************
*/
s32 debugger_putc(char ch)
{
   uart_putc(ch);
 
   return OK;
}
 
/*
*********************************************************************************************************
*                                           GET A CHARSET
*
* Description:  get a charset.
*
* Arguments  :  none.
*
* Returns    :  the charset we read from uart.
*********************************************************************************************************
*/
u32 debugger_get(char *buf)
{
   return uart_get(buf);
}
 
/*
*********************************************************************************************************
*                                           PUT A STRING
*
* Description:  put out a string.
*
* Arguments  :  string  : the string which we want to put out.
*
* Returns    :  OK if put out string succeeded, others if failed.
*********************************************************************************************************
*/
s32 debugger_puts(char *string)
{
   uart_puts(string);
 
   return OK;
}
 
/*
*********************************************************************************************************
*                                           FORMATTED PRINTF
*
* Description:  print out a formatted string, similar to ANSI-C function printf().
*               This function can support and only support the following conversion specifiers:
*               %d  signed decimal integer.
*               %u  unsigned decimal integer.
*               %x  unsigned hexadecimal integer, using hex digits 0x.
*               %c  single character.
*               %s  character string.
*
* Arguments  :  format  : format control.
*               ...     : arguments.
*
* Returns    :  the number of characters printed out.
*
* Note       :  the usage refer to ANSI-C function printf().
*********************************************************************************************************
*/
char debugger_buffer[DEBUG_BUFFER_SIZE];
s32 debugger_printf(u32 level, const char *format, ...)
{
   va_list  args;
   char     string[16];    /* align by cpu word */
   char    *pdest;
   char    *psrc;
   s32      align;
   s32      len = 0;
   s32      cpsr;
 
   if (level & (0xf0 >> (debug_level + 1))) {
       cpsr = cpu_disable_int();
       /* dump current timestemp */
       if (print_timeflg)
       print_current_time();
       pdest = debugger_buffer;
       va_start(args, format);
       while (*format) {
           if (*format == '%') {
               ++format;
               if (('0' < (*format)) && ((*format) <= '9')) {
                   /* we just suport wide from 1 to 9. */
                   align = *format - '0';
                   ++format;
               } else {
                   align = 0;
               }
 
               switch (*format) {
               case 'd':
                   {
                       /* int */
                       itoa(va_arg(args, int), string, 10);
                       len = strlen(string);
                       len += print_align(string, len, align);
                       strcpy(pdest, string);
                       pdest += len;
                       break;
                   }
               case 'x':
               case 'p':
                   {
                       /* hex */
                       utoa(va_arg(args, unsigned long), string, 16);
                       len = strlen(string);
                       len += print_align(string, len, align);
                       strcpy(pdest, string);
                       pdest += len;
                       break;
                   }
               case 'u':
                   {
                       /* unsigned int */
                       utoa(va_arg(args, unsigned long), string, 10);
                       len = strlen(string);
                       len += print_align(string, len, align);
                       strcpy(pdest, string);
                       pdest += len;
                       break;
                   }
               case 'c':
                   {
                       /* charset, aligned by cpu word */
                       *pdest = (char)va_arg(args, int);
                       break;
                   }
               case 's':
                   {
                       /* string */
                       psrc = va_arg(args, char *);
                       strcpy(pdest, psrc);
                       pdest += strlen(psrc);
                       break;
                   }
               default:
                   {
                       /* no-conversion */
                       *pdest++ = '%';
                       *pdest++ = *format;
                   }
               }
           } else {
               *pdest++ = *format;
           }
           /* parse next token */
           ++format;
       }
       va_end(args);
 
       if (*(pdest - 1) == '\n')
           print_timeflg = 1;
       else
           print_timeflg = 0;
       /* must end with '\0' */
       *pdest = '\0';
       pdest++;
       debugger_puts(debugger_buffer);
       cpu_enable_int(cpsr);
 
       return (pdest - debugger_buffer);
   }
   return OK;
}
 
static s32 print_align(char *string, s32 len, s32 align)
{
   /*
    * fill with space ' ' when align request,
    * the max align length is 16 byte.
    */
   char fill_ch[] = "                ";
   if (len < align) {
       /* fill at right */
       strncat(string, fill_ch, align - len);
       return align - len;
   }
   /* not fill anything */
   return 0;
}
 
/*
*********************************************************************************************************
*                                           print current time
*
* Description:  print current time.
*
* Arguments  :  none.
*
* Returns    :  OK if print current time succeeded, others if failed.
*********************************************************************************************************
*/
static s32 print_current_time(void)
{
   char time[12];
   u32  millisec;
   u32  sec;
   u32  div;
   u32  i;
 
   /* convert current ticks to millisecond. */
   millisec = current_time_tick() * (1000 / TICK_PER_SEC);
 
   /*
    * time print format : [secs.ms] s,
    * example           : [0001.00] s.
    */
   time[0]  = '[';
   time[5]  = '.';
   time[8]  = ']';
   time[9]  = ' ';
   time[10] = '\0';
 
   /* second */
   sec = millisec / 1000;
   sec = sec % 10000;
   div = 1000;
   for (i = 1; i <= 4; i++) {
       time[i] = ((u8)(sec / div)) + '0';
       sec = sec % div;
       div = div / 10;
   }
   /* millisecond */
   millisec = millisec % 1000;
   div = 100;
   for (i = 6; i <= 7; i++) {
       time[i] = ((u8)(millisec / div)) + '0';
       millisec = millisec % div;
       div = div / 10;
   }
   /* dump time buffer */
   debugger_puts(time);
 
   return OK;
}
 
/*
*********************************************************************************************************
*                                           SET DEBUG LEVEL
*
* Description:  set debug level.
*
* Arguments  :  para level.
*
* Returns    :  OK if set the debug level succeeded.
*********************************************************************************************************
*/
s32 set_debug_level(u32 level)
{
   LOG("debug_mask from %d to %d\n", debug_level, level);
   debug_level  = level;
 
   return OK;
}