📄 uart1.c
字号:
#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;
//---------------------------------------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_TxFifo(void)
{
rINTSUBMSK|=(BIT_SUB_RXD1|BIT_SUB_TXD1|BIT_SUB_ERR1); // Just for the safety
while (!(rUFSTAT1 & 0x200) && (*uart1TxStr != '\0')) //until tx fifo full or end of string
WrUTXH1(*uart1TxStr++);
if(*uart1TxStr == '\0')
{
rINTMSK|=BIT_UART1;
rSUBSRCPND=BIT_SUB_TXD1; // Clear Sub int pending
ClearPending(BIT_UART1); // Clear master pending
}
else
{
ClearPending(BIT_UART1); // Clear master pending
rSUBSRCPND=BIT_SUB_TXD1; // Clear Sub int pending
rINTSUBMSK&=~(BIT_SUB_TXD1); // Unmask sub int
}
}
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&0x100)||(rUFSTAT1&0xf)) //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;
}
}
void Test_Uart1_Int(void)
{
Uart_Port_Set();
Uart_Select(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");
//-------------------------------------------------------------------------------------------->
Uart_Printf("\nConnect PC[COM1 or COM2] and UART1 of SMDK241S0 with a serial cable!!! \n");
Uart_Printf("Then, press any key........\n");
Uart_Select(1); // Change the uart port
Uart_Getch();
//----------------------------------------->
pISR_UART1=(unsigned)Uart1_TxInt;
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)|(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(0); //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 &= 0x400; // For the PCLK <-> UCLK fuction
rUCON1 |= (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_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("\nConnect PC[COM1 or COM2] and UART0 of SMDK241S0 with a serial cable!!! \n");
Uart_Printf("Then, press any key........\n");
Uart_Select(0);
Uart_Getch();
//--------------------------->
Uart_Port_Return();
}
void Test_Uart1_Dma(void)
{
Uart_Port_Set();
Uart_Select(0);
/*********** 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(0);
//-------------------------------------------------------------------------------------------->
Uart_Printf("\nConnect PC[COM1 or COM2] and UART1 of SMDK241S0 with a serial cable!!! \n");
Uart_Printf("Then, press any key........\n");
Uart_Select(1); // Change the uart port
Uart_Getch();
//----------------------------------------->
pISR_DMA1 =(unsigned)Uart1_TxDmaDone;
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)|(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=((unsigned)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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -