2007-09-20 09:08:40 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2007 Catalin Patulea <cat@vv.carleton.ca>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
|
|
|
* MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "system.h"
|
|
|
|
|
|
|
|
/* UART 0/1 */
|
|
|
|
|
2007-09-30 08:18:46 +00:00
|
|
|
#define CONFIG_UART_BRSR 87
|
|
|
|
#define MAX_UART_BUFFER 32
|
|
|
|
static unsigned char uart1buffer[MAX_UART_BUFFER];
|
|
|
|
int uart1read = 0, uart1write = 0, uart1count = 0;
|
2007-09-20 09:08:40 +00:00
|
|
|
|
|
|
|
void do_checksums(char *data, int len, char *xor, char *add)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
*xor = data[0];
|
|
|
|
*add = data[0];
|
|
|
|
for(i=1;i<len;i++)
|
|
|
|
{
|
|
|
|
*xor ^= data[i];
|
|
|
|
*add += data[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-30 08:18:46 +00:00
|
|
|
void uart_init(void)
|
|
|
|
{
|
2007-09-20 09:08:40 +00:00
|
|
|
// 8-N-1
|
2007-09-23 23:08:39 +00:00
|
|
|
IO_UART1_MSR=0x8000;
|
|
|
|
IO_UART1_BRSR=CONFIG_UART_BRSR;
|
2007-09-30 08:18:46 +00:00
|
|
|
IO_UART1_RFCR = 0x8000;
|
|
|
|
/* gio 27 is input, uart1 rx
|
|
|
|
gio 28 is output, uart1 tx */
|
|
|
|
IO_GIO_DIR1 |= (1<<11); /* gio 27 */
|
|
|
|
IO_GIO_DIR1 &= ~(1<<12); /* gio 28 */
|
|
|
|
|
|
|
|
/* init the recieve buffer */
|
|
|
|
uart1read = 0;
|
|
|
|
uart1write = 0;
|
|
|
|
uart1count = 0;
|
|
|
|
|
|
|
|
/* Enable the interrupt */
|
|
|
|
IO_INTC_EINT0 |= (1<<IRQ_UART1);
|
2007-09-20 09:08:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void uartPutc(char ch) {
|
|
|
|
// 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
|
|
|
|
|
|
|
// Write character
|
2007-09-23 23:08:39 +00:00
|
|
|
IO_UART1_DTRR=ch;
|
2007-09-20 09:08:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unsigned integer to ASCII hexadecimal conversion
|
|
|
|
void uartPutHex(unsigned int n) {
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 8; i != 0; i--) {
|
|
|
|
unsigned int digit = n >> 28;
|
|
|
|
uartPutc(digit >= 10 ? digit - 10 + 'A' : digit + '0');
|
|
|
|
n <<= 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void uartPuts(const char *str) {
|
|
|
|
char ch;
|
|
|
|
while ((ch = *str++) != '\0') {
|
|
|
|
uartPutc(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void uartGets(char *str, unsigned int size) {
|
|
|
|
for (;;) {
|
|
|
|
char ch;
|
|
|
|
|
|
|
|
// Wait for FIFO to contain something
|
2007-09-23 23:08:39 +00:00
|
|
|
while ((IO_UART1_RFCR & 0x3f) == 0);
|
2007-09-20 09:08:40 +00:00
|
|
|
|
|
|
|
// Read character
|
2007-09-23 23:08:39 +00:00
|
|
|
ch = (char)IO_UART1_DTRR;
|
2007-09-20 09:08:40 +00:00
|
|
|
|
|
|
|
// Echo character back
|
2007-09-23 23:08:39 +00:00
|
|
|
IO_UART1_DTRR=ch;
|
2007-09-20 09:08:40 +00:00
|
|
|
|
|
|
|
// If CR, also echo LF, null-terminate, and return
|
|
|
|
if (ch == '\r') {
|
2007-09-23 23:08:39 +00:00
|
|
|
IO_UART1_DTRR='\n';
|
2007-09-20 09:08:40 +00:00
|
|
|
if (size) {
|
|
|
|
*str++ = '\0';
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append to buffer
|
|
|
|
if (size) {
|
|
|
|
*str++ = ch;
|
|
|
|
--size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int uartPollch(unsigned int ticks) {
|
|
|
|
while (ticks--) {
|
2007-09-23 23:08:39 +00:00
|
|
|
if (IO_UART1_RFCR & 0x3f) {
|
|
|
|
return IO_UART1_DTRR & 0xff;
|
2007-09-20 09:08:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool uartAvailable(void)
|
|
|
|
{
|
2007-09-30 08:18:46 +00:00
|
|
|
return uart1count > 0;
|
2007-09-20 09:08:40 +00:00
|
|
|
}
|
|
|
|
|
2007-09-30 08:18:46 +00:00
|
|
|
void uart1_heartbeat(void)
|
2007-09-20 09:08:40 +00:00
|
|
|
{
|
|
|
|
char data[5] = {0x11, 0x30, 0x11^0x30, 0x11+0x30, '\0'};
|
|
|
|
uartPuts(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void uartSendData(char* data, int len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i=0;i<len;i++)
|
|
|
|
uartPutc(data[i]);
|
|
|
|
}
|
2007-09-30 08:18:46 +00:00
|
|
|
|
|
|
|
bool uart1_getch(char *c)
|
|
|
|
{
|
|
|
|
if (uart1count > 0)
|
|
|
|
{
|
|
|
|
*c = uart1buffer[uart1read];
|
|
|
|
uart1read = (uart1read+1) % MAX_UART_BUFFER;
|
|
|
|
uart1count--;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* UART1 receive intterupt handler */
|
|
|
|
void UART1(void)
|
|
|
|
{
|
|
|
|
if (IO_UART1_RFCR & 0x3f)
|
|
|
|
{
|
|
|
|
if (uart1count >= MAX_UART_BUFFER)
|
|
|
|
panicf("UART1 buffer overflow");
|
|
|
|
uart1buffer[uart1write] = IO_UART1_DTRR & 0xff;
|
|
|
|
uart1write = (uart1write+1) % MAX_UART_BUFFER;
|
|
|
|
uart1count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
IO_INTC_IRQ0 = (1<<IRQ_UART1);
|
|
|
|
}
|