Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
687 theseven 1
//
2
//
3
//    Copyright 2011 TheSeven
4
//
5
//
6
//    This file is part of emCORE.
7
//
8
//    emCORE 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
//    emCORE 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 emCORE.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
24
#include "global.h"
25
#include "uart.h"
26
#include "s5l8702.h"
27
#include "timer.h"
28
 
29
 
30
void uart_init()
31
{
32
    clockgate_enable(41, true);
33
    ULCON = 3;
34
    UCON = 0x405;
35
    UFCON = 7;
36
    UMCON = 0;
37
    uart_set_baud(250000);
38
}
39
 
40
void uart_set_baud(int baud)
41
{
42
    UBRDIV = (750000 / baud) - 1;
43
}
44
 
45
void uart_putc(char byte)
46
{
47
    while (UFSTAT & BIT(9)) sleep(100);
48
    UTXH = byte;
49
}
50
 
704 theseven 51
void uart_sputc(char byte)
52
{
53
    while (UFSTAT & BIT(9));
54
    UTXH = byte;
55
}
56
 
687 theseven 57
void uart_puts(const char* string)
58
{
59
    char byte;
60
    while (byte = *string++) uart_putc(byte);
61
}
62
 
704 theseven 63
void uart_sputs(const char* string)
64
{
65
    char byte;
66
    while (byte = *string++) uart_sputc(byte);
67
}
68
 
687 theseven 69
void uart_write(const char* string, size_t length)
704 theseven 70
 
687 theseven 71
{
72
    while (length--) uart_putc(*string++);
73
}
74
 
704 theseven 75
void uart_swrite(const char* string, size_t length)
76
 
77
{
78
    while (length--) uart_sputc(*string++);
79
}
80
 
687 theseven 81
int uart_getc(int timeout)
82
{
83
    int byte = -1;
84
    long starttime = USEC_TIMER;
85
    while (!(UFSTAT & BITRANGE(0, 3)) && !TIMEOUT_EXPIRED(starttime, timeout)) sleep(100);
86
    if (UFSTAT & BITRANGE(0, 3)) byte = URXH;
87
    return byte;
88
}
89
 
90
int uart_read(char* string, size_t length, int timeout)
91
{
92
    int count = 0;
93
    long starttime = USEC_TIMER;
94
    while (length && !TIMEOUT_EXPIRED(starttime, timeout))
95
    {
96
        if (UFSTAT & BITRANGE(0, 3))
97
        {
98
            *string++ = URXH;
99
            length--;
100
            count++;
101
        }
102
        else sleep(100);
103
    }
104
    return count;
105
}