?? uart1.c
字號:
/*****************************************
NAME: uart1.c
DESC: UART1 test
WWW.YCTEK.COM
*****************************************/
#include <string.h>
#include <stdlib.h>
#include "2410addr.h"
#include "2410lib.h"
#include "def.h"
#include "uart0.h"
#include "uart1.h"
void __irq Uart1_TxInt(void);
void __irq Uart1_RxIntOrErr(void);
void __irq Uart1_TxDmaDone(void);
void __irq Uart1_RxDmaDone(void);
void __irq Uart1_RxDmaOrErr(void);
void __irq Uart1_TxFifo(void);
void __irq Uart1_RxFifoOrErr(void);
void __irq Uart1_AfcTx(void);
void __irq Uart1_AfcRxOrErr(void);
void __sub_Uart1_RxInt(void);
void __sub_Uart1_RxFifo(void);
void __sub_Uart1_RxAfc(void);
void __sub_Uart1_RxErrInt(void);
volatile static char *uart1TxStr;
volatile static char *uart1RxStr;
extern U32 Pclk;
//---------------------------------------UART1 test function-------------------------------------
void __irq Uart1_TxInt(void)
{
rINTSUBMSK|=(BIT_SUB_RXD1|BIT_SUB_TXD1|BIT_SUB_ERR1); // Just for the safety
if(*uart1TxStr != '\0')
{
WrUTXH1(*uart1TxStr++);
ClearPending(BIT_UART1); // Clear master pending
rSUBSRCPND=(BIT_SUB_TXD1);
rINTSUBMSK&=~(BIT_SUB_TXD1); // Unmask sub int
}
else
{
isTxInt=0;
ClearPending(BIT_UART1); // Clear master pending
rSUBSRCPND=(BIT_SUB_TXD1);
rINTMSK|=(BIT_UART1); // Unmask sub int
}
}
void __irq Uart1_RxIntOrErr(void)
{
rINTSUBMSK|=(BIT_SUB_RXD1|BIT_SUB_TXD1|BIT_SUB_ERR1);
if(rSUBSRCPND&BIT_SUB_RXD1) __sub_Uart1_RxInt();
else __sub_Uart1_RxErrInt();
ClearPending(BIT_UART1);
rSUBSRCPND=(BIT_SUB_RXD1|BIT_SUB_ERR1); // Clear Sub int pending
rINTSUBMSK&=~(BIT_SUB_RXD1|BIT_SUB_ERR1);
}
void __irq Uart1_TxDmaDone(void)
{
rDMASKTRIG1=0x0; // Stop Dma1
isDone=0;
rINTMSK |= BIT_DMA1;
ClearPending(BIT_DMA1);
}
void __irq Uart1_RxDmaDone(void)
{
rDMASKTRIG1=0x0; //DMA1 Channel Off
isDone=0;
*(uart1RxStr+5)='\0';
rINTMSK|=(BIT_DMA1);
ClearPending(BIT_DMA1);
}
void __irq Uart1_RxDmaOrErr(void)
{
rINTSUBMSK|=(BIT_SUB_RXD1|BIT_SUB_TXD1|BIT_SUB_ERR1);
if(rSUBSRCPND&BIT_SUB_RXD1) Uart_Printf("Error : UART1 Rx Interrupt is occured!!!\n");
else __sub_Uart1_RxErrInt();
ClearPending(BIT_UART1);
rSUBSRCPND=(BIT_SUB_ERR1); // Clear Sub int pending
rINTSUBMSK&=~(BIT_SUB_ERR1);
}
void __irq Uart1_RxFifoOrErr(void)
{
rINTSUBMSK|=(BIT_SUB_RXD1|BIT_SUB_TXD1|BIT_SUB_ERR1);
if(rSUBSRCPND&BIT_SUB_RXD1) __sub_Uart1_RxFifo();
else __sub_Uart1_RxErrInt();
ClearPending(BIT_UART1);
rSUBSRCPND=(BIT_SUB_RXD1|BIT_SUB_ERR1);
rINTSUBMSK&=~(BIT_SUB_RXD1|BIT_SUB_ERR1);
}
void __sub_Uart1_RxInt(void)
{
if(RdURXH1()!='\r')
{
Uart_Printf("%c",RdURXH1());
*uart1RxStr++ =(char) RdURXH1();
}
else
{
isRxInt=0;
*uart1RxStr='\0';
Uart_Printf("\n");
}
}
void __sub_Uart1_RxFifo(void)
{
while(rUFSTAT1&0x7f) //During the Rx FIFO is not empty
{
rx_point++;
if(rx_point<5)
rx_filesize |= (RdURXH1()<<(8*(rx_point-1))); // First 4-bytes mean file size
else if(rx_point>(rx_filesize-2))
{
rx_dncs |= (RdURXH1()<<(8*(1-(rx_filesize-rx_point)))); //Last 2-bytes mean checksum.
if(rx_point==rx_filesize) rx_isdone=0;
}
else
rx_checksum+=RdURXH1();
}
}
void __sub_Uart1_RxErrInt(void)
{
switch(rUERSTAT1)//to clear and check the status of register bits
{
case 1:
Uart_Printf("Overrun error\n");
break;
case 2:
Uart_Printf("Parity error\n");
break;
case 4:
Uart_Printf("Frame error\n");
break;
case 8:
Uart_Printf("Breake detect\n");
break;
default :
break;
}
isRxInt=0;
}
void Test_Uart1_Int(void)
{
U8 ch;
int iBaud;
Uart_Printf("Change serial cable to UART1,then press any key to continue......\n");
Uart_Port_Set();
Uart_Select(1);
#if 0
/******** For operating clock test *********/
Uart_Printf("[Uart channel 1 Interrupt test]\n");
Uart_Printf("Select operating clock 1. PCLK(D) 2. UCLK 3. FCLK/n \n");
ch = Uart_Getch();
switch (ch)
{
case '2' :
rMISCCR = rMISCCR & ~(7<<8) | (1<<10); // CLKOUT1 = PCLK
Uart_Uextclk_En(1, 115200, Pclk);
break;
case '3' :
Uart_Printf("Type the baudrate and then change the same baudrate of host, too.\n");
Uart_Printf("Baudrate (ex 9600, 115200[D], 921600) : ");
iBaud = Uart_GetIntNum();
if (iBaud == -1) iBaud = 115200;
Uart_Fclkn_En(1, iBaud);
Uart_Getch();
break;
default :
Uart_Pclk_En(1, 115200);
}
#endif
Uart_Pclk_En(1, 115200);
#if 0
/******** Select UART or IrDA *********/
Uart_Printf("Select 1. UART(D) or 2. IrDA mode\nChoose : ");
if (Uart_Getch() == '2')
rULCON1 |= (1<<6); // IrDA mode
else
rULCON1 &= ~(1<<6); // UART mode
#endif
#if 0
/*********** UART1 Tx test with interrupt ***********/
isTxInt=1;
uart1TxStr="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890->UART1 Tx interrupt test is good!!!!\r\n";
Uart_Printf("[Uart channel 1 Tx Interrupt Test]\n");
pISR_UART1=(unsigned)Uart1_TxInt;
rULCON1=(0<<6)|(0<<3)|(0<<2)|(3); // Normal,No parity,One stop bit, 8bit
rUCON1 |= (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_UART1);
rINTSUBMSK=~(BIT_SUB_TXD1);
while(isTxInt);
/*********** UART1 Rx test with interrupt ***********/
isRxInt=1;
uart1RxStr=(volatile char *)UARTBUFFER;
Uart_Printf("\n[Uart channel 1 Rx Interrupt Test]:\n");
Uart_Printf("After typing ENTER key, you will see the characters which was typed by you.");
Uart_Printf("\nTo quit, press ENTER key.!!!\n");
pISR_UART1 =(unsigned)Uart1_RxIntOrErr;
rULCON1=(0<<6)|(0<<3)|(0<<2)|(3); // Normal,No parity,One stop bit, 8bit
rUCON1 |= (1<<6);
//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_UART1);
rINTMSK=~(BIT_UART1);
rSUBSRCPND=(BIT_SUB_RXD1|BIT_SUB_ERR1);
rINTSUBMSK=~(BIT_SUB_RXD1|BIT_SUB_ERR1);
while(isRxInt);
rINTSUBMSK|=(BIT_SUB_RXD1|BIT_SUB_TXD1|BIT_SUB_ERR1);
rINTMSK|=(BIT_UART1);
Uart_Printf("%s\n",(char *)UARTBUFFER);
Uart_Printf("Change the baudrate of terminal to 115200 bps and Press Enter.\n");
#endif
Uart_Port_Return();
}
void Test_Uart1_Dma(void)
{
Uart_Port_Set();
Uart_Select(1);
/*********** UART1 Tx test with DMA1 ***********/
isDone=1;
uart1TxStr="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890->UART1 Tx Test by DMA1 is good!!!!\r\n";
Uart_Printf("\n[Uart channel 1 DMA1 Tx Test]\n");
Uart_TxEmpty(1);
pISR_DMA1 =(unsigned)Uart1_TxDmaDone;
rULCON1=(0<<6)|(0<<3)|(0<<2)|(3); // Normal,No parity,One stop bit, 8bit
rUCON1 |= (TX_INTTYPE<<9)|(RX_INTTYPE<<8)|(0<<7)|(0<<6)|(0<<5)|(0<<4)|(3<<2)|(1);
//Clock,Tx:Def,Rx:Def,Rx timeout:x,Rx error int:x,Loop-back:x,Send break:x,Tx:dma1,Rx:int
/***DMA1 init***/
rDISRC1=(U32)uart1TxStr; // Start address
rDISRCC1=(0<<1)|(0); // AHB,Increment
rDIDST1=(U32)UTXH1; // Memory buffer Address
rDIDSTC1=(1<<1)|(1); // APB,Fixed
rDCON1=(1<<31)|(0<<30)|(1<<29)|(0<<28)|(0<<27)|(1<<24)|(1<<23)|(1<<22)|(0<<20)|strlen((char*)uart1TxStr);
//handshake, sync PCLK, TC int, single tx, single service, Uart1, H/W request,auto-reload off, Byte size Tx, Tx count value
rINTMSK=~(BIT_DMA1);
rDMASKTRIG1=(0<<2)|(1<<1)|(0); //no-stop, DMA1 channel on, no-SW trigger
while(isDone);
/*********** UART1 Rx test with DMA1 ***********/
isDone=1;
uart1RxStr=(char *)UARTBUFFER;
Uart_Printf("\n[Uart channel 1 DMA1 Rx Test]\n");
Uart_Printf("Type any five keys!!!\n");
Uart_Printf("Then you will see what you typed.\n");
pISR_DMA1=(unsigned)Uart1_RxDmaDone;
pISR_UART1=(unsigned)Uart1_RxDmaOrErr;
rULCON1=(0<<6)|(0<<3)|(0<<2)|(3); // Normal,No parity,One stop bit, 8bit
rUCON1 &= 0x400; // For the PCLK <-> UCLK fuction
rUCON1 |= (TX_INTTYPE<<9)|(RX_INTTYPE<<8)|(0<<7)|(1<<6)|(0<<5)|(0<<4)|(1<<2)|(3);
//Clock,Tx:Def,Rx:Def,Rx timeout:x,Rx error int:o,Loop-back:x,Send break:x,Tx:int,Rx:dma1
/***DMA1 init***/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -