IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    如何在NIOS II中读写EPCS剩余空间(转)

    techbulo发表于 2016-03-16 12:31:16
    love 0

    由于测试需要,网上收到的,直接贴代码,以备不时之需:

    
    #include <stdio.h>
    
    #include <unistd.h>
    
    #include "system.h"
    
    #include "alt_types.h"
    
    #include "sys/alt_flash.h"
    
    #include "sys/alt_flash_dev.h"
    
    alt_u8 epcsbuf[32];
    
    int ret_code;
    
    alt_flash_fd* my_epcs;//定义句柄
    
    main()
    
    {
    
    my_epcs = alt_flash_open_dev("/dev/epcs_controller");//打开FLASH器件,获取句柄
    
    ret_code = alt_epcs_flash_get_info (my_epcs, &regions, &number_of_regions);//获取配置芯片信息
    
    if(my_epcs) //信息获取成功
    
    {
    
    //example application, read general data from epcs address 0x70000
    
    ret_code = alt_epcs_flash_erase_block(my_epcs, regions->offset+0x70000);//擦除第8块
    
    ret_code = alt_epcs_flash_write(my_epcs, regions->offset+0x70000, epcsbuf, 32); //写32字节
    
    ret_code = alt_epcs_flash_read(my_epcs, regions->offset+0x70000, epcsbuf, 32); //读32字节
    
    }
    
    while(1)
    
    {
    
    }
    
    }
    
    

    上面的程序就是对EPCS配置芯片操作的流程和方式。首先打开器件获取句柄my_epcs,然后的读写及擦除操作都是通过句柄my_epcs来操作的。“/dev/epcs_controller”中的“epcs_controller”是用户在配置NIOS核时自命名的,可以在system.h中查到,即“EPCS_CONTROLLER_NAME”。通过IDE调试的话可以查看my_epcs指向的FLASH相关参数。EPCS器件只有一个区(regions),EPCS4区内有8个块(block),每个块是65536字节。(注:只有EPCS1每个块32768字节,其余配置芯片是每块65536字节。)

    alt_epcs_flash_erase_block(my_epcs, regions->offset+0x70000);//擦除第8块

    这个函数是擦除整块的函数,第一个参数是刚才获得的句柄,用于指示是对刚刚打开的FLASH进行擦除,第二个regions->offset的值其实是0,是由EPCS控制模块自己管理的。FPGA配置文件和NIOS核中的程序是从前边存储的,即从regions->offset+0x00000开始的地址。我的用掉不到4个块,还剩4个块可以用于自定义的存储。为便于将来进行功能扩展,尽量空余低块以备将来使用,优先使用高地址空间(第8块,起始地址为regions->offset+0x70000)进行用户配置信息存储。

    alt_epcs_flash_write(my_epcs, regions->offset+0x70000, epcsbuf, 32); //写32字节

    将epcsbuf数组中的连续32字节写入regions->offset+0x70000开始的EPCS4芯片内。如果是写入第6块的第0x100开始的地址,那么可用regions->offset+0x60100代替regions->offset+0x70000。

    alt_epcs_flash_read(my_epcs, regions->offset+0x70000, epcsbuf, 32); //读32字节

    从EPCS4中的第8段起点regions->offset+0x70000读取连续32字节存到epcsbuf数组内。

    调试过程中可以在执行alt_epcs_flash_write前手动修改epcsbuf数组内的值,在执行alt_epcs_flash_read前再更改epcsbuf数组内的值为其他值,如果执行alt_epcs_flash_read后epcsbuf数组内的值恢复到执行alt_epcs_flash_write前的值,那么对EPCS芯片的读写操作已经成功了!

    epcs

    epcs



沪ICP备19023445号-2号
友情链接