NIOS II 软核中EPCS配置芯片的存储操作

来源:互联网 发布:拓扑图自动布局算法 编辑:程序博客网 时间:2024/05/16 12:23

                                                 NIOS II软核中EPCS配置芯片的存储操作

EPCS4配置芯片除了存储FPGA配置信息和NIOS II程序外,还有很多存储空间剩余未使用,可以用来做用户配置信息存储。

ALTERA提供的HAL函数来调用EPCS相关的函数,在“altera_avalon_epcs_flash_controller.c”函数(路径为:\\altera\72\ip\sopc_builder_ip\altera_avalon_epcs_flash_controller\HAL\src)中。

一个老外写的关于NIOS IICPCS器件操作的帖子,他是花了一个多月才摸索到怎么操作,然后给出了一个简单的例子。

先看一下这个简单程序

#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;

flash_region *regions;
int number_of_regions;

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_readepcsbuf数组内的值恢复到执行alt_epcs_flash_write前的值,那么对EPCS芯片的读写操作已经成功了

 注意:EPCS的擦写是针对整个块的,也可以不用擦写,直接写数据,因为重新写入时会发生覆盖。

 

EPCS操作相关的头文件有以下:

#include "system.h"

#include "alt_types.h"

#include "sys/alt_flash.h"

#include "sys/alt_flash_dev.h"

当然系统还需要一些相关的标准头文件,执行一些操作。

 

其他参考程序:

1First of all init the flash device and possibly get extra info about the flash device, like block size:

flash_region *regions;
int numRegions;
int error = 0;
pFlashDevice = alt_flash_open_dev(FLASH_CONTROLLER_NAME);
if (pFlashDevice <= 0)
error = -1;
if (!error)
error = alt_epcs_flash_get_info(pFlashDevice, &regions, &numRegions);
if (!error)

 {flash_block_size = regions->block_size;
flash_max_addr = regions->region_size;
This is for erasing a single block:
alt_epcs_flash_erase_block(pFlashDevice, block_address);
This is for writing any data to a previously erased area:
int buf[10] = { 1, 2,3,4,5,6,7,8,9,10 };
rv = alt_epcs_flash_write_block(pFlashDevice, block_address,
data_offset_inside_block , buf, 10);
Then, read back data with:
alt_epcs_flash_read(pFlashDevice, addr, buf, len);

 

问题:

First of all init the flash device and possibly get extra info about the flash device, like block size:

flash_region *regions;
int numRegions;
int error = 0;
pFlashDevice = alt_flash_open_dev(FLASH_CONTROLLER_NAME);
if (pFlashDevice <= 0)
error = -1;
if (!error)
error = alt_epcs_flash_get_info(pFlashDevice, &regions, &numRegions);
if (!error) {
flash_block_size = regions->block_size;
flash_max_addr = regions->region_size;

i am using cfi, is it the same? besides, my pFlashDevice is <= 0. i dunno why. is it sth to do with reset vector as my reset vector is sdram.

This is for writing any data to a previously erased area:
int buf[10] = { 1, 2,3,4,5,6,7,8,9,10 };
rv = alt_epcs_flash_write_block(pFlashDevice, block_address,
data_offset_inside_block , buf, 10);

why need rv when write data?

Then, read back data with:
alt_epcs_flash_read(pFlashDevice, addr, buf, len);

where is the data when we read it from the flash? like i want to print out the data.
please help me with the ques in bold. thanks!

 

 

回答:

why need rv when write data?
This is not mandatory, but recommended to check if data has been written correctlywhere is the data when we read it from the flash? like i want to print out the data.
???
What's the problem? The buf array contains your data.
Maybe this way is clearer:
int buf[10];
alt_flash_read(pFlashDevice, addr, buf, 10);Remark:
remove epcs_ from the previous post. I copied and pasted from a project using epcs instead of parallel flash and forgot to change function names.

 

注:此程序为altera论坛问答帖子,如何在DE2上使用flash的问题,具体链接地址为,http://www.alteraforum.com/forum/archive/index.php/t-27526.html

原创粉丝点击