Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
427 farthen 6
//    This file is part of emCORE.
2 theseven 7
//
427 farthen 8
//    emCORE is free software: you can redistribute it and/or
2 theseven 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
//
427 farthen 13
//    emCORE is distributed in the hope that it will be useful,
2 theseven 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
427 farthen 19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
2 theseven 20
//
21
//
22
 
23
 
24
#include "global.h"
25
#include "console.h"
26
#include "lcdconsole.h"
25 theseven 27
#include "usb/dbgconsole.h"
684 theseven 28
#include "uart.h"
2 theseven 29
#include "format.h"
14 theseven 30
#include "thread.h"
2 theseven 31
#include <stdarg.h>
32
#include <limits.h>
33
 
34
 
35
struct for_cprintf
36
{
37
    unsigned int consoles;
38
    size_t bytes;
39
};
40
 
41
 
14 theseven 42
struct mutex console_mutex;
26 theseven 43
struct mutex console_readmutex;
14 theseven 44
 
45
 
46
void console_init()
47
{
48
    mutex_init(&console_mutex);
26 theseven 49
    mutex_init(&console_readmutex);
684 theseven 50
#ifdef HAVE_UART
51
    uart_init();
52
#endif
14 theseven 53
}
54
 
55
void cputc_internal(unsigned int consoles, char string) ICODE_ATTR;
56
void cputc_internal(unsigned int consoles, char string)
57
{
85 theseven 58
#ifdef HAVE_LCD
704 theseven 59
    if (consoles & 1) lcdconsole_putc_noblit(string, LCDCONSOLE_FGCOLOR, LCDCONSOLE_BGCOLOR);
85 theseven 60
#endif
61
#ifdef HAVE_USB
25 theseven 62
    if (consoles & 2) dbgconsole_putc(string);
85 theseven 63
#endif
684 theseven 64
#ifdef HAVE_UART
65
    if (consoles & 4) uart_putc(string);
66
#endif
14 theseven 67
}
68
 
2 theseven 69
static int cprfunc(void* ptr, unsigned char letter)
70
{
71
    struct for_cprintf* pr = (struct for_cprintf*)ptr;
14 theseven 72
    cputc_internal(pr->consoles, letter);
2 theseven 73
    pr->bytes++;
74
    return true;
75
}
76
 
704 theseven 77
static int csprfunc(void* ptr, unsigned char letter)
78
{
79
    struct for_cprintf* pr = (struct for_cprintf*)ptr;
80
    csputc(pr->consoles, letter);
81
    pr->bytes++;
82
    return true;
83
}
84
 
2 theseven 85
int cprintf(unsigned int consoles, const char* fmt, ...)
86
{
87
    va_list ap;
88
    struct for_cprintf pr;
89
 
90
    pr.consoles = consoles;
91
    pr.bytes = 0;
92
 
14 theseven 93
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
2 theseven 94
    va_start(ap, fmt);
95
    format(cprfunc, &pr, fmt, ap);
96
    va_end(ap);
704 theseven 97
#ifdef HAVE_LCD
98
    if (consoles & 1) lcdconsole_update();
99
#endif
14 theseven 100
    mutex_unlock(&console_mutex);
2 theseven 101
 
102
    return pr.bytes;
103
}
104
 
704 theseven 105
int csprintf(unsigned int consoles, const char* fmt, ...)
106
 
107
{
108
    va_list ap;
109
    struct for_cprintf pr;
110
 
111
    pr.consoles = consoles;
112
    pr.bytes = 0;
113
 
114
    uint32_t mode = enter_critical_section();
115
    va_start(ap, fmt);
116
    format(csprfunc, &pr, fmt, ap);
117
    va_end(ap);
118
    leave_critical_section(mode);
119
 
120
    return pr.bytes;
121
}
122
 
2 theseven 123
int cvprintf(unsigned int consoles, const char* fmt, va_list ap)
124
{
125
    struct for_cprintf pr;
126
 
127
    pr.consoles = consoles;
128
    pr.bytes = 0;
129
 
14 theseven 130
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
2 theseven 131
    format(cprfunc, &pr, fmt, ap);
704 theseven 132
#ifdef HAVE_LCD
133
    if (consoles & 1) lcdconsole_update();
134
#endif
14 theseven 135
    mutex_unlock(&console_mutex);
2 theseven 136
 
137
    return pr.bytes;
138
}
139
 
704 theseven 140
int csvprintf(unsigned int consoles, const char* fmt, va_list ap)
141
{
142
    struct for_cprintf pr;
143
 
144
    pr.consoles = consoles;
145
    pr.bytes = 0;
146
 
147
    uint32_t mode = enter_critical_section();
148
    format(csprfunc, &pr, fmt, ap);
149
    leave_critical_section(mode);
150
 
151
    return pr.bytes;
152
}
153
 
2 theseven 154
void cputc(unsigned int consoles, char string)
155
{
14 theseven 156
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
157
    cputc_internal(consoles, string);
704 theseven 158
#ifdef HAVE_LCD
159
    if (consoles & 1) lcdconsole_update();
160
#endif
14 theseven 161
    mutex_unlock(&console_mutex);
2 theseven 162
}
163
 
704 theseven 164
void csputc(unsigned int consoles, char string)
165
{
166
    uint32_t mode = enter_critical_section();
167
#ifdef HAVE_LCD
168
    if (consoles & 1) lcdconsole_putc_noblit(string, LCDCONSOLE_FGCOLOR, LCDCONSOLE_BGCOLOR);
169
#endif
170
#ifdef HAVE_USB
171
    if (consoles & 2) dbgconsole_sputc(string);
172
#endif
173
#ifdef HAVE_UART
174
    if (consoles & 4) uart_sputc(string);
175
#endif
176
    leave_critical_section(mode);
177
}
178
 
2 theseven 179
void cputs(unsigned int consoles, const char* string)
180
{
14 theseven 181
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
85 theseven 182
#ifdef HAVE_LCD
257 theseven 183
    if (consoles & 1) lcdconsole_puts(string, LCDCONSOLE_FGCOLOR, LCDCONSOLE_BGCOLOR);
85 theseven 184
#endif
185
#ifdef HAVE_USB
25 theseven 186
    if (consoles & 2) dbgconsole_puts(string);
85 theseven 187
#endif
684 theseven 188
#ifdef HAVE_UART
189
    if (consoles & 4) uart_puts(string);
190
#endif
14 theseven 191
    mutex_unlock(&console_mutex);
2 theseven 192
}
193
 
704 theseven 194
void csputs(unsigned int consoles, const char* string)
195
{
196
    uint32_t mode = enter_critical_section();
197
#ifdef HAVE_LCD
198
    if (consoles & 1) lcdconsole_puts_noblit(string, LCDCONSOLE_FGCOLOR, LCDCONSOLE_BGCOLOR);
199
#endif
200
#ifdef HAVE_USB
201
    if (consoles & 2) dbgconsole_sputs(string);
202
#endif
203
#ifdef HAVE_UART
204
    if (consoles & 4) uart_sputs(string);
205
#endif
206
    leave_critical_section(mode);
207
}
208
 
25 theseven 209
void cwrite(unsigned int consoles, const char* string, size_t length)
210
{
211
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
85 theseven 212
#ifdef HAVE_LCD
257 theseven 213
    if (consoles & 1) lcdconsole_write(string, length, LCDCONSOLE_FGCOLOR, LCDCONSOLE_BGCOLOR);
85 theseven 214
#endif
215
#ifdef HAVE_USB
25 theseven 216
    if (consoles & 2) dbgconsole_write(string, length);
85 theseven 217
#endif
684 theseven 218
#ifdef HAVE_UART
219
    if (consoles & 4) uart_write(string, length);
220
#endif
25 theseven 221
    mutex_unlock(&console_mutex);
222
}
223
 
704 theseven 224
void cswrite(unsigned int consoles, const char* string, size_t length)
225
{
226
    uint32_t mode = enter_critical_section();
227
#ifdef HAVE_LCD
228
    if (consoles & 1) lcdconsole_write_noblit(string, length, LCDCONSOLE_FGCOLOR, LCDCONSOLE_BGCOLOR);
229
#endif
230
#ifdef HAVE_USB
231
    if (consoles & 2) dbgconsole_swrite(string, length);
232
#endif
233
#ifdef HAVE_UART
234
    if (consoles & 4) uart_swrite(string, length);
235
#endif
236
    leave_critical_section(mode);
237
}
238
 
2 theseven 239
void cflush(unsigned int consoles)
240
{
14 theseven 241
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
85 theseven 242
#ifdef HAVE_LCD
14 theseven 243
    if (consoles & 1) lcdconsole_update();
85 theseven 244
#endif
14 theseven 245
    mutex_unlock(&console_mutex);
2 theseven 246
}
26 theseven 247
 
704 theseven 248
void csflush(unsigned int consoles)
249
{
250
    uint32_t mode = enter_critical_section();
251
#ifdef HAVE_LCD
252
    if (consoles & 1) lcdconsole_supdate();
253
#endif
254
    leave_critical_section(mode);
255
}
256
 
26 theseven 257
int cgetc(unsigned int consoles, int timeout)
258
{
259
    int result;
260
    mutex_lock(&console_readmutex, TIMEOUT_BLOCK);
85 theseven 261
#ifdef HAVE_USB
26 theseven 262
    if ((consoles & 2) && (result = dbgconsole_getc(timeout)) != -1) return result;
85 theseven 263
#endif
684 theseven 264
#ifdef HAVE_UART
265
    if ((consoles & 4) && (result = uart_getc(timeout)) != -1) return result;
266
#endif
214 theseven 267
    mutex_unlock(&console_readmutex);
26 theseven 268
}
269
 
270
int cread(unsigned int consoles, char* buffer, size_t length, int timeout)
271
{
272
    int result;
273
    mutex_lock(&console_readmutex, TIMEOUT_BLOCK);
85 theseven 274
#ifdef HAVE_USB
26 theseven 275
    if ((consoles & 2) && (result = dbgconsole_read(buffer, length, timeout))) return result;
85 theseven 276
#endif
684 theseven 277
#ifdef HAVE_UART
278
    if ((consoles & 2) && (result = uart_read(buffer, length, timeout))) return result;
279
#endif
214 theseven 280
    mutex_unlock(&console_readmutex);
26 theseven 281
}
282
 
283
void creada(unsigned int consoles, char* buffer, size_t length, int timeout)
284
{
285
    int result;
286
    mutex_lock(&console_readmutex, TIMEOUT_BLOCK);
287
    while (length)
288
    {
684 theseven 289
        if (
85 theseven 290
#ifdef HAVE_USB
684 theseven 291
            ((consoles & 2) && (result = dbgconsole_read(buffer, length, timeout)))
292
#else
293
            false
294
#endif
295
            ||
296
#ifdef HAVE_UART
297
            ((consoles & 4) && (result = uart_read(buffer, length, timeout)))
298
#else
299
            false
300
#endif
301
            )
26 theseven 302
        {
303
            buffer = &buffer[result];
304
            length -= result;
305
        }
306
    }
214 theseven 307
    mutex_unlock(&console_readmutex);
26 theseven 308
}