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

📄 uart_isr_cts_rts.c

📁 altera串口源代码程序
💻 C
字号:

// Includes ISR for UART with handshaking support using CTS/RTS

#include "excalibur.h"
#include "buffer.h"  	// defines RXBUFSIZE and TXBUFSIZE

// global definitions.
unsigned char  RxBuf[RXBUFSIZE];	// receive buffer
int RxHead =0;				// circular buffer index
int RxTail =0;				// circular buffer index
unsigned char  TxBuf[TXBUFSIZE];	// transmit buffer
int TxHead =0;				// circular buffer index
int TxTail =0;				// circular buffer index


unsigned char _getchar(); 
void uart_isr(int context);
int _putchar(int in_char);

int main(void)
{
	int context = 0;
	
	nr_installuserisr(na_uart1_irq, uart_isr, context);  // install UART ISR
	na_uart1->np_uartcontrol = np_uartcontrol_irrdy_mask;  // enable rx interrupts
	
	// Continually wait for chars and transmit them as they come in.
	while(1)
	{
		_putchar( (int)(_getchar()) );
	}

	return 0;
}



// UART ISR with handshaking support using CTS/RTS
void uart_isr(int context)
{
	int size;
	// retrieve the status register
	int sr = na_uart1->np_uartstatus;

	// start of receiver
	if(na_uart1->np_uartstatus & np_uartstatus_rrdy_mask)
	{	// something ready for the receiver.
 		// put new char into buffer from UART
		RxBuf[RxHead] = na_uart1->np_uartrxdata; 
		// clear the errors and interrupts
		na_uart1->np_uartstatus=0; 

		if ((++RxHead) > (RXBUFSIZE - 1))		
		{
			RxHead = 0;
		}
		
		// check to see if need to turn RTS off
		if (RxHead >= RxTail)
         		size = RxHead - RxTail;
       		else
         		size = ((RXBUFSIZE) - RxTail) + RxHead;

	     	if (size > RXBUFSIZE - 5)       /* don't overflow RxBuf[] */
			na_uart1->np_uartcontrol = na_uart1->np_uartcontrol & ~np_uartcontrol_rts_mask;  // turn off the RTS bit

	}// end of receiver


	// start of transmitter
	if(sr & np_uartstatus_trdy_mask)
	{	//  transmitter ready
		if(na_uart1->np_uartcontrol & np_uartcontrol_itrdy_mask)  
		{	// set to handle transmitter interrupts
			if ((TxTail != TxHead) && (na_uart1->np_uartstatus & np_uartstatus_cts_mask))   // check CTS too  
			{      		
				na_uart1->np_uarttxdata = TxBuf[TxTail];	// send a byte of data
				if (++TxTail > (TXBUFSIZE - 1))	// check for wrap around.
		               		TxTail = 0;
			}
			else 
			{   // buffer empty, disable TRDY interrupts.
				// Remember, this ISR handles only RRDY and TRDY (no error handling).
				na_uart1->np_uartcontrol = np_uartcontrol_irrdy_mask ;	
			}
		}
	}// end of transmitter


}// end of ISR

⌨️ 快捷键说明

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