uart

#include "S3C2451_uart.h"

#include "S3C2451_vector.h"

#include

#include

#include

 

UART_MemMapPtr const UART_BASE_PTR[4] = {UART0_BASE_PTR,UART1_BASE_PTR,UART2_BASE_PTR,UART3_BASE_PTR};

 

void UART_Init(UART_Typedef uartx,UART_Init_Typedef uart_init_struct)

{

UART_BASE_PTR[uartx]->ULCON &= ~(0x3F);

//wordwidth

UART_BASE_PTR[uartx]->ULCON |= uart_init_struct.wordwidth;

//stop

UART_BASE_PTR[uartx]->ULCON |= (uart_init_struct.stop<<2);

//parity

UART_BASE_PTR[uartx]->ULCON |= (uart_init_struct.parity<<3);

//baud

if( 9600 == uart_init_struct.baudrate)

{

UART_BASE_PTR[uartx]->UBRDIV = 10;

UART_BASE_PTR[uartx]->UDIVSLOT = 0xeeeee;

}else if(115200 == uart_init_struct.baudrate)

{

UART_BASE_PTR[uartx]->UBRDIV = 34; //pclk=66.5Mhz

UART_BASE_PTR[uartx]->UDIVSLOT = 0xDFDD;

}

UART_BASE_PTR[uartx]->UCON &= ~(0x0f);

//enable tx

if((uart_init_struct.mode&UART_Mode_TX) == UART_Mode_TX)

{

UART_BASE_PTR[uartx]->UCON |= (1<<2);

}

//enable rx

if((uart_init_struct.mode&UART_Mode_RX) == UART_Mode_RX)

{

UART_BASE_PTR[uartx]->UCON |= 1;

}

 

}

 

void UART_SendData(UART_Typedef uartx,U8 data)

{

UART_BASE_PTR[uartx]->UTXH = data;

while(!(UART_BASE_PTR[uartx]->UTRSTAT&0x04));

}

 

void UART_SendDataBuf(UART_Typedef uartx,U8 *data,U32 len)

{

unsigned char i;

for(i=0;i

UART_SendData(uartx,data[i]);

}

}

 

void UART_SendString(UART_Typedef uartx,U8 *s)

{

U32 len = strlen((char*)s);

UART_SendDataBuf(uartx,s,len);

}

 

void UART_Printf(char *fmt,...)

{

va_list ap;

char string[1024];

 

va_start(ap,fmt);

vsprintf(string,fmt,ap);

va_end(ap);

 

UART_SendString(UART0,(U8 *)string);

}

 

U8 UART_ReceiveData(UART_Typedef uartx)

{

//while(!(UART_BASE_PTR[uartx]->UTRSTAT&0x01));

return (UART_BASE_PTR[uartx]->URXH);

}

 

void UART_IRQService_Init(UART_Typedef uartx,U32 addr)

{

switch(uartx)

{

case UART0:

pISR_UART0 = addr;

break;

case UART1:

pISR_UART1 = addr;

break;

case UART2:

pISR_UART2 = addr;

break;

case UART3:

pISR_UART3 = addr;

break;

}

}

 

 

你可能感兴趣的:(uart)