📄 uart.c
字号:
#include <stdio.h>
#include "apuart.h"
int fflush(FILE *stream);
UART_t uart_0;
UART_t uart_1;
UART_t uart_2;
UART_t uart_3;
void drv_UART_initialize(unsigned int *handle, unsigned int cmd, unsigned int *argument);
void apUART_Init (UART_t *uart,unsigned int speed, unsigned long flags);
void apUART_SetSpeed (UART_t *uart, unsigned int speed);
void sendchar(char *ch);
void UART_scanf(int n,char *str);
void UART_printf(int n,char *str);
int main(void)
{ char ch,*str,tx;
//drv_UART_initialize((unsigned int *)0,(unsigned int)115200,(unsigned int *)0);
drv_UART_initialize((unsigned int *)0,(unsigned int)38400,(unsigned int *)0);
//drv_UART_initialize((unsigned int *)0,(unsigned int)19200,(unsigned int *)0);
//drv_UART_initialize((unsigned int *)0,(unsigned int)9600,(unsigned int *)0);
while(1)
{
UART_printf(0,"ATD13817727302;\r");
}
/*
//UART_printf(0,"ATD+CHFA=1\r");
while(0)
{
volatile int j = 1000;
UART_printf(0,"U");
//UART_printf(0,"UUUU\n");
while(j--)
{
volatile int i = 33;
while(i--);
}
}
UART_scanf(0,str);
apSleepus(100);
UART_printf(0,"ATD13817727302;\r");
//while(1);
while(1){
UART_scanf(0,str);
}
while(1){
UART_printf(0,"AT\r\n");
UART_scanf(0,str);
scanf("%s",&tx);
// UART_printf(0,&tx);
}
*/
}
void drv_UART_initialize(unsigned int *handle, unsigned int cmd, unsigned int *argument)
{
HW_REG(0x2002B420, 0x0) = 0x0000000F;
//int cmd1;
uart_0.base = UART0_BASE;
uart_0.ref_clk = UART_REF_CLK012;
uart_1.base = UART1_BASE;
uart_1.ref_clk = UART_REF_CLK012;
uart_2.base = UART2_BASE;
uart_2.ref_clk = UART_REF_CLK012;
uart_3.base = UART3_BASE;
uart_3.ref_clk = UART_REF_CLK012;
apUART_Init (&uart_0, cmd, (UART_M_ENABLE_FIFO));
// apUART_Init (&uart_1, cmd, (UART_M_ENABLE_FIFO));
// apUART_Init (&uart_2, cmd, (UART_M_ENABLE_FIFO));
// apUART_Init (&uart_3, cmd1, (UART_M_ENABLE_FIFO));
}
//-------------------------------------------------------------
// Initiate the specified UART
void apUART_Init (UART_t *uart,unsigned int speed, unsigned long flags)
{
unsigned int temp,rx_char;
uart->overrun_errors = 0;
uart->break_errors = 0;
uart->parity_errors = 0;
uart->framing_errors = 0;
// Disable everything
HW_REG(uart->base, CR) = 0;
apUART_SetSpeed (uart, speed);
// Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled
temp = 0;
if (flags & UART_M_ENABLE_FIFO)
SET_BIT(temp, LCRH_FEN);
//temp|=(1<<LCRH_FEN);
// 3 = 8 Bit
//INSERT_FIELD(temp, LCRH_WLEN, 3);
temp|=(1<<LCRH_WLEN_LSB);
temp|=(1<<LCRH_WLEN_MSB);
HW_REG(uart->base, LCRH) = temp;
// Enable the UART
temp = 0;
if (flags & UART_M_ENABLE_IRDA)
SET_BIT(temp, CR_SIREN);
SET_BIT(temp, CR_TXE);
SET_BIT(temp, CR_RXE);
SET_BIT(temp, CR_UARTEN);
HW_REG(uart->base, CR) = temp;
// Flush RX FIFO's
//while (!apUART_Status (uart, UART_RX_FIFO_EMPTY))
//rx_char = apUART_GetChar (uart);
}
// Set the baud rate on the specified UART
void apUART_SetSpeed (UART_t *uart, unsigned int speed)
{
unsigned int divider, remainder, fraction, temp;
temp = 16 * speed;
divider = uart->ref_clk / temp;
remainder = uart->ref_clk % temp;
temp = (128 * remainder) / temp;
fraction = temp / 2;
if (temp & 1)
fraction++;
HW_REG(uart->base, IBRD) = divider;
HW_REG(uart->base, FBRD) = fraction;
// Must write LCRH after LCRM and/or LCRL.
HW_REG(uart->base, LCRH) = HW_REG(uart->base, LCRH);
}
void sendchar(char *ch)
{
//UART_printf(2,ch);
while(!EXTRACT_BIT(HW_REG(UART0_BASE, FR), FR_TXFE));
HW_REG(UART0_BASE, DR) = *ch;
}
// Write a character to the specified UART
void apUART_PutChar (UART_t *uart, int c)
{
while (TEST_BIT(HW_REG(uart->base, FR), FR_TXFF));
HW_REG(uart->base, DR) = c;
}
void drv_UART_write(unsigned int *handle, unsigned int *request, unsigned int *ibuffer)
{
// Transmit data
UART_t *uart_n;
int tx_char;
tx_char = *ibuffer;
switch((int)handle)
{
case 0:
uart_n=&uart_0;
break;
case 1:
uart_n=&uart_1;
break;
case 2:
uart_n=&uart_2;
break;
case 3:
uart_n=&uart_3;
break;
}
apUART_PutChar (uart_n, tx_char);
// printf("TX has been sent: %X\n",tx_char);
}
void apSleepus(unsigned int usec)
{
unsigned int etimer, ttimer;
if (usec > 0xFFFF)
usec = 0xFFFF;
etimer = 0xFFFF - usec;
ttimer = SLEEP_TIMEOUT;
*TIMER0_CTRL = 0x00;
*TIMER0_LOAD = 0xFFFF;
// Wait for timer to be loaded
while((*TIMER0_LOAD != 0xFFFF) && ttimer)
ttimer--;
// Wait for timer to count down
*TIMER0_CTRL = 0x80;
while((*TIMER0_VALUE & 0xFFFF) > etimer)
continue;
}
// Read the status flags on the specified UART
int apUART_Status (UART_t *uart, int req_status)
{
switch (req_status)
{
case UART_RX_FIFO_EMPTY:
return EXTRACT_BIT(HW_REG(uart->base, FR), FR_RXFE);
case UART_TX_FIFO_FULL:
return EXTRACT_BIT(HW_REG(uart->base, FR), FR_TXFF);
default:
return -1;
}
}
/*
void apSleep(unsigned int msec)
{
while (msec--)
apSleepus(1000);
}
void apSleepus(unsigned int usec)
{
while (usec--)
{
int i=33;
while(i--);
}
}
*/
// Read a character from the specified UART
int apUART_GetChar (UART_t *uart)
{
unsigned int data, c,data1;
while (TEST_BIT(HW_REG(uart->base, FR), FR_RXFE));
data = HW_REG(uart->base, DR);
//c = EXTRACT_FIELD(data, DR_DATA);
c = data&0x00FF;
data1=data&0x0F00;
if (data1!=0/*EXTRACT_FIELD(data, DR_ERRORS) != 0*/)
{
if (TEST_BIT(data, DR_OE))
uart->overrun_errors++;
if (TEST_BIT(data, DR_BE))
uart->break_errors++;
if (TEST_BIT(data, DR_PE))
uart->parity_errors++;
if (TEST_BIT(data, DR_FE))
uart->framing_errors++;
}
return c;
}
void drv_UART_read (unsigned int *handle, unsigned int *request, unsigned int *obuffer)
{
UART_t *uart_n;
unsigned int rx_char,rx_timeout;
switch((int)handle)
{
case 0:
uart_n=&uart_0;
break;
case 1:
uart_n=&uart_1;
break;
case 2:
uart_n=&uart_2;
break;
case 3:
uart_n=&uart_3;
break;
}
rx_timeout = 100;
// Receive data
/* while (apUART_Status (uart_n, UART_RX_FIFO_EMPTY)) {
if (rx_timeout <= 0) {
printf ("Error: RX Timeout\n");
}
apSleepus(500);
rx_timeout--;
}
rx_char = apUART_GetChar (uart_n);
*obuffer = rx_char;
printf("RX has been received: %X\n",*obuffer); */
while (apUART_Status (uart_n, UART_RX_FIFO_EMPTY)&&rx_timeout)
{
if (rx_timeout <= 0) {
printf ("Error: RX Timeout\n");
}
apSleepus(500);
rx_timeout--;
}
if (!apUART_Status (uart_n, UART_RX_FIFO_EMPTY))
{
rx_char = apUART_GetChar (uart_n);
}
*obuffer = rx_char;
// printf("%c",rx_char);
}
//define function-----------------------------------------------------
void UART_printf(int n,char *str)
{
char tx;
UART_t *uart_n;
switch(n)
{
case 0:
uart_n=&uart_0;
break;
case 1:
uart_n=&uart_1;
break;
case 2:
uart_n=&uart_2;
break;
case 3:
uart_n=&uart_3;
break;
}
while(*str!='\0'/*!TEST_BIT(HW_REG(uart_n->base, FR), FR_TXFE)*/)
{
tx=*str;
str++;
// printf("_%c",tx);
// fflush(0);
drv_UART_write((unsigned int *)n, (unsigned int *)0, (unsigned int *)&tx);
}
tx=*str;
drv_UART_write((unsigned int *)n, (unsigned int *)0, (unsigned int *)&tx);
}
void UART_scanf(int n,char *str)
{
// unsigned int *handle;
UART_t *uart_n;
char *strtemp,rx;
strtemp=str;
// handle=&n;
switch(n)
{
case 0:
uart_n=&uart_0;
break;
case 1:
uart_n=&uart_1;
break;
case 2:
uart_n=&uart_2;
break;
case 3:
uart_n=&uart_3;
break;
}
timer_set(1000000); //40ms
do
{
while (apUART_Status (uart_n, UART_RX_FIFO_EMPTY));
if (!apUART_Status (uart_n, UART_RX_FIFO_EMPTY))
drv_UART_read ((unsigned int *)n, (unsigned int *)0, (unsigned int *)&rx);
//rx = rx & 0x7F;
*(strtemp++)=rx;
printf("%c",rx);
fflush(0);
// if(timer_get()<100) break;
}while((rx!='\0')&&(rx!='\r'));
*(--strtemp)='\0';
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -