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
 
867 user890104 55
void console_set_speed(unsigned int consoles, unsigned int cps)
56
{
57
#ifdef HAVE_UART
58
    if (consoles & 4) uart_set_baud(cps * 10);
59
#endif
60
}
61
 
14 theseven 62
void cputc_internal(unsigned int consoles, char string) ICODE_ATTR;
63
void cputc_internal(unsigned int consoles, char string)
64
{
85 theseven 65
#ifdef HAVE_LCD
704 theseven 66
    if (consoles & 1) lcdconsole_putc_noblit(string, LCDCONSOLE_FGCOLOR, LCDCONSOLE_BGCOLOR);
85 theseven 67
#endif
68
#ifdef HAVE_USB
25 theseven 69
    if (consoles & 2) dbgconsole_putc(string);
85 theseven 70
#endif
684 theseven 71
#ifdef HAVE_UART
72
    if (consoles & 4) uart_putc(string);
73
#endif
14 theseven 74
}
75
 
2 theseven 76
static int cprfunc(void* ptr, unsigned char letter)
77
{
78
    struct for_cprintf* pr = (struct for_cprintf*)ptr;
14 theseven 79
    cputc_internal(pr->consoles, letter);
2 theseven 80
    pr->bytes++;
81
    return true;
82
}
83
 
704 theseven 84
static int csprfunc(void* ptr, unsigned char letter)
85
{
86
    struct for_cprintf* pr = (struct for_cprintf*)ptr;
87
    csputc(pr->consoles, letter);
88
    pr->bytes++;
89
    return true;
90
}
91
 
2 theseven 92
int cprintf(unsigned int consoles, const char* fmt, ...)
93
{
94
    va_list ap;
95
    struct for_cprintf pr;
96
 
97
    pr.consoles = consoles;
98
    pr.bytes = 0;
99
 
14 theseven 100
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
2 theseven 101
    va_start(ap, fmt);
102
    format(cprfunc, &pr, fmt, ap);
103
    va_end(ap);
704 theseven 104
#ifdef HAVE_LCD
105
    if (consoles & 1) lcdconsole_update();
106
#endif
14 theseven 107
    mutex_unlock(&console_mutex);
2 theseven 108
 
109
    return pr.bytes;
110
}
111
 
704 theseven 112
int csprintf(unsigned int consoles, const char* fmt, ...)
113
 
114
{
115
    va_list ap;
116
    struct for_cprintf pr;
117
 
118
    pr.consoles = consoles;
119
    pr.bytes = 0;
120
 
121
    uint32_t mode = enter_critical_section();
122
    va_start(ap, fmt);
123
    format(csprfunc, &pr, fmt, ap);
124
    va_end(ap);
125
    leave_critical_section(mode);
126
 
127
    return pr.bytes;
128
}
129
 
2 theseven 130
int cvprintf(unsigned int consoles, const char* fmt, va_list ap)
131
{
132
    struct for_cprintf pr;
133
 
134
    pr.consoles = consoles;
135
    pr.bytes = 0;
136
 
14 theseven 137
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
2 theseven 138
    format(cprfunc, &pr, fmt, ap);
704 theseven 139
#ifdef HAVE_LCD
140
    if (consoles & 1) lcdconsole_update();
141
#endif
14 theseven 142
    mutex_unlock(&console_mutex);
2 theseven 143
 
144
    return pr.bytes;
145
}
146
 
704 theseven 147
int csvprintf(unsigned int consoles, const char* fmt, va_list ap)
148
{
149
    struct for_cprintf pr;
150
 
151
    pr.consoles = consoles;
152
    pr.bytes = 0;
153
 
154
    uint32_t mode = enter_critical_section();
155
    format(csprfunc, &pr, fmt, ap);
156
    leave_critical_section(mode);
157
 
158
    return pr.bytes;
159
}
160
 
2 theseven 161
void cputc(unsigned int consoles, char string)
162
{
14 theseven 163
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
164
    cputc_internal(consoles, string);
704 theseven 165
#ifdef HAVE_LCD
166
    if (consoles & 1) lcdconsole_update();
167
#endif
14 theseven 168
    mutex_unlock(&console_mutex);
2 theseven 169
}
170
 
704 theseven 171
void csputc(unsigned int consoles, char string)
172
{
173
    uint32_t mode = enter_critical_section();
174
#ifdef HAVE_LCD
175
    if (consoles & 1) lcdconsole_putc_noblit(string, LCDCONSOLE_FGCOLOR, LCDCONSOLE_BGCOLOR);
176
#endif
177
#ifdef HAVE_USB
178
    if (consoles & 2) dbgconsole_sputc(string);
179
#endif
180
#ifdef HAVE_UART
181
    if (consoles & 4) uart_sputc(string);
182
#endif
183
    leave_critical_section(mode);
184
}
185
 
2 theseven 186
void cputs(unsigned int consoles, const char* string)
187
{
14 theseven 188
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
85 theseven 189
#ifdef HAVE_LCD
257 theseven 190
    if (consoles & 1) lcdconsole_puts(string, LCDCONSOLE_FGCOLOR, LCDCONSOLE_BGCOLOR);
85 theseven 191
#endif
192
#ifdef HAVE_USB
25 theseven 193
    if (consoles & 2) dbgconsole_puts(string);
85 theseven 194
#endif
684 theseven 195
#ifdef HAVE_UART
196
    if (consoles & 4) uart_puts(string);
197
#endif
14 theseven 198
    mutex_unlock(&console_mutex);
2 theseven 199
}
200
 
704 theseven 201
void csputs(unsigned int consoles, const char* string)
202
{
203
    uint32_t mode = enter_critical_section();
204
#ifdef HAVE_LCD
205
    if (consoles & 1) lcdconsole_puts_noblit(string, LCDCONSOLE_FGCOLOR, LCDCONSOLE_BGCOLOR);
206
#endif
207
#ifdef HAVE_USB
208
    if (consoles & 2) dbgconsole_sputs(string);
209
#endif
210
#ifdef HAVE_UART
211
    if (consoles & 4) uart_sputs(string);
212
#endif
213
    leave_critical_section(mode);
214
}
215
 
25 theseven 216
void cwrite(unsigned int consoles, const char* string, size_t length)
217
{
218
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
85 theseven 219
#ifdef HAVE_LCD
257 theseven 220
    if (consoles & 1) lcdconsole_write(string, length, LCDCONSOLE_FGCOLOR, LCDCONSOLE_BGCOLOR);
85 theseven 221
#endif
222
#ifdef HAVE_USB
25 theseven 223
    if (consoles & 2) dbgconsole_write(string, length);
85 theseven 224
#endif
684 theseven 225
#ifdef HAVE_UART
226
    if (consoles & 4) uart_write(string, length);
227
#endif
25 theseven 228
    mutex_unlock(&console_mutex);
229
}
230
 
704 theseven 231
void cswrite(unsigned int consoles, const char* string, size_t length)
232
{
233
    uint32_t mode = enter_critical_section();
234
#ifdef HAVE_LCD
235
    if (consoles & 1) lcdconsole_write_noblit(string, length, LCDCONSOLE_FGCOLOR, LCDCONSOLE_BGCOLOR);
236
#endif
237
#ifdef HAVE_USB
238
    if (consoles & 2) dbgconsole_swrite(string, length);
239
#endif
240
#ifdef HAVE_UART
241
    if (consoles & 4) uart_swrite(string, length);
242
#endif
243
    leave_critical_section(mode);
244
}
245
 
2 theseven 246
void cflush(unsigned int consoles)
247
{
14 theseven 248
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
85 theseven 249
#ifdef HAVE_LCD
14 theseven 250
    if (consoles & 1) lcdconsole_update();
85 theseven 251
#endif
14 theseven 252
    mutex_unlock(&console_mutex);
2 theseven 253
}
26 theseven 254
 
704 theseven 255
void csflush(unsigned int consoles)
256
{
257
    uint32_t mode = enter_critical_section();
258
#ifdef HAVE_LCD
259
    if (consoles & 1) lcdconsole_supdate();
260
#endif
261
    leave_critical_section(mode);
262
}
263
 
867 user890104 264
static inline int cread_unlock_return(unsigned int rc)
265
{
266
    mutex_unlock(&console_readmutex);
267
    return rc;
268
}
269
 
26 theseven 270
int cgetc(unsigned int consoles, int timeout)
271
{
272
    int result;
273
    mutex_lock(&console_readmutex, TIMEOUT_BLOCK);
85 theseven 274
#ifdef HAVE_USB
867 user890104 275
    if ((consoles & 2) && (result = dbgconsole_getc(timeout)) != -1) return cread_unlock_return(result);
85 theseven 276
#endif
684 theseven 277
#ifdef HAVE_UART
867 user890104 278
    if ((consoles & 4) && (result = uart_getc(timeout)) != -1) return cread_unlock_return(result);
684 theseven 279
#endif
867 user890104 280
    return cread_unlock_return(-1);
26 theseven 281
}
282
 
283
int cread(unsigned int consoles, char* buffer, size_t length, int timeout)
284
{
285
    int result;
286
    mutex_lock(&console_readmutex, TIMEOUT_BLOCK);
85 theseven 287
#ifdef HAVE_USB
867 user890104 288
    if ((consoles & 2) && (result = dbgconsole_read(buffer, length, timeout))) return cread_unlock_return(result);
85 theseven 289
#endif
684 theseven 290
#ifdef HAVE_UART
867 user890104 291
    if ((consoles & 4) && (result = uart_read(buffer, length, timeout))) return cread_unlock_return(result);
684 theseven 292
#endif
867 user890104 293
    return cread_unlock_return(-1);
26 theseven 294
}
295
 
296
void creada(unsigned int consoles, char* buffer, size_t length, int timeout)
297
{
298
    int result;
299
    mutex_lock(&console_readmutex, TIMEOUT_BLOCK);
300
    while (length)
301
    {
684 theseven 302
        if (
85 theseven 303
#ifdef HAVE_USB
684 theseven 304
            ((consoles & 2) && (result = dbgconsole_read(buffer, length, timeout)))
305
#else
306
            false
307
#endif
308
            ||
309
#ifdef HAVE_UART
310
            ((consoles & 4) && (result = uart_read(buffer, length, timeout)))
311
#else
312
            false
313
#endif
314
            )
26 theseven 315
        {
316
            buffer = &buffer[result];
317
            length -= result;
318
        }
319
    }
214 theseven 320
    mutex_unlock(&console_readmutex);
26 theseven 321
}