| | 网站首页 | 新闻 | SOPC | FPGA | DSP | ARM | 嵌入式操作系统 | 下载 | 网上商城 | 芯片价格参考 | 留言 | 论坛 | 网络协议 | 驱动设计 | 购买指南-HowtoBuy | | |
![]() |
![]() |
| 您现在的位置: FPGA开发板 FPGA 开发板 SOPC开发板 DSP开发板 视频开发板 -嵌入式控制研究室 >> SOPC >> Nios >> 文章正文 |
|
|||||
| nios flash read/write | |||||
| 作者:佚名 文章来源:互联网 点击数: 更新时间:2006-7-10 | |||||
|
#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[i] = j*5; for(i=sizeof(data_written)/2;i<sizeof(data_written);i++) data_written[i] = (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[i] != 0xff) { printf("\n\rERROR: erase compare failed. %d %#x\n\r", i, read_data[i]); goto finished; } } } printf(" passed.\n\r"); printf("-Testing flash block write..."); 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( "\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[i] != write_data[i]) { printf( "\n\rERROR: compare failed, expected %#x read %#x\n\r", write_data[i], read_data[i]); 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[i] != write_data[i]) { printf( "\n\rERROR: compare failed, expected %#x read %#x\n\r", write_data[i], read_data[i]); 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; } |
|||||
| 文章录入:fengfeiyi 责任编辑:fengfeiyi | |||||
| 【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 | |||||
| 最新热点 | 最新推荐 | 相关文章 | ||
| SOPC技术在电力机车改造中的… 基于SOPC技术的核信息远程采… 把SOPC Builder用于非Nios系… SOPC自定义模块的添加和接口… NiosII开发常见问题 应用SoPC Builder开发电子系… niosII初学者无痛起步 nios ii的 中断 sopc Flash Programmer,nios… 基于μClinux的SoPC应用系统… |
网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!) |
| | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 管理登录 | | |
![]() |
Copyright © 2005www.21control.com 嵌入式控制技术研究室 版权所有 站长:康草科技 |