slow - but working - IRQ based uart/button driver.
[Rockbox.git] / firmware / target / arm / olympus / mrobe-500 / uart-mr500.c
blob66e59eaaaca0d1be32269947716bf5e6b0abac5d
1 /*
2 * (C) Copyright 2007 Catalin Patulea <cat@vv.carleton.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17 * MA 02111-1307 USA
20 #include "config.h"
21 #include "cpu.h"
22 #include "system.h"
24 /* UART 0/1 */
26 #define CONFIG_UART_BRSR 87
27 #define MAX_UART_BUFFER 32
28 static unsigned char uart1buffer[MAX_UART_BUFFER];
29 int uart1read = 0, uart1write = 0, uart1count = 0;
31 void do_checksums(char *data, int len, char *xor, char *add)
33 int i;
34 *xor = data[0];
35 *add = data[0];
36 for(i=1;i<len;i++)
38 *xor ^= data[i];
39 *add += data[i];
43 void uart_init(void)
45 // 8-N-1
46 IO_UART1_MSR=0x8000;
47 IO_UART1_BRSR=CONFIG_UART_BRSR;
48 IO_UART1_RFCR = 0x8000;
49 /* gio 27 is input, uart1 rx
50 gio 28 is output, uart1 tx */
51 IO_GIO_DIR1 |= (1<<11); /* gio 27 */
52 IO_GIO_DIR1 &= ~(1<<12); /* gio 28 */
54 /* init the recieve buffer */
55 uart1read = 0;
56 uart1write = 0;
57 uart1count = 0;
59 /* Enable the interrupt */
60 IO_INTC_EINT0 |= (1<<IRQ_UART1);
63 void uartPutc(char ch) {
64 // Wait for room in FIFO
65 while ((IO_UART1_TFCR & 0x3f) >= 0x20);
67 // Write character
68 IO_UART1_DTRR=ch;
71 // Unsigned integer to ASCII hexadecimal conversion
72 void uartPutHex(unsigned int n) {
73 unsigned int i;
75 for (i = 8; i != 0; i--) {
76 unsigned int digit = n >> 28;
77 uartPutc(digit >= 10 ? digit - 10 + 'A' : digit + '0');
78 n <<= 4;
82 void uartPuts(const char *str) {
83 char ch;
84 while ((ch = *str++) != '\0') {
85 uartPutc(ch);
89 void uartGets(char *str, unsigned int size) {
90 for (;;) {
91 char ch;
93 // Wait for FIFO to contain something
94 while ((IO_UART1_RFCR & 0x3f) == 0);
96 // Read character
97 ch = (char)IO_UART1_DTRR;
99 // Echo character back
100 IO_UART1_DTRR=ch;
102 // If CR, also echo LF, null-terminate, and return
103 if (ch == '\r') {
104 IO_UART1_DTRR='\n';
105 if (size) {
106 *str++ = '\0';
108 return;
111 // Append to buffer
112 if (size) {
113 *str++ = ch;
114 --size;
119 int uartPollch(unsigned int ticks) {
120 while (ticks--) {
121 if (IO_UART1_RFCR & 0x3f) {
122 return IO_UART1_DTRR & 0xff;
126 return -1;
129 bool uartAvailable(void)
131 return uart1count > 0;
134 void uart1_heartbeat(void)
136 char data[5] = {0x11, 0x30, 0x11^0x30, 0x11+0x30, '\0'};
137 uartPuts(data);
140 void uartSendData(char* data, int len)
142 int i;
143 for(i=0;i<len;i++)
144 uartPutc(data[i]);
147 bool uart1_getch(char *c)
149 if (uart1count > 0)
151 *c = uart1buffer[uart1read];
152 uart1read = (uart1read+1) % MAX_UART_BUFFER;
153 uart1count--;
154 return true;
156 return false;
159 /* UART1 receive intterupt handler */
160 void UART1(void)
162 if (IO_UART1_RFCR & 0x3f)
164 if (uart1count >= MAX_UART_BUFFER)
165 panicf("UART1 buffer overflow");
166 uart1buffer[uart1write] = IO_UART1_DTRR & 0xff;
167 uart1write = (uart1write+1) % MAX_UART_BUFFER;
168 uart1count++;
171 IO_INTC_IRQ0 = (1<<IRQ_UART1);