freemyipod r25 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r24‎ | r25 | r26 >
Date:15:27, 5 August 2010
Author:theseven
Status:new
Tags:
Comment:
Add USB console
Modified paths:
  • /embios/trunk/console.c (modified) (history)
  • /embios/trunk/console.h (modified) (history)
  • /embios/trunk/lcdconsole.c (modified) (history)
  • /embios/trunk/lcdconsole.h (modified) (history)
  • /embios/trunk/strlen.c (modified) (history)
  • /embios/trunk/usb/usb.c (modified) (history)

Diff [purge]

Index: embios/trunk/console.c
@@ -24,6 +24,7 @@
2525 #include "global.h"
2626 #include "console.h"
2727 #include "lcdconsole.h"
 28+#include "usb/dbgconsole.h"
2829 #include "format.h"
2930 #include "thread.h"
3031 #include <stdio.h>
@@ -51,6 +52,7 @@
5253 void cputc_internal(unsigned int consoles, char string)
5354 {
5455 if (consoles & 1) lcdconsole_putc(string, 0, -1);
 56+ if (consoles & 2) dbgconsole_putc(string);
5557 }
5658
5759 static int cprfunc(void* ptr, unsigned char letter)
@@ -103,9 +105,18 @@
104106 {
105107 mutex_lock(&console_mutex, TIMEOUT_BLOCK);
106108 if (consoles & 1) lcdconsole_puts(string, 0, -1);
 109+ if (consoles & 2) dbgconsole_puts(string);
107110 mutex_unlock(&console_mutex);
108111 }
109112
 113+void cwrite(unsigned int consoles, const char* string, size_t length)
 114+{
 115+ mutex_lock(&console_mutex, TIMEOUT_BLOCK);
 116+ if (consoles & 1) lcdconsole_write(string, length, 0, -1);
 117+ if (consoles & 2) dbgconsole_write(string, length);
 118+ mutex_unlock(&console_mutex);
 119+}
 120+
110121 void cflush(unsigned int consoles)
111122 {
112123 mutex_lock(&console_mutex, TIMEOUT_BLOCK);
Index: embios/trunk/console.h
@@ -32,6 +32,7 @@
3333 void console_init() INITCODE_ATTR;
3434 void cputc(unsigned int consoles, char string) ICODE_ATTR;
3535 void cputs(unsigned int consoles, const char* string) ICODE_ATTR;
 36+void cwrite(unsigned int consoles, const char* string, size_t length) ICODE_ATTR;
3637 int cprintf(unsigned int consoles, const char* fmt, ...) ICODE_ATTR;
3738 int cvprintf(unsigned int consoles, const char* fmt, va_list ap) ICODE_ATTR;
3839 void cflush(unsigned int consoles) ICODE_ATTR;
Index: embios/trunk/strlen.c
@@ -22,13 +22,12 @@
2323
2424
2525 #include "global.h"
 26+#include "strlen.h"
2627
2728
2829 size_t strlen(const char *str)
2930 {
30 - const char *start = str;
31 -
32 - while (*str) str++;
33 -
34 - return str - start;
 31+ const char *start = str;
 32+ while (*str) str++;
 33+ return str - start;
3534 }
Index: embios/trunk/usb/usb.c
@@ -30,6 +30,8 @@
3131 #include "console.h"
3232 #include "util.h"
3333 #include "i2c.h"
 34+#include "strlen.h"
 35+#include "contextswitch.h"
3436
3537
3638 static uint8_t ctrlresp[2] CACHEALIGN_ATTR;
@@ -54,8 +56,19 @@
5557 static int dbgi2cslave IBSS_ATTR;
5658 static int dbgi2caddr IBSS_ATTR;
5759 static int dbgi2clen IBSS_ATTR;
 60+static char dbgconsendbuf[4096];
 61+static char dbgconrecvbuf[1024];
 62+static int dbgconsendreadidx IBSS_ATTR;
 63+static int dbgconsendwriteidx IBSS_ATTR;
 64+static int dbgconrecvreadidx IBSS_ATTR;
 65+static int dbgconrecvwriteidx IBSS_ATTR;
 66+static struct wakeup dbgconsendwakeup IBSS_ATTR;
 67+static struct wakeup dbgconrecvwakeup IBSS_ATTR;
 68+static bool dbgconsoleattached IBSS_ATTR;
5869
 70+static const char dbgconoverflowstr[] = "\n\n[overflowed]\n\n";
5971
 72+
6073 static struct usb_device_descriptor CACHEALIGN_ATTR device_descriptor =
6174 {
6275 .bLength = sizeof(struct usb_device_descriptor),
@@ -295,7 +308,7 @@
296309 dbgsendbuf[0] = 1;
297310 dbgsendbuf[1] = 0x01010000;
298311 dbgsendbuf[2] = PLATFORM_ID;
299 - dbgsendbuf[3] = usb_drv_port_speed() ? 0x02000200 : 0x00400040;
 312+ dbgsendbuf[3] = 0x02000200;
300313 size = 16;
301314 break;
302315 case 2: // RESET
@@ -339,6 +352,34 @@
340353 dbgi2clen = dbgrecvbuf[1] >> 24;
341354 memcpy(dbgasyncsendbuf, &dbgsendbuf[4], dbgi2clen);
342355 break;
 356+ case 10: // READ CONSOLE
 357+ dbgconsoleattached = true;
 358+ wakeup_signal(&dbgconsendwakeup);
 359+ int bytes = dbgconsendwriteidx - dbgconsendreadidx;
 360+ if (bytes >= sizeof(dbgconsendbuf)) bytes -= sizeof(dbgconsendbuf);
 361+ if (bytes)
 362+ {
 363+ if (bytes < 0) bytes += sizeof(dbgconsendbuf);
 364+ if (bytes > dbgrecvbuf[1]) bytes = dbgrecvbuf[1];
 365+ int readbytes = bytes;
 366+ char* outptr = (char*)&dbgsendbuf[4];
 367+ if (dbgconsendreadidx + bytes >= sizeof(dbgconsendbuf))
 368+ {
 369+ readbytes = sizeof(dbgconsendbuf) - dbgconsendreadidx;
 370+ memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
 371+ dbgconsendreadidx = 0;
 372+ outptr = &outptr[readbytes];
 373+ readbytes = bytes - readbytes;
 374+ }
 375+ if (readbytes) memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
 376+ dbgconsendreadidx += readbytes;
 377+ }
 378+ dbgsendbuf[0] = 1;
 379+ dbgsendbuf[1] = bytes;
 380+ dbgsendbuf[2] = sizeof(dbgconsendbuf);
 381+ dbgsendbuf[3] = dbgconsendwriteidx - dbgconsendreadidx;
 382+ size = 16 + dbgrecvbuf[1];
 383+ break;
343384 default:
344385 dbgsendbuf[0] = 2;
345386 size = 16;
@@ -364,6 +405,7 @@
365406 void dbgthread(void)
366407 {
367408 int i;
 409+ int t;
368410 while (1)
369411 {
370412 wakeup_wait(&dbgwakeup, TIMEOUT_BLOCK);
@@ -402,6 +444,84 @@
403445 {
404446 dbgaction = DBGACTION_IDLE;
405447 wakeup_init(&dbgwakeup);
 448+ dbgconsendreadidx = 0;
 449+ dbgconsendwriteidx = 0;
 450+ dbgconrecvreadidx = 0;
 451+ dbgconrecvwriteidx = 0;
 452+ wakeup_init(&dbgconsendwakeup);
 453+ wakeup_init(&dbgconrecvwakeup);
 454+ dbgconsoleattached = false;;
406455 thread_create("Debugger", dbgthread, dbgstack, sizeof(dbgstack), 255, SYSTEM_THREAD, true);
407456 usb_drv_init();
408457 }
 458+
 459+int dbgconsole_getfree() ICODE_ATTR;
 460+int dbgconsole_getfree()
 461+{
 462+ int free = dbgconsendreadidx - dbgconsendwriteidx - 1;
 463+ if (free < 0) free += sizeof(dbgconsendbuf);
 464+ return free;
 465+}
 466+
 467+int dbgconsole_makespace(int length) ICODE_ATTR;
 468+int dbgconsole_makespace(int length)
 469+{
 470+ int free = dbgconsole_getfree();
 471+ while (!free && dbgconsoleattached)
 472+ {
 473+ if (wakeup_wait(&dbgconsendwakeup, 2000000) == THREAD_TIMEOUT)
 474+ dbgconsoleattached = false;
 475+ free = dbgconsole_getfree();
 476+ }
 477+ if (free) return free > length ? length : free;
 478+ if (length > sizeof(dbgconsendbuf) - 17) length = sizeof(dbgconsendbuf) - 17;
 479+ uint32_t mode = enter_critical_section();
 480+ dbgconsendreadidx += length;
 481+ if (dbgconsendreadidx >= sizeof(dbgconsendbuf))
 482+ dbgconsendreadidx -= sizeof(dbgconsendbuf);
 483+ int offset = 0;
 484+ int idx = dbgconsendreadidx;
 485+ if (idx + 16 >= sizeof(dbgconsendbuf))
 486+ {
 487+ offset = sizeof(dbgconsendbuf) - dbgconsendreadidx;
 488+ memcpy(&dbgconsendbuf[dbgconsendreadidx], dbgconoverflowstr, offset);
 489+ idx = 0;
 490+ }
 491+ if (offset != 16) memcpy(&dbgconsendbuf[idx], &dbgconoverflowstr[offset], 16 - offset);
 492+ leave_critical_section(mode);
 493+ return length;
 494+}
 495+
 496+void dbgconsole_putc(char string)
 497+{
 498+ dbgconsole_makespace(1);
 499+ dbgconsendbuf[dbgconsendwriteidx++] = string;
 500+ if (dbgconsendwriteidx >= sizeof(dbgconsendbuf))
 501+ dbgconsendwriteidx -= sizeof(dbgconsendbuf);
 502+}
 503+
 504+void dbgconsole_write(const char* string, size_t length)
 505+{
 506+ while (length)
 507+ {
 508+ int space = dbgconsole_makespace(length);
 509+ if (dbgconsendwriteidx + space >= sizeof(dbgconsendbuf))
 510+ {
 511+ int bytes = sizeof(dbgconsendbuf) - dbgconsendwriteidx;
 512+ memcpy(&dbgconsendbuf[dbgconsendwriteidx], string, bytes);
 513+ dbgconsendwriteidx = 0;
 514+ string = &string[bytes];
 515+ space -= bytes;
 516+ length -= bytes;
 517+ }
 518+ if (space) memcpy(&dbgconsendbuf[dbgconsendwriteidx], string, space);
 519+ dbgconsendwriteidx += space;
 520+ string = &string[space];
 521+ length -= space;
 522+ }
 523+}
 524+
 525+void dbgconsole_puts(const char* string)
 526+{
 527+ dbgconsole_write(string, strlen(string));
 528+}
Index: embios/trunk/lcdconsole.c
@@ -89,6 +89,11 @@
9090 while (*string) lcdconsole_putc_noblit(*string++, fgcolor, bgcolor);
9191 }
9292
 93+void lcdconsole_write_noblit(const char* string, size_t length, int fgcolor, int bgcolor)
 94+{
 95+ while (length--) lcdconsole_putc_noblit(*string++, fgcolor, bgcolor);
 96+}
 97+
9398 void lcdconsole_update()
9499 {
95100 uint32_t mode = enter_critical_section();
@@ -114,6 +119,12 @@
115120 lcdconsole_update();
116121 }
117122
 123+void lcdconsole_write(const char* string, size_t length, int fgcolor, int bgcolor)
 124+{
 125+ while (length--) lcdconsole_putc_noblit(*string++, fgcolor, bgcolor);
 126+ lcdconsole_update();
 127+}
 128+
118129 void lcdconsole_callback()
119130 {
120131 if (lcdconsole_needs_update)
Index: embios/trunk/lcdconsole.h
@@ -39,8 +39,10 @@
4040 void lcdconsole_init();
4141 void lcdconsole_putc(char string, int fgcolor, int bgcolor) ICODE_ATTR;
4242 void lcdconsole_puts(const char* string, int fgcolor, int bgcolor) ICODE_ATTR;
 43+void lcdconsole_write(const char* string, size_t length, int fgcolor, int bgcolor) ICODE_ATTR;
4344 void lcdconsole_putc_noblit(char string, int fgcolor, int bgcolor) ICODE_ATTR;
4445 void lcdconsole_puts_noblit(const char* string, int fgcolor, int bgcolor) ICODE_ATTR;
 46+void lcdconsole_write_noblit(const char* string, size_t length, int fgcolor, int bgcolor) ICODE_ATTR;
4547 void lcdconsole_update() ICODE_ATTR;
4648 void lcdconsole_callback() ICODE_ATTR;
4749