自定义IP原来如此简单

首先用Verilog语言或者VHDL编写、或者用原理图来画硬件驱动,以数码管驱动为例子,编写Verilog如下:

1 module sg7IP(
2
3  input reset,
4  input clk,
5  input avs_s1_write,
6  input [31:0]avs_s1_writedata,
7  input avs_s1_address,
8  output wire [7:0] oSEG7
9 );
10
11  reg [7:0] sg7_r;
12  always @(posedge clk or posedge reset)
13 begin
14 if(reset)
15 sg7_r<=8'hf;
16 else if(avs_s1_write&&avs_s1_address==0)
17 sg7_r[7:0]<=avs_s1_writedata[7:0];
18
19 end
20 assign oSEG7=~sg7_r;
21 endmodule
22

然后编写一个顶层文件来调用sg7IP.v来测试验证上面的驱动是正常的。我这里编写了sg7top.v来作为顶层文件。

1 module seg7top(
2 input iCLK_50,
3 input [0:0]iKEY,
4 output wire [6:0] oHEX0_D,
5 output wire oHEX0_DP
6 );
7
8
9 sg7IP sg7ipu0(
10 .reset(!iKEY[0]),
11 .clk(iCLK_50),
12 .avs_s1_write(1'b1),
13 .avs_s1_writedata(32'd113),
14 .avs_s1_address(1'b0),
15 .oSEG7({oHEX0_DP,oHEX0_D[6:0]})
16

17 endmodule
18

分配管教,编译后下载到DE2-70的开发板中,数码管HEX0显示为F则表示硬件驱动正常。

然后打开SOPCBuilder,新建一个component。过程如下图。

添加sg7IP、onchipmemory、cpu、jtag uart组件,点击“生成”

完了后在QUARTUS II中修改sg7top.v

1 module seg7top(
2 input iCLK_50,
3 input [0:0]iKEY,
4 output wire [6:0] oHEX0_D,
5 output wire oHEX0_DP
6 );
7
8
9 nios_cpu nios_cpu_inst
10 (
11 .clk_0 (iCLK_50),
12 .oSEG7_from_the_sg7IP_0 ({oHEX0_DP,oHEX0_D[6:0]}),
13 .reset_n (iKEY[0])
14 );
15
16 endmodule
17

重新编译,下载。

打开NISO IDE。

新建空工程。

建立sg7.h

1 #ifndef SEG7_H_
2 #define SEG7_H_
3
4 void SEG7_NUM(alt_u8 num);
5
6
7 #endif /*SEG7_H_*/
8

建立sg7.c

1 #include "io.h"
2 #include "alt_types.h"
3
4 #include "system.h"
5
6 #include "SEG7.h"
7
8 #define SEG7_SET(seg_mask) IOWR(SG7IP_0_BASE,0,seg_mask)
9
10 static unsigned char szMap[] = {
11 63, 6, 91, 79, 102, 109, 125, 7,
12 127, 111, 119, 124, 57, 94, 121, 113
13 }; // 0,1,2,....9, a, b, c, d, e, f
14
15
16 void SEG7_NUM(alt_u8 num)
17 {
18 SEG7_SET(szMap[num]);
19 }
20

建立main.c

1 #include <stdio.h>
2 #include "system.h"
3 #include "unistd.h"
4 #include "alt_types.h"
5 #include "SEG7.h"
6
7 int main(void) {
8 alt_u8 i=0;
9 printf("My first IP");
10 while(1){
11 for(i = 0; i <10; i++) {
12 SEG7_NUM(i);
13 printf("Now print:%d\n",i);
14 usleep(1 * 1000 * 1000);
15 }
16 }
17 return 0;
18 }
19

CTRL+B编译。

run as  NIOS II  hardware。

开发板中的数码管从0到9不断变化。IP编写测试完毕。

原文地址:https://www.cnblogs.com/nios_ii/p/1844432.html