技术标签: fpga
大多数FPGA内部不具有掉电存储程序的功能,所以都是外置flash存储器来存储程序,上电后加载flash中的程序到FPGA中,在运行。外置flash不仅可以作为存储程序使用,也可以存储任何你想存储的用户数据,这样可以更有效的利用flash的存储空间,本文不讲其寄存器及原理,这个网上很多。
flash_spi:
`timescale 1ns / 1ps
//
// Company:
// Engineer: QSJ
//
// Create Date: 2021/03/26 9:02:44
// Design Name:
// Module Name: flash_spi
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//
module flash_spi(
input wire sys_clk,
input wire i_rst_n,
// ---------- spi port ---------
output wire o_spi_clk,
output wire o_spi_cs,
output wire o_spi_mosi,
input wire i_spi_miso,
// --------- data port -----------
input wire [7:0] iv_write_data ,
output reg o_wr_1_byte_ok ,
input wire [15:0] iv_write_num,
input wire [15:0] iv_read_num,
input wire [3:0] iv_cmd_type,
output reg o_flash_done,
input wire [7:0] iv_flash_cmd,
input wire [31:0] iv_flash_addr,
output reg [7:0] ov_read_data,
output wire o_read_data_vld
);
wire spi_clk;
reg spi_cs;
reg spi_mosi;
wire spi_miso;
assign o_spi_clk = spi_clk;
assign o_spi_cs = spi_cs;
assign o_spi_mosi = spi_mosi;
assign spi_miso = i_spi_miso;
reg read_data_vld;
reg [7:0] read_data;
reg [2:0] spi_state;
reg spi_clk_en=1'b0;
reg data_come;
assign o_read_data_vld=read_data_vld;
assign spi_clk=spi_clk_en?sys_clk:0;
parameter IDLE = 3'b000;
parameter CMD_SEND = 3'b001;
parameter ADDRESS_SEND = 3'b010;
parameter READ_WAIT = 3'b011;
parameter WRITE_DATA = 3'b101;
parameter FINISH_DONE = 3'b110;
reg [7:0] cmd_reg;
reg [31:0] address_reg;
reg [7:0] wr_bit_cnt;
reg [15:0] write_cnt;
reg [7:0] rd_bit_cnt;
reg [15:0] read_cnt;
reg [15:0] read_num_inner;
reg read_finish;
always @(negedge sys_clk)
begin
if(!i_rst_n)
begin
spi_cs<=1'b1;
spi_state<=IDLE;
cmd_reg<=0;
address_reg<=0;
spi_clk_en<=1'b0;
wr_bit_cnt<=0;
write_cnt<=0;
read_num_inner<=0;
o_flash_done<=1'b0;
data_come<=1'b0;
o_wr_1_byte_ok <= 1'b0;
end
else
begin
case(spi_state)
IDLE: begin
spi_clk_en<=1'b0;
spi_cs<=1'b1;
spi_mosi<=1'b1;
cmd_reg<=iv_flash_cmd;
address_reg<=iv_flash_addr;
o_flash_done<=1'b0;
if(iv_cmd_type[3]==1'b1)
begin
spi_state<=CMD_SEND;
wr_bit_cnt<=7;
write_cnt<=0;
read_num_inner<=0;
end
end
CMD_SEND:
begin
spi_clk_en<=1'b1;
spi_cs<=1'b0;
if(wr_bit_cnt>0)
begin
spi_mosi<=cmd_reg[wr_bit_cnt];
wr_bit_cnt<=wr_bit_cnt-1'b1;
end
else
begin
spi_mosi<=cmd_reg[0];
if ((iv_cmd_type[2:0]==3'b001) | (iv_cmd_type[2:0]==3'b100))
begin
spi_state<=FINISH_DONE;
end
else if (iv_cmd_type[2:0]==3'b011)
begin
spi_state<=READ_WAIT;
wr_bit_cnt<=7;
read_num_inner<=1;
end
else if (iv_cmd_type[2:0]==3'b000)
begin
spi_state<=READ_WAIT;
wr_bit_cnt<=7;
read_num_inner<=17;
end
else
begin
spi_state<=ADDRESS_SEND;
wr_bit_cnt<=31;
end
end
end
ADDRESS_SEND:
begin
if(wr_bit_cnt>0)
begin
spi_mosi<=address_reg[wr_bit_cnt];
wr_bit_cnt<=wr_bit_cnt-1;
end
else begin
spi_mosi<=address_reg[0];
if(iv_cmd_type[2:0]==3'b010)
begin
spi_state<=FINISH_DONE;
end
else if (iv_cmd_type[2:0]==3'b101)
begin
spi_state<=WRITE_DATA;
wr_bit_cnt<=7;
end
else begin
spi_state<=READ_WAIT;
read_num_inner<=iv_read_num;
end
end
end
READ_WAIT:
begin
if(read_finish)
begin
spi_state<=FINISH_DONE;
data_come<=1'b0;
end
else
data_come<=1'b1;
end
WRITE_DATA:
begin
if(write_cnt<iv_write_num)
begin
if(wr_bit_cnt>0)
begin
spi_mosi<=iv_write_data[wr_bit_cnt];
wr_bit_cnt<=wr_bit_cnt-1'b1;
o_wr_1_byte_ok <= 1'b0;
end
else
begin
spi_mosi<=iv_write_data[0];
wr_bit_cnt<=7;
o_wr_1_byte_ok <= 1'b1;
write_cnt<=write_cnt+1'b1;
end
end
else
begin
spi_state<=FINISH_DONE;
spi_clk_en<=1'b0;
o_wr_1_byte_ok <= 1'b0;
write_cnt <= 0;
end
end
FINISH_DONE:
begin
spi_cs<=1'b1;
spi_mosi<=1'b1;
spi_clk_en<=1'b0;
o_flash_done<=1'b1;
spi_state<=IDLE;
end
default:spi_state<=IDLE;
endcase
end
end
always @(posedge sys_clk)
begin
if(!i_rst_n)
begin
read_cnt<=0;
rd_bit_cnt<=0;
read_finish<=1'b0;
read_data_vld<=1'b0;
read_data<=0;
ov_read_data<=0;
end
else
if(data_come)
begin
if(read_cnt<read_num_inner)
begin
if(rd_bit_cnt<7)
begin
read_data_vld<=1'b0;
read_data<={
read_data[6:0],spi_miso};
rd_bit_cnt<=rd_bit_cnt+1'b1;
end
else
begin
read_data_vld<=1'b1;
ov_read_data<={
read_data[6:0],spi_miso};
rd_bit_cnt<=0;
read_cnt<=read_cnt+1'b1;
end
end
else begin
read_cnt<=0;
read_finish<=1'b1;
read_data_vld<=1'b0;
end
end
else
begin
read_cnt<=0;
rd_bit_cnt<=0;
read_finish<=1'b0;
read_data_vld<=1'b0;
read_data<=0;
end
end
endmodule
flash_cmd:
`timescale 1ns / 1ps
//
// Company:
// Engineer: QSJ
//
// Create Date: 2021/03/26 9:04:02
// Design Name:
// Module Name: flash_cmd
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//
module flash_cmd(
input sys_clk,
input i_rst_n,
input i_wr_flash_start,
input i_rd_flash_start,
input i_rd_device_id_start,
input i_subsector_erase_start,
input wire [7:0] iv_write_data ,
output wire o_wr_1_byte_ok ,
input wire [15:0] iv_write_num,
input wire [15:0] iv_read_num,
input wire [31:0] iv_base_addr ,
output o_spi_clk ,
output o_spi_cs ,
output o_spi_mosi ,
input i_spi_miso ,
output [7:0] ov_rd_data,
output o_rd_data_vld
);
reg [7:0] flash_cmd_def;
reg [3:0] cmd_type;
wire flash_done;
wire [7:0] read_data;
wire read_data_vld;
reg [4:0] curr_state = 'd15;
always @ ( posedge sys_clk )
begin
if( !i_rst_n ) begin
curr_state <= 'd15;
flash_cmd_def <= 8'd0;
cmd_type <= 4'b0000;
end
else
begin
case( curr_state )
'd0://idle
if(i_wr_flash_start) curr_state <= 'd8 ;
else if(i_rd_flash_start) curr_state <= 'd13;
else if(i_rd_device_id_start) curr_state <= 'd1 ;
else if(i_subsector_erase_start) curr_state <= 'd3 ;
else curr_state <= 'd0;
// -------------- read device ID ------------------
'd1://Read Status Register:05H
if( flash_done )
begin
if (read_data[0]==1'b0)
begin
flash_cmd_def <= 8'h00;
curr_state <= 'd2;
cmd_type <= 4'b0000;
end
else
begin
flash_cmd_def <= 8'h05;
cmd_type <= 4'b1011;
end
end
else
begin
flash_cmd_def <= 8'h05;
cmd_type <= 4'b1011;
end
'd2:// Read Device ID:9FH
if( flash_done )
begin
flash_cmd_def <= 8'h00;
if(read_data == 8'hFF) // if the device id is error
curr_state <= 'd1;
else
curr_state <= 'd0;
cmd_type <= 4'b0000;
end
else
begin
flash_cmd_def <= 8'h9f;
curr_state <= curr_state;
cmd_type <= 4'b1000;
end
// -------------- Erase ------------------
'd3://Read Status Register:05H
if( flash_done )
begin
if (read_data[0]==1'b0)
begin
flash_cmd_def <= 8'h00;
curr_state <= 'd4;
cmd_type <= 4'b0000;
end
else
begin
flash_cmd_def <= 8'h05;
cmd_type <= 4'b1011;
end
end
else
begin
flash_cmd_def <= 8'h05;
cmd_type <= 4'b1011;
end
'd4://Write Enable:06H
if( flash_done )
begin
flash_cmd_def <= 8'h00;
curr_state <= 'd5;
cmd_type <= 4'b0000;
end
else
begin
flash_cmd_def <= 8'h06;
curr_state <= curr_state;
cmd_type <= 4'b1001;
end
'd5://4-byte address mode Sector Erase:DCH Subsector Erase:21H
if( flash_done )
begin
flash_cmd_def <= 8'h00;
curr_state <= 'd6;
cmd_type<=4'b0000;
end
else
begin
flash_cmd_def <= 8'h21;
curr_state <= curr_state;
cmd_type <= 4'b1010;
end
'd6://Read Status Register:05H
if( flash_done )
begin
if (read_data[0]==1'b0)
begin
flash_cmd_def <= 8'h00;
curr_state <= 'd7;
cmd_type <= 4'b0000;
end
else
begin
flash_cmd_def <= 8'h05;
cmd_type <= 4'b1011;
end
end
else
begin
flash_cmd_def <= 8'h05;
cmd_type <= 4'b1011;
end
'd7://Write disable: 04H
if( flash_done )
begin
flash_cmd_def <= 8'h00;
curr_state <= 'd0;
cmd_type <= 4'b0000;
end
else
begin
flash_cmd_def <= 8'h04;
cmd_type <= 4'b1100;
end
// -------------- write Data ------------------
'd8://Read Status Register:05H
if( flash_done )
begin
if (read_data[0]==1'b0)
begin
flash_cmd_def <= 8'h00;
curr_state <= 'd9;
cmd_type <= 4'b0000;
end
else
begin
flash_cmd_def <= 8'h05;
cmd_type <= 4'b1011;
end
end
else
begin
flash_cmd_def <= 8'h05;
cmd_type <= 4'b1011;
end
'd9://Write Enable:06H
if( flash_done )
begin
flash_cmd_def <= 8'h00;
curr_state <= 'd10;
cmd_type <= 4'b0000;
end
else
begin
flash_cmd_def <= 8'h06;
cmd_type <= 4'b1001;
end
'd10://4-byte address page program: write data to flash
if( flash_done )
begin
flash_cmd_def <= 8'h00;
curr_state <= 'd11;
cmd_type <= 4'b0000;
end
else
begin
flash_cmd_def <= 8'h12;
cmd_type <= 4'b1101;
end
'd11://Read Status Register:05H
if( flash_done )
begin
if (read_data[0]==1'b0)
begin
flash_cmd_def <= 8'h00;
curr_state <= 'd12;
cmd_type <= 4'b0000;
end
else
begin
flash_cmd_def <= 8'h05;
cmd_type <= 4'b1011;
end
end
else
begin
flash_cmd_def <= 8'h05;
cmd_type <= 4'b1011;
end
'd12://Write disable: 04H
if( flash_done )
begin
flash_cmd_def <= 8'h00;
curr_state <= 'd0;
cmd_type <= 4'b0000;
end
else
begin
flash_cmd_def <= 8'h04;
cmd_type <= 4'b1100;
end
// -------------- read Data ------------------
'd13://Read Status Register:05H
if( flash_done ) begin
if (read_data[0]==1'b0)
begin
flash_cmd_def <= 8'h00;
curr_state <= 'd14;
cmd_type <= 4'b0000;
end
else
begin
flash_cmd_def <= 8'h05;
cmd_type <= 4'b1011;
end
end
else
begin
flash_cmd_def <= 8'h05;
cmd_type <= 4'b1011;
end
'd14://4-byte address read flash data
if( flash_done )
begin
flash_cmd_def <= 8'h00;
curr_state <= 'd0;
cmd_type <= 4'b0000;
end
else
begin
flash_cmd_def <= 8'h13;
cmd_type <= 4'b1110;
end
// -------------- enter 4-byte mode ------------------
'd15://Read Status Register:05H
if( flash_done ) begin
if (read_data[0]==1'b0)
begin
flash_cmd_def <= 8'h00;
curr_state <= 'd16;
cmd_type <= 4'b0000;
end
else
begin
flash_cmd_def <= 8'h05;
cmd_type <= 4'b1011;
end
end
else
begin
flash_cmd_def <= 8'h05;
cmd_type <= 4'b1011;
end
'd16://Write Enable:06H
if( flash_done )
begin
flash_cmd_def <= 8'h00;
curr_state <= 'd17;
cmd_type <= 4'b0000;
end
else
begin
flash_cmd_def <= 8'h06;
cmd_type <= 4'b1001;
end
'd17://Enter 4-byte address mode:B7H
if( flash_done )
begin
flash_cmd_def <= 8'h00;
curr_state <= 'd18;
cmd_type <= 4'b0000;
end
else
begin
flash_cmd_def <= 8'hB7;
cmd_type <= 4'b1001;
end
'd18://Read Status Register:05H
if( flash_done )
begin
if (read_data[0]==1'b0)
begin
flash_cmd_def <= 8'h00;
curr_state <= 'd19;
cmd_type <= 4'b0000;
end
else
begin
flash_cmd_def <= 8'h05;
cmd_type <= 4'b1011;
end
end
else
begin
flash_cmd_def <= 8'h05;
cmd_type <= 4'b1011;
end
'd19://Write disable: 04H
if( flash_done )
begin
flash_cmd_def <= 8'h00;
curr_state <= 'd1;
cmd_type <= 4'b0000;
end
else
begin
flash_cmd_def <= 8'h04;
cmd_type <= 4'b1100;
end
endcase
end
end
reg [31:0] base_addr;
always @ ( posedge sys_clk)
begin
if( !i_rst_n ) begin
base_addr <= 'd0;
end
else
begin
if(curr_state == 0)
begin
if(i_wr_flash_start|i_rd_flash_start|i_subsector_erase_start) base_addr <= iv_base_addr ;
else base_addr <= 'd0;
end
else base_addr <= base_addr ;
end
end
flash_spi U_flash_spi(
.sys_clk( sys_clk ),
.i_rst_n( i_rst_n ),
.o_spi_clk ( o_spi_clk ),
.o_spi_cs ( o_spi_cs ),
.o_spi_mosi ( o_spi_mosi ),
.i_spi_miso ( i_spi_miso ),
.iv_write_data ( iv_write_data ),
.o_wr_1_byte_ok ( o_wr_1_byte_ok ),
.iv_write_num ( iv_write_num ),
.iv_read_num ( iv_read_num ),
.iv_cmd_type ( cmd_type ),
.o_flash_done ( flash_done ),
.iv_flash_cmd ( flash_cmd_def ),
.iv_flash_addr ( base_addr ),
.ov_read_data ( read_data ),
.o_read_data_vld( read_data_vld )
);
assign ov_rd_data = read_data;
assign o_rd_data_vld = (curr_state == 'd14) ? read_data_vld : 0;
endmodule
以上就是全部内容,仅做个人记录,若需要对flash进行更多操作,可阅读flash对应的datasheet。
有完整的VIVADO FPGA测试工程。
工程未经过全面的调试,有问题请指正!
文章浏览阅读1k次。Sequence Numberlzyws7393074532892018-04-25Number Sequenceqq_391789932452017-09-21理解TCP序列号(Sequence Number)和确认号(Acknowledgment Number)hebbely9822017-01-14Number Sequence(规律)l25336363712902017-07-18Numb..._ack num
文章浏览阅读5.9k次。笔记本电脑怎么进CMOS密码巧设置笔记本电脑怎么进CMOS密码巧设置 笔记本电脑为了保护用户的数据安全,往往采用加密的方式,最常见的还是CMOS密码加密技术。为了让你的重要数据更加安全,你可能需要设置不同的密码,这也就要求你记住许多密码。对于笔记本电脑用户来说,真的需要设置一道道密码关卡吗?非也非也! 一、认识与设置笔记本电脑的CMOS密码 笔记本电脑的CMOS密码大致分为超级密码(Supervi..._电脑第一道密码修改
文章浏览阅读2.5k次,点赞2次,收藏5次。迟到的文章,就当库存发出来吧~_jangow01
文章浏览阅读1.7w次,点赞2次,收藏5次。默认情况下RDD的transformation是lazy形式,实际计算只有在ation时才会进行,而且rdd的计算结果默认都是临时的,用过即丢弃,每个action都会触发整个DAG的从头开始计算,因此在迭代计算时都会想到用cache或persist进结果进行缓存。敝人看到很多资料或书籍有的说是persist或cache会触发transformation真正执行计算,有的说是不会!敝人亲自实验了一把..._spark cache和persist不生效
文章浏览阅读2.4k次。HTML之marquee(文字滚动)详解语法:以下是一个最简单的例子:代码如下:Hello, World下面这两个事件经常用到:onMouseOut=“this.start()” :用来设置鼠标移出该区域时继续滚动onMouseOver=“this.stop()”:用来设置鼠标移入该区域时停止滚动代码如下:onMouseOut=“this.start()” :用来设置鼠标移出该区域时继续滚动 onMouseOver=“this.stop()”:用来设置鼠标移入该区域时停止滚动这是一个完_html滚动
leecode++理解:本文介绍了一些LeetCode题目的解析和思路,包括两数相加、寻找有序数组的中位数、最长字串、整数反转等。还讨论了一些下标规律和正则表达式匹配问题。
文章浏览阅读629次。Java Concurrency in Practice中对线程安全的定义:当多个线程访问一个类时,如果不用考虑这些线程在运行时环境下的调度和交替运行,并且不需要额外的同步及在调用方代码不必做其他的协调,这个类的行为仍然是正确的,那么这个类就是线程安全的。显然只有资源竞争时才会导致线程不安全,因此无状态对象永远是线程安全的 。过多的同步会产生死锁的问题,死锁属于程序运行的时_java 线程概述
文章浏览阅读1.2w次,点赞10次,收藏61次。读取表单Sheet2中部分信息。_matlab读取数据
文章浏览阅读1.4w次。最近项目中用到ItemBased Collaborative Filtering,实践过spark mllib中的ALS,但是因为其中涉及到降维操作,大数据量的计算实在不能恭维。所以自己实践实现基于spark的分布式cf,已经做了部分优化。目测运行效率还不错。以下代码package modelimport org.apache.spark.broadcast.Broadcastimp_spark cf
文章浏览阅读1.8w次。 在我的博客移动平台播放器ijkplayer开源框架分析(以IOS源码为例),大致介绍了一下ijkplayer的基本函数调用顺序和主要线程作用,本博客想介绍一下在直播应用中,针对卡顿和秒开做的一些优化,本优化经验主要是用在Android系统上,ios上也可以借鉴,按本博客修改代码,网络带宽足够的情况下,音视频播放基本流畅不卡顿,首屏时间在500ms以内。 首先来看直播应用中的卡顿。直..._libijkplayer 播放直播流卡顿
文章浏览阅读2.3k次,点赞3次,收藏28次。数据挖掘实践(金融风控-贷款违约预测)(三):特征工程目录数据挖掘实践(金融风控-贷款违约预测)(三):特征工程1.引言2.特征预处理2.1缺失值填充2.2时间格式处理2.3类别特征处理3.异常值处理3.1 检测异常的方法一:正态分布法3.2 检测异常的方法二:箱型图3.3异常值的处理方法4.数据分桶5.特征交互6.特征编码6.1 labelEncode 直接放入树模型中6.2 逻辑回归等模型要单独增加的特征工程7.特征选择7.1 Filter7.2 Wrapper (Recursive feature _金融风控(大数据)特征工程
文章浏览阅读1.1k次,点赞7次,收藏11次。2️⃣若失败消息提示需要更换http,去阿里云中找到相关镜像 测试连接通过后重新下载驱动。3️⃣上述方法还是不行 可以关闭data grip后再来几遍。1️⃣首先将电脑连接的Wi-Fi关闭 连接手机热点。_datagrip插件下载失败