| Index: embios/trunk/console.c |
| — | — | @@ -24,6 +24,7 @@ |
| 25 | 25 | #include "global.h"
|
| 26 | 26 | #include "console.h"
|
| 27 | 27 | #include "lcdconsole.h"
|
| | 28 | +#include "usb/dbgconsole.h"
|
| 28 | 29 | #include "format.h"
|
| 29 | 30 | #include "thread.h"
|
| 30 | 31 | #include <stdio.h>
|
| — | — | @@ -51,6 +52,7 @@ |
| 52 | 53 | void cputc_internal(unsigned int consoles, char string)
|
| 53 | 54 | {
|
| 54 | 55 | if (consoles & 1) lcdconsole_putc(string, 0, -1);
|
| | 56 | + if (consoles & 2) dbgconsole_putc(string);
|
| 55 | 57 | }
|
| 56 | 58 |
|
| 57 | 59 | static int cprfunc(void* ptr, unsigned char letter)
|
| — | — | @@ -103,9 +105,18 @@ |
| 104 | 106 | {
|
| 105 | 107 | mutex_lock(&console_mutex, TIMEOUT_BLOCK);
|
| 106 | 108 | if (consoles & 1) lcdconsole_puts(string, 0, -1);
|
| | 109 | + if (consoles & 2) dbgconsole_puts(string);
|
| 107 | 110 | mutex_unlock(&console_mutex);
|
| 108 | 111 | }
|
| 109 | 112 |
|
| | 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 | +
|
| 110 | 121 | void cflush(unsigned int consoles)
|
| 111 | 122 | {
|
| 112 | 123 | mutex_lock(&console_mutex, TIMEOUT_BLOCK);
|
| Index: embios/trunk/console.h |
| — | — | @@ -32,6 +32,7 @@ |
| 33 | 33 | void console_init() INITCODE_ATTR;
|
| 34 | 34 | void cputc(unsigned int consoles, char string) ICODE_ATTR;
|
| 35 | 35 | void cputs(unsigned int consoles, const char* string) ICODE_ATTR;
|
| | 36 | +void cwrite(unsigned int consoles, const char* string, size_t length) ICODE_ATTR;
|
| 36 | 37 | int cprintf(unsigned int consoles, const char* fmt, ...) ICODE_ATTR;
|
| 37 | 38 | int cvprintf(unsigned int consoles, const char* fmt, va_list ap) ICODE_ATTR;
|
| 38 | 39 | void cflush(unsigned int consoles) ICODE_ATTR;
|
| Index: embios/trunk/strlen.c |
| — | — | @@ -22,13 +22,12 @@ |
| 23 | 23 |
|
| 24 | 24 |
|
| 25 | 25 | #include "global.h"
|
| | 26 | +#include "strlen.h"
|
| 26 | 27 |
|
| 27 | 28 |
|
| 28 | 29 | size_t strlen(const char *str)
|
| 29 | 30 | {
|
| 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;
|
| 35 | 34 | }
|
| Index: embios/trunk/usb/usb.c |
| — | — | @@ -30,6 +30,8 @@ |
| 31 | 31 | #include "console.h"
|
| 32 | 32 | #include "util.h"
|
| 33 | 33 | #include "i2c.h"
|
| | 34 | +#include "strlen.h"
|
| | 35 | +#include "contextswitch.h"
|
| 34 | 36 |
|
| 35 | 37 |
|
| 36 | 38 | static uint8_t ctrlresp[2] CACHEALIGN_ATTR;
|
| — | — | @@ -54,8 +56,19 @@ |
| 55 | 57 | static int dbgi2cslave IBSS_ATTR;
|
| 56 | 58 | static int dbgi2caddr IBSS_ATTR;
|
| 57 | 59 | 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;
|
| 58 | 69 |
|
| | 70 | +static const char dbgconoverflowstr[] = "\n\n[overflowed]\n\n";
|
| 59 | 71 |
|
| | 72 | +
|
| 60 | 73 | static struct usb_device_descriptor CACHEALIGN_ATTR device_descriptor =
|
| 61 | 74 | {
|
| 62 | 75 | .bLength = sizeof(struct usb_device_descriptor),
|
| — | — | @@ -295,7 +308,7 @@ |
| 296 | 309 | dbgsendbuf[0] = 1;
|
| 297 | 310 | dbgsendbuf[1] = 0x01010000;
|
| 298 | 311 | dbgsendbuf[2] = PLATFORM_ID;
|
| 299 | | - dbgsendbuf[3] = usb_drv_port_speed() ? 0x02000200 : 0x00400040;
|
| | 312 | + dbgsendbuf[3] = 0x02000200;
|
| 300 | 313 | size = 16;
|
| 301 | 314 | break;
|
| 302 | 315 | case 2: // RESET
|
| — | — | @@ -339,6 +352,34 @@ |
| 340 | 353 | dbgi2clen = dbgrecvbuf[1] >> 24;
|
| 341 | 354 | memcpy(dbgasyncsendbuf, &dbgsendbuf[4], dbgi2clen);
|
| 342 | 355 | 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;
|
| 343 | 384 | default:
|
| 344 | 385 | dbgsendbuf[0] = 2;
|
| 345 | 386 | size = 16;
|
| — | — | @@ -364,6 +405,7 @@ |
| 365 | 406 | void dbgthread(void)
|
| 366 | 407 | {
|
| 367 | 408 | int i;
|
| | 409 | + int t;
|
| 368 | 410 | while (1)
|
| 369 | 411 | {
|
| 370 | 412 | wakeup_wait(&dbgwakeup, TIMEOUT_BLOCK);
|
| — | — | @@ -402,6 +444,84 @@ |
| 403 | 445 | {
|
| 404 | 446 | dbgaction = DBGACTION_IDLE;
|
| 405 | 447 | 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;;
|
| 406 | 455 | thread_create("Debugger", dbgthread, dbgstack, sizeof(dbgstack), 255, SYSTEM_THREAD, true);
|
| 407 | 456 | usb_drv_init();
|
| 408 | 457 | }
|
| | 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 @@ |
| 90 | 90 | while (*string) lcdconsole_putc_noblit(*string++, fgcolor, bgcolor);
|
| 91 | 91 | }
|
| 92 | 92 |
|
| | 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 | +
|
| 93 | 98 | void lcdconsole_update()
|
| 94 | 99 | {
|
| 95 | 100 | uint32_t mode = enter_critical_section();
|
| — | — | @@ -114,6 +119,12 @@ |
| 115 | 120 | lcdconsole_update();
|
| 116 | 121 | }
|
| 117 | 122 |
|
| | 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 | +
|
| 118 | 129 | void lcdconsole_callback()
|
| 119 | 130 | {
|
| 120 | 131 | if (lcdconsole_needs_update)
|
| Index: embios/trunk/lcdconsole.h |
| — | — | @@ -39,8 +39,10 @@ |
| 40 | 40 | void lcdconsole_init();
|
| 41 | 41 | void lcdconsole_putc(char string, int fgcolor, int bgcolor) ICODE_ATTR;
|
| 42 | 42 | 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;
|
| 43 | 44 | void lcdconsole_putc_noblit(char string, int fgcolor, int bgcolor) ICODE_ATTR;
|
| 44 | 45 | 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;
|
| 45 | 47 | void lcdconsole_update() ICODE_ATTR;
|
| 46 | 48 | void lcdconsole_callback() ICODE_ATTR;
|
| 47 | 49 |
|