Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
6
//    This file is part of emBIOS.
7
//
8
//    emBIOS is free software: you can redistribute it and/or
9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
13
//    emBIOS is distributed in the hope that it will be useful,
14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
19
//    with emBIOS.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
24
#include "global.h"
25
#include "panic.h"
26
#include "usb.h"
27
#include "usb_ch9.h"
28
#include "usbdrv.h"
29
#include "thread.h"
30
#include "console.h"
31
#include "util.h"
32
#include "i2c.h"
25 theseven 33
#include "strlen.h"
34
#include "contextswitch.h"
15 theseven 35
 
36
 
37
static uint8_t ctrlresp[2] CACHEALIGN_ATTR;
38
static uint32_t dbgrecvbuf[0x80] CACHEALIGN_ATTR;
39
static uint32_t dbgsendbuf[0x80] CACHEALIGN_ATTR;
40
static uint32_t dbgasyncsendbuf[0x80] CACHEALIGN_ATTR;
41
static char dbgendpoints[4] IBSS_ATTR;
42
 
43
enum dbgaction_t
44
{
45
    DBGACTION_IDLE = 0,
46
    DBGACTION_I2CSEND,
47
    DBGACTION_I2CRECV,
48
    DBGACTION_POWEROFF
49
};
50
 
51
static uint32_t dbgstack[0x100] STACK_ATTR;
52
struct wakeup dbgwakeup IBSS_ATTR;
53
extern struct scheduler_thread* scheduler_threads;
54
static enum dbgaction_t dbgaction IBSS_ATTR;
55
static int dbgi2cbus IBSS_ATTR;
56
static int dbgi2cslave IBSS_ATTR;
57
static int dbgi2caddr IBSS_ATTR;
58
static int dbgi2clen IBSS_ATTR;
25 theseven 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;
15 theseven 68
 
25 theseven 69
static const char dbgconoverflowstr[] = "\n\n[overflowed]\n\n";
15 theseven 70
 
25 theseven 71
 
15 theseven 72
static struct usb_device_descriptor CACHEALIGN_ATTR device_descriptor =
73
{
74
    .bLength            = sizeof(struct usb_device_descriptor),
75
    .bDescriptorType    = USB_DT_DEVICE,
76
    .bcdUSB             = 0x0200,
77
    .bDeviceClass       = USB_CLASS_VENDOR_SPEC,
78
    .bDeviceSubClass    = 0xff,
79
    .bDeviceProtocol    = 0xff,
80
    .bMaxPacketSize0    = 64,
81
    .idVendor           = 0xffff,
82
    .idProduct          = 0xe000,
83
    .bcdDevice          = 0x0001,
84
    .iManufacturer      = 1,
85
    .iProduct           = 2,
86
    .iSerialNumber      = 0,
87
    .bNumConfigurations = 1
88
};
89
 
90
static struct usb_config_bundle
91
{
92
    struct usb_config_descriptor config_descriptor;
93
    struct usb_interface_descriptor interface_descriptor;
94
    struct usb_endpoint_descriptor endpoint1_descriptor;
95
    struct usb_endpoint_descriptor endpoint2_descriptor;
96
    struct usb_endpoint_descriptor endpoint3_descriptor;
97
    struct usb_endpoint_descriptor endpoint4_descriptor;
98
} __attribute__((packed)) CACHEALIGN_ATTR config_bundle = 
99
{
100
    .config_descriptor =
101
    {
102
        .bLength             = sizeof(struct usb_config_descriptor),
103
        .bDescriptorType     = USB_DT_CONFIG,
104
        .wTotalLength        = sizeof(struct usb_config_descriptor)
105
                             + sizeof(struct usb_interface_descriptor)
106
                             + sizeof(struct usb_endpoint_descriptor) * 4,
107
        .bNumInterfaces      = 1,
108
        .bConfigurationValue = 1,
109
        .iConfiguration      = 0,
110
        .bmAttributes        = USB_CONFIG_ATT_ONE,
111
        .bMaxPower           = 250
112
    },
113
    .interface_descriptor =
114
    {
115
        .bLength             = sizeof(struct usb_interface_descriptor),
116
        .bDescriptorType     = USB_DT_INTERFACE,
117
        .bInterfaceNumber    = 0,
118
        .bAlternateSetting   = 0,
119
        .bNumEndpoints       = 4,
120
        .bInterfaceClass     = USB_CLASS_VENDOR_SPEC,
121
        .bInterfaceSubClass  = 0xff,
122
        .bInterfaceProtocol  = 0xff,
123
        .iInterface          = 0
124
    },
125
    .endpoint1_descriptor =
126
    {
127
        .bLength             = sizeof(struct usb_endpoint_descriptor),
128
        .bDescriptorType     = USB_DT_ENDPOINT,
129
        .bEndpointAddress    = 0,
130
        .bmAttributes        = USB_ENDPOINT_XFER_BULK,
131
        .wMaxPacketSize      = 0,
132
        .bInterval           = 1
133
    },
134
    .endpoint2_descriptor =
135
    {
136
        .bLength             = sizeof(struct usb_endpoint_descriptor),
137
        .bDescriptorType     = USB_DT_ENDPOINT,
138
        .bEndpointAddress    = 0,
139
        .bmAttributes        = USB_ENDPOINT_XFER_BULK,
140
        .wMaxPacketSize      = 0,
141
        .bInterval           = 1
142
    },
143
    .endpoint3_descriptor =
144
    {
145
        .bLength             = sizeof(struct usb_endpoint_descriptor),
146
        .bDescriptorType     = USB_DT_ENDPOINT,
147
        .bEndpointAddress    = 0,
148
        .bmAttributes        = USB_ENDPOINT_XFER_BULK,
149
        .wMaxPacketSize      = 0,
150
        .bInterval           = 1
151
    },
152
    .endpoint4_descriptor =
153
    {
154
        .bLength             = sizeof(struct usb_endpoint_descriptor),
155
        .bDescriptorType     = USB_DT_ENDPOINT,
156
        .bEndpointAddress    = 0,
157
        .bmAttributes        = USB_ENDPOINT_XFER_BULK,
158
        .wMaxPacketSize      = 0,
159
        .bInterval           = 1
160
    }
161
};
162
 
163
static struct usb_string_descriptor CACHEALIGN_ATTR string_devicename =
164
{
165
    32,
166
    USB_DT_STRING,
167
    {'e', 'm', 'B', 'I', 'O', 'S', ' ', 'D', 'e', 'b', 'u', 'g', 'g', 'e', 'r'}
168
};
169
 
170
static const struct usb_string_descriptor CACHEALIGN_ATTR lang_descriptor =
171
{
172
    4,
173
    USB_DT_STRING,
174
    {0x0409}
175
};
176
 
177
 
178
void usb_setup_dbg_listener()
179
{
180
    usb_drv_recv(dbgendpoints[0], dbgrecvbuf, usb_drv_port_speed() ? 512 : 64);
181
}
182
 
183
void usb_handle_control_request(struct usb_ctrlrequest* req)
184
{
185
    const void* addr;
186
    int size = -1;
187
    switch (req->bRequest)
188
    {
189
    case USB_REQ_GET_STATUS:
190
        if (req->bRequestType == USB_DIR_IN) ctrlresp[0] = 1;
191
        else ctrlresp[0] = 0;
192
        ctrlresp[1] = 0;
193
        addr = ctrlresp;
194
        size = 2;
195
        break;
196
    case USB_REQ_CLEAR_FEATURE:
197
        if (req->bRequestType == USB_RECIP_ENDPOINT && req->wValue == USB_ENDPOINT_HALT)
198
            usb_drv_stall(req->wIndex & 0xf, false, req->wIndex >> 7);
199
        size = 0;
200
        break;
201
    case USB_REQ_SET_FEATURE:
202
        size = 0;
203
        break;
204
    case USB_REQ_SET_ADDRESS:
205
        size = 0;
206
        usb_drv_cancel_all_transfers();
207
        usb_drv_set_address(req->wValue);
208
        usb_setup_dbg_listener();
209
        break;
210
    case USB_REQ_GET_DESCRIPTOR:
211
        switch (req->wValue >> 8)
212
        {
213
        case USB_DT_DEVICE:
214
            addr = &device_descriptor;
215
            size = sizeof(device_descriptor);
216
            break;
217
        case USB_DT_OTHER_SPEED_CONFIG:
218
        case USB_DT_CONFIG:
219
            if ((req->wValue >> 8) == USB_DT_CONFIG)
220
            {
221
                int maxpacket = usb_drv_port_speed() ? 512 : 64;
222
                config_bundle.endpoint1_descriptor.wMaxPacketSize = maxpacket;
223
                config_bundle.endpoint2_descriptor.wMaxPacketSize = maxpacket;
224
                config_bundle.endpoint3_descriptor.wMaxPacketSize = maxpacket;
225
                config_bundle.endpoint4_descriptor.wMaxPacketSize = maxpacket;
226
                config_bundle.config_descriptor.bDescriptorType = USB_DT_CONFIG;
227
            }
228
            else
229
            {
230
                int maxpacket = usb_drv_port_speed() ? 64 : 512;
231
                config_bundle.endpoint1_descriptor.wMaxPacketSize = maxpacket;
232
                config_bundle.endpoint2_descriptor.wMaxPacketSize = maxpacket;
233
                config_bundle.endpoint3_descriptor.wMaxPacketSize = maxpacket;
234
                config_bundle.endpoint4_descriptor.wMaxPacketSize = maxpacket;
235
                config_bundle.config_descriptor.bDescriptorType = USB_DT_OTHER_SPEED_CONFIG;
236
            }
237
            addr = &config_bundle;
238
            size = sizeof(config_bundle);
239
            break;
240
        case USB_DT_STRING:
241
            switch (req->wValue & 0xff)
242
            {
243
            case 0:
244
                addr = &lang_descriptor;
245
                size = sizeof(lang_descriptor);
246
                break;
247
            case 1:
248
                string_devicename.bLength = 14;
249
                addr = &string_devicename;
250
                size = 14;
251
            case 2:
252
                string_devicename.bLength = sizeof(string_devicename);
253
                addr = &string_devicename;
254
                size = sizeof(string_devicename);
255
                break;
256
            }
257
            break;
258
        }
259
        break;
260
    case USB_REQ_GET_CONFIGURATION:
261
        ctrlresp[0] = 1;
262
        addr = ctrlresp;
263
        size = 1;
264
        break;
265
    case USB_REQ_SET_CONFIGURATION:
266
        usb_drv_cancel_all_transfers();
267
        usb_setup_dbg_listener();
268
        size = 0;
269
        break;
270
    }
271
    if (!size) usb_drv_send_nonblocking(0, NULL, 0);
272
    else if (size == -1)
273
    {
274
        usb_drv_stall(0, true, true);
275
        usb_drv_stall(0, true, false);
276
    }
277
    else
278
    {
279
        usb_drv_recv(0, NULL, 0);
280
        usb_drv_send_nonblocking(0, addr, size > req->wLength ? req->wLength : size);
281
    }
282
}
283
 
284
bool set_dbgaction(enum dbgaction_t action)
285
{
286
    if (dbgaction != DBGACTION_IDLE)
287
    {
288
        dbgsendbuf[0] = 3;
289
        usb_drv_send_nonblocking(dbgendpoints[1], dbgsendbuf, 16);
290
        return true;
291
    }
292
    dbgaction = action;
293
    wakeup_signal(&dbgwakeup);
294
    return false;
295
}
296
 
297
void reset() __attribute__((noreturn));
298
 
299
void usb_handle_transfer_complete(int endpoint, int dir, int status, int length)
300
{
301
    void* addr = dbgsendbuf;
302
    int size = 0;
303
    if (endpoint == dbgendpoints[0])
304
    {
305
        switch (dbgrecvbuf[0])
306
        {
307
        case 1:  // GET VERSION
308
            dbgsendbuf[0] = 1;
309
            dbgsendbuf[1] = 0x01010000;
310
            dbgsendbuf[2] = PLATFORM_ID;
25 theseven 311
            dbgsendbuf[3] = 0x02000200;
15 theseven 312
            size = 16;
313
            break;
314
        case 2:  // RESET
315
            reset();
316
            break;
317
        case 3:  // POWER OFF
318
            set_dbgaction(DBGACTION_POWEROFF);
319
            break;
320
        case 4:  // READ MEMORY
321
            dbgsendbuf[0] = 1;
322
            memcpy(&dbgsendbuf[4], (const void*)dbgrecvbuf[1], dbgrecvbuf[2]);
323
            size = dbgrecvbuf[2] + 16;
324
            break;
325
        case 5:  // WRITE MEMORY
326
            dbgsendbuf[0] = 1;
327
            memcpy((void*)dbgrecvbuf[1], &dbgrecvbuf[4], dbgrecvbuf[2]);
328
            size = 16;
329
            break;
330
        case 6:  // READ DMA
331
            dbgsendbuf[0] = 1;
332
            usb_drv_send_nonblocking(dbgendpoints[1], dbgsendbuf, 16);
333
            usb_drv_send_nonblocking(dbgendpoints[3], (const void*)dbgrecvbuf[1], dbgrecvbuf[2]);
334
            break;
335
        case 7:  // WRITE DMA
336
            dbgsendbuf[0] = 1;
337
            size = 16;
338
            usb_drv_recv(dbgendpoints[2], (void*)dbgrecvbuf[1], dbgrecvbuf[2]);
339
            break;
340
        case 8:  // READ I2C
341
            if (set_dbgaction(DBGACTION_I2CRECV)) break;
342
            dbgi2cbus = dbgrecvbuf[1] & 0xff;
343
            dbgi2cslave = (dbgrecvbuf[1] >> 8) & 0xff;
344
            dbgi2caddr = (dbgrecvbuf[1] >> 16) & 0xff;
345
            dbgi2clen = dbgrecvbuf[1] >> 24;
346
            break;
347
        case 9:  // WRITE I2C
348
            if (set_dbgaction(DBGACTION_I2CSEND)) break;
349
            dbgi2cbus = dbgrecvbuf[1] & 0xff;
350
            dbgi2cslave = (dbgrecvbuf[1] >> 8) & 0xff;
351
            dbgi2caddr = (dbgrecvbuf[1] >> 16) & 0xff;
352
            dbgi2clen = dbgrecvbuf[1] >> 24;
353
            memcpy(dbgasyncsendbuf, &dbgsendbuf[4], dbgi2clen);
354
            break;
25 theseven 355
        case 10:  // READ CONSOLE
356
            dbgconsoleattached = true;
357
            int bytes = dbgconsendwriteidx - dbgconsendreadidx;
358
            if (bytes >= sizeof(dbgconsendbuf)) bytes -= sizeof(dbgconsendbuf);
359
            if (bytes)
360
            {
361
                if (bytes < 0) bytes += sizeof(dbgconsendbuf);
362
                if (bytes > dbgrecvbuf[1]) bytes = dbgrecvbuf[1];
363
                int readbytes = bytes;
364
                char* outptr = (char*)&dbgsendbuf[4];
365
                if (dbgconsendreadidx + bytes >= sizeof(dbgconsendbuf))
366
                {
367
                    readbytes = sizeof(dbgconsendbuf) - dbgconsendreadidx;
368
                    memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
369
                    dbgconsendreadidx = 0;
370
                    outptr = &outptr[readbytes];
371
                    readbytes = bytes - readbytes;
372
                }
373
                if (readbytes) memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
374
                dbgconsendreadidx += readbytes;
26 theseven 375
                wakeup_signal(&dbgconsendwakeup);
25 theseven 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;
26 theseven 383
        case 11:  // WRITE CONSOLE
384
            bytes = dbgconrecvreadidx - dbgconrecvwriteidx - 1;
385
            if (bytes < 0) bytes += sizeof(dbgconrecvbuf);
386
            if (bytes)
387
            {
388
                if (bytes > dbgrecvbuf[1]) bytes = dbgrecvbuf[1];
389
                int writebytes = bytes;
390
                char* readptr = (char*)&dbgrecvbuf[4];
391
                if (dbgconrecvwriteidx + bytes >= sizeof(dbgconrecvbuf))
392
                {
393
                    writebytes = sizeof(dbgconrecvbuf) - dbgconrecvwriteidx;
394
                    memcpy(&dbgconrecvbuf[dbgconrecvwriteidx], readptr, writebytes);
395
                    dbgconrecvwriteidx = 0;
396
                    readptr = &readptr[writebytes];
397
                    writebytes = bytes - writebytes;
398
                }
399
                if (writebytes) memcpy(&dbgconrecvbuf[dbgconrecvwriteidx], readptr, writebytes);
400
                dbgconrecvwriteidx += writebytes;
401
                wakeup_signal(&dbgconrecvwakeup);
402
            }
403
            dbgsendbuf[0] = 1;
404
            dbgsendbuf[1] = bytes;
405
            dbgsendbuf[2] = sizeof(dbgconrecvbuf);
406
            dbgsendbuf[3] = dbgconrecvreadidx - dbgconrecvwriteidx - 1;
407
            size = 16;
408
            break;
15 theseven 409
        default:
410
            dbgsendbuf[0] = 2;
411
            size = 16;
412
        }
413
        usb_setup_dbg_listener();
414
        if (size) usb_drv_send_nonblocking(dbgendpoints[1], addr, size);
415
    }
416
}
417
 
418
void usb_handle_bus_reset(void)
419
{
420
    dbgendpoints[0] = usb_drv_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_OUT);
421
    dbgendpoints[1] = usb_drv_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_IN);
422
    dbgendpoints[2] = usb_drv_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_OUT);
423
    dbgendpoints[3] = usb_drv_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_IN);
424
    config_bundle.endpoint1_descriptor.bEndpointAddress = dbgendpoints[0];
425
    config_bundle.endpoint2_descriptor.bEndpointAddress = dbgendpoints[1];
426
    config_bundle.endpoint3_descriptor.bEndpointAddress = dbgendpoints[2];
427
    config_bundle.endpoint4_descriptor.bEndpointAddress = dbgendpoints[3];
428
    usb_setup_dbg_listener();
429
}
430
 
431
void dbgthread(void)
432
{
433
    int i;
25 theseven 434
    int t;
15 theseven 435
    while (1)
436
    {
437
        wakeup_wait(&dbgwakeup, TIMEOUT_BLOCK);
438
        for (i = 0; i < MAX_THREADS; i++)
439
            if (scheduler_threads[i].state == THREAD_DEFUNCT)
440
            {
441
                if (scheduler_threads[i].block_type == THREAD_DEFUNCT_STKOV)
442
                    cprintf(1, "\n*PANIC*\nStack overflow! (%s)\n",
443
                            scheduler_threads[i].name);
444
                scheduler_threads[i].state = THREAD_DEFUNCT_ACK;
445
            }
446
        if (dbgaction != DBGACTION_IDLE)
447
        {
448
            switch (dbgaction)
449
            {
450
            case DBGACTION_I2CSEND:
451
                i2c_send(dbgi2cbus, dbgi2cslave, dbgi2caddr, (uint8_t*)dbgasyncsendbuf, dbgi2clen);
452
                dbgasyncsendbuf[0] = 1;
453
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
454
                break;
455
            case DBGACTION_I2CRECV:
456
                i2c_recv(dbgi2cbus, dbgi2cslave, dbgi2caddr,
457
                         (uint8_t*)(&dbgasyncsendbuf[4]), dbgi2clen);
458
                dbgasyncsendbuf[0] = 1;
459
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16 + dbgi2clen);
460
                break;
461
            case DBGACTION_POWEROFF:
462
                break;
463
            }
464
            dbgaction = DBGACTION_IDLE;
465
        }
466
    }
467
}
468
 
469
void usb_init(void)
470
{
471
    dbgaction = DBGACTION_IDLE;
472
    wakeup_init(&dbgwakeup);
25 theseven 473
    dbgconsendreadidx = 0;
474
    dbgconsendwriteidx = 0;
475
    dbgconrecvreadidx = 0;
476
    dbgconrecvwriteidx = 0;
477
    wakeup_init(&dbgconsendwakeup);
478
    wakeup_init(&dbgconrecvwakeup);
479
    dbgconsoleattached = false;;
15 theseven 480
    thread_create("Debugger", dbgthread, dbgstack, sizeof(dbgstack), 255, SYSTEM_THREAD, true);
481
    usb_drv_init();
482
}
25 theseven 483
 
484
int dbgconsole_getfree() ICODE_ATTR;
485
int dbgconsole_getfree()
486
{
487
    int free = dbgconsendreadidx - dbgconsendwriteidx - 1;
488
    if (free < 0) free += sizeof(dbgconsendbuf);
489
    return free;
490
}
491
 
492
int dbgconsole_makespace(int length) ICODE_ATTR;
493
int dbgconsole_makespace(int length)
494
{
495
    int free = dbgconsole_getfree();
496
    while (!free && dbgconsoleattached)
497
    {
498
        if (wakeup_wait(&dbgconsendwakeup, 2000000) == THREAD_TIMEOUT)
499
            dbgconsoleattached = false;
500
        free = dbgconsole_getfree();
501
    }
502
    if (free) return free > length ? length : free;
503
    if (length > sizeof(dbgconsendbuf) - 17) length = sizeof(dbgconsendbuf) - 17;
504
    uint32_t mode = enter_critical_section();
505
    dbgconsendreadidx += length;
506
    if (dbgconsendreadidx >= sizeof(dbgconsendbuf))
507
        dbgconsendreadidx -= sizeof(dbgconsendbuf);
508
    int offset = 0;
509
    int idx = dbgconsendreadidx;
510
    if (idx + 16 >= sizeof(dbgconsendbuf))
511
    {
512
        offset = sizeof(dbgconsendbuf) - dbgconsendreadidx;
513
        memcpy(&dbgconsendbuf[dbgconsendreadidx], dbgconoverflowstr, offset);
514
        idx = 0;
515
    }
516
    if (offset != 16) memcpy(&dbgconsendbuf[idx], &dbgconoverflowstr[offset], 16 - offset);
517
    leave_critical_section(mode);
518
    return length;
519
}
520
 
521
void dbgconsole_putc(char string)
522
{
523
    dbgconsole_makespace(1);
524
    dbgconsendbuf[dbgconsendwriteidx++] = string;
525
    if (dbgconsendwriteidx >= sizeof(dbgconsendbuf))
526
        dbgconsendwriteidx -= sizeof(dbgconsendbuf);
527
}
528
 
529
void dbgconsole_write(const char* string, size_t length)
530
{
531
    while (length)
532
    {
533
        int space = dbgconsole_makespace(length);
534
        if (dbgconsendwriteidx + space >= sizeof(dbgconsendbuf))
535
        {
536
            int bytes = sizeof(dbgconsendbuf) - dbgconsendwriteidx;
537
            memcpy(&dbgconsendbuf[dbgconsendwriteidx], string, bytes);
538
            dbgconsendwriteidx = 0;
539
            string = &string[bytes];
540
            space -= bytes;
541
            length -= bytes;
542
        }
543
        if (space) memcpy(&dbgconsendbuf[dbgconsendwriteidx], string, space);
544
        dbgconsendwriteidx += space;
545
        string = &string[space];
546
        length -= space;
547
    }
548
}
549
 
550
void dbgconsole_puts(const char* string)
551
{
552
    dbgconsole_write(string, strlen(string));
553
}
26 theseven 554
 
555
int dbgconsole_getavailable() ICODE_ATTR;
556
int dbgconsole_getavailable()
557
{
558
    int available = dbgconrecvwriteidx - dbgconrecvreadidx;
559
    if (available < 0) available += sizeof(dbgconrecvbuf);
560
    return available;
561
}
562
 
563
int dbgconsole_getc(int timeout)
564
{
565
    if (!dbgconsole_getavailable())
566
    {
567
        wakeup_wait(&dbgconrecvwakeup, TIMEOUT_NONE);
568
        if (!dbgconsole_getavailable())
569
        {
570
            wakeup_wait(&dbgconrecvwakeup, timeout);
571
            if (!dbgconsole_getavailable()) return -1;
572
        }
573
    }
574
    int byte = dbgconrecvbuf[dbgconrecvreadidx++];
575
    if (dbgconrecvreadidx >= sizeof(dbgconrecvbuf))
576
        dbgconrecvreadidx -= sizeof(dbgconrecvbuf);
577
    return byte;
578
}
579
 
580
int dbgconsole_read(char* buffer, size_t length, int timeout)
581
{
582
    if (!length) return 0;
583
    int available = dbgconsole_getavailable();
584
    if (!available)
585
    {
586
        wakeup_wait(&dbgconrecvwakeup, TIMEOUT_NONE);
587
        int available = dbgconsole_getavailable();
588
        if (!available)
589
        {
590
            wakeup_wait(&dbgconrecvwakeup, timeout);
591
            int available = dbgconsole_getavailable();
592
            if (!available) return 0;
593
        }
594
    }
595
    if (available > length) available = length;
596
    int left = available;
597
    if (dbgconrecvreadidx + available >= sizeof(dbgconrecvbuf))
598
    {
599
        int bytes = sizeof(dbgconrecvbuf) - dbgconrecvreadidx;
600
        memcpy(buffer, &dbgconrecvbuf[dbgconrecvreadidx], bytes);
601
        dbgconrecvreadidx = 0;
602
        buffer = &buffer[bytes];
603
        left -= bytes;
604
    }
605
    if (left) memcpy(buffer, &dbgconrecvbuf[dbgconrecvreadidx], left);
606
    dbgconrecvreadidx += left;
607
    return available;
608
}