//####################-----------状态机频率---FLASH用---我的FLASH是90ns的---状态机用100ns即频率为10M#################
reg [15:0]clk_flash_10m;
reg clk_10m;
always @(posedge sys_clk)
begin
if(clk_flash_10m==16'd5)
begin
clk_flash_10m=0;clk_10m=1;
end
else
begin
clk_flash_10m=clk_flash_10m+1;clk_10m=0;
end
end
//####################-----------状态机频率---FLASH用------#################
assign FLASH_CE = 1'b0;
assign FLASH_RST = 1'b1;
reg [7:0] state;//state
reg FLASH_OE; // flash output signal
reg FLASH_WE;
reg [20:0] FLASH_ADDR;//flash address
reg [15:0]FLASH_DQ;
reg [15:0]FLASH_DQ_TEM;
reg [7:0] LED;//led,compare the writed data with the readed data offlash.
//if the first writed data is samed with the firstreader data, ledr[0]=0.
//if the second writed data is samed with thesecond reader data ,ledr[1]=0
//if erase chip succeed,ledr[2]=0;
reg [39:0] erase_cnt; //chip erase hold time
reg [15:0] number_flash;
always@ (posedge clk_10m or negedge sys_rst_n)
begin
if(!sys_rst_n) //reset
begin
state <= 99;
LED <= 8'd0; // led of red off
FLASH_OE <= 1'b1; // output enable lose effective
FLASH_WE<=1;
FLASH_ADDR <= 21'h555;
end
else
begin
case(state)
// chip erase初始化/格式化
99: begin if(number_flash==3000)begin state <= 0; number_flash<=0;end else begin number_flash<=number_flash+1;end end
0: begin FLASH_ADDR <= 21'h555; state <= 1 ;end
1: begin FLASH_WE <= 0;FLASH_DQ <= 16'haa; state <= 2 ;end
2: begin FLASH_WE <= 1;if(number_flash==1)begin state <= 3; number_flash<=0;end else begin number_flash<=number_flash+1;end end
3: begin FLASH_ADDR <= 21'h2aa; state <= 4 ;end
4: begin FLASH_WE <= 0;FLASH_DQ <= 16'h55; state <= 5 ;end
5: begin FLASH_WE <= 1;if(number_flash==1)begin state <= 6; number_flash<=0;end else begin number_flash<=number_flash+1; end end
6: begin FLASH_ADDR <= 21'h555; state <= 7 ;end
7: begin FLASH_WE <= 0;FLASH_DQ <= 16'h80; state <= 8 ;end
8: begin FLASH_WE <= 1;if(number_flash==1)begin state <= 9; number_flash<=0;end else begin number_flash<=number_flash+1; end end
9: begin FLASH_ADDR <= 21'h555; state <= 10 ;end
10: begin FLASH_WE <= 0;FLASH_DQ <= 16'haa; state <= 11 ;end
11: begin FLASH_WE <= 1;if(number_flash==1)begin state <= 12;number_flash<=0;end else begin number_flash<=number_flash+1;end end
12: begin FLASH_ADDR <= 21'h2aa; state <= 13 ;end
13: begin FLASH_WE <= 0;FLASH_DQ <= 16'h55; state <= 14 ;end
14: begin FLASH_WE <= 1;if(number_flash==1)begin state <= 15;number_flash<=0;end else begin number_flash<=number_flash+1;end end
15: begin FLASH_ADDR <= 21'b0000_0100_0000_0000_0000_0; state <= 16 ;end//扇区擦除命令
16: begin FLASH_WE <= 0; FLASH_DQ <= 16'h30; state <= 17 ;end
17: begin FLASH_WE <= 1;if(number_flash==1)begin state <= 18;number_flash<=0;end else begin number_flash<=number_flash+1;end end
18: begin // FLASH 初始化,0.7s一个扇区
if(erase_cnt == 40'd100000000)
begin
state <= 19;
//LED[0]<=1;//初始化完成 LED提示
end
else
begin
state <= 18; // current state
erase_cnt <= erase_cnt + 1'b1;
end
end
//**********************write first data**********************
19 : begin FLASH_ADDR <= 21'h555; state <= 20 ;end
20 : begin FLASH_WE <= 0; FLASH_DQ <= 16'haa; state <= 21 ;end
21 : begin FLASH_WE <= 1;if(number_flash==1)begin state <= 22;number_flash<=0;end else begin number_flash<=number_flash+1;end end
22 : begin FLASH_ADDR <= 21'h2aa; state <= 23 ;end
23 : begin FLASH_WE <= 0;FLASH_DQ <= 16'h55; state <= 24 ;end
24 : begin FLASH_WE <= 1;if(number_flash==1)begin state <= 25;number_flash<=0;end else begin number_flash<=number_flash+1;end end
25 : begin FLASH_ADDR <= 21'h555; state <= 26 ;end
26 : begin FLASH_WE <= 0;FLASH_DQ <= 16'ha0; state <= 27 ;end
27 : begin FLASH_WE <= 1;if(number_flash==1)begin state <= 28;number_flash<=0;end else begin number_flash<=number_flash+1;end end
28 : begin FLASH_ADDR <= 21'h008000; state <= 29; end//FIRST_ADDR = 0 ; FIRST_DATA = 1;16'h01
29 : begin FLASH_WE <= 0;FLASH_DQ <= 16'haaaa; state <= 30 ;end
30 : begin FLASH_WE <= 1;if(number_flash==90)begin state <= 31;number_flash<=0;end else begin number_flash<=number_flash+1;end end
31 : begin state <= 32; end
//********************** write second data **************************
32 : begin FLASH_ADDR <= 21'h555; state <= 33 ;end
33 : begin FLASH_WE <= 0;FLASH_DQ <= 16'haa; state <= 34 ;end
34 : begin FLASH_WE <= 1;if(number_flash==1)begin state <= 35;number_flash<=0;end else begin number_flash<=number_flash+1;end end
35 : begin FLASH_ADDR <= 21'h2aa; state <= 36 ;end
36 : begin FLASH_WE <= 0;FLASH_DQ <= 16'h55; state <= 37 ;end
37 : begin FLASH_WE <= 1;if(number_flash==1)begin state <= 38;number_flash<=0;end else begin number_flash<=number_flash+1;end end
38 : begin FLASH_ADDR <= 21'h555; state <= 39 ;end
39 : begin FLASH_WE <= 0;FLASH_DQ <= 16'ha0; state <= 40 ;end
40 : begin FLASH_WE <= 1;if(number_flash==1)begin state <= 41;number_flash<=0;end else begin number_flash<=number_flash+1;end end
// write 16'h02 of data to 21'h1 of address
41 : begin FLASH_ADDR <= 21'h008001; state <= 42;end
42 : begin FLASH_WE <= 0;FLASH_DQ <= 16'h0; state <= 43 ;end
43 : begin FLASH_WE <= 1;if(number_flash==90)begin state <= 44;number_flash<=0;end else begin number_flash<=number_flash+1;end end
44 : begin state <= 45; end
45 : begin FLASH_ADDR <= 21'h008000; state <= 46; end
46 : begin FLASH_OE <= 0; state <= 47 ;end
47 : begin FLASH_DQ_TEM <= FLASH_DQ; state <= 48 ;end
48 : begin FLASH_OE <= 1; state <= 49 ;end