您的位置:澳门新葡萄京最大平台 > www.4996.com > NiosII中Flash的使用(转)

NiosII中Flash的使用(转)

发布时间:2019-10-12 02:42编辑:www.4996.com浏览(182)

    NiosII中Flash的使用

           在嵌入式系统中,Flash是最常用组件之一。许多采纳过flash的朋友都打听,Flash的性状是“读来轻巧写来难”。经常,能够直接读出Flash的开始和结果;但要是要写入数据,将在发送一长串命令,举例像:555 ,AA,2AA,55,555,A0 ,PA,PD 就代表对PA地址写入数据PD,真实情状还要复杂一点,因为普通还要包括众多查询操作。

        哎呀,那真是好繁琐呀,有没有省力的方法吧?未来好了,NiosII的成本碰着提供了对相符CFI规范的Flash的支撑,使用多少个简易的函数,就可以操作Flash,真是便利了过多。

       在那处,作者大约得介绍一下nios 中flash的使用;越发详实的鼎力相助消息请您参照他事他说加以考察Altera公司提供的文书档案。如果本文能对你有一丢丢支持,笔者都会倍感特别快乐。

    一. 备选职业:

       在应用Flash从前,有多少个备选干活要做:

       1。开荒板上最少要有一片切合CFI标准的Flash。

       2。设计好对应指标板的Flash编制程序器(Flash Programmer)。

        下边笔者以本站设计的开荒板ezNiosDKC6B为目的板,来介绍怎么着使用Flash,你能够一隅三反,在和煦的种类中行使Flash.

    二. 为SOPC系统中增加Flash接口:

    1。   双击在Memory中的Flash Memory(Common Flash Interface),就可以为系统加多Flash接口。

    2。 对于ezNiosDK的顾客,Address Width能够挑选 20 Bits,Data Width 可以选择 16 Bits,这样总的体量是2M Bytes

        www.4996.com 1

    3。Timing选项卡中,能够如下添写:

        www.4996.com 2

       完结后,采纳Finish,就能够为系统增添Flash接口。

    4。扩大三态总线桥。Flash 接口必需透过三态总线桥接到实际的微芯片上。双击Bridges下的Avalon Tri-State Bridge,请依据下图设置,为系统扩充三态总线桥。

        www.4996.com 3

    5。为Flash接口分配引脚。请参见笔者事先的小说:Step By Step创建标准(Standard) niosII系统

        注意,如果Data Width是16Bits,那么tri_state_bridge_0_address[0]不供给接到Flash上,tri_state_bridge_0_address[1]对应Flash的A[www.4996.com,0],tri_state_bridge_0_address[2]对应Flash的A[1],就那样类推。

    三. 在Nios IDE中使用Flash编程器:

    3-1。配置Component/Kit Library Search Path。指标是在系统中追加本站设计的Flash编制程序器目录,请把光盘CD1上的/Example/ezNiosC6 拷贝到您的Computer的硬盘上。比方,笔者把她放在h:/DB二零零六/project/niosDK/CD/CD1/Example/中,然后在Altera SOPC Builder中,选择File -> SOPC Builder Setup,增添如下寻觅路线:+h:/DB二〇〇七/project/niosDK/CD/CD1/Example/ezNiosC6

    ezNiosC3的客商如下操作:请把光盘CD1上的/Example/ezNiosC3 拷贝到您的计算机的硬盘上。举个例子,小编把她位于h:/DB2006/project/niosDK/CD/CD1/Example/中,然后在Altera SOPC Builder中,选用File -> SOPC Builder Setup,扩展如下寻找路线:+h:/DB2006/project/niosDK/CD/CD1/Example/ezNiosC3

    并顺便检查Modelsim Directory是还是不是准确:比方,笔者的是 g:/w2k/eda/fpga/altera/modeltech_6.0/win32

    下一场,你必要关闭Altera SOPC Builder,然后再重复通过QuartusII的Tools ->SOPC Builder来开启SOPC Builder,那样上述修改工夫一蹴而就。

       www.4996.com 4

    大规模错误:有为数不菲相爱的人每每在安装路线的时候,犯与下图相近的失实。错误在于第3个渠道前边是不须要

    • 号的!要去掉第一个门路后面包车型地铁 + 号

    www.4996.com 5

    3-2:使用Flash编程器。

    自身动用项目:ezC6Be_StdF50_zip_filesystem_0(在CD1ExampleezNiosC6BezC6Be_StdF50softwareezC6Be_StdF50_zip_filesystem_0下,请使用Nios IDE中的File-Import来导入这些项目),来表明什么使用Flash编制程序器。

        首先为系统上电,并三番五次好下载电缆。

        选择Tools -> Flash Programmer,启动 Flash 编程器

        选择New,能够看来扩充了一个名称为 ezC6Be_StdF50_zip_filesystem_0 programmer的flash 编程器

    www.4996.com 6

       选取Program software project into flash memory,然后选取Apply,在甄选Program Flash,就运维了Flash 编制程序器!即使顺遂的话,能够看见如下的提示:

        www.4996.com 7

    假若见到如上提示,OK,小功告成,大家已经足以健康使用Flash编制程序器啦!

       下边作者来介绍怎么着在程序中援用Flash。

     使用NiosII提供的系统函数,能够方便得利用Flash,免除了家常操作Flash 的累赘,那对客户来讲,真是便利多了。

        Altera 提供了三种档案的次序的函数,提供给客商:Simple Flash Access(简单的Flash访问),以及Fine-Grained Flash Access(细粒度Flash访问)。

        常常景观下,小编要么引入应用Fine-Grained Flash Access(细粒度Flash访问)函数,比 Simple Flash Access也复杂不了多少,但足以制止经常的跨块擦除难点。因为Flash是遵循   Block组织起来的,经常一回擦除一整个块。即便写Flash的地方于Flash块的团伙结构不切合,比如胜过了Flash块的边缘,那么大概会擦除掉别的的数据。比方,就算要写入1Byte,也要擦除掉4Kbyte,恐怕那4Kbyte里面还会有相当多管用的数目,就能被抹掉。

        大家下边介绍多少个常用的函数,关于更详细的内容,请参谋Altera提供的文书档案。

        首先介绍第一步:张开Flash,似乎c程序张开硬盘中的数据文件同样,使用在此之前要开拓Flash.

    咱俩选用alt_flash_open_dev()打开Flash,它回到三个句柄。比方,下边是运用这些函数的片断:

    alt_flash_fd* fd;

    fd = alt_flash_open_dev(EXT_FLASH_NAME);

    其中,EXT_FLASH_NAME是预先定义的Flash的名字(#define EXT_FLASH_NAME "/dev/ext_flash")

    接下去的操作,都以通过该句柄:fd,来访谈Flash的。

        读出Flash使用函数:alt_read_flash,原型如下:

    int alt_read_flash( alt_flash_fd* fd,

          int offset,

          void* dest_addr,

          int length )

        使用完了,也别忘记关闭该Flash,就象读写完硬盘中的数据文件后要关张同样。其原型如下:

    void alt_flash_close_dev(alt_flash_fd* fd )

        Fine-Grained Flash Access体制提供了如下多少个函数:alt_get_flash_info(), alt_erase_flash_block(), alt_write_flash_block()。

       alt_get_flash_info()能够领取Flash的音讯,举例含有多少个区域,各区有多少个块,各样块的深浅等等。它的原型如下:

        int alt_get_flash_info( alt_flash_fd* fd,flash_region** info,int* number_of_regions)

    譬喻说,如下就是一个调用该函数的次序片断:

      int ret_code = 0;

      int number_of_regions=0;

      flash_region* regions;

      ret_code = alt_get_flash_info(fd, ®ions, &number_of_regions

     这里涉及到贰个结构:flash_region,原型如下:

    typedef struct flash_region

    {int offset;

    int region_size;

    int number_of_blocks;

    int block_size;

    }flash_region;

      擦除一个块使用函数:alt_flash_fd,函数原型如下:

    int alt_erase_flash_block( alt_flash_fd* fd,

               int offset,

               int length)

      写入三个块使用函数:alt_write_flash_block,函数原型如下:

    int alt_write_flash_block( alt_flash_fd* fd,

               int block_offset,

               int data_offset,

               const void *data,

               int length)

      罗罗嗦嗦说了这么多,上边我们来看一个实在的圭表;那个榜样是小编动用来测试flash的,相信我们能够从当中受益

    良多。

    #include <stdio.h>

    #include <errno.h>

    #include <string.h>

    #include "alt_types.h"

    #include "sys/alt_flash.h"

    #include "sys/alt_flash_dev.h"

    #define NUM_BYTES_TO_WRITE 512

    //#define NUM_BYTES_TO_WRITE 64

    int test_programming( alt_flash_fd* fd, int test_offset)

    {

    int i,j;

    alt_u8 data_written[NUM_BYTES_TO_WRITE];

    alt_u8 data_read[NUM_BYTES_TO_WRITE];

    int ret_code = 0;

    int test_length = sizeof(data_written);

     

    for (j=0;j<7;j++)

    {

    for(i=0;i<sizeof(data_written)/2;i++)

    data_written[i] = j*0x15;

    for(i=sizeof(data_written)/2;i<sizeof(data_written);i++)

    data_written[i] = (j*0x15)+1;

     

    ret_code = alt_write_flash(fd, test_offset, data_written, test_length);

    if (!ret_code)

    {

    ret_code = alt_read_flash(fd, test_offset, data_read, test_length);

    if(!ret_code)

    {

    if (memcmp(data_written, data_read, test_length))

    {

    printf( "nERROR: compare failed sector offset %#x iteration%#xn",

    test_offset, j);

    return ret_code;

    }

    }

    }

     

    printf("*");

    if (ret_code)

    {

    printf( "nERROR: function alt_write_flash failed. ret_code %dn",

    ret_code);

    return ret_code;

    }

    }

    return ret_code;

    }

    int test_flash_erase( alt_flash_fd* fd)

    {

    int ret_code = 0;

    int number_of_regions=0;

    flash_region* regions;

    int i,j,k;

    int test_offset;

    int test_length;

    alt_u8 read_data[200] ;

    ret_code = alt_get_flash_info(fd, ®ions, &number_of_regions);

    if (ret_code)

    {

    printf( "nERROR: function alt_get_flash_info failed. ret_code %dn",

    ret_code);

    }

     

    printf("nThis is AMD29LV160DBn");

    printf("Flash name %sn",fd->name);

     

    printf("This flash has %d erase regionsn", number_of_regions);

    for (i=0;i<number_of_regions;i++)

    {

    printf("Start 0x%8x End 0x%8x Number of Blocks %3d Block Size 0x%8xn",

    (regions+i)->offset,

    (regions+i)->region_size+(regions+i)->offset,

    (regions+i)->number_of_blocks,

    (regions+i)->block_size);

    }

     

    for(j=0;j<number_of_regions;j++)

    {

    for(i=0;i<((regions+j)->number_of_blocks);i++)

    { test_offset = (regions+j)->offset + i*(regions+j)->block_size;

    test_length = (regions+j)->block_size;

    printf(" ---Testing flash block erase...@ 0x%8x length= 0x%8x n",test_offset,test_length);

    ret_code = alt_erase_flash_block(fd, (regions+j)->offset + i*(regions+j)->block_size, (regions+j)->block_size);

    if (ret_code)

    {

    printf( "nERROR: function alt_erase_flash_block failed. ret_code %dn", ret_code);

    break;

    }

    else

    {

    ret_code = alt_read_flash(fd, test_offset, read_data, 100);

    for (k=0;k<100;k++)

    {

    if (read_data[k] != 0xff)

    {

    printf("nERROR: erase compare failed. %d %#xn", k, read_data[k]);

    break;

    }

    }

    }

    }//@for(i=1

    }//@for(j=1)

    printf(" passed.n");

    return ret_code;

    }

     

     

     

    int test_flash_write( alt_flash_fd* fd)

    {

    int ret_code = 0;

    int number_of_regions=0;

    flash_region* regions;

    int i,j,k;

    int test_offset;

    int test_length;

    alt_u8 read_data[200] ;

    alt_u8 write_data[200];

    ret_code = alt_get_flash_info(fd, ®ions, &number_of_regions);

    if (ret_code)

    {

    printf( "nERROR: function alt_get_flash_info failed. ret_code %dn",

    ret_code);

    }

     

    printf("nThis is AMD29LV160DBn");

    printf("Flash name %sn",fd->name);

     

    printf("This flash has %d erase regionsn", number_of_regions);

    for (i=0;i<number_of_regions;i++)

    {

    printf("Start 0x%8x End 0x%8x Number of Blocks %3d Block Size 0x%8xn",

    (regions+i)->offset,

    (regions+i)->region_size+(regions+i)->offset,

    (regions+i)->number_of_blocks,

    (regions+i)->block_size);

    }

     

    for(j=0;j<number_of_regions;j++)

    {

    for(i=0;i<((regions+j)->number_of_blocks);i++)

    { test_offset = (regions+j)->offset + i*(regions+j)->block_size;

    test_length = (regions+j)->block_size;

    printf(" ---Testing flash block erase...@ 0x%8x length= 0x%8x n",test_offset,test_length);

    ret_code = alt_erase_flash_block(fd, (regions+j)->offset + i*(regions+j)->block_size, (regions+j)->block_size);

    if (ret_code)

    {

    printf( "nERROR: function alt_erase_flash_block failed. ret_code %dn", ret_code);

    break;

    }

    else

    {

    ret_code = alt_read_flash(fd, test_offset, read_data, 100);

    for (k=0;k<100;k++)

    {

    if (read_data[k] != 0xff)

    {

    printf("nERROR: erase compare failed. %d %#xn", k, read_data[k]);

    break;

    }

    }//@for (k=0)

     

     

    printf(" -----------Now Testing flash block write...@ 0x%8x n",test_offset);

     

    for(k=0;k<100;k++)

    write_data[k] = k;

     

    ret_code = alt_write_flash_block( fd, (regions+j)->offset + i*(regions+j)->block_size,

    test_offset

    • i, write_data, 100);

    if (ret_code)

    {

    printf( "nERROR: function aXlt_write_flash_block failed. ret_code %dn", ret_code);

    break;

    }

    else

    { ret_code = alt_read_flash(fd, test_offset + i, read_data, 100);

    for (k=0;k<100;k++)

    {

    if (read_data[k] != write_data[k])

    {printf( "nERROR: compare failed, expected %#x read %#xn",write_data[i], read_data[i]);

    break;

    }

    }

    }//@ else

     

    }

    }//@for(i=1

    }//@for(j=1)

    printf(" passed.n");

    return ret_code;

    }

     

     

     

     

    int test_get_info( alt_flash_fd* fd)

    {

    int ret_code = 0;

    int number_of_regions=0;

    flash_region* regions;

    int i;

    ret_code = alt_get_flash_info(fd, ®ions, &number_of_regions);

    if (ret_code)

    {

    printf( "nERROR: function alt_get_flash_info failed. ret_code %dn",

    ret_code);

    }

     

    if (0)

    {

     

    }

    else

    {

    printf("nThis is not the standard reference designn");

    printf("Flash name %sn",fd->name);

     

    printf("This flash has %d erase regionsn", number_of_regions);

    for (i=0;i<number_of_regions;i++)

    {

    printf("Start 0x%8x End 0x%8x Number of Blocks %3d Block Size 0x%8xn",

    (regions+i)->offset,

    (regions+i)->region_size+(regions+i)->offset,

    (regions+i)->number_of_blocks,

    (regions+i)->block_size);

    }

    }

    return ret_code;

    }

    int main (void)

    {

    int ret_code;

    int test_offset;

    alt_flash_fd* fd;

    alt_u8 write_data[100];

    alt_u8 read_data[100];

    int i,j;

     

    fd = alt_flash_open_dev(EXT_FLASH_NAME);

     

    if (fd)

    {

    printf("nnn<----> Running Flash Tests <---->n");

    printf("This will take approximately 1 minutenn");

     

    printf("-Testing flash info retrieval...");

    ret_code = test_get_info(fd);

    if (ret_code)

    {

    printf( "nERROR: function test_get_info failed. ret_code %dn",

    ret_code);

    goto finished;

    }

    printf(" passed.n");

     

    printf("-Testing flash block erase,write...n");

    ret_code = test_flash_write(fd);

    if (ret_code)

    {

    printf( "nERROR: test_flash_erase failed. ret_code %dn",

    ret_code);

    goto finished;

    }

    printf(" passed.n");

     

     

     

    printf("-Testing flash write...n");

     

    test_offset = 0x0;

    printf("n test_offset=0x%8x ",test_offset);

    ret_code = test_programming(fd, test_offset);

    if (ret_code)

    goto finished;

    printf(" passed.n");

     

    test_offset = 0x4000;

    printf("n test_offset=0x%8x ",test_offset);

    ret_code = test_programming(fd, test_offset);

    if (ret_code)

    goto finished;

    printf(" passed.n");

     

    test_offset = 0x6000;

    printf("n test_offset=0x%8x ",test_offset);

    ret_code = test_programming(fd, test_offset);

    if (ret_code)

    goto finished;

    printf(" passed.n");

     

    test_offset = 0x8000;

    printf("n test_offset=0x%8x ",test_offset);

    ret_code = test_programming(fd, test_offset);

    if (ret_code)

    goto finished;

    printf(" passed.n");

     

    for(j=1;j<=31;j++)

    {test_offset = 0x10000*j;

    printf("n test_offset=0x%8x ",test_offset);

    ret_code = test_programming(fd, test_offset);

    if (ret_code)

    goto finished;

    }

    printf(" passed.n");

     

     

    printf(" 0x10ff00: ");

    test_offset = 0x10ff00;

    ret_code = test_programming(fd, test_offset);

    if (ret_code)

    goto finished;

    printf(" passed.n");

     

     

    printf(" 0x100100: ");

    test_offset = 0x100100;

    ret_code = test_programming(fd, test_offset);

    if (ret_code)

    goto finished;

    printf(" passed.n");

    printf("-Testing flash block erase...n");

    ret_code = test_flash_erase(fd);

    if (ret_code)

    {

    printf( "nERROR: test_flash_erase failed. ret_code %dn",

    ret_code);

    goto finished;

    }

    printf(" passed.n");

    test_offset = 0x10000;

    printf(" ---Testing flash block write...@ 0x%8x n",test_offset);

     

    for(i=0;i<100;i++)

    write_data[i] = i;

     

    ret_code = alt_write_flash_block( fd, 0x10000,

    test_offset, write_data,

    100);

    if (ret_code)

    {

    printf( "nERROR: function alt_write_flash_block failed. ret_code %dn",

    ret_code);

    goto finished;

    }

    else

    {

    ret_code = alt_read_flash(fd, test_offset, read_data, 100);

    for (i=0;i<100;i++)

    {

    if (read_data[i] != write_data[i])

    {

    printf( "nERROR: compare failed, expected %#x read %#xn",

    write_data[i], read_data[i]);

    goto finished;

    }

    }

    }

    printf(" passed.n");

     

    test_offset = 0x10010;

    printf("-Testing unaligned writes.....");

    alt_erase_flash_block(fd, 0x10000, 0x100000);

     

    ret_code = alt_write_flash_block( fd, 0x10000,

    test_offset, write_data,

    100);

     

    if (ret_code)

    {

    printf( "nERROR: function alt_write_flash_block failed. ret_code %dn",

    ret_code);

    goto finished;

    }

    else

    {

    ret_code = alt_read_flash(fd, test_offset, read_data, 100);

    for (i=0;i<100;i++)

    {

    if (read_data[i] != write_data[i])

    {

    printf( "nERROR: compare failed, expected %#x read %#xn",

    write_data[i], read_data[i]);

    goto finished;

    }

    }

    }

    printf(" passed.n");

     

    printf("-Testing flash block erase...n");

    ret_code = test_flash_erase(fd);

    if (ret_code)

    {

    printf( "nERROR: test_flash_erase failed. ret_code %dn",

    ret_code);

    goto finished;

    }

    printf(" passed.n");

     

     

    printf("All Tests Passed!n");

    }

    else

    {

    printf("Can't open the flash devicen");

    }

    finished:

    alt_flash_close_dev(fd);

    printf("Exiting Flash Testsn");

     

    return 0;

    }

    本文由澳门新葡萄京最大平台发布于www.4996.com,转载请注明出处:NiosII中Flash的使用(转)

    关键词:

上一篇:技术干货

下一篇:没有了