📄 uart.c
字号:
/**************************************************************************************************
*
* Copyright (c) 2001 - 2003 IST Electronics Corp. All rights reserved.
*
* FILENAME
* uart.c
*
* VERSION
* 1.0
*
* DESCRIPTION
* This file contains the uart initial routine and Tx/Rx functions.
*
* DATA STRUCTURES
* None
*
* FUNCTIONS
* 1. Fun_UART_SetBaudRate()
* 2. Fun_UART_Initialize()
* 3. Fun_UART_getchar()
* 4. Fun_UART_putchar()
*
* HISTORY
* 11/24/2003 Ver 1.0 Created by Jim Lin
* REMARK
* None
*
*************************************************************************************************/
#include "740defs.h"
#include "ist.h"
uint8 ascii_1,ascii_2;
uint8 hexdata;
/************************************************/
/* Name: Fun_UART_SetBaudRate */
/* In: uint32 baud_rate:波特率,如9600 */
/* Out: None */
/* Description: */
/* set uart baud rate */
/************************************************/
void Fun_UART_SetBaudRate(uint32 baud_rate)
{
uint32 baud_value;
// First, compute the baudrate divisor.
baud_value = (EXT_CLOCK_RATE / (baud_rate * 16));
if ((EXT_CLOCK_RATE % (baud_rate * 16)) > ((baud_rate * 16) / 2))
baud_value++;
baud_value -= 2;
// Set the divisor latch bit.
UART_LCR = 0x80;
// First write the LSB of the baud rate divisor.
UART_LSB = (uint8) baud_value;
// Now write the MSB of the baud rate divisor.
UART_MSB = (uint8) (baud_value >> 8);
// Now that the baud rate has been set turn off Divisor Latch
// to reference data in line control
UART_LCR &= 0x7F;
}
/************************************************/
/* Name: Fun_UART_Initialize */
/* In: uint32 baud_rate:波特率,如9600 */
/* Out: None */
/* Description: */
/* set uart port */
/************************************************/
int Fun_UART_Initialize(uint32 baud_rate)
{
// Reset the TX/RX FIFOs
UART_FCR = 0x06;
// Setup baud rate
Fun_UART_SetBaudRate(baud_rate);
// Setup none parity, 8 data bits, and 1 stop bits
UART_LCR = 0x03;
// Timeout if more than ??? bits xfer time
UART_TOR = (0x80+0x20);
// Setup Fifo trigger level and enable FIFO
UART_FCR = 0xC7;
return 0;
}
/************************************************/
/* Name: Fun_UART_getchar */
/* In: None */
/* Out: received char */
/* Description: */
/* get a char by uart port */
/************************************************/
uint8 Fun_UART_getchar(void)
{
uint8 x;
while(1)
{
while (!(UART_LSR & 0x1)) // Wait until the RX FIFO data ready
{;}
x = UART_RX;
if(x == PCCOM_HEAD)
{
return UART_RX;
}
}
}
uint16 Fun_UART_getdata(uint8 *getdata)
{
uint8 getdata1,getdata2;
uint16 i = 1;
while(1)
{
//get first ascii
Fun_TimerInitialize(0,100);
while (!(UART_LSR & 0x1)) // Wait until the RX FIFO data ready
{
if(!Flag_timer) return 0;
}
Disable_Int(TIMERINT0); //close int
getdata1 = UART_RX; // Transmit the character
if(getdata1 == PCCOM_END)
{
getdata[i] = getdata1;
return i;
}
if(atoi(getdata1))
{
return 0;
}
getdata1 = hexdata;
//get second ascii
Fun_TimerInitialize(0,100);
while (!(UART_LSR & 0x1)) // Wait until the RX FIFO data ready
{
if(!Flag_timer) return 0;
}
Disable_Int(TIMERINT0); //close int
getdata2 = UART_RX; // Transmit the character
if(atoi(getdata2))
{
return 0;
}
getdata2 = hexdata;
getdata[i] = (getdata1 <<4) + getdata2;
i++;
}
}
/************************************************/
/* Name: Fun_UART_putchar */
/* In: send char */
/* Out: None */
/* Description: */
/* send a char by uart port */
/************************************************/
bool Fun_UART_putchar(uint8 ch)
{
Fun_TimerInitialize(0,100);
while (!(UART_LSR & 0x20)) // Wait until the transmitter buffer is empty
{
if(!Flag_timer) return False;
}
// Transmit the character
Disable_Int(TIMERINT0); //close int
UART_TX = ch;
return True;
}
bool Fun_UART_senddata(uint8 *pSenddata,uint16 Num,uint8 mode)
{
uint8 temp_send;
uint16 i = 1;
if(mode == 0)
{
temp_send = pSenddata[i-1];
if(temp_send != PCCOM_HEAD)return False;
if(Fun_UART_putchar(temp_send))return False;
}
while(1)
{
temp_send = pSenddata[i];
if((i == Num) && (temp_send == PCCOM_END))
{
if(Fun_UART_putchar(temp_send))return False;
return True;
}
itoa(temp_send);
if(Fun_UART_putchar(ascii_1))return False; //put ascii 1
if(Fun_UART_putchar(ascii_2))return False; //put ascii 2
i++;
if(i == 0 )return False;
if(i > Num)return True;
}
}
//translate one ascii to one hex
bool atoi(uint8 asciiData)
{
if((asciiData>=0x30)&&(asciiData<=0x39))
{
hexdata = asciiData - 0x30;
return True;
}
if((asciiData>=65)&&(asciiData<=70))
{
hexdata = asciiData - 65 + 10;
return True;
}
return False;
}
//translate one hex to two ascii
void itoa(uint8 inData)
{
uint8 xl,xh;
xh = inData>>4;
xl = inData & 0x0f;
if(xh <= 9)
{
ascii_1 = xh + 0x30;
}
else
{
ascii_1 = xh - 10 + 65;
}
if(xl <= 9)
{
ascii_2 = xl + 0x30;
}
else
{
ascii_2 = xl - 10 + 65;
}
}
/*
void UART_PutString(char *string)
{
while (*string != '\0')
{
UART_putchar(*string);
string++;
}
}
void PutRepChar(char c, int count)
{
while (count--)
UART_putchar(c);
}
void PutStringReverse(char *s, int index)
{
while ((index--) > 0)
UART_putchar(s[index]);
}
void PutNumber(int value, int radix, int width, char fill)
{
char buffer[40];
int bi = 0;
uint32 uvalue;
uint16 digit;
uint16 left = FALSE;
uint16 negative = FALSE;
if (fill == 0)
fill = ' ';
if (width < 0)
{
width = -width;
left = TRUE;
}
if (width < 0 || width > 80)
width = 0;
if (radix < 0)
{
radix = -radix;
if (value < 0)
{
negative = TRUE;
value = -value;
}
}
uvalue = value;
do
{
if (radix != 16)
{
digit = uvalue % radix;
uvalue = uvalue / radix;
}
else
{
digit = uvalue & 0xf;
uvalue = uvalue >> 4;
}
buffer[bi] = digit + ((digit <= 9) ? '0' : ('A' - 10));
bi++;
if (uvalue != 0)
{
if ((radix == 10)
&& ((bi == 3) || (bi == 7) || (bi == 11) | (bi == 15)))
{
buffer[bi++] = ',';
}
}
}
while (uvalue != 0);
if (negative)
{
buffer[bi] = '-';
bi += 1;
}
if (width <= bi)
PutStringReverse(buffer, bi);
else
{
width -= bi;
if (!left)
PutRepChar(fill, width);
PutStringReverse(buffer, bi);
if (left)
PutRepChar(fill, width);
}
}
char *FormatItem(char *f, int a)
{
char c;
int fieldwidth = 0;
int leftjust = FALSE;
int radix = 0;
char fill = ' ';
if (*f == '0')
fill = '0';
while ((c = *f++) != 0)
{
if (c >= '0' && c <= '9')
{
fieldwidth = (fieldwidth * 10) + (c - '0');
}
else
switch (c)
{
case '\000':
return (--f);
case '%':
UART_putchar('%');
return (f);
case '-':
leftjust = TRUE;
break;
case 'c':
{
if (leftjust)
UART_putchar(a & 0x7f);
if (fieldwidth > 0)
PutRepChar(fill, fieldwidth - 1);
if (!leftjust)
UART_putchar(a & 0x7f);
return (f);
}
case 's':
{
if (leftjust)
UART_PutString((char *)a);
if (fieldwidth > strlen((char *)a))
PutRepChar(fill,
fieldwidth - strlen((char *)a));
if (!leftjust)
UART_PutString((char *)a);
return (f);
}
case 'd':
case 'i':
radix = -10;
break;
case 'u':
radix = 10;
break;
case 'x':
radix = 16;
break;
case 'X':
radix = 16;
break;
case 'o':
radix = 8;
break;
default:
radix = 3;
break; // unknown switch!
}
if (radix)
break;
}
if (leftjust)
fieldwidth = -fieldwidth;
PutNumber(a, radix, fieldwidth, fill);
return (f);
}
void UART_printf(char *f, ...) // variable arguments
{
char *argP;
vaStart(argP, f); // point at the end of the format string
while (*f)
{ // this works because args are all ints
if (*f == '%')
f = FormatItem(f + 1, vaArg(argP, int));
else
UART_putchar(*f++);
}
}
void HexDumpBuffer(char str[], uint8 *buf, int size)
{
int idx, i;
UART_printf("%s -> 0x%X\n", str, (int)buf);
idx = 0;
while (size > 0)
{
for (i = 0; i < 16; i++)
UART_printf("%02x ", buf[idx + i]);
UART_printf(" ");
for (i = 0; i < 16; i++)
{
if ((buf[idx + i] >= 0x20) && (buf[idx + i] < 127))
UART_printf("%c", buf[idx + i]);
else
UART_printf(".");
size--;
}
idx += 16;
UART_printf("\n");
}
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -