⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart.c

📁 三星s3c2460开发板完整功能测试代码
💻 C
📖 第 1 页 / 共 3 页
字号:
		rDIDSTC1= (0<<1)|(0);		// AHB,Increment
		rDCON1=(1<<31)|(0<<30)|(1<<29)|(0<<28)|(0<<27)|(1<<22)|(0<<20)|(DMA_BUF_LEN);
		//handshake, sync PCLK, TC int, single tx, single service,auto-reload off, Byte size Tx, Tx count value
		rDMAREQSEL1 = ((20+ch*2)<<1)|1;

		// Clear Int Pending and Unmask    
		rSUBSRCPND = ((1<<ch)|BIT_SUB_DMA1);        
		ClearPending(BIT_DMA_SBUS|BIT_UART);
		rINTMSK &= ~(BIT_DMA_SBUS|BIT_UART);
		rINTSUBMSK &= ~((1<<ch)|BIT_SUB_DMA1);
		rDMASKTRIG1 = (0<<2)|(1<<1)|(0);    //no-stop, DMA1 channel on, no-SW trigger 
	}
	
	while(!isRxDone[ch]);

	// for debugging fifo 
	if ( (pUartRegs->rUfCon & 1) && (iTemp == 1) ) // 1 : fifo enable 
	{
		pFifoDebug = (unsigned int *)FIFO_DEBUG_BUF;
		while(*pFifoDebug)
			printf("[0x%x,%d,0x%x,%d] ", pFifoDebug, *pFifoDebug++, *pFifoDebug++,*pFifoDebug++);
	}
	pUartRxStr[ch] = ( char *)(UART_BUF+DMA_BUF_LEN);

	return pUartRxStr[ch];
}


void TestUart(void)
{
	unsigned char ch;

	ch = UartConfig();
	UartOpen(ch); 

	// UART Tx test with interrupt
	printf("\n[Uart channel %d Tx Test]\n",ch);
	TxString(ch, TestString);
	printf("\nTx Done~ \n");

	// UART Rx test with interrupt 
	printf("\n[Uart channel %d Rx Test]\n",ch);
	printf("Case 1 : Interrupt mode. After typing characters and pressing ENTER key.\n");
	printf("Case 2 : Interrupt FIFO mode & DMA mode. After typing characters and pressing ENTER key until FIFO level trigger.\n");	
	printf(" Then, you will see those characters.\n");
	printf("\nRx : %s \n",RxString(ch));
	printf("\nRx Done~ \n");

	// Test End
	printf("\nComplete UART test!!! \n");
	UartClose(ch);
	SetDbgUart(0); // change channel setting for debug port set
}


void UartTx(void)
{
	unsigned char ch;
	char str[128];

	ch = UartConfig();
	UartOpen(ch); 

	printf("\n[Uart%1d Tx String]\n",ch);
	printf("\nIf you want to exit, press 'ESC' key at first. The character instead of 'ESC' is ignored.\n");
	while (getchar() != 0x1b)
	{
		gets(str);
//		printf("Tx : ");
		TxString(ch, str);
		printf("\nIf you want to exit, press 'ESC' key at first. The character instead of 'ESC' is ignored.\n");
	}	

	UartClose(ch);
	SetDbgUart(0); // change channel setting for debug port set
}


void UartRx(void)
{
	unsigned char ch;

	ch = UartConfig();
	UartOpen(ch); 
//	SetLineCon(ch, 0, 5, 1);  // for Rx error interrupt test

	printf("\n[Uart%1d Rx String]\n",ch);
	printf("\nIf you want to exit, press 'ESC' key at first. The character instead of 'ESC' is ignored.\n");
	while (getchar() != 0x1b)
	{
		printf("Rx : %s ", RxString(ch));		
		printf("\nIf you want to exit, press 'ESC' key at first. The character instead of 'ESC' is ignored.\n");
	}	

	UartClose(ch);
	SetDbgUart(0); // change channel setting for debug port set
}


#if 0

void __sub_Uart0_RxFifo(void)
{
    while(rUFSTAT0&0x7f)	//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 __irq Uart0_AfcTx(void)
{
    rINTSUBMSK|=(BIT_SUB_RXD0|BIT_SUB_UART0|BIT_SUB_ERR0);

    if(tx_cnt<AFC_BUFLEN)
    {
    	printf("%d,",*txdataPt);
    	WrUTXH0(*txdataPt++);
		tx_cnt++;
        ClearPending(BIT_UART);
        rSUBSRCPND=(BIT_SUB_UART0);
        rINTSUBMSK&=~(BIT_SUB_UART0);
    }
    else
    {
  	 	tx_end=1;
        while(rUFSTAT0 & 0x7f00);	//Until FIFO is empty
        ClearPending(BIT_UART);
        rSUBSRCPND=(BIT_SUB_UART0);
    	rINTMSK|=BIT_UART;
    }
}

void __irq Uart0_AfcRxOrErr(void)
{
    rINTSUBMSK|=(BIT_SUB_RXD0|BIT_SUB_UART0|BIT_SUB_ERR0);
    if(rSUBSRCPND&BIT_SUB_RXD0) __sub_Uart0_RxAfc();    
    else __sub_Uart0_RxErrInt();

    ClearPending(BIT_UART);
    rSUBSRCPND=(BIT_SUB_RXD0|BIT_SUB_UART0|BIT_SUB_ERR0);
    rINTSUBMSK&=~(BIT_SUB_RXD0|BIT_SUB_UART0|BIT_SUB_ERR0);
}

void __sub_Uart0_RxAfc(void)
{
    while( rUFSTAT0 & 0x7f )
    {
	*rxdataPt=rURXH0;
	printf("%d,",*rxdataPt++);
	rx_cnt++;
    }
    if(rx_cnt == AFC_BUFLEN) 
    {
   	rx_end=1;
    	rINTMSK|=BIT_UART;
    }
}
 
void Test_Uart0_AfcTx(void)
{
    int i;
    tx_cnt=0;
    tx_end=0;
    txdataFl=(volatile unsigned char *)UARTBUFFER;
    txdataPt=(volatile unsigned char *)UARTBUFFER;
    for(i=0;i<AFC_BUFLEN;i++) *txdataFl++=i;	// Initialize the AFC data
    Uart_Port_Set(); 
    Uart_Select(1);
    printf("[Uart channel 0 AFC Tx Test]\n");
    printf("This test should be configured two boards.\n");
    printf("Connect Tx and Rx Board with twitsted(rx/tx, nCTS/nRTS) cable .\n");
     
   
    pISR_UART=(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=(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 

    printf("\nKeep the connection between PC[COM1 or COM2] and UART1 of SMDK2410!!! \n");
    printf("Press any key to start Rx and then Star Tx....\n");
    Uart_TxEmpty(1);
    getchar();
  
    // Clear Int Pending and Unmask 
    rINTMSK=~(BIT_UART);
    rINTSUBMSK=~(BIT_SUB_UART0);
	
     while(!tx_end);

     rINTMSK|=(BIT_UART);
    rINTSUBMSK|=(BIT_SUB_RXD0|BIT_SUB_UART0|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
    printf("\nEnd Tx, transfer data count=%d\n",tx_cnt);
	

    Uart_Port_Return();
}

void Test_Uart0_AfcRx(void)
{
    unsigned int i;
    rx_cnt=0;
    rx_end=0;
    afc_err=0;
    rxdataCk=(volatile unsigned char *)UARTBUFFER;
    rxdataPt=(volatile unsigned char *)UARTBUFFER;
    Uart_Port_Set(); 
    Uart_Select(1);
    printf("[Uart channel 0 AFC Rx Test]\n");
    printf("This test should be configured two boards.\n");
    printf("Connect Tx and Rx Board with twitsted(rx/tx, nCTS/nRTS) cable .\n");
    
    pISR_UART=(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 |= (1<<9)|(1<<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 

    
    printf("\nKeep the connection between PC[COM1 or COM2] and UART0 of SMDK2410!!! \n");
    printf("Press any key to start Rx and then Star Tx....\n");
    getchar();

 
	   // Clear Int Pending and Unmask 
     rSUBSRCPND=(BIT_SUB_RXD0|BIT_SUB_UART0|BIT_SUB_ERR0);
     rINTSUBMSK=~(BIT_SUB_RXD0|BIT_SUB_ERR0);
     ClearPending(BIT_UART);
     rINTMSK=~(BIT_UART);

    while(!rx_end);

  //  rINTMSK|=BIT_UART;
    rINTSUBMSK|=(BIT_SUB_RXD0|BIT_SUB_UART0|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
    printf("\nEnd Rx, receive data count=%d\n",rx_cnt);
    for(i=0;i<AFC_BUFLEN;i++) 
    	if(i-(*rxdataCk++)) {
    		printf("i=%d\n",i);
    		afc_err++;
    		}
    if(afc_err)
    	printf("AFC test fail!! Error count=%d\n",afc_err);
    else
    	printf("AFC test is good!!\n");

    Uart_Port_Return();
}


// added by junon start
void __irq Uart0_RxOverrunErr(void)
{
    rINTSUBMSK|=(BIT_SUB_RXD0|BIT_SUB_UART0|BIT_SUB_ERR0);
    if(rSUBSRCPND&BIT_SUB_ERR0) 
	{
		__sub_Uart0_RxErrInt();
    	ClearPending(BIT_UART); 
	    rSUBSRCPND=(BIT_SUB_RXD0|BIT_SUB_ERR0);	// Clear Sub int pending    
		return;
    }
    rINTSUBMSK&=~(BIT_SUB_RXD0|BIT_SUB_ERR0);    
}


void Test_Uart0_RxErr(void) // need two serial port cables.
{
	unsigned char ch;
	unsigned char cError;
	
    Uart_Port_Set(); 
   
    Uart_Select(1);
    printf("\nConnect PC[COM1 or COM2] and UART0 of SMDK2460 with a serial cable!!! \n");
    printf("In this case, Uart0 : test port,  Uart1 : debug port\n");
    printf("Then, press any key........\n");
//    Uart_Select(0);	// Change the uart port    
    getchar();
			
  	while(1)
	{
	    /*********** UART0 Rx test with interrupt ***********/
	    isRxInt=1;
	    uart0RxStr=(char *)UARTBUFFER;			
	    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 2460A. add Frame error, Parity error, Break detect check.
	    printf("\n1. Overrun Error check[D]   2. Frame error   3. Parity error  \n"); 
		cError = getchar();
	    if (cError== '2')
		{
			pISR_UART =(unsigned)Uart0_RxIntOrErr;

			rULCON0=(0<<6)|(4<<3)|(0<<2)|(0); // Normal,No parity,One stop bit, 7bit
			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
			printf("This port was set 7 data bit, no parity, 1 stop bit. Send just characters..\n");
	    }
		else if (cError== '3')
		{
			pISR_UART =(unsigned)Uart0_RxIntOrErr;
		
			rULCON0=(0<<6)|(5<<3)|(0<<2)|(3); // Normal,Even 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
			printf("This port was set 8 data bit, even parity, 1 stop bit. Send just characters..\n");
		}
	    else 
		{
			pISR_UART = (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

			printf("1. Using FIFO	2. Not using FIFO[D] \n");
			if (getchar() == '1') 
			{
				rUFCON0 |= 1;
				printf("Press Any key as 65 times in UART0 terminal window..\n");
				printf("then Overrun error will be occured.. \n"); 
			}
			else
			{
				rUFCON0 &= ~1;
				printf("Press Any key as 2 times in UART0 terminal window..\n");
				printf("then Overrun error will be occured.. \n");
			}
		}
		
	    // Clear Int Pending and Unmask    
	    rSUBSRCPND=(BIT_SUB_UART0|BIT_SUB_RXD0|BIT_SUB_ERR0);    
	    ClearPending(BIT_UART);
	    rINTSUBMSK=~(BIT_SUB_RXD0|BIT_SUB_ERR0);
	    rINTMSK=~(BIT_UART);

	    while(isRxInt);

		// UART0 mask
    	rINTSUBMSK|=(BIT_SUB_UART0|BIT_SUB_RXD0|BIT_SUB_ERR0);
		rINTMSK|=(BIT_UART);

		rUFCON0 |= 3<<1; // fifo reset
		printf("1. One more  2. Exit[D] \n");
		if (getchar() == '1') continue;
		else break;
	}	

    Uart_Port_Return();	
}

#if 0
void Enter_Uart0_ISR(void)
{
	rINTSUBMSK|=(BIT_SUB_RXD0|BIT_SUB_UART0|BIT_SUB_ERR0);
	rINTMSK |= BIT_UART;
	ClearPending(BIT_UART);
	rSUBSRCPND=(BIT_SUB_RXD0|BIT_SUB_UART0|BIT_SUB_ERR0);	
}

void Exit_Uart0_ISR(void)
{
	rINTSUBMSK&=~(BIT_SUB_RXD0|BIT_SUB_UART0|BIT_SUB_ERR0);
	rINTMSK &= ~(BIT_UART);
}

void __irq Uart0_Cts(void)
{
	Enter_Uart0_ISR();

	Exit_Uart0_ISR();	
}

void Test_Rts_Rx(void)
{
    int i = 1;

    Uart_Port_Set(); 
    //unsigned int safc_rGPHCON,safc_rGPHDAT,safc_rGPHUP;
    tx_cnt=0;
    Uart_Select(1);
    printf("[Uart channel 0 CTS Rx Test]\n");
    printf("Check nCTS0 signal with oscilloscope! \n");
	printf("When FIFO nuber is 32, Active nRTS0... \n");
    
//    pISR_UART=(unsigned) Uart0_Cts;

    rULCON0=(0<<6)|(0<<3)|(0<<2)|(3);	// Normal,No parity,One stop bit, 8bit
    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)|(3<<4)|(1<<2)|(1<<1)|(1);
    //Tx and Rx FIFO Trigger Level:32byte,Tx and Rx FIFO Reset,FIFO on
    rUMCON0=0x10;   // Enable Uart0 AFC 

//	rINTMSK&=~(BIT_UART);
//	rINTSUBMSK&=~(BIT_SUB_RXD0);
	rINTMSK |= (BIT_UART); 
	rINTSUBMSK|=(BIT_SUB_RXD0|BIT_SUB_UART0|BIT_SUB_ERR0);

	while( getchar() != '\r');  // just press any key at 32 times..

	printf("\nEnd Tx, transfer data count=%d\n",tx_cnt);

	Uart_Select(0);
    Uart_Port_Return();
}
#endif


#endif
// junon end

//---------------------------------------UART0 test function-------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -