📄 uart0.c
字号:
while(isDone);
Uart_Printf("%s\n",uart0RxStr);
Uart_Port_Return();
}
void __irq Uart0_TxFifo(void)
{
rINTSUBMSK|=(BIT_SUB_RXD0|BIT_SUB_TXD0|BIT_SUB_ERR0); // Just for the safety
while (!(rUFSTAT0 & 0x4000) && (*uart0TxStr != '\0')) //until tx fifo full or end of string
WrUTXH0(*uart0TxStr++);
if(*uart0TxStr == '\0')
{
rINTMSK|=BIT_UART0;
rSUBSRCPND=BIT_SUB_TXD0; // Clear Sub int pending
ClearPending(BIT_UART0); // Clear master pending
}
else
{
ClearPending(BIT_UART0); // Clear master pending
rSUBSRCPND=BIT_SUB_TXD0; // Clear Sub int pending
rINTSUBMSK&=~(BIT_SUB_TXD0); // Unmask sub int
}
}
void __irq Uart0_RxFifoOrErr(void)
{
rINTSUBMSK|=(BIT_SUB_RXD0|BIT_SUB_TXD0|BIT_SUB_ERR0);
if(rSUBSRCPND&BIT_SUB_RXD0) __sub_Uart0_RxFifo();
else __sub_Uart0_RxErrInt();
ClearPending(BIT_UART0);
rSUBSRCPND=(BIT_SUB_RXD0|BIT_SUB_ERR0); // Clear Sub int pending
rINTSUBMSK&=~(BIT_SUB_RXD0|BIT_SUB_ERR0);
}
void __sub_Uart0_RxFifo(void)
{
while((rUFSTAT0&0x100)||(rUFSTAT0&0xf)) //During the Rx FIFO is not empty
{
rx_point++;
if(rx_point<5)
rx_filesize |= (RdURXH0()<<(8*(rx_point-1))); // First 4-bytes mean file size
else if(rx_point>(rx_filesize-2))
{
rx_dncs |= (RdURXH0()<<(8*(1-(rx_filesize-rx_point)))); //Last 2-bytes mean checksum.
if(rx_point==rx_filesize) rx_isdone=0;
}
else
rx_checksum+=RdURXH0();
}
}
void Test_Uart0_Fifo(void)
{
Uart_Port_Set();
Uart_Select(0);
/******* UART0 Tx FIFO test with interrupt ********/
Uart_Printf("[Uart channel 0 Tx FIFO Interrupt Test]\n");
Uart_TxEmpty(0); //wait until tx buffer is empty.
/* <Tx Trigger Level:empty> */
uart0TxStr="ABCDEFGHIJKLMNOPQRSTUVWXYZ->UART0 Tx FIFO interrupt(empty) test is good!!!!\r\n";
pISR_UART0=(U32)Uart0_TxFifo;
rULCON0=(0<<6)|(0<<3)|(0<<2)|(3); // Normal,No parity,One stop bit, 8bit
rUCON0 &= 0x400; // For the PCLK <-> UCLK fuction
rUCON0 |= (TX_INTTYPE<<9)|(RX_INTTYPE<<8)|(0<<7)|(0<<6)|(0<<5)|(0<<4)|(1<<2)|(0);
//Clock,Tx:Def,Rx:Def,Rx timeout:x,Rx error int:x,Loop-back:x,Send break:x,Tx:int,Rx:x
rUFCON0=(2<<6)|(1<<4)|(1<<2)|(1<<1)|(1);
//Tx and Rx FIFO Trigger Level:8byte,Tx and Rx FIFO Reset,FIFO on
rINTMSK=~(BIT_UART0);
rINTSUBMSK=~(BIT_SUB_TXD0);
Delay(500);
// edited by junon
/* <Tx Trigger Level:16Byte> */
uart0TxStr="ABCDEFGHIJKLMNOPQRSTUVWXYZ->UART0 Tx FIFO interrupt(16byte) test is good!!!!\r\n";
rUFCON0=(1<<6)|(2<<4)|(1<<2)|(1<<1)|(1);
rINTMSK=~(BIT_UART0);
rINTSUBMSK=~(BIT_SUB_RXD0|BIT_SUB_TXD0|BIT_SUB_ERR0);
Delay(500);
/* <Tx Trigger Level:32Byte> */
uart0TxStr="ABCDEFGHIJKLMNOPQRSTUVWXYZ->UART0 Tx FIFO interrupt(32byte) test is good!!!!\r\n";
rUFCON0=(2<<6)|(2<<4)|(1<<2)|(1<<1)|(1);
rINTMSK=~(BIT_UART0);
rINTSUBMSK=~(BIT_SUB_RXD0|BIT_SUB_TXD0|BIT_SUB_ERR0);
Delay(500);
/* <Tx Trigger Level:48Byte> */
uart0TxStr="ABCDEFGHIJKLMNOPQRSTUVWXYZ->UART0 Tx FIFO interrupt(48byte) test is good!!!!\r\n";
rUFCON0=(3<<6)|(2<<4)|(1<<2)|(1<<1)|(1);
//Tx and Rx FIFO Trigger Level:12byte,Tx and Rx FIFO Reset,FIFO on
rINTMSK=~(BIT_UART0);
rINTSUBMSK=~(BIT_SUB_RXD0|BIT_SUB_TXD0|BIT_SUB_ERR0);
Delay(500);
rUFCON0=(3<<6)|(2<<4)|(1<<2)|(1<<1)|(0);
//Tx and Rx FIFO Trigger Level:12byte,Tx and Rx FIFO Reset,FIFO off
/******* UART0 Rx FIFO test with interrupt ********/
rx_dncs=0;
rx_point=0;
rx_isdone=1;
rx_filesize=0;
rx_checksum=0;
Uart_Printf("[Uart channel 0 Rx FIFO Interrupt Test]\n");
//----------------------------------------------------
pISR_UART0=(unsigned)Uart0_RxFifoOrErr;
rULCON0=(0<<6)|(0<<3)|(0<<2)|(3); // Normal,No parity,One stop bit, 8bit
rUCON0 &= 0x400; // For the PCLK <-> UCLK fuction
rUCON0 |= (TX_INTTYPE<<9)|(RX_INTTYPE<<8)|(1<<7)|(1<<6)|(0<<5)|(0<<4)|(1<<2)|(1);
//Clock,Tx:Def,Rx:Def,Rx timeout:o,Rx error int:o,Loop-back:x,Send break:x,Tx:int,Rx:int
rUFCON0=(1<<6)|(0<<4)|(1<<2)|(1<<1)|(1);
//Tx and Rx FIFO Trigger Level:4byte,Tx and Rx FIFO Reset,FIFO on
// Clear Int Pending and Unmask
ClearPending(BIT_UART0);
rINTMSK=~(BIT_UART0);
rSUBSRCPND=(BIT_SUB_RXD0|BIT_SUB_TXD0|BIT_SUB_ERR0);
rINTSUBMSK=~(BIT_SUB_RXD0|BIT_SUB_ERR0);
Uart_Printf("Download the target file[*.bin] by Uart0\n");
while(rx_isdone);
rINTMSK |= (BIT_UART0);
rINTSUBMSK|=(BIT_SUB_RXD0|BIT_SUB_TXD0|BIT_SUB_ERR0);
rUFCON0=(3<<6)|(2<<4)|(1<<2)|(1<<1)|(0);
//Tx and Rx FIFO Trigger Level:32byte,Tx and Rx FIFO Reset,FIFO off
if(rx_dncs==(rx_checksum&0xffff))
{
Uart_Printf("\nDownload test OK!!!\n");
Uart_Printf("\nUART0 Tx FIFO interrupt(48byte) test is good!!!!\n");
}
else
Uart_Printf("\nError!!!\n");
Uart_Port_Return();
}
void __irq Uart0_AfcTx(void)
{
rINTSUBMSK|=(BIT_SUB_RXD0|BIT_SUB_TXD0|BIT_SUB_ERR0);
ClearPending(BIT_UART0);
rSUBSRCPND=(BIT_SUB_TXD0);
if( !(rUFSTAT0&(0x40<<8)) ) // until fifo is full.
{
WrUTXH0(*txdataPt);
*dbg_data++ = (rUFSTAT0>>8)&0x3f;
*dbg_data++ = rUMSTAT0;
tx_cnt++;
if( *txdataPt == 0 ) // if data is null.
{
tx_end=1;
while(rUFSTAT0 & 0x7f00); //Until FIFO is empty
rINTMSK|=(BIT_UART0);
return;
}
txdataPt++;
}
rINTSUBMSK&=~(BIT_SUB_TXD0);
#if 0
rINTSUBMSK|=(BIT_SUB_RXD0|BIT_SUB_TXD0|BIT_SUB_ERR0);
if(tx_cnt<AFC_BUFLEN)
{
Uart_Printf("%d,",*txdataPt);
WrUTXH0(*txdataPt++);
tx_cnt++;
ClearPending(BIT_UART0);
rSUBSRCPND=(BIT_SUB_TXD0);
rINTSUBMSK&=~(BIT_SUB_TXD0);
}
else
{
tx_end=1;
while(rUFSTAT0 & 0x7f00); //Until FIFO is empty
ClearPending(BIT_UART0);
rSUBSRCPND=(BIT_SUB_TXD0);
rINTMSK|=BIT_UART0;
}
#endif
}
void __irq Uart0_AfcRxOrErr(void)
{
rINTSUBMSK|=(BIT_SUB_RXD0|BIT_SUB_TXD0|BIT_SUB_ERR0);
if(rSUBSRCPND&BIT_SUB_RXD0) __sub_Uart0_RxAfc();
else __sub_Uart0_RxErrInt();
ClearPending(BIT_UART0);
rSUBSRCPND=(BIT_SUB_RXD0|BIT_SUB_TXD0|BIT_SUB_ERR0);
rINTSUBMSK&=~(BIT_SUB_RXD0|BIT_SUB_TXD0|BIT_SUB_ERR0);
}
void __sub_Uart0_RxAfc(void)
{
while( rUFSTAT0 & 0x7f ) // until data is absent..
{
*rxdataPt=rURXH0;
*dbg_data++ = rUFSTAT0&0x3f;
*dbg_data++ = rUMSTAT0;
rx_cnt++;
if(*rxdataPt == 0)
{
rx_end=1;
rINTMSK|=BIT_UART0;
}
rxdataPt++;
}
#if 0
while( (rUFSTAT0 & 0x40) || ((rUFSTAT0 & 0x3f) >0) )
{
*rxdataPt=rURXH0;
Uart_Printf("%d,",*rxdataPt++);
rx_cnt++;
}
if(rx_cnt == AFC_BUFLEN)
{
rx_end=1;
rINTMSK|=BIT_UART0;
}
#endif
}
void Test_Uart0_AfcTx(void)
{
int i, baudrate;
tx_cnt=0;
tx_end=0;
txdataFl=(volatile U8 *)UARTBUFFER;
txdataPt=(volatile U8 *)UARTBUFFER;
dbg_data=(volatile U32 *)(UARTBUFFER+0x1000);
for(i=1;i<AFC_BUFLEN;i++) *txdataFl++=i; // Initialize the AFC data
*txdataFl = 0;
Uart_Port_Set();
Uart_Select(0);
Uart_Printf("[Uart channel 0 AFC Tx Test]\n");
Uart_Printf("This test should be configured two boards.\n");
Uart_Printf("Connect Tx and Rx Board with twitsted(rx/tx, nCTS/nRTS) cable .\n");
Uart_Printf("\nConnect PC[COM1 or COM2] and UART1 of SMDK24A0 with a serial cable!!! \n");
Uart_Printf("Then, press any key........\n");
Uart_Select(1); // Change the uart port
Uart_Getch();
Uart_Printf("buadrate(D:115200) = ");
baudrate = Uart_GetIntNum();
if (baudrate = '\r')
rUBRDIV0 = (int)(PCLK/16./115200+0.5)-1;
else
rUBRDIV0 = (int)(PCLK/16./baudrate+0.5)-1;
Uart_Printf("UBRDIV = %d\n",rUBRDIV0);
Uart_Printf("Start Rx first and then press any key to start Tx.....\n");
Uart_Getch();
pISR_UART0=(unsigned) Uart0_AfcTx;
rULCON0=(0<<6)|(0<<3)|(0<<2)|(3); // Normal,No parity,One stop bit, 8bit
rUCON0 &= 0x400; // For the PCLK <-> UCLK fuction
rUCON0 |= (1<<9)|(1<<8)|(0<<7)|(0<<6)|(0<<5)|(0<<4)|(1<<2)|(1);
//Clock,Tx:Lev,Rx:Lev,Rx timeout:x,Rx error int:x,Loop-back:x,Send break:x,Tx:int,Rx:int
rUFCON0=(3<<6)|(0<<4)|(1<<2)|(1<<1)|(1);
//Tx and Rx FIFO Trigger Level:4byte,Tx and Rx FIFO Reset,FIFO on
rUMCON0=0x10; // Enable Uart0 AFC
rINTMSK=~(BIT_UART0);
rINTSUBMSK=~(BIT_SUB_RXD0|BIT_SUB_TXD0|BIT_SUB_ERR0);
while(!tx_end);
rINTSUBMSK|=(BIT_SUB_RXD0|BIT_SUB_TXD0|BIT_SUB_ERR0);
rUFCON0=(3<<6)|(2<<4)|(1<<2)|(1<<1)|(0);
//Tx and Rx FIFO Trigger Level:12byte,Tx and Rx FIFO Reset,FIFO off
rxdataPt=(volatile U8 *)UARTBUFFER;
dbg_data=(volatile U32 *)(UARTBUFFER+0x1000);
Uart_Printf("[Rx data, CTS satus, FIFO count\n");
for (i=0;i<AFC_BUFLEN;i++)
Uart_Printf("[%3d,0x%2x,%2d] ",*rxdataPt++,*dbg_data++,*dbg_data++);
Uart_Printf("\nEnd Tx, transfer data count=%d\n",tx_cnt);
Uart_Printf("\nConnect PC[COM1 or COM2] and UART0 of SMDK24A0 with a serial cable!!! \n");
Uart_Printf("Then, press any key........\n");
Uart_Select(0);
Uart_Getch();
Uart_Port_Return();
}
void Test_Uart0_AfcRx(void)
{
unsigned int i, baudrate;
rx_cnt=0;
rx_end=0;
afc_err=0;
rxdataCk=(volatile U8 *)UARTBUFFER;
rxdataPt=(volatile U8 *)UARTBUFFER;
dbg_data=(volatile U32 *)(UARTBUFFER+0x1000);
Uart_Port_Set();
Uart_Select(0);
Uart_Printf("[Uart channel 0 AFC Rx Test]\n");
Uart_Printf("This test should be configured two boards.\n");
Uart_Printf("Connect Tx and Rx Board with twitsted(rx/tx, nCTS/nRTS) cable .\n");
Uart_Printf("\nConnect PC[COM1 or COM2] and UART1 of SMDK24A0 with a serial cable!!! \n");
Uart_Printf("Then, press any key........\n");
Uart_Select(1); // Change the uart port
Uart_Getch();
Uart_Printf("buadrate(D:115200) = ");
baudrate = Uart_GetIntNum();
if (baudrate = '\r')
rUBRDIV0 = (int)(PCLK/16./115200+0.5)-1;
else
rUBRDIV0 = (int)(PCLK/16./baudrate+0.5)-1;
Uart_Printf("UBRDIV = %d\n",rUBRDIV0);
Uart_Printf("Press any key to start Rx and then Star Tx....\n");
Uart_Getch();
pISR_UART0=(unsigned) Uart0_AfcRxOrErr;
rULCON0=(0<<6)|(0<<3)|(0<<2)|(3); // Normal,No parity,One stop bit, 8bit
rUCON0 &= 0x400; // For the PCLK <-> UCLK fuction
rUCON0 |= (0<<9)|(0<<8)|(1<<7)|(1<<6)|(0<<5)|(0<<4)|(1<<2)|(1);
//Clock,Tx:Lev,Rx:Lev,Rx timeout:o,Rx error int:o,Loop-back:x,Send break:x,Tx:o,Rx:o
rUFCON0=(1<<6)|(0<<4)|(1<<2)|(1<<1)|(1);
//Tx and Rx FIFO Trigger Level:4byte,Tx and Rx FIFO Reset,FIFO on
rUMCON0=0x10; // Enable Uart0 AFC
rSUBSRCPND=(BIT_SUB_RXD0|BIT_SUB_TXD0|BIT_SUB_ERR0);
ClearPending(BIT_UART0);
rINTSUBMSK=~(BIT_SUB_RXD0|BIT_SUB_TXD0|BIT_SUB_ERR0);
rINTMSK=~(BIT_UART0);
while(!rx_end);
rINTSUBMSK|=(BIT_SUB_RXD0|BIT_SUB_TXD0|BIT_SUB_ERR0);
rINTMSK=(BIT_UART0);
rUFCON0=(3<<6)|(2<<4)|(1<<2)|(1<<1)|(0);
//Tx and Rx FIFO Trigger Level:12byte,Tx and Rx FIFO Reset,FIFO off
rxdataPt=(volatile U8 *)UARTBUFFER;
dbg_data=(volatile U32 *)(UARTBUFFER+0x1000);
Uart_Printf("[Rx data, CTS satus, FIFO count\n");
for (i=0;i<AFC_BUFLEN;i++)
Uart_Printf("[%3d,0x%2x,%2d] ",*rxdataPt++,*dbg_data++,*dbg_data++);
Uart_Printf("\nEnd Rx, receive data count=%d\n",rx_cnt);
for(i=1;i<AFC_BUFLEN;i++)
if(i-(*rxdataCk++)) {
Uart_Printf("i=%d\n",i);
afc_err++;
}
if(afc_err)
Uart_Printf("AFC test fail!! Error count=%d\n",afc_err);
else
Uart_Printf("AFC test is good!!\n");
Uart_Printf("\nConnect PC[COM1 or COM2] and UART0 of SMDK24A0 with a serial cable!!! \n");
Uart_Printf("Then, press any key........\n");
Uart_Select(0);
Uart_Getch();
Uart_Port_Return();
}
void Test_Uart0_RxErr(void) // need two serial port cables.
{
U8 ch;
U8 cError;
Uart_Port_Set();
Uart_Select(1);
Uart_Printf("\nConnect PC[COM1 or COM2] and UART0 of SMDK2440 with a serial cable!!! \n");
Uart_Printf("In this case, Uart0 : test port, Uart1 : debug port\n");
Uart_Printf("Then, press any key........\n");
// Uart_Select(0); // Change the uart port
Uart_Getch();
while(1)
{
/*********** UART0 Rx test with interrupt ***********/
isRxInt=1;
uart0RxStr=(char *)UARTBUFFER;
Uart_Printf("\n[Uart channel 0 Rx Error Check]\n");
Uart_TxEmpty(1); //wait until tx buffer is empty.
rUFCON0=(1<<6)|(0<<4)|(1<<2)|(1<<1)|0; // FIFO disable
// for 2440A. add Frame error, Parity error, Break detect check.
Uart_Printf("\n1. Overrun Error check[D] 2. Frame error 3. Parity error \n");
cError = Uart_Getch();
if (cError== '2')
{
pISR_UART0 =(unsigned)Uart0_RxIntOrErr;
rULCON0=(0<<6)|(0<<3)|(0<<2)|(2); // Normal,No parity,One stop bit, 8bit
rUCON0 = (TX_INTTYPE<<9)|(RX_INTTYPE<<8)|(0<<7)|(1<<6)|(0<<5)|(0<<4)|(1<<2)|(1);
//Clock,Tx:pulse,Rx:pulse,Rx timeout:x,Rx error int:o,Loop-back:x,Send break:x,Tx:int,Rx:int
}
else if (cError== '3')
{
pISR_UART0 =(unsigned)Uart0_RxIntOrErr;
rULCON0=(0<<6)|(5<<3)|(0<<2)|(3); // Normal,No parity,One stop bit, 8bit
rUCON0 = (TX_INTTYPE<<9)|(RX_INTTYPE<<8)|(0<<7)|(1<<6)|(0<<5)|(0<<4)|(1<<2)|(1);
//Clock,Tx:pulse,Rx:pulse,Rx timeout:x,Rx error int:o,Loop-back:x,Send break:x,Tx:int,Rx:int
}
else
{
pISR_UART0 = (unsigned)Uart0_RxOverrunErr;
rULCON0=(0<<6)|(0<<3)|(0<<2)|(3); // Normal,No parity,One stop bit, 8bit
rUCON0 = (TX_INTTYPE<<9)|(RX_INTTYPE<<8)|(0<<7)|(1<<6)|(0<<5)|(0<<4)|(1<<2)|(1);
//Clock,Tx:pulse,Rx:pulse,Rx timeout:x,Rx error int:o,Loop-back:x,Send break:x,Tx:int,Rx:int
Uart_Printf("1. Using FIFO 2. Not using FIFO[D] \n");
if (Uart_Getch() == '1')
{
rUFCON0 |= 1;
Uart_Printf("Press Any key as 65 times in UART0 terminal window..\n");
Uart_Printf("then Overrun error will be occured.. \n");
}
else
{
rUFCON0 &= ~1;
Uart_Printf("Press Any key as 2 times in UART0 terminal window..\n");
Uart_Printf("then Overrun error will be occured.. \n");
}
}
// Clear Int Pending and Unmask
rSUBSRCPND=(BIT_SUB_TXD0|BIT_SUB_RXD0|BIT_SUB_ERR0);
ClearPending(BIT_UART0);
rINTSUBMSK=~(BIT_SUB_RXD0|BIT_SUB_ERR0);
rINTMSK=~(BIT_UART0);
while(isRxInt);
// UART0 mask
rINTSUBMSK|=(BIT_SUB_TXD0|BIT_SUB_RXD0|BIT_SUB_ERR0);
rINTMSK|=(BIT_UART0);
rUFCON0 |= 3<<1; // fifo reset
Uart_Printf("1. One more 2. Exit[D] \n");
if (Uart_Getch() == '1') continue;
else break;
}
Uart_Port_Return();
}
void __irq Uart0_RxOverrunErr(void)
{
rINTSUBMSK|=(BIT_SUB_RXD0|BIT_SUB_TXD0|BIT_SUB_ERR0);
if(rSUBSRCPND&BIT_SUB_ERR0)
{
__sub_Uart0_RxErrInt();
ClearPending(BIT_UART0);
rSUBSRCPND=(BIT_SUB_RXD0|BIT_SUB_ERR0); // Clear Sub int pending
return;
}
rINTSUBMSK&=~(BIT_SUB_RXD0|BIT_SUB_ERR0);
}
//---------------------------------------UART0 test function-------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -