NIOS II JTAG UART通讯

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

P8062389

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

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

选择标准NIOS即可

2、添加PLL

点击Launch Altera‘s ALTPLL MegaWizard

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

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

输出两个时钟:

C0 50MHz   相位  0

C1 50MHz    相位 -72度

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

CPU的clock选择sys_clk

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

Presets选择Custom

Data width选择16

4、添加JTAG_UART

5、添加PIO

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

Direction选择Output port only

pio更名为led_pio

6、添加system ID

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

最后点击“Generate”生成系统

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

配置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<<i;
           else     led=8>>i;
           IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,~led);
           j=0;
           while(j<100000) j++;
        }
    }
}

#endif /* LED_H_ */

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

#include <stdio.h>
#include <string.h>
#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;
}

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

运行

原文地址:https://www.cnblogs.com/kingst/p/1795264.html