📄 uart270.c
字号:
/*
DM270 ARM Evaluation Software
(c)Texas Instruments 2003
*/
/**
\file uart270.c
\brief UART Related APIs
*/
#include <string.h>
#include <uart270.h>
#include <clkc270.h>
/**
\brief Set UART configuration
\param uartID UART ID, UART0 or UART1
\param uartConfig UART configuration paramters
\return if success, \c E_PASS, else error code
\see UART_ConfigData, UART_ID
\par Example:
\code
#include <uart270.h>
...
UART_ConfigData uart0;
uart0.baudRate = 115200;
uart0.charLength = UART_CHAR_LEN_8;
uart0.stopBits = UART_STOPBIT1;
uart0.parity = UART_EVEN_PARITY;
UART_setConfig( UART0, &uart0 ); // setup UART0
...
UART_sendString( UART0, "\r\n Hello, World !!! \r\n" ); // output over UART0
\endcode
*/
STATUS UART_setConfig( UART_ID uartID, UART_ConfigData *uartConfig ){
Uint32 uart_clk=0;
if(uartID == UART0)
{
UART0_FSET(RFCR0,RFCB,UART_CLEAR_FIFO);
UART0_FSET(TFCR0,TFCB,UART_CLEAR_FIFO);
if((CLKC_moduleSelectClockSource(CLK_UART0,CLKC_PLL_IN)) != E_PASS)
return E_INVALID_INPUT;
uart_clk = 27*1000*1000; //Clock for PLL_IN is 27MHz
//uart_clk = 27*1024*1024; //Clock for PLL_IN is 27MHz
UART0_RSET(BRSR0,((uart_clk/16/uartConfig->baudRate) - 1 ));
UART0_FSET(MSR0,SBLS,uartConfig->stopBits);
if(uartConfig->parity != UART_NO_PARITY)
{
UART0_FSET(MSR0,PEB,UART_PARITY_ENABLE);
UART0_FSET(MSR0, PSB, uartConfig->parity & 0x1);
}
else
UART0_FSET(MSR0,PEB,UART_PARITY_DISABLE);
return E_PASS;
}
else if(uartID == UART1)
{
UART1_FSET(RFCR1,RFCB,UART_CLEAR_FIFO);
UART1_FSET(TFCR1,TFCB,UART_CLEAR_FIFO);
if((CLKC_moduleSelectClockSource(CLK_UART1,CLKC_PLL_IN)) != E_PASS)
return E_INVALID_INPUT;
uart_clk = 26500000; //Clock for PLL_IN is 27MHz
UART1_RSET(BRSR1,((uart_clk/16/uartConfig->baudRate) - 1 ));
UART1_FSET(MSR1,SBLS,uartConfig->stopBits);
if(uartConfig->parity != UART_NO_PARITY)
{
UART1_FSET(MSR1,PEB,UART_PARITY_ENABLE);
UART1_FSET(MSR1, PSB, uartConfig->parity & 0x1);
}
else
UART1_FSET(MSR1,PEB,UART_PARITY_DISABLE);
return E_PASS;
}
return E_INVALID_INPUT;
}
/**
\brief Set UART Mode of Operation
\param uartID UART ID, UART0 or UART1
\param uartMode UART Mode paramters
\return if success, \c E_PASS, else error code
\see UART_ModeData, UART_ID
*/
STATUS UART_setMode( UART_ID uartID, UART_ModeData *uartMode ){
if(uartID == UART0)
{
UART0_FSET(RFCR0, RTL, uartMode->rxFifoSize);
UART0_FSET(TFCR0, TTL, uartMode->txFifoSize);
UART0_FSET(MSR0, RFTIE, uartMode->rxFifoIntEnable);
UART0_FSET(MSR0, TFTIE, uartMode->txFifoIntEnable);
UART0_FSET(MSR0, REIE, uartMode->rxErrorIntEnable);
UART0_FSET(MSR0, TOIC, uartMode->timeoutIntEnable);
return E_PASS;
}
else if(uartID == UART1)
{
UART1_FSET(RFCR1, RTL, uartMode->rxFifoSize);
UART1_FSET(TFCR1, TTL, uartMode->txFifoSize);
UART1_FSET(MSR1, RFTIE, uartMode->rxFifoIntEnable);
UART1_FSET(MSR1, TFTIE, uartMode->txFifoIntEnable);
UART1_FSET(MSR1, REIE, uartMode->rxErrorIntEnable);
UART1_FSET(MSR1, TOIC, uartMode->timeoutIntEnable);
return E_PASS;
}
return E_INVALID_INPUT;
}
/**
\brief Clear UART FIFO
\param uartID UART ID, UART0 or UART1
\param fifo UART FIFO between UART_FIFO_RX and UART_FIFO_TX
\return if success, \c E_PASS, else error code
\see UART_ID
*/
STATUS UART_clearFifo( UART_ID uartID, Uchar fifo ){
if(uartID == UART0)
{
if(fifo == UART_FIFO_RX)
UART0_FSET(RFCR0,RFCB,UART_CLEAR_FIFO);
else
UART0_FSET(TFCR0,TFCB,UART_CLEAR_FIFO);
return E_PASS;
}
else if(uartID == UART1)
{
if(fifo == UART_FIFO_RX)
UART1_FSET(RFCR1,RFCB,UART_CLEAR_FIFO);
else
UART1_FSET(TFCR1,TFCB,UART_CLEAR_FIFO);
return E_PASS;
}
return E_INVALID_INPUT;
}
/**
\brief Get the count of words in UART FIFO
\param uartID UART ID, UART0 or UART1
\param fifo UART FIFO between UART_FIFO_RX and UART_FIFO_TX
\return UART FIFO word count, 0 if error
\see UART_ID
*/
Uint32 UART_getCount(Uchar uartID, Uchar fifo ){
if(uartID == UART0){
if(fifo == UART_FIFO_RX)
return ( UART0_FGET(RFCR0,RWC)) ;
else
return ( UART0_FGET(TFCR0,TWC)) ;
}
else if(uartID == UART1){
if(fifo == UART_FIFO_RX)
return ( UART1_FGET(RFCR1,RWC)) ;
else
return ( UART1_FGET(TFCR1,TWC)) ;
}
//If there is an error in the UART ID returning 0
return 0;
}
/**
\brief Get the status of UART
\param uartID UART ID, UART0 or UART1
\return UART status, 0 if error
\see UART_ID
*/
Uint16 UART_getStatus(UART_ID uartID){
if(uartID == UART0)
return UART0_RGET(SR0);
else if(uartID == UART1)
return UART1_RGET(SR1);
else
//Anshuman - If there is an error in the UART ID returning 0
return 0;
}
/**
\brief Send Byte on UART
If transmit FIFO is full, it does not transmit the byte and returns with an error (E_DEVICE)
\param uartID UART ID, UART0 or UART1
\param byte Byte of data to be transmitted
\return if success, \c E_PASS, else error code
\see UART_ID
*/
STATUS UART_sendByte(UART_ID uartID, char byte){
if(UART_getCount(uartID, UART_FIFO_TX) < 32)
{
if(uartID == UART0)
{
UART0_FSET(DTRR0, DTR, byte);
return E_PASS;
}
else if(uartID == UART1)
{
UART1_FSET(DTRR1, DTR, byte);
return E_PASS;
}
}
return E_DEVICE;
}
/**
\brief Receive Byte on UART
If receive FIFO is empty, it does not receive the byte and returns with an error (E_DEVICE)
\param uartID UART ID, UART0 or UART1
\param byte Pre-initialized data variable to receive byte
\return if success, \c E_PASS, else error code
\see UART_ID
*/
STATUS UART_recvByte(UART_ID uartID, char* byte){
Uint16 regVal;
if( UART_getCount(uartID, UART_FIFO_RX) > 0)
{
if(uartID == UART0)
regVal = UART0_RGET(DTRR0);
else if(uartID == UART1)
regVal = UART1_RGET(DTRR1);
if( (regVal & 0xFF00) == 0x1000 )
{
*byte = (Uchar)(regVal & 0xFF);
return E_PASS;
}
else
return E_INVALID_INPUT;
}
return E_DEVICE;
}
/**
\brief Send data on UART
If transmit FIFO becomes full, it waits till there is space to transmit the data.
This routine returns only when all the data has been transmitted
\param uartID UART ID, UART0 or UART1
\param dataAddress Address of data location to be transmitted
\param dataSize Size of data to be transmitted
\return if success, \c E_PASS, else error code
\see UART_ID
*/
STATUS UART_send(UART_ID uartID, char *dataAddress, Uint16 dataSize){
while(dataSize > 0) {
if(UART_getCount(uartID, UART_FIFO_TX) < 32)
{
if(uartID == UART0)
{
UART0_FSET( DTRR0,DTR, *(dataAddress++));
dataSize--;
}
else if(uartID == UART1)
{
UART1_FSET( DTRR1, DTR,*(dataAddress++));
dataSize--;
}
else
return E_INVALID_INPUT;
}
}
return E_PASS;
}
/**
\brief Receive data on UART
If receive FIFO becomes empty, it waits till new data is received.
This routine returns only when all the data has been received
\param uartID UART ID, UART0 or UART1
\param dataAddress Address of location where data is received
\param dataSize Size of data to be received
\return if success, \c E_PASS, else error code
\see UART_ID
*/
STATUS UART_recv(UART_ID uartID, char *dataAddress, Uint16 dataSize ){
while(dataSize > 0){
if(UART_getCount(uartID, UART_FIFO_RX) > 0){
if(uartID == UART0){
*dataAddress++ = (Uchar) ( UART0_RGET(DTRR0) & 0xFF );
dataSize--;
}
else if(uartID == UART1){
*dataAddress++ = (Uchar) ( UART1_RGET(DTRR1) & 0xFF );
dataSize--;
}
else
return E_INVALID_INPUT;
}
}
return E_PASS;
}
/**
\brief Send a string on UART
The string must be terminated by the '\\0' character
\param uartID UART ID, UART0 or UART1
\param string String to be transmitted
\return if success, \c E_PASS, else error code
\see UART_ID
*/
STATUS UART_sendString( UART_ID uartID, char *string ){
return (UART_send( uartID, string, strlen( (const char * ) string) ) );
}
/**
\brief Receive a string on UART
The routine returns when it receives a new line character, '\\n' or the carriage return character, '\\r' \n
The routine also exits when the length of the received string exceeds 'maxStringLength'
\param uartID UART ID, UART0 or UART1
\param string address of the string to be received
\param maxStringLength Maximum length of string that can be received
\return if success, \c E_PASS, else error code
\see UART_ID
*/
STATUS UART_recvString(UART_ID uartID, char *string, Uint16 maxStringLength){
Uint16 recvStrLen = 0;
while(recvStrLen < maxStringLength){
if(UART_getCount(uartID, UART_FIFO_RX) > 0){
if(uartID == UART0) {
*string = (Uchar) ( UART0_RGET(DTRR0) & 0xFF );
recvStrLen++;
}
else if(uartID == UART1) {
*string = (Uchar) ( UART1_RGET(DTRR1) & 0xFF );
recvStrLen++;
}
else
return E_INVALID_INPUT;
if(*(string) == '\n' || *(string) == '\r') {
*string='\0';
return E_PASS;
}
string++;
}
}
*string = '\0';
return E_DEVICE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -