code clean up!
[my-code/arm.git] / betty / uart.c
1 /*
2  * uart.c - uart0/1 api
3  *
4  * author: hackbard@hackdaworld.org
5  *
6  */
7
8 #include "uart.h"
9
10 /*
11  * functions
12  */
13
14 void uart0_init(void) {
15
16         /* select pins 0.0 and 0.1 as tx and rx */
17         PINSEL0=(PINSEL0&~(0xf))|0x05;
18
19         /* configure uart 0 */
20         UART0_FCR=0x07;                 // enable fifo
21         UART0_LCR=0x83;                 // set dlab + word length
22         UART0_DLL=0x04;                 // br: 115200
23         UART0_DLM=0x00; 
24         UART0_LCR=0x03;                 // unset dlab
25 }
26
27 void uart0_send_string(char *txbuf) {
28
29         int i;
30
31         i=0;
32
33         while(txbuf[i]) {
34                 UART0_THR=txbuf[i++];
35                 /* flush if tx buffer maximum reached */
36                 if(!(i%16))
37                         while(!(UART0_LSR&(1<<6)))
38                                 continue;
39         }
40         
41         /* flush if \n and \r do not fit in the tx buffer */
42         if(i>14)
43                 while(!(UART0_LSR&(1<<6)))
44                         continue;
45
46         UART0_THR='\n';
47         UART0_THR='\r';
48
49         /* flush uart0 anyways */
50         while(!(UART0_LSR&(1<<6)))
51                 continue;
52 }
53
54 void uart0_send_buf16(u16 *buf,int len) {
55
56         int i;
57
58         i=0;
59
60         for(i=0;i<len/2;i++) {
61                 if(!(i%8))
62                         while(!(UART0_LSR&(1<<6)))
63                                 continue;
64                 UART0_THR=buf[i]&0xff;
65                 UART0_THR=(buf[i]>>8)&0xff;
66         }
67 }
68
69 void uart0_send_buf32(u32 *buf,int len) {
70
71         int i;
72
73         i=0;
74
75         for(i=0;i<len/4;i++) {
76                 if(!(i%4))
77                         while(!(UART0_LSR&(1<<6)))
78                                 continue;
79                 UART0_THR=buf[i]&0xff;
80                 UART0_THR=(buf[i]>>8)&0xff;
81                 UART0_THR=(buf[i]>>16)&0xff;
82                 UART0_THR=(buf[i]>>24)&0xff;
83         }
84 }
85
86 void uart0_send_byte(u8 send) {
87
88         while(!(UART0_LSR&(1<<5)))
89                 continue;
90
91         UART0_THR=send;
92 }
93
94 u8 uart0_get_byte(void) {
95
96         u8 rx;
97
98         while(!(UART0_LSR&(1<<0)))
99                 continue;
100
101         rx=UART0_RBR;
102
103         return rx;
104 }
105