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
//
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 "console.h"
26
#include "lcdconsole.h"
25 theseven 27
#include "usb/dbgconsole.h"
2 theseven 28
#include "format.h"
14 theseven 29
#include "thread.h"
2 theseven 30
#include <stdio.h>
31
#include <stdarg.h>
32
#include <stdbool.h>
33
#include <limits.h>
34
 
35
 
36
struct for_cprintf
37
{
38
    unsigned int consoles;
39
    size_t bytes;
40
};
41
 
42
 
14 theseven 43
struct mutex console_mutex;
26 theseven 44
struct mutex console_readmutex;
14 theseven 45
 
46
 
47
void console_init()
48
{
49
    mutex_init(&console_mutex);
26 theseven 50
    mutex_init(&console_readmutex);
14 theseven 51
}
52
 
53
void cputc_internal(unsigned int consoles, char string) ICODE_ATTR;
54
void cputc_internal(unsigned int consoles, char string)
55
{
56
    if (consoles & 1) lcdconsole_putc(string, 0, -1);
25 theseven 57
    if (consoles & 2) dbgconsole_putc(string);
14 theseven 58
}
59
 
2 theseven 60
static int cprfunc(void* ptr, unsigned char letter)
61
{
62
    struct for_cprintf* pr = (struct for_cprintf*)ptr;
14 theseven 63
    cputc_internal(pr->consoles, letter);
2 theseven 64
    pr->bytes++;
65
    return true;
66
}
67
 
68
int cprintf(unsigned int consoles, const char* fmt, ...)
69
{
70
    va_list ap;
71
    struct for_cprintf pr;
72
 
73
    pr.consoles = consoles;
74
    pr.bytes = 0;
75
 
14 theseven 76
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
2 theseven 77
    va_start(ap, fmt);
78
    format(cprfunc, &pr, fmt, ap);
79
    va_end(ap);
14 theseven 80
    mutex_unlock(&console_mutex);
2 theseven 81
 
82
    return pr.bytes;
83
}
84
 
85
int cvprintf(unsigned int consoles, const char* fmt, va_list ap)
86
{
87
    struct for_cprintf pr;
88
 
89
    pr.consoles = consoles;
90
    pr.bytes = 0;
91
 
14 theseven 92
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
2 theseven 93
    format(cprfunc, &pr, fmt, ap);
14 theseven 94
    mutex_unlock(&console_mutex);
2 theseven 95
 
96
    return pr.bytes;
97
}
98
 
99
void cputc(unsigned int consoles, char string)
100
{
14 theseven 101
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
102
    cputc_internal(consoles, string);
103
    mutex_unlock(&console_mutex);
2 theseven 104
}
105
 
106
void cputs(unsigned int consoles, const char* string)
107
{
14 theseven 108
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
109
    if (consoles & 1) lcdconsole_puts(string, 0, -1);
25 theseven 110
    if (consoles & 2) dbgconsole_puts(string);
14 theseven 111
    mutex_unlock(&console_mutex);
2 theseven 112
}
113
 
25 theseven 114
void cwrite(unsigned int consoles, const char* string, size_t length)
115
{
116
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
117
    if (consoles & 1) lcdconsole_write(string, length, 0, -1);
118
    if (consoles & 2) dbgconsole_write(string, length);
119
    mutex_unlock(&console_mutex);
120
}
121
 
2 theseven 122
void cflush(unsigned int consoles)
123
{
14 theseven 124
    mutex_lock(&console_mutex, TIMEOUT_BLOCK);
125
    if (consoles & 1) lcdconsole_update();
126
    mutex_unlock(&console_mutex);
2 theseven 127
}
26 theseven 128
 
129
int cgetc(unsigned int consoles, int timeout)
130
{
131
    int result;
132
    mutex_lock(&console_readmutex, TIMEOUT_BLOCK);
133
    if ((consoles & 2) && (result = dbgconsole_getc(timeout)) != -1) return result;
134
    mutex_unlock(&console_mutex);
135
}
136
 
137
int cread(unsigned int consoles, char* buffer, size_t length, int timeout)
138
{
139
    int result;
140
    mutex_lock(&console_readmutex, TIMEOUT_BLOCK);
141
    if ((consoles & 2) && (result = dbgconsole_read(buffer, length, timeout))) return result;
142
    mutex_unlock(&console_mutex);
143
}
144
 
145
void creada(unsigned int consoles, char* buffer, size_t length, int timeout)
146
{
147
    int result;
148
    mutex_lock(&console_readmutex, TIMEOUT_BLOCK);
149
    while (length)
150
    {
151
        if (length && (consoles & 2) && (result = dbgconsole_read(buffer, length, timeout)))
152
        {
153
            buffer = &buffer[result];
154
            length -= result;
155
        }
156
    }
157
    mutex_unlock(&console_mutex);
158
}