NIOS II JTAG UART通讯

本文转载至:http://www.cnblogs.com/Neddy/archive/2010/07/18/1780249.html,在此对原作者的辛勤劳作表示深深的敬意!

NIOS II JTAG UART通讯_第1张图片

一、硬件(使用的是Quartus II 9.0)

1、建立工程,打开SOPC Builder,添加CPU

2010071817103989.jpg

选择标准NIOS即可

2、添加PLL

2010071817143047.jpg

点击Launch Altera‘s ALTPLL MegaWizard

2010071817165168.jpg

器件速度等级按自己的FPGA选择,我的FPGA是EP2C8,所以选择8

输入时钟根据晶振决定,我的板子上是50MHz

输出两个时钟:

C0 50MHz   相位  0

C1 50MHz    相位 -72度

2010071817212424.jpg

2010071817230978.jpg

将Clock Settings中的pll.co更名为sys_clk,pll.c1更名为sd_clk

2010071817252992.jpg

CPU的clock选择sys_clk

3、添加SDRAM(我板子上是K4S641632H)

Presets选择Custom

Data width选择16

2010071817293065.jpg

4、添加JTAG_UART

2010071817315474.jpg

5、添加PIO

width选择4,我的板子上只有四个LED

Direction选择Output port only

2010071817330734.jpg

pio更名为led_pio

6、添加system ID

2010071817360129.jpg

7、点击System—>Auto Assign Base address和Auto Assign IRQs,系统配置完毕

2010071817394917.jpg

最后点击“Generate”生成系统

8、在Quartus II新建原理图BDF文件,添加刚建的SOPC

2010071817425924.jpg

配置I/O后,运行Quartus,并在到目标板上……

二、软件(NIOS II 9.0)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/*
* led.h
*
*  Created on: 2010-7-17
*      Author: Administrator
*/

#ifndef LED_H_
#define LED_H_
void led(unsigned char dir)
{
    unsigned char k,led;
    unsigned int i,j;
    for(k=10;k;k--)
    {
        for(i=0;i<4;i++)
        {
           if(!dir) led=1<           else     led=8>>i;
           IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,~led);
           j=0;
           while(j<100000) j++;
        }
    }
}

#endif /* LED_H_ */

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include
#include
#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "alt_types.h"
#include "led.h"

int main()
{
    FILE *fp;
    char str[6];
    char str1[5];
    char str2;
    fp=fopen("/dev/jtag_uart","r+");
    if(fp)
    {
        while(strcmp(str1,"stop")!=0)
        {
            printf("Leds are ready,please input start or stop\n");
            fgets(str,6,fp);
            strcpy(str1,str);
            str1[4]='\0';
            if(strcmp(str,"start")==0)  printf("Leds have run over,please input p or n or stop\n");
          while(strcmp(str1,"stop")!=0)
          {
                str2=getc(fp);
                if(str2=='n')
                {
                    led(0);
                    printf("Leds have run over,please input p or n or s\n");
                }
                else if(str2=='p')
                 {
                    led(1);
                    printf("Leds have run over,please input  p or n or s'\n");
                }              
                else if(str2=='s')  strcpy(str1,"stop");
          }
        }
    }

    printf("Leds have run over\n");
    fclose(fp);

    return 0;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

运行

2010071817511892.jpg

你可能感兴趣的:(NIOS II JTAG UART通讯)