?? uart.c
字號:
/**************************************************************************************
*
* Project Name : S3C6400 Validation
*
* Copyright 2006 by Samsung Electronics, Inc.
* All rights reserved.
*
* Project Description :
* This software is only for validating functions of the S3C6400.
* Anybody can use this software without our permission.
*
*--------------------------------------------------------------------------------------
*
* File Name : uart.c
*
* File Description : This file implements the API functon for UART.
*
* Author : Woojin,Kim
* Dept. : AP Development Team
* Created Date : 2007/01/16
* Version : 0.1
*
* History
* - Creat debug function (InitDebug,Putc,Getc,Getkey)(Haksoo,Kim 2006/11/08)
* - Creat Full function (Woojin,Kim 2007/01/16)
*
**************************************************************************************/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "option.h"
#include "library.h"
#include "sfr6400.h"
#include "system.h"
#include "uart.h"
#include "sysc.h"
#include "gpio.h"
#include "intc.h"
#include "dma.h"
#define UART_OFFSET 0x400
#define UART0_BASE (UART_REGS *)(UART_BASE)
#define UART1_BASE (UART_REGS *)(UART_BASE+UART_OFFSET)
#define UART2_BASE (UART_REGS *)(UART_BASE+UART_OFFSET*2)
#define UART3_BASE (UART_REGS *)(UART_BASE+UART_OFFSET*3)
#define UART_BUF (0x51000000)
//#define UART_BUF (DefaultDownloadAddress + 0x20000)
#define FIFO_DEBUG_BUF (0x51202000)
#define TX_FIFO_RESET (1<<2)
#define RX_FIFO_RESET (1<<1)
#define TX_INT_TYPE (1<<9) // 0:pulse 1:level(6400 should be level)
#define RX_INT_TYPE (1<<8) // 0:pulse 1:level(6400 should be level)
#define RX_TIMEOUT_EN (1<<7) // 0:disable 1:enable, disable for FIFO test
#define RX_ERR_INT_EN (1<<6) // 0:disable 1:enable
#define RTS_ACTIVE (1) // In normal mode, nRTS signal 0:low, 1:High
#define BIT_UART_MODEM (1<<3)
#define BIT_UART_TXD (1<<2)
#define BIT_UART_ERROR (1<<1)
#define BIT_UART_RXD (1)
#define DMA_BUF_LEN 8 // for rx
#define TX_END_CHAR NULL
#define RX_END_CHAR '\r'
static DMAC oUARTDma;
/////////
// Global variables
static UART_CON g_AUartCon[5] = {{115200,0,0,0,0,0,3,0,0,1,1,1,1,1,1}
, {115200,0,0,0,0,0,3,0,0,1,1,1,1,1,1}
, {115200,0,0,0,0,0,3,0,0,1,1,1,1,1,1}
, {115200,0,0,0,0,0,3,0,0,1,1,1,1,1,1}
, {115200,0,0,0,0,0,3,0,0,1,1,1,1,1,1}};
// control property per each channel. 4th data is defualt value for initialize
static volatile u8 g_AisTxDone[4] = {0, 0, 0, 0};
static volatile u8 g_AisRxDone[4] = {0, 0, 0, 0};
u8 *g_pUartTxStr[4];
u8 *g_pUartRxStr[4];
u8 *g_pUartBuf;
volatile u32 *g_pFifoDebug = (u32 *)FIFO_DEBUG_BUF; //temporary for fifo count test
volatile u32 g_uFcnt = 0;
volatile u32 g_uOpClock=0;
static UART_REGS *g_pUartDebugRegs;
//////////
// Function Name : UART_InitDebugCh
// Function Description : This function initializes a certain uart ch. for debugging console
// Input : NONE
// Output : NONE
// Version :
void UART_InitDebugCh(u8 ch, u32 uBaudRate)
{
UART_SetConfig(ch,0,1,1,4,1,2,1,uBaudRate,1,1,1,1);
UART_Open(ch);
return;
}
//////////
// Function Name : UART_Putc
// Function Description : This function write character data to uart debugging ch
// Input : NONE
// Output : NONE
// Version :
void UART_Putc(char data)
{
u32 temp;
if(data=='\n')
{
while(1)
{
temp = Inp32(&g_pUartDebugRegs->rUtrStat);
temp&=0x2;
if(temp)
break;
}
// Delay(10);
Outp8(&g_pUartDebugRegs->rUtxh,'\r');
}
while(1)
{
temp = Inp32(&g_pUartDebugRegs->rUtrStat);
temp&=0x02;
if(temp)
break;
}
// Delay(10);
Outp8(&g_pUartDebugRegs->rUtxh,data);
return;
}
//////////
// Function Name : UART_TxEmpty
// Function Description : This function Hold Uart Tx until FIFO empty
// Input : NONE
// Output : NONE
// Version :
void UART_TxEmpty(void)
{
u32 temp32;
while(1)
{
temp32 = Inp32(&g_pUartDebugRegs->rUtrStat);
temp32&=0x04;
if(temp32)
break;
}
}
//////////
// Function Name : UART_Getc
// Function Description : This function read character data from uart debugging ch
// Input : NONE
// Output : temp8, character data received through uart
// Version :
s8 UART_Getc( void)
{
u32 temp32;
char temp8;
while(1)
{
temp32 = Inp32(&g_pUartDebugRegs->rUtrStat);
temp32&=0x01;
if(temp32)
break;
}
temp8 = Inp8(&g_pUartDebugRegs->rUrxh);
return temp8;
}
//////////
// Function Name : UART_GetKey
// Function Description : This function read character data from uart debugging ch if there is received data
// Input : NONE
// Output : temp8 or 0, character data received through uart or 0
// Version :
s8 UART_GetKey( void)
{
u32 temp32;
char temp8;
temp32 = Inp32(&g_pUartDebugRegs->rUtrStat);
if(temp32 & 0x1)
{
temp8 = Inp8(&g_pUartDebugRegs->rUrxh);
return temp8;
}
else
return 0;
}
//////////
// Function Name : UART_GetIntNum
// Function Description : Input a number(hex, dec) from UART
// Input : NONE
// Version :
// added by rb1004
s32 UART_GetIntNum(void)
{
u8 str[32];
UART_GetString((s8 *)str);
return Str2Int((s8 *)str);
}
//////////
// Function Name : UART_GetString
// Function Description : Input a string from UART
// Input :
// Version :
// added by rb1004
void UART_GetString(s8 *pStr)
{
u8 *pStrOrg;
u8 c;
pStrOrg = (u8 *)pStr;
while ((c= UART_Getc())!='\r')
{
if (c=='\b') {
if ((int)pStrOrg < (int)pStr) {
UART_PutString("\b \b");
pStr--;
}
}
else {
*pStr++ = c;
UART_Putc(c);
}
}
*pStr = '\0';
UART_Putc('\n');
}
//////////
// Function Name : UART_PutString
// Function Description : Transmit a string through UART
// Input :
// Version :
// added by rb1004
void UART_PutString(const s8 *string)
{
while(*string)
UART_Putc(*string++);
}
//////////
// Function Name : Str2Int
// Function Description : convert string to digit.
// Input :
// Version :
// added by rb1004
static s32 Str2Int(s8 *string)
{
s32 base = 10;
s32 minus = 0;
s32 result = 0;
s32 lastIndex;
s32 i;
if(string[0]=='-')
{
minus = 1;
string++;
}
if(string[0]=='0' && (string[1]=='x' || string[1]=='X'))
{
base = 16;
string += 2;
}
lastIndex = strlen((const char *)string) - 1;
if(lastIndex<0)
return -1;
if(string[lastIndex]=='h' || string[lastIndex]=='H' )
{
base = 16;
string[lastIndex] = 0;
lastIndex--;
}
if(base==10)
{
//result = atoi(string); // atoi conversion not working properly //trb 051011
/* Alternate implementation for atoi */
result = 0;
for(i = 0; i<=lastIndex; i++){
result = result * 10 + string[i] - 0x30;
}
result = minus ? (-1*result):result;
}
else
{
for(i=0;i<=lastIndex;i++)
{
if(isalpha(string[i]))
{
if(isupper(string[i]))
result = (result<<4) + string[i] - 'A' + 10;
else
result = (result<<4) + string[i] - 'a' + 10;
}
else
result = (result<<4) + string[i] - '0';
}
result = minus ? (-1*result):result;
}
return result;
}
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
//////////
// Function Name : UART_Config
// Function Description : This function set up UART by user's choice
// Input : NONE
// Output : NONE
// Version : v0.1
u8 UART_Config(void)
{
u8 cCh;
s32 iNum = 0;
volatile UART_CON *pUartCon;
g_uOpClock = 0;
// Select Channel
printf("Note : [D] mark means default value. If you press ENTER key, default value is selected.\n");
printf("Select Channel(0~3) [D=0] : ");
cCh = (u8)GetIntNum();
if ( cCh>3 ) cCh = 0; // default uart 0
pUartCon = &g_AUartCon[cCh];
printf("\n\nConnect PC[COM1 or COM2] and UART%d of S3C6400 with a serial cable for test!!! \n", cCh);
//Set Other Options
printf("\nSelect Other Options\n 0. Nothing[D] 1.Send Break Signal 2. Loop Back Mode \n Choose : ");
switch(GetIntNum())
{
default :
pUartCon->cSendBreakSignal = 0x0;
pUartCon->cLoopTest = 0x0;
break;
case 1 :
pUartCon->cSendBreakSignal = 1;
return cCh;
case 2 :
pUartCon->cLoopTest = 1;
break;
}
//Set Parity mode
printf("\nSelect Parity Mode\n 1. No parity[D] 2. Odd 3. Even 4. Forced as '1' 5. Forced as '0' \n Choose : ");
switch(GetIntNum())
{
default :
pUartCon->cParityBit = 0;
break;
case 2 :
pUartCon->cParityBit = 4;
break;
case 3 :
pUartCon->cParityBit = 5;
break;
case 4 :
pUartCon->cParityBit = 6;
break;
case 5 :
pUartCon->cParityBit = 7;
break;
}
//Set the number of stop bit
printf("\n\nSelect Number of Stop Bit\n 1. One stop bit per frame[D] 2. Two stop bit per frame");
switch(GetIntNum())
{
default :
pUartCon->cStopBit = 0;
break;
case 2 :
pUartCon->cStopBit = 1;
break;
}
//Set Word Length
printf("\n\nSelect Word Length\n 1. 5bits 2. 6bits 3. 7bits 4. 8bits \n Choose : ");
switch(GetIntNum())
{
case 1 :
pUartCon->cDataBit = 0;
break;
case 2 :
pUartCon->cDataBit = 1;
break;
case 3 :
pUartCon->cDataBit = 2;
break;
default :
pUartCon->cDataBit = 3;
break;
}
// Set Operation clock
printf("\n\nSelect Operating Clock\n 1. PCLK[D] 2. EXT_CLK0(pwm) 3. EXT_CLK1(EPLL/MPLL) \n Choose : ");
switch (GetIntNum())
{
case 2 :
pUartCon->cOpClock = 1;
// connect CLKOUT and UEXTCLK
printf("\nInput PWM EXT_CLK by Pulse Generater\n");
printf("How much CLK do you input through the pwmECLK?");
printf("Mhz : ");
g_uOpClock = GetIntNum()*1000000;
GPIO_SetFunctionEach(eGPIO_F,eGPIO_13,2);
break;
case 3 :
pUartCon->cOpClock = 3;
printf("\nSelect Clock SRC\n 1.EPLL 2.MPLL \n Choose: ");
switch(GetIntNum())
{
case 1:
SYSC_SetPLL(eEPLL,32,1,1,0); //EPLL=192Mhz
SYSC_ClkSrc(eEPLL_FOUT);
SYSC_ClkSrc(eUART_MOUTEPLL);
SYSC_CtrlCLKOUT(eCLKOUT_EPLLOUT,0);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -