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
 
51
void uart_puts(const char* string)
52
{
53
    char byte;
54
    while (byte = *string++) uart_putc(byte);
55
}
56
 
57
void uart_write(const char* string, size_t length)
58
{
59
    while (length--) uart_putc(*string++);
60
}
61
 
62
int uart_getc(int timeout)
63
{
64
    int byte = -1;
65
    long starttime = USEC_TIMER;
66
    while (!(UFSTAT & BITRANGE(0, 3)) && !TIMEOUT_EXPIRED(starttime, timeout)) sleep(100);
67
    if (UFSTAT & BITRANGE(0, 3)) byte = URXH;
68
    return byte;
69
}
70
 
71
int uart_read(char* string, size_t length, int timeout)
72
{
73
    int count = 0;
74
    long starttime = USEC_TIMER;
75
    while (length && !TIMEOUT_EXPIRED(starttime, timeout))
76
    {
77
        if (UFSTAT & BITRANGE(0, 3))
78
        {
79
            *string++ = URXH;
80
            length--;
81
            count++;
82
        }
83
        else sleep(100);
84
    }
85
    return count;
86
}