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

📄 uart0.c

📁 YC2440_ADS1.2示例代码.rar
💻 C
📖 第 1 页 / 共 3 页
字号:

    pISR_UART0=(unsigned)Uart0_TxInt;

    rULCON0=(0<<6)|(0<<3)|(0<<2)|(3);	// Normal,No parity,One stop bit, 8bit
    rUCON0 |= (TX_INTTYPE<<9)|(RX_INTTYPE<<8)|(0<<7)|(0<<6)|(0<<5)|(0<<4)|(1<<2)|(1);
    //Clock,Tx:Def,Rx:Def,Rx timeout:x,Rx error int:x,Loop-back:x,Send break:x,Tx:int,Rx:int
    Uart_TxEmpty(1); //wait until tx buffer is empty.
    rINTMSK=~(BIT_UART0);
    rINTSUBMSK=~(BIT_SUB_TXD0);

    while(isTxInt);
    
    /*********** UART0 Rx test with interrupt ***********/
    isRxInt=1;
    uart0RxStr=(char *)UARTBUFFER;
    Uart_Printf("\n[Uart channel 0 Rx Interrupt Test]:\n");
    Uart_Printf("After typing characters and ENTER key  you will see the characters which was typed by you.");
    Uart_Printf("\nTo quit, press ENTER key.!!!\n");
	
    Uart_TxEmpty(1); //wait until tx buffer is empty.
    pISR_UART0 =(unsigned)Uart0_RxIntOrErr;
    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

    // Clear Int Pending and Unmask    
    ClearPending(BIT_UART0);
    rINTMSK=~(BIT_UART0);
    rSUBSRCPND=(BIT_SUB_TXD0|BIT_SUB_RXD0|BIT_SUB_ERR0);    
    rINTSUBMSK=~(BIT_SUB_RXD0|BIT_SUB_ERR0);
    
    while(isRxInt);
    
    rINTSUBMSK|=(BIT_SUB_RXD0|BIT_SUB_ERR0);
    rINTMSK|=(BIT_UART0);
 
    Uart_Printf("%s\n",(char *)UARTBUFFER);

     Uart_Printf("\nConnect PC[COM1 or COM2] and UART1 of SMDK2440 with a serial cable!!! \n");
    Uart_Printf("Then, press any key........\n");
   	
    Uart_Select(1);
    Uart_Getch();
#endif	
    Uart_Port_Return();
}


void __irq Uart0_TxDmaDone(void)
{
    rDMASKTRIG0=0x0;	// Stop Dma0
    isDone=0;
    rINTMSK |= BIT_DMA0;
    ClearPending(BIT_DMA0);
}

void __irq Uart0_RxDmaOrErr(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_ERR0);	// Clear Sub int pending    
    rINTSUBMSK&=~(BIT_SUB_ERR0);    
}

void __irq Uart0_RxDmaDone(void)
{
    rDMASKTRIG0=0x0;	//DMA0 Channel Off
    isDone=0;
    *(uart0RxStr+5)='\0';
    rINTMSK|=(BIT_DMA0);
    ClearPending(BIT_DMA0);
}

void Test_Uart0_Dma(void)
{
    Uart_Port_Set(); 
    Uart_Select(1);    
    /*********** UART0 Tx test with DMA0 ***********/ 
    isDone=1;
    uart0TxStr="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890->UART0 Tx Test by DMA0 is good!!!!\r\n";
    Uart_Printf("\n[Uart channel 0 DMA0 Tx Test]\n");
    Uart_TxEmpty(1);

    Uart_Printf("\nConnect PC[COM1 or COM2] and UART0 of SMDK2440 with a serial cable!!! \n");
    Uart_Printf("Then, press any key........\n");
   	
    Uart_Select(0);
    Uart_Getch();

   
    pISR_DMA0  =(unsigned)Uart0_TxDmaDone;

    rULCON0=(0<<6)|(0<<3)|(0<<2)|(3);	// Normal,No parity,One stop bit, 8bit
    rUCON0 = (TX_INTTYPE<<9)|(RX_INTTYPE<<8)|(0<<7)|(0<<6)|(0<<5)|(0<<4)|(2<<2)|(0);
    //Clock,Tx:Def,Rx:Def,Rx timeout:x,Rx error int:x,Loop-back:x,Send break:x,Tx:dma0,Rx:x

    /***DMA0 init***/
    rDISRC0=(U32)uart0TxStr;	// Start address
    rDISRCC0=(0<<1)|(0);		// AHB,Increment
    rDIDST0=(U32)UTXH0;			// Memory buffer Address
    rDIDSTC0=(1<<1)|(1);		// APB,Fixed
    rDCON0=(1<<31)|(0<<30)|(1<<29)|(0<<28)|(0<<27)|(1<<24)|(1<<23)|(1<<22)|(0<<20)|strlen((char*)uart0TxStr);
    //handshake, sync PCLK, TC int, single tx, single service, Uart0, H/W request,auto-reload off, Byte size Tx, Tx count value
    rINTMSK=~(BIT_DMA0);
    rDMASKTRIG0=(0<<2)|(1<<1)|(0);    //no-stop, DMA0 channel on, no-SW trigger 

    while(isDone);

    /*********** UART0 Rx test with DMA0 ***********/ 
    isDone=1;
    uart0RxStr=(char *)UARTBUFFER;
   
    Uart_Printf("\n[Uart channel 0 DMA0 Rx Test]\n");
    Uart_Printf("Type any five keys!!!\n");    
    Uart_Printf("Then you will see what you typed.\n");

    pISR_DMA0=(unsigned)Uart0_RxDmaDone;
    pISR_UART0=(unsigned)Uart0_RxDmaOrErr;

    
    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)|(1<<6)|(0<<5)|(0<<4)|(1<<2)|(2);
    //Clock,Tx:Def,Rx:Def,Rx timeout:x,Rx error int:o,Loop-back:x,Send break:x,Tx:int,Rx:dma0

    
    /***DMA0 init***/
    rDISRC0=(U32)URXH0;			// Start address
    rDISRCC0=(1<<1)|(1);		// APB,Fixed
    rDIDST0=(U32)uart0RxStr;	        // Memory buffer Address
    rDIDSTC0= (0<<1)|(0);		// AHB,Increment
    rDCON0=(1<<31)|(0<<30)|(1<<29)|(0<<28)|(0<<27)|(1<<24)|(1<<23)|(1<<22)|(0<<20)|(5);
    //handshake, sync PCLK, TC int, single tx, single service, Uart0, H/W request,auto-reload off, Byte size Tx, Tx count value

    // Clear Int Pending and Unmask    
    ClearPending(BIT_UART0);
    rINTMSK=~(BIT_DMA0|BIT_UART0);
    rSUBSRCPND=(BIT_SUB_TXD0|BIT_SUB_RXD0|BIT_SUB_ERR0);        
    rINTSUBMSK=~(BIT_SUB_ERR0);
    rDMASKTRIG0=(0<<2)|(1<<1)|(0);    //no-stop, DMA0 channel on, no-SW trigger 

    while(isDone);

    Uart_Printf("%s\n",uart0RxStr);

     Uart_Printf("\nConnect PC[COM1 or COM2] and UART1 of SMDK2440 with a serial cable!!! \n");
    Uart_Printf("Then, press any key........\n");
   	
    Uart_Select(1);
    Uart_Getch();
	
    Uart_Port_Return();
}

volatile U32 *fifo_cnt; //temporary for fifo count test
volatile U32 fcnt = 0;

void __irq Uart0_TxFifo(void)
{
    rINTSUBMSK|=(BIT_SUB_RXD0|BIT_SUB_TXD0|BIT_SUB_ERR0);	// Just for the safety
	rINTMSK|=BIT_UART0;
	rSUBSRCPND=BIT_SUB_TXD0;	// Clear Sub int pending
	ClearPending(BIT_UART0);	// Clear master pending

	*fifo_cnt++ = ++fcnt;
	*fifo_cnt++ = rUFCON0;
	*fifo_cnt++ = (rUFSTAT0>>8)&0x3f;
	*fifo_cnt = 0;

    while (!(rUFSTAT0 & 0x4000) && (*uart0TxStr != '\0')) 	//until tx fifo full or end of string
    	WrUTXH0(*uart0TxStr++);	

    if(*uart0TxStr != '\0') 
    {
        rINTSUBMSK&=~(BIT_SUB_TXD0);	// Unmask sub int
        rINTMSK&=~(BIT_UART0);
    }
}

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&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 Test_Uart0_Fifo(void)
{
	int i;
	
    Uart_Port_Set(); 
    Uart_Select(1);
    /******* UART0 Tx FIFO test with interrupt ********/     
    Uart_Printf("[Uart channel 0 Tx FIFO Interrupt Test]\n");
    Uart_TxEmpty(1);	//wait until tx buffer is empty.

    Uart_Printf("\nConnect PC[COM1 or COM2] and UART0 of SMDK2440 with a serial cable!!! \n");
    Uart_Printf("Then, press any key........\n");
   	
    Uart_Select(0);
    Uart_Getch();

	fifo_cnt = (U32 *)_NONCACHE_STARTADDRESS; // temporary buffer
		
    /* <Tx Trigger Level:empty> */    
    uart0TxStr="ABCDEFGHIJKLMNOPQRSTUVWXYZ->UART0 Tx FIFO interrupt(TL 48byte) 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=(3<<6)|(2<<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);
	rUFCON0=(3<<6)|(2<<4)|(1<<2)|(1<<1)|(0);

// edited by junon
	/* <Tx Trigger Level:16Byte> */ 	 
	rUFCON0=(2<<6)|(2<<4)|(1<<2)|(1<<1)|(1);
	uart0TxStr="ABCDEFGHIJKLMNOPQRSTUVWXYZ->UART0 Tx FIFO interrupt(TL 32byte) test is good!!!!\r\n";
	rINTMSK=~(BIT_UART0);
	rINTSUBMSK=~(BIT_SUB_RXD0|BIT_SUB_TXD0|BIT_SUB_ERR0);
	Delay(500);

	/* <Tx Trigger Level:32Byte> */ 	 
	rUFCON0=(1<<6)|(2<<4)|(1<<2)|(1<<1)|(1);
	uart0TxStr="ABCDEFGHIJKLMNOPQRSTUVWXYZ->UART0 Tx FIFO interrupt(TL 16byte) test is good!!!!\r\n";
	rINTMSK=~(BIT_UART0);
	rINTSUBMSK=~(BIT_SUB_RXD0|BIT_SUB_TXD0|BIT_SUB_ERR0);
	Delay(500);

	/* <Tx Trigger Level:48Byte> */ 	 
	rUFCON0=(0<<6)|(2<<4)|(1<<2)|(1<<1)|(1);
	uart0TxStr="ABCDEFGHIJKLMNOPQRSTUVWXYZ->UART0 Tx FIFO interrupt(TL 0byte) test is good!!!!\r\n";
	rINTMSK=~(BIT_UART0);
	rINTSUBMSK=~(BIT_SUB_RXD0|BIT_SUB_TXD0|BIT_SUB_ERR0);
	Delay(500);
	rUFCON0=(0<<6)|(2<<4)|(1<<2)|(1<<1)|(0);
	//Tx and Rx FIFO Trigger Level:48byte,Tx and Rx FIFO Reset,FIFO off

	Uart_Printf("Saved FIFO current count in ISR!! Interrupt count : %d\n",fcnt);
	fifo_cnt = (U32 *)_NONCACHE_STARTADDRESS;
	while(*fifo_cnt)
		Uart_Printf("[0x%x,%d,0x%x,%d] ", fifo_cnt, *fifo_cnt++, *fifo_cnt++,*fifo_cnt++);
	fcnt = 0;
	
    /******* 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)|(2<<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:12byte,Tx and Rx FIFO Reset,FIFO off
			
    if(rx_dncs==(rx_checksum&0xffff)) 
	Uart_Printf("\nDownload test OK!!!\n");
    else 
	Uart_Printf("\nError!!!\n");

    Uart_Printf("\nConnect PC[COM1 or COM2] and UART1 of SMDK2440 with a serial cable!!! \n");
    Uart_Printf("Then, press any key........\n");
   	
    Uart_Select(1);
    Uart_Getch();
	
    Uart_Port_Return();	
}



void __irq Uart0_AfcTx(void)
{
    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;
    }
}

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)
{

⌨️ 快捷键说明

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