實(shí)驗(yàn)?zāi)康模涸贒E10-Standard 下通過HPS控制FPGA實(shí)現(xiàn)流水燈
實(shí)驗(yàn)環(huán)境:
Quartus15.0-Lite
Soc EDS 17.0 (DS-5部分若需使用需付費(fèi))
Win32DiskImager
參考手冊(cè):
DE10官方手冊(cè)(DE10-Standard_v.1.2.4_SystemCD\Manual)
DE10-Standard Getting Started Guide
DE10-Standard User Manual Chapter 7
第一步:打開FPGA例程,修改引腳
位置:DE10-Standard_v.1.2.4_SystemCD\Demonstration\SoC_FPGA\DE10_Standard_GHRD
1.打開Quartus->File->Open Project->定位到官方例程的位置選擇qpf文件打開工程
2.點(diǎn)擊pin planner 根據(jù)新的開發(fā)板電路圖為 led0-led9重新分配引腳
具體引腳分配如上圖所示佩谣,綁定引腳過程中辜腺,若存在當(dāng)前引腳已分配页响,則刪除之前被綁定的部分志鞍,然后將引腳綁定到led上
3.編譯程序
4.編譯成功后燒錄程序
點(diǎn)擊program device
插上JTAG線,啟動(dòng)開發(fā)板電源优幸,選擇hardware setup
點(diǎn)擊auto detect 選擇型號(hào)
選擇生成的.sof文件燒錄
第二步編寫HPS程序
生成HPS頭文件
1.找到 sopc-create-header-files并把文件改為.sh文件
2.復(fù)制文件到FPGA工程下寂纪,并更改sopc-create-header-files的最后幾行的內(nèi)容
具體修改如下
cmd="sopcinfo2swinfo --input=$sopc_design_file --output=$swinfo_tmp_fname ${sopcinfo2swinfo_args[@]}"
/cygdrive/d/altera/15.0/quartus/sopc_builder/bin/sopcinfo2swinfo --input="$sopc_design_file" --output="$swinfo_tmp_fname" ${sopcinfo2swinfo_args[@]} || {
echo "$PN: $cmd failed"
exit 1
}
cmd="swinfo2header --swinfo $swinfo_tmp_fname --sopc $sopc_design_file ${swinfo2header_args[@]}"
/cygdrive/d/altera/15.0/quartus/sopc_builder/bin/swinfo2header --swinfo "$swinfo_tmp_fname" --sopc "$sopc_design_file" "${swinfo2header_args[@]}" || {
echo "$PN: $cmd failed"
exit 1
}
exit 0
3.打開SoCDES的shell,切到工程目錄下執(zhí)行
執(zhí)行如下語句夭织,便可在工程目錄下生成hps0.h文件
./sopc-create-header-files.sh soc_system.sopcinfo --single hps_0.h --module hps_0
編寫C語言程序
/*
This program demonstrate how to use hps communicate with FPGA through light AXI Bridge.
uses should program the FPGA by GHRD project before executing the program
refer to user manual chapter 7 for details about the demo
*/
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "hwlib.h"
#include "socal/socal.h"
#include "socal/hps.h"
#include "socal/alt_gpio.h"
#include "hps_0.h"
#define HW_REGS_BASE ( ALT_STM_OFST )
#define HW_REGS_SPAN ( 0x04000000 )
#define HW_REGS_MASK ( HW_REGS_SPAN - 1 )
int main() {
void *virtual_base;
int fd;
int loop_count;
int led_direction;
int led_mask;
void *h2p_lw_led_addr;
// map the address space for the LED registers into user space so we can interact with them.
// we'll actually map in the entire CSR span of the HPS since we want to access various registers within that span
if( ( fd = open( "/dev/mem", ( O_RDWR | O_SYNC ) ) ) == -1 ) {
printf( "ERROR: could not open \"/dev/mem\"...\n" );
return( 1 );
}
virtual_base = mmap( NULL, HW_REGS_SPAN, ( PROT_READ | PROT_WRITE ), MAP_SHARED, fd, HW_REGS_BASE );
if( virtual_base == MAP_FAILED ) {
printf( "ERROR: mmap() failed...\n" );
close( fd );
return( 1 );
}
h2p_lw_led_addr=virtual_base + ( ( unsigned long )( ALT_LWFPGASLVS_OFST + LED_PIO_BASE ) & ( unsigned long)( HW_REGS_MASK ) );
// toggle the LEDs a bit
loop_count = 0;
led_mask = 0x01;
led_direction = 0; // 0: left to right direction
while( loop_count < 60 ) {
// control led
*(uint32_t *)h2p_lw_led_addr = ~led_mask;
// wait 100ms
usleep( 100*1000 );
// update led mask
if (led_direction == 0){
led_mask <<= 1;
if (led_mask == (0x01 << (LED_PIO_DATA_WIDTH-1)))
led_direction = 1;
}else{
led_mask >>= 1;
if (led_mask == 0x01){
led_direction = 0;
loop_count++;
}
}
} // while
// clean up our memory mapping and exit
if( munmap( virtual_base, HW_REGS_SPAN ) != 0 ) {
printf( "ERROR: munmap() failed...\n" );
close( fd );
return( 1 );
}
close( fd );
return( 0 );
}
?編寫Makefile文件
#
TARGET = HPS_FPGA_LED
#
ALT_DEVICE_FAMILY ?= soc_cv_av
SOCEDS_ROOT ?= $(SOCEDS_DEST_ROOT)
HWLIBS_ROOT = $(SOCEDS_ROOT)/ip/altera/hps/altera_hps/hwlib
CROSS_COMPILE = arm-linux-gnueabihf-
CFLAGS = -g -Wall -D$(ALT_DEVICE_FAMILY) -I$(HWLIBS_ROOT)/include/$(ALT_DEVICE_FAMILY) -I$(HWLIBS_ROOT)/include/
LDFLAGS = -g -Wall
CC = $(CROSS_COMPILE)gcc
ARCH= arm
build: $(TARGET)
$(TARGET): main.o
$(CC) $(LDFLAGS) $^ -o $@
%.o : %.c
$(CC) $(CFLAGS) -c $< -o $@
.PHONY: clean
clean:
rm -f $(TARGET) *.a *.o *~
打開SoCDES的shell切到.c文件所在的文件夾之后make即可生成可執(zhí)行文件
第三步吭露,將生成的可執(zhí)行文件拷貝到操作系統(tǒng)中運(yùn)行即可
參考:
http://www.ngui.cc/51cto/show-59734.html