?? uart.c
字號:
//====================================================================
// File Name : rtc.c
// Function : S3C2413 RTC Test Program
// Date : Sep. 27, 2005
// Version : 0.0
// History
// 0.0 : Programming start (Sep. 27, 2005)
//====================================================================
#include <stdio.h>
#include <stdlib.h>
#include "2413addr.h"
#include "System.h"
#include "Exception.h"
#include "Console.h"
#include "def.h"
#include "Uart.h"
#define NP 0 // No Parity
#define OP 4 // Odd Parity
#define EP 5 // Even Parity
#define PF1 6 // Parity force 1
#define PF0 7 // Parity force 0
typedef struct tagUART
{
volatile U32 ULCON; //0x0
volatile U32 UCON; //0x4
volatile U32 UFCON; //0x8
volatile U32 UMCON; //0xc
volatile U32 UTRSTAT; //0x10 Read Only
volatile U32 UERSTAT; //0x14 Read Only
volatile U32 UFSTAT; //0x18 Read Only
volatile U32 UMSTAT; //0x1c Read Only
volatile U32 UTXH; //0x20 Write Only
volatile U32 URXH; //0x24 Read Only
volatile U32 UBRDIV; //0x28
volatile U32 UDIVSLOT; //0x2c
}UART;
UART *pUART;
const unsigned int nSlotTable[16] = {0x0000, 0x0080, 0x0808, 0x0888, 0x2222, 0x4924, 0x4a52, 0x54aa,
0x5555, 0xd555, 0xd5d5, 0xddd5, 0xdddd, 0xdfdd, 0xdfdf, 0xffdf};
U32 saved_rGPHCON, saved_rGPHDAT, saved_rGPHDN;
U32 saved_ULCON, saved_UCON, saved_UFCON, saved_UMCON, saved_UBRDIV, saved_UDIVSLOT;
static S8 TestString[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890->UART0 Tx interrupt test is First!!!!\rABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890->UART0 Tx interrupt test is Second!!!!\rABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890->UART0 Tx interrupt test is Third!!!!\r\n";
volatile S8 *uartTxStr, *uartRxStr;
void * uart_func[][2]=
{
(void *)Uart_Intrrupt_Tx_Fifo, "Uart Interrupt Tx FIFO",
0,0
};
void Ch10_UART(void)
{
int i=0;
while(1) {
printf("\n");
while(1) { //display menu
printf("%2d:%s",i,uart_func[i][1]);
i++;
if((int)(uart_func[i][0])==0) {
printf("\n");
break;
}
if((i%4)==0) printf("\n");
}
printf("\nSelect(-1 to exit): ");
i = GetIntNum();
if(i==-1) break;
if(i>=0 && (i<(sizeof(uart_func)/4)) )
( (void (*)(void)) (uart_func[i][0]) )(); // execute selected function.
}
}
void Uart_Intrrupt_Tx_Fifo(void)
{
Uart_Port_Init(); // GPIO port init.
Uart_Select(0);
uartTxStr=TestString; // Tx Data Init.
Uart_Line_Set(NP,1,8); // No Parity, One stop bit, Data 8 bit
Uart_Baud_Rate_Set(115200);
#if 0
Uart_Select(0);
pUART->UCON = (0x1<<9); // Tx = Level
Uart_Baud_Rate_Set();
Uart_Mode_Set(INTORPOL,DISABLE);
pUART->UFCON = (2<<6)|(1<<4)|(3<<1)|(1); // Fifo Clear, Fifo Enable
pUART->UMCON = (0<<4); // AFC Disable
pISR_UART0 = (U32)UART0_Tx_ISR;
rINTMSK &= ~(BIT_UART0);
rINTSUBMSK &= ~(BIT_SUB_TXD0);
TxDone=1;
while(TxDone);
while(pUART->UTRSTAT!=0x6);
rINTMSK &= ~(BIT_UART0);
rINTSUBMSK &= ~(BIT_SUB_TXD0);
#endif
Uart_Port_Return(0);
}
void Uart_Baud_Rate_Set(U32 bps)
{
int DIV_VAL, nSlot;
int UClk;
float tempDiv;
switch((pUART->UCON >>10)&0x3) {
case 0:
case 2:
SystemCLK();
UClk=PCLK;
break;
case 1:
printf("UEXTCLK must be set\n"); while(1);
break;
case 3:
SystemCLK();
UClk=UARTCLK;
break;
}
DIV_VAL = ( (int)(UClk/16./bps+0.5) -1 ); //Baud rate divisior
nSlot = (int)((DIV_VAL - (int)DIV_VAL) * 16);
pUART->UBRDIV = DIV_VAL;
pUART->UDIVSLOT = nSlotTable[nSlot];
}
void Uart_Line_Set(U32 ParityMode, U32 StopBitNum, U32 DataBitNum)
{
pUART->ULCON = 0;
pUART->ULCON = (ParityMode<<3)|((StopBitNum-1)<<2)|(DataBitNum-5);
}
void Uart_Port_Init(void)
{
saved_rGPHCON=rGPHCON; // GPIO Configuration Backup
saved_rGPHDAT=rGPHDAT;
saved_rGPHDN=rGPHDN;
rGPHCON = (rGPHCON&~(0xfff))|(0xaaa); // GPIO -> RXD 0,1,2 TXD 0,1,2
rGPHDN = 0x7fff; // Pull-down Disable
printf("Uart Port Init\n");
}
void Uart_Select(U32 ch)
{
switch(ch) {
case 0:
pUART = (void *)(0x50000000);
printf("Uart0 is selected\n");
break;
case 1:
pUART = (void *)(0x50004000);
printf("Uart1 is selected\n");
break;
case 2:
pUART = (void *)(0x50008000);
printf("Uart2 is selected\n");
break;
default:
printf("[Error] Select Uart\n");
while(1);
}
if(ch==1) { // Uart1 Configuration backup for DNW Monitoring
saved_ULCON = pUART->ULCON;
saved_UCON = pUART->UCON;
saved_UFCON = pUART->UFCON;
saved_UMCON = pUART->UMCON;
saved_UBRDIV = pUART->UBRDIV;
saved_UDIVSLOT = pUART->UDIVSLOT;
}
}
void Uart_Port_Return(U32 ch)
{
rGPHCON=saved_rGPHCON; // GPIO Configuration Recovery
rGPHDAT=saved_rGPHDAT;
rGPHDN=saved_rGPHDN;
if(ch==1) { // Uart1 Configuration recovery for DNW Monitoring
pUART->ULCON = saved_ULCON;
pUART->UCON = saved_UCON;
pUART->UFCON = saved_UFCON;
pUART->UMCON = saved_UMCON;
pUART->UBRDIV = saved_UBRDIV;
pUART->UDIVSLOT = saved_UDIVSLOT;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -