📄 uart2_trace.c
字号:
#include <stdarg.h>
#include "uart2_trace.h"
void uart2_init()
{
*(unsigned short*)0xfffb101a |= 0x00c0; //SOFT
/* Reset the core. This will set all registers to their reset values. */
if ((*(unsigned short*)(UART_REG_MDR1_ADDR)) != 0x07)
{
*(unsigned short*)(UART_REG_MDR1_ADDR) = 0x07;
}
*(unsigned short*)(UART_REG_LCR_ADDR) = 0xbf;
*(unsigned short*)(UART_REG_DL_LSB_ADDR) = 0x1a;
*(unsigned short*)(UART_REG_DL_MSB_ADRR) = 0x00;
*(unsigned short*)(UART_REG_EFR_ADDR) = 0x10;
*(unsigned short*)(UART_REG_LCR_ADDR) = 0x80;
*(unsigned short*)(UART_REG_MCR_ADDR) = 0x40;
*(unsigned short*)(UART_REG_SCR_ADDR) = 0x09;
*(unsigned short*)(UART_REG_LCR_ADDR) = 0x03;
*(unsigned short*)(UART_REG_MDR1_ADDR) = 0x00; //16x
*(unsigned short*)(UART_REG_IER_ADDR) = 0x00;
}
void uart2_writeNchar(unsigned char *wr_buffer,unsigned short length)
{
unsigned short i;
for(i=0;i<length;i++)
{
while((*(unsigned short*)(UART_REG_LSR_ADDR)&0x60) != 0x60);
*(unsigned short*)(UART_REG_THR_ADDR) = *wr_buffer++;
}
}
void UA_WriteString (char *buffer)
{
(void) uart2_writeNchar (buffer, strlen (buffer));
}
void UART_Printf(char *data, ...)
{
char data_temp[256];
va_list _ap;
va_start(_ap, data);
vsprintf(data_temp, data, _ap);
UA_WriteString(data_temp);
va_end(_ap);
}
//=====================================================================//
//UART get number //
//=====================================================================//
char *UART_Receive()
{
int i = 0;
static char data[256];
do
{
while(!((*(unsigned short*)(UART_REG_LSR_ADDR))&0x01)); //wait for data in
data[i] = *(unsigned short*)(UART_REG_RHR_ADDR);
i++;
}while((data[i-1] != '\n') && (data[i-1] != '\r'));
return data;
}
unsigned int UART_GetNum()
{
char *data;
unsigned int number = 0;
int i =0;
data = UART_Receive();
while(data[i] != '\n' && data[i] != '\r')
{
if((data[0] == '0') && ((data[1] == 'x') || (data[1] == 'X')))
{
if((data[i] >= '0') && (data[i] <= '9'))
number = (int)(data[i] - 0x30) + number * 16;
else if((data[i] >= 'a') && (data[i] <= 'f'))
number = (int)(data[i] - 0x57) + number * 16;
else if((data[i] >= 'A') && (data[i] <= 'F'))
number = (int)(data[i] - 0x37) + number * 16;
}
else
{
if((data[i] >= '0') && (data[i] <= '9'))
number = (int)(data[i] - 0x30) + number * 10;
}
i++;
}
return number;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -