Subversion Repositories freemyipod

Rev

Rev 15 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 15 Rev 25
Line 28... Line 28...
28
#include "usbdrv.h"
28
#include "usbdrv.h"
29
#include "thread.h"
29
#include "thread.h"
30
#include "console.h"
30
#include "console.h"
31
#include "util.h"
31
#include "util.h"
32
#include "i2c.h"
32
#include "i2c.h"
-
 
33
#include "strlen.h"
-
 
34
#include "contextswitch.h"
33
 
35
 
34
 
36
 
35
static uint8_t ctrlresp[2] CACHEALIGN_ATTR;
37
static uint8_t ctrlresp[2] CACHEALIGN_ATTR;
36
static uint32_t dbgrecvbuf[0x80] CACHEALIGN_ATTR;
38
static uint32_t dbgrecvbuf[0x80] CACHEALIGN_ATTR;
37
static uint32_t dbgsendbuf[0x80] CACHEALIGN_ATTR;
39
static uint32_t dbgsendbuf[0x80] CACHEALIGN_ATTR;
Line 52... Line 54...
52
static enum dbgaction_t dbgaction IBSS_ATTR;
54
static enum dbgaction_t dbgaction IBSS_ATTR;
53
static int dbgi2cbus IBSS_ATTR;
55
static int dbgi2cbus IBSS_ATTR;
54
static int dbgi2cslave IBSS_ATTR;
56
static int dbgi2cslave IBSS_ATTR;
55
static int dbgi2caddr IBSS_ATTR;
57
static int dbgi2caddr IBSS_ATTR;
56
static int dbgi2clen IBSS_ATTR;
58
static int dbgi2clen IBSS_ATTR;
-
 
59
static char dbgconsendbuf[4096];
-
 
60
static char dbgconrecvbuf[1024];
-
 
61
static int dbgconsendreadidx IBSS_ATTR;
-
 
62
static int dbgconsendwriteidx IBSS_ATTR;
-
 
63
static int dbgconrecvreadidx IBSS_ATTR;
-
 
64
static int dbgconrecvwriteidx IBSS_ATTR;
-
 
65
static struct wakeup dbgconsendwakeup IBSS_ATTR;
-
 
66
static struct wakeup dbgconrecvwakeup IBSS_ATTR;
-
 
67
static bool dbgconsoleattached IBSS_ATTR;
-
 
68
 
-
 
69
static const char dbgconoverflowstr[] = "\n\n[overflowed]\n\n";
57
 
70
 
58
 
71
 
59
static struct usb_device_descriptor CACHEALIGN_ATTR device_descriptor =
72
static struct usb_device_descriptor CACHEALIGN_ATTR device_descriptor =
60
{
73
{
61
    .bLength            = sizeof(struct usb_device_descriptor),
74
    .bLength            = sizeof(struct usb_device_descriptor),
Line 293... Line 306...
293
        {
306
        {
294
        case 1:  // GET VERSION
307
        case 1:  // GET VERSION
295
            dbgsendbuf[0] = 1;
308
            dbgsendbuf[0] = 1;
296
            dbgsendbuf[1] = 0x01010000;
309
            dbgsendbuf[1] = 0x01010000;
297
            dbgsendbuf[2] = PLATFORM_ID;
310
            dbgsendbuf[2] = PLATFORM_ID;
298
            dbgsendbuf[3] = usb_drv_port_speed() ? 0x02000200 : 0x00400040;
311
            dbgsendbuf[3] = 0x02000200;
299
            size = 16;
312
            size = 16;
300
            break;
313
            break;
301
        case 2:  // RESET
314
        case 2:  // RESET
302
            reset();
315
            reset();
303
            break;
316
            break;
Line 337... Line 350...
337
            dbgi2cslave = (dbgrecvbuf[1] >> 8) & 0xff;
350
            dbgi2cslave = (dbgrecvbuf[1] >> 8) & 0xff;
338
            dbgi2caddr = (dbgrecvbuf[1] >> 16) & 0xff;
351
            dbgi2caddr = (dbgrecvbuf[1] >> 16) & 0xff;
339
            dbgi2clen = dbgrecvbuf[1] >> 24;
352
            dbgi2clen = dbgrecvbuf[1] >> 24;
340
            memcpy(dbgasyncsendbuf, &dbgsendbuf[4], dbgi2clen);
353
            memcpy(dbgasyncsendbuf, &dbgsendbuf[4], dbgi2clen);
341
            break;
354
            break;
-
 
355
        case 10:  // READ CONSOLE
-
 
356
            dbgconsoleattached = true;
-
 
357
            wakeup_signal(&dbgconsendwakeup);
-
 
358
            int bytes = dbgconsendwriteidx - dbgconsendreadidx;
-
 
359
            if (bytes >= sizeof(dbgconsendbuf)) bytes -= sizeof(dbgconsendbuf);
-
 
360
            if (bytes)
-
 
361
            {
-
 
362
                if (bytes < 0) bytes += sizeof(dbgconsendbuf);
-
 
363
                if (bytes > dbgrecvbuf[1]) bytes = dbgrecvbuf[1];
-
 
364
                int readbytes = bytes;
-
 
365
                char* outptr = (char*)&dbgsendbuf[4];
-
 
366
                if (dbgconsendreadidx + bytes >= sizeof(dbgconsendbuf))
-
 
367
                {
-
 
368
                    readbytes = sizeof(dbgconsendbuf) - dbgconsendreadidx;
-
 
369
                    memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
-
 
370
                    dbgconsendreadidx = 0;
-
 
371
                    outptr = &outptr[readbytes];
-
 
372
                    readbytes = bytes - readbytes;
-
 
373
                }
-
 
374
                if (readbytes) memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
-
 
375
                dbgconsendreadidx += readbytes;
-
 
376
            }
-
 
377
            dbgsendbuf[0] = 1;
-
 
378
            dbgsendbuf[1] = bytes;
-
 
379
            dbgsendbuf[2] = sizeof(dbgconsendbuf);
-
 
380
            dbgsendbuf[3] = dbgconsendwriteidx - dbgconsendreadidx;
-
 
381
            size = 16 + dbgrecvbuf[1];
-
 
382
            break;
342
        default:
383
        default:
343
            dbgsendbuf[0] = 2;
384
            dbgsendbuf[0] = 2;
344
            size = 16;
385
            size = 16;
345
        }
386
        }
346
        usb_setup_dbg_listener();
387
        usb_setup_dbg_listener();
Line 362... Line 403...
362
}
403
}
363
 
404
 
364
void dbgthread(void)
405
void dbgthread(void)
365
{
406
{
366
    int i;
407
    int i;
-
 
408
    int t;
367
    while (1)
409
    while (1)
368
    {
410
    {
369
        wakeup_wait(&dbgwakeup, TIMEOUT_BLOCK);
411
        wakeup_wait(&dbgwakeup, TIMEOUT_BLOCK);
370
        for (i = 0; i < MAX_THREADS; i++)
412
        for (i = 0; i < MAX_THREADS; i++)
371
            if (scheduler_threads[i].state == THREAD_DEFUNCT)
413
            if (scheduler_threads[i].state == THREAD_DEFUNCT)
Line 400... Line 442...
400
 
442
 
401
void usb_init(void)
443
void usb_init(void)
402
{
444
{
403
    dbgaction = DBGACTION_IDLE;
445
    dbgaction = DBGACTION_IDLE;
404
    wakeup_init(&dbgwakeup);
446
    wakeup_init(&dbgwakeup);
-
 
447
    dbgconsendreadidx = 0;
-
 
448
    dbgconsendwriteidx = 0;
-
 
449
    dbgconrecvreadidx = 0;
-
 
450
    dbgconrecvwriteidx = 0;
-
 
451
    wakeup_init(&dbgconsendwakeup);
-
 
452
    wakeup_init(&dbgconrecvwakeup);
-
 
453
    dbgconsoleattached = false;;
405
    thread_create("Debugger", dbgthread, dbgstack, sizeof(dbgstack), 255, SYSTEM_THREAD, true);
454
    thread_create("Debugger", dbgthread, dbgstack, sizeof(dbgstack), 255, SYSTEM_THREAD, true);
406
    usb_drv_init();
455
    usb_drv_init();
407
}
456
}
-
 
457
 
-
 
458
int dbgconsole_getfree() ICODE_ATTR;
-
 
459
int dbgconsole_getfree()
-
 
460
{
-
 
461
    int free = dbgconsendreadidx - dbgconsendwriteidx - 1;
-
 
462
    if (free < 0) free += sizeof(dbgconsendbuf);
-
 
463
    return free;
-
 
464
}
-
 
465
 
-
 
466
int dbgconsole_makespace(int length) ICODE_ATTR;
-
 
467
int dbgconsole_makespace(int length)
-
 
468
{
-
 
469
    int free = dbgconsole_getfree();
-
 
470
    while (!free && dbgconsoleattached)
-
 
471
    {
-
 
472
        if (wakeup_wait(&dbgconsendwakeup, 2000000) == THREAD_TIMEOUT)
-
 
473
            dbgconsoleattached = false;
-
 
474
        free = dbgconsole_getfree();
-
 
475
    }
-
 
476
    if (free) return free > length ? length : free;
-
 
477
    if (length > sizeof(dbgconsendbuf) - 17) length = sizeof(dbgconsendbuf) - 17;
-
 
478
    uint32_t mode = enter_critical_section();
-
 
479
    dbgconsendreadidx += length;
-
 
480
    if (dbgconsendreadidx >= sizeof(dbgconsendbuf))
-
 
481
        dbgconsendreadidx -= sizeof(dbgconsendbuf);
-
 
482
    int offset = 0;
-
 
483
    int idx = dbgconsendreadidx;
-
 
484
    if (idx + 16 >= sizeof(dbgconsendbuf))
-
 
485
    {
-
 
486
        offset = sizeof(dbgconsendbuf) - dbgconsendreadidx;
-
 
487
        memcpy(&dbgconsendbuf[dbgconsendreadidx], dbgconoverflowstr, offset);
-
 
488
        idx = 0;
-
 
489
    }
-
 
490
    if (offset != 16) memcpy(&dbgconsendbuf[idx], &dbgconoverflowstr[offset], 16 - offset);
-
 
491
    leave_critical_section(mode);
-
 
492
    return length;
-
 
493
}
-
 
494
 
-
 
495
void dbgconsole_putc(char string)
-
 
496
{
-
 
497
    dbgconsole_makespace(1);
-
 
498
    dbgconsendbuf[dbgconsendwriteidx++] = string;
-
 
499
    if (dbgconsendwriteidx >= sizeof(dbgconsendbuf))
-
 
500
        dbgconsendwriteidx -= sizeof(dbgconsendbuf);
-
 
501
}
-
 
502
 
-
 
503
void dbgconsole_write(const char* string, size_t length)
-
 
504
{
-
 
505
    while (length)
-
 
506
    {
-
 
507
        int space = dbgconsole_makespace(length);
-
 
508
        if (dbgconsendwriteidx + space >= sizeof(dbgconsendbuf))
-
 
509
        {
-
 
510
            int bytes = sizeof(dbgconsendbuf) - dbgconsendwriteidx;
-
 
511
            memcpy(&dbgconsendbuf[dbgconsendwriteidx], string, bytes);
-
 
512
            dbgconsendwriteidx = 0;
-
 
513
            string = &string[bytes];
-
 
514
            space -= bytes;
-
 
515
            length -= bytes;
-
 
516
        }
-
 
517
        if (space) memcpy(&dbgconsendbuf[dbgconsendwriteidx], string, space);
-
 
518
        dbgconsendwriteidx += space;
-
 
519
        string = &string[space];
-
 
520
        length -= space;
-
 
521
    }
-
 
522
}
-
 
523
 
-
 
524
void dbgconsole_puts(const char* string)
-
 
525
{
-
 
526
    dbgconsole_write(string, strlen(string));
-
 
527
}