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

📄 peripheral_test.c

📁 基于fpga和sopc的用VHDL语言编写的EDA的32位Nios CPU嵌入式系统软硬件设计
💻 C
字号:


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


//
// This "Peripheral Test" is only
// meaningful on the Nios32 Reference
// design. If you build a Nios system
// with a different collection of
// peripherals, then parts of this
// code will no longer apply, and
// might not even compile.
//


// --------------------------------------
// Menu Utility Routines

void MenuBegin(char *title)
	{
	printf("\n\n");
	printf("----------------------------------\n");
	printf("Nios Peripheral Test\n");
	printf("----------------------------------\n");
	printf(" %s\n",title);
	}

void MenuEntry(char letter,char *string)
	{
	printf("     %c:  %s\n",letter,string);
	}

int MenuEnd(char lowLetter,char highLetter)
	{
	int c;
	c = 23;
	printf("     q:  Exit\n");

	printf("----------------------------------\n");
	printf("\nSelect Choice (%c-%c): ",lowLetter,highLetter);

here:
	while((c = rGetChar()) < 0)
		;

	if(c >= 'A' && c <= 'Z')
		c += 'a' - 'A';
	
	if(c == 27)
		c = 'q';
	
	if(c == 'q' || (c >= lowLetter && c <= highLetter))
		{
		printf("%c\n",c);
		return (char)c;
		}

	goto here;
	}

#define MenuCase(letter,proc) case letter:proc(); break;


void *gReturnToMonitor;	// referenced by rGetChar, so any <RETURN> goes there.

int main(void)
	{
	int c;
	register void *returnToMonitor;

    setbuf(stdout,0);  // turn off printf buffering

	//
	// Set up a global return-address, so that
	// rGetChar can return to the monitor at ANY time
	// if user presses <RETURN>.
	//
	// Normally, main's return (or any call to exit())
	// executes a TRAP 0. But we want this program in flash,
	// to a TRAP 0 would just rerun itself. In this particular
	// case, we want to return to the caller.
	//
	// nr_setup puts the caller's return address in %i5 for us.
	//

	asm ("mov %0,%%i5" : "=r" (returnToMonitor) );
	gReturnToMonitor = returnToMonitor;

	printf("\n(Return address is 0x%08x\n",(int)gReturnToMonitor * 2);

    //
    // Clear the UART of buffered characters
    //
    nr_delay(100);
    while(nr_uart_rxchar(0) >= 0)
        {
        nr_delay(10);
        }

	//
	// Print a welcome message
	//

	printf("\n\n\n");
	printf("-------------------------------------\n");
	printf("Welcome to the Nios development board\n");
	printf("-------------------------------------\n");
	printf("\n");

mainMenu:
	MenuBegin("MAIN MENU");
	MenuEntry('a',"Timer");
	MenuEntry('b',"UART");
	MenuEntry('c',"Buttons & Switches");
	MenuEntry('d',"LED");
	MenuEntry('e',"Seven Segment Display");
	MenuEntry('f',"Bus & Memory");

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

	switch(c)
		{
		MenuCase('a',DoTimerMenu);
		MenuCase('b',DoUARTMenu);
		MenuCase('c',DoButtonsMenu);
		MenuCase('d',DoLEDMenu);
		MenuCase('e',DoSevenSegMenu);
		MenuCase('f',DoMemoryMenu);
		}

	if(c != 'q')
		goto mainMenu;

	// else, exit

	ExitPeripheralTest();
	}


// |
// | Turn off timer and uart irqs, just in case
// |

void ExitPeripheralTest(void)
	{
	// Set the 7-seg back to all-on
	na_seven_seg_pio->np_piodata = 0;

	DoUARTDisableInterruptHandler();
	DoDisableTimerInterrupt();

	exit(0);
	}

void DoButtonsMenu(void)
	{
	int c;
	long delay;
	int buttons,buttonsLast;
	int j;
	np_pio *pio = na_button_pio;

	pio->np_piodirection = 0;	// all input

menu:
	MenuBegin("BUTTONS PIO MENU");
	MenuEntry('a',"Show Buttons");

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

	switch(c)
		{
		case 'a':
			printf("\n\nButtons shown when changed. <Esc> to exit.\n\n");
			buttonsLast = 0x5555;
			while(rGetChar() != 27)
				{
				buttons = pio->np_piodata;
				if(buttons != buttonsLast)
					{
					printf("buttons: %02x/%1x \n",
						(buttons >> 4) & 0xff,
						buttons & 0xf);

					buttonsLast = buttons;
					}
				}
		}
	}

void DoLEDMenu(void)
	{
	int c;
	np_pio *pio = na_led_pio;

	pio->np_piodirection = 3;

menu:
	MenuBegin("LED MENU");
	MenuEntry('a',"LED 0 on/off");
	MenuEntry('b',"LED 1 on/off");

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

	switch(c)
		{
		case 'a':
			pio->np_piodata ^= 1;
			break;
			
		case 'b':
			pio->np_piodata ^= 2;
			break;
		}
	if(c != 'q')
		goto menu;
	}

void DoSevenSegMenu(void)
	{
	int c;
	long delay;
	int i;
	long j;

menu:
	MenuBegin("SEVEN SEGMENT MENU");
	MenuEntry('a',"Count Slowly");
	MenuEntry('b',"Count Rapidly");
	MenuEntry('c',"Control Segments");

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

	switch(c)
		{
		case 'a':
			delay = 150000;
		count:
			printf("\n\n\nCounting on seven segment display... (*) ");
			for(i = 0; i <= 0xff; i++)
				{
				printf("%02x ",i);
				for(j = 0; j < delay; j++)
					{
					// Pressing a key aborts this tedious process.
					if(rGetChar() > 0)
						goto doneCount;

					nr_pio_showhex(i);
					}
				}
		doneCount:
			printf("(*) Done.\n\n\n");
			break;

		case 'b':
			delay = 8000;
			goto count;

		case 'c':
			DoSevenSegSegmentControls();
			break;
		}
	if(c != 'q')
		goto menu;

	}
void DoSevenSegSegmentControls(void)
	{
	np_pio *pio = na_seven_seg_pio;
	long bits = 0xffff;
	long keyBit;
	char c;

	printf("\n");
	printf("\n");
	printf("         +-A--+     +-a--+\n");
	printf("         |    |     |    |\n");
	printf("         F    B     f    b\n");
	printf("         |    |     |    |\n");
	printf("         +-G--+     +-g--+\n");
	printf("         |    |     |    |\n");
	printf("         E    C     e    c\n");
	printf("         |    |     |    |\n");
	printf("         +-D--+ (H) +-d--+ (h)\n");
	printf("\n");
	printf("Press <Esc> to finish.\n");

	while((c = rGetChar()) != 27)
		{
		keyBit = 0;
		if(c >= 'a' && c <= 'g')
			keyBit = 1 << ('g' - c);
		else if(c == 'h')
			keyBit = 1 << 7;
		else if(c >= 'A' && c <= 'G')
			keyBit = 1 << ('G' - c + 8);
		else if(c == 'H')
			keyBit = 1 << 15;

		bits ^= keyBit;
		pio->np_piodata = bits;
		}
	}


⌨️ 快捷键说明

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