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
257 theseven 59
    if (consoles & 1) lcdconsole_putc(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
 
77
int cprintf(unsigned int consoles, const char* fmt, ...)
78
{
79
    va_list ap;
80
    struct for_cprintf pr;
81
 
82
    pr.consoles = consoles;
83
    pr.bytes = 0;
84
 
14 theseven 85
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
2 theseven 86
    va_start(ap, fmt);
87
    format(cprfunc, &pr, fmt, ap);
88
    va_end(ap);
14 theseven 89
    mutex_unlock(&console_mutex);
2 theseven 90
 
91
    return pr.bytes;
92
}
93
 
94
int cvprintf(unsigned int consoles, const char* fmt, va_list ap)
95
{
96
    struct for_cprintf pr;
97
 
98
    pr.consoles = consoles;
99
    pr.bytes = 0;
100
 
14 theseven 101
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
2 theseven 102
    format(cprfunc, &pr, fmt, ap);
14 theseven 103
    mutex_unlock(&console_mutex);
2 theseven 104
 
105
    return pr.bytes;
106
}
107
 
108
void cputc(unsigned int consoles, char string)
109
{
14 theseven 110
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
111
    cputc_internal(consoles, string);
112
    mutex_unlock(&console_mutex);
2 theseven 113
}
114
 
115
void cputs(unsigned int consoles, const char* string)
116
{
14 theseven 117
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
85 theseven 118
#ifdef HAVE_LCD
257 theseven 119
    if (consoles & 1) lcdconsole_puts(string, LCDCONSOLE_FGCOLOR, LCDCONSOLE_BGCOLOR);
85 theseven 120
#endif
121
#ifdef HAVE_USB
25 theseven 122
    if (consoles & 2) dbgconsole_puts(string);
85 theseven 123
#endif
684 theseven 124
#ifdef HAVE_UART
125
    if (consoles & 4) uart_puts(string);
126
#endif
14 theseven 127
    mutex_unlock(&console_mutex);
2 theseven 128
}
129
 
25 theseven 130
void cwrite(unsigned int consoles, const char* string, size_t length)
131
{
132
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
85 theseven 133
#ifdef HAVE_LCD
257 theseven 134
    if (consoles & 1) lcdconsole_write(string, length, LCDCONSOLE_FGCOLOR, LCDCONSOLE_BGCOLOR);
85 theseven 135
#endif
136
#ifdef HAVE_USB
25 theseven 137
    if (consoles & 2) dbgconsole_write(string, length);
85 theseven 138
#endif
684 theseven 139
#ifdef HAVE_UART
140
    if (consoles & 4) uart_write(string, length);
141
#endif
25 theseven 142
    mutex_unlock(&console_mutex);
143
}
144
 
2 theseven 145
void cflush(unsigned int consoles)
146
{
14 theseven 147
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
85 theseven 148
#ifdef HAVE_LCD
14 theseven 149
    if (consoles & 1) lcdconsole_update();
85 theseven 150
#endif
14 theseven 151
    mutex_unlock(&console_mutex);
2 theseven 152
}
26 theseven 153
 
154
int cgetc(unsigned int consoles, int timeout)
155
{
156
    int result;
157
    mutex_lock(&console_readmutex, TIMEOUT_BLOCK);
85 theseven 158
#ifdef HAVE_USB
26 theseven 159
    if ((consoles & 2) && (result = dbgconsole_getc(timeout)) != -1) return result;
85 theseven 160
#endif
684 theseven 161
#ifdef HAVE_UART
162
    if ((consoles & 4) && (result = uart_getc(timeout)) != -1) return result;
163
#endif
214 theseven 164
    mutex_unlock(&console_readmutex);
26 theseven 165
}
166
 
167
int cread(unsigned int consoles, char* buffer, size_t length, int timeout)
168
{
169
    int result;
170
    mutex_lock(&console_readmutex, TIMEOUT_BLOCK);
85 theseven 171
#ifdef HAVE_USB
26 theseven 172
    if ((consoles & 2) && (result = dbgconsole_read(buffer, length, timeout))) return result;
85 theseven 173
#endif
684 theseven 174
#ifdef HAVE_UART
175
    if ((consoles & 2) && (result = uart_read(buffer, length, timeout))) return result;
176
#endif
214 theseven 177
    mutex_unlock(&console_readmutex);
26 theseven 178
}
179
 
180
void creada(unsigned int consoles, char* buffer, size_t length, int timeout)
181
{
182
    int result;
183
    mutex_lock(&console_readmutex, TIMEOUT_BLOCK);
184
    while (length)
185
    {
684 theseven 186
        if (
85 theseven 187
#ifdef HAVE_USB
684 theseven 188
            ((consoles & 2) && (result = dbgconsole_read(buffer, length, timeout)))
189
#else
190
            false
191
#endif
192
            ||
193
#ifdef HAVE_UART
194
            ((consoles & 4) && (result = uart_read(buffer, length, timeout)))
195
#else
196
            false
197
#endif
198
            )
26 theseven 199
        {
200
            buffer = &buffer[result];
201
            length -= result;
202
        }
203
    }
214 theseven 204
    mutex_unlock(&console_readmutex);
26 theseven 205
}