|
发表于 15-1-2007 02:27 AM
|
显示全部楼层
你们哦~ 在这里谈到津津有味~
还好我有来看看~ 呵呵
凯文。。。。。 你又一次摆我的名字上来啊。。。:@ :@ :@
很久没有见到你了。
GuoHui,你的问题噢。。。
我没有尝试过 不过你可以参考这个 nios flash read/write 程式
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "alt_types.h"
#include "sys/alt_flash.h"
#include "system.h"
#include "sys/alt_flash_dev.h"
#define NUM_BYTES_TO_WRITE 512
/*
* test_programming() is called by main to test a range of flash by
* writing incrementing patterns, then reading them back and comparing
* the result
* The start of the range to be tested is defined by test_offset, and
* the size of the range to be tested is defined by NUM_BYTES_TO_WRITE
*/
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);
/*
* 30 iterations takes about 60 seconds
*/
for (j=0;j<30;j++)
{
for(i=0;i<sizeof(data_written)/2;i++)
data_written = j*5;
for(i=sizeof(data_written)/2;i<sizeof(data_written);i++)
data_written = (j*5)+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%#x\n",
test_offset, j);
return ret_code;
}
}
}
printf("*";
if (ret_code)
{
printf( "\nERROR: function alt_write_flash failed. ret_code %d\n",
ret_code);
return ret_code;
}
}
return ret_code;
}
/*
* test_get_info() is called by main to test that the regions, sector
* size, block size and number of blocks can be correctly read from
* the flash
*/
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 %d\n",
ret_code);
}
/*
* If this is the development board check the number of regions etc.
*/
if (!strcmp("/dev/ext_flash_altera", fd->name))
{
if (number_of_regions != 1)
{
printf("\nERROR: number of regions is wrong\n";
ret_code = -EINVAL;
}
else if ( (regions->offset != 0) || (regions->region_size != 0x800000)
|| (regions->block_size != 0x10000) ||
(regions->number_of_blocks != 0x80))
{
printf("\nERROR: region info is wrong\n";
ret_code = -EINVAL;
}
}
else
{
printf("\n\rThis is www.icwin.net NIOSII Board Designed by flash Logic\n\r";
printf("Flash name %s\n\r",fd->name);
printf("This flash has %d erase regions\n\r", 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%8x\n\r",
(regions+i)->offset,
(regions+i)->region_size+(regions+i)->offset,
(regions+i)->number_of_blocks,
(regions+i)->block_size);
}
}
return ret_code;
}
/*
* Run various tests on a small section of the system flash.
*/
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;
fd = alt_flash_open_dev(EXT_FLASH_NAME);
if (fd)
{
printf("\n<----> Running Flash Tests <---->\n\r";
printf("-Testing flash info retrieval...";
ret_code = test_get_info(fd);
if (ret_code)
{
printf( "\n\rERROR: function test_get_info failed. ret_code %d\n\r",
ret_code);
goto finished;
}
printf(" passed.\n\r";
printf("-Testing flash write...\n\r";
printf(" 0x10000: ";
test_offset = 0x10000;
ret_code = test_programming(fd, test_offset);
if (ret_code)
goto finished;
printf(" passed.\n\r";
printf(" 0x1ff00: ";
test_offset = 0x1ff00;
ret_code = test_programming(fd, test_offset);
if (ret_code)
goto finished;
printf(" passed.\n\r";
printf(" 0x10100: ";
test_offset = 0x10100;
ret_code = test_programming(fd, test_offset);
if (ret_code)
goto finished;
printf(" passed.\n\r";
printf("-Testing flash block erase...";
ret_code = alt_erase_flash_block(fd, test_offset, 0x10000);
if (ret_code)
{
printf( "\n\rERROR: function alt_erase_flash_block failed. ret_code %d\n\r",
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 != 0xff)
{
printf("\n\rERROR: erase compare failed. %d %#x\n\r", i, read_data);
goto finished;
}
}
}
printf(" passed.\n\r";
printf("-Testing flash block write...";
for(i=0;i<100;i++)
write_data = i;
ret_code = alt_write_flash_block( fd, 0x10000,
test_offset, write_data,
100);
if (ret_code)
{
printf( "\n\rERROR: function alt_write_flash_block failed. ret_code %d\n\r",
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 != write_data)
{
printf( "\n\rERROR: compare failed, expected %#x read %#x\n\r",
write_data, read_data);
goto finished;
}
}
}
printf(" passed.\n\r";
test_offset = 0x10003;
printf("-Testing unaligned writes.....";
ret_code = alt_write_flash_block( fd, 0x10000,
test_offset, write_data,
100);
if (ret_code)
{
printf( "\n\rERROR: function alt_write_flash_block failed. ret_code %d\n\r",
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 != write_data)
{
printf( "\n\rERROR: compare failed, expected %#x read %#x\n\r",
write_data, read_data);
goto finished;
}
}
}
printf(" passed.\n\r";
printf("All Tests Passed!\n\r";
}
else
{
printf("Can't open the flash device\n\r";
}
finished:
alt_flash_close_dev(fd);
printf("Exiting Flash Tests\n\r";
return 0;
}
[ 本帖最后由 jefferylee 于 15-1-2007 02:31 AM 编辑 ] |
|