硬件环境是ICORE FPGA与ARM的双核心开发板
STM32配置的FSMC的代码(寄存器操作模式):
static int
initialize(void)
{
GPIOD_R->CRH.W = 0xbbbbbbbb;
GPIOD_R->CRL.W = 0xbbbbbbbb;
GPIOE_R->CRH.W = 0xbbbbbbbb;
GPIOE_R->CRL.W = 0xbbbbbbbb;
FSMC_Bank1_R->BCR1 &= ~(1 << 1 | 1 << 2 | 1 << 3);
FSMC_Bank1_R->BCR1 |= 1 << 14; //EXTMOD
FSMC_Bank1_R->BTR1 = 0x300;
FSMC_Bank1E_R->BWTR1 = 0X300;
return 0;
}
icore 利用STM32的FSMC的地址总线AB23 AB22 跟CS 通过138译码器 扩展出4路片选信号,CS0,CS1,CS2,CS3
CS0为FPGA的片选信号 4路片选的宏定义如下所示:
#define CS0_BASE (0x60000000 + (0 << 23))
#define CS1_BASE (0x60000000 + (1 << 23))
#define CS2_BASE (0x60000000 + (2 << 23))
#define CS3_BASE (0x60000000 + (3 << 23))
在icore的fsmc读取fpga的例程中利用STM32的FSMC的地址总线AB18、AB17、AB16进行对FPGA内部的存储空间地址选择,
这样只能定义8个16位的空间
#define fpga_write(offset, data) *((volatile unsigned short int*)(CS0_BASE + (offset << 17))) = data
#define fpga_read(offset) *((volatile unsigned short int*)(CS0_BASE + (offset << 17)))
以下对这部分代码进行修改
#define fpga_write_addr *((volatile unsigned short int*)(CS0_BASE))
#define fpga_write_read *((volatile unsigned short int*)(CS0_BASE)+ (1 << 16))
这样就利用AB18、AB17、AB16组合的8个空间中的2个 但可以扩展出更多的空间
fpga_write_addr 用于向fpga写入所要访问空间的1地址
fpga_write_read 用于对已定义地址空间数据的读写
调用下面代码就可实现读写
static int
fpga_w(uint16_t addr,uint16_t data) 往FPGA中写数据函数如 fpga_w(1,230); 往空间1 中写入230
{
fpga_write_addr = addr;
fpga_write_read = data;
return 1;
}
static int
fpga_r(uint16_t addr) 往FPGA中读取数据函数如 data = fpga_r(1); 读取空间1 中的数据存入data
{
uint16_t data=0;
fpga_write_addr = addr;
data = fpga_write_read ;
return data;
}
FPGA实现代码
fsmc.v模块的代码如下
module fsmc(
input[2:0] ab,
inout[15:0] data,
input clk_f,
input wrn,
input rdn,
input resetn,
input fpga_cs0,
output sing
);
reg [15:0] mem_d[7:0]; //定义数据空间 大小自己可以设定
reg [15:0] addr; //数据空间的地址
reg [15:0] indata;
reg [24:0] cnt;
reg rd;
reg wr;
always @(*)
begin
rd <= ~fpga_cs0 && ~rdn;
wr <= ~fpga_cs0 && ~wrn;
end
//---------------------------------------------------
always @(posedge clk_f)
cnt <= cnt + 1'b1;
assign sing = wr|rd;//|cnt[24];
initial begin
mem_d[0] = 16'd120;
mem_d[1] = 16'd121;
mem_d[2] = 16'd122;
mem_d[3] = 16'd123;
mem_d[4] = 16'd124;
mem_d[5] = 16'd125;
mem_d[6] = 16'd126;
mem_d[7] = 16'd127;
end
//---------------------------------------------------
//write data,
always @(posedge wr)// or negedge resetn)
begin
if(wr)//(!resetn)
begin
case (ab)
3'b000:addr <= data; //地址暂存
3'b001:mem_d[addr[3:0]] <= data; //往预设地址的空间中写入数据
default: ;
endcase
end
end
//red data
always @(posedge rd)//(rd)// or negedge resetn
begin
if(rd)//(!resetn) indata <= 16'h0000;
begin
case (ab)
3'b000:;
3'b001: indata <= mem_d[addr[3:0]]; //读取数据
default:;
endcase
end
end
assign data = rd ? indata:16'hzzzz;
endmodule