2007-10-18 05:14:10 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
2007-09-20 09:08:40 +00:00
|
|
|
*
|
2007-10-18 05:14:10 +00:00
|
|
|
* Copyright (C) 2007 Catalin Patulea <cat@vv.carleton.ca>
|
2007-09-20 09:08:40 +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.
|
2007-09-20 09:08:40 +00:00
|
|
|
*
|
2007-10-18 05:14:10 +00:00
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
2007-09-20 09:08:40 +00:00
|
|
|
*
|
2007-10-18 05:14:10 +00:00
|
|
|
****************************************************************************/
|
2007-09-20 09:08:40 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "system.h"
|
2007-11-10 22:12:54 +00:00
|
|
|
#include "string.h"
|
|
|
|
#include "panic.h"
|
2008-09-10 19:03:49 +00:00
|
|
|
#include "uart-target.h"
|
2007-09-20 09:08:40 +00:00
|
|
|
|
2007-10-18 05:14:10 +00:00
|
|
|
#define MAX_UART_BUFFER 31
|
2007-11-10 22:12:54 +00:00
|
|
|
#define SEND_RING_SIZE 256
|
2009-04-14 05:17:03 +00:00
|
|
|
#define RECEIVE_RING_SIZE 20
|
2007-09-20 09:08:40 +00:00
|
|
|
|
2007-11-10 22:12:54 +00:00
|
|
|
char
|
2009-04-14 05:17:03 +00:00
|
|
|
uart1_send_buffer_ring[SEND_RING_SIZE],
|
|
|
|
uart1_receive_buffer_ring[RECEIVE_RING_SIZE];
|
2007-11-10 22:12:54 +00:00
|
|
|
|
2009-04-14 05:17:03 +00:00
|
|
|
static volatile int uart1_send_count, uart1_send_read, uart1_send_write;
|
|
|
|
static volatile int uart1_receive_count, uart1_receive_read, uart1_receive_write;
|
2007-09-20 09:08:40 +00:00
|
|
|
|
2007-10-18 05:14:10 +00:00
|
|
|
void uart_init(void)
|
2007-09-30 08:18:46 +00:00
|
|
|
{
|
2009-12-14 07:00:37 +00:00
|
|
|
/* Setup UART 1 pins:
|
|
|
|
* 27 - input, uart1 rx
|
|
|
|
* 28 - output, uart1 tx */
|
|
|
|
/* 27: input , non-inverted, no-irq, falling edge, no-chat, UART RX */
|
|
|
|
dm320_set_io(27, true, false, false, false, false, 0x01);
|
|
|
|
|
|
|
|
/* 28: output, non-inverted, no-irq, falling edge, no-chat, UART TX */
|
|
|
|
dm320_set_io(28, false, false, false, false, false, 0x01);
|
|
|
|
|
2007-09-20 09:08:40 +00:00
|
|
|
// 8-N-1
|
2009-04-14 05:17:03 +00:00
|
|
|
IO_UART1_MSR = 0xC400;
|
2009-03-24 16:40:31 +00:00
|
|
|
IO_UART1_BRSR = 0x0057;
|
2009-04-14 05:17:03 +00:00
|
|
|
IO_UART1_RFCR = 0x8020; /* Trigger later */
|
|
|
|
IO_UART1_TFCR = 0x0000; /* Trigger level */
|
2007-10-18 05:14:10 +00:00
|
|
|
|
2009-04-14 05:17:03 +00:00
|
|
|
/* init the receive buffer */
|
|
|
|
uart1_receive_count=0;
|
|
|
|
uart1_receive_read=0;
|
|
|
|
uart1_receive_write=0;
|
|
|
|
|
|
|
|
/* init the send buffer */
|
|
|
|
uart1_send_count=0;
|
|
|
|
uart1_send_read=0;
|
|
|
|
uart1_send_write=0;
|
2007-11-10 22:12:54 +00:00
|
|
|
|
2007-09-30 08:18:46 +00:00
|
|
|
/* Enable the interrupt */
|
2008-04-24 20:08:28 +00:00
|
|
|
IO_INTC_EINT0 |= INTR_EINT0_UART1;
|
2007-09-20 09:08:40 +00:00
|
|
|
}
|
|
|
|
|
2009-04-14 05:17:03 +00:00
|
|
|
/* This function is not interrupt driven */
|
2007-10-01 05:42:12 +00:00
|
|
|
void uart1_putc(char ch)
|
|
|
|
{
|
2009-04-14 05:17:03 +00:00
|
|
|
/* Wait for the interupt driven puts to finish */
|
|
|
|
while(uart1_send_count>0);
|
|
|
|
|
2007-10-01 05:42:12 +00:00
|
|
|
/* Wait for room in FIFO */
|
2007-09-23 23:08:39 +00:00
|
|
|
while ((IO_UART1_TFCR & 0x3f) >= 0x20);
|
2007-09-20 09:08:40 +00:00
|
|
|
|
2007-10-01 05:42:12 +00:00
|
|
|
/* Write character */
|
2007-09-23 23:08:39 +00:00
|
|
|
IO_UART1_DTRR=ch;
|
2007-09-20 09:08:40 +00:00
|
|
|
}
|
|
|
|
|
2007-11-15 06:44:35 +00:00
|
|
|
void uart1_puts(const char *str, int size)
|
2007-10-01 05:42:12 +00:00
|
|
|
{
|
2009-04-14 05:17:03 +00:00
|
|
|
if(size>SEND_RING_SIZE)
|
|
|
|
panicf("Too much data passed to uart1_puts");
|
|
|
|
|
|
|
|
/* Wait for the previous transfer to finish */
|
|
|
|
while(uart1_send_count>0);
|
|
|
|
|
|
|
|
memcpy(uart1_send_buffer_ring, str, size);
|
|
|
|
|
|
|
|
/* Disable interrupt while modifying the pointers */
|
|
|
|
IO_INTC_EINT0 &= ~INTR_EINT0_UART1;
|
|
|
|
|
|
|
|
uart1_send_count=size;
|
|
|
|
uart1_send_read=0;
|
|
|
|
|
|
|
|
/* prime the hardware buffer */
|
|
|
|
while(((IO_UART1_TFCR & 0x3f) < 0x20) && (uart1_send_count > 0))
|
2007-11-15 06:44:35 +00:00
|
|
|
{
|
2009-04-14 05:17:03 +00:00
|
|
|
IO_UART1_DTRR=uart1_send_buffer_ring[uart1_send_read++];
|
|
|
|
uart1_send_count--;
|
2007-09-20 09:08:40 +00:00
|
|
|
}
|
2009-04-14 05:17:03 +00:00
|
|
|
|
|
|
|
/* Enable interrupt */
|
|
|
|
IO_INTC_EINT0 |= INTR_EINT0_UART1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uart1_clear_queue(void)
|
|
|
|
{
|
|
|
|
/* Disable interrupt while modifying the pointers */
|
|
|
|
IO_INTC_EINT0 &= ~INTR_EINT0_UART1;
|
|
|
|
uart1_receive_write=0;
|
|
|
|
uart1_receive_count=0;
|
|
|
|
uart1_receive_read=0;
|
|
|
|
/* Enable interrupt */
|
|
|
|
IO_INTC_EINT0 |= INTR_EINT0_UART1;
|
2007-09-20 09:08:40 +00:00
|
|
|
}
|
|
|
|
|
2007-11-10 22:12:54 +00:00
|
|
|
/* This function returns the number of bytes left in the queue after a read is done (negative if fail)*/
|
2009-04-14 05:17:03 +00:00
|
|
|
int uart1_gets_queue(char *str, int size)
|
2007-10-01 05:42:12 +00:00
|
|
|
{
|
2009-04-14 05:17:03 +00:00
|
|
|
/* Disable the interrupt while modifying the pointers */
|
2008-04-24 20:08:28 +00:00
|
|
|
IO_INTC_EINT0 &= ~INTR_EINT0_UART1;
|
2007-11-15 06:44:35 +00:00
|
|
|
int retval;
|
|
|
|
|
2009-04-14 05:17:03 +00:00
|
|
|
if(uart1_receive_count<size)
|
2007-09-30 08:18:46 +00:00
|
|
|
{
|
2007-11-15 06:44:35 +00:00
|
|
|
retval= -1;
|
|
|
|
}
|
2007-11-10 22:12:54 +00:00
|
|
|
else
|
|
|
|
{
|
2009-04-14 05:17:03 +00:00
|
|
|
if(uart1_receive_read+size<=RECEIVE_RING_SIZE)
|
2007-11-15 06:44:35 +00:00
|
|
|
{
|
2009-04-14 05:17:03 +00:00
|
|
|
memcpy(str,uart1_receive_buffer_ring+uart1_receive_read,size);
|
|
|
|
|
|
|
|
uart1_receive_read+=size;
|
2007-11-15 06:44:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-04-14 05:17:03 +00:00
|
|
|
int tempcount=(RECEIVE_RING_SIZE-uart1_receive_read);
|
|
|
|
memcpy(str,uart1_receive_buffer_ring+uart1_receive_read,tempcount);
|
|
|
|
memcpy(str+tempcount,uart1_receive_buffer_ring,size-tempcount);
|
|
|
|
|
|
|
|
uart1_receive_read=size-tempcount;
|
2007-11-15 06:44:35 +00:00
|
|
|
}
|
|
|
|
|
2009-04-14 05:17:03 +00:00
|
|
|
uart1_receive_count-=size;
|
|
|
|
|
|
|
|
retval=uart1_receive_count;
|
2007-09-30 08:18:46 +00:00
|
|
|
}
|
2007-11-10 22:12:54 +00:00
|
|
|
|
2007-11-15 06:44:35 +00:00
|
|
|
/* Enable the interrupt */
|
2008-04-24 20:08:28 +00:00
|
|
|
IO_INTC_EINT0 |= INTR_EINT0_UART1;
|
2007-11-10 22:12:54 +00:00
|
|
|
|
2007-11-15 06:44:35 +00:00
|
|
|
return retval;
|
2007-09-30 08:18:46 +00:00
|
|
|
}
|
|
|
|
|
2009-04-14 05:17:03 +00:00
|
|
|
/* UART1 receive/transmit interupt handler */
|
2007-09-30 08:18:46 +00:00
|
|
|
void UART1(void)
|
|
|
|
{
|
2009-08-01 15:41:40 +00:00
|
|
|
IO_INTC_IRQ0 = INTR_IRQ0_UART1; /* Clear the interrupt first */
|
2007-10-18 05:14:10 +00:00
|
|
|
while (IO_UART1_RFCR & 0x3f)
|
2007-09-30 08:18:46 +00:00
|
|
|
{
|
2009-04-14 05:17:03 +00:00
|
|
|
if (uart1_receive_count > RECEIVE_RING_SIZE)
|
|
|
|
panicf("UART1 receive buffer overflow");
|
2007-10-18 05:14:10 +00:00
|
|
|
else
|
|
|
|
{
|
2009-04-14 05:17:03 +00:00
|
|
|
if(uart1_receive_write>=RECEIVE_RING_SIZE)
|
|
|
|
uart1_receive_write=0;
|
2007-10-18 05:14:10 +00:00
|
|
|
|
2009-04-14 05:17:03 +00:00
|
|
|
uart1_receive_buffer_ring[uart1_receive_write]=IO_UART1_DTRR & 0xff;
|
|
|
|
uart1_receive_write++;
|
|
|
|
uart1_receive_count++;
|
2007-10-18 05:14:10 +00:00
|
|
|
}
|
2007-09-30 08:18:46 +00:00
|
|
|
}
|
2007-10-01 05:42:12 +00:00
|
|
|
|
2009-04-14 05:17:03 +00:00
|
|
|
while ( ((IO_UART1_TFCR & 0x3f) < 0x20) && (uart1_send_count > 0) )
|
|
|
|
{
|
|
|
|
IO_UART1_DTRR=uart1_send_buffer_ring[uart1_send_read++];
|
|
|
|
uart1_send_count--;
|
|
|
|
}
|
2007-09-30 08:18:46 +00:00
|
|
|
}
|