基于FPGA(cyclone IV)的NOR FLASH的应用

基于FPGA(cyclone IV)的NOR FLASH的应用

1.器件挂载到QSYS的三态驱动器上;
2. 查阅datasheet,配置时序;
3.编译,参考“黑金”或者“微雪”的例程…

实际工程使用S29GL512P,EPCS选用EPCS64,IS61WV51216该系统搭载十分豪华配置…
NOR FLASH 容量 512Mbits,64MB,
EPCS 64Mbit,8MB,
不用sdram,避免时钟相位问题

在NIOS II Eclipse中,第一行中include “sys/alt_flash.h”,包含着FLASH库函数
,nor flash 中的地址宽度为0x3fff_ffff(该参数位QSYS中对齐数据位用),实际的25根地址线,寻址范围位2^25 =512*63335,所以共512个Block,1个region,每个Block为0x10000,所以FLASH_OFFSET 值不宜乱取。由于flash本身的性质,如果要擦除位于某一个block中的某个地址(哪怕仅仅是一个),整个block的内容都会被擦除(全1)。

下面是的基本flash操作函数:
alt_flash_open_dev(),
alt_write_flash(),
alt_read_flash(),
alt_flash_close_dev(),
alt_get_flash_info(),
alt_erase_flash_block(),
alt_write_flash_block(),

 1 #include "sys/alt_flash.h"
 2 #include 
 3 #include 
 4 #include 
 5 
 6 #define BUF_SIZE 1024
 7 #define FLASH_OFFSET 0x50000//偏移地址,实际为第六个Block
 8 
 9 int main()
10 {
11     printf("Fuck Nios II!\n");
12 
13     int i;
14     alt_flash_fd* fd;
15     flash_region* regions;
16     int number_of_regions;
17     int ret_code = 0x0;
18     unsigned char source[BUF_SIZE];
19     unsigned char dest[BUF_SIZE];
20 
21     unsigned char block_dest[0x2000];
22 
23     for(i=0;i<100;i++){
24         source[i] = i;
25     }
26     fd = alt_flash_open_dev("/dev/cfi_flash");
27     printf("flash opening...\n");    //Debug
28     //fd = alt_flash_open_dev(“/dev/epcs_flash”);    //EPCSX
29     if (fd){
30         printf("flash opened\n");    //Debug
31         printf("fd is %d\n",fd);    //Debug
32         ret_code = alt_get_flash_info(fd, ®ions, &number_of_regions);
33         for(i=0;ioffset,
36                     (regions+i)->region_size+(regions+i)->offset,
37                     (regions+i)->number_of_blocks,
38                     (regions+i)->block_size);
39         }
40         alt_erase_flash_block(fd, FLASH_OFFSET, BUF_SIZE);
41 
42 /*        //////////////////////////////////////////////////////////
43         /////        the entire block is erased to 1            //////
44         //////////////////////////////////////////////////////////
45         ret_code = alt_read_flash(fd,0x4000,block_dest,0x2000);///
46         if(ret_code != 0){                                        //
47             printf("Can't read flash device\n");                //
48             exit(-1);                                            //
49         }                                                        //
50         else{                                                    //
51             printf("Read Flash Device Successfully.\n");        //
52         }                                                        //
53         //    Print the destination data                            //
54         for(i=0;i<0x2000;i++){                                    //
55             printf("OFFSET_Address %4d  is  %d\n",i,block_dest[i]);
56         }                                                        //
57         //////////////////////////////////////////////////////////*/
58 
59         ret_code = alt_read_flash(fd,FLASH_OFFSET,dest,BUF_SIZE);
60         if(ret_code != 0){
61             printf("Can't read flash device\n");
62             exit(-1);
63         }
64         else{
65             printf("Read Flash Device Successfully.\n");
66         }
67         //    Print the destination data
68         for(i=0;i<100;i++){
69             printf("OFFSET_Address %2d  is  %d\n",i,dest[i]);
70         }
71 
72         ret_code = alt_write_flash(fd,FLASH_OFFSET,source,BUF_SIZE);
73         if(ret_code != 0){
74             printf("Can't write flash device\n");
75             exit(-1);
76         }
77         else{
78             printf("Write Flash Device Successfully.\n");
79         }
80 
81         ret_code = alt_read_flash(fd,FLASH_OFFSET,dest,BUF_SIZE);
82         if(ret_code != 0){
83             printf("Can't read flash device\n");
84             exit(-1);
85         }
86         else{
87             printf("Read Flash Device Successfully.\n");
88         }
89 
90         for(i=0;i<100;i++){
91             printf("OFFSET_Address %2d  is  %d\n",i,dest[i]);
92         }
93         }
94     alt_flash_close_dev(fd);
95     printf("flash closed \n");
96 }

你可能感兴趣的:(FPGA)