2002-03-28 15:09:10 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2002-08-12 12:44:18 +00:00
|
|
|
* Copyright (C) 2002 by Alan Korr & Nick Robinson
|
2002-03-28 15:09:10 +00:00
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2002-03-28 15:09:10 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2008-12-25 01:46:16 +00:00
|
|
|
#include <stdio.h>
|
2002-08-12 12:44:18 +00:00
|
|
|
#include <stdlib.h>
|
2008-12-25 01:46:16 +00:00
|
|
|
#include <stdarg.h>
|
2002-08-12 12:44:18 +00:00
|
|
|
#include "button.h"
|
|
|
|
#include "config.h"
|
2005-02-02 21:47:43 +00:00
|
|
|
#include "cpu.h"
|
2002-08-12 12:44:18 +00:00
|
|
|
#include "system.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "lcd.h"
|
2002-04-21 21:50:00 +00:00
|
|
|
#include "serial.h"
|
2008-12-25 01:46:16 +00:00
|
|
|
#include "iap.h"
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2011-04-23 08:23:07 +00:00
|
|
|
#if defined(IPOD_ACCESSORY_PROTOCOL)
|
2008-12-25 01:46:16 +00:00
|
|
|
static int autobaud = 0;
|
2011-04-23 09:12:28 +00:00
|
|
|
|
|
|
|
static void set_bitrate(unsigned int rate)
|
|
|
|
{
|
|
|
|
unsigned int divisor;
|
|
|
|
|
|
|
|
divisor = 24000000L / rate / 16;
|
|
|
|
SER0_LCR = 0x80; /* Divisor latch enable */
|
|
|
|
SER0_DLL = (divisor >> 0) & 0xFF;
|
|
|
|
SER0_LCR = 0x03; /* Divisor latch disable, 8-N-1 */
|
|
|
|
}
|
|
|
|
|
2008-12-25 01:46:16 +00:00
|
|
|
void serial_setup (void)
|
|
|
|
{
|
|
|
|
int tmp;
|
2008-12-25 03:23:22 +00:00
|
|
|
|
2011-02-12 21:28:23 +00:00
|
|
|
#if defined(IPOD_COLOR) || defined(IPOD_4G)
|
2008-12-25 01:46:16 +00:00
|
|
|
/* Route the Tx/Rx pins. 4G Ipod??? */
|
|
|
|
outl(0x70000018, inl(0x70000018) & ~0xc00);
|
2011-02-12 21:28:23 +00:00
|
|
|
#elif defined(IPOD_NANO) || defined(IPOD_VIDEO)
|
2008-12-25 01:46:16 +00:00
|
|
|
/* Route the Tx/Rx pins. 5G Ipod */
|
|
|
|
(*(volatile unsigned long *)(0x7000008C)) &= ~0x0C;
|
|
|
|
GPO32_ENABLE &= ~0x0C;
|
|
|
|
#endif
|
2008-12-25 03:23:22 +00:00
|
|
|
|
2008-12-25 01:46:16 +00:00
|
|
|
DEV_EN = DEV_EN | DEV_SER0;
|
|
|
|
CPU_HI_INT_DIS = SER0_MASK;
|
|
|
|
|
|
|
|
DEV_RS |= DEV_SER0;
|
|
|
|
sleep(1);
|
|
|
|
DEV_RS &= ~DEV_SER0;
|
|
|
|
|
|
|
|
SER0_LCR = 0x80; /* Divisor latch enable */
|
|
|
|
SER0_DLM = 0x00;
|
|
|
|
SER0_LCR = 0x03; /* Divisor latch disable, 8-N-1 */
|
|
|
|
SER0_IER = 0x01;
|
|
|
|
|
|
|
|
SER0_FCR = 0x07; /* Tx+Rx FIFO reset and FIFO enable */
|
|
|
|
|
|
|
|
CPU_INT_EN |= HI_MASK;
|
|
|
|
CPU_HI_INT_EN |= SER0_MASK;
|
|
|
|
tmp = SER0_RBR;
|
|
|
|
|
|
|
|
serial_bitrate(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void serial_bitrate(int rate)
|
|
|
|
{
|
|
|
|
if(rate == 0)
|
|
|
|
{
|
2009-01-11 18:13:58 +00:00
|
|
|
autobaud = 2;
|
2011-04-23 09:12:28 +00:00
|
|
|
set_bitrate(115200);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
autobaud = 0;
|
|
|
|
set_bitrate(rate);
|
2008-12-25 01:46:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int tx_rdy(void)
|
|
|
|
{
|
|
|
|
if((SER0_LSR & 0x20))
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-21 17:36:18 +00:00
|
|
|
static int rx_rdy(void)
|
2008-12-25 01:46:16 +00:00
|
|
|
{
|
|
|
|
if((SER0_LSR & 0x1))
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tx_writec(unsigned char c)
|
|
|
|
{
|
|
|
|
SER0_THR =(int) c;
|
|
|
|
}
|
|
|
|
|
2011-12-21 17:36:18 +00:00
|
|
|
static unsigned char rx_readc(void)
|
2008-12-25 01:46:16 +00:00
|
|
|
{
|
|
|
|
return (SER0_RBR & 0xFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SERIAL0(void)
|
|
|
|
{
|
|
|
|
static int badbaud = 0;
|
|
|
|
static bool newpkt = true;
|
|
|
|
char temp;
|
2008-12-25 03:23:22 +00:00
|
|
|
|
2008-12-25 01:46:16 +00:00
|
|
|
while(rx_rdy())
|
|
|
|
{
|
2009-01-11 18:13:58 +00:00
|
|
|
temp = rx_readc();
|
|
|
|
if (newpkt && autobaud > 0)
|
|
|
|
{
|
|
|
|
if (autobaud == 1)
|
|
|
|
{
|
|
|
|
switch (temp)
|
|
|
|
{
|
|
|
|
case 0xFF:
|
|
|
|
case 0x55:
|
|
|
|
break;
|
|
|
|
case 0xFC:
|
2011-04-23 09:12:28 +00:00
|
|
|
set_bitrate(19200);
|
2009-01-11 18:13:58 +00:00
|
|
|
temp = 0xFF;
|
|
|
|
break;
|
|
|
|
case 0xE0:
|
2011-04-23 09:12:28 +00:00
|
|
|
set_bitrate(9600);
|
2009-01-11 18:13:58 +00:00
|
|
|
temp = 0xFF;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
badbaud++;
|
|
|
|
if (badbaud >= 6) /* Switch baud detection mode */
|
|
|
|
{
|
|
|
|
autobaud = 2;
|
2011-04-23 09:12:28 +00:00
|
|
|
set_bitrate(115200);
|
2009-01-11 18:13:58 +00:00
|
|
|
badbaud = 0;
|
|
|
|
} else {
|
2011-04-23 09:12:28 +00:00
|
|
|
set_bitrate(57600);
|
2009-01-11 18:13:58 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (temp)
|
|
|
|
{
|
|
|
|
case 0xFF:
|
|
|
|
case 0x55:
|
|
|
|
break;
|
|
|
|
case 0xFE:
|
2011-04-23 09:12:28 +00:00
|
|
|
set_bitrate(57600);
|
2009-01-11 18:13:58 +00:00
|
|
|
temp = 0xFF;
|
|
|
|
break;
|
|
|
|
case 0xFC:
|
2011-04-23 09:12:28 +00:00
|
|
|
set_bitrate(38400);
|
2009-01-11 18:13:58 +00:00
|
|
|
temp = 0xFF;
|
|
|
|
break;
|
|
|
|
case 0xE0:
|
2011-04-23 09:12:28 +00:00
|
|
|
set_bitrate(19200);
|
2009-01-11 18:13:58 +00:00
|
|
|
temp = 0xFF;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
badbaud++;
|
|
|
|
if (badbaud >= 6) /* Switch baud detection */
|
|
|
|
{
|
|
|
|
autobaud = 1;
|
2011-04-23 09:12:28 +00:00
|
|
|
set_bitrate(57600);
|
2009-01-11 18:13:58 +00:00
|
|
|
badbaud = 0;
|
|
|
|
} else {
|
2011-04-23 09:12:28 +00:00
|
|
|
set_bitrate(115200);
|
2009-01-11 18:13:58 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool pkt = iap_getc(temp);
|
2010-01-02 16:03:30 +00:00
|
|
|
if(newpkt && !pkt)
|
2009-01-11 18:13:58 +00:00
|
|
|
autobaud = 0; /* Found good baud */
|
|
|
|
newpkt = pkt;
|
2008-12-25 01:46:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-09-22 02:17:08 +00:00
|
|
|
void dprintf(const char * str, ... )
|
|
|
|
{
|
|
|
|
char dprintfbuff[256];
|
2008-12-25 01:46:16 +00:00
|
|
|
char * ptr;
|
2008-12-25 03:23:22 +00:00
|
|
|
|
2007-09-22 02:17:08 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, str);
|
|
|
|
|
|
|
|
ptr = dprintfbuff;
|
|
|
|
vsnprintf(ptr,sizeof(dprintfbuff),str,ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
2008-12-25 01:46:16 +00:00
|
|
|
serial_tx((unsigned char *)ptr);
|
2007-09-22 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void serial_tx(const unsigned char * buf)
|
|
|
|
{
|
|
|
|
/*Tx*/
|
|
|
|
for(;;) {
|
2008-02-19 14:15:59 +00:00
|
|
|
if(tx_rdy()) {
|
2007-09-22 02:17:08 +00:00
|
|
|
if(*buf == '\0')
|
|
|
|
return;
|
|
|
|
if(*buf == '\n')
|
2008-02-19 14:15:59 +00:00
|
|
|
tx_writec('\r');
|
|
|
|
tx_writec(*buf);
|
2007-09-22 02:17:08 +00:00
|
|
|
buf++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|