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

📄 peripheral_test_uart.c

📁 基于fpga和sopc的用VHDL语言编写的EDA的32位Nios CPU嵌入式系统及其DMA设计俄罗斯方块游戏机
💻 C
字号:


#include "peripheral_test.h"
#include "excalibur.h"


void DoUARTMenu(void)
	{
	char c;
menu:
	MenuBegin("UART Menu");
	MenuEntry('a',"Send Lots");
	MenuEntry('b',"Receive Chars");
	MenuEntry('c',"Enable Interrupt Handler");
	MenuEntry('d',"Disable Interrupt Handler");

	c = MenuEnd('a','d');

	switch(c)
		{
		MenuCase('a',DoUARTSendLots);
		MenuCase('b',DoUARTReceiveChars);
		MenuCase('c',DoUARTEnableInterruptHandler);
		MenuCase('d',DoUARTDisableInterruptHandler);
		}
	
	if(c != 'q')
		goto menu;
	}

#define kLineWidth 77
#define kLineCount 100

void DoUARTSendLots(void)
	{
	char c;
	int i,j;
	int mix;

	printf("\n\nPress character, or <space> for mix: ");
	while((c = rGetChar()) < 0)
		;
	
	printf("%c\n\n",c);

	// Don't spew unprintables!
	if(c < 32)
		c = '.';

	mix = c==' ';

	for(i = 0; i < kLineCount; i++)
		{
		for(j = 0; j < kLineWidth; j++)
			{
			if(mix)
				{
				c++;
				if(c >= 127)
					c = 33;
				}
			nr_uart_txchar(c,0);
			}
		nr_uart_txcr();
		}
	printf("\n\n");
	}


void DoUARTReceiveChars(void)
	{
	int escCount = 0;
	char c;
	char cP;

	printf("\n\nEnter characters; five <Esc>'s to finish.\n\n");

	while(escCount < 5)
		{
		while(( c = rGetChar() ) < 0)
			;

		if(c == 27)
			escCount++;
		else
			escCount = 0;

		cP = c >= 32 ? c : '.';

		printf("\'%c\' 0x%02x %d\n",cP,c,c);
		}
	}

typedef struct
	{
	np_uart *uart;
	char rxChar;
	} UARTISRContext;

UARTISRContext gC = {0,-1};

void MyUARTISR(int context)
	{
	UARTISRContext *c = (UARTISRContext *)context;

	int status;
	int rxChar;

	status = c->uart->np_uartstatus;
	rxChar = c->uart->np_uartrxdata;

	c->uart->np_uartstatus = 0;	// clear the interrupt condition

	if(status & np_uartstatus_rrdy_mask)	// character arrived?
		printf("(%02x)",rxChar);	// we can printf from ISR! woo hoo!
	c->rxChar = rxChar;			// save the character for later use.
	}

void DoUARTEnableInterruptHandler(void)
	{
	printf("\n\n\n");
	printf("The UART interrupt will print out the hex code for\n");
	printf("each received character, without affecting the\n");
	printf("the operation of the main thread.\n\n");
	printf("This will remain in effect until you choose the\n");
	printf("Disable Interrupt Handler menu option.\n\n");


	// To "enable interrupts" for the UART,
	// we do two things.

	// 1. Install a "user ISR" using our library,
	//    on all three UART trap numbers.

	gC.uart = (np_uart *)na_uart1;
	nr_installuserisr(na_uart1_irq,MyUARTISR,(long)&gC);

	// 2. Enable interrupts for every condition on the UART for iRrdy only.
	//    We want the normal TxChar routines to behave.

		{
		np_uart *uart;

		uart = na_uart1;
		uart->np_uartcontrol = np_uartcontrol_irrdy_mask;
		}

	// That's all.
	}

void DoUARTDisableInterruptHandler(void)
	{
	np_uart *uart;

	// 1. Disable the UART's control bits for interrupts
	uart = na_uart1;
	uart->np_uartcontrol = 0;

	// 2. Remove our User ISR's
	nr_installuserisr(na_uart1_irq,0,0);

	printf("\n\n\nUART interrupt has been disabled.\n\n");
	}


// --------------------------------------------
/* rGetChar()
   return either the last character stashed
   by the UART interrupt, *or* whatever nr_uart_rxchar
   gives us. This, because the UART ISR will
   stash characters before nr_uart_rxchar can see 'em.

	2002-11-22	dspitzer	call nm_printf_rxchar instead of nr_uart_rxchar
 */

extern void *gReturnToMonitor;		// set by main. Any <RETURN> exits...

int rGetChar(void)
	{
	int result;

	if(gC.rxChar >= 0)
		{
		result = gC.rxChar;
		gC.rxChar = -1;
		}
	else
		{
#if nm_printf_rxchar == nr_jtag_rxchar
		result = nm_printf_rxchar( nasys_printf_uart );
#else
		result = nr_uart_rxchar(0);
#endif
		}

	if ( ( 13 == result ) || ( 10 == result ) )	// exit on CR or LF
		{
		ExitPeripheralTest();
		}

	return result;
	}

⌨️ 快捷键说明

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