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

📄 uart_bak.c

📁 s3c44box 的远程序~!很有价值的啊!!!包括地层硬件编程!
💻 C
字号:
#include <string.h>
#include <stdlib.h>
#include "..\inc\44b.h"
#include "..\inc\44blib.h"
#include "..\inc\def.h"

char Uart_IntGetKey(void);

void __irq Uart0_RxInt(void);
void __irq Uart0_TxInt(void);
void __irq Uart0_RxFifoInt(void);
void __irq Uart0_TxFifoInt(void);
void __irq Uart1_RxInt(void);
void __irq Uart1_TxInt(void);

#define KEY_BUFLEN 100

static U8 keyBuf[KEY_BUFLEN];
volatile static int keyBufRdPt=0;
volatile static int keyBufWrPt=0;
static char *uart0TxStr;
static char *uart1TxStr;


char Uart_IntGetkey(void)
{
    if(keyBufRdPt==KEY_BUFLEN)
		keyBufRdPt=0;

    while(keyBufWrPt==keyBufRdPt);
    return keyBuf[keyBufRdPt++];
}


void __irq Uart0_RxInt(void)
{
    rI_ISPC=BIT_URXD0;
    //rI_ISPC; //is needed only when cache=on & wrbuf=on & BSFRD=0

    keyBuf[keyBufWrPt++]=RdURXH0();
    if(keyBufWrPt==KEY_BUFLEN)
		keyBufWrPt=0;
}


void __irq Uart0_TxInt(void)
{
    rI_ISPC=BIT_UTXD0;
    //rI_ISPC; //is needed only when cache=on & wrbuf=on & BSFRD=0

    if(*uart0TxStr != '\0')
        WrUTXH0(*uart0TxStr++);
    else
        rINTMSK|=BIT_UTXD0;
}


void __irq Uart0_RxFifoInt(void)
{
    rI_ISPC=BIT_URXD0;        
    //rI_ISPC; //is needed only when cache=on & wrbuf=on & BSFRD=0
        
    //keyBuf[keyBufWrPt++]=RdURXH0();
    //if(keyBufWrPt==KEY_BUFLEN)keyBufWrPt=0;

    while( (rUFSTAT0&0xf) >0 )
    {
        keyBuf[keyBufWrPt++]=rURXH0;
        if(keyBufWrPt==KEY_BUFLEN)
			keyBufWrPt=0;
    }
}


void __irq Uart0_RxFifoErrorInt(void) //receiver time out error
{
//    rI_ISPC=BIT_UERR0;        
    //rI_ISPC; //is needed only when cache=on & wrbuf=on & BSFRD=0
    
    //keyBuf[keyBufWrPt++]=RdURXH0();
    //if(keyBufWrPt==KEY_BUFLEN)keyBufWrPt=0;

//    rUSTAT0;   //to clear status register bits  
               //but time_out error is cleared also when the rx buffer is empty.

    while( (rUFSTAT0&0xf) >0 )
    {
        keyBuf[keyBufWrPt++]=rURXH0;
        if(keyBufWrPt==KEY_BUFLEN)
			keyBufWrPt=0;
    }
}


void __irq Uart0_TxFifoInt(void)
{
    rI_ISPC=BIT_UTXD0;        
    //rI_ISPC; //is needed only when cache=on & wrbuf=on & BSFRD=0

    while( !(rUFSTAT0 & 0x200) && (*uart0TxStr != '\0') )
    //while( ( ((rUFSTAT0>>4)&0xf)<=12 ) && (*uart0TxStr != '\0') )
    {
        rUTXH0=*uart0TxStr++;
    }
    if(*uart0TxStr == '\0')
    rINTMSK|=BIT_UTXD0;
}


void __irq Uart1_RxInt(void)
{
    rI_ISPC=BIT_URXD1;
    //rI_ISPC; //is needed only when cache=on & wrbuf=on & BSFRD=0

    keyBuf[keyBufWrPt++]=RdURXH1();
    if(keyBufWrPt==KEY_BUFLEN)
		keyBufWrPt=0;
}


void __irq Uart1_TxInt(void)
{
    rI_ISPC=BIT_UTXD1;
    //rI_ISPC; //is needed only when cache=on & wrbuf=on & BSFRD=0

    if(*uart1TxStr != '\0')
        WrUTXH1(*uart1TxStr++);
    else
        rINTMSK|=BIT_UTXD1;
}


void Test_Uart0Fifo(void)
{
    int key;

    keyBufRdPt=keyBufWrPt=0;
    pISR_UTXD0=(unsigned)Uart0_TxFifoInt;
    pISR_URXD0=(unsigned)Uart0_RxFifoInt;
//    pISR_UERR0=(unsigned)Uart0_RxFifoErrorInt;
    Uart_Printf("[Uart ch 0 tx FIFO Interrupt Test]\n");
    Uart_TxEmpty(0); //wait until tx shifter is empty.

    uart0TxStr="UART0 Tx FIFO interrupt test is good!!\r\n";
    rUFCON0=(2<<6)|(1<<4)|(6)|1;  
    //FIFO trigger:tx:8byte ,rx:8byte, txrx_fifo reset(will be cleared), FIFO enable.

    rINTMSK=~(BIT_GLOBAL|BIT_UTXD0);
    
    rUCON0=rUCON0&0xf3|0x8; 
    rUCON0=rUCON0&0xf3|0x4; //needed to set the UTXD0 pending bit.              
    

    Delay(3000);

    Uart_Printf("\n[Uart0 FIFO Tx Test by BDMA0]\n");
    uart0TxStr="UART0 Tx Test by BDMA0 is good!!!!\r\n";

    Uart_TxEmpty(0);
    rUCON0=0x49;              //tx:BDMA0 rx:int

    rBDICNT0=0;
    rBDCON0 =0x0;
    rBDISRC0=(unsigned int)uart0TxStr|(0<<30)|(1<<28);  // byte,inc
    rBDIDES0=UTXH0 |(1<<30)|(3<<28);  //L/B endian,M2IO,fix   
    rBDICNT0=strlen(uart0TxStr)|(1<<30)|(1<<26)|(1<<20); //UART0,

    while((rBDCON0&0x30)==0x10);

    Uart_TxEmpty(0);
    rUCON0=0x45;

    Uart_Printf("\n[Uart0 FIFO Tx Test by BDMA1]\n");
    uart0TxStr="UART0 Tx Test by BDMA1 is good!!!!\r\n";

    Uart_TxEmpty(0);
    rUCON0=0x4d;              //tx:BDMA1 rx:int

    rBDICNT1=0;
    rBDCON1 =0x0;
    rBDISRC1=(unsigned int)uart0TxStr|(0<<30)|(1<<28);  // byte,inc
    rBDIDES1=UTXH0 |(1<<30)|(3<<28);  //L/B endian,M2IO,fix   
    rBDICNT1=strlen(uart0TxStr)|(1<<30)|(1<<26)|(1<<20); //UART0,

    while((rBDCON1&0x30)==0x10);

    Uart_TxEmpty(0);
    rUCON0=0x45|0x80;         //tx:int rx:int rcv_time_out enabled

    Uart_Printf("\n[Uart ch 0 FIFO Rx Interrupt Test]:Type any key!!!\n");
    Uart_Printf("You have to see the typed character. To quit, press Enter key.\n");

//    rINTMSK=~(BIT_GLOBAL|BIT_URXD0|BIT_UERR0);
    
    while( (rUFSTAT0&0xf) >0 )
        key=RdURXH0(); //To clear overrun error state if overrun error occurs while interrupt was masked.  
//    rUSTAT0;  //To clear time_out error state if time_out error occurred before while interrupt was masked.
    
    while((key=Uart_IntGetkey())!='\r')
        Uart_SendByte(key);
    
    rUFCON0=(2<<6)|(1<<4)|(6)|0;  
    //FIFO trigger:tx:8byte ,rx:8byte, txrx_fifo reset(will be cleared), FIFO disable.

    rINTMSK=~BIT_GLOBAL;
    rUCON0=0x45;              //rcv_time_out disabled
    Uart_Printf("\n");
}


void Test_Uart0(void)
{
    int key;
         
    keyBufRdPt=keyBufWrPt=0;
    pISR_UTXD0=(unsigned)Uart0_TxInt;
    pISR_URXD0=(unsigned)Uart0_RxInt;

       
    Uart_Printf("[Uart ch 0 tx Interrupt Test]\n");
    Uart_TxEmpty(0); //wait until tx shifter is empty.

    uart0TxStr="UART0 Tx interrupt test is good!!!!\r\n";

    rINTMSK=~(BIT_GLOBAL|BIT_UTXD0);
    rUCON0&=0xf3;   
    rUCON0|=0x4;   //needed to set the UTXD0 pending bit.
    
    Delay(3000);

    Uart_Printf("\n[Uart0 Tx Test by BDMA0]\n");
    uart0TxStr="UART0 Tx Test by BDMA0 is good!!!!\r\n";

    Uart_TxEmpty(0);
    rUCON0=0x49;              //tx:BDMA0 rx:int

    rBDICNT0=0;
    rBDCON0 =0x0;
    rBDISRC0=(unsigned int)uart0TxStr|(0<<30)|(1<<28);  // byte,inc
    rBDIDES0=UTXH0 |(1<<30)|(3<<28);  //L/B endian,M2IO,fix   
    rBDICNT0=strlen(uart0TxStr)|(1<<30)|(1<<26)|(1<<20); //UART0,

    while((rBDCON0&0x30)==0x10);

    Uart_TxEmpty(0);
    rUCON0=0x45;

    Uart_Printf("\n[Uart0 Tx Test by BDMA1]\n");
    uart0TxStr="UART0 Tx Test by BDMA1 is good!!!!\r\n";

    Uart_TxEmpty(0);
    rUCON0=0x4d;              //tx:BDMA1 rx:int

    rBDICNT1=0;
    rBDCON1 =0x0;
    rBDISRC1=(unsigned int)uart0TxStr|(0<<30)|(1<<28);  // byte,inc
    rBDIDES1=UTXH0 |(1<<30)|(3<<28);  //L/B endian,M2IO,fix   
    rBDICNT1=strlen(uart0TxStr)|(1<<30)|(1<<26)|(1<<20); //UART0,

    while((rBDCON1&0x30)==0x10);

    Uart_TxEmpty(0);
    rUCON0=0x45;              //tx:int rx:int


    Uart_Printf("\n[Uart ch 0 Rx Interrupt Test]:Type any key!!!\n");
    Uart_Printf("You have to see the typed character. To quit, press Enter key.\n");

    rINTMSK=~(BIT_GLOBAL|BIT_URXD0);

    RdURXH0(); //To clear overrun error state if overrun error is occurred. 
        
    while((key=Uart_IntGetkey())!='\r')
         Uart_SendByte(key);

    rINTMSK=~BIT_GLOBAL;
    Uart_Printf("\n");
}


void Test_Uart1(void)
{
    int key;
       
    keyBufRdPt=keyBufWrPt=0;
    pISR_UTXD1=(unsigned)Uart1_TxInt;
    pISR_URXD1=(unsigned)Uart1_RxInt;

    Uart_Printf("[ Uart ch 1 Test ]\n");
    Uart_Printf("Plug the serial cable into ch1 connector!!! \n");
    Uart_Printf("Then, press any key through UART ch1.\n");
    Uart_Select(1);
    Uart_Getch();

    uart1TxStr="UART1 Tx interrupt test is good!!!!\r\n";
    Uart_Printf("[Uart ch 1 tx Interrupt Test]\n");
   
    Uart_TxEmpty(1);

    rINTMSK=~(BIT_GLOBAL|BIT_UTXD1);
    
    rUCON1=rUCON1 & 0xf3|0x8; 
    rUCON1=rUCON1 & 0xf3|0x4; //needed to set the UTXD0 pending bit.            

    Delay(3000);

    Uart_Printf("\n[Uart1 Tx Test by BDMA0]\n");

    uart1TxStr="UART1 Tx Test by BDMA0 is good!!!!\r\n";

    Uart_TxEmpty(1);
    rUCON1=0x49;              //tx:BDMA0 rx:int

    rBDICNT0=0;
    rBDCON0 =0x0;
    rBDISRC0=(unsigned int)uart1TxStr|(0<<30)|(1<<28);  // byte,inc
    rBDIDES0=UTXH1 |(1<<30)|(3<<28);  //L/B endian,M2IO,fix   
    rBDICNT0=strlen(uart1TxStr)|(2<<30)|(1<<26)|(1<<20); //UART1,
    while((rBDCON0&0x30)==0x10);

    Uart_TxEmpty(1);//wait until tx shifter is empty.
    rUCON1=0x45;     

    Uart_Printf("\n[Uart1 Tx Test by BDMA1]\n");

    uart1TxStr="UART1 Tx Test by BDMA1 is good!!!!\r\n";

    Uart_TxEmpty(1);
    rUCON1=0x4d;              //tx:BDMA1 rx:int

    rBDICNT1=0;
    rBDCON1 =0x0;
    rBDISRC1=(unsigned int)uart1TxStr|(0<<30)|(1<<28);  // byte,inc
    rBDIDES1=UTXH1 |(1<<30)|(3<<28);  //L/B endian,M2IO,fix   
    rBDICNT1=strlen(uart1TxStr)|(2<<30)|(1<<26)|(1<<20); //UART1,

    while((rBDCON1&0x30)==0x10);


    Uart_TxEmpty(1);
    rUCON1=0x45;              //tx:int rx:int
    
    Uart_Printf("\n[Uart ch 1 Rx Interrupt Test]\n");
    Uart_Printf("Type any key!!! You have to see the typed character.\n"
                "To quit, press Enter key.\n");


    rINTMSK=~(BIT_GLOBAL|BIT_URXD1);

    key=RdURXH1(); //To clear overrun error state if overrun error is occurred. 
        
    while((key=Uart_IntGetkey())!='\r')
    {Uart_SendByte(key);}

    rINTMSK=~BIT_GLOBAL;
    Uart_Printf("\n");

    Uart_Printf("Plug the serial cable into ch0 as before this test!!!\n");
    Uart_Printf("Then, press any key through UART ch 0.\n");
    Uart_Select(0);
    Uart_Getch();
}


void Test_Uart0Range(void)
{
    S32 count=0;
    char str[10];
    U32 saveUBRDIV0=0,newUBRDIV0;
    char key;


    Uart_Printf("Uart ch0 baud-rate range test.\n");

    while(1)
    {
        Uart_Printf("Input the rUBRDIV0 register value:");

        Uart_GetString(str);
        newUBRDIV0=atoi("22");

        count=0;
        while(1)
        {
            saveUBRDIV0=rUBRDIV0;
            rUBRDIV0=newUBRDIV0;
            Uart_Printf("If you see this text, Press [p] to exit.\n");

            Delay(100); //wait until UART operation ends.

            if(count++==100)
            {
                rUBRDIV0=saveUBRDIV0;
                Uart_Printf("\nError!!! Return to UBRDIV0=%d\n",saveUBRDIV0);
                break;
            }
            key=Uart_GetKey();

            if(key=='p')
            {
                Uart_Printf("\nOK!!! Return to UBRDIV0=%d\n",saveUBRDIV0);
                break;
            }
        }
    }
}


#define U1M_DATA_LENGTH (0x10000)

void Test_Uart1Max(void)
{
    int key;
    int i;
    U32 checkSum=0;
    char string[20];
    char *rxDataPt,*txDataPt;
    int ubrdiv,saveUbrdiv;
    int error=0;
       
    saveUbrdiv=rUBRDIV1;
    Uart_Printf("[ Uart ch 1 Max baudrate Test by BDMA0]\n");
    Uart_Printf("Selcet receive or transmit[r]/[t]:");
    key=Uart_Getch();Uart_Printf("\n");
    Uart_Printf("Select UBRDIV[21](115.2Kbps):");
    Uart_GetString(string);       
    ubrdiv=atoi(string);
    Uart_Printf("Test baudrate=%d(UBRDIV=%d)\n",MCLK/16/(ubrdiv+1),ubrdiv);
    
    Uart_TxEmpty(1);

    if(key=='t')
    {
        Uart_Printf("Transmit mode.\n");
        txDataPt=malloc(U1M_DATA_LENGTH);
        Uart_Printf("txDataPt=%x",txDataPt);
        for(i=0;i<U1M_DATA_LENGTH;i++)*(txDataPt+i)=i%256;
        rUCON1=0x49;              //tx:BDMA0  rx:int
        rUBRDIV1=ubrdiv;
        rBDICNT0=0;
        rBDCON0 =0x0;
        rBDISRC0=(unsigned int)txDataPt|(0<<30)|(1<<28);  // byte,inc
        rBDIDES0=UTXH1 |(1<<30)|(3<<28);  //L/B endian,M2IO,fix   
        rBDICNT0=U1M_DATA_LENGTH|(2<<30)|(1<<26)|(1<<20); //UART1, with null character
        while((rBDCON0&0x30)!=0x20);
        for(i=0;i<U1M_DATA_LENGTH;i++)checkSum+=*(txDataPt+i);
        Uart_Printf("Transmit data checksum:%x\n",checkSum);
        free(txDataPt);
    }
    else if(key=='r')   
    {
    /*  Uart_Printf("Receive mode.\n");
                rxDataPt=malloc(U1M_DATA_LENGTH);
        Uart_Printf("rxDataPt=%x\n",rxDataPt);
            rUCON1=0x46;              //rx:BDMA0  tx:int
        rUBRDIV1=ubrdiv;
            rBDICNT0=0;
        rBDCON0 =0x0;
            rBDISRC0=URXH1 |(0<<30)|(3<<28);  // byte,inc
        rBDIDES0=(unsigned int)rxDataPt |(2<<30)|(1<<28);  //L/B endian,IO2M,fix   
            rBDICNT0=U1M_DATA_LENGTH|(2<<30)|(1<<26)|(1<<20); //UART1,
        while((rBDCON0&0x30)!=0x20)Uart_Printf("rBDICNT0=%x             \r",rBDCCNT0);
                Uart_Printf("rBDICNT0=%x\n",rBDCCNT0);
        for(i=0;i<U1M_DATA_LENGTH;i++)
            if(*(rxDataPt+i)!=(i%256)){Uart_Printf("%d:%x->%x\n",i,i%256,*(rxDataPt+i));error++;}
        free(rxDataPt);
        Uart_Printf("Total Error=%d\n",error);
        */

        Uart_Printf("Receive mode.\n");
        rxDataPt=malloc(U1M_DATA_LENGTH);
        Uart_Printf("rxDataPt=%x\n",rxDataPt);
        rUCON1=0x45;              //rx:int  tx:int
        rUBRDIV1=ubrdiv;
    
        rINTMSK=~BIT_GLOBAL;
        key=rURXH1; //To remove overrun error state.

        i=0;    
        while(i<U1M_DATA_LENGTH)
        {
           // while(!(rUSTAT1&0x20));
            *(rxDataPt+i)=rURXH1;
            i++;
        }
        rINTMSK=~BIT_GLOBAL;
        Uart_TxEmpty(1);//wait until tx shifter is empty.
    
        for(i=0;i<U1M_DATA_LENGTH;i++)
            if(*(rxDataPt+i)!=(i%256)){Uart_Printf("%d:%x->%x\n",i,i%256,*(rxDataPt+i));error++;}
        free(rxDataPt);
        Uart_Printf("Total Error=%d\n",error);
    }

    rUCON1=0x45;     
    rUBRDIV1=saveUbrdiv;
}


void __irq Uart1_ErrInt2(void)
{
    //rI_ISPC=BIT_UERR1;
    //rI_ISPC; //is needed only when cache=on & wrbuf=on & BSFRD=0
    //Uart_Printf("S:USTAT=%x ",rUSTAT1);
}


void __irq Uart1_RxInt2(void)
{
    rI_ISPC=BIT_URXD1;
    //rI_ISPC; //is needed only when cache=on & wrbuf=on & BSFRD=0
    Uart_Printf("%c",RdURXH1());
}


void __irq Uart1_TxInt2(void)
{
    rI_ISPC=BIT_UTXD1;
    //rI_ISPC; //is needed only when cache=on & wrbuf=on & BSFRD=0
    Uart_Printf("T:?\n");
}


void Test_Uart1Status(void)
{
    pISR_UTXD1=(unsigned)Uart1_TxInt2;
    pISR_URXD1=(unsigned)Uart1_RxInt2;
//    pISR_UERR1=(unsigned)Uart1_ErrInt2;

//    rINTMSK=~(BIT_GLOBAL|BIT_UTXD1|BIT_URXD1|BIT_UERR1);
    
    Uart_Printf("[ Uart ch 1 status interrupt test\n");
    Uart_Printf("Press any key through UART0 to exit!!!\n");

    //place your teset code here!!!
    //......

    //Uart_Printf("UART1 19200 bps receive test\n");
    //rUBRDIV1=( (int)(MCLK/16./19200 + 0.5)-1 );

    while(Uart_GetKey()==0);
}

⌨️ 快捷键说明

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