Rockbox Utility: bump version to 1.2.10.
[kugel-rb.git] / firmware / drivers / serial.c
blobaf0f3abe68efa1d1d62bb617dcabe80c3a5ccf87
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Alan Korr & Nick Robinson
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include "button.h"
25 #include "config.h"
26 #include "cpu.h"
27 #include "system.h"
28 #include "kernel.h"
29 #include "lcd.h"
30 #include "serial.h"
31 #include "iap.h"
33 #if defined(IPOD_ACCESSORY_PROTOCOL)
34 static int autobaud = 0;
36 static void set_bitrate(unsigned int rate)
38 unsigned int divisor;
40 divisor = 24000000L / rate / 16;
41 SER0_LCR = 0x80; /* Divisor latch enable */
42 SER0_DLL = (divisor >> 0) & 0xFF;
43 SER0_LCR = 0x03; /* Divisor latch disable, 8-N-1 */
46 void serial_setup (void)
48 int tmp;
50 #if defined(IPOD_COLOR) || defined(IPOD_4G)
51 /* Route the Tx/Rx pins. 4G Ipod??? */
52 outl(0x70000018, inl(0x70000018) & ~0xc00);
53 #elif defined(IPOD_NANO) || defined(IPOD_VIDEO)
54 /* Route the Tx/Rx pins. 5G Ipod */
55 (*(volatile unsigned long *)(0x7000008C)) &= ~0x0C;
56 GPO32_ENABLE &= ~0x0C;
57 #endif
59 DEV_EN = DEV_EN | DEV_SER0;
60 CPU_HI_INT_DIS = SER0_MASK;
62 DEV_RS |= DEV_SER0;
63 sleep(1);
64 DEV_RS &= ~DEV_SER0;
66 SER0_LCR = 0x80; /* Divisor latch enable */
67 SER0_DLM = 0x00;
68 SER0_LCR = 0x03; /* Divisor latch disable, 8-N-1 */
69 SER0_IER = 0x01;
71 SER0_FCR = 0x07; /* Tx+Rx FIFO reset and FIFO enable */
73 CPU_INT_EN |= HI_MASK;
74 CPU_HI_INT_EN |= SER0_MASK;
75 tmp = SER0_RBR;
77 serial_bitrate(0);
80 void serial_bitrate(int rate)
82 if(rate == 0)
84 autobaud = 2;
85 set_bitrate(115200);
87 else
89 autobaud = 0;
90 set_bitrate(rate);
94 int tx_rdy(void)
96 if((SER0_LSR & 0x20))
97 return 1;
98 else
99 return 0;
102 int rx_rdy(void)
104 if((SER0_LSR & 0x1))
105 return 1;
106 else
107 return 0;
110 void tx_writec(unsigned char c)
112 SER0_THR =(int) c;
115 unsigned char rx_readc(void)
117 return (SER0_RBR & 0xFF);
120 void SERIAL0(void)
122 static int badbaud = 0;
123 static bool newpkt = true;
124 char temp;
126 while(rx_rdy())
128 temp = rx_readc();
129 if (newpkt && autobaud > 0)
131 if (autobaud == 1)
133 switch (temp)
135 case 0xFF:
136 case 0x55:
137 break;
138 case 0xFC:
139 set_bitrate(19200);
140 temp = 0xFF;
141 break;
142 case 0xE0:
143 set_bitrate(9600);
144 temp = 0xFF;
145 break;
146 default:
147 badbaud++;
148 if (badbaud >= 6) /* Switch baud detection mode */
150 autobaud = 2;
151 set_bitrate(115200);
152 badbaud = 0;
153 } else {
154 set_bitrate(57600);
156 continue;
158 } else {
159 switch (temp)
161 case 0xFF:
162 case 0x55:
163 break;
164 case 0xFE:
165 set_bitrate(57600);
166 temp = 0xFF;
167 break;
168 case 0xFC:
169 set_bitrate(38400);
170 temp = 0xFF;
171 break;
172 case 0xE0:
173 set_bitrate(19200);
174 temp = 0xFF;
175 break;
176 default:
177 badbaud++;
178 if (badbaud >= 6) /* Switch baud detection */
180 autobaud = 1;
181 set_bitrate(57600);
182 badbaud = 0;
183 } else {
184 set_bitrate(115200);
186 continue;
190 bool pkt = iap_getc(temp);
191 if(newpkt && !pkt)
192 autobaud = 0; /* Found good baud */
193 newpkt = pkt;
196 #endif
198 void dprintf(const char * str, ... )
200 char dprintfbuff[256];
201 char * ptr;
203 va_list ap;
204 va_start(ap, str);
206 ptr = dprintfbuff;
207 vsnprintf(ptr,sizeof(dprintfbuff),str,ap);
208 va_end(ap);
210 serial_tx((unsigned char *)ptr);
213 void serial_tx(const unsigned char * buf)
215 /*Tx*/
216 for(;;) {
217 if(tx_rdy()) {
218 if(*buf == '\0')
219 return;
220 if(*buf == '\n')
221 tx_writec('\r');
222 tx_writec(*buf);
223 buf++;