Corrected name spelling.
[kugel-rb/myfork.git] / apps / debug_menu.c
blob9be6667808b36b94b303a89f5be3e82a7f33ba1d
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Heikki Hannikainen
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "config.h"
21 #ifndef SIMULATOR
22 #include <stdio.h>
23 #include <stdbool.h>
24 #include "lcd.h"
25 #include "menu.h"
26 #include "debug_menu.h"
27 #include "kernel.h"
28 #include "sprintf.h"
29 #include "button.h"
30 #include "adc.h"
31 #include "mas.h"
32 #include "power.h"
33 #include "rtc.h"
34 #include "debug.h"
35 #include "thread.h"
36 #include "powermgmt.h"
37 #include "system.h"
39 /*---------------------------------------------------*/
40 /* SPECIAL DEBUG STUFF */
41 /*---------------------------------------------------*/
42 extern int ata_device;
43 extern int ata_io_address;
44 extern int num_threads;
45 extern char *thread_name[];
47 #ifdef HAVE_LCD_BITMAP
48 /* Test code!!! */
49 Menu dbg_os(void)
51 char buf[32];
52 int button;
53 int i;
54 int usage;
56 #ifdef HAVE_LCD_BITMAP
57 lcd_setmargins(0, 0);
58 #endif
59 lcd_clear_display();
61 while(1)
63 lcd_puts(0, 0, "Stack usage:");
64 for(i = 0; i < num_threads;i++)
66 usage = thread_stack_usage(i);
67 snprintf(buf, 32, "%s: %d%%", thread_name[i], usage);
68 lcd_puts(0, 1+i, buf);
71 lcd_update();
72 sleep(HZ/10);
74 button = button_get(false);
76 switch(button)
78 case BUTTON_OFF:
79 case BUTTON_LEFT:
80 return MENU_OK;
83 return MENU_OK;
85 #else
86 Menu dbg_os(void)
88 char buf[32];
89 int button;
90 int usage;
91 int currval = 0;
93 #ifdef HAVE_LCD_BITMAP
94 lcd_setmargins(0, 0);
95 #endif
96 lcd_clear_display();
98 while(1)
100 lcd_puts(0, 0, "Stack usage");
102 usage = thread_stack_usage(currval);
103 snprintf(buf, 32, "%d: %d%% ", currval, usage);
104 lcd_puts(0, 1, buf);
106 sleep(HZ/10);
108 button = button_get(false);
110 switch(button)
112 case BUTTON_STOP:
113 return MENU_OK;
115 case BUTTON_LEFT:
116 currval--;
117 if(currval < 0)
118 currval = num_threads-1;
119 break;
121 case BUTTON_RIGHT:
122 currval++;
123 if(currval > num_threads-1)
124 currval = 0;
125 break;
128 return MENU_OK;
130 #endif
132 #ifdef HAVE_LCD_BITMAP
133 /* Test code!!! */
134 Menu dbg_ports(void)
136 unsigned short porta;
137 unsigned short portb;
138 unsigned char portc;
139 char buf[32];
140 int button;
141 int battery_voltage;
142 int batt_int, batt_frac;
144 #ifdef HAVE_LCD_BITMAP
145 lcd_setmargins(0, 0);
146 #endif
147 lcd_clear_display();
149 while(1)
151 porta = PADR;
152 portb = PBDR;
153 portc = PCDR;
155 snprintf(buf, 32, "PADR: %04x", porta);
156 lcd_puts(0, 0, buf);
157 snprintf(buf, 32, "PBDR: %04x", portb);
158 lcd_puts(0, 1, buf);
160 snprintf(buf, 32, "AN0: %03x AN4: %03x", adc_read(0), adc_read(4));
161 lcd_puts(0, 2, buf);
162 snprintf(buf, 32, "AN1: %03x AN5: %03x", adc_read(1), adc_read(5));
163 lcd_puts(0, 3, buf);
164 snprintf(buf, 32, "AN2: %03x AN6: %03x", adc_read(2), adc_read(6));
165 lcd_puts(0, 4, buf);
166 snprintf(buf, 32, "AN3: %03x AN7: %03x", adc_read(3), adc_read(7));
167 lcd_puts(0, 5, buf);
169 battery_voltage = (adc_read(ADC_UNREG_POWER) * BATTERY_SCALE_FACTOR) / 10000;
170 batt_int = battery_voltage / 100;
171 batt_frac = battery_voltage % 100;
173 snprintf(buf, 32, "Batt: %d.%02dV %d%% ", batt_int, batt_frac,
174 battery_level());
175 lcd_puts(0, 6, buf);
177 snprintf(buf, 32, "ATA: %s, 0x%x",
178 ata_device?"slave":"master", ata_io_address);
179 lcd_puts(0, 7, buf);
181 lcd_update();
182 sleep(HZ/10);
184 button = button_get(false);
186 switch(button)
188 case BUTTON_OFF:
189 return MENU_OK;
192 return MENU_OK;
194 #else
195 Menu dbg_ports(void)
197 unsigned short porta;
198 unsigned short portb;
199 unsigned char portc;
200 char buf[32];
201 int button;
202 int battery_voltage;
203 int batt_int, batt_frac;
204 int currval = 0;
206 #ifdef HAVE_LCD_BITMAP
207 lcd_setmargins(0, 0);
208 #endif
209 lcd_clear_display();
211 while(1)
213 porta = PADR;
214 portb = PBDR;
215 portc = PCDR;
217 switch(currval)
219 case 0:
220 snprintf(buf, 32, "PADR: %04x ", porta);
221 break;
222 case 1:
223 snprintf(buf, 32, "PBDR: %04x ", portb);
224 break;
225 case 2:
226 snprintf(buf, 32, "AN0: %03x ", adc_read(0));
227 break;
228 case 3:
229 snprintf(buf, 32, "AN1: %03x ", adc_read(1));
230 break;
231 case 4:
232 snprintf(buf, 32, "AN2: %03x ", adc_read(2));
233 break;
234 case 5:
235 snprintf(buf, 32, "AN3: %03x ", adc_read(3));
236 break;
237 case 6:
238 snprintf(buf, 32, "AN4: %03x ", adc_read(4));
239 break;
240 case 7:
241 snprintf(buf, 32, "AN5: %03x ", adc_read(5));
242 break;
243 case 8:
244 snprintf(buf, 32, "AN6: %03x ", adc_read(6));
245 break;
246 case 9:
247 snprintf(buf, 32, "AN7: %03x ", adc_read(7));
248 break;
249 case 10:
250 snprintf(buf, 32, "%s, 0x%x ",
251 ata_device?"slv":"mst", ata_io_address);
252 break;
254 lcd_puts(0, 0, buf);
256 battery_voltage = (adc_read(ADC_UNREG_POWER) *
257 BATTERY_SCALE_FACTOR) / 10000;
258 batt_int = battery_voltage / 100;
259 batt_frac = battery_voltage % 100;
261 snprintf(buf, 32, "Batt: %d.%02dV", batt_int, batt_frac);
262 lcd_puts(0, 1, buf);
264 sleep(HZ/5);
266 button = button_get(false);
268 switch(button)
270 case BUTTON_STOP:
271 return MENU_OK;
273 case BUTTON_LEFT:
274 currval--;
275 if(currval < 0)
276 currval = 10;
277 break;
279 case BUTTON_RIGHT:
280 currval++;
281 if(currval > 10)
282 currval = 0;
283 break;
286 return MENU_OK;
288 #endif
290 #ifdef HAVE_RTC
291 /* Read RTC RAM contents and display them */
292 Menu dbg_rtc(void)
294 char buf[32];
295 unsigned char addr = 0, r, c;
296 int i;
297 int button;
299 #ifdef HAVE_LCD_BITMAP
300 lcd_setmargins(0, 0);
301 #endif
302 lcd_clear_display();
303 lcd_puts(0, 0, "RTC read:");
305 while(1)
307 for (r = 0; r < 4; r++) {
308 snprintf(buf, 10, "0x%02x: ", addr + r*4);
309 for (c = 0; c <= 3; c++) {
310 i = rtc_read(addr + r*4 + c);
311 snprintf(buf + 6 + c*2, 3, "%02x", i);
313 lcd_puts(1, r+1, buf);
316 lcd_update();
317 sleep(HZ/2);
319 button = button_get(false);
321 switch(button)
323 case BUTTON_DOWN:
324 if (addr < 63-16) { addr += 16; }
325 break;
326 case BUTTON_UP:
327 if (addr) { addr -= 16; }
328 break;
329 case BUTTON_F2:
330 /* clear the user RAM space */
331 for (c = 0; c <= 43; c++)
332 rtc_write(0x14 + c, 0);
333 break;
334 case BUTTON_OFF:
335 case BUTTON_LEFT:
336 return MENU_OK;
339 return MENU_OK;
341 #else
342 Menu dbg_rtc(void)
344 return MENU_OK;
346 #endif
348 #ifdef HAVE_LCD_CHARCELLS
349 #define NUMROWS 1
350 #else
351 #define NUMROWS 4
352 #endif
353 /* Read MAS registers and display them */
354 Menu dbg_mas(void)
356 char buf[32];
357 unsigned int addr = 0, r, i;
359 #ifdef HAVE_LCD_BITMAP
360 lcd_setmargins(0, 0);
361 #endif
362 lcd_clear_display();
363 lcd_puts(0, 0, "MAS register read:");
365 while(1)
367 for (r = 0; r < NUMROWS; r++) {
368 i = mas_readreg(addr + r);
369 snprintf(buf, 30, "%02x %08x", addr + r, i);
370 lcd_puts(0, r+1, buf);
373 lcd_update();
374 sleep(HZ/16);
376 switch(button_get(false))
378 #ifdef HAVE_RECORDER_KEYPAD
379 case BUTTON_DOWN:
380 #else
381 case BUTTON_RIGHT:
382 #endif
383 addr += NUMROWS;
384 break;
385 #ifdef HAVE_RECORDER_KEYPAD
386 case BUTTON_UP:
387 #else
388 case BUTTON_LEFT:
389 #endif
390 if(addr)
391 addr -= NUMROWS;
392 break;
393 #ifdef HAVE_RECORDER_KEYPAD
394 case BUTTON_LEFT:
395 #else
396 case BUTTON_DOWN:
397 #endif
398 return MENU_OK;
401 return MENU_OK;
404 #ifdef HAVE_MAS3587F
405 Menu dbg_mas_codec(void)
407 char buf[32];
408 unsigned int addr = 0, r, i;
410 #ifdef HAVE_LCD_BITMAP
411 lcd_setmargins(0, 0);
412 #endif
413 lcd_clear_display();
414 lcd_puts(0, 0, "MAS codec reg read:");
416 while(1)
418 for (r = 0; r < 4; r++) {
419 i = mas_codec_readreg(addr + r);
420 snprintf(buf, 30, "0x%02x: %08x", addr + r, i);
421 lcd_puts(1, r+1, buf);
424 lcd_update();
425 sleep(HZ/16);
427 switch(button_get(false))
429 case BUTTON_DOWN:
430 addr += 4;
431 break;
432 case BUTTON_UP:
433 if (addr) { addr -= 4; }
434 break;
435 case BUTTON_LEFT:
436 return MENU_OK;
439 return MENU_OK;
441 #endif
443 #ifdef HAVE_LCD_BITMAP
445 * view_battery() shows a automatically scaled graph of the battery voltage
446 * over time. Usable for estimating battery life / charging rate.
447 * The power_history array is updated in power_thread of powermgmt.c.
450 #define BAT_FIRST_VAL MAX(POWER_HISTORY_LEN - LCD_WIDTH - 1, 0)
451 #define BAT_YSPACE (LCD_HEIGHT - 20)
453 Menu view_battery(void)
455 int view = 0;
456 int i, x, y;
457 int maxv, minv;
458 char buf[32];
460 #ifdef HAVE_LCD_BITMAP
461 lcd_setmargins(0, 0);
462 #endif
463 while(1)
465 switch (view) {
466 case 0: /* voltage history graph */
467 /* Find maximum and minimum voltage for scaling */
468 maxv = minv = 0;
469 for (i = BAT_FIRST_VAL; i < POWER_HISTORY_LEN; i++) {
470 if (power_history[i] > maxv)
471 maxv = power_history[i];
472 if ((minv == 0) || ((power_history[i]) &&
473 (power_history[i] < minv)) )
475 minv = power_history[i];
479 if (minv < 1)
480 minv = 1;
481 if (maxv < 2)
482 maxv = 2;
484 lcd_clear_display();
485 lcd_puts(0, 0, "Battery voltage:");
486 snprintf(buf, 30, "scale %d.%02d-%d.%02d V",
487 minv / 100, minv % 100, maxv / 100, maxv % 100);
488 lcd_puts(0, 1, buf);
490 x = 0;
491 for (i = BAT_FIRST_VAL+1; i < POWER_HISTORY_LEN; i++) {
492 y = (power_history[i] - minv) * BAT_YSPACE / (maxv - minv);
493 lcd_clearline(x, LCD_HEIGHT-1, x, 20);
494 lcd_drawline(x, LCD_HEIGHT-1, x,
495 MIN(MAX(LCD_HEIGHT-1 - y, 20), LCD_HEIGHT-1));
496 x++;
499 break;
501 case 1: /* status: */
502 lcd_clear_display();
503 lcd_puts(0, 0, "Power status:");
505 y = (adc_read(ADC_UNREG_POWER) * BATTERY_SCALE_FACTOR) / 10000;
506 snprintf(buf, 30, "Battery: %d.%02d V", y / 100, y % 100);
507 lcd_puts(0, 1, buf);
508 y = (adc_read(ADC_EXT_POWER) * EXT_SCALE_FACTOR) / 10000;
509 snprintf(buf, 30, "External: %d.%02d V", y / 100, y % 100);
510 lcd_puts(0, 2, buf);
511 snprintf(buf, 30, "Charger: %s",
512 charger_inserted() ? "present" : "absent");
513 lcd_puts(0, 3, buf);
514 #ifdef HAVE_CHARGE_CTRL
515 snprintf(buf, 30, "Charging: %s",
516 charger_enabled ? "yes" : "no");
517 lcd_puts(0, 4, buf);
518 #endif
519 y = ( power_history[POWER_HISTORY_LEN-1] * 100
520 + power_history[POWER_HISTORY_LEN-2] * 100
521 - power_history[POWER_HISTORY_LEN-1-CHARGE_END_NEGD+1] * 100
522 - power_history[POWER_HISTORY_LEN-1-CHARGE_END_NEGD] * 100 )
523 / CHARGE_END_NEGD / 2;
525 snprintf(buf, 30, "short delta: %d", y);
526 lcd_puts(0, 5, buf);
528 y = ( power_history[POWER_HISTORY_LEN-1] * 100
529 + power_history[POWER_HISTORY_LEN-2] * 100
530 - power_history[POWER_HISTORY_LEN-1-CHARGE_END_ZEROD+1] * 100
531 - power_history[POWER_HISTORY_LEN-1-CHARGE_END_ZEROD] * 100 )
532 / CHARGE_END_ZEROD / 2;
534 snprintf(buf, 30, "long delta: %d", y);
535 lcd_puts(0, 6, buf);
537 #ifdef HAVE_CHARGE_CTRL
538 lcd_puts(0, 7, power_message);
539 #endif
540 break;
542 case 2: /* voltage deltas: */
543 lcd_clear_display();
544 lcd_puts(0, 0, "Voltage deltas:");
546 for (i = 0; i <= 6; i++) {
547 y = power_history[POWER_HISTORY_LEN-1-i] -
548 power_history[POWER_HISTORY_LEN-1-i-1];
549 snprintf(buf, 30, "-%d min: %s%d.%02d V", i,
550 (y < 0) ? "-" : "", ((y < 0) ? y * -1 : y) / 100,
551 ((y < 0) ? y * -1 : y ) % 100);
552 lcd_puts(0, i+1, buf);
554 break;
557 lcd_update();
558 sleep(HZ/2);
560 switch(button_get(false))
562 case BUTTON_UP:
563 if (view)
564 view--;
565 break;
567 case BUTTON_DOWN:
568 if (view < 2)
569 view++;
570 break;
572 case BUTTON_LEFT:
573 case BUTTON_OFF:
574 return MENU_OK;
577 return MENU_OK;
580 #endif
582 #ifdef HAVE_MAS3507D
583 Menu dbg_mas_info(void)
585 int button;
586 char buf[32];
587 int currval = 0;
588 unsigned long val;
589 unsigned long pll48, pll44, config;
590 int pll_toggle = 0;
592 #ifdef HAVE_LCD_BITMAP
593 lcd_setmargins(0, 0);
594 #endif
595 while(1)
597 switch(currval)
599 case 0:
600 mas_readmem(MAS_BANK_D1, 0xff7, &val, 1);
601 lcd_puts(0, 0, "Design Code");
602 snprintf(buf, 32, "%05x ", val);
603 break;
604 case 1:
605 lcd_puts(0, 0, "DC/DC mode ");
606 snprintf(buf, 32, "8e: %05x ", mas_readreg(0x8e) & 0xfffff);
607 break;
608 case 2:
609 lcd_puts(0, 0, "Mute/Bypass");
610 snprintf(buf, 32, "aa: %05x ", mas_readreg(0xaa) & 0xfffff);
611 break;
612 case 3:
613 lcd_puts(0, 0, "PIOData ");
614 snprintf(buf, 32, "ed: %05x ", mas_readreg(0xed) & 0xfffff);
615 break;
616 case 4:
617 lcd_puts(0, 0, "Startup Cfg");
618 snprintf(buf, 32, "e6: %05x ", mas_readreg(0xe6) & 0xfffff);
619 break;
620 case 5:
621 lcd_puts(0, 0, "KPrescale ");
622 snprintf(buf, 32, "e7: %05x ", mas_readreg(0xe7) & 0xfffff);
623 break;
624 case 6:
625 lcd_puts(0, 0, "KBass ");
626 snprintf(buf, 32, "6b: %05x ", mas_readreg(0x6b) & 0xfffff);
627 break;
628 case 7:
629 lcd_puts(0, 0, "KTreble ");
630 snprintf(buf, 32, "6f: %05x ", mas_readreg(0x6f) & 0xfffff);
631 break;
632 case 8:
633 mas_readmem(MAS_BANK_D0, 0x300, &val, 1);
634 lcd_puts(0, 0, "Frame Count");
635 snprintf(buf, 32, "0/300: %04x", val & 0xffff);
636 break;
637 case 9:
638 mas_readmem(MAS_BANK_D0, 0x301, &val, 1);
639 lcd_puts(0, 0, "Status1 ");
640 snprintf(buf, 32, "0/301: %04x", val & 0xffff);
641 break;
642 case 10:
643 mas_readmem(MAS_BANK_D0, 0x302, &val, 1);
644 lcd_puts(0, 0, "Status2 ");
645 snprintf(buf, 32, "0/302: %04x", val & 0xffff);
646 break;
647 case 11:
648 mas_readmem(MAS_BANK_D0, 0x303, &val, 1);
649 lcd_puts(0, 0, "CRC Count ");
650 snprintf(buf, 32, "0/303: %04x", val & 0xffff);
651 break;
652 case 12:
653 mas_readmem(MAS_BANK_D0, 0x36d, &val, 1);
654 lcd_puts(0, 0, "PLLOffset48");
655 snprintf(buf, 32, "0/36d %05x", val & 0xfffff);
656 break;
657 case 13:
658 mas_readmem(MAS_BANK_D0, 0x32d, &val, 1);
659 lcd_puts(0, 0, "PLLOffset48");
660 snprintf(buf, 32, "0/32d %05x", val & 0xfffff);
661 break;
662 case 14:
663 mas_readmem(MAS_BANK_D0, 0x36e, &val, 1);
664 lcd_puts(0, 0, "PLLOffset44");
665 snprintf(buf, 32, "0/36e %05x", val & 0xfffff);
666 break;
667 case 15:
668 mas_readmem(MAS_BANK_D0, 0x32e, &val, 1);
669 lcd_puts(0, 0, "PLLOffset44");
670 snprintf(buf, 32, "0/32e %05x", val & 0xfffff);
671 break;
672 case 16:
673 mas_readmem(MAS_BANK_D0, 0x36f, &val, 1);
674 lcd_puts(0, 0, "OutputConf ");
675 snprintf(buf, 32, "0/36f %05x", val & 0xfffff);
676 break;
677 case 17:
678 mas_readmem(MAS_BANK_D0, 0x32f, &val, 1);
679 lcd_puts(0, 0, "OutputConf ");
680 snprintf(buf, 32, "0/32f %05x", val & 0xfffff);
681 break;
682 case 18:
683 mas_readmem(MAS_BANK_D1, 0x7f8, &val, 1);
684 lcd_puts(0, 0, "LL Gain ");
685 snprintf(buf, 32, "1/7f8 %05x", val & 0xfffff);
686 break;
687 case 19:
688 mas_readmem(MAS_BANK_D1, 0x7f9, &val, 1);
689 lcd_puts(0, 0, "LR Gain ");
690 snprintf(buf, 32, "1/7f9 %05x", val & 0xfffff);
691 break;
692 case 20:
693 mas_readmem(MAS_BANK_D1, 0x7fa, &val, 1);
694 lcd_puts(0, 0, "RL Gain ");
695 snprintf(buf, 32, "1/7fa %05x", val & 0xfffff);
696 break;
697 case 21:
698 mas_readmem(MAS_BANK_D1, 0x7fb, &val, 1);
699 lcd_puts(0, 0, "RR Gain ");
700 snprintf(buf, 32, "1/7fb %05x", val & 0xfffff);
701 break;
702 case 22:
703 lcd_puts(0, 0, "L Trailbits");
704 snprintf(buf, 32, "c5: %05x ", mas_readreg(0xc5) & 0xfffff);
705 break;
706 case 23:
707 lcd_puts(0, 0, "R Trailbits");
708 snprintf(buf, 32, "c6: %05x ", mas_readreg(0xc6) & 0xfffff);
709 break;
711 lcd_puts(0, 1, buf);
713 button = button_get_w_tmo(HZ/5);
714 switch(button)
716 case BUTTON_STOP:
717 return MENU_OK;
719 case BUTTON_LEFT:
720 currval--;
721 if(currval < 0)
722 currval = 23;
723 break;
725 case BUTTON_RIGHT:
726 currval++;
727 if(currval > 23)
728 currval = 0;
729 break;
730 case BUTTON_PLAY:
731 pll_toggle = !pll_toggle;
732 if(pll_toggle)
734 /* 14.31818 MHz crystal */
735 pll48 = 0x5d9d0;
736 pll44 = 0xfffceceb;
737 config = 0;
739 else
741 /* 14.725 MHz crystal */
742 pll48 = 0x2d0de;
743 pll44 = 0xfffa2319;
744 config = 0;
746 mas_writemem(MAS_BANK_D0, 0x32d, &pll48, 1);
747 mas_writemem(MAS_BANK_D0, 0x32e, &pll44, 1);
748 mas_writemem(MAS_BANK_D0, 0x32f, &config, 1);
749 mas_run(0x475);
750 break;
753 return MENU_OK;
755 #endif
757 Menu debug_menu(void)
759 int m;
760 Menu result;
762 struct menu_items items[] = {
763 { "View I/O ports", dbg_ports },
764 #ifdef HAVE_LCD_BITMAP
765 #ifdef HAVE_RTC
766 { "View/clr RTC RAM", dbg_rtc },
767 #endif /* HAVE_RTC */
768 #endif /* HAVE_LCD_BITMAP */
769 { "View OS stacks", dbg_os },
770 #ifdef HAVE_MAS3507D
771 { "View MAS info", dbg_mas_info },
772 #endif
773 { "View MAS regs", dbg_mas },
774 #ifdef HAVE_MAS3587F
775 { "View MAS codec", dbg_mas_codec },
776 #endif
777 #ifdef HAVE_LCD_BITMAP
778 { "View battery", view_battery },
779 #endif
782 m=menu_init( items, sizeof items / sizeof(struct menu_items) );
783 result = menu_run(m);
784 menu_exit(m);
786 return result;
789 #endif /* SIMULATOR */